comparison Orthanc/Core/SQLite/StatementReference.cpp @ 78:d6da56f86e5a

sync
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 25 Sep 2015 11:29:17 +0200
parents
children 3809121c3290
comparison
equal deleted inserted replaced
77:f44ebb25691c 78:d6da56f86e5a
1 /**
2 * Orthanc - A Lightweight, RESTful DICOM Store
3 *
4 * Copyright (C) 2012-2015 Sebastien Jodogne <s.jodogne@gmail.com>,
5 * Medical Physics Department, CHU of Liege, Belgium
6 *
7 * Copyright (c) 2012 The Chromium Authors. All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are
11 * met:
12 *
13 * * Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * * Redistributions in binary form must reproduce the above
16 * copyright notice, this list of conditions and the following disclaimer
17 * in the documentation and/or other materials provided with the
18 * distribution.
19 * * Neither the name of Google Inc., the name of the CHU of Liege,
20 * nor the names of its contributors may be used to endorse or promote
21 * products derived from this software without specific prior written
22 * permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 **/
36
37
38 #if ORTHANC_SQLITE_STANDALONE != 1
39 #include "../PrecompiledHeaders.h"
40 #endif
41
42 #include "StatementReference.h"
43 #include "OrthancSQLiteException.h"
44
45 #if ORTHANC_SQLITE_STANDALONE != 1
46 #include "../Logging.h"
47 #endif
48
49 #include <string>
50 #include <cassert>
51 #include "sqlite3.h"
52
53 namespace Orthanc
54 {
55 namespace SQLite
56 {
57 bool StatementReference::IsRoot() const
58 {
59 return root_ == NULL;
60 }
61
62 StatementReference::StatementReference()
63 {
64 root_ = NULL;
65 refCount_ = 0;
66 statement_ = NULL;
67 assert(IsRoot());
68 }
69
70 StatementReference::StatementReference(sqlite3* database,
71 const char* sql)
72 {
73 if (database == NULL || sql == NULL)
74 {
75 throw OrthancSQLiteException(ErrorCode_ParameterOutOfRange);
76 }
77
78 root_ = NULL;
79 refCount_ = 0;
80
81 int error = sqlite3_prepare_v2(database, sql, -1, &statement_, NULL);
82 if (error != SQLITE_OK)
83 {
84 #if ORTHANC_SQLITE_STANDALONE != 1
85 LOG(ERROR) << "SQLite: " << sqlite3_errmsg(database);
86 #endif
87
88 throw OrthancSQLiteException(ErrorCode_SQLitePrepareStatement);
89 }
90
91 assert(IsRoot());
92 }
93
94 StatementReference::StatementReference(StatementReference& other)
95 {
96 refCount_ = 0;
97
98 if (other.IsRoot())
99 {
100 root_ = &other;
101 }
102 else
103 {
104 root_ = other.root_;
105 }
106
107 root_->refCount_++;
108 statement_ = root_->statement_;
109
110 assert(!IsRoot());
111 }
112
113 StatementReference::~StatementReference()
114 {
115 if (IsRoot())
116 {
117 if (refCount_ != 0)
118 {
119 // There remain references to this object. We cannot throw
120 // an exception because:
121 // http://www.parashift.com/c++-faq/dtors-shouldnt-throw.html
122
123 #if ORTHANC_SQLITE_STANDALONE != 1
124 LOG(ERROR) << "Bad value of the reference counter";
125 #endif
126 }
127 else if (statement_ != NULL)
128 {
129 sqlite3_finalize(statement_);
130 }
131 }
132 else
133 {
134 if (root_->refCount_ == 0)
135 {
136 // There remain references to this object. We cannot throw
137 // an exception because:
138 // http://www.parashift.com/c++-faq/dtors-shouldnt-throw.html
139
140 #if ORTHANC_SQLITE_STANDALONE != 1
141 LOG(ERROR) << "Bad value of the reference counter";
142 #endif
143 }
144 else
145 {
146 root_->refCount_--;
147 }
148 }
149 }
150
151 uint32_t StatementReference::GetReferenceCount() const
152 {
153 return refCount_;
154 }
155 }
156 }