summaryrefslogtreecommitdiffstats
path: root/library/cpp/sqlite3/sqlite.h
blob: 8b35e2606a1d59e08a6c2d9d336c7d96b45dede6 (plain) (blame)
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
#pragma once

#include <util/generic/yexception.h>
#include <util/generic/ptr.h>

#include <contrib/libs/sqlite3/sqlite3.h>

namespace NSQLite {
    class TSQLiteError: public yexception {
    public:
        TSQLiteError(sqlite3* hndl);
        TSQLiteError(int rc);

        int GetErrorCode() const {
            return ErrorCode;
        }

    private:
        int ErrorCode;
    };

    template <class T, int (*Func)(T*)>
    struct TCFree {
        static void Destroy(T* t) {
            Func(t);
        }
    };

    class TSQLiteDB {
    public:
        TSQLiteDB(const TString& path, int flags);
        TSQLiteDB(const TString& path);

        sqlite3* Handle() const noexcept;
        size_t RowsAffected() const noexcept;

    private:
        THolder<sqlite3, TCFree<sqlite3, sqlite3_close>> H_;
    };

    class ISQLiteColumnVisitor {
    public:
        virtual ~ISQLiteColumnVisitor() = default;

        virtual void OnColumnInt64(i64 value) = 0;
        virtual void OnColumnDouble(double value) = 0;
        virtual void OnColumnText(TStringBuf value) = 0;
        virtual void OnColumnBlob(TStringBuf value) = 0;
        virtual void OnColumnNull() = 0;
    };

    class TSQLiteStatement {
    public:
        TSQLiteStatement(TSQLiteDB& db, const TString& s);

        void Execute();
        TSQLiteStatement& Bind(size_t idx, i64 val);
        TSQLiteStatement& Bind(size_t idx, int val);
        TSQLiteStatement& Bind(size_t idx);
        TSQLiteStatement& Bind(size_t idx, double val);
        TSQLiteStatement& Bind(size_t idx, TStringBuf str);
        TSQLiteStatement& BindBlob(size_t idx, TStringBuf blob);
        template <typename Value>
        TSQLiteStatement& Bind(TStringBuf name, Value val) {
            size_t idx = BoundNamePosition(name);
            Y_ASSERT(idx > 0);
            return Bind(idx, val);
        }
        TSQLiteStatement& BindBlob(TStringBuf name, TStringBuf blob) {
            size_t idx = BoundNamePosition(name);
            Y_ASSERT(idx > 0);
            return BindBlob(idx, blob);
        }
        TSQLiteStatement& Bind(TStringBuf name) {
            size_t idx = BoundNamePosition(name);
            Y_ASSERT(idx > 0);
            return Bind(idx);
        }
        size_t BoundNamePosition(TStringBuf name) const noexcept;
        size_t BoundParameterCount() const noexcept;
        const char* BoundParameterName(size_t idx) const noexcept;

        sqlite3_stmt* Handle() const noexcept;
        bool Step();
        i64 ColumnInt64(size_t idx);
        double ColumnDouble(size_t idx);
        TStringBuf ColumnText(size_t idx);
        TStringBuf ColumnBlob(size_t idx);
        void ColumnAccept(size_t idx, ISQLiteColumnVisitor& visitor);
        size_t ColumnCount() const noexcept;
        TStringBuf ColumnName(size_t idx) const noexcept;
        void Reset();
        // Ignore last error on this statement
        void ResetHard();
        void ClearBindings() noexcept;

    private:
        typedef void (*TFreeFunc)(void*);
        void BindText(size_t col, const char* text, size_t len, TFreeFunc func);

    private:
        TString S_;
        THolder<sqlite3_stmt, TCFree<sqlite3_stmt, sqlite3_finalize>> H_;
    };

    /**
     * Forces user to commit transaction explicitly, to not get exception in destructor (with all consequences of it).
     */
    class TSQLiteTransaction: private TNonCopyable {
    private:
        TSQLiteDB* Db;

    public:
        TSQLiteTransaction(TSQLiteDB& db);
        ~TSQLiteTransaction();

        void Commit();
        void Rollback();

    private:
        void Execute(const TString& query);
    };

    class TSimpleDB: public TSQLiteDB {
    public:
        TSimpleDB(const TString& path);

        void Execute(const TString& statement);
        void Acquire();
        void Release();

    private:
        TSQLiteStatement Start_;
        TSQLiteStatement End_;
    };
}