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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#pragma once
///@file

#include <chrono>
#include <kj/async.h>
#include <string>
#include <type_traits>

#include "lix/libutil/async.hh"
#include "lix/libutil/result.hh"
#include "lix/libutil/types.hh"
#include "lix/libutil/error.hh"

struct sqlite3;
struct sqlite3_stmt;

namespace nix {

enum class SQLiteOpenMode {
    /**
     * Open the database in read-write mode.
     * If the database does not exist, it will be created.
     */
    Normal,
    /**
     * Open the database in read-write mode.
     * Fails with an error if the database does not exist.
     */
    NoCreate,
    /**
     * Open the database in immutable mode.
     * In addition to the database being read-only,
     * no wal or journal files will be created by sqlite.
     * Use this mode if the database is on a read-only filesystem.
     * Fails with an error if the database does not exist.
     */
    Immutable,
};

enum class SQLiteTxnType {
    /**
     * A deferred transaction does not actually begin until the database is first accessed.
     * If the first statement in the transaction is a SELECT then a read transaction is started.
     * Subsequent write statements will upgrade the transaction to a write transaction if possible,
     * or return SQLITE_BUSY if another write transaction started on another database connection.
     * If the first statement in the transaction is a write statement then a write transaction
     * is started.
     */
    Deferred,
    /**
     * An immediate transaction causes the database to start a write transaction immediately,
     * without waiting for a write statement. The transaction might fail wth SQLITE_BUSY if another
     * write transaction is already active on another database connection.
     */
    Immediate,
    /**
     * An exclusive transaction causes the database to start a write transaction immediately.
     * In WAL mode this is the same as Immediate, but in other journaling modes this prevents
     * other database connections from reading the database while a transction is underway.
     */
    Exclusive,
};

struct SQLiteError;
class SQLiteStmt;
class SQLiteTxn;

/**
 * RAII wrapper to close a SQLite database automatically.
 */
class SQLite
{
    friend SQLiteError;

    struct Close {
        void operator()(sqlite3 * db);
    };
    std::unique_ptr<sqlite3, Close> db;

public:
    SQLite() = default;
    SQLite(const Path & path, SQLiteOpenMode mode = SQLiteOpenMode::Normal);

    /**
     * Disable synchronous mode, set truncate journal mode.
     */
    void isCache();

    void exec(const std::string & stmt, NeverAsync = {});

    SQLiteStmt create(const std::string & stmt);

    SQLiteTxn beginTransaction(SQLiteTxnType type = SQLiteTxnType::Deferred);

    void setPersistWAL(bool persist);

    uint64_t getLastInsertedRowId();
    uint64_t getRowsChanged();
};

/**
 * RAII wrapper to create and destroy SQLite prepared statements.
 */
class SQLiteStmt
{
    friend SQLite;

    struct Finalize {
        SQLiteStmt * parent;
        void operator()(sqlite3_stmt * stmt);
    };

    sqlite3 * db = 0;
    std::unique_ptr<sqlite3_stmt, Finalize> stmt;
    std::string sql;

    SQLiteStmt(sqlite3 * db, const std::string & sql);

public:
    SQLiteStmt() = default;

    /**
     * Helper for binding / executing statements.
     */
    class Use
    {
        friend class SQLiteStmt;
    private:
        SQLiteStmt & stmt;
        unsigned int curArg = 1;
        Use(SQLiteStmt & stmt);

    public:

        ~Use();

        /**
         * Bind the next parameter.
         */
        Use & operator () (std::string_view value, bool notNull = true);
        Use & operator () (const unsigned char * data, size_t len, bool notNull = true);
        Use & operator () (int64_t value, bool notNull = true);
        Use & bind(); // null

        /**
         * Execute a statement that does not return rows.
         */
        void exec();

        /**
         * For statements that return 0 or more rows. Returns true iff
         * a row is available.
         */
        bool next();

        std::string getStr(int col);
        std::optional<std::string> getStrNullable(int col);
        int64_t getInt(int col);
        bool isNull(int col);
    };

    Use use()
    {
        return Use(*this);
    }
};

/**
 * RAII helper that ensures transactions are aborted unless explicitly
 * committed.
 */
class SQLiteTxn
{
    friend SQLite;

    struct Rollback {
        void operator()(sqlite3 * db);
    };
    std::unique_ptr<sqlite3, Rollback> db;

    explicit SQLiteTxn(sqlite3 * db, SQLiteTxnType type);

public:
    void commit();
};


struct SQLiteError : Error
{
    friend SQLite;
    friend SQLiteStmt;
    friend SQLiteTxn;

    std::string path;
    std::string errMsg;
    int errNo, extendedErrNo, offset;

    SQLiteError(const char *path, const char *errMsg, int errNo, int extendedErrNo, int offset, HintFmt && hf);

protected:

    template<typename... Args>
    SQLiteError(const char *path, const char *errMsg, int errNo, int extendedErrNo, int offset, const std::string & fs, const Args & ... args)
      : SQLiteError(path, errMsg, errNo, extendedErrNo, offset, HintFmt(fs, args...))
    { }

    template<typename... Args>
    [[noreturn]] static void throw_(sqlite3 * db, const std::string & fs, const Args & ... args) {
        throw_(db, HintFmt(fs, args...));
    }

    [[noreturn]] static void throw_(sqlite3 * db, HintFmt && hf);

};

MakeError(SQLiteBusy, SQLiteError);

void handleSQLiteBusy(const SQLiteBusy & e, std::chrono::time_point<std::chrono::steady_clock> & nextWarning);
kj::Promise<Result<void>> handleSQLiteBusyAsync(const SQLiteBusy & e, std::chrono::time_point<std::chrono::steady_clock> & nextWarning);

/**
 * Convenience function for retrying a SQLite transaction when the
 * database is busy.
 */
template<typename F>
    requires(!requires(F f) { []<typename T>(kj::Promise<T>) {}(f()); })
auto retrySQLite(F fun, NeverAsync = {})
{
    auto nextWarning = std::chrono::steady_clock::now() + std::chrono::seconds(1);

    while (true) {
        try {
            return fun();
        } catch (SQLiteBusy & e) {
            handleSQLiteBusy(e, nextWarning);
        }
    }
}

template<typename F>
    requires requires(F f) { []<typename T>(kj::Promise<Result<T>>) {}(f()); }
auto retrySQLite(F fun)
{
    return [](F fun) -> decltype(fun()) {
        auto nextWarning = std::chrono::steady_clock::now() + std::chrono::seconds(1);

        while (true) {
            kj::Promise<Result<void>> handleBusy{nullptr};
            try {
                if constexpr (std::is_same_v<decltype(fun()), kj::Promise<Result<void>>>) {
                    LIX_TRY_AWAIT(fun());
                    co_return result::success();
                } else {
                    co_return LIX_TRY_AWAIT(fun());
                }
            } catch (SQLiteBusy & e) {
                handleBusy = handleSQLiteBusyAsync(e, nextWarning);
            } catch (...) {
                co_return result::current_exception();
            }
            LIX_TRY_AWAIT(handleBusy);
        }
    }(std::move(fun));
}
}