Mercurial > hg > orthanc
annotate Core/SQLite/Connection.h @ 2044:0f35383dd6cc
doc
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 23 Jun 2016 17:32:01 +0200 |
parents | b1291df2f780 |
children | f31dfb131dee |
rev | line source |
---|---|
0 | 1 /** |
59 | 2 * Orthanc - A Lightweight, RESTful DICOM Store |
1220
9b9026560a5f
SQLite wrapper is now fully independent of Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
3 * |
1900 | 4 * Copyright (C) 2012-2016 Sebastien Jodogne <s.jodogne@gmail.com>, |
1220
9b9026560a5f
SQLite wrapper is now fully independent of Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
5 * Medical Physics Department, CHU of Liege, Belgium |
0 | 6 * |
17 | 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: | |
0 | 12 * |
17 | 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. | |
0 | 35 **/ |
36 | |
37 | |
38 #pragma once | |
39 | |
40 #include "Statement.h" | |
41 #include "IScalarFunction.h" | |
42 | |
43 #include <string> | |
44 #include <map> | |
45 | |
46 struct sqlite3; | |
47 struct sqlite3_stmt; | |
48 | |
1672
4c5a85c3ff43
sample database plugin now working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1291
diff
changeset
|
49 #define SQLITE_FROM_HERE ::Orthanc::SQLite::StatementId(__FILE__, __LINE__) |
0 | 50 |
59 | 51 namespace Orthanc |
0 | 52 { |
53 namespace SQLite | |
54 { | |
1221
2255e66da726
getting rid of the dependency against Boost in the SQLite C++ wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1220
diff
changeset
|
55 class Connection : NonCopyable |
0 | 56 { |
57 friend class Statement; | |
58 friend class Transaction; | |
59 | |
60 private: | |
61 // All cached statements. Keeping a reference to these statements means that | |
62 // they'll remain active. | |
63 typedef std::map<StatementId, StatementReference*> CachedStatements; | |
64 CachedStatements cachedStatements_; | |
65 | |
66 // The actual sqlite database. Will be NULL before Init has been called or if | |
67 // Init resulted in an error. | |
68 sqlite3* db_; | |
69 | |
70 // Number of currently-nested transactions. | |
71 int transactionNesting_; | |
72 | |
73 // True if any of the currently nested transactions have been rolled back. | |
74 // When we get to the outermost transaction, this will determine if we do | |
75 // a rollback instead of a commit. | |
76 bool needsRollback_; | |
77 | |
78 void ClearCache(); | |
79 | |
80 void CheckIsOpen() const; | |
81 | |
82 sqlite3* GetWrappedObject() | |
83 { | |
84 return db_; | |
85 } | |
86 | |
87 StatementReference& GetCachedStatement(const StatementId& id, | |
88 const char* sql); | |
89 | |
90 bool DoesTableOrIndexExist(const char* name, | |
91 const char* type) const; | |
92 | |
93 void DoRollback(); | |
94 | |
95 public: | |
96 // The database is opened by calling Open[InMemory](). Any uncommitted | |
97 // transactions will be rolled back when this object is deleted. | |
98 Connection(); | |
99 ~Connection(); | |
100 | |
101 void Open(const std::string& path); | |
102 | |
103 void OpenInMemory(); | |
104 | |
105 void Close(); | |
106 | |
107 bool Execute(const char* sql); | |
108 | |
109 bool Execute(const std::string& sql) | |
110 { | |
111 return Execute(sql.c_str()); | |
112 } | |
113 | |
206
4453a010d0db
flush to disk thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
114 void FlushToDisk(); |
4453a010d0db
flush to disk thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
115 |
0 | 116 IScalarFunction* Register(IScalarFunction* func); // Takes the ownership of the function |
117 | |
118 // Info querying ------------------------------------------------------------- | |
119 | |
120 // Used to check a |sql| statement for syntactic validity. If the | |
121 // statement is valid SQL, returns true. | |
122 bool IsSQLValid(const char* sql); | |
123 | |
124 // Returns true if the given table exists. | |
125 bool DoesTableExist(const char* table_name) const; | |
126 | |
127 // Returns true if the given index exists. | |
128 bool DoesIndexExist(const char* index_name) const; | |
129 | |
130 // Returns true if a column with the given name exists in the given table. | |
131 bool DoesColumnExist(const char* table_name, const char* column_name) const; | |
132 | |
133 // Returns sqlite's internal ID for the last inserted row. Valid only | |
134 // immediately after an insert. | |
135 int64_t GetLastInsertRowId() const; | |
136 | |
137 // Returns sqlite's count of the number of rows modified by the last | |
138 // statement executed. Will be 0 if no statement has executed or the database | |
139 // is closed. | |
140 int GetLastChangeCount() const; | |
141 | |
142 // Errors -------------------------------------------------------------------- | |
143 | |
144 // Returns the error code associated with the last sqlite operation. | |
145 int GetErrorCode() const; | |
146 | |
147 // Returns the errno associated with GetErrorCode(). See | |
148 // SQLITE_LAST_ERRNO in SQLite documentation. | |
149 int GetLastErrno() const; | |
150 | |
151 // Returns a pointer to a statically allocated string associated with the | |
152 // last sqlite operation. | |
153 const char* GetErrorMessage() const; | |
154 | |
155 | |
156 // Diagnostics (for unit tests) ---------------------------------------------- | |
157 | |
158 int ExecuteAndReturnErrorCode(const char* sql); | |
159 | |
160 bool HasCachedStatement(const StatementId& id) const | |
161 { | |
162 return cachedStatements_.find(id) != cachedStatements_.end(); | |
163 } | |
164 | |
165 int GetTransactionNesting() const | |
166 { | |
167 return transactionNesting_; | |
168 } | |
169 | |
170 // Transactions -------------------------------------------------------------- | |
171 | |
172 bool BeginTransaction(); | |
173 void RollbackTransaction(); | |
206
4453a010d0db
flush to disk thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
174 bool CommitTransaction(); |
0 | 175 }; |
176 } | |
177 } |