Tizen 2.0 Release
[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
25 #include "util-func.h"
26 #include "collation.h"
27 #include "db-util-debug.h"
28
29 static int __db_util_busyhandler(void *pData, int count)
30 {
31         if(5 - count > 0) {
32                 DB_UTIL_TRACE_DEBUG("Busy Handler Called! : PID(%d) / CNT(%d)\n", getpid(), count+1);
33                 usleep((count+1)*100000);
34                 return 1;
35         } else {
36                 DB_UTIL_TRACE_DEBUG("Busy Handler will be returned SQLITE_BUSY error : PID(%d) \n", getpid());
37                 return 0;
38         }
39 }
40
41 static int __db_util_open(sqlite3 *ppDB)
42 {
43         int rc = 0;
44
45         if(ppDB == NULL) {
46                 DB_UTIL_TRACE_WARNING("Invalid input param error");
47                 return DB_UTIL_ERROR;
48         }
49
50         /* Register Busy handler */
51         rc = sqlite3_busy_handler(ppDB, __db_util_busyhandler, NULL);
52         if (SQLITE_OK != rc) {
53                 DB_UTIL_TRACE_WARNING("Fail to register busy handler\n");
54                 sqlite3_close(ppDB);
55                 return rc;
56         }
57
58 #ifdef SET_PERSIST_JOURNAL_MODE
59         /* Code to change default journal mode of sqlite3 is enabled so this option is disabled */
60         /* Enable persist journal mode */
61         rc = sqlite3_exec(ppDB, "PRAGMA journal_mode = PERSIST",
62                         NULL, NULL, &pszErrorMsg);
63         if (SQLITE_OK != rc) {
64                 DB_UTIL_TRACE_WARNING("Fail to change journal mode: %d, %d, %s, %s\n",
65                                                                 sqlite3_errcode(ppDB),
66                                                                 sqlite3_extended_errcode(ppDB),
67                                                                 pszErrorMsg,
68                                                                 sqlite3_errmsg(ppDB));
69                 sqlite3_free(pszErrorMsg);
70                 sqlite3_close(ppDB);
71                 return rc;
72         }
73 #endif
74
75         db_util_create_collation(ppDB, DB_UTIL_COL_LS_AS_CI,
76                                                         DB_UTIL_COL_UTF8, "localized");
77 #if 0
78         if (DB_UTIL_OK != rc) {
79                 DB_UTIL_TRACE_WARNING("Fail to create collation");
80                 return rc;
81         }
82 #endif
83
84         return DB_UTIL_OK;
85 }
86
87 int db_util_open(const char *pszFilePath, sqlite3 **ppDB, int nOption)
88 {
89         char *pszErrorMsg = NULL;
90
91         if((pszFilePath == NULL) || (ppDB == NULL)) {
92                 DB_UTIL_TRACE_WARNING("Invalid input param error");
93                 return DB_UTIL_ERROR;
94         }
95
96         /* Open DB */
97         int rc = sqlite3_open(pszFilePath, ppDB);
98         if (SQLITE_OK != rc) {
99                 return rc;
100         }
101
102         rc = __db_util_open(*ppDB);
103
104         return rc;
105 }
106
107 int db_util_open_with_options(const char *pszFilePath, sqlite3 **ppDB,
108                                 int flags, const char *zVfs)
109 {
110         char *pszErrorMsg = NULL;
111
112         if((pszFilePath == NULL) || (ppDB == NULL)) {
113                 DB_UTIL_TRACE_WARNING("sqlite3 handle null error");
114                 return DB_UTIL_ERROR;
115         }
116
117         /* Open DB */
118         int rc = sqlite3_open_v2(pszFilePath, ppDB, flags, zVfs);
119         if (SQLITE_OK != rc) {
120                 return rc;
121         }
122
123         rc = __db_util_open(*ppDB);
124
125         return rc;
126 }
127
128 int db_util_close(sqlite3 *ppDB)
129 {
130         char *pszErrorMsg = NULL;
131
132         /* Close DB */
133         int rc = sqlite3_close(ppDB);
134         if (SQLITE_OK != rc) {
135                 DB_UTIL_TRACE_WARNING("Fail to change journal mode: %s\n", pszErrorMsg);
136                 sqlite3_free(pszErrorMsg);
137                 return rc;
138         }
139
140         return DB_UTIL_OK;
141 }
142
143