summaryrefslogtreecommitdiff
path: root/include/sqlite3xx/prepared_statement.hpp
blob: 336adaf1410cdb356605c86a526eea4528b04d8f (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
/*
 *  sqlite3xx - sqlite3 C++ layer, following the ideas of libpqxx
 *  Copyright (C) 2009  Andreas Baumann
 *
 *  This copyrighted material is made available to anyone wishing to use,
 *  modify, copy, or redistribute it subject to the terms and conditions of
 *  the GNU Lesser General Public License, as published by the Free Software
 *  Foundation.
 *
 *  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 Lesser General Public
 *  License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this distribution; if not, write to:
 *  Free Software Foundation, Inc.
 *  51 Franklin Street, Fifth Floor
 *  Boston, MA  02110-1301  USA
 *
 */

#ifndef SQLITE3XX_PREPARED_STATEMENT_H
#define SQLITE3XX_PREPARED_STATEMENT_H

#include <vector>
#include <string>

#include <sqlite3xx/port/stdint.h>		/* for uintmax_t */

#include "port/dllexport.h"

#include "sqlite3.h"

using namespace std;

namespace sqlite3xx {

class connection;
class transaction;
class result;

namespace prepare {

	enum param_treatment {
		treat_direct
	};

	/* class to add () parameters to a prepare statement */
	class SQLITEXX_LIBEXPORT declaration {
		private:
			connection& _c;
			const string& _stmt;

		public:
			declaration( connection &c, const string& stmt );
		
			const declaration& operator( )( const string& sql_type, 
				param_treatment treat ) const;
	};

	class SQLITEXX_LIBEXPORT invocation {
		private:
			connection& _c;
			transaction& _t;
			const string& _stmt;
			int _pos;

		public:
			invocation( connection& c, transaction& t, const string& stmt );

			invocation &setparam( const int& value, bool nonnull );
			invocation &setparam( const unsigned int& value, bool nonnull );
			invocation &setparam( const char* value, bool nonnull );
			invocation &setparam( const string& value, bool nonnull );
			invocation &setparam( const double& value, bool nonnull );
			invocation &setparam( const long& value, bool nonnull );
			invocation &setparam( const uintmax_t& value, bool nonnull );

			result exec( ) const;

			template<typename T>
				invocation &operator( )( const T &v, bool nonnull = true ) {
					return setparam( v, nonnull );
			}
	};
}

class SQLITEXX_LIBEXPORT prepared_stmt {

	private:
		typedef pair<string,prepare::param_treatment> param;

		sqlite3_stmt *_stmt;
		string _sql;
		vector<param> parameters;
		sqlite3 *_db;

	public:
		prepared_stmt( sqlite3 *db, string __sql );
		~prepared_stmt( );
		string sql( );
		void addparam( const string& sqltype, prepare::param_treatment& treatment );

		void reset( );
		void setparam( const int pos, const int value );
		void setparam( const int pos, const unsigned int value );
		void setparam( const int pos, const char* value );
		void setparam( const int pos, const double value );
		void setparam( const int pos, const long value );
		void setparam( const int pos, const uintmax_t value );

		sqlite3_stmt* getStmt( ) const;
};

} /* namespace sqlite3xx */

#endif /* SQLITE3XX_PREPARED_STATEMENT_H */