File: transaction_item.cpp

package info (click to toggle)
dnf5 5.4.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 17,960 kB
  • sloc: cpp: 94,312; python: 3,370; xml: 1,073; ruby: 600; sql: 250; ansic: 232; sh: 104; perl: 62; makefile: 30
file content (228 lines) | stat: -rw-r--r-- 6,311 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// Copyright Contributors to the DNF5 project.
// Copyright Contributors to the libdnf project.
// SPDX-License-Identifier: LGPL-2.1-or-later
//
// This file is part of libdnf: https://github.com/rpm-software-management/libdnf/
//
// Libdnf is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 2.1 of the License, or
// (at your option) any later version.
//
// Libdnf is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with libdnf.  If not, see <https://www.gnu.org/licenses/>.


#include "libdnf5/transaction/transaction_item.hpp"

#include "libdnf5/transaction/transaction.hpp"
#include "libdnf5/transaction/transaction_item_action.hpp"


namespace libdnf5::transaction {

class TransactionItem::Impl {
public:
    Impl(const Transaction & trans);

private:
    friend TransactionItem;

    int64_t id = 0;
    Action action = Action::INSTALL;
    Reason reason = Reason::NONE;
    State state = State::STARTED;
    std::string repoid;
    int64_t item_id = 0;

    // TODO(lukash) this won't be safe in bindings (or in general when a
    // TransactionItem is kept around after a Transaction is destroyed), but we
    // can't easily use a WeakPtr here, since the Transactions are expected to
    // be at least movable, and the WeakPtrGuard would make the Transaction
    // unmovable
    const Transaction * trans = nullptr;
};

TransactionItem::~TransactionItem() = default;

TransactionItem::TransactionItem(const TransactionItem & mpkg) : p_impl(new Impl(*mpkg.p_impl)) {}
TransactionItem::TransactionItem(TransactionItem && mpkg) noexcept = default;

TransactionItem & TransactionItem::operator=(const TransactionItem & mpkg) {
    if (this != &mpkg) {
        if (p_impl) {
            *p_impl = *mpkg.p_impl;
        } else {
            p_impl = std::make_unique<Impl>(*mpkg.p_impl);
        }
    }

    return *this;
}
TransactionItem & TransactionItem::operator=(TransactionItem && mpkg) noexcept = default;

TransactionItem::Impl::Impl(const Transaction & trans) : trans(&trans) {}

std::string TransactionItem::get_action_name() {
    return transaction_item_action_to_string(p_impl->action);
}


std::string TransactionItem::get_action_short() {
    return transaction_item_action_to_letter(p_impl->action);
}


TransactionItem::TransactionItem(const Transaction & trans) : p_impl(std::make_unique<Impl>(trans)) {}


bool TransactionItem::is_inbound_action() const {
    return transaction_item_action_is_inbound(p_impl->action);
}


bool TransactionItem::is_outbound_action() const {
    return transaction_item_action_is_outbound(p_impl->action);
}


const Transaction & TransactionItem::get_transaction() const {
    libdnf_assert(p_impl->trans, "Transaction in TransactionItem was not set.");
    return *p_impl->trans;
}

TransactionItemAction TransactionItem::get_action() const noexcept {
    return p_impl->action;
}
TransactionItemReason TransactionItem::get_reason() const noexcept {
    return p_impl->reason;
}
const std::string & TransactionItem::get_repoid() const noexcept {
    return p_impl->repoid;
}
TransactionItemState TransactionItem::get_state() const noexcept {
    return p_impl->state;
}

int64_t TransactionItem::get_id() const noexcept {
    return p_impl->id;
}

void TransactionItem::set_id(int64_t value) {
    p_impl->id = value;
}

void TransactionItem::set_action(Action value) {
    p_impl->action = value;
}

void TransactionItem::set_reason(Reason value) {
    p_impl->reason = value;
}

void TransactionItem::set_state(State value) {
    p_impl->state = value;
}

void TransactionItem::set_repoid(const std::string & value) {
    p_impl->repoid = value;
}

int64_t TransactionItem::get_item_id() const noexcept {
    return p_impl->item_id;
}

void TransactionItem::set_item_id(int64_t value) {
    p_impl->item_id = value;
}

/*
void
TransactionItem::saveReplacedBy()
{
    if (replacedBy.empty()) {
        return;
    }
    const char *sql = "INSERT OR REPLACE INTO item_replaced_by VALUES (?, ?)";
    libdnf5::utils::SQLite3::Statement replacedByQuery(trans.get_connection(), sql);
    bool first = true;
    for (const auto &newItem : replacedBy) {
        if (!first) {
            // reset the prepared statement, so it can be executed again
            replacedByQuery.reset();
        }
        replacedByQuery.bindv(get_id(), newItem->get_id());
        replacedByQuery.step();
        first = false;
    }
}

void
TransactionItem::saveState()
{
    const char *sql = R"**(
        UPDATE
          trans_item
        SET
          state = ?
        WHERE
          id = ?
    )**";

    libdnf5::utils::SQLite3::Statement query(trans.get_connection(), sql);
    query.bindv(static_cast< int >(get_state()), get_id());
    query.step();
}

void
TransactionItem::dbUpdate()
{
    const char *sql = R"**(
        UPDATE
          trans_item
        SET
          trans_id=?,
          item_id=?,
          repo_id=?,
          action=?,
          reason=?,
          state=?
        WHERE
          id = ?
    )**";

    // try to find an existing repo
    auto query_repo_select_pkg = repo_select_pk_new_query(trans.get_connection());
    auto repo_id = repo_select_pk(*query_repo_select_pkg, get_repoid());

    if (!repo_id) {
        // if an existing repo was not found, insert a new record
        auto query_repo_insert = repo_insert_new_query(trans.get_connection());
        repo_id = repo_insert(*query_repo_insert, get_repoid());
    }


    libdnf5::utils::SQLite3::Statement query(trans.get_connection(), sql);
    query.bindv(trans.get_id(),
                getItem()->getId(),
                repo_id,
                static_cast< int >(get_action()),
                static_cast< int >(get_reason()),
                static_cast< int >(get_state()),
                get_id());
    query.step();
}

uint32_t
TransactionItem::getInstalledBy() const {
    return trans.get_user_id();
}
*/


}  // namespace libdnf5::transaction