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.
22 #include <util-func.h>
26 #include "db-util-debug.h"
29 static char _szDBPath[BUFSIZE] = { 0, };
31 static int db_util_busyhandler(void *pData, int count)
34 DBUTIL_TRACE_DEBUG("Busy Handler Called! : PID(%d) / CNT(%d)\n", getpid(), count+1);
35 usleep((count+1)*100000);
38 DBUTIL_TRACE_DEBUG("Busy Handler will be returned SQLITE_BUSY error : PID(%d) \n", getpid());
43 int db_util_open(const char *pszFilePath, sqlite3 **ppDB, int nOption)
45 char *pszErrorMsg = NULL;
47 /* Save DB Path & Option */
48 strncpy(_szDBPath, pszFilePath, strlen(pszFilePath));
49 _szDBPath[strlen(pszFilePath)] = '\0';
52 int rc = sqlite3_open(pszFilePath, ppDB);
53 if (SQLITE_OK != rc) {
57 /* Register Busy handler */
58 rc = sqlite3_busy_handler(*ppDB, db_util_busyhandler, NULL);
59 if (SQLITE_OK != rc) {
60 printf("Fail to register busy handler\n");
65 #if SET_PERSIST_JOURNAL_MODE
66 /* Enable persist journal mode */
67 rc = sqlite3_exec(*ppDB, "PRAGMA journal_mode = PERSIST",
68 NULL, NULL, &pszErrorMsg);
69 if (SQLITE_OK != rc) {
70 DBUTIL_TRACE_WARNING("Fail to change journal mode: %d, %d, %s, %s\n", sqlite3_errcode(*ppDB), sqlite3_extended_errcode(*ppDB), pszErrorMsg, sqlite3_errmsg(*ppDB));
71 sqlite3_free(pszErrorMsg);
79 int db_util_open_with_options(const char *pszFilePath, sqlite3 **ppDB,
80 int flags, const char *zVfs)
82 char *pszErrorMsg = NULL;
84 /* Save DB Path & Option */
85 strncpy(_szDBPath, pszFilePath, strlen(pszFilePath));
86 _szDBPath[strlen(pszFilePath)] = '\0';
89 int rc = sqlite3_open_v2(pszFilePath, ppDB, flags, zVfs);
90 if (SQLITE_OK != rc) {
94 /* Register Busy handler */
95 rc = sqlite3_busy_handler(*ppDB, db_util_busyhandler, NULL);
96 if (SQLITE_OK != rc) {
97 printf("Fail to register busy handler\n");
102 #if SET_PERSIST_JOURNAL_MODE
103 /* Enable persist journal mode */
104 rc = sqlite3_exec(*ppDB, "PRAGMA journal_mode = PERSIST",
105 NULL, NULL, &pszErrorMsg);
106 if (SQLITE_OK != rc) {
107 DBUTIL_TRACE_WARNING("Fail to change journal mode: %s\n", pszErrorMsg);
108 sqlite3_free(pszErrorMsg);
109 sqlite3_close(*ppDB);
116 int db_util_close(sqlite3 *ppDB)
119 int rc = sqlite3_close(ppDB);