Debian Codegraph @

src/lib/object_store/DB.h

raw
1 /*
2 * Copyright (c) 2013 SURFnet bv
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
20 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
22 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
24 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27 /*****************************************************************************
28 DB.h
29
30 Specifies classes to access the Token Database
31 *****************************************************************************/
32
33 #ifndef _SOFTHSM_V2_DB_H
34 #define _SOFTHSM_V2_DB_H
35
36 #include "config.h"
37
38 #include <string>
39 #include <sqlite3.h>
40 #include <time.h>
41
42 namespace DB {
43
44 // Log an error to the error handler that has been setup using a call to setLogErrorHandler declared below.
45 void logError(const std::string &format, ...);
46
47 // The ap parameter has already been started with va_start.
48 // So the handler only has to pass this on to a vprintf function
49 // to actually print it.
50 typedef int (*LogErrorHandler)(const char *format, va_list ap);
51
52 // Set an alternative for vprintf to log the actual errors.
53 // Set to NULL to disable logging al together.
54 LogErrorHandler setLogErrorHandler(LogErrorHandler handler);
55
56 // Set the log error handler back to the default value that logs to stdout.
57 void resetLogErrorHandler();
58
59 // Forward declaration of the handle class used by Statement, Binding and Result.
60 class Handle;
61
62 // Responsible for holding on to a prepared statement.
63 // After a prepared statement has been used it can be reused when the same query is performed again.
64 class Statement {
65 public:
66 Statement();
67 Statement(sqlite3_stmt *statement);
68 Statement(const Statement &statement);
69 Statement &operator=(const Statement &statement);
70
71 virtual ~Statement();
72 bool isValid();
73
74 // Something we'd like to check during testing.
75 int refcount();
76
77 // Reset a prepared statement
78 bool reset();
79
80 // Perform a single step of the prepared statement.
81 enum ReturnCode {
82 ReturnCodeRow,
83 ReturnCodeDone,
84 ReturnCodeError
85 };
86
87 ReturnCode step();
88
89 Handle *handle() const;
90 protected:
91 Handle *_handle;
92 };
93
94 // Responsible for allowing parameters to be bound to statements.
95 // On a statement that has been performed or executed you first
96 // need to call reset() before new parameters can be bound.
97 class Bindings : public Statement {
98 public:
99 Bindings();
100 Bindings(const Statement &statement);
101
102 // To clear all existing bindings call this method.
103 bool clear();
104
105 // Bind a value to a parameter in a prepared statement
106 bool bindBlob(int index, const void *value, int n, void(*destruct)(void*));
107 bool bindDouble(int index, double value);
108 bool bindInt(int index, int value);
109 bool bindInt64(int index, long long value );
110 //bool bindNull(int index);
111 bool bindText(int index, const char *value, int n, void(*destruct)(void*));
112 //bool bindZeroBlob(int index, int n);
113 };
114
115 // Responsible for providing access to the result set of a query.
116 // Used for queries that actually provide a result set.
117 // A result that is returned will be positioned at the first row.
118 class Result : public Statement {
119 public:
120 Result();
121 Result(const Statement &statement);
122
123 bool fieldIsNull(unsigned int fieldidx);
124 time_t getDatetime(unsigned int fieldidx);
125 unsigned char getUChar(unsigned int fieldidx);
126 float getFloat(unsigned int fieldidx);
127 double getDouble(unsigned int fieldidx);
128 int getInt(unsigned int fieldidx);
129 unsigned int getUInt(unsigned int fieldidx);
130 long long getLongLong(unsigned int fieldidx);
131 unsigned long long getULongLong(unsigned int fieldidx);
132
133 const char *getString(unsigned int fieldidx);
134 const unsigned char *getBinary(unsigned int fieldidx);
135 size_t getFieldLength(unsigned int fieldidx);
136
137 // Position the result on the first row again.
138 bool firstRow();
139
140 // Position the result on the next row.
141 bool nextRow();
142 };
143
144 // Responsible for connection to the database and for managing prepared statements.
145 class Connection {
146 public:
147 static Connection *Create(const std::string &dbdir, const std::string &dbname);
148 virtual ~Connection();
149
150 // value that was passed into dbdir when this connection was created.
151 const std::string &dbdir();
152
153 // concatenation of dbdir and dbname
154 const std::string &dbpath();
155
156 Statement prepare(const std::string &format, ...);
157 Result perform(Statement &statement);
158 bool execute(Statement &statement);
159
160 bool connect(const char *connectionLabel = NULL);
161 void close();
162
163 bool tableExists(const std::string &tablename);
164 long long lastInsertRowId();
165
166 bool inTransaction();
167 bool beginTransactionRO();
168 bool endTransactionRO();
169 bool beginTransactionRW();
170 bool commitTransaction();
171 bool rollbackTransaction();
172
173 // Set the busy timeout that the database layer will wait for a database lock to become available.
174 bool setBusyTimeout(int ms);
175 private:
176 std::string _dbdir;
177 std::string _dbpath;
178 sqlite3 *_db;
179
180 Connection(const std::string &dbdir, const std::string &dbname);
181
182 // disable evil constructors
183 Connection(const Connection &);
184 void operator=(const Connection&);
185 };
186
187 }
188
189 #endif // !_SOFTHSM_V2_DB_H