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