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 struct timespec time = {
36 .tv_nsec = (count+1) * 100 * 1000 * 1000
38 nanosleep(&time, NULL);
42 DB_UTIL_TRACE_DEBUG("Busy Handler will be returned SQLITE_BUSY error : PID(%d) \n", getpid());
47 void __db_util_collation_cb(void* pArg, sqlite3* pDB, int eTextRep, const char* szName)
49 if (eTextRep == SQLITE_UTF8 && !sqlite3_stricmp(szName, "localized"))
50 db_util_create_collation(pDB, DB_UTIL_COL_LS_AS_CI,
51 DB_UTIL_COL_UTF8, "localized");
53 DB_UTIL_TRACE_WARNING("No matching collator for %s", szName);
56 static int __db_util_open(sqlite3 *pDB)
61 DB_UTIL_TRACE_WARNING("Invalid input param error");
65 /* Register Busy handler */
66 rc = sqlite3_busy_handler(pDB, __db_util_busyhandler, NULL);
67 if (SQLITE_OK != rc) {
68 DB_UTIL_TRACE_WARNING("Fail to register busy handler\n");
73 #ifdef SET_PERSIST_JOURNAL_MODE
74 /* Code to change default journal mode of sqlite3 is enabled so this option is disabled */
75 /* Enable persist journal mode */
76 rc = sqlite3_exec(pDB, "PRAGMA journal_mode = PERSIST",
77 NULL, NULL, &pszErrorMsg);
78 if (SQLITE_OK != rc) {
79 DB_UTIL_TRACE_WARNING("Fail to change journal mode: %d, %d, %s, %s\n",
81 sqlite3_extended_errcode(pDB),
84 sqlite3_free(pszErrorMsg);
90 sqlite3_collation_needed(pDB, NULL, __db_util_collation_cb);
93 if (DB_UTIL_OK != rc) {
94 DB_UTIL_TRACE_WARNING("Fail to create collation");
102 int db_util_open(const char *pszFilePath, sqlite3 **ppDB, int nOption)
104 if ((pszFilePath == NULL) || (ppDB == NULL)) {
105 DB_UTIL_TRACE_WARNING("Invalid input param error");
106 return DB_UTIL_ERROR;
109 if ((geteuid() != 0) && (access(pszFilePath, R_OK))) {
110 if (errno == EACCES) {
111 DB_UTIL_TRACE_ERROR("file access permission error");
117 int rc = sqlite3_open(pszFilePath, ppDB);
118 if (SQLITE_OK != rc) {
122 rc = __db_util_open(*ppDB);
127 int db_util_open_with_options(const char *pszFilePath, sqlite3 **ppDB,
128 int flags, const char *zVfs)
130 if ((pszFilePath == NULL) || (ppDB == NULL)) {
131 DB_UTIL_TRACE_WARNING("sqlite3 handle null error");
132 return DB_UTIL_ERROR;
136 int rc = sqlite3_open_v2(pszFilePath, ppDB, flags, zVfs);
137 if (SQLITE_OK != rc) {
141 rc = __db_util_open(*ppDB);
146 int db_util_close(sqlite3 *pDB)
149 int rc = sqlite3_close(pDB);
150 if (SQLITE_OK != rc) {
151 DB_UTIL_TRACE_WARNING("Fail to close DB\n");