comparison Core/SQLite/Statement.h @ 0:3959d33612cc

initial commit
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 19 Jul 2012 14:32:22 +0200
parents
children db4d996ea264
comparison
equal deleted inserted replaced
-1:000000000000 0:3959d33612cc
1 /**
2 * Palantir - A Lightweight, RESTful DICOM Store
3 * Copyright (C) 2012 Medical Physics Department, CHU of Liege,
4 * Belgium
5 *
6 * This program is free software: you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation, either version 3 of the
9 * License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 **/
19
20
21 #pragma once
22
23 #include "../PalantirException.h"
24 #include "StatementId.h"
25 #include "StatementReference.h"
26
27 #include <vector>
28 #include <stdint.h>
29 #include <boost/noncopyable.hpp>
30
31 struct sqlite3_stmt;
32
33
34 namespace Palantir
35 {
36 namespace SQLite
37 {
38 class Connection;
39
40 // Possible return values from ColumnType in a statement. These
41 // should match the values in sqlite3.h.
42 enum ColumnType
43 {
44 COLUMN_TYPE_INTEGER = 1,
45 COLUMN_TYPE_FLOAT = 2,
46 COLUMN_TYPE_TEXT = 3,
47 COLUMN_TYPE_BLOB = 4,
48 COLUMN_TYPE_NULL = 5
49 };
50
51 class Statement : public boost::noncopyable
52 {
53 friend class Connection;
54
55 private:
56 StatementReference reference_;
57
58 int CheckError(int err) const;
59
60 void CheckOk(int err) const;
61
62 struct sqlite3_stmt* GetStatement() const
63 {
64 return reference_.GetWrappedObject();
65 }
66
67 public:
68 Statement(Connection& database,
69 const std::string& sql);
70
71 Statement(Connection& database,
72 const StatementId& id,
73 const std::string& sql);
74
75 Statement(Connection& database,
76 const char* sql);
77
78 Statement(Connection& database,
79 const StatementId& id,
80 const char* sql);
81
82 bool Run();
83
84 bool Step();
85
86 // Resets the statement to its initial condition. This includes any current
87 // result row, and also the bound variables if the |clear_bound_vars| is true.
88 void Reset(bool clear_bound_vars = true);
89
90 // Diagnostics --------------------------------------------------------------
91
92 std::string GetOriginalSQLStatement();
93
94
95 // Binding -------------------------------------------------------------------
96
97 // These all take a 0-based argument index
98 void BindNull(int col);
99 void BindBool(int col, bool val);
100 void BindInt(int col, int val);
101 void BindInt64(int col, int64_t val);
102 void BindDouble(int col, double val);
103 void BindCString(int col, const char* val);
104 void BindString(int col, const std::string& val);
105 //void BindString16(int col, const string16& value);
106 void BindBlob(int col, const void* value, int value_len);
107
108
109 // Retrieving ----------------------------------------------------------------
110
111 // Returns the number of output columns in the result.
112 int ColumnCount() const;
113
114 // Returns the type associated with the given column.
115 //
116 // Watch out: the type may be undefined if you've done something to cause a
117 // "type conversion." This means requesting the value of a column of a type
118 // where that type is not the native type. For safety, call ColumnType only
119 // on a column before getting the value out in any way.
120 ColumnType GetColumnType(int col) const;
121 ColumnType GetDeclaredColumnType(int col) const;
122
123 // These all take a 0-based argument index.
124 bool ColumnBool(int col) const;
125 int ColumnInt(int col) const;
126 int64_t ColumnInt64(int col) const;
127 double ColumnDouble(int col) const;
128 std::string ColumnString(int col) const;
129 //string16 ColumnString16(int col) const;
130
131 // When reading a blob, you can get a raw pointer to the underlying data,
132 // along with the length, or you can just ask us to copy the blob into a
133 // vector. Danger! ColumnBlob may return NULL if there is no data!
134 int ColumnByteLength(int col) const;
135 const void* ColumnBlob(int col) const;
136 bool ColumnBlobAsString(int col, std::string* blob);
137 //bool ColumnBlobAsString16(int col, string16* val) const;
138 bool ColumnBlobAsVector(int col, std::vector<char>* val) const;
139 bool ColumnBlobAsVector(int col, std::vector<unsigned char>* val) const;
140
141 };
142 }
143 }