aboutsummaryrefslogtreecommitdiffstats
path: root/include/sqdb.hpp
blob: b92cac6f27a1f711d55fb82ed49e6595ec14e8c7 (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
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
#pragma once

#include <string>

#include "sqlite3.h"

#ifdef _WIN32
#  include <tchar.h>
#  define SQDB_MAKE_TEXT(x) _TEXT(x)
#  define SQDB_STRLEN _tcslen
#  define SQDB_STRDUP _tcsdup
#else
#  define SQDB_MAKE_TEXT(x) (x)
#  define SQDB_STRLEN strlen
#  define SQDB_STRDUP strdup
#endif

#if !defined(SQDB_UTF16) && !defined(SQDB_UTF8)
#  ifdef _WIN32
#    if defined(UNICODE) || defined(_UNICODE)
#      define SQDB_UTF16
#    else
#      define SQDB_UTF8
#    endif
#  else
#    define SQDB_UTF8
#  endif
#endif

#ifdef SQDB_UTF8
#  define SQDB_CHAR char
#  define SQDB_STD_STRING std::string
#endif

#ifdef SQDB_UTF16
#  define SQDB_CHAR TCHAR
#  define SQDB_STD_STRING std::wstring
#endif

namespace sqdb {

    class Exception {
    public:
        Exception(sqlite3 *db);

        Exception(sqlite3 *db, int errorCode);

        Exception(const SQDB_CHAR *errorMsg);

        ~Exception();

        int GetErrorCode() const;

        const SQDB_CHAR *GetErrorMsg() const;

    private:
        int m_errorCode;
        SQDB_CHAR *m_errorMsg;
    };

#define CHECK(db, returnCode) \
  if ( (returnCode) != SQLITE_OK ) throw Exception(db, returnCode)

    class RefCount {
    protected:
        RefCount();

        RefCount(const RefCount &x);

        RefCount &operator=(const RefCount &x);

        void IncRef();

        unsigned DecRef();

    private:
        unsigned *m_refCount;
    };

    class Blob : public RefCount {
    public:
        Blob(const void *data, int size);

        Blob(const Blob &x);

        Blob &operator=(const Blob &x);

        int GetSize() const;

        const char *GetData() const;

        ~Blob();

    private:
        char *m_data;
        int m_size;
    };

    class Convertor {
    public:
        Convertor(sqlite3 *db, sqlite3_stmt *stmt, int field);

        operator int() const;

        operator unsigned long() const;

        operator long long() const;

        operator double() const;

        operator SQDB_STD_STRING() const;

        operator const SQDB_CHAR *() const;

        operator Blob() const;

        int GetInt() const;

        unsigned long GetUnsignedLong() const;

        long long GetLongLong() const;

        double GetDouble() const;

        SQDB_STD_STRING GetString() const;

        const SQDB_CHAR *GetText() const;

        Blob GetBlob() const;

    private:
        sqlite3 *m_db;
        sqlite3_stmt *m_stmt;
        int m_field;
    };

    class Statement : public RefCount {
    public:
        Statement(sqlite3 *db, sqlite3_stmt *stmt);

        Statement(const Statement &x);

        Statement &operator=(const Statement &x);

        bool Next();

        Convertor GetField(int field) const;

        template<class T>
        void Bind(int i, const T &value) {
            if (m_needReset)
                Reset();
            DoBind(i, value);
        }

        void BindBlob(int i, const void *value, int n);

        void BindNull(int i);

        ~Statement();

    private:
        void DoBind(int i, int value);

        void DoBind(int i, long long value);

        void DoBind(int i, double value);

        void DoBind(int i, const SQDB_STD_STRING &value);

        void DoBind(int i, const SQDB_CHAR *value);

        // Bind blob.
        void DoBind(int i, const void *value, int n);

        // Bind null.
        void DoBind(int i);

        // Reset binders so that new values can be bound.
        void Reset();

        sqlite3 *m_db;
        sqlite3_stmt *m_stmt;
        bool m_needReset;
    };

    class QueryStr {
    public:
        QueryStr();

        const SQDB_CHAR *Format(const SQDB_CHAR *fmt, ...);

        const SQDB_CHAR *Get() const;

        ~QueryStr();

    private:
        SQDB_CHAR *m_buf;
    };

    class Db : public RefCount {
    public:
        Db(const SQDB_CHAR *fileName);

        void BeginTransaction();

        void CommitTransaction();

        void RollbackTransaction();

        bool TableExists(const SQDB_CHAR *tableName);

        Statement Query(const SQDB_CHAR *queryStr);

        long long LastId();

        Db(const Db &x);

        Db &operator=(const Db &x);

        ~Db();

    private:
        sqlite3 *m_db;
    };

}