tizen 2.3.1 release
[framework/appfw/pkgmgr-info.git] / parser / pkgmgr_parser_db_util.c
1 /*
2  * pkgmgr_parser_db_util
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Jayoun Lee <airjany@samsung.com>, Sewook Park <sewook7.park@samsung.com>,
7  * Jaeho Lee <jaeho81.lee@samsung.com>, Shobhit Srivastava <shobhit.s@samsung.com>
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  */
22
23 #include <db-util.h>
24 #include "pkgmgr_parser.h"
25 #include "pkgmgrinfo_debug.h"
26
27 #ifdef LOG_TAG
28 #undef LOG_TAG
29 #endif
30 #define LOG_TAG "PKGMGR_PARSER"
31
32 int _pkgmgr_db_open(const char *dbfile, sqlite3 **database)
33 {
34         int ret = 0;
35         retvm_if(dbfile == NULL, PM_PARSER_R_ERROR, "dbfile is NULL");
36         retvm_if(database == NULL, PM_PARSER_R_ERROR, "database is NULL");
37
38         ret = db_util_open(dbfile, database, 0);
39         retvm_if(ret != SQLITE_OK, PM_PARSER_R_ERROR, "db_open fail[ret = %d]\n", ret);
40
41         return 0;
42 }
43
44 int _pkgmgr_db_prepare(sqlite3 *database, const char *query, sqlite3_stmt **stmt)
45 {
46         int ret = 0;
47         retvm_if(database == NULL, PM_PARSER_R_ERROR, "database is NULL");
48         retvm_if(query == NULL, PM_PARSER_R_ERROR, "query is NULL");
49         retvm_if(stmt == NULL, PM_PARSER_R_ERROR, "stmt is NULL");
50
51         ret = sqlite3_prepare_v2(database, query, strlen(query), stmt, NULL);
52         retvm_if(ret != SQLITE_OK, PM_PARSER_R_ERROR, "db_prepare fail[ret = %d]\n", ret);
53
54         return 0;
55 }
56
57 int _pkgmgr_db_step(sqlite3_stmt *stmt)
58 {
59         int ret = 0;
60         retvm_if(stmt == NULL, PM_PARSER_R_ERROR, "stmt is NULL");
61
62         ret = sqlite3_step(stmt);
63         retvm_if(ret != SQLITE_ROW, PM_PARSER_R_ERROR, "db_step fail[ret = %d]\n", ret);
64
65         return 0;
66 }
67
68 int _pkgmgr_db_get_str(sqlite3_stmt *stmt, int index, char **str)
69 {
70         retvm_if(stmt == NULL, PM_PARSER_R_ERROR, "stmt is NULL");
71         retvm_if(str == NULL, PM_PARSER_R_ERROR, "str is NULL");
72
73         *str = (char *)sqlite3_column_text(stmt, index);
74
75         return 0;
76 }
77
78 int _pkgmgr_db_finalize(sqlite3_stmt *stmt)
79 {
80         int ret = 0;
81         retvm_if(stmt == NULL, PM_PARSER_R_ERROR, "stmt is NULL");
82
83         ret = sqlite3_finalize(stmt);
84         retvm_if(ret != SQLITE_OK, PM_PARSER_R_ERROR, "db_finalize fail[ret = %d]\n", ret);
85
86         return 0;
87 }
88
89 int _pkgmgr_db_close(sqlite3 *database)
90 {
91         int ret = 0;
92         ret = sqlite3_close(database);
93         retvm_if(ret != SQLITE_OK, PM_PARSER_R_ERROR, "db_close fail[ret = %d]\n", ret);
94
95         return 0;
96 }
97