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