Mercurial > hg > orthanc
annotate OrthancFramework/Sources/SQLite/Connection.h @ 5577:9e74e761b108 find-refactoring
integration mainline->find-refactoring
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 26 Apr 2024 17:43:22 +0200 |
parents | 8b507b1514eb |
children | 3f13db27b399 |
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 * |
3063 | 4 * Copyright (C) 2012-2016 Sebastien Jodogne <s.jodogne@orthanc-labs.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 |
5485
48b8dae6dc77
upgrade to year 2024
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
5357
diff
changeset
|
6 * Copyright (C) 2017-2024 Osimis S.A., Belgium |
48b8dae6dc77
upgrade to year 2024
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
5357
diff
changeset
|
7 * Copyright (C) 2021-2024 Sebastien Jodogne, ICTEAM UCLouvain, Belgium |
0 | 8 * |
17 | 9 * Copyright (c) 2012 The Chromium Authors. All rights reserved. |
10 * | |
11 * Redistribution and use in source and binary forms, with or without | |
12 * modification, are permitted provided that the following conditions are | |
13 * met: | |
0 | 14 * |
17 | 15 * * Redistributions of source code must retain the above copyright |
16 * notice, this list of conditions and the following disclaimer. | |
17 * * Redistributions in binary form must reproduce the above | |
18 * copyright notice, this list of conditions and the following disclaimer | |
19 * in the documentation and/or other materials provided with the | |
20 * distribution. | |
21 * * Neither the name of Google Inc., the name of the CHU of Liege, | |
22 * nor the names of its contributors may be used to endorse or promote | |
23 * products derived from this software without specific prior written | |
24 * permission. | |
25 * | |
26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
27 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
28 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
29 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
30 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
31 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
32 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
33 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
34 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
35 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
36 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
0 | 37 **/ |
38 | |
39 | |
40 #pragma once | |
41 | |
42 #include "Statement.h" | |
43 #include "IScalarFunction.h" | |
2302
f31dfb131dee
fix backward compatibility with SQLite < 3.19.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
44 #include "SQLiteTypes.h" |
0 | 45 |
46 #include <string> | |
47 #include <map> | |
48 | |
5357
fddb5d8d0021
Prevent the leak of the full path of the source files in the binaries
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
5185
diff
changeset
|
49 #if !defined(__ORTHANC_FILE__) |
fddb5d8d0021
Prevent the leak of the full path of the source files in the binaries
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
5185
diff
changeset
|
50 # if defined(_MSC_VER) |
fddb5d8d0021
Prevent the leak of the full path of the source files in the binaries
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
5185
diff
changeset
|
51 # pragma message("Warning: Macro __ORTHANC_FILE__ is not defined, this will leak the full path of the source files in the binaries") |
fddb5d8d0021
Prevent the leak of the full path of the source files in the binaries
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
5185
diff
changeset
|
52 # else |
fddb5d8d0021
Prevent the leak of the full path of the source files in the binaries
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
5185
diff
changeset
|
53 # warning Warning: Macro __ORTHANC_FILE__ is not defined, this will leak the full path of the source files in the binaries |
fddb5d8d0021
Prevent the leak of the full path of the source files in the binaries
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
5185
diff
changeset
|
54 # endif |
fddb5d8d0021
Prevent the leak of the full path of the source files in the binaries
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
5185
diff
changeset
|
55 # define __ORTHANC_FILE__ __FILE__ |
fddb5d8d0021
Prevent the leak of the full path of the source files in the binaries
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
5185
diff
changeset
|
56 #endif |
fddb5d8d0021
Prevent the leak of the full path of the source files in the binaries
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
5185
diff
changeset
|
57 |
fddb5d8d0021
Prevent the leak of the full path of the source files in the binaries
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
5185
diff
changeset
|
58 #define SQLITE_FROM_HERE ::Orthanc::SQLite::StatementId(__ORTHANC_FILE__, __LINE__) |
5566
8b507b1514eb
cache dynamic SQLite statements (imported from large-queries branch)
Alain Mazy <am@orthanc.team>
parents:
5485
diff
changeset
|
59 #define SQLITE_FROM_HERE_DYNAMIC(sql) ::Orthanc::SQLite::StatementId(__ORTHANC_FILE__, __LINE__, sql) |
0 | 60 |
59 | 61 namespace Orthanc |
0 | 62 { |
63 namespace SQLite | |
64 { | |
3992
f9863630ec7f
working on the shared library for Orthanc framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3063
diff
changeset
|
65 class ORTHANC_PUBLIC Connection : NonCopyable |
0 | 66 { |
67 friend class Statement; | |
68 friend class Transaction; | |
69 | |
70 private: | |
71 // All cached statements. Keeping a reference to these statements means that | |
72 // they'll remain active. | |
73 typedef std::map<StatementId, StatementReference*> CachedStatements; | |
74 CachedStatements cachedStatements_; | |
75 | |
76 // The actual sqlite database. Will be NULL before Init has been called or if | |
77 // Init resulted in an error. | |
78 sqlite3* db_; | |
79 | |
80 // Number of currently-nested transactions. | |
81 int transactionNesting_; | |
82 | |
83 // True if any of the currently nested transactions have been rolled back. | |
84 // When we get to the outermost transaction, this will determine if we do | |
85 // a rollback instead of a commit. | |
86 bool needsRollback_; | |
87 | |
88 void ClearCache(); | |
89 | |
90 void CheckIsOpen() const; | |
91 | |
92 sqlite3* GetWrappedObject() | |
93 { | |
94 return db_; | |
95 } | |
96 | |
97 StatementReference& GetCachedStatement(const StatementId& id, | |
98 const char* sql); | |
99 | |
100 bool DoesTableOrIndexExist(const char* name, | |
101 const char* type) const; | |
102 | |
103 void DoRollback(); | |
104 | |
105 public: | |
106 // The database is opened by calling Open[InMemory](). Any uncommitted | |
107 // transactions will be rolled back when this object is deleted. | |
108 Connection(); | |
109 ~Connection(); | |
110 | |
111 void Open(const std::string& path); | |
112 | |
113 void OpenInMemory(); | |
114 | |
115 void Close(); | |
116 | |
117 bool Execute(const char* sql); | |
118 | |
4304 | 119 bool Execute(const std::string& sql); |
0 | 120 |
206
4453a010d0db
flush to disk thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
121 void FlushToDisk(); |
4453a010d0db
flush to disk thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
122 |
0 | 123 IScalarFunction* Register(IScalarFunction* func); // Takes the ownership of the function |
124 | |
125 // Info querying ------------------------------------------------------------- | |
126 | |
127 // Used to check a |sql| statement for syntactic validity. If the | |
128 // statement is valid SQL, returns true. | |
129 bool IsSQLValid(const char* sql); | |
130 | |
131 // Returns true if the given table exists. | |
132 bool DoesTableExist(const char* table_name) const; | |
133 | |
134 // Returns true if the given index exists. | |
135 bool DoesIndexExist(const char* index_name) const; | |
136 | |
137 // Returns true if a column with the given name exists in the given table. | |
138 bool DoesColumnExist(const char* table_name, const char* column_name) const; | |
139 | |
140 // Returns sqlite's internal ID for the last inserted row. Valid only | |
141 // immediately after an insert. | |
142 int64_t GetLastInsertRowId() const; | |
143 | |
144 // Returns sqlite's count of the number of rows modified by the last | |
145 // statement executed. Will be 0 if no statement has executed or the database | |
146 // is closed. | |
147 int GetLastChangeCount() const; | |
148 | |
149 // Errors -------------------------------------------------------------------- | |
150 | |
151 // Returns the error code associated with the last sqlite operation. | |
152 int GetErrorCode() const; | |
153 | |
154 // Returns the errno associated with GetErrorCode(). See | |
155 // SQLITE_LAST_ERRNO in SQLite documentation. | |
156 int GetLastErrno() const; | |
157 | |
158 // Returns a pointer to a statically allocated string associated with the | |
159 // last sqlite operation. | |
160 const char* GetErrorMessage() const; | |
161 | |
162 | |
163 // Diagnostics (for unit tests) ---------------------------------------------- | |
164 | |
165 int ExecuteAndReturnErrorCode(const char* sql); | |
166 | |
4304 | 167 bool HasCachedStatement(const StatementId& id) const; |
0 | 168 |
4304 | 169 int GetTransactionNesting() const; |
0 | 170 |
171 // Transactions -------------------------------------------------------------- | |
172 | |
173 bool BeginTransaction(); | |
174 void RollbackTransaction(); | |
206
4453a010d0db
flush to disk thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
175 bool CommitTransaction(); |
0 | 176 }; |
177 } | |
178 } |