sync with tizen_2.0
[platform/framework/native/appfw.git] / src / io / inc / FIo_DatabaseImpl.h
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 /**
19  * @file        FIoDatabase.h
20  * @brief   This is the header file for the %_DatabaseImpl class.
21  *
22  * This header file contains the declarations of the %_DatabaseImpl class.
23  */
24
25 #ifndef _FIO_INTERNAL_DATABASE_IMPL_H_
26 #define _FIO_INTERNAL_DATABASE_IMPL_H_
27
28 #include <FIoDatabase.h>
29 #include <FBaseObject.h>
30 #include <FBaseColLinkedList.h>
31 #include <FOspConfig.h>
32
33 namespace Tizen { namespace Base
34 {
35 class String;
36 class ByteBuffer;
37 namespace Collection
38 {
39 class LinkedList;
40 }
41 }}
42
43 namespace Tizen { namespace Io
44 {
45
46 class DbStatement;
47 class DbEnumerator;
48 class _DbEnumeratorImpl;
49 class _IDbUserFunctionListener;
50 class _IDbUserAggregateListener;
51 class _IDbUserCollationListener;
52
53 typedef struct Mem sqlite3_value;
54 typedef struct sqlite3_context sqlite3_context;
55
56 class _OSP_EXPORT_ _DatabaseImpl
57         : public Tizen::Base::Object
58 {
59
60 public:
61         _DatabaseImpl(void);
62
63         virtual ~_DatabaseImpl(void);
64
65         result Construct(const Tizen::Base::String& dbPath, bool createIfNotExist);
66
67         result Construct(const Tizen::Base::String& dbPath, long openMode, const Tizen::Base::ByteBuffer* pSecretKey);
68
69         result Construct(const Tizen::Base::String& dbPath, const char* pOpenMode, const Tizen::Base::ByteBuffer* pSecretKey);
70
71         DbStatement* CreateStatementN(const Tizen::Base::String& statement);
72
73         DbEnumerator* ExecuteStatementN(const DbStatement& statement);
74
75         result ExecuteSql(const Tizen::Base::String& sql, bool autoCommit);
76
77         DbEnumerator* QueryN(const Tizen::Base::String& query);
78
79         result BeginTransaction(void);
80
81         result CommitTransaction(void);
82
83         result RollbackTransaction(void);
84
85         Tizen::Base::String GetName(void) const;
86
87         //
88         // This method is for internal use only.
89         // Using this method can cause behavioral, security-related, and consistency-related issues in the application.
90         //
91         // Registers the user defined SQL function.
92         //
93         // @since 2.1
94         // @return              An error code
95         // @param[in]   functionName                    Name of the SQL function.
96         // @param[in]   argumentCount                   The number of arguments to be used by the SQL function.
97         // @param[in]   pUserData                               User data if any
98         // @param[in]   pUserFunctionListener   A listener invoked when the SQL function is executed.
99         // @param[in]   pUserAggregateListener  A listener invoked when the SQL aggregate is executed.
100         // @exception   E_SUCCESS                               The method was successful.
101         // @exception   E_INVALID_STATE                 The database was not opened.
102         // @exception   E_INVALID_ARG                   Either of the following conditions has occurred: @n
103         //                                                                                      - The length of the specified @c functionName exceeded system limitation. @n
104         //                                                                                      - The specified @c argumentCount passed was < 0 or > 127.
105         //                                                                              - One of the specified @c pUserFunctionListener and the specified @c pUserAggregateListener
106         //                                                                              cannot be registered together.
107         // @exception   E_DATABASE                              Failed to create the user defined SQL function due to its misuse.
108         //
109         result RegisterUserFunction(const Tizen::Base::String& functionName, int argumentCount, void* pUserData, _IDbUserFunctionListener* pUserFunctionListener, _IDbUserAggregateListener* pUserAggregateListener);
110
111         //
112         // This method is for internal use only.
113         // Using this method can cause behavioral, security-related, and consistency-related issues in the application.
114         //
115         // Registers the user defined SQL collation function.
116         //
117         // @since 2.1
118         // @return              An error code
119         // @param[in]   collationName                   Name of the SQL collating function
120         // @param[in]   pUserData                               User data if any
121         // @param[in]   pUserCollationListener  A listener invoked when the SQL collating function is executed.
122         // @exception   E_SUCCESS                               The method was successful.
123         // @exception   E_INVALID_STATE                 The database was not opened.
124         // @exception   E_DATABASE                              Failed to create the user defined SQL collating function due to its misuse.
125         //
126         result RegisterUserCollation(const Tizen::Base::String& collationName, void* pUserData, _IDbUserCollationListener* pUserCollationListener);
127
128         long long GetLastInsertRowId(void) const;
129
130         static result Delete(const Tizen::Base::String& databasePath);
131
132         static bool Exists(const Tizen::Base::String& databasePath);
133
134         static result ConvertToSecureDatabase(const Tizen::Base::String& plainDatabasePath, const Tizen::Base::String& secureDatabasePath, const Tizen::Base::ByteBuffer* pKey = null);
135
136         static _DatabaseImpl* GetInstance(Database& database);
137
138         static const _DatabaseImpl* GetInstance(const Database& database);
139
140 private:
141         _DatabaseImpl(_DatabaseImpl& databaseImpl);
142
143         _DatabaseImpl& operator =(const _DatabaseImpl& databaseImpl);
144
145         bool IsDirectory(const Tizen::Base::String& databasePath);
146
147         bool VerifyDatabaseOpenMode(const char* pOpenMode, long& legacyMode);
148
149         static void UserFunctionListener(sqlite3_context* pContext, int argumentCount, sqlite3_value** pArgList);
150
151         static void UserFunctionStepListener(sqlite3_context* pContext, int argumentCount, sqlite3_value** pArgList);
152
153         static void UserFunctionFinalListener(sqlite3_context* pContext);
154
155         static int UserCollationListener(void* pContext, int length1, const void* pInput1, int length2, const void* pInput2);
156
157         Tizen::Base::String __dbName;
158         void* __pDatabase;
159         bool __transactionGoingOn;
160         Tizen::Base::Collection::LinkedList __userFunctionContextList;
161         Tizen::Base::Collection::LinkedList __userCollationContextList;
162
163         friend class DbStatement;
164         friend class DbEnumerator;
165         friend class _DbEnumeratorImpl;
166
167 }; // _DatabaseImpl
168
169 }} // Tizen::Io
170
171 #endif //_FIO_INTERNAL_DATABASE_IMPL_H_
172