Mercurial > hg > orthanc-databases
annotate Framework/Odbc/OdbcPreparedStatement.cpp @ 459:ecd0b719cff5
update year to 2024
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 24 Jan 2024 15:25:48 +0100 |
parents | 3d6886f3e5b3 |
children | 54d518dcd74a |
rev | line source |
---|---|
329
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
1 /** |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
2 * Orthanc - A Lightweight, RESTful DICOM Store |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
4 * Department, University Hospital of Liege, Belgium |
459
ecd0b719cff5
update year to 2024
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
389
diff
changeset
|
5 * Copyright (C) 2017-2024 Osimis S.A., Belgium |
ecd0b719cff5
update year to 2024
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
389
diff
changeset
|
6 * Copyright (C) 2021-2024 Sebastien Jodogne, ICTEAM UCLouvain, Belgium |
329
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
7 * |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
8 * This program is free software: you can redistribute it and/or |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
9 * modify it under the terms of the GNU Affero General Public License |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
10 * as published by the Free Software Foundation, either version 3 of |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
11 * the License, or (at your option) any later version. |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
12 * |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
13 * This program is distributed in the hope that it will be useful, but |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
14 * WITHOUT ANY WARRANTY; without even the implied warranty of |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
16 * Affero General Public License for more details. |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
17 * |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
18 * You should have received a copy of the GNU Affero General Public License |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
19 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
20 **/ |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
21 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
22 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
23 #include "OdbcPreparedStatement.h" |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
24 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
25 #include "../Common/InputFileValue.h" |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
26 #include "../Common/Integer64Value.h" |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
27 #include "../Common/Utf8StringValue.h" |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
28 #include "OdbcResult.h" |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
29 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
30 #include <Logging.h> |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
31 #include <OrthancException.h> |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
32 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
33 #include <sqlext.h> |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
34 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
35 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
36 namespace OrthancDatabases |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
37 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
38 void OdbcPreparedStatement::Setup(const Query& query) |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
39 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
40 formatter_.SetNamedDialect(Dialect_MSSQL); /* ODBC uses "?" to name its parameters */ |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
41 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
42 std::string sql; |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
43 query.Format(sql, formatter_); |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
44 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
45 LOG(INFO) << "Preparing ODBC statement: " << sql; |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
46 SQLCHAR* s = const_cast<SQLCHAR*>(reinterpret_cast<const SQLCHAR*>(sql.c_str())); |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
47 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
48 if (!SQL_SUCCEEDED(SQLPrepare(statement_.GetHandle(), s, SQL_NTS))) |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
49 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
50 throw Orthanc::OrthancException(Orthanc::ErrorCode_Database, |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
51 "Cannot prepare ODBC statement: " + sql); |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
52 } |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
53 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
54 paramsIndex_.resize(formatter_.GetParametersCount()); |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
55 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
56 size_t countInt64 = 0; |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
57 size_t countStrings = 0; |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
58 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
59 for (size_t i = 0; i < paramsIndex_.size(); i++) |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
60 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
61 switch (formatter_.GetParameterType(i)) |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
62 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
63 case ValueType_Integer64: |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
64 paramsIndex_[i] = countInt64; |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
65 countInt64++; |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
66 break; |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
67 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
68 case ValueType_InputFile: |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
69 case ValueType_Utf8String: |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
70 paramsIndex_[i] = countStrings; |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
71 countStrings++; |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
72 break; |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
73 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
74 default: |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
75 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented); |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
76 } |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
77 } |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
78 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
79 paramsInt64_.resize(countInt64); |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
80 paramsString_.resize(countStrings); |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
81 } |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
82 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
83 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
84 OdbcPreparedStatement::OdbcPreparedStatement(SQLHSTMT databaseHandle, |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
85 Dialect dialect, |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
86 const Query& query) : |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
87 statement_(databaseHandle), |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
88 formatter_(dialect) |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
89 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
90 Setup(query); |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
91 } |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
92 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
93 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
94 OdbcPreparedStatement::OdbcPreparedStatement(SQLHSTMT databaseHandle, |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
95 Dialect dialect, |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
96 const std::string& sql) : |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
97 statement_(databaseHandle), |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
98 formatter_(dialect) |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
99 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
100 Query query(sql); |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
101 Setup(query); |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
102 } |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
103 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
104 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
105 IResult* OdbcPreparedStatement::Execute() |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
106 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
107 Dictionary parameters; |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
108 return Execute(parameters); |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
109 } |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
110 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
111 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
112 IResult* OdbcPreparedStatement::Execute(const Dictionary& parameters) |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
113 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
114 /** |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
115 * NB: This class creates a copy of all the string parameters from |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
116 * "parameters", because "SQLBindParameter()" assumes that the |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
117 * lifetime of the bound values must be larger than the lifetime |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
118 * of the cursor. This is no problem for the index plugin, but |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
119 * might be problematic if storing large files in the storage area |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
120 * (as this doubles RAM requirements). |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
121 **/ |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
122 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
123 for (size_t i = 0; i < formatter_.GetParametersCount(); i++) |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
124 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
125 const std::string& name = formatter_.GetParameterName(i); |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
126 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
127 if (!parameters.HasKey(name)) |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
128 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
129 throw Orthanc::OrthancException(Orthanc::ErrorCode_InexistentItem, |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
130 "Missing parameter to SQL prepared statement: " + name); |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
131 } |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
132 else |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
133 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
134 const IValue& value = parameters.GetValue(name); |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
135 if (value.GetType() == ValueType_Null) |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
136 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
137 SQLSMALLINT cType, sqlType; |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
138 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
139 switch (formatter_.GetParameterType(i)) |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
140 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
141 case ValueType_Integer64: |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
142 cType = SQL_C_SBIGINT; |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
143 sqlType = SQL_BIGINT; |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
144 break; |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
145 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
146 case ValueType_Utf8String: |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
147 cType = SQL_C_CHAR; |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
148 sqlType = SQL_VARCHAR; |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
149 break; |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
150 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
151 default: |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
152 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented); |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
153 } |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
154 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
155 SQLLEN null = SQL_NULL_DATA; |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
156 if (!SQL_SUCCEEDED(SQLBindParameter(statement_.GetHandle(), i + 1, SQL_PARAM_INPUT, cType, sqlType, |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
157 0 /* ignored */, 0 /* ignored */, NULL, 0, &null))) |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
158 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
159 throw Orthanc::OrthancException(Orthanc::ErrorCode_Database, |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
160 "Cannot bind NULL parameter: " + statement_.FormatError()); |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
161 } |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
162 } |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
163 else if (value.GetType() != formatter_.GetParameterType(i)) |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
164 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
165 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadParameterType, |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
166 "Parameter \"" + name + "\" should be of type \"" + |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
167 EnumerationToString(formatter_.GetParameterType(i)) + |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
168 "\", found \"" + EnumerationToString(value.GetType()) + "\""); |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
169 } |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
170 else |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
171 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
172 assert(i < paramsIndex_.size()); |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
173 size_t index = paramsIndex_[i]; |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
174 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
175 switch (value.GetType()) |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
176 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
177 case ValueType_Integer64: |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
178 assert(index < paramsInt64_.size()); |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
179 paramsInt64_[index] = dynamic_cast<const Integer64Value&>(value).GetValue(); |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
180 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
181 if (!SQL_SUCCEEDED(SQLBindParameter(statement_.GetHandle(), i + 1, SQL_PARAM_INPUT, SQL_C_SBIGINT, SQL_BIGINT, |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
182 0 /* ignored */, 0 /* ignored */, ¶msInt64_[index], |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
183 sizeof(int64_t), NULL))) |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
184 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
185 throw Orthanc::OrthancException(Orthanc::ErrorCode_Database, |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
186 "Cannot bind integer parameter: " + statement_.FormatError()); |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
187 } |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
188 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
189 break; |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
190 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
191 case ValueType_Utf8String: |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
192 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
193 assert(index < paramsString_.size()); |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
194 paramsString_[index] = dynamic_cast<const Utf8StringValue&>(value).GetContent(); |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
195 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
196 const char* content = (paramsString_[index].empty() ? "" : paramsString_[index].c_str()); |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
197 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
198 if (!SQL_SUCCEEDED(SQLBindParameter( |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
199 statement_.GetHandle(), i + 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_VARCHAR, |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
200 0 /* ignored */, 0 /* ignored */, const_cast<char*>(content), paramsString_[index].size(), NULL))) |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
201 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
202 throw Orthanc::OrthancException(Orthanc::ErrorCode_Database, |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
203 "Cannot bind UTF-8 parameter: " + statement_.FormatError()); |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
204 } |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
205 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
206 break; |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
207 } |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
208 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
209 case ValueType_InputFile: |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
210 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
211 assert(index < paramsString_.size()); |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
212 paramsString_[index] = dynamic_cast<const InputFileValue&>(value).GetContent(); |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
213 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
214 const char* content = (paramsString_[index].empty() ? NULL : paramsString_[index].c_str()); |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
215 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
216 SQLLEN a = paramsString_[index].size(); |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
217 if (!SQL_SUCCEEDED(SQLBindParameter( |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
218 statement_.GetHandle(), i + 1, SQL_PARAM_INPUT, SQL_C_BINARY, SQL_LONGVARBINARY, |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
219 paramsString_[index].size() /* only used by MSSQL */, |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
220 0 /* ignored */, const_cast<char*>(content), 0, &a))) |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
221 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
222 throw Orthanc::OrthancException(Orthanc::ErrorCode_Database, |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
223 "Cannot bind binary parameter: " + statement_.FormatError()); |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
224 } |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
225 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
226 break; |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
227 } |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
228 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
229 default: |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
230 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented); |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
231 } |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
232 } |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
233 } |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
234 } |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
235 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
236 const Dialect dialect = formatter_.GetAutoincrementDialect(); |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
237 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
238 SQLRETURN code = SQLExecute(statement_.GetHandle()); |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
239 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
240 if (code == SQL_SUCCESS || |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
241 code == SQL_NO_DATA /* this is the case of DELETE in PostgreSQL and MSSQL */) |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
242 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
243 return new OdbcResult(statement_, dialect); |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
244 } |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
245 else |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
246 { |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
247 statement_.CheckCollision(dialect); |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
248 |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
249 throw Orthanc::OrthancException(Orthanc::ErrorCode_Database, |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
250 "Cannot execute ODBC statement:\n" + statement_.FormatError()); |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
251 } |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
252 } |
b5fb8b77ce4d
initial commit of ODBC framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
253 } |