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>
28 #include <dpl/scoped_ptr.h>
29 #include <dpl/string.h>
30 #include <dpl/log/log.h>
33 #include <dpl/assert.h>
43 * SQL connection class
49 * SQL Exception classes
54 DECLARE_EXCEPTION_TYPE(DPL::Exception, Base)
55 DECLARE_EXCEPTION_TYPE(Base, SyntaxError)
56 DECLARE_EXCEPTION_TYPE(Base, ConnectionBroken)
57 DECLARE_EXCEPTION_TYPE(Base, InternalError)
58 DECLARE_EXCEPTION_TYPE(Base, InvalidColumn)
61 typedef int ColumnIndex;
62 typedef int ArgumentIndex;
65 * SQL processed data command
71 SqlConnection *m_masterConnection;
74 void CheckBindResult(int result);
75 void CheckColumnIndex(SqlConnection::ColumnIndex column);
77 DataCommand(SqlConnection *connection, const char *buffer);
79 friend class SqlConnection;
82 virtual ~DataCommand();
85 * Bind null to the prepared statement argument
87 * @param position Index of argument to bind value to
89 void BindNull(ArgumentIndex position);
92 * Bind int to the prepared statement argument
94 * @param position Index of argument to bind value to
95 * @param value Value to bind
97 void BindInteger(ArgumentIndex position, int value);
100 * Bind int8_t to the prepared statement argument
102 * @param position Index of argument to bind value to
103 * @param value Value to bind
105 void BindInt8(ArgumentIndex position, int8_t value);
108 * Bind int16 to the prepared statement argument
110 * @param position Index of argument to bind value to
111 * @param value Value to bind
113 void BindInt16(ArgumentIndex position, int16_t value);
116 * Bind int32 to the prepared statement argument
118 * @param position Index of argument to bind value to
119 * @param value Value to bind
121 void BindInt32(ArgumentIndex position, int32_t value);
124 * Bind int64 to the prepared statement argument
126 * @param position Index of argument to bind value to
127 * @param value Value to bind
129 void BindInt64(ArgumentIndex position, int64_t value);
132 * Bind float to the prepared statement argument
134 * @param position Index of argument to bind value to
135 * @param value Value to bind
137 void BindFloat(ArgumentIndex position, float value);
140 * Bind double to the prepared statement argument
142 * @param position Index of argument to bind value to
143 * @param value Value to bind
145 void BindDouble(ArgumentIndex position, double value);
148 * Bind string to the prepared statement argument
150 * @param position Index of argument to bind value to
151 * @param value Value to bind
153 void BindString(ArgumentIndex position, const char *value);
156 * Bind string to the prepared statement argument
158 * @param position Index of argument to bind value to
159 * @param value Value to bind
161 void BindString(ArgumentIndex position, const String& value);
164 * Bind optional int to the prepared statement argument.
165 * If optional is not set null will be bound
167 * @param position Index of argument to bind value to
168 * @param value Value to bind
170 void BindInteger(ArgumentIndex position, const Optional<int> &value);
173 * Bind optional int8 to the prepared statement argument.
174 * If optional is not set null will be bound
176 * @param position Index of argument to bind value to
177 * @param value Value to bind
179 void BindInt8(ArgumentIndex position, const Optional<int8_t> &value);
182 * Bind optional int16 to the prepared statement argument.
183 * If optional is not set null will be bound
185 * @param position Index of argument to bind value to
186 * @param value Value to bind
188 void BindInt16(ArgumentIndex position, const Optional<int16_t> &value);
191 * Bind optional int32 to the prepared statement argument.
192 * If optional is not set null will be bound
194 * @param position Index of argument to bind value to
195 * @param value Value to bind
197 void BindInt32(ArgumentIndex position, const Optional<int32_t> &value);
200 * Bind optional int64 to the prepared statement argument.
201 * If optional is not set null will be bound
203 * @param position Index of argument to bind value to
204 * @param value Value to bind
206 void BindInt64(ArgumentIndex position, const Optional<int64_t> &value);
209 * Bind optional float to the prepared statement argument.
210 * If optional is not set null will be bound
212 * @param position Index of argument to bind value to
213 * @param value Value to bind
215 void BindFloat(ArgumentIndex position, const Optional<float> &value);
218 * Bind optional double to the prepared statement argument.
219 * If optional is not set null will be bound
221 * @param position Index of argument to bind value to
222 * @param value Value to bind
224 void BindDouble(ArgumentIndex position, const Optional<double> &value);
227 * Bind optional string to the prepared statement argument.
228 * If optional is not set null will be bound
230 * @param position Index of argument to bind value to
231 * @param value Value to bind
233 void BindString(ArgumentIndex position, const Optional<String> &value);
236 * Execute the prepared statement and/or move
237 * to the next row of the result
239 * @return True when there was a row returned
244 * Reset prepared statement's arguments
245 * All parameters will become null
250 * Checks whether column value is null
252 * @throw Exception::InvalidColumn
254 bool IsColumnNull(ColumnIndex column);
257 * Get integer value from column in current row.
259 * @throw Exception::InvalidColumn
261 int GetColumnInteger(ColumnIndex column);
264 * Get int8 value from column in current row.
266 * @throw Exception::InvalidColumn
268 int8_t GetColumnInt8(ColumnIndex column);
271 * Get int16 value from column in current row.
273 * @throw Exception::InvalidColumn
275 int16_t GetColumnInt16(ColumnIndex column);
277 * Get int32 value from column in current row.
279 * @throw Exception::InvalidColumn
281 int32_t GetColumnInt32(ColumnIndex column);
284 * Get int64 value from column in current row.
286 * @throw Exception::InvalidColumn
288 int64_t GetColumnInt64(ColumnIndex column);
291 * Get float value from column in current row.
293 * @throw Exception::InvalidColumn
295 float GetColumnFloat(ColumnIndex column);
298 * Get double value from column in current row.
300 * @throw Exception::InvalidColumn
302 double GetColumnDouble(ColumnIndex column);
305 * Get string value from column in current row.
307 * @throw Exception::InvalidColumn
309 std::string GetColumnString(ColumnIndex column);
312 * Get optional integer value from column in current row.
314 * @throw Exception::InvalidColumn
316 Optional<int> GetColumnOptionalInteger(ColumnIndex column);
319 * Get optional int8 value from column in current row.
321 * @throw Exception::InvalidColumn
323 Optional<int8_t> GetColumnOptionalInt8(ColumnIndex column);
326 * Get optional int16value from column in current row.
328 * @throw Exception::InvalidColumn
330 Optional<int16_t> GetColumnOptionalInt16(ColumnIndex column);
333 * Get optional int32 value from column in current row.
335 * @throw Exception::InvalidColumn
337 Optional<int32_t> GetColumnOptionalInt32(ColumnIndex column);
340 * Get optional int64 value from column in current row.
342 * @throw Exception::InvalidColumn
344 Optional<int64_t> GetColumnOptionalInt64(ColumnIndex column);
347 * Get optional float value from column in current row.
349 * @throw Exception::InvalidColumn
351 Optional<float> GetColumnOptionalFloat(ColumnIndex column);
354 * Get optional double value from column in current row.
356 * @throw Exception::InvalidColumn
358 Optional<double> GetColumnOptionalDouble(ColumnIndex column);
361 * Get optional string value from column in current row.
363 * @throw Exception::InvalidColumn
365 Optional<String> GetColumnOptionalString(ColumnIndex column);
368 // Move on copy semantics
369 typedef std::auto_ptr<DataCommand> DataCommandAutoPtr;
383 typedef sqlite3_int64 RowID;
386 * Synchronization object used to synchronize SQL connection
387 * to the same database across different threads and processes
389 class SynchronizationObject
392 virtual ~SynchronizationObject() {}
395 * Synchronizes SQL connection for multiple clients.
397 virtual void Synchronize() = 0;
400 * Notify all waiting clients that the connection is no longer locked.
402 virtual void NotifyAll() = 0;
406 sqlite3 *m_connection;
411 // Stored data procedures
412 int m_dataCommandsCount;
414 // Synchronization object
415 ScopedPtr<SynchronizationObject> m_synchronizationObject;
417 virtual void Connect(const std::string &address, Flag::Type = Flag::None);
418 virtual void Disconnect();
420 void TurnOnForeignKeys();
422 static SynchronizationObject *AllocDefaultSynchronizationObject();
426 * Open SQL connection
428 * Synchronization is archieved by using provided asynchronization object.
429 * If synchronizationObject is set to NULL, so synchronization is performed.
430 * Ownership of the synchronization object is transfered to sql connection
433 * @param address Database file name
434 * @param flags Open flags
435 * @param synchronizationObject A synchronization object to use.
437 explicit SqlConnection(const std::string &address = std::string(),
438 Flag::Type flags = Flag::None,
439 SynchronizationObject *synchronizationObject =
440 AllocDefaultSynchronizationObject());
445 virtual ~SqlConnection();
448 * Execute SQL command without result
453 void ExecCommand(const char *format, ...);
456 * Prepare stored procedure
458 * @param format SQL statement
459 * @return Data command representing stored procedure
461 DataCommandAutoPtr PrepareDataCommand(const char *format, ...);
464 * Check whether given table exists
466 * @param tableName Name of the table to check
467 * @return True if given table name exists
469 bool CheckTableExist(const char *tableName);
472 * Get last insert operation new row id
476 RowID GetLastInsertRowID() const;
482 #endif // DPL_SQL_CONNECTION_H