Mercurial > hg > orthanc-databases
comparison Framework/PostgreSQL/PostgreSQLResult.cpp @ 0:7cea966b6829
initial commit
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 04 Jul 2018 08:16:29 +0200 |
parents | |
children | 9774802fd05f |
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 "PostgreSQLResult.h" | |
23 | |
24 #include "../Common/BinaryStringValue.h" | |
25 #include "../Common/Integer64Value.h" | |
26 #include "../Common/NullValue.h" | |
27 #include "../Common/Utf8StringValue.h" | |
28 | |
29 #include <Core/OrthancException.h> | |
30 #include <Core/Logging.h> | |
31 | |
32 #include <cassert> | |
33 #include <boost/lexical_cast.hpp> | |
34 | |
35 // PostgreSQL includes | |
36 #include <libpq-fe.h> | |
37 #include <c.h> | |
38 #include <catalog/pg_type.h> | |
39 | |
40 #include <Core/Endianness.h> | |
41 | |
42 | |
43 namespace OrthancDatabases | |
44 { | |
45 void PostgreSQLResult::Clear() | |
46 { | |
47 if (result_ != NULL) | |
48 { | |
49 PQclear(reinterpret_cast<PGresult*>(result_)); | |
50 result_ = NULL; | |
51 } | |
52 } | |
53 | |
54 | |
55 void PostgreSQLResult::CheckDone() | |
56 { | |
57 if (position_ >= PQntuples(reinterpret_cast<PGresult*>(result_))) | |
58 { | |
59 // We are at the end of the result set | |
60 Clear(); | |
61 } | |
62 } | |
63 | |
64 | |
65 void PostgreSQLResult::CheckColumn(unsigned int column, unsigned int /*Oid*/ expectedType) const | |
66 { | |
67 if (IsDone()) | |
68 { | |
69 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); | |
70 } | |
71 | |
72 if (column >= static_cast<unsigned int>(PQnfields(reinterpret_cast<PGresult*>(result_)))) | |
73 { | |
74 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); | |
75 } | |
76 | |
77 if (expectedType != 0 && | |
78 expectedType != PQftype(reinterpret_cast<PGresult*>(result_), column)) | |
79 { | |
80 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadParameterType); | |
81 } | |
82 } | |
83 | |
84 | |
85 PostgreSQLResult::PostgreSQLResult(PostgreSQLStatement& statement) : | |
86 position_(0), | |
87 database_(statement.GetDatabase()) | |
88 { | |
89 result_ = statement.Execute(); | |
90 assert(result_ != NULL); // An exception would have been thrown otherwise | |
91 | |
92 // This is the first call to "Next()" | |
93 if (PQresultStatus(reinterpret_cast<PGresult*>(result_)) == PGRES_TUPLES_OK) | |
94 { | |
95 CheckDone(); | |
96 columnsCount_ = static_cast<unsigned int>(PQnfields(reinterpret_cast<PGresult*>(result_))); | |
97 } | |
98 else | |
99 { | |
100 // This is not a SELECT request, we're done | |
101 Clear(); | |
102 columnsCount_ = 0; | |
103 } | |
104 } | |
105 | |
106 | |
107 void PostgreSQLResult::Next() | |
108 { | |
109 position_++; | |
110 CheckDone(); | |
111 } | |
112 | |
113 | |
114 bool PostgreSQLResult::IsNull(unsigned int column) const | |
115 { | |
116 CheckColumn(column, 0); | |
117 return PQgetisnull(reinterpret_cast<PGresult*>(result_), position_, column) != 0; | |
118 } | |
119 | |
120 | |
121 bool PostgreSQLResult::GetBoolean(unsigned int column) const | |
122 { | |
123 CheckColumn(column, BOOLOID); | |
124 assert(PQfsize(reinterpret_cast<PGresult*>(result_), column) == 1); | |
125 const uint8_t *v = reinterpret_cast<const uint8_t*> | |
126 (PQgetvalue(reinterpret_cast<PGresult*>(result_), position_, column)); | |
127 return (v[0] != 0); | |
128 } | |
129 | |
130 | |
131 int PostgreSQLResult::GetInteger(unsigned int column) const | |
132 { | |
133 CheckColumn(column, INT4OID); | |
134 assert(PQfsize(reinterpret_cast<PGresult*>(result_), column) == 4); | |
135 char *v = PQgetvalue(reinterpret_cast<PGresult*>(result_), position_, column); | |
136 return htobe32(*reinterpret_cast<int32_t*>(v)); | |
137 } | |
138 | |
139 | |
140 int64_t PostgreSQLResult::GetInteger64(unsigned int column) const | |
141 { | |
142 CheckColumn(column, INT8OID); | |
143 assert(PQfsize(reinterpret_cast<PGresult*>(result_), column) == 8); | |
144 char *v = PQgetvalue(reinterpret_cast<PGresult*>(result_), position_, column); | |
145 return htobe64(*reinterpret_cast<int64_t*>(v)); | |
146 } | |
147 | |
148 | |
149 std::string PostgreSQLResult::GetString(unsigned int column) const | |
150 { | |
151 CheckColumn(column, 0); | |
152 | |
153 Oid oid = PQftype(reinterpret_cast<PGresult*>(result_), column); | |
154 if (oid != TEXTOID && oid != VARCHAROID && oid != BYTEAOID) | |
155 { | |
156 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadParameterType); | |
157 } | |
158 | |
159 return std::string(PQgetvalue(reinterpret_cast<PGresult*>(result_), position_, column)); | |
160 } | |
161 | |
162 | |
163 void PostgreSQLResult::GetLargeObject(std::string& result, | |
164 unsigned int column) const | |
165 { | |
166 CheckColumn(column, OIDOID); | |
167 | |
168 Oid oid; | |
169 assert(PQfsize(reinterpret_cast<PGresult*>(result_), column) == sizeof(oid)); | |
170 | |
171 oid = *(const Oid*) PQgetvalue(reinterpret_cast<PGresult*>(result_), position_, column); | |
172 oid = ntohl(oid); | |
173 | |
174 PostgreSQLLargeObject::Read(result, database_, boost::lexical_cast<std::string>(oid)); | |
175 } | |
176 | |
177 | |
178 void PostgreSQLResult::GetLargeObject(void*& result, | |
179 size_t& size, | |
180 unsigned int column) const | |
181 { | |
182 CheckColumn(column, OIDOID); | |
183 | |
184 Oid oid; | |
185 assert(PQfsize(reinterpret_cast<PGresult*>(result_), column) == sizeof(oid)); | |
186 | |
187 oid = *(const Oid*) PQgetvalue(reinterpret_cast<PGresult*>(result_), position_, column); | |
188 oid = ntohl(oid); | |
189 | |
190 PostgreSQLLargeObject::Read(result, size, database_, boost::lexical_cast<std::string>(oid)); | |
191 } | |
192 | |
193 | |
194 IValue* PostgreSQLResult::GetValue(unsigned int column) const | |
195 { | |
196 if (IsNull(column)) | |
197 { | |
198 return new NullValue; | |
199 } | |
200 | |
201 Oid type = PQftype(reinterpret_cast<PGresult*>(result_), column); | |
202 | |
203 switch (type) | |
204 { | |
205 case BOOLOID: | |
206 // Convert Boolean values as integers | |
207 return new Integer64Value(GetBoolean(column) ? 1 : 0); | |
208 | |
209 case INT4OID: | |
210 return new Integer64Value(GetInteger(column)); | |
211 | |
212 case INT8OID: | |
213 return new Integer64Value(GetInteger64(column)); | |
214 | |
215 case TEXTOID: | |
216 case VARCHAROID: | |
217 return new Utf8StringValue(GetString(column)); | |
218 | |
219 case BYTEAOID: | |
220 return new BinaryStringValue(GetString(column)); | |
221 | |
222 case OIDOID: | |
223 { | |
224 std::auto_ptr<BinaryStringValue> value(new BinaryStringValue); | |
225 GetLargeObject(value->GetContent(), column); | |
226 return value.release(); | |
227 } | |
228 | |
229 default: | |
230 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented); | |
231 } | |
232 } | |
233 } |