4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Hakjoo Ko <hakjoo.ko@samsung.com>
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
26 #include "util-func.h"
27 #include "collation.h"
28 #include "db-util-debug.h"
30 static int __db_util_busyhandler(void *pData, int count)
33 DB_UTIL_TRACE_DEBUG("Busy Handler Called! : PID(%d) / CNT(%d)\n", getpid(), count+1);
34 usleep((count+1)*100000);
37 DB_UTIL_TRACE_WARNING("Busy Handler will be returned SQLITE_BUSY error : PID(%d) \n", getpid());
42 void __db_util_collation_cb(void* pArg, sqlite3* pDB, int eTextRep, const char* szName)
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");
48 DB_UTIL_TRACE_WARNING("No matching collator for %s", szName);
51 static int __db_util_open(sqlite3 *pDB)
56 DB_UTIL_TRACE_WARNING("Invalid input param error");
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");
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",
76 sqlite3_extended_errcode(pDB),
79 sqlite3_free(pszErrorMsg);
85 sqlite3_collation_needed(pDB, NULL, __db_util_collation_cb);
88 if (DB_UTIL_OK != rc) {
89 DB_UTIL_TRACE_WARNING("Fail to create collation");
97 int db_util_open(const char *pszFilePath, sqlite3 **ppDB, int nOption)
99 if((pszFilePath == NULL) || (ppDB == NULL)) {
100 DB_UTIL_TRACE_WARNING("Invalid input param error");
101 return DB_UTIL_ERROR;
104 if((geteuid() != 0) && (access(pszFilePath, R_OK))) {
105 if(errno == EACCES) {
106 DB_UTIL_TRACE_ERROR("file access permission error");
112 int rc = sqlite3_open(pszFilePath, ppDB);
113 if (SQLITE_OK != rc) {
114 DB_UTIL_TRACE_ERROR("sqlite3_open error(%d)", rc);
118 rc = __db_util_open(*ppDB);
123 int db_util_open_with_options(const char *pszFilePath, sqlite3 **ppDB,
124 int flags, const char *zVfs)
128 if((pszFilePath == NULL) || (ppDB == NULL)) {
129 DB_UTIL_TRACE_WARNING("sqlite3 handle null error");
130 return DB_UTIL_ERROR;
134 if(flags == SQLITE_OPEN_READONLY)
142 if((geteuid() != 0) && (access(pszFilePath, mode))) {
143 if(errno == EACCES) {
144 DB_UTIL_TRACE_ERROR("file access permission error");
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)",rc);
156 rc = __db_util_open(*ppDB);
161 int db_util_close(sqlite3 *pDB)
163 char *pszErrorMsg = NULL;
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);