-
Notifications
You must be signed in to change notification settings - Fork 27
/
sqlite-strategies.hh
86 lines (67 loc) · 1.93 KB
/
sqlite-strategies.hh
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
/* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
#ifndef SQLITE_STRATEGIES_H
#define SQLITE_STRATEGIES_H 1
#include <cstdlib>
#include <vector>
#include "common.hh"
#include "sqlite-pst.hh"
class SqliteStrategy {
public:
SqliteStrategy(const char * const fn, const char * const finit = NULL) :
filename(fn),
initFile(finit),
db(NULL),
statements()
{ }
virtual ~SqliteStrategy() {
close();
}
const std::vector<Statements *> &allStatements() {
return statements;
}
Statements *forKey(const std::string &key) {
assert(statements.size() > 0);
int h=5381;
int i=0;
const char *str = key.c_str();
for(i=0; str[i] != 0x00; i++) {
h = ((h << 5) + h) ^ str[i];
}
return statements.at(std::abs(h) % (int)statements.size());
}
virtual void initTables(void);
virtual void initStatements(void);
virtual void destroyTables(void);
virtual void initPragmas(void);
void destroyStatements(void);
void execute(const char * const query);
sqlite3 *open(void);
void close(void);
protected:
const char * const filename;
const char * const initFile;
sqlite3 *db;
std::vector<Statements *> statements;
private:
DISALLOW_COPY_AND_ASSIGN(SqliteStrategy);
};
//
// ----------------------------------------------------------------------
// Multi DB strategy
// ----------------------------------------------------------------------
//
class MultiDBSqliteStrategy : public SqliteStrategy {
public:
MultiDBSqliteStrategy(const char * const fn,
const char * const finit = NULL,
int n=4):
SqliteStrategy(fn, finit),
numTables(n)
{}
void initTables(void);
void initStatements(void);
void destroyTables(void);
private:
int numTables;
};
#endif /* SQLITE_STRATEGIES_H */