aboutsummaryrefslogtreecommitdiffstats
path: root/src/Utilities/sqdb.cpp
blob: 0b88139d90358071ad8aa5e96be7e9b3f0a25fb2 (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
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
#include <cassert>
#include <cstdio>
#include <cstring>
#include <memory>

#include <cstdlib>

#include "sqdb.hpp"

#include <unistd.h>

using namespace sqdb;

Exception::Exception(sqlite3* db)
{
    m_errorCode = sqlite3_errcode(db);
    auto* c = (SQDB_CHAR*)
#ifdef SQDB_UTF8
            sqlite3_errmsg
#else
            sqlite3_errmsg16
#endif
                    (db);
    m_errorMsg = SQDB_STRDUP(c);
}

Exception::Exception(sqlite3* db, int errorCode)
        : m_errorCode(errorCode)
{
    auto* c = (SQDB_CHAR*)
#ifdef SQDB_UTF8
            sqlite3_errmsg
#else
            sqlite3_errmsg16
#endif
                    (db);
    m_errorMsg = SQDB_STRDUP(c);
}

Exception::Exception(const SQDB_CHAR* errorMsg)
        : m_errorCode(-1)
{
    m_errorMsg = SQDB_STRDUP(errorMsg);
}

Exception::~Exception()
{
    free(m_errorMsg);
}

int Exception::GetErrorCode() const
{
    return m_errorCode;
}

const SQDB_CHAR* Exception::GetErrorMsg() const
{
    return m_errorMsg;
}

RefCount::RefCount()
        : m_refCount(nullptr)
{
}

RefCount::RefCount(const RefCount& x)
        : m_refCount(x.m_refCount)
{
}

RefCount& RefCount::operator=(const RefCount& x)
{
    if ( this != &x )
    {
        m_refCount = x.m_refCount;
    }
    return *this;
}

void RefCount::IncRef()
{
    if ( !m_refCount )
    {
        m_refCount = new unsigned;
        *m_refCount = 0;
    }
    ++*m_refCount;
}

unsigned RefCount::DecRef()
{
    assert(m_refCount);
    unsigned value = --*m_refCount;
    if ( value == 0 )
    {
        delete m_refCount;
        m_refCount = nullptr;
    }
    return value;
}

Blob::Blob(const void* data, int size)
        : m_size(size)
{
    m_data = new char[size];
    std::uninitialized_copy((char*)data, (char*)data + size, m_data);
    IncRef();
}

Blob::Blob(const Blob& x)
        : RefCount(x), m_data(x.m_data), m_size(x.m_size)
{
    IncRef();
}

Blob& Blob::operator=(const Blob& x)
{
    if ( this != &x )
    {
        RefCount::operator=(x);
        IncRef();
        m_data = x.m_data;
        m_size = x.m_size;
    }
    return *this;
}

int Blob::GetSize() const
{
    return m_size;
}

const char* Blob::GetData() const
{
    return m_data;
}

Blob::~Blob()
{
    if ( DecRef() == 0 )
    {
        delete[] m_data;
    }
}

Convertor::Convertor(sqlite3* db, sqlite3_stmt* stmt, int field)
        : m_db(db), m_stmt(stmt), m_field(field)
{
}

Convertor::operator int() const
{
    return GetInt();
}

Convertor::operator unsigned long() const
{
    return GetUnsignedLong();
}

Convertor::operator long long() const
{
    return GetLongLong();
}

Convertor::operator double() const
{
    return GetDouble();
}

Convertor::operator SQDB_STD_STRING() const
{
    return GetString();
}

Convertor::operator const SQDB_CHAR*() const
{
    return GetText();
}

Convertor::operator Blob() const
{
    return GetBlob();
}

int Convertor::GetInt() const
{
    assert(m_stmt);
    return sqlite3_column_int(m_stmt, m_field);
}

unsigned long Convertor::GetUnsignedLong() const
{
    assert(m_stmt);
    const char* data = (char*) sqlite3_column_blob(m_stmt, m_field);
    return strtoul(data, nullptr, 10);
}

long long Convertor::GetLongLong() const
{
    assert(m_stmt);
    return sqlite3_column_int64(m_stmt, m_field);
}

double Convertor::GetDouble() const
{
    assert(m_stmt);
    return sqlite3_column_double(m_stmt, m_field);
}

SQDB_STD_STRING Convertor::GetString() const
{
    assert(m_stmt);
    const auto* result = (const SQDB_CHAR*)
#ifdef SQDB_UTF8
            sqlite3_column_text
#else
            sqlite3_column_text16
#endif
                    (m_stmt, m_field);
    return result;
}

const SQDB_CHAR* Convertor::GetText() const
{
    assert(m_stmt);
    const auto* result = (const SQDB_CHAR*)
#ifdef SQDB_UTF8
            sqlite3_column_text
#else
            sqlite3_column_text16
#endif
                    (m_stmt, m_field);
    return result;
}

Blob Convertor::GetBlob() const
{
    assert(m_stmt);
    const void* data = sqlite3_column_blob(m_stmt, m_field);
    int size = sqlite3_column_bytes(m_stmt, m_field);
    return Blob(data, size);
}

Statement::Statement(sqlite3* db, sqlite3_stmt* stmt)
        : RefCount(), m_db(db), m_stmt(stmt), m_needReset(false)
{
    IncRef();
}

Statement::Statement(const Statement& x)
        : RefCount(x), m_db(x.m_db), m_stmt(x.m_stmt), m_needReset(false)
{
    IncRef();
}

Statement& Statement::operator=(const Statement& x)
{
    if ( this != &x )
    {
        RefCount::operator=(x);
        IncRef();
        m_db = x.m_db;
        m_stmt = x.m_stmt;
        m_needReset = x.m_needReset;
    }
    return *this;
}

bool Statement::Next() {
    assert(m_stmt);
    int ret = SQLITE_BUSY;

    while (ret == SQLITE_BUSY) {
        ret = sqlite3_step(m_stmt);
        m_needReset = true;
        if ( ret == SQLITE_DONE ) {
            return false;
        } else if ( ret == SQLITE_ROW ) {
            return true;
        } else if ( ret == SQLITE_BUSY ) {
            sleep(1);
            continue;
        } else {
            throw Exception(m_db, ret);
        }
    }
    return false;
}

Convertor Statement::GetField(int field) const
{
    return {m_db, m_stmt, field};
}

Statement::~Statement()
{
    if ( DecRef() == 0 )
    {
        sqlite3_finalize(m_stmt);
    }
}

void Statement::BindBlob(int i, const void* value, int n)
{
    if ( m_needReset )
        Reset();
    DoBind(i, value, n);
}

void Statement::BindNull(int i)
{
    if ( m_needReset )
        Reset();
    DoBind(i);
}

void Statement::DoBind(int i, int value)
{
    const int ret = sqlite3_bind_int(m_stmt, i, value);
    CHECK(m_db, ret);
}

void Statement::DoBind(int i, long long value)
{
    const int ret = sqlite3_bind_int64(m_stmt, i, value);
    CHECK(m_db, ret);
}

void Statement::DoBind(int i, double value)
{
    const int ret = sqlite3_bind_double(m_stmt, i, value);
    CHECK(m_db, ret);
}

void Statement::DoBind(int i, const SQDB_STD_STRING& value)
{
    const int ret =
#ifdef SQDB_UTF8
            sqlite3_bind_text
#else
            sqlite3_bind_text16
#endif
                    (m_stmt, i, value.c_str(), value.size() * sizeof(SQDB_STD_STRING::value_type), SQLITE_TRANSIENT);
    CHECK(m_db, ret);
}

void Statement::DoBind(int i, const SQDB_CHAR* value)
{
    const int len = SQDB_STRLEN(value);

    const int ret =
#ifdef SQDB_UTF8
            sqlite3_bind_text
#else
            sqlite3_bind_text16
#endif
                    (m_stmt, i, value, len * sizeof(SQDB_CHAR), SQLITE_TRANSIENT);

    CHECK(m_db, ret);
}

void Statement::DoBind(int i, const void* value, int n)
{
    const int ret = sqlite3_bind_blob(m_stmt, i, value, n, SQLITE_TRANSIENT);
    CHECK(m_db, ret);
}

void Statement::DoBind(int i)
{
    const int ret = sqlite3_bind_null(m_stmt, i);
    CHECK(m_db, ret);
}

void Statement::Reset()
{
    assert(m_needReset);
    assert(m_stmt);

    sqlite3_reset(m_stmt);
    m_needReset = false;
}

QueryStr::QueryStr()
        : m_buf(nullptr)
{
}

const SQDB_CHAR* QueryStr::Format(const SQDB_CHAR* fmt, ...)
{
    va_list va;
    va_start(va, fmt);
#ifdef SQDB_UTF8
    sqlite3_free(m_buf);
  m_buf = sqlite3_vmprintf(fmt, va);
#else
    free(m_buf);
    int len = _vscwprintf(fmt, va) + 1;
    m_buf = (SQDB_CHAR*)malloc(len * sizeof(SQDB_CHAR));
    vswprintf(m_buf, len, fmt, va);
#endif

    va_end(va);

    return m_buf;
}

const SQDB_CHAR* QueryStr::Get() const
{
    return m_buf;
}

QueryStr::~QueryStr()
{
#ifdef SQDB_UTF8
    sqlite3_free(m_buf);
#else
    free(m_buf);
#endif
}

Db::Db(const SQDB_CHAR* fileName)
        : RefCount()
{
#ifdef SQDB_UTF8
    const int ret = sqlite3_open(fileName, &m_db);
#else
    const int ret = sqlite3_open16(fileName, &m_db);
#endif
    CHECK(m_db, ret);

    IncRef();
}

void Db::BeginTransaction()
{
    Query(SQDB_MAKE_TEXT("BEGIN;")).Next();
}

void Db::CommitTransaction()
{
    Query(SQDB_MAKE_TEXT("COMMIT;")).Next();
}

void Db::RollbackTransaction()
{
    Query(SQDB_MAKE_TEXT("ROLLBACK;")).Next();
}

bool Db::TableExists(const SQDB_CHAR* tableName)
{
    QueryStr str;
    Statement s =
            Query(
                    str.Format(SQDB_MAKE_TEXT("select count(*) from sqlite_master where type='table' and name='%s';"), tableName));
    s.Next();
    const int count = s.GetField(0);
    return count > 0;
}

Statement Db::Query(const SQDB_CHAR* queryStr)
{
    sqlite3_stmt* stmt = nullptr;
#ifdef SQDB_UTF8
    const int ret = sqlite3_prepare(m_db, queryStr, -1, &stmt, nullptr);
#else
    const int ret = sqlite3_prepare16(m_db, queryStr, -1, &stmt, NULL);
#endif
    CHECK(m_db, ret);

    return Statement(m_db, stmt);
}

long long Db::LastId()
{
    long long ret = sqlite3_last_insert_rowid(m_db);
    return ret;
}

Db::Db(const Db& x)
        : RefCount(x),
          m_db(x.m_db)
{
    IncRef();
}

Db& Db::operator=(const Db& x)
{
    if ( this != &x )
    {
        RefCount::operator=(x);

        IncRef();
        m_db = x.m_db;
    }
    return *this;
}

Db::~Db()
{
    if ( DecRef() == 0 )
    {
        sqlite3_close(m_db);
    }
}