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.
25 #include "util-func.h"
26 #include "collation.h"
27 #include "db-util-debug.h"
29 static int __db_util_busyhandler(void *pData, int count)
32 DB_UTIL_TRACE_DEBUG("Busy Handler Called! : PID(%d) / CNT(%d)\n", getpid(), count+1);
33 struct timespec time = {
35 .tv_nsec = (count+1) * 100 * 1000 * 1000
37 nanosleep(&time, NULL);
41 DB_UTIL_TRACE_DEBUG("Busy Handler will be returned SQLITE_BUSY error : PID(%d) \n", getpid());
46 void __db_util_collation_cb(void* pArg, sqlite3* pDB, int eTextRep, const char* szName)
48 if (eTextRep == SQLITE_UTF8 && !sqlite3_stricmp(szName, "localized"))
49 db_util_create_collation(pDB, DB_UTIL_COL_LS_AS_CI,
50 DB_UTIL_COL_UTF8, "localized");
52 DB_UTIL_TRACE_WARNING("No matching collator for %s", szName);
55 static int __db_util_open(sqlite3 *pDB)
60 DB_UTIL_TRACE_WARNING("Invalid input param error");
64 /* Register Busy handler */
65 rc = sqlite3_busy_handler(pDB, __db_util_busyhandler, NULL);
66 if (SQLITE_OK != rc) {
67 DB_UTIL_TRACE_WARNING("Fail to register busy handler\n");
72 #ifdef SET_PERSIST_JOURNAL_MODE
73 /* Code to change default journal mode of sqlite3 is enabled so this option is disabled */
74 /* Enable persist journal mode */
75 rc = sqlite3_exec(pDB, "PRAGMA journal_mode = PERSIST",
76 NULL, NULL, &pszErrorMsg);
77 if (SQLITE_OK != rc) {
78 DB_UTIL_TRACE_WARNING("Fail to change journal mode: %d, %d, %s, %s\n",
80 sqlite3_extended_errcode(pDB),
83 sqlite3_free(pszErrorMsg);
89 sqlite3_collation_needed(pDB, NULL, __db_util_collation_cb);
92 if (DB_UTIL_OK != rc) {
93 DB_UTIL_TRACE_WARNING("Fail to create collation");
101 int db_util_open(const char *pszFilePath, sqlite3 **ppDB, int nOption)
103 if((pszFilePath == NULL) || (ppDB == NULL)) {
104 DB_UTIL_TRACE_WARNING("Invalid input param error");
105 return DB_UTIL_ERROR;
108 if((geteuid() != 0) && (access(pszFilePath, R_OK))) {
109 if(errno == EACCES) {
110 DB_UTIL_TRACE_ERROR("file access permission error");
116 int rc = sqlite3_open(pszFilePath, ppDB);
117 if (SQLITE_OK != rc) {
121 rc = __db_util_open(*ppDB);
126 int db_util_open_with_options(const char *pszFilePath, sqlite3 **ppDB,
127 int flags, const char *zVfs)
129 if((pszFilePath == NULL) || (ppDB == NULL)) {
130 DB_UTIL_TRACE_WARNING("sqlite3 handle null error");
131 return DB_UTIL_ERROR;
135 int rc = sqlite3_open_v2(pszFilePath, ppDB, flags, zVfs);
136 if (SQLITE_OK != rc) {
140 rc = __db_util_open(*ppDB);
145 int db_util_close(sqlite3 *pDB)
147 char *pszErrorMsg = NULL;
150 int rc = sqlite3_close(pDB);
151 if (SQLITE_OK != rc) {
152 DB_UTIL_TRACE_WARNING("Fail to change journal mode: %s\n", pszErrorMsg);
153 sqlite3_free(pszErrorMsg);