[Tizen_6_build] Enable build with GCC 9.2
[platform/core/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 #include <time.h>
25
26 #include "util-func.h"
27 #include "collation.h"
28 #include "db-util-debug.h"
29
30 static int __db_util_busyhandler(void *pData, int count)
31 {
32         if (5 - count > 0) {
33                 DB_UTIL_TRACE_DEBUG("Busy Handler Called! : PID(%d) / CNT(%d)\n", getpid(), count+1);
34                 struct timespec time = {
35                         .tv_sec = 0,
36                         .tv_nsec = (count+1) * 100 * 1000 * 1000
37                 };
38                 nanosleep(&time, NULL);
39
40                 return 1;
41         } else {
42                 DB_UTIL_TRACE_DEBUG("Busy Handler will be returned SQLITE_BUSY error : PID(%d) \n", getpid());
43                 return 0;
44         }
45 }
46
47 void __db_util_collation_cb(void* pArg, sqlite3* pDB, int eTextRep, const char* szName)
48 {
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");
52         else
53                 DB_UTIL_TRACE_WARNING("No matching collator for %s", szName);
54 }
55
56 static int __db_util_open(sqlite3 *pDB)
57 {
58         int rc = 0;
59
60         if (pDB == NULL) {
61                 DB_UTIL_TRACE_WARNING("Invalid input param error");
62                 return DB_UTIL_ERROR;
63         }
64
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");
69                 sqlite3_close(pDB);
70                 return rc;
71         }
72
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",
80                                                                 sqlite3_errcode(pDB),
81                                                                 sqlite3_extended_errcode(pDB),
82                                                                 pszErrorMsg,
83                                                                 sqlite3_errmsg(pDB));
84                 sqlite3_free(pszErrorMsg);
85                 sqlite3_close(pDB);
86                 return rc;
87         }
88 #endif
89
90         sqlite3_collation_needed(pDB, NULL, __db_util_collation_cb);
91
92 #if 0
93         if (DB_UTIL_OK != rc) {
94                 DB_UTIL_TRACE_WARNING("Fail to create collation");
95                 return rc;
96         }
97 #endif
98
99         return DB_UTIL_OK;
100 }
101
102 int db_util_open(const char *pszFilePath, sqlite3 **ppDB, int nOption)
103 {
104         if ((pszFilePath == NULL) || (ppDB == NULL)) {
105                 DB_UTIL_TRACE_WARNING("Invalid input param error");
106                 return DB_UTIL_ERROR;
107         }
108
109         if ((geteuid() != 0) && (access(pszFilePath, R_OK))) {
110                 if (errno == EACCES) {
111                         DB_UTIL_TRACE_ERROR("file access permission error");
112                         return SQLITE_PERM;
113                 }
114         }
115
116         /* Open DB */
117         int rc = sqlite3_open(pszFilePath, ppDB);
118         if (SQLITE_OK != rc) {
119                 return rc;
120         }
121
122         rc = __db_util_open(*ppDB);
123
124         return rc;
125 }
126
127 int db_util_open_with_options(const char *pszFilePath, sqlite3 **ppDB,
128                                 int flags, const char *zVfs)
129 {
130         if ((pszFilePath == NULL) || (ppDB == NULL)) {
131                 DB_UTIL_TRACE_WARNING("sqlite3 handle null error");
132                 return DB_UTIL_ERROR;
133         }
134
135         /* Open DB */
136         int rc = sqlite3_open_v2(pszFilePath, ppDB, flags, zVfs);
137         if (SQLITE_OK != rc) {
138                 return rc;
139         }
140
141         rc = __db_util_open(*ppDB);
142
143         return rc;
144 }
145
146 int db_util_close(sqlite3 *pDB)
147 {
148         /* Close DB */
149         int rc = sqlite3_close(pDB);
150         if (SQLITE_OK != rc) {
151                 DB_UTIL_TRACE_WARNING("Fail to close DB\n");
152                 return rc;
153         }
154
155         return DB_UTIL_OK;
156 }
157
158