2465cb3b7b67c23a946769fbe1c77722e793f02f
[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 <util-func.h>
23 #include <string.h>
24 #include <unistd.h>
25
26 #include "db-util-debug.h"
27
28 #define BUFSIZE         512
29 static char _szDBPath[BUFSIZE] = { 0, };
30
31 static int db_util_busyhandler(void *pData, int count)
32 {
33         if(5 - count > 0) {
34                 DBUTIL_TRACE_DEBUG("Busy Handler Called! : PID(%d) / CNT(%d)\n", getpid(), count+1);    
35                 usleep((count+1)*100000);
36                 return 1;
37         } else {
38                 DBUTIL_TRACE_DEBUG("Busy Handler will be returned SQLITE_BUSY error : PID(%d) \n", getpid());
39                 return 0;
40         }
41 }
42
43 int db_util_open(const char *pszFilePath, sqlite3 **ppDB, int nOption)
44 {
45         char *pszErrorMsg = NULL;
46         
47         /* Save DB Path & Option */
48         strncpy(_szDBPath, pszFilePath, strlen(pszFilePath));
49         _szDBPath[strlen(pszFilePath)] = '\0';
50         
51         /* Open DB */
52         int rc = sqlite3_open(pszFilePath, ppDB);
53         if (SQLITE_OK != rc) {
54                 return rc;
55         }
56         
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");
61                 sqlite3_close(*ppDB);
62                 return rc;
63         }
64
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);
72                 sqlite3_close(*ppDB);
73                 return rc;
74         }
75 #endif
76         return rc;
77 }
78
79 int db_util_open_with_options(const char *pszFilePath, sqlite3 **ppDB,
80                                 int flags, const char *zVfs)
81 {
82         char *pszErrorMsg = NULL;
83         
84         /* Save DB Path & Option */
85         strncpy(_szDBPath, pszFilePath, strlen(pszFilePath));
86         _szDBPath[strlen(pszFilePath)] = '\0';
87         
88         /* Open DB */
89         int rc = sqlite3_open_v2(pszFilePath, ppDB, flags, zVfs);
90         if (SQLITE_OK != rc) {
91                 return rc;
92         }
93         
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");
98                 sqlite3_close(*ppDB);
99                 return rc;
100         }
101         
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);
110                 return rc;
111         }
112 #endif
113         return rc;
114 }
115
116 int db_util_close(sqlite3 *ppDB)
117 {
118         /* Close DB */
119         int rc = sqlite3_close(ppDB);
120         return rc;
121 }
122
123