summaryrefslogtreecommitdiffstats
path: root/fbreader/src/database/sqldb/implsqlite/SQLiteCommand.h
blob: 395fd3066d17e53feb2891c6400b9b937dca3efd (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
/*
 * Copyright (C) 2009-2012 Geometer Plus <[email protected]>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 */


#ifndef __SQLITECOMMAND_H__
#define __SQLITECOMMAND_H__

#include <sqlite3.h>

#include "../DBCommand.h"

#include "SQLiteConnection.h"


/*
 * Command can contain parameters with following names:
 *     ?    - anonymous parameter
 *     @AAA - named parameter
 *
 * where AAA is alpha-numeric parameter name
 */
class SQLiteCommand : public DBCommand {

public:
	static std::string packCommand(const std::string &command);

public:
	SQLiteCommand(const std::string &command, DBConnection &connection);
	~SQLiteCommand();

	bool execute();
	shared_ptr<DBValue> executeScalar();
	shared_ptr<DBDataReader> executeReader();

public:
	void unlock();

	// TODO: hide sqlite3_stmt object inside
	std::vector<sqlite3_stmt *> &statements();
	const std::vector<sqlite3_stmt *> &statements() const;

	void dumpError() const;
	void dumpError(const std::string &msg) const;

	bool resetStatements();

private:

	struct BindParameter {
		std::size_t Position;
		std::string Name;

		BindParameter(std::size_t pos) : Position(pos), Name("") {}
		BindParameter(std::size_t pos, const std::string &name) : Position(pos), Name(name) {}

		bool hasName() const;
	};
	
	class BindParameterComparator {

	public:
		BindParameterComparator(const std::string &name);
		bool operator () (const SQLiteCommand::BindParameter &p) const;

	private:
		const std::string myName;
	};

	void prepareBindContext();
	bool bindParameters();
	bool bindParameterByName(const std::string &name, shared_ptr<DBValue> value);
	bool bindParameterByIndex(std::size_t index, shared_ptr<DBValue> value);
	bool bindParameter(sqlite3_stmt *statement, int number, shared_ptr<DBValue> value);
	bool prepareStatements(SQLiteConnection &conn);

	void finalizeStatements();
	
private:
	std::vector<sqlite3_stmt *> myStatements;
	std::vector<BindParameter> myBindContext;
	bool myLocked;

private: // disable copying:
	SQLiteCommand(const SQLiteCommand &);
	const SQLiteCommand &operator = (const SQLiteCommand &);
};


inline SQLiteCommand::SQLiteCommand(const std::string &command, DBConnection &connection) 
	: DBCommand(SQLiteCommand::packCommand(command), connection), myStatements(), myLocked(false) {}

inline void SQLiteCommand::unlock() { myLocked = false; }
inline std::vector<sqlite3_stmt *> &SQLiteCommand::statements() { return myStatements; }
inline const std::vector<sqlite3_stmt *> &SQLiteCommand::statements() const { return myStatements; }

inline bool SQLiteCommand::BindParameter::hasName() const { return Name.size() > 0; }

inline SQLiteCommand::BindParameterComparator::BindParameterComparator(const std::string &name) : myName(name) {}
inline bool SQLiteCommand::BindParameterComparator::operator () (const SQLiteCommand::BindParameter &p) const { return myName == p.Name; }

#endif /* __SQLITECOMMAND_H__ */