[Contact] Refactoring API structures
[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 calendar_query_error calendar_db_init()
27 {
28         int rc = 0;
29         calendar_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 calendar_query_error calendar_db_finish()
42 {
43         int rc = 0;
44         calendar_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 calendar_query_error calendar_exec_query(char* query)
59 {
60         int rc = -1;
61         char* pszErrorMsg = NULL;
62         calendar_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 calendar_query_step(stmt pStmt)
78 {
79         return sqlite3_step(pStmt);
80 }
81
82 void calendar_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
96 int calendar_query_bind_double(stmt pStmt, int pos, double num)
97 {
98    return sqlite3_bind_double(pStmt, pos, num);
99 }
100
101 int calendar_query_bind_int(stmt pStmt, int pos, int num)
102 {
103    return sqlite3_bind_int(pStmt, pos, num);
104 }
105
106 int calendar_query_bind_text(stmt pStmt, int pos, char* str)
107 {
108         int len = 0;
109         if(str != NULL)
110         {
111                 len = strlen(str);
112                 return sqlite3_bind_text(pStmt, pos, (const char*)str, len, SQLITE_STATIC);
113         }
114         return -1;
115 }
116
117 void calendar_query_stmt_reset(stmt stmt)
118 {
119    sqlite3_reset(stmt);
120    sqlite3_clear_bindings(stmt);
121 }
122
123 int calendar_query_column_int(stmt pStmt, int pos)
124 {
125    return sqlite3_column_int(pStmt, pos);
126 }
127
128 double calendar_query_column_double(stmt pStmt, int pos)
129 {
130    return sqlite3_column_double(pStmt, pos);
131 }
132
133 char* calendar_query_column_text(stmt pStmt, int pos)
134 {
135    return (char *)sqlite3_column_text(pStmt, pos);
136 }
137
138 stmt calendar_query_prepare(char *query)
139 {
140         int rc = -1;
141         stmt pStmt = NULL;
142
143         printf("\nquery : %s !!!!\n\n", query);
144
145         rc = sqlite3_prepare_v2(hDBCt, query, strlen(query), &pStmt, NULL);
146
147         if(SQLITE_OK != rc)
148         {
149                 return NULL;
150         }
151
152         return pStmt;
153 }
154