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