summaryrefslogtreecommitdiff
path: root/src/dpl/db/include/cchecker/dpl/db/sql_connection.h
blob: 8af5a586d8f50400f62e3f93159e745ba19adb7e (plain)
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
/*
 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
/*
 * @file        sql_connection.h
 * @author      Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com)
 * @version     1.0
 * @brief       This file is the implementation file of SQL connection
 */
#ifndef CCHECKER_SQL_CONNECTION_H
#define CCHECKER_SQL_CONNECTION_H

#include <memory>
#include <sqlite3.h>
#include <string>
#include <memory>
#include <stdint.h>

#include <cchecker/dpl/string.h>
#include <cchecker/dpl/assert.h>
#include <cchecker/dpl/noncopyable.h>
#include <cchecker/dpl/exception.h>
#include <cchecker/dpl/optional.h>
#include <cchecker/log.h>

namespace CCHECKER {
namespace DB {
/**
 * SQL connection class
 */
class SqlConnection
{
  public:
    /**
     * SQL Exception classes
     */
    class Exception
    {
      public:
        DECLARE_EXCEPTION_TYPE(CCHECKER::Exception, Base)
        DECLARE_EXCEPTION_TYPE(Base, SyntaxError)
        DECLARE_EXCEPTION_TYPE(Base, ConnectionBroken)
        DECLARE_EXCEPTION_TYPE(Base, InternalError)
        DECLARE_EXCEPTION_TYPE(Base, InvalidColumn)
    };

    typedef int ColumnIndex;
    typedef int ArgumentIndex;

    /*
     * SQL processed data command
     */
    class DataCommand :
        private Noncopyable
    {
      private:
        SqlConnection *m_masterConnection;
        sqlite3_stmt *m_stmt;

        void CheckBindResult(int result);
        void CheckColumnIndex(SqlConnection::ColumnIndex column);

        DataCommand(SqlConnection *connection, const char *buffer);

        friend class SqlConnection;

      public:
        virtual ~DataCommand();

        /**
         * Bind null to the prepared statement argument
         *
         * @param position Index of argument to bind value to
         */
        void BindNull(ArgumentIndex position);

        /**
         * Bind int to the prepared statement argument
         *
         * @param position Index of argument to bind value to
         * @param value Value to bind
         */
        void BindInteger(ArgumentIndex position, int value);

        /**
         * Bind int8_t to the prepared statement argument
         *
         * @param position Index of argument to bind value to
         * @param value Value to bind
         */
        void BindInt8(ArgumentIndex position, int8_t value);

        /**
         * Bind int16 to the prepared statement argument
         *
         * @param position Index of argument to bind value to
         * @param value Value to bind
         */
        void BindInt16(ArgumentIndex position, int16_t value);

        /**
         * Bind int32 to the prepared statement argument
         *
         * @param position Index of argument to bind value to
         * @param value Value to bind
         */
        void BindInt32(ArgumentIndex position, int32_t value);

        /**
         * Bind int64 to the prepared statement argument
         *
         * @param position Index of argument to bind value to
         * @param value Value to bind
         */
        void BindInt64(ArgumentIndex position, int64_t value);

        /**
         * Bind float to the prepared statement argument
         *
         * @param position Index of argument to bind value to
         * @param value Value to bind
         */
        void BindFloat(ArgumentIndex position, float value);

        /**
         * Bind double to the prepared statement argument
         *
         * @param position Index of argument to bind value to
         * @param value Value to bind
         */
        void BindDouble(ArgumentIndex position, double value);

        /**
         * Bind string to the prepared statement argument
         *
         * @param position Index of argument to bind value to
         * @param value Value to bind
         */
        void BindString(ArgumentIndex position, const char *value);

        /**
         * Bind string to the prepared statement argument
         *
         * @param position Index of argument to bind value to
         * @param value Value to bind
         */
        void BindString(ArgumentIndex position, const String& value);

        /**
         * Bind optional int to the prepared statement argument.
         * If optional is not set null will be bound
         *
         * @param position Index of argument to bind value to
         * @param value Value to bind
         */
        void BindInteger(ArgumentIndex position, const Optional<int> &value);

        /**
         * Bind optional int8 to the prepared statement argument.
         * If optional is not set null will be bound
         *
         * @param position Index of argument to bind value to
         * @param value Value to bind
         */
        void BindInt8(ArgumentIndex position, const Optional<int8_t> &value);

        /**
         * Bind optional int16 to the prepared statement argument.
         * If optional is not set null will be bound
         *
         * @param position Index of argument to bind value to
         * @param value Value to bind
         */
        void BindInt16(ArgumentIndex position, const Optional<int16_t> &value);

        /**
         * Bind optional int32 to the prepared statement argument.
         * If optional is not set null will be bound
         *
         * @param position Index of argument to bind value to
         * @param value Value to bind
         */
        void BindInt32(ArgumentIndex position, const Optional<int32_t> &value);

        /**
         * Bind optional int64 to the prepared statement argument.
         * If optional is not set null will be bound
         *
         * @param position Index of argument to bind value to
         * @param value Value to bind
         */
        void BindInt64(ArgumentIndex position, const Optional<int64_t> &value);

        /**
         * Bind optional float to the prepared statement argument.
         * If optional is not set null will be bound
         *
         * @param position Index of argument to bind value to
         * @param value Value to bind
         */
        void BindFloat(ArgumentIndex position, const Optional<float> &value);

        /**
         * Bind optional double to the prepared statement argument.
         * If optional is not set null will be bound
         *
         * @param position Index of argument to bind value to
         * @param value Value to bind
         */
        void BindDouble(ArgumentIndex position, const Optional<double> &value);

        /**
         * Bind optional string to the prepared statement argument.
         * If optional is not set null will be bound
         *
         * @param position Index of argument to bind value to
         * @param value Value to bind
         */
        void BindString(ArgumentIndex position, const Optional<String> &value);

        /**
         * Execute the prepared statement and/or move
         * to the next row of the result
         *
         * @return True when there was a row returned
         */
        bool Step();

        /**
         * Reset prepared statement's arguments
         * All parameters will become null
         */
        void Reset();

        /**
         * Checks whether column value is null
         *
         * @throw Exception::InvalidColumn
         */
        bool IsColumnNull(ColumnIndex column);

        /**
         * Get integer value from column in current row.
         *
         * @throw Exception::InvalidColumn
         */
        int GetColumnInteger(ColumnIndex column);

        /**
         * Get int8 value from column in current row.
         *
         * @throw Exception::InvalidColumn
         */
        int8_t GetColumnInt8(ColumnIndex column);

        /**
         * Get int16 value from column in current row.
         *
         * @throw Exception::InvalidColumn
         */
        int16_t GetColumnInt16(ColumnIndex column);
        /**
         * Get int32 value from column in current row.
         *
         * @throw Exception::InvalidColumn
         */
        int32_t GetColumnInt32(ColumnIndex column);

        /**
         * Get int64 value from column in current row.
         *
         * @throw Exception::InvalidColumn
         */
        int64_t GetColumnInt64(ColumnIndex column);

        /**
         * Get float value from column in current row.
         *
         * @throw Exception::InvalidColumn
         */
        float GetColumnFloat(ColumnIndex column);

        /**
         * Get double value from column in current row.
         *
         * @throw Exception::InvalidColumn
         */
        double GetColumnDouble(ColumnIndex column);

        /**
         * Get string value from column in current row.
         *
         * @throw Exception::InvalidColumn
         */
        std::string GetColumnString(ColumnIndex column);

        /**
         * Get optional integer value from column in current row.
         *
         * @throw Exception::InvalidColumn
         */
        Optional<int> GetColumnOptionalInteger(ColumnIndex column);

        /**
         * Get optional int8 value from column in current row.
         *
         * @throw Exception::InvalidColumn
         */
        Optional<int8_t> GetColumnOptionalInt8(ColumnIndex column);

        /**
         * Get optional int16value from column in current row.
         *
         * @throw Exception::InvalidColumn
         */
        Optional<int16_t> GetColumnOptionalInt16(ColumnIndex column);

        /**
         * Get optional int32 value from column in current row.
         *
         * @throw Exception::InvalidColumn
         */
        Optional<int32_t> GetColumnOptionalInt32(ColumnIndex column);

        /**
         * Get optional int64 value from column in current row.
         *
         * @throw Exception::InvalidColumn
         */
        Optional<int64_t> GetColumnOptionalInt64(ColumnIndex column);

        /**
         * Get optional float value from column in current row.
         *
         * @throw Exception::InvalidColumn
         */
        Optional<float> GetColumnOptionalFloat(ColumnIndex column);

        /**
         * Get optional double value from column in current row.
         *
         * @throw Exception::InvalidColumn
         */
        Optional<double> GetColumnOptionalDouble(ColumnIndex column);

        /**
         * Get optional string value from column in current row.
         *
         * @throw Exception::InvalidColumn
         */
        Optional<String> GetColumnOptionalString(ColumnIndex column);
    };

    // Move on copy semantics
    typedef std::auto_ptr<DataCommand> DataCommandAutoPtr;

    // Open flags
    class Flag
    {
      public:
        enum Type
        {
            None = 1 << 0,
            UseLucene = 1 << 1
        };

        enum Option
        {
            RO = SQLITE_OPEN_NOMUTEX | SQLITE_OPEN_READONLY,
            /**
             * *TODO: please remove CREATE option from RW flag when all places
             *      that need that switched do CRW
             */
            RW = SQLITE_OPEN_NOMUTEX | SQLITE_OPEN_READWRITE |
                SQLITE_OPEN_CREATE,
            CRW = RW | SQLITE_OPEN_CREATE
        };
    };

    // RowID
    typedef sqlite3_int64 RowID;

    /**
     * Synchronization object used to synchronize SQL connection
     * to the same database across different threads and processes
     */
    class SynchronizationObject
    {
      public:
        virtual ~SynchronizationObject() {}

        /**
         * Synchronizes SQL connection for multiple clients.
         */
        virtual void Synchronize() = 0;

        /**
         * Notify all waiting clients that the connection is no longer locked.
         */
        virtual void NotifyAll() = 0;
    };

  protected:
    sqlite3 *m_connection;

    // Options
    bool m_usingLucene;

    // Stored data procedures
    int m_dataCommandsCount;

    // Synchronization object
    std::unique_ptr<SynchronizationObject> m_synchronizationObject;

    virtual void Connect(const std::string &address,
                         Flag::Type = Flag::None, Flag::Option = Flag::RO);
    virtual void Disconnect();

    void TurnOnForeignKeys();

    static SynchronizationObject *AllocDefaultSynchronizationObject();

  public:
    /**
     * Open SQL connection
     *
     * Synchronization is archieved by using provided asynchronization object.
     * If synchronizationObject is set to NULL, so synchronization is performed.
     * Ownership of the synchronization object is transfered to sql connection
     * object.
     *
     * @param address Database file name
     * @param flags Open flags
     * @param synchronizationObject A synchronization object to use.
     */
    explicit SqlConnection(const std::string &address = std::string(),
                           Flag::Type flags = Flag::None,
                           Flag::Option options = Flag::RO,
                           SynchronizationObject *synchronizationObject =
                               AllocDefaultSynchronizationObject());

    /**
     * Destructor
     */
    virtual ~SqlConnection();

    /**
     * Execute SQL command without result
     *
     * @param format
     * @param ...
     */
    void ExecCommand(const char *format, ...);

    /**
     * Prepare stored procedure
     *
     * @param format SQL statement
     * @return Data command representing stored procedure
     */
    DataCommandAutoPtr PrepareDataCommand(const char *format, ...);

    /**
     * Check whether given table exists
     *
     * @param tableName Name of the table to check
     * @return True if given table name exists
     */
    bool CheckTableExist(const char *tableName);

    /**
     * Get last insert operation new row id
     *
     * @return Row ID
     */
    RowID GetLastInsertRowID() const;

    void BeginTransaction();

    void RollbackTransaction();

    void CommitTransaction();
};
} // namespace DB
} // namespace CCHECKER

#endif // CCHECKER_SQL_CONNECTION_H