Git Init
[profile/ivi/wrt-plugins-tizen.git] / src / platform / Tizen / Calendar / CalendarQuery.c
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17
18 #include "CalendarQuery.h"
19 #include <unicode/ucol.h>
20 #include <string.h>
21
22 sqlite3 *hDBCt;
23
24 static int db_connnect_count = 0;
25
26 query_error _db_init()
27 {
28         int rc = 0;
29         query_error error_code = QUERY_SUCCESS;
30
31         db_connnect_count++;
32         if(!hDBCt)
33         {
34                 rc = db_util_open(CALENDAR_DB_PATH, &hDBCt, 0);
35                 if(rc != SQLITE_OK)
36                         error_code = QUERY_FAIL;
37         }
38         return error_code;
39 }
40
41 query_error _db_finish()
42 {
43         int rc = 0;
44         query_error error_code = QUERY_SUCCESS;
45
46         db_connnect_count--;
47         if(db_connnect_count == 0)
48         {
49                 rc = db_util_close(hDBCt);
50                 if(rc != SQLITE_OK)
51                         error_code = QUERY_FAIL;
52                 else
53                         hDBCt = NULL;
54         }
55         return error_code;
56 }
57
58 query_error _exec_query(char* query)
59 {
60         int rc = -1;
61         char* pszErrorMsg = NULL;
62         query_error             error_code = QUERY_SUCCESS;
63
64         rc = sqlite3_exec(hDBCt, query, NULL, NULL, &pszErrorMsg);
65
66         if(SQLITE_OK != rc)
67         {
68                 error_code = QUERY_FAIL;
69         }
70
71         if(pszErrorMsg)
72             sqlite3_free(pszErrorMsg);
73
74         return error_code;
75 }
76
77 int _query_step(stmt pStmt)
78 {
79         return sqlite3_step(pStmt);
80 }
81
82 void _contact_query_finalize(stmt pStmt)
83 {
84         int rc = -1;
85
86         if(!pStmt)
87           return;
88
89         rc = sqlite3_finalize(pStmt);
90         if(rc != SQLITE_OK)
91         {
92         }
93 }
94
95 int _calendar_query_column_int(stmt pStmt, int pos)
96 {
97         return (int)sqlite3_column_int(pStmt, pos);
98 }
99
100 char* _calendar_query_column_text(stmt pStmt, int pos)
101 {
102         return (char *)sqlite3_column_text(pStmt, pos);
103 }
104
105 void _query_finalize(stmt pStmt)
106 {
107         int rc = -1;
108
109         if(!pStmt)
110           return;
111
112         rc = sqlite3_finalize(pStmt);
113         if(rc != SQLITE_OK)
114         {
115
116         }
117 }
118
119 int _query_bind_double(stmt pStmt, int pos, double num)
120 {
121    return sqlite3_bind_double(pStmt, pos, num);
122 }
123
124 int _query_bind_int(stmt pStmt, int pos, int num)
125 {
126    return sqlite3_bind_int(pStmt, pos, num);
127 }
128
129 int _query_bind_text(stmt pStmt, int pos, char* str)
130 {
131         int len = 0;
132         if(str != NULL)
133         {
134                 len = strlen(str);
135                 return sqlite3_bind_text(pStmt, pos, (const char*)str, len, SQLITE_STATIC);
136         }
137         return -1;
138 }
139
140 void _query_stmt_reset(stmt stmt)
141 {
142    sqlite3_reset(stmt);
143    sqlite3_clear_bindings(stmt);
144 }
145
146 int _query_column_int(stmt pStmt, int pos)
147 {
148    return sqlite3_column_int(pStmt, pos);
149 }
150
151 double _query_column_double(stmt pStmt, int pos)
152 {
153    return sqlite3_column_double(pStmt, pos);
154 }
155
156 char* _query_column_text(stmt pStmt, int pos)
157 {
158    return (char *)sqlite3_column_text(pStmt, pos);
159 }
160
161 stmt _query_prepare(char *query)
162 {
163         int rc = -1;
164         stmt pStmt = NULL;
165
166         printf("\nquery : %s !!!!\n", query);
167
168         rc = sqlite3_prepare_v2(hDBCt, query, strlen(query), &pStmt, NULL);
169
170         if(SQLITE_OK != rc)
171         {
172                 return NULL;
173         }
174
175         return pStmt;
176 }
177