1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561
|
/**
* Orthanc - A Lightweight, RESTful DICOM Store
* Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
* Department, University Hospital of Liege, Belgium
* Copyright (C) 2017-2023 Osimis S.A., Belgium
* Copyright (C) 2021-2023 Sebastien Jodogne, ICTEAM UCLouvain, Belgium
*
* This program is free software: you can redistribute it and/or
* modify it under the terms of the GNU Affero General Public License
* as published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
**/
#include "PostgreSQLIncludes.h" // Must be the first
#include "PostgreSQLStatement.h"
#include "../Common/BinaryStringValue.h"
#include "../Common/InputFileValue.h"
#include "../Common/Integer64Value.h"
#include "../Common/NullValue.h"
#include "../Common/ResultBase.h"
#include "../Common/Utf8StringValue.h"
#include "PostgreSQLResult.h"
#include <Compatibility.h> // For std::unique_ptr<>
#include <Logging.h>
#include <OrthancException.h>
#include <Toolbox.h>
#include <Endianness.h>
#include <cassert>
namespace OrthancDatabases
{
class PostgreSQLStatement::Inputs : public boost::noncopyable
{
private:
std::vector<char*> values_;
std::vector<int> sizes_;
static char* Allocate(const void* source, int size)
{
if (size == 0)
{
return NULL;
}
else
{
char* ptr = reinterpret_cast<char*>(malloc(size));
if (source != NULL)
{
memcpy(ptr, source, size);
}
return ptr;
}
}
void Resize(size_t size)
{
// Shrinking of the vector
for (size_t i = size; i < values_.size(); i++)
{
if (values_[i] != NULL)
free(values_[i]);
}
values_.resize(size, NULL);
sizes_.resize(size, 0);
}
void EnlargeForIndex(size_t index)
{
if (index >= values_.size())
{
// The vector is too small
Resize(index + 1);
}
}
public:
Inputs()
{
}
~Inputs()
{
Resize(0);
}
void SetItem(size_t pos, const void* source, int size)
{
EnlargeForIndex(pos);
if (sizes_[pos] == size)
{
if (source && size != 0)
{
memcpy(values_[pos], source, size);
}
}
else
{
if (values_[pos] != NULL)
{
free(values_[pos]);
}
values_[pos] = Allocate(source, size);
sizes_[pos] = size;
}
}
void SetItem(size_t pos, int size)
{
SetItem(pos, NULL, size);
}
void* GetItem(size_t pos) const
{
if (pos >= values_.size())
{
throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
}
return values_[pos];
}
const std::vector<char*>& GetValues() const
{
return values_;
}
const std::vector<int>& GetSizes() const
{
return sizes_;
}
};
void PostgreSQLStatement::Prepare()
{
if (id_.size() > 0)
{
// Already prepared
return;
}
for (size_t i = 0; i < oids_.size(); i++)
{
if (oids_[i] == 0)
{
// The type of an input parameter was not set
throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls);
}
}
id_ = Orthanc::Toolbox::GenerateUuid();
const unsigned int* tmp = oids_.size() ? &oids_[0] : NULL;
PGresult* result = PQprepare(reinterpret_cast<PGconn*>(database_.pg_),
id_.c_str(), sql_.c_str(), oids_.size(), tmp);
if (result == NULL)
{
id_.clear();
database_.ThrowException(true);
}
bool ok = (PQresultStatus(result) == PGRES_COMMAND_OK);
if (ok)
{
PQclear(result);
}
else
{
std::string message = PQresultErrorMessage(result);
PQclear(result);
id_.clear();
LOG(ERROR) << "PostgreSQL error: " << message;
database_.ThrowException(false);
}
}
void PostgreSQLStatement::Unprepare()
{
if (id_.size() > 0)
{
// "Although there is no libpq function for deleting a
// prepared statement, the SQL DEALLOCATE statement can be
// used for that purpose."
database_.ExecuteMultiLines("DEALLOCATE \"" + id_ + "\"");
}
id_.clear();
}
void PostgreSQLStatement::DeclareInputInternal(unsigned int param,
unsigned int /*Oid*/ type)
{
Unprepare();
if (oids_.size() <= param)
{
oids_.resize(param + 1, 0);
binary_.resize(param + 1);
}
oids_[param] = type;
binary_[param] = (type == TEXTOID || type == BYTEAOID || type == OIDOID) ? 0 : 1;
}
void PostgreSQLStatement::DeclareInputInteger(unsigned int param)
{
DeclareInputInternal(param, INT4OID);
}
void PostgreSQLStatement::DeclareInputInteger64(unsigned int param)
{
DeclareInputInternal(param, INT8OID);
}
void PostgreSQLStatement::DeclareInputString(unsigned int param)
{
DeclareInputInternal(param, TEXTOID);
}
void PostgreSQLStatement::DeclareInputBinary(unsigned int param)
{
DeclareInputInternal(param, BYTEAOID);
}
void PostgreSQLStatement::DeclareInputLargeObject(unsigned int param)
{
DeclareInputInternal(param, OIDOID);
}
void* /* PGresult* */ PostgreSQLStatement::Execute()
{
Prepare();
PGresult* result;
if (oids_.size() == 0)
{
// No parameter
result = PQexecPrepared(reinterpret_cast<PGconn*>(database_.pg_),
id_.c_str(), 0, NULL, NULL, NULL, 1);
}
else
{
// At least 1 parameter
result = PQexecPrepared(reinterpret_cast<PGconn*>(database_.pg_),
id_.c_str(),
oids_.size(),
&inputs_->GetValues()[0],
&inputs_->GetSizes()[0],
&binary_[0],
1);
}
if (PQtransactionStatus(reinterpret_cast<PGconn*>(database_.pg_)) == PQTRANS_INERROR)
{
if (result != NULL)
{
PQclear(result);
}
#if ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 9, 2)
throw Orthanc::OrthancException(Orthanc::ErrorCode_DatabaseCannotSerialize);
#else
throw Orthanc::OrthancException(Orthanc::ErrorCode_Database, "Collision between multiple writers");
#endif
}
else if (result == NULL)
{
database_.ThrowException(true);
}
return result;
}
PostgreSQLStatement::PostgreSQLStatement(PostgreSQLDatabase& database,
const std::string& sql) :
database_(database),
sql_(sql),
inputs_(new Inputs),
formatter_(Dialect_PostgreSQL)
{
LOG(TRACE) << "PostgreSQL: " << sql;
}
PostgreSQLStatement::PostgreSQLStatement(PostgreSQLDatabase& database,
const Query& query) :
database_(database),
inputs_(new Inputs),
formatter_(Dialect_PostgreSQL)
{
query.Format(sql_, formatter_);
LOG(TRACE) << "PostgreSQL: " << sql_;
for (size_t i = 0; i < formatter_.GetParametersCount(); i++)
{
switch (formatter_.GetParameterType(i))
{
case ValueType_Integer64:
DeclareInputInteger64(i);
break;
case ValueType_Utf8String:
DeclareInputString(i);
break;
case ValueType_BinaryString:
DeclareInputBinary(i);
break;
case ValueType_InputFile:
DeclareInputLargeObject(i);
break;
case ValueType_Null:
default:
throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented);
}
}
}
PostgreSQLStatement::~PostgreSQLStatement()
{
try
{
Unprepare();
}
catch (Orthanc::OrthancException&)
{
// Ignore possible exceptions due to connection loss
}
}
void PostgreSQLStatement::Run()
{
PGresult* result = reinterpret_cast<PGresult*>(Execute());
assert(result != NULL); // An exception would have been thrown otherwise
bool ok = (PQresultStatus(result) == PGRES_COMMAND_OK ||
PQresultStatus(result) == PGRES_TUPLES_OK);
if (ok)
{
PQclear(result);
}
else
{
std::string error = PQresultErrorMessage(result);
PQclear(result);
LOG(ERROR) << "PostgreSQL error: " << error;
database_.ThrowException(false);
}
}
void PostgreSQLStatement::BindNull(unsigned int param)
{
if (param >= oids_.size())
{
throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
}
inputs_->SetItem(param, 0);
}
void PostgreSQLStatement::BindInteger(unsigned int param,
int value)
{
if (param >= oids_.size())
{
throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
}
if (oids_[param] != INT4OID)
{
throw Orthanc::OrthancException(Orthanc::ErrorCode_BadParameterType);
}
assert(sizeof(int32_t) == 4);
int32_t v = htobe32(static_cast<int32_t>(value));
inputs_->SetItem(param, &v, sizeof(int32_t));
}
void PostgreSQLStatement::BindInteger64(unsigned int param,
int64_t value)
{
if (param >= oids_.size())
{
throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
}
if (oids_[param] != INT8OID)
{
throw Orthanc::OrthancException(Orthanc::ErrorCode_BadParameterType);
}
assert(sizeof(int64_t) == 8);
int64_t v = htobe64(value);
inputs_->SetItem(param, &v, sizeof(int64_t));
}
void PostgreSQLStatement::BindString(unsigned int param,
const std::string& value)
{
if (param >= oids_.size())
{
throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
}
if (oids_[param] != TEXTOID && oids_[param] != BYTEAOID)
{
throw Orthanc::OrthancException(Orthanc::ErrorCode_BadParameterType);
}
if (value.size() == 0)
{
inputs_->SetItem(param, "", 1 /* end-of-string character */);
}
else
{
inputs_->SetItem(param, value.c_str(),
value.size() + 1); // "+1" for end-of-string character
}
}
void PostgreSQLStatement::BindLargeObject(unsigned int param,
const PostgreSQLLargeObject& value)
{
if (param >= oids_.size())
{
throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
}
if (oids_[param] != OIDOID)
{
throw Orthanc::OrthancException(Orthanc::ErrorCode_BadParameterType);
}
inputs_->SetItem(param, value.GetOid().c_str(),
value.GetOid().size() + 1); // "+1" for end-of-string character
}
class PostgreSQLStatement::ResultWrapper : public ResultBase
{
private:
std::unique_ptr<PostgreSQLResult> result_;
protected:
virtual IValue* FetchField(size_t index)
{
return result_->GetValue(index);
}
public:
explicit ResultWrapper(PostgreSQLStatement& statement) :
result_(new PostgreSQLResult(statement))
{
SetFieldsCount(result_->GetColumnsCount());
FetchFields();
}
virtual void Next()
{
result_->Next();
FetchFields();
}
virtual bool IsDone() const
{
return result_->IsDone();
}
};
IResult* PostgreSQLStatement::Execute(ITransaction& transaction,
const Dictionary& parameters)
{
for (size_t i = 0; i < formatter_.GetParametersCount(); i++)
{
const std::string& name = formatter_.GetParameterName(i);
switch (formatter_.GetParameterType(i))
{
case ValueType_Integer64:
BindInteger64(i, dynamic_cast<const Integer64Value&>(parameters.GetValue(name)).GetValue());
break;
case ValueType_Null:
BindNull(i);
break;
case ValueType_Utf8String:
BindString(i, dynamic_cast<const Utf8StringValue&>
(parameters.GetValue(name)).GetContent());
break;
case ValueType_BinaryString:
BindString(i, dynamic_cast<const BinaryStringValue&>
(parameters.GetValue(name)).GetContent());
break;
case ValueType_InputFile:
{
const InputFileValue& blob =
dynamic_cast<const InputFileValue&>(parameters.GetValue(name));
PostgreSQLLargeObject largeObject(database_, blob.GetContent());
BindLargeObject(i, largeObject);
break;
}
default:
throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
}
}
return new ResultWrapper(*this);
}
void PostgreSQLStatement::ExecuteWithoutResult(ITransaction& transaction,
const Dictionary& parameters)
{
std::unique_ptr<IResult> dummy(Execute(transaction, parameters));
}
}
|