0
|
1 /**
|
59
|
2 * Orthanc - A Lightweight, RESTful DICOM Store
|
0
|
3 * Copyright (C) 2012 Medical Physics Department, CHU of Liege,
|
|
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>
|
|
46
|
|
47 struct sqlite3_stmt;
|
|
48
|
|
49
|
59
|
50 namespace Orthanc
|
0
|
51 {
|
|
52 namespace SQLite
|
|
53 {
|
|
54 class Connection;
|
|
55
|
|
56 // Possible return values from ColumnType in a statement. These
|
|
57 // should match the values in sqlite3.h.
|
|
58 enum ColumnType
|
|
59 {
|
|
60 COLUMN_TYPE_INTEGER = 1,
|
|
61 COLUMN_TYPE_FLOAT = 2,
|
|
62 COLUMN_TYPE_TEXT = 3,
|
|
63 COLUMN_TYPE_BLOB = 4,
|
|
64 COLUMN_TYPE_NULL = 5
|
|
65 };
|
|
66
|
|
67 class Statement : public boost::noncopyable
|
|
68 {
|
|
69 friend class Connection;
|
|
70
|
|
71 private:
|
|
72 StatementReference reference_;
|
|
73
|
|
74 int CheckError(int err) const;
|
|
75
|
|
76 void CheckOk(int err) const;
|
|
77
|
|
78 struct sqlite3_stmt* GetStatement() const
|
|
79 {
|
|
80 return reference_.GetWrappedObject();
|
|
81 }
|
|
82
|
|
83 public:
|
|
84 Statement(Connection& database,
|
|
85 const std::string& sql);
|
|
86
|
|
87 Statement(Connection& database,
|
|
88 const StatementId& id,
|
|
89 const std::string& sql);
|
|
90
|
|
91 Statement(Connection& database,
|
|
92 const char* sql);
|
|
93
|
|
94 Statement(Connection& database,
|
|
95 const StatementId& id,
|
|
96 const char* sql);
|
|
97
|
|
98 bool Run();
|
|
99
|
|
100 bool Step();
|
|
101
|
|
102 // Resets the statement to its initial condition. This includes any current
|
|
103 // result row, and also the bound variables if the |clear_bound_vars| is true.
|
|
104 void Reset(bool clear_bound_vars = true);
|
|
105
|
|
106 // Diagnostics --------------------------------------------------------------
|
|
107
|
|
108 std::string GetOriginalSQLStatement();
|
|
109
|
|
110
|
|
111 // Binding -------------------------------------------------------------------
|
|
112
|
|
113 // These all take a 0-based argument index
|
|
114 void BindNull(int col);
|
|
115 void BindBool(int col, bool val);
|
|
116 void BindInt(int col, int val);
|
|
117 void BindInt64(int col, int64_t val);
|
|
118 void BindDouble(int col, double val);
|
|
119 void BindCString(int col, const char* val);
|
|
120 void BindString(int col, const std::string& val);
|
|
121 //void BindString16(int col, const string16& value);
|
|
122 void BindBlob(int col, const void* value, int value_len);
|
|
123
|
|
124
|
|
125 // Retrieving ----------------------------------------------------------------
|
|
126
|
|
127 // Returns the number of output columns in the result.
|
|
128 int ColumnCount() const;
|
|
129
|
|
130 // Returns the type associated with the given column.
|
|
131 //
|
|
132 // Watch out: the type may be undefined if you've done something to cause a
|
|
133 // "type conversion." This means requesting the value of a column of a type
|
|
134 // where that type is not the native type. For safety, call ColumnType only
|
|
135 // on a column before getting the value out in any way.
|
|
136 ColumnType GetColumnType(int col) const;
|
|
137 ColumnType GetDeclaredColumnType(int col) const;
|
|
138
|
|
139 // These all take a 0-based argument index.
|
80
|
140 bool ColumnIsNull(int col) const ;
|
0
|
141 bool ColumnBool(int col) const;
|
|
142 int ColumnInt(int col) const;
|
|
143 int64_t ColumnInt64(int col) const;
|
|
144 double ColumnDouble(int col) const;
|
|
145 std::string ColumnString(int col) const;
|
|
146 //string16 ColumnString16(int col) const;
|
|
147
|
|
148 // When reading a blob, you can get a raw pointer to the underlying data,
|
|
149 // along with the length, or you can just ask us to copy the blob into a
|
|
150 // vector. Danger! ColumnBlob may return NULL if there is no data!
|
|
151 int ColumnByteLength(int col) const;
|
|
152 const void* ColumnBlob(int col) const;
|
|
153 bool ColumnBlobAsString(int col, std::string* blob);
|
|
154 //bool ColumnBlobAsString16(int col, string16* val) const;
|
|
155 bool ColumnBlobAsVector(int col, std::vector<char>* val) const;
|
|
156 bool ColumnBlobAsVector(int col, std::vector<unsigned char>* val) const;
|
|
157
|
|
158 };
|
|
159 }
|
|
160 }
|