0
|
1 /**
|
50
|
2 * Palanthir - A Lightweight, RESTful DICOM Store
|
0
|
3 * Copyright (C) 2012 Medical Physics Department, CHU of Liege,
|
|
4 * Belgium
|
|
5 *
|
17
|
6 * Copyright (c) 2012 The Chromium Authors. All rights reserved.
|
|
7 *
|
|
8 * Redistribution and use in source and binary forms, with or without
|
|
9 * modification, are permitted provided that the following conditions are
|
|
10 * met:
|
0
|
11 *
|
17
|
12 * * Redistributions of source code must retain the above copyright
|
|
13 * notice, this list of conditions and the following disclaimer.
|
|
14 * * Redistributions in binary form must reproduce the above
|
|
15 * copyright notice, this list of conditions and the following disclaimer
|
|
16 * in the documentation and/or other materials provided with the
|
|
17 * distribution.
|
|
18 * * Neither the name of Google Inc., the name of the CHU of Liege,
|
|
19 * nor the names of its contributors may be used to endorse or promote
|
|
20 * products derived from this software without specific prior written
|
|
21 * permission.
|
|
22 *
|
|
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
27 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
29 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
0
|
34 **/
|
|
35
|
|
36
|
|
37 #pragma once
|
|
38
|
|
39 #include "Statement.h"
|
|
40 #include "IScalarFunction.h"
|
|
41
|
|
42 #include <string>
|
|
43 #include <boost/noncopyable.hpp>
|
|
44 #include <map>
|
|
45
|
|
46 struct sqlite3;
|
|
47 struct sqlite3_stmt;
|
|
48
|
|
49 #define SQLITE_FROM_HERE SQLite::StatementId(__FILE__, __LINE__)
|
|
50
|
50
|
51 namespace Palanthir
|
0
|
52 {
|
|
53 namespace SQLite
|
|
54 {
|
|
55 class Connection : boost::noncopyable
|
|
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
|
|
109 bool Execute(const std::string& sql)
|
|
110 {
|
|
111 return Execute(sql.c_str());
|
|
112 }
|
|
113
|
|
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();
|
|
172 bool CommitTransaction();
|
|
173 };
|
|
174 }
|
|
175 }
|