aboutsummaryrefslogtreecommitdiffstats
path: root/SoftHSMv2/src/lib/object_store/DB.h
blob: e4e1a114fad2f86df508ee51186272ed56fc247b (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
/*
 * Copyright (c) 2013 SURFnet bv
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/*****************************************************************************
 DB.h

 Specifies classes to access the Token Database
 *****************************************************************************/

#ifndef _SOFTHSM_V2_DB_H
#define _SOFTHSM_V2_DB_H

#include "config.h"

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

namespace DB {

// Log an error to the error handler that has been setup using a call to setLogErrorHandler declared below.
void logError(const std::string &format, ...);

// The ap parameter has already been started with va_start.
// So the handler only has to pass this on to a vprintf function
// to actually print it.
typedef int (*LogErrorHandler)(const char *format, va_list ap);

// Set an alternative for vprintf to log the actual errors.
// Set to NULL to disable logging al together.
LogErrorHandler setLogErrorHandler(LogErrorHandler handler);

// Set the log error handler back to the default value that logs to stdout.
void resetLogErrorHandler();

// Forward declaration of the handle class used by Statement, Binding and Result.
class Handle;

// Responsible for holding on to a prepared statement.
// After a prepared statement has been used it can be reused when the same query is performed again.
class Statement {
public:
	Statement();
	Statement(sqlite3_stmt *statement);
	Statement(const Statement &statement);
	Statement &operator=(const Statement &statement);

	virtual ~Statement();
	bool isValid();

	// Something we'd like to check during testing.
	int refcount();

	// Reset a prepared statement
	bool reset();

	// Perform a single step of the prepared statement.
	enum ReturnCode {
		ReturnCodeRow,
		ReturnCodeDone,
		ReturnCodeError
	};

	ReturnCode step();

	Handle *handle() const;
protected:
	Handle *_handle;
};

// Responsible for allowing parameters to be bound to statements.
// On a statement that has been performed or executed you first
// need to call reset() before new parameters can be bound.
class Bindings : public Statement {
public:
	Bindings();
	Bindings(const Statement &statement);

	// To clear all existing bindings call this method.
	bool clear();

	// Bind a value to a parameter in a prepared statement
	bool bindBlob(int index, const void *value, int n, void(*destruct)(void*));
	bool bindDouble(int index, double value);
	bool bindInt(int index, int value);
	bool bindInt64(int index, long long value );
	//bool bindNull(int index);
	bool bindText(int index, const char *value, int n, void(*destruct)(void*));
	//bool bindZeroBlob(int index, int n);
};

// Responsible for providing access to the result set of a query.
// Used for queries that actually provide a result set.
// A result that is returned will be positioned at the first row.
class Result : public Statement {
public:
	Result();
	Result(const Statement &statement);

	bool fieldIsNull(unsigned int fieldidx);
	time_t getDatetime(unsigned int fieldidx);
	unsigned char getUChar(unsigned int fieldidx);
	float getFloat(unsigned int fieldidx);
	double getDouble(unsigned int fieldidx);
	int getInt(unsigned int fieldidx);
	unsigned int getUInt(unsigned int fieldidx);
	long long getLongLong(unsigned int fieldidx);
	unsigned long long getULongLong(unsigned int fieldidx);

	const char *getString(unsigned int fieldidx);
	const unsigned char *getBinary(unsigned int fieldidx);
	size_t getFieldLength(unsigned int fieldidx);

	// Position the result on the first row again.
	bool firstRow();

	// Position the result on the next row.
	bool nextRow();
};

// Responsible for connection to the database and for managing prepared statements.
class Connection {
public:
	static Connection *Create(const std::string &dbdir, const std::string &dbname);
	virtual ~Connection();

	// value that was passed into dbdir when this connection was created.
	const std::string &dbdir();

	// concatenation of dbdir and dbname
	const std::string &dbpath();

	Statement prepare(const std::string &format, ...);
	Result perform(Statement &statement);
	bool execute(Statement &statement);

	bool connect(const char *connectionLabel = NULL);
	void close();

	bool tableExists(const std::string &tablename);
	long long lastInsertRowId();

	bool inTransaction();
	bool beginTransactionRO();
	bool endTransactionRO();
	bool beginTransactionRW();
	bool commitTransaction();
	bool rollbackTransaction();

	// Set the busy timeout that the database layer will wait for a database lock to become available.
	bool setBusyTimeout(int ms);
private:
	std::string _dbdir;
	std::string _dbpath;
	sqlite3 *_db;

	Connection(const std::string &dbdir, const std::string &dbname);

	// disable evil constructors
	Connection(const Connection &);
	void operator=(const Connection&);
};

}

#endif // !_SOFTHSM_V2_DB_H