Mercurial > hg > orthanc
annotate OrthancFramework/Sources/SQLite/Connection.h @ 5666:aa231c18b9d2 find-refactoring
adding computed tags
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 04 Jul 2024 18:31:54 +0200 |
parents | 3f13db27b399 |
children | ca06dde85358 |
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__) |
5566
8b507b1514eb
cache dynamic SQLite statements (imported from large-queries branch)
Alain Mazy <am@orthanc.team>
parents:
5485
diff
changeset
|
60 #define SQLITE_FROM_HERE_DYNAMIC(sql) ::Orthanc::SQLite::StatementId(__ORTHANC_FILE__, __LINE__, sql) |
0 | 61 |
59 | 62 namespace Orthanc |
0 | 63 { |
64 namespace SQLite | |
65 { | |
3992
f9863630ec7f
working on the shared library for Orthanc framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3063
diff
changeset
|
66 class ORTHANC_PUBLIC Connection : NonCopyable |
0 | 67 { |
68 friend class Statement; | |
69 friend class Transaction; | |
70 | |
71 private: | |
72 // All cached statements. Keeping a reference to these statements means that | |
73 // they'll remain active. | |
74 typedef std::map<StatementId, StatementReference*> CachedStatements; | |
75 CachedStatements cachedStatements_; | |
76 | |
77 // The actual sqlite database. Will be NULL before Init has been called or if | |
78 // Init resulted in an error. | |
79 sqlite3* db_; | |
80 | |
81 // Number of currently-nested transactions. | |
82 int transactionNesting_; | |
83 | |
84 // True if any of the currently nested transactions have been rolled back. | |
85 // When we get to the outermost transaction, this will determine if we do | |
86 // a rollback instead of a commit. | |
87 bool needsRollback_; | |
88 | |
89 void ClearCache(); | |
90 | |
91 void CheckIsOpen() const; | |
92 | |
93 sqlite3* GetWrappedObject() | |
94 { | |
95 return db_; | |
96 } | |
97 | |
98 StatementReference& GetCachedStatement(const StatementId& id, | |
99 const char* sql); | |
100 | |
101 bool DoesTableOrIndexExist(const char* name, | |
102 const char* type) const; | |
103 | |
104 void DoRollback(); | |
105 | |
106 public: | |
107 // The database is opened by calling Open[InMemory](). Any uncommitted | |
108 // transactions will be rolled back when this object is deleted. | |
109 Connection(); | |
110 ~Connection(); | |
111 | |
112 void Open(const std::string& path); | |
113 | |
114 void OpenInMemory(); | |
115 | |
116 void Close(); | |
117 | |
118 bool Execute(const char* sql); | |
119 | |
4304 | 120 bool Execute(const std::string& sql); |
0 | 121 |
206
4453a010d0db
flush to disk thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
122 void FlushToDisk(); |
4453a010d0db
flush to disk thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
123 |
0 | 124 IScalarFunction* Register(IScalarFunction* func); // Takes the ownership of the function |
125 | |
126 // Info querying ------------------------------------------------------------- | |
127 | |
128 // Used to check a |sql| statement for syntactic validity. If the | |
129 // statement is valid SQL, returns true. | |
130 bool IsSQLValid(const char* sql); | |
131 | |
132 // Returns true if the given table exists. | |
133 bool DoesTableExist(const char* table_name) const; | |
134 | |
135 // Returns true if the given index exists. | |
136 bool DoesIndexExist(const char* index_name) const; | |
137 | |
138 // Returns true if a column with the given name exists in the given table. | |
139 bool DoesColumnExist(const char* table_name, const char* column_name) const; | |
140 | |
141 // Returns sqlite's internal ID for the last inserted row. Valid only | |
142 // immediately after an insert. | |
143 int64_t GetLastInsertRowId() const; | |
144 | |
145 // Returns sqlite's count of the number of rows modified by the last | |
146 // statement executed. Will be 0 if no statement has executed or the database | |
147 // is closed. | |
148 int GetLastChangeCount() const; | |
149 | |
150 // Errors -------------------------------------------------------------------- | |
151 | |
152 // Returns the error code associated with the last sqlite operation. | |
153 int GetErrorCode() const; | |
154 | |
155 // Returns the errno associated with GetErrorCode(). See | |
156 // SQLITE_LAST_ERRNO in SQLite documentation. | |
157 int GetLastErrno() const; | |
158 | |
159 // Returns a pointer to a statically allocated string associated with the | |
160 // last sqlite operation. | |
161 const char* GetErrorMessage() const; | |
162 | |
163 | |
164 // Diagnostics (for unit tests) ---------------------------------------------- | |
165 | |
166 int ExecuteAndReturnErrorCode(const char* sql); | |
167 | |
4304 | 168 bool HasCachedStatement(const StatementId& id) const; |
0 | 169 |
4304 | 170 int GetTransactionNesting() const; |
0 | 171 |
172 // Transactions -------------------------------------------------------------- | |
173 | |
174 bool BeginTransaction(); | |
175 void RollbackTransaction(); | |
206
4453a010d0db
flush to disk thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
176 bool CommitTransaction(); |
0 | 177 }; |
178 } | |
179 } |