Mercurial > hg > orthanc
annotate OrthancFramework/Sources/SQLite/Connection.h @ 5262:bf304ee8dd80
revert precompiled headers, as they really help
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 12 Apr 2023 15:53:32 +0200 |
parents | 0ea402b4d901 |
children | fddb5d8d0021 |
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 |
5185
0ea402b4d901
upgrade to year 2023
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4870
diff
changeset
|
6 * Copyright (C) 2017-2023 Osimis S.A., Belgium |
0ea402b4d901
upgrade to year 2023
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4870
diff
changeset
|
7 * Copyright (C) 2021-2023 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 | |
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 { | |
3992
f9863630ec7f
working on the shared library for Orthanc framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3063
diff
changeset
|
55 class ORTHANC_PUBLIC 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 | |
4304 | 109 bool Execute(const std::string& sql); |
0 | 110 |
206
4453a010d0db
flush to disk thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
111 void FlushToDisk(); |
4453a010d0db
flush to disk thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
112 |
0 | 113 IScalarFunction* Register(IScalarFunction* func); // Takes the ownership of the function |
114 | |
115 // Info querying ------------------------------------------------------------- | |
116 | |
117 // Used to check a |sql| statement for syntactic validity. If the | |
118 // statement is valid SQL, returns true. | |
119 bool IsSQLValid(const char* sql); | |
120 | |
121 // Returns true if the given table exists. | |
122 bool DoesTableExist(const char* table_name) const; | |
123 | |
124 // Returns true if the given index exists. | |
125 bool DoesIndexExist(const char* index_name) const; | |
126 | |
127 // Returns true if a column with the given name exists in the given table. | |
128 bool DoesColumnExist(const char* table_name, const char* column_name) const; | |
129 | |
130 // Returns sqlite's internal ID for the last inserted row. Valid only | |
131 // immediately after an insert. | |
132 int64_t GetLastInsertRowId() const; | |
133 | |
134 // Returns sqlite's count of the number of rows modified by the last | |
135 // statement executed. Will be 0 if no statement has executed or the database | |
136 // is closed. | |
137 int GetLastChangeCount() const; | |
138 | |
139 // Errors -------------------------------------------------------------------- | |
140 | |
141 // Returns the error code associated with the last sqlite operation. | |
142 int GetErrorCode() const; | |
143 | |
144 // Returns the errno associated with GetErrorCode(). See | |
145 // SQLITE_LAST_ERRNO in SQLite documentation. | |
146 int GetLastErrno() const; | |
147 | |
148 // Returns a pointer to a statically allocated string associated with the | |
149 // last sqlite operation. | |
150 const char* GetErrorMessage() const; | |
151 | |
152 | |
153 // Diagnostics (for unit tests) ---------------------------------------------- | |
154 | |
155 int ExecuteAndReturnErrorCode(const char* sql); | |
156 | |
4304 | 157 bool HasCachedStatement(const StatementId& id) const; |
0 | 158 |
4304 | 159 int GetTransactionNesting() const; |
0 | 160 |
161 // Transactions -------------------------------------------------------------- | |
162 | |
163 bool BeginTransaction(); | |
164 void RollbackTransaction(); | |
206
4453a010d0db
flush to disk thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
165 bool CommitTransaction(); |
0 | 166 }; |
167 } | |
168 } |