comparison Framework/SQLite/SQLiteStatement.cpp @ 0:7cea966b6829

initial commit
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 04 Jul 2018 08:16:29 +0200
parents
children b2ff1cd2907a
comparison
equal deleted inserted replaced
-1:000000000000 0:7cea966b6829
1 /**
2 * Orthanc - A Lightweight, RESTful DICOM Store
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
4 * Department, University Hospital of Liege, Belgium
5 * Copyright (C) 2017-2018 Osimis S.A., Belgium
6 *
7 * This program is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU Affero General Public License
9 * as published by the Free Software Foundation, either version 3 of
10 * the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Affero General Public License for more details.
16 *
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 **/
20
21
22 #include "SQLiteStatement.h"
23
24 #include "../Common/BinaryStringValue.h"
25 #include "../Common/FileValue.h"
26 #include "../Common/Integer64Value.h"
27 #include "../Common/Query.h"
28 #include "../Common/Utf8StringValue.h"
29 #include "SQLiteResult.h"
30
31 #include <Core/OrthancException.h>
32
33 namespace OrthancDatabases
34 {
35 SQLiteStatement::SQLiteStatement(SQLiteDatabase& database,
36 const Query& query) :
37 readOnly_(query.IsReadOnly()),
38 formatter_(Dialect_SQLite)
39 {
40 std::string sql;
41 query.Format(sql, formatter_);
42
43 statement_.reset(new Orthanc::SQLite::Statement(database.GetObject(), sql));
44 }
45
46
47 Orthanc::SQLite::Statement& SQLiteStatement::GetObject()
48 {
49 if (statement_.get() == NULL)
50 {
51 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
52 }
53 else
54 {
55 return *statement_;
56 }
57 }
58
59
60 void SQLiteStatement::BindParameters(const Dictionary& parameters)
61 {
62 statement_->Reset();
63
64 for (size_t i = 0; i < formatter_.GetParametersCount(); i++)
65 {
66 const std::string& name = formatter_.GetParameterName(i);
67
68 switch (formatter_.GetParameterType(i))
69 {
70 case ValueType_BinaryString:
71 {
72 const BinaryStringValue& blob =
73 dynamic_cast<const BinaryStringValue&>(parameters.GetValue(name));
74 statement_->BindBlob(i, blob.GetBuffer(), blob.GetSize());
75 break;
76 }
77
78 case ValueType_File:
79 {
80 const FileValue& blob =
81 dynamic_cast<const FileValue&>(parameters.GetValue(name));
82 statement_->BindBlob(i, blob.GetBuffer(), blob.GetSize());
83 break;
84 }
85
86 case ValueType_Integer64:
87 statement_->BindInt64(i, dynamic_cast<const Integer64Value&>
88 (parameters.GetValue(name)).GetValue());
89 break;
90
91 case ValueType_Null:
92 statement_->BindNull(i);
93 break;
94
95 case ValueType_Utf8String:
96 statement_->BindString(i, dynamic_cast<const Utf8StringValue&>
97 (parameters.GetValue(name)).GetContent());
98 break;
99
100 default:
101 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
102 }
103 }
104 }
105
106
107 IResult* SQLiteStatement::Execute(SQLiteTransaction& transaction,
108 const Dictionary& parameters)
109 {
110 BindParameters(parameters);
111 return new SQLiteResult(*this);
112 }
113
114
115 void SQLiteStatement::ExecuteWithoutResult(SQLiteTransaction& transaction,
116 const Dictionary& parameters)
117 {
118 BindParameters(parameters);
119
120 if (!statement_->Run())
121 {
122 throw Orthanc::OrthancException(Orthanc::ErrorCode_Database);
123 }
124 }
125 }