2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 * @file sql_connection.h
18 * @author Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com)
20 * @brief This file is the implementation file of SQL connection
22 #ifndef DPL_SQL_CONNECTION_H
23 #define DPL_SQL_CONNECTION_H
25 #include <dpl/noncopyable.h>
26 #include <dpl/exception.h>
27 #include <dpl/optional.h>
29 #include <dpl/string.h>
30 #include <dpl/log/log.h>
33 #include <dpl/assert.h>
40 * SQL connection class
46 * SQL Exception classes
51 DECLARE_EXCEPTION_TYPE(DPL::Exception, Base)
52 DECLARE_EXCEPTION_TYPE(Base, SyntaxError)
53 DECLARE_EXCEPTION_TYPE(Base, ConnectionBroken)
54 DECLARE_EXCEPTION_TYPE(Base, InternalError)
55 DECLARE_EXCEPTION_TYPE(Base, InvalidColumn)
58 typedef int ColumnIndex;
59 typedef int ArgumentIndex;
62 * SQL processed data command
68 SqlConnection *m_masterConnection;
71 void CheckBindResult(int result);
72 void CheckColumnIndex(SqlConnection::ColumnIndex column);
74 DataCommand(SqlConnection *connection, const char *buffer);
76 friend class SqlConnection;
79 virtual ~DataCommand();
82 * Bind null to the prepared statement argument
84 * @param position Index of argument to bind value to
86 void BindNull(ArgumentIndex position);
89 * Bind int to the prepared statement argument
91 * @param position Index of argument to bind value to
92 * @param value Value to bind
94 void BindInteger(ArgumentIndex position, int value);
97 * Bind int8_t to the prepared statement argument
99 * @param position Index of argument to bind value to
100 * @param value Value to bind
102 void BindInt8(ArgumentIndex position, int8_t value);
105 * Bind int16 to the prepared statement argument
107 * @param position Index of argument to bind value to
108 * @param value Value to bind
110 void BindInt16(ArgumentIndex position, int16_t value);
113 * Bind int32 to the prepared statement argument
115 * @param position Index of argument to bind value to
116 * @param value Value to bind
118 void BindInt32(ArgumentIndex position, int32_t value);
121 * Bind int64 to the prepared statement argument
123 * @param position Index of argument to bind value to
124 * @param value Value to bind
126 void BindInt64(ArgumentIndex position, int64_t value);
129 * Bind float to the prepared statement argument
131 * @param position Index of argument to bind value to
132 * @param value Value to bind
134 void BindFloat(ArgumentIndex position, float value);
137 * Bind double to the prepared statement argument
139 * @param position Index of argument to bind value to
140 * @param value Value to bind
142 void BindDouble(ArgumentIndex position, double value);
145 * Bind string to the prepared statement argument
147 * @param position Index of argument to bind value to
148 * @param value Value to bind
150 void BindString(ArgumentIndex position, const char *value);
153 * Bind string to the prepared statement argument
155 * @param position Index of argument to bind value to
156 * @param value Value to bind
158 void BindString(ArgumentIndex position, const String& value);
161 * Bind optional int to the prepared statement argument.
162 * If optional is not set null will be bound
164 * @param position Index of argument to bind value to
165 * @param value Value to bind
167 void BindInteger(ArgumentIndex position, const Optional<int> &value);
170 * Bind optional int8 to the prepared statement argument.
171 * If optional is not set null will be bound
173 * @param position Index of argument to bind value to
174 * @param value Value to bind
176 void BindInt8(ArgumentIndex position, const Optional<int8_t> &value);
179 * Bind optional int16 to the prepared statement argument.
180 * If optional is not set null will be bound
182 * @param position Index of argument to bind value to
183 * @param value Value to bind
185 void BindInt16(ArgumentIndex position, const Optional<int16_t> &value);
188 * Bind optional int32 to the prepared statement argument.
189 * If optional is not set null will be bound
191 * @param position Index of argument to bind value to
192 * @param value Value to bind
194 void BindInt32(ArgumentIndex position, const Optional<int32_t> &value);
197 * Bind optional int64 to the prepared statement argument.
198 * If optional is not set null will be bound
200 * @param position Index of argument to bind value to
201 * @param value Value to bind
203 void BindInt64(ArgumentIndex position, const Optional<int64_t> &value);
206 * Bind optional float to the prepared statement argument.
207 * If optional is not set null will be bound
209 * @param position Index of argument to bind value to
210 * @param value Value to bind
212 void BindFloat(ArgumentIndex position, const Optional<float> &value);
215 * Bind optional double to the prepared statement argument.
216 * If optional is not set null will be bound
218 * @param position Index of argument to bind value to
219 * @param value Value to bind
221 void BindDouble(ArgumentIndex position, const Optional<double> &value);
224 * Bind optional string to the prepared statement argument.
225 * If optional is not set null will be bound
227 * @param position Index of argument to bind value to
228 * @param value Value to bind
230 void BindString(ArgumentIndex position, const Optional<String> &value);
233 * Execute the prepared statement and/or move
234 * to the next row of the result
236 * @return True when there was a row returned
241 * Reset prepared statement's arguments
242 * All parameters will become null
247 * Checks whether column value is null
249 * @throw Exception::InvalidColumn
251 bool IsColumnNull(ColumnIndex column);
254 * Get integer value from column in current row.
256 * @throw Exception::InvalidColumn
258 int GetColumnInteger(ColumnIndex column);
261 * Get int8 value from column in current row.
263 * @throw Exception::InvalidColumn
265 int8_t GetColumnInt8(ColumnIndex column);
268 * Get int16 value from column in current row.
270 * @throw Exception::InvalidColumn
272 int16_t GetColumnInt16(ColumnIndex column);
274 * Get int32 value from column in current row.
276 * @throw Exception::InvalidColumn
278 int32_t GetColumnInt32(ColumnIndex column);
281 * Get int64 value from column in current row.
283 * @throw Exception::InvalidColumn
285 int64_t GetColumnInt64(ColumnIndex column);
288 * Get float value from column in current row.
290 * @throw Exception::InvalidColumn
292 float GetColumnFloat(ColumnIndex column);
295 * Get double value from column in current row.
297 * @throw Exception::InvalidColumn
299 double GetColumnDouble(ColumnIndex column);
302 * Get string value from column in current row.
304 * @throw Exception::InvalidColumn
306 std::string GetColumnString(ColumnIndex column);
309 * Get optional integer value from column in current row.
311 * @throw Exception::InvalidColumn
313 Optional<int> GetColumnOptionalInteger(ColumnIndex column);
316 * Get optional int8 value from column in current row.
318 * @throw Exception::InvalidColumn
320 Optional<int8_t> GetColumnOptionalInt8(ColumnIndex column);
323 * Get optional int16value from column in current row.
325 * @throw Exception::InvalidColumn
327 Optional<int16_t> GetColumnOptionalInt16(ColumnIndex column);
330 * Get optional int32 value from column in current row.
332 * @throw Exception::InvalidColumn
334 Optional<int32_t> GetColumnOptionalInt32(ColumnIndex column);
337 * Get optional int64 value from column in current row.
339 * @throw Exception::InvalidColumn
341 Optional<int64_t> GetColumnOptionalInt64(ColumnIndex column);
344 * Get optional float value from column in current row.
346 * @throw Exception::InvalidColumn
348 Optional<float> GetColumnOptionalFloat(ColumnIndex column);
351 * Get optional double value from column in current row.
353 * @throw Exception::InvalidColumn
355 Optional<double> GetColumnOptionalDouble(ColumnIndex column);
358 * Get optional string value from column in current row.
360 * @throw Exception::InvalidColumn
362 Optional<String> GetColumnOptionalString(ColumnIndex column);
365 // Move on copy semantics
366 typedef std::auto_ptr<DataCommand> DataCommandAutoPtr;
380 RO = SQLITE_OPEN_NOMUTEX | SQLITE_OPEN_READONLY,
382 * *TODO: please remove CREATE option from RW flag when all places
383 * that need that switched do CRW
385 RW = SQLITE_OPEN_NOMUTEX | SQLITE_OPEN_READWRITE |
387 CRW = RW | SQLITE_OPEN_CREATE
392 typedef sqlite3_int64 RowID;
395 * Synchronization object used to synchronize SQL connection
396 * to the same database across different threads and processes
398 class SynchronizationObject
401 virtual ~SynchronizationObject() {}
404 * Synchronizes SQL connection for multiple clients.
406 virtual void Synchronize() = 0;
409 * Notify all waiting clients that the connection is no longer locked.
411 virtual void NotifyAll() = 0;
415 sqlite3 *m_connection;
420 // Stored data procedures
421 int m_dataCommandsCount;
423 // Synchronization object
424 std::unique_ptr<SynchronizationObject> m_synchronizationObject;
426 virtual void Connect(const std::string &address,
427 Flag::Type = Flag::None, Flag::Option = Flag::RO);
428 virtual void Disconnect();
430 void TurnOnForeignKeys();
432 static SynchronizationObject *AllocDefaultSynchronizationObject();
436 * Open SQL connection
438 * Synchronization is archieved by using provided asynchronization object.
439 * If synchronizationObject is set to NULL, so synchronization is performed.
440 * Ownership of the synchronization object is transfered to sql connection
443 * @param address Database file name
444 * @param flags Open flags
445 * @param synchronizationObject A synchronization object to use.
447 explicit SqlConnection(const std::string &address = std::string(),
448 Flag::Type flags = Flag::None,
449 Flag::Option options = Flag::RO,
450 SynchronizationObject *synchronizationObject =
451 AllocDefaultSynchronizationObject());
456 virtual ~SqlConnection();
459 * Execute SQL command without result
464 void ExecCommand(const char *format, ...);
467 * Prepare stored procedure
469 * @param format SQL statement
470 * @return Data command representing stored procedure
472 DataCommandAutoPtr PrepareDataCommand(const char *format, ...);
475 * Check whether given table exists
477 * @param tableName Name of the table to check
478 * @return True if given table name exists
480 bool CheckTableExist(const char *tableName);
483 * Get last insert operation new row id
487 RowID GetLastInsertRowID() const;
492 #endif // DPL_SQL_CONNECTION_H