Mercurial > hg > orthanc
comparison Core/SQLite/Connection.h @ 0:3959d33612cc
initial commit
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 19 Jul 2012 14:32:22 +0200 |
parents | |
children | db4d996ea264 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:3959d33612cc |
---|---|
1 /** | |
2 * Palantir - A Lightweight, RESTful DICOM Store | |
3 * Copyright (C) 2012 Medical Physics Department, CHU of Liege, | |
4 * Belgium | |
5 * | |
6 * This program is free software: you can redistribute it and/or | |
7 * modify it under the terms of the GNU General Public License as | |
8 * published by the Free Software Foundation, either version 3 of the | |
9 * License, or (at your option) any later version. | |
10 * | |
11 * This program is distributed in the hope that it will be useful, but | |
12 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 * General Public License for more details. | |
15 * | |
16 * You should have received a copy of the GNU General Public License | |
17 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
18 **/ | |
19 | |
20 | |
21 #pragma once | |
22 | |
23 #include "Statement.h" | |
24 #include "IScalarFunction.h" | |
25 | |
26 #include <string> | |
27 #include <boost/noncopyable.hpp> | |
28 #include <map> | |
29 | |
30 struct sqlite3; | |
31 struct sqlite3_stmt; | |
32 | |
33 #define SQLITE_FROM_HERE SQLite::StatementId(__FILE__, __LINE__) | |
34 | |
35 namespace Palantir | |
36 { | |
37 namespace SQLite | |
38 { | |
39 class Connection : boost::noncopyable | |
40 { | |
41 friend class Statement; | |
42 friend class Transaction; | |
43 | |
44 private: | |
45 // All cached statements. Keeping a reference to these statements means that | |
46 // they'll remain active. | |
47 typedef std::map<StatementId, StatementReference*> CachedStatements; | |
48 CachedStatements cachedStatements_; | |
49 | |
50 // The actual sqlite database. Will be NULL before Init has been called or if | |
51 // Init resulted in an error. | |
52 sqlite3* db_; | |
53 | |
54 // Number of currently-nested transactions. | |
55 int transactionNesting_; | |
56 | |
57 // True if any of the currently nested transactions have been rolled back. | |
58 // When we get to the outermost transaction, this will determine if we do | |
59 // a rollback instead of a commit. | |
60 bool needsRollback_; | |
61 | |
62 void ClearCache(); | |
63 | |
64 void CheckIsOpen() const; | |
65 | |
66 sqlite3* GetWrappedObject() | |
67 { | |
68 return db_; | |
69 } | |
70 | |
71 StatementReference& GetCachedStatement(const StatementId& id, | |
72 const char* sql); | |
73 | |
74 bool DoesTableOrIndexExist(const char* name, | |
75 const char* type) const; | |
76 | |
77 void DoRollback(); | |
78 | |
79 public: | |
80 // The database is opened by calling Open[InMemory](). Any uncommitted | |
81 // transactions will be rolled back when this object is deleted. | |
82 Connection(); | |
83 ~Connection(); | |
84 | |
85 void Open(const std::string& path); | |
86 | |
87 void OpenInMemory(); | |
88 | |
89 void Close(); | |
90 | |
91 bool Execute(const char* sql); | |
92 | |
93 bool Execute(const std::string& sql) | |
94 { | |
95 return Execute(sql.c_str()); | |
96 } | |
97 | |
98 IScalarFunction* Register(IScalarFunction* func); // Takes the ownership of the function | |
99 | |
100 // Info querying ------------------------------------------------------------- | |
101 | |
102 // Used to check a |sql| statement for syntactic validity. If the | |
103 // statement is valid SQL, returns true. | |
104 bool IsSQLValid(const char* sql); | |
105 | |
106 // Returns true if the given table exists. | |
107 bool DoesTableExist(const char* table_name) const; | |
108 | |
109 // Returns true if the given index exists. | |
110 bool DoesIndexExist(const char* index_name) const; | |
111 | |
112 // Returns true if a column with the given name exists in the given table. | |
113 bool DoesColumnExist(const char* table_name, const char* column_name) const; | |
114 | |
115 // Returns sqlite's internal ID for the last inserted row. Valid only | |
116 // immediately after an insert. | |
117 int64_t GetLastInsertRowId() const; | |
118 | |
119 // Returns sqlite's count of the number of rows modified by the last | |
120 // statement executed. Will be 0 if no statement has executed or the database | |
121 // is closed. | |
122 int GetLastChangeCount() const; | |
123 | |
124 // Errors -------------------------------------------------------------------- | |
125 | |
126 // Returns the error code associated with the last sqlite operation. | |
127 int GetErrorCode() const; | |
128 | |
129 // Returns the errno associated with GetErrorCode(). See | |
130 // SQLITE_LAST_ERRNO in SQLite documentation. | |
131 int GetLastErrno() const; | |
132 | |
133 // Returns a pointer to a statically allocated string associated with the | |
134 // last sqlite operation. | |
135 const char* GetErrorMessage() const; | |
136 | |
137 | |
138 // Diagnostics (for unit tests) ---------------------------------------------- | |
139 | |
140 int ExecuteAndReturnErrorCode(const char* sql); | |
141 | |
142 bool HasCachedStatement(const StatementId& id) const | |
143 { | |
144 return cachedStatements_.find(id) != cachedStatements_.end(); | |
145 } | |
146 | |
147 int GetTransactionNesting() const | |
148 { | |
149 return transactionNesting_; | |
150 } | |
151 | |
152 // Transactions -------------------------------------------------------------- | |
153 | |
154 bool BeginTransaction(); | |
155 void RollbackTransaction(); | |
156 bool CommitTransaction(); | |
157 }; | |
158 } | |
159 } |