Mercurial > hg > orthanc
annotate Core/SQLite/Statement.h @ 2759:0cfab50c1381 Orthanc-0.7.5
close old branch
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 17 Jul 2018 09:41:32 +0200 |
parents | 96a2d2da0fee |
children | 9b9026560a5f |
rev | line source |
---|---|
0 | 1 /** |
59 | 2 * Orthanc - A Lightweight, RESTful DICOM Store |
689 | 3 * Copyright (C) 2012-2014 Medical Physics Department, CHU of Liege, |
0 | 4 * Belgium |
5 * | |
17 | 6 * Copyright (c) 2012 The Chromium Authors. All rights reserved. |
7 * | |
8 * Redistribution and use in source and binary forms, with or without | |
9 * modification, are permitted provided that the following conditions are | |
10 * met: | |
0 | 11 * |
17 | 12 * * Redistributions of source code must retain the above copyright |
13 * notice, this list of conditions and the following disclaimer. | |
14 * * Redistributions in binary form must reproduce the above | |
15 * copyright notice, this list of conditions and the following disclaimer | |
16 * in the documentation and/or other materials provided with the | |
17 * distribution. | |
18 * * Neither the name of Google Inc., the name of the CHU of Liege, | |
19 * nor the names of its contributors may be used to endorse or promote | |
20 * products derived from this software without specific prior written | |
21 * permission. | |
22 * | |
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
27 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
29 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
0 | 34 **/ |
35 | |
36 | |
37 #pragma once | |
38 | |
59 | 39 #include "../OrthancException.h" |
0 | 40 #include "StatementId.h" |
41 #include "StatementReference.h" | |
42 | |
43 #include <vector> | |
44 #include <stdint.h> | |
45 #include <boost/noncopyable.hpp> | |
265 | 46 |
47 #if ORTHANC_BUILD_UNIT_TESTS == 1 | |
138 | 48 #include <gtest/gtest_prod.h> |
265 | 49 #endif |
0 | 50 |
51 struct sqlite3_stmt; | |
52 | |
53 | |
59 | 54 namespace Orthanc |
0 | 55 { |
56 namespace SQLite | |
57 { | |
58 class Connection; | |
59 | |
60 // Possible return values from ColumnType in a statement. These | |
61 // should match the values in sqlite3.h. | |
62 enum ColumnType | |
63 { | |
64 COLUMN_TYPE_INTEGER = 1, | |
65 COLUMN_TYPE_FLOAT = 2, | |
66 COLUMN_TYPE_TEXT = 3, | |
67 COLUMN_TYPE_BLOB = 4, | |
68 COLUMN_TYPE_NULL = 5 | |
69 }; | |
70 | |
71 class Statement : public boost::noncopyable | |
72 { | |
73 friend class Connection; | |
265 | 74 |
75 #if ORTHANC_BUILD_UNIT_TESTS == 1 | |
138 | 76 FRIEND_TEST(SQLStatementTest, Run); |
77 FRIEND_TEST(SQLStatementTest, Reset); | |
265 | 78 #endif |
0 | 79 |
80 private: | |
81 StatementReference reference_; | |
82 | |
83 int CheckError(int err) const; | |
84 | |
85 void CheckOk(int err) const; | |
86 | |
87 struct sqlite3_stmt* GetStatement() const | |
88 { | |
89 return reference_.GetWrappedObject(); | |
90 } | |
91 | |
92 public: | |
93 Statement(Connection& database, | |
94 const std::string& sql); | |
95 | |
96 Statement(Connection& database, | |
97 const StatementId& id, | |
98 const std::string& sql); | |
99 | |
100 Statement(Connection& database, | |
101 const char* sql); | |
102 | |
103 Statement(Connection& database, | |
104 const StatementId& id, | |
105 const char* sql); | |
106 | |
137
0e97abc7b950
fix of a bug in older versions of sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
80
diff
changeset
|
107 ~Statement() |
0e97abc7b950
fix of a bug in older versions of sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
80
diff
changeset
|
108 { |
0e97abc7b950
fix of a bug in older versions of sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
80
diff
changeset
|
109 Reset(); |
0e97abc7b950
fix of a bug in older versions of sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
80
diff
changeset
|
110 } |
0e97abc7b950
fix of a bug in older versions of sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
80
diff
changeset
|
111 |
0 | 112 bool Run(); |
113 | |
114 bool Step(); | |
115 | |
116 // Diagnostics -------------------------------------------------------------- | |
117 | |
118 std::string GetOriginalSQLStatement(); | |
119 | |
120 | |
121 // Binding ------------------------------------------------------------------- | |
122 | |
123 // These all take a 0-based argument index | |
124 void BindNull(int col); | |
125 void BindBool(int col, bool val); | |
126 void BindInt(int col, int val); | |
127 void BindInt64(int col, int64_t val); | |
128 void BindDouble(int col, double val); | |
129 void BindCString(int col, const char* val); | |
130 void BindString(int col, const std::string& val); | |
131 //void BindString16(int col, const string16& value); | |
132 void BindBlob(int col, const void* value, int value_len); | |
133 | |
134 | |
135 // Retrieving ---------------------------------------------------------------- | |
136 | |
137 // Returns the number of output columns in the result. | |
138 int ColumnCount() const; | |
139 | |
140 // Returns the type associated with the given column. | |
141 // | |
142 // Watch out: the type may be undefined if you've done something to cause a | |
143 // "type conversion." This means requesting the value of a column of a type | |
144 // where that type is not the native type. For safety, call ColumnType only | |
145 // on a column before getting the value out in any way. | |
146 ColumnType GetColumnType(int col) const; | |
147 ColumnType GetDeclaredColumnType(int col) const; | |
148 | |
149 // These all take a 0-based argument index. | |
80 | 150 bool ColumnIsNull(int col) const ; |
0 | 151 bool ColumnBool(int col) const; |
152 int ColumnInt(int col) const; | |
153 int64_t ColumnInt64(int col) const; | |
154 double ColumnDouble(int col) const; | |
155 std::string ColumnString(int col) const; | |
156 //string16 ColumnString16(int col) const; | |
157 | |
158 // When reading a blob, you can get a raw pointer to the underlying data, | |
159 // along with the length, or you can just ask us to copy the blob into a | |
160 // vector. Danger! ColumnBlob may return NULL if there is no data! | |
161 int ColumnByteLength(int col) const; | |
162 const void* ColumnBlob(int col) const; | |
163 bool ColumnBlobAsString(int col, std::string* blob); | |
164 //bool ColumnBlobAsString16(int col, string16* val) const; | |
724 | 165 //bool ColumnBlobAsVector(int col, std::vector<char>* val) const; |
166 //bool ColumnBlobAsVector(int col, std::vector<unsigned char>* val) const; | |
0 | 167 |
724 | 168 // Resets the statement to its initial condition. This includes any current |
169 // result row, and also the bound variables if the |clear_bound_vars| is true. | |
170 void Reset(bool clear_bound_vars = true); | |
0 | 171 }; |
172 } | |
173 } |