Initialize Tizen 2.3
[framework/appfw/libslp-db-util.git] / util_func.c
1 /*
2  * libslp-db-util
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Hakjoo Ko <hakjoo.ko@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22 #include <string.h>
23 #include <unistd.h>
24 #include <errno.h>
25
26 #include "util-func.h"
27 #include "collation.h"
28 #include "db-util-debug.h"
29
30 static int __db_util_busyhandler(void *pData, int count)
31 {
32         if(5 - count > 0) {
33                 DB_UTIL_TRACE_DEBUG("Busy Handler Called! : PID(%d) / CNT(%d)\n", getpid(), count+1);
34                 usleep((count+1)*100000);
35                 return 1;
36         } else {
37                 DB_UTIL_TRACE_WARNING("Busy Handler will be returned SQLITE_BUSY error : PID(%d) \n", getpid());
38                 return 0;
39         }
40 }
41
42 void __db_util_collation_cb(void* pArg, sqlite3* pDB, int eTextRep, const char* szName)
43 {
44         if (eTextRep == SQLITE_UTF8 && !sqlite3_stricmp(szName, "localized"))
45                 db_util_create_collation(pDB, DB_UTIL_COL_LS_AI_CI,
46                                                                 DB_UTIL_COL_UTF8, "localized");
47         else
48                 DB_UTIL_TRACE_WARNING("No matching collator for %s", szName);
49 }
50
51 static int __db_util_open(sqlite3 *pDB)
52 {
53         int rc = 0;
54
55         if(pDB == NULL) {
56                 DB_UTIL_TRACE_WARNING("Invalid input param error");
57                 return DB_UTIL_ERROR;
58         }
59
60         /* Register Busy handler */
61         rc = sqlite3_busy_handler(pDB, __db_util_busyhandler, NULL);
62         if (SQLITE_OK != rc) {
63                 DB_UTIL_TRACE_WARNING("Fail to register busy handler\n");
64                 sqlite3_close(pDB);
65                 return rc;
66         }
67
68 #ifdef SET_PERSIST_JOURNAL_MODE
69         /* Code to change default journal mode of sqlite3 is enabled so this option is disabled */
70         /* Enable persist journal mode */
71         rc = sqlite3_exec(pDB, "PRAGMA journal_mode = PERSIST",
72                         NULL, NULL, &pszErrorMsg);
73         if (SQLITE_OK != rc) {
74                 DB_UTIL_TRACE_WARNING("Fail to change journal mode: %d, %d, %s, %s\n",
75                                                                 sqlite3_errcode(pDB),
76                                                                 sqlite3_extended_errcode(pDB),
77                                                                 pszErrorMsg,
78                                                                 sqlite3_errmsg(pDB));
79                 sqlite3_free(pszErrorMsg);
80                 sqlite3_close(pDB);
81                 return rc;
82         }
83 #endif
84
85         sqlite3_collation_needed(pDB, NULL, __db_util_collation_cb);
86
87 #if 0
88         if (DB_UTIL_OK != rc) {
89                 DB_UTIL_TRACE_WARNING("Fail to create collation");
90                 return rc;
91         }
92 #endif
93
94         return DB_UTIL_OK;
95 }
96
97 int db_util_open(const char *pszFilePath, sqlite3 **ppDB, int nOption)
98 {
99         if((pszFilePath == NULL) || (ppDB == NULL)) {
100                 DB_UTIL_TRACE_WARNING("Invalid input param error");
101                 return DB_UTIL_ERROR;
102         }
103
104         if(access(pszFilePath, R_OK)) {
105                 if(errno == EACCES) {
106                         DB_UTIL_TRACE_ERROR("file access permission error");
107                         return SQLITE_PERM;
108                 }
109         }
110
111         /* Open DB */
112         int rc = sqlite3_open(pszFilePath, ppDB);
113         if (SQLITE_OK != rc) {
114                 DB_UTIL_TRACE_ERROR("sqlite3_open error(%d), ");
115                 return rc;
116         }
117
118         rc = __db_util_open(*ppDB);
119
120         return rc;
121 }
122
123 int db_util_open_with_options(const char *pszFilePath, sqlite3 **ppDB,
124                                 int flags, const char *zVfs)
125 {
126         int mode;
127
128         if((pszFilePath == NULL) || (ppDB == NULL)) {
129                 DB_UTIL_TRACE_WARNING("sqlite3 handle null error");
130                 return DB_UTIL_ERROR;
131         }
132
133 #if 0
134         if(flags == SQLITE_OPEN_READONLY)
135                 mode = R_OK;
136         else
137                 mode = R_OK|W_OK;
138 #else
139         mode = R_OK;
140 #endif
141
142         if(access(pszFilePath, mode)) {
143                 if(errno == EACCES) {
144                         DB_UTIL_TRACE_ERROR("file access permission error");
145                         return SQLITE_PERM;
146                 }
147         }
148
149         /* Open DB */
150         int rc = sqlite3_open_v2(pszFilePath, ppDB, flags, zVfs);
151         if (SQLITE_OK != rc) {
152                 DB_UTIL_TRACE_ERROR("sqlite3_open_v2 error(%d), ");
153                 return rc;
154         }
155
156         rc = __db_util_open(*ppDB);
157
158         return rc;
159 }
160
161 int db_util_close(sqlite3 *pDB)
162 {
163         char *pszErrorMsg = NULL;
164
165         /* Close DB */
166         int rc = sqlite3_close(pDB);
167         if (SQLITE_OK != rc) {
168                 DB_UTIL_TRACE_WARNING("Fail to change journal mode: %s\n", pszErrorMsg);
169                 sqlite3_free(pszErrorMsg);
170                 return rc;
171         }
172
173         return DB_UTIL_OK;
174 }
175
176