summaryrefslogtreecommitdiff
path: root/src/db.hh
blob: a4f65e4dbd962bf8cce44bafd32c6021b087e811 (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
#ifndef DB_HH
#define DB_HH

#include <functional>
#include <memory>
#include <string>
#include <vector>

namespace stuff {

class DB {
public:
    virtual ~DB() {}

    enum class Type {
        STRING,
        BOOL,
        DOUBLE,
        INT32,
        INT64,
        RAW
    };

    class Constraint {
    public:
        Constraint(Type type)
            : Constraint(type, false, false, false) {
        }

        Type type() const {
            return type_;
        }

        bool primary_key() const {
            return primary_;
        }

        bool unique() const {
            return primary_ || unique_;
        }

        bool not_null() const {
            return primary_ || not_null_;
        }

    protected:
        Constraint(Type type, bool primary, bool unique, bool not_null)
            : type_(type), primary_(primary), unique_(unique),
              not_null_(not_null) {
        }

    private:
        Type type_;
        bool primary_;
        bool unique_;
        bool not_null_;
    };

    class PrimaryKey : public Constraint {
    public:
        explicit PrimaryKey(Type type)
            : Constraint(type, true, true, true) {
        }
    };

    class Unique : public Constraint {
    public:
        explicit Unique(Type type)
            : Constraint(type, false, true, false) {
        }
    };

    class NotNull : public Constraint {
    public:
        explicit NotNull(Type type)
            : Constraint(type, false, false, true) {
        }
    };

    typedef std::vector<std::pair<std::string,Constraint>> Declaration;

    class Editor {
    public:
        virtual ~Editor() {}

        // Set the column matching name to value
        virtual void set(const std::string& name, const std::string& value) = 0;
        virtual void set(const std::string& name, const char* value) {
            set(name, std::string(value));
        }
        virtual void set(const std::string& name, bool value) = 0;
        virtual void set(const std::string& name, double value) = 0;
        virtual void set(const std::string& name, int32_t value) = 0;
        virtual void set(const std::string& name, int64_t value) = 0;
        virtual void set_null(const std::string& name) = 0;
        virtual void set(const std::string& name, const void* data,
                         size_t size) = 0;

        // Return true if the insert/update succeeded, false in case of error
        // After calling commit, release the object, it's no longer usable
        virtual bool commit() = 0;

        // Return the latest inserted rowid
        virtual int64_t last_insert_rowid() = 0;

    protected:
        Editor() {}

    private:
        Editor(const Editor&) = delete;
        Editor& operator=(const Editor&) = delete;
    };

    class Snapshot {
    public:
        virtual ~Snapshot() {}

        // Get the value for the column matching name, returns false if no
        // column matching that name exists or if the type doesn't match
        virtual bool get(const std::string& name, std::string* value) = 0;
        virtual bool get(const std::string& name, bool* value) = 0;
        virtual bool get(const std::string& name, double* value) = 0;
        virtual bool get(const std::string& name, int32_t* value) = 0;
        virtual bool get(const std::string& name, int64_t* value) = 0;
        virtual bool get(const std::string& name,
                         std::vector<uint8_t>* value) = 0;
        virtual bool is_null(const std::string& name, bool* value) = 0;

        // Set the column to value, returns false if no column with that index
        // exists or if the type doesn't match
        virtual bool get(uint32_t column, std::string* value) = 0;
        virtual bool get(uint32_t column, bool* value) = 0;
        virtual bool get(uint32_t column, double* value) = 0;
        virtual bool get(uint32_t column, int32_t* value) = 0;
        virtual bool get(uint32_t column, int64_t* value) = 0;
        virtual bool get(uint32_t column, std::vector<uint8_t>* value) = 0;
        virtual bool is_null(uint32_t column, bool* value) = 0;

        // Go to the next row in snapshot, returns false if this was the last
        // or an error occurred
        virtual bool next() = 0;

        // Returns true if the snapshot had an error
        virtual bool bad() = 0;

    protected:
        Snapshot() {}

    private:
        Snapshot(const Snapshot&) = delete;
        Snapshot& operator=(const Snapshot&) = delete;
    };

    class Condition;

    class Value {
    public:
        explicit Value(const std::string& value);
        Value(int32_t value);
        Value(int64_t value);
        Value(bool value);
        Value(double value);
        Value(std::nullptr_t);

        Type type() const {
            return type_;
        }

        const std::string& string() const;

        int32_t i32() const;
        int64_t i64() const;
        bool b() const;
        double d() const;

    private:
        friend class Condition;
        Value() {}

        Type type_;
        std::string string_;
        union {
            int32_t i32;
            int64_t i64;
            bool b;
            double d;
        } data_;
    };

    class Column {
    public:
        explicit Column(const std::string& name)
            : name_(name) {
        }

        const std::string& name() const {
            return name_;
        }

    private:
        friend class Condition;
        Column() {}

        std::string name_;
    };

    class Condition {
    public:
        enum BinaryBooleanOperator {
            AND,
            OR
        };
        enum UnaryBooleanOperator {
            NOT
        };
        enum BinaryOperator {
            EQUAL,
            NOT_EQUAL,
            GREATER_THAN,
            LESS_THAN,
            GREATER_EQUAL,
            LESS_EQUAL,
        };
        enum UnaryOperator {
            NEGATIVE,
            IS_NULL,
        };
        enum Mode {
            NOOP,
            BOOL_BINARY,
            BOOL_UNARY,
            COMP_BINARY,
            COMP_UNARY
        };

        Condition()
            : mode_(NOOP) {
        }
        Condition(const Condition& c1, BinaryBooleanOperator op,
                  const Condition& c2)
            : mode_(BOOL_BINARY), c1_(new Condition(c1)),
              c2_(new Condition(c2)) {
            assert(!c1_->empty() && !c2_->empty());
            op_.bool_binary = op;
        }
        Condition(UnaryBooleanOperator op, const Condition& c)
            : mode_(BOOL_UNARY), c1_(new Condition(c)) {
            assert(!c1_->empty());
            op_.bool_unary = op;
        }
        Condition(const Column& c, BinaryOperator op, const Value& v)
            : mode_(COMP_BINARY), c_(c), v_(v) {
            op_.comp_binary = op;
        }
        Condition(UnaryOperator op, const Column& c)
            : mode_(COMP_UNARY), c_(c) {
            op_.comp_unary = op;
        }

        Mode mode() const {
            return mode_;
        }

        bool empty() const {
            return mode_ == NOOP;
        }

        const Condition& c1() const {
            assert(mode_ == BOOL_BINARY || mode_ == BOOL_UNARY);
            return *c1_;
        }

        BinaryBooleanOperator bool_binary_op() const {
            assert(mode_ == BOOL_BINARY);
            return op_.bool_binary;
        }

        UnaryBooleanOperator bool_unary_op() const {
            assert(mode_ == BOOL_UNARY);
            return op_.bool_unary;
        }

        const Condition& c2() const {
            assert(mode_ == BOOL_BINARY);
            return *c2_;
        }

        BinaryOperator binary_op() const {
            assert(mode_ == COMP_BINARY);
            return op_.comp_binary;
        }

        UnaryOperator unary_op() const {
            assert(mode_ == COMP_UNARY);
            return op_.comp_unary;
        }

        const Column& column() const {
            assert(mode_ == COMP_BINARY || mode_ == COMP_UNARY);
            return c_;
        }

        const Value& value() const {
            assert(mode_ == COMP_BINARY);
            return v_;
        }

    private:
        const Mode mode_;
        const std::shared_ptr<Condition> c1_, c2_;
        const Column c_;
        const Value v_;
        union {
            BinaryBooleanOperator bool_binary;
            UnaryBooleanOperator bool_unary;
            BinaryOperator comp_binary;
            UnaryOperator comp_unary;
        } op_;
    };

    class OrderBy {
    public:
        OrderBy(const std::string& name, bool ascending = true)
            : name_(name), ascending_(ascending) {
        }
        OrderBy(const Column& column, bool ascending = true)
            : name_(column.name()), ascending_(ascending) {
        }

        const std::string& name() const {
            return name_;
        }

        bool ascending() const {
            return ascending_;
        }

    private:
        std::string name_;
        bool ascending_;
    };

    // Create a table with the given declarations.
    // If a table with that name already exists nothing happens.
    // Returns false in case of error
    virtual bool insert_table(const std::string& table,
                              const Declaration& declaration) = 0;
    // Returns false in case of error. The table not existing is not an error.
    virtual bool remove_table(const std::string& table) = 0;
    // Create an editor for inserting an row in the table.
    // Always succeeds and doesn't do anything until commit is called on the
    // Editor.
    virtual std::shared_ptr<Editor> insert(const std::string& table) = 0;
    // Create an editor for updating an row in the table.
    // Always succeeds and doesn't do anything until commit is called on the
    // Editor.
    virtual std::shared_ptr<Editor> update(const std::string& table,
                                           const Condition& condition =
                                           Condition()) = 0;
    // Return a snapshot for rows in table matching condition.
    // Check bad() in snapshot for errors
    virtual std::shared_ptr<Snapshot> select(
            const std::string& table, const Condition& condition = Condition(),
            const std::vector<OrderBy>& order_by = std::vector<OrderBy>()) = 0;
    // Remove all rows matching condition in table. Returns number of rows
    // removed or -1 in case of error
    virtual int64_t remove(const std::string& table,
                           const Condition& condition = Condition()) = 0;

    virtual bool start_transaction() = 0;
    virtual bool commit_transaction() = 0;
    virtual bool rollback_transaction() = 0;

    // Returns true if a fatal database error has occurred
    virtual bool bad() = 0;

    // Returns a description of the last error returned by an earlier method
    virtual std::string last_error() = 0;

protected:
    DB() {}

private:
    DB(const DB&) = delete;
    DB& operator=(const DB&) = delete;
};

inline DB::Condition operator&&(const DB::Condition& c1,
                                const DB::Condition& c2) {
    return DB::Condition(c1, DB::Condition::AND, c2);
}
inline DB::Condition operator||(const DB::Condition& c1,
                                const DB::Condition& c2) {
    return DB::Condition(c1, DB::Condition::OR, c2);
}
inline DB::Condition operator!(const DB::Condition& c) {
    return DB::Condition(DB::Condition::NOT, c);
}
inline DB::Condition operator==(const DB::Column& c, const DB::Value& v) {
    return DB::Condition(c, DB::Condition::EQUAL, v);
}
inline DB::Condition operator!=(const DB::Column& c, const DB::Value& v) {
    return DB::Condition(c, DB::Condition::NOT_EQUAL, v);
}
inline DB::Condition operator<(const DB::Column& c, const DB::Value& v) {
    return DB::Condition(c, DB::Condition::LESS_THAN, v);
}
inline DB::Condition operator>(const DB::Column& c, const DB::Value& v) {
    return DB::Condition(c, DB::Condition::GREATER_THAN, v);
}
inline DB::Condition operator<=(const DB::Column& c, const DB::Value& v) {
    return DB::Condition(c, DB::Condition::LESS_EQUAL, v);
}
inline DB::Condition operator>=(const DB::Column& c, const DB::Value& v) {
    return DB::Condition(c, DB::Condition::GREATER_EQUAL, v);
}
inline DB::Condition operator-(const DB::Column& c) {
    return DB::Condition(DB::Condition::NEGATIVE, c);
}
inline DB::Condition is_null(const DB::Column& c) {
    return DB::Condition(DB::Condition::IS_NULL, c);
}

}  // namespace stuff

#endif /* DB_HH */