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