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 SECURITY_MANAGER_SQL_CONNECTION_H
23 #define SECURITY_MANAGER_SQL_CONNECTION_H
25 #include <dpl/noncopyable.h>
26 #include <dpl/exception.h>
27 #include <dpl/availability.h>
29 #include <boost/optional.hpp>
30 #include <dpl/string.h>
31 #include <dpl/log/log.h>
34 #include <dpl/assert.h>
38 namespace SecurityManager {
41 * SQL connection class
47 * SQL Exception classes
52 DECLARE_EXCEPTION_TYPE(SecurityManager::Exception, Base)
53 DECLARE_EXCEPTION_TYPE(Base, SyntaxError)
54 DECLARE_EXCEPTION_TYPE(Base, ConnectionBroken)
55 DECLARE_EXCEPTION_TYPE(Base, InternalError)
56 DECLARE_EXCEPTION_TYPE(Base, InvalidColumn)
59 typedef int ColumnIndex;
60 typedef int ArgumentIndex;
63 * SQL processed data command
69 SqlConnection *m_masterConnection;
72 void CheckBindResult(int result);
73 void CheckColumnIndex(SqlConnection::ColumnIndex column);
75 DataCommand(SqlConnection *connection, const char *buffer);
77 friend class SqlConnection;
80 virtual ~DataCommand();
83 * Bind null to the prepared statement argument
85 * @param position Index of argument to bind value to
87 void BindNull(ArgumentIndex position);
90 * Bind int to the prepared statement argument
92 * @param position Index of argument to bind value to
93 * @param value Value to bind
95 void BindInteger(ArgumentIndex position, int value);
98 * Bind int8_t to the prepared statement argument
100 * @param position Index of argument to bind value to
101 * @param value Value to bind
103 void BindInt8(ArgumentIndex position, int8_t value);
106 * Bind int16 to the prepared statement argument
108 * @param position Index of argument to bind value to
109 * @param value Value to bind
111 void BindInt16(ArgumentIndex position, int16_t value);
114 * Bind int32 to the prepared statement argument
116 * @param position Index of argument to bind value to
117 * @param value Value to bind
119 void BindInt32(ArgumentIndex position, int32_t value);
122 * Bind int64 to the prepared statement argument
124 * @param position Index of argument to bind value to
125 * @param value Value to bind
127 void BindInt64(ArgumentIndex position, int64_t value);
130 * Bind float to the prepared statement argument
132 * @param position Index of argument to bind value to
133 * @param value Value to bind
135 void BindFloat(ArgumentIndex position, float value);
138 * Bind double to the prepared statement argument
140 * @param position Index of argument to bind value to
141 * @param value Value to bind
143 void BindDouble(ArgumentIndex position, double value);
146 * Bind string to the prepared statement argument
148 * @param position Index of argument to bind value to
149 * @param value Value to bind
151 void BindString(ArgumentIndex position, const char *value);
154 * Bind string to the prepared statement argument
156 * @param position Index of argument to bind value to
157 * @param value Value to bind
159 void BindString(ArgumentIndex position, const String& value);
162 * Bind string to the prepared statement argument
164 * @param position Index of argument to bind value to
165 * @param value Value to bind
167 void BindString(ArgumentIndex position, const std::string& value);
170 * Bind optional int 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 BindInteger(ArgumentIndex position, const boost::optional<int> &value);
179 * Bind optional int8 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 BindInt8(ArgumentIndex position, const boost::optional<int8_t> &value);
188 * Bind optional int16 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 BindInt16(ArgumentIndex position, const boost::optional<int16_t> &value);
197 * Bind optional int32 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 BindInt32(ArgumentIndex position, const boost::optional<int32_t> &value);
206 * Bind optional int64 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 BindInt64(ArgumentIndex position, const boost::optional<int64_t> &value);
215 * Bind optional float 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 BindFloat(ArgumentIndex position, const boost::optional<float> &value);
224 * Bind optional double 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 BindDouble(ArgumentIndex position, const boost::optional<double> &value);
233 * Bind optional string to the prepared statement argument.
234 * If optional is not set null will be bound
236 * @param position Index of argument to bind value to
237 * @param value Value to bind
239 void BindString(ArgumentIndex position, const boost::optional<String> &value);
242 * Execute the prepared statement and/or move
243 * to the next row of the result
245 * @return True when there was a row returned
250 * Reset prepared statement's arguments
251 * All parameters will become null
256 * Checks whether column value is null
258 * @throw Exception::InvalidColumn
260 bool IsColumnNull(ColumnIndex column);
263 * Get integer value from column in current row.
265 * @throw Exception::InvalidColumn
267 int GetColumnInteger(ColumnIndex column);
270 * Get int8 value from column in current row.
272 * @throw Exception::InvalidColumn
274 int8_t GetColumnInt8(ColumnIndex column);
277 * Get int16 value from column in current row.
279 * @throw Exception::InvalidColumn
281 int16_t GetColumnInt16(ColumnIndex column);
283 * Get int32 value from column in current row.
285 * @throw Exception::InvalidColumn
287 int32_t GetColumnInt32(ColumnIndex column);
290 * Get int64 value from column in current row.
292 * @throw Exception::InvalidColumn
294 int64_t GetColumnInt64(ColumnIndex column);
297 * Get float value from column in current row.
299 * @throw Exception::InvalidColumn
301 float GetColumnFloat(ColumnIndex column);
304 * Get double value from column in current row.
306 * @throw Exception::InvalidColumn
308 double GetColumnDouble(ColumnIndex column);
311 * Get string value from column in current row.
313 * @throw Exception::InvalidColumn
315 std::string GetColumnString(ColumnIndex column);
318 * Get optional integer value from column in current row.
320 * @throw Exception::InvalidColumn
322 boost::optional<int> GetColumnOptionalInteger(ColumnIndex column);
325 * Get optional int8 value from column in current row.
327 * @throw Exception::InvalidColumn
329 boost::optional<int8_t> GetColumnOptionalInt8(ColumnIndex column);
332 * Get optional int16value from column in current row.
334 * @throw Exception::InvalidColumn
336 boost::optional<int16_t> GetColumnOptionalInt16(ColumnIndex column);
339 * Get optional int32 value from column in current row.
341 * @throw Exception::InvalidColumn
343 boost::optional<int32_t> GetColumnOptionalInt32(ColumnIndex column);
346 * Get optional int64 value from column in current row.
348 * @throw Exception::InvalidColumn
350 boost::optional<int64_t> GetColumnOptionalInt64(ColumnIndex column);
353 * Get optional float value from column in current row.
355 * @throw Exception::InvalidColumn
357 boost::optional<float> GetColumnOptionalFloat(ColumnIndex column);
360 * Get optional double value from column in current row.
362 * @throw Exception::InvalidColumn
364 boost::optional<double> GetColumnOptionalDouble(ColumnIndex column);
367 * Get optional string value from column in current row.
369 * @throw Exception::InvalidColumn
371 boost::optional<String> GetColumnOptionalString(ColumnIndex column);
374 // Move on copy semantics
375 typedef std::unique_ptr<DataCommand> DataCommandAutoPtr;
389 RO = SQLITE_OPEN_NOMUTEX | SQLITE_OPEN_READONLY,
391 * *TODO: please remove CREATE option from RW flag when all places
392 * that need that switched do CRW
394 RW = SQLITE_OPEN_NOMUTEX | SQLITE_OPEN_READWRITE |
396 CRW = RW | SQLITE_OPEN_CREATE
401 typedef sqlite3_int64 RowID;
404 * Synchronization object used to synchronize SQL connection
405 * to the same database across different threads and processes
407 class SynchronizationObject
410 virtual ~SynchronizationObject() {}
413 * Synchronizes SQL connection for multiple clients.
415 virtual void Synchronize() = 0;
418 * Notify all waiting clients that the connection is no longer locked.
420 virtual void NotifyAll() = 0;
424 sqlite3 *m_connection;
429 // Stored data procedures
430 int m_dataCommandsCount;
432 // Synchronization object
433 std::unique_ptr<SynchronizationObject> m_synchronizationObject;
435 virtual void Connect(const std::string &address,
436 Flag::Type = Flag::None, Flag::Option = Flag::RO);
437 virtual void Disconnect();
439 void TurnOnForeignKeys();
441 static SynchronizationObject *AllocDefaultSynchronizationObject();
444 * Execute SQL command without result
449 void ExecCommand(const char *format, ...);
453 * Open SQL connection
455 * Synchronization is archieved by using provided asynchronization object.
456 * If synchronizationObject is set to NULL, so synchronization is performed.
457 * Ownership of the synchronization object is transfered to sql connection
460 * @param address Database file name
461 * @param flags Open flags
462 * @param synchronizationObject A synchronization object to use.
464 explicit SqlConnection(const std::string &address = std::string(),
465 Flag::Type flags = Flag::None,
466 Flag::Option options = Flag::RO,
467 SynchronizationObject *synchronizationObject =
468 AllocDefaultSynchronizationObject());
473 virtual ~SqlConnection();
476 * Execute BEGIN; command to start new transaction
479 void BeginTransaction();
482 * Execute ROLLBACK; command to discard changes made
485 void RollbackTransaction();
488 * Execute COMMIT; command to commit changes in database
491 void CommitTransaction();
494 * Prepare stored procedure
496 * @param format SQL statement
497 * @return Data command representing stored procedure
499 DataCommandAutoPtr PrepareDataCommand(const char *format, ...);
502 * Check whether given table exists
504 * @param tableName Name of the table to check
505 * @return True if given table name exists
507 bool CheckTableExist(const char *tableName);
510 * Get last insert operation new row id
514 RowID GetLastInsertRowID() const;
517 } // namespace SecurityManager
519 #endif // SECURITY_MANAGER_SQL_CONNECTION_H