2 * Copyright (c) 2012, 2013 Samsung Electronics Co., Ltd.
4 * Licensed under the Flora License, Version 1.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
8 * http://floralicense.org/license/
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.
18 #include "ug-nfc-setting-main.h"
19 #include "ug-nfc-setting-db.h"
22 #define DB_FILE_PATH "/opt/dbspace/.net-nfcpush.db"
23 #define DB_TABLE_NAME "NfcPushMsgTable"
26 /* CREATE TABLE NfcPushMsgTable ( AppId TEXT PRIMARY KEY, PkgName TEXT, IconPath TEXT, MsgDesc TEXT ); */
28 #define DB_QUERY_LEN 512
30 static sqlite3 *g_predefined_item_db;
31 static sqlite3_stmt *g_predefined_item_pstmt;
34 static void _db_finalize_statement(void)
36 int db_ret = SQLITE_OK;
38 if (g_predefined_item_pstmt != NULL)
40 db_ret = sqlite3_finalize(g_predefined_item_pstmt);
41 g_predefined_item_pstmt = NULL;
43 if(db_ret != SQLITE_OK)
45 LOGE("Failed to sqlite3_finalize error[%d]", db_ret);
51 int _ug_nfc_setting_db_open(void)
53 int db_ret = SQLITE_OK;
55 db_ret = sqlite3_open(DB_FILE_PATH, &g_predefined_item_db);
56 if(db_ret != SQLITE_OK)
58 LOGE("Failed to open database. error[%d]", db_ret);
64 int _ug_nfc_setting_db_close(void)
66 int db_ret = SQLITE_OK;
68 if (g_predefined_item_db != NULL)
70 db_ret = sqlite3_close(g_predefined_item_db);
71 if(db_ret != SQLITE_OK)
73 LOGE("Failed to close database. error[%d]", db_ret);
76 g_predefined_item_db = NULL;
82 int _ug_nfc_setting_db_open_table(int *table_handle)
84 int db_ret = SQLITE_OK;
87 /* get num_of_index */
88 _ug_nfc_setting_db_get_count(&count);
92 char query[DB_QUERY_LEN] = { 0, };
93 sqlite3_stmt *stmt = NULL;
95 snprintf(query, DB_QUERY_LEN, "select * from %s", DB_TABLE_NAME);
96 //LOGD("query [%s]", query);
98 db_ret = sqlite3_prepare_v2(g_predefined_item_db, query, strlen(query), &stmt, NULL);
99 if (db_ret != SQLITE_OK)
101 LOGE("Failed to sqlite3_prepare_v2 error[%d]", db_ret);
102 if (g_predefined_item_pstmt != NULL)
104 db_ret = sqlite3_finalize(stmt);
106 if(db_ret != SQLITE_OK)
108 LOGE("Failed to sqlite3_finalize error[%d]", db_ret);
114 *table_handle = (int)stmt;
119 db_ret = SQLITE_ERROR;
125 int _ug_nfc_setting_db_get_count(int *count)
127 int db_ret = SQLITE_OK;
128 char query[DB_QUERY_LEN] = { 0, };
130 snprintf(query, DB_QUERY_LEN,
131 "select count(AppId) from %s ",
133 //LOGD("query [%s]", query);
135 db_ret = sqlite3_prepare_v2(g_predefined_item_db, query, strlen(query), &g_predefined_item_pstmt, NULL);
136 if (db_ret != SQLITE_OK)
138 LOGE("Failed to sqlite3_prepare_v2 error[%d]", db_ret);
139 _db_finalize_statement();
143 db_ret = sqlite3_step(g_predefined_item_pstmt);
144 if (db_ret != SQLITE_ROW)
146 LOGE("Failed to sqlite3_step error[%d]", db_ret);
147 _db_finalize_statement();
151 *count = sqlite3_column_int(g_predefined_item_pstmt, 0);
153 _db_finalize_statement();
158 int _ug_nfc_setting_db_get_next_record(int table_handle,
159 char **app_id, char **pkgName,
160 char **iconPath, char **msgDes)
162 int db_ret = SQLITE_OK;
163 sqlite3_stmt *stmt = NULL;
165 stmt = (sqlite3_stmt *)table_handle;
171 db_ret = sqlite3_step(stmt);
172 if (db_ret != SQLITE_DONE)
174 if ((char *)sqlite3_column_text(stmt, 0) != NULL)
175 *app_id = strdup((char *)sqlite3_column_text(stmt, 0));
176 if ((char *)sqlite3_column_text(stmt, 1) != NULL)
177 *pkgName = strdup((char *)sqlite3_column_text(stmt, 1));
178 if ((char *)sqlite3_column_text(stmt, 2) != NULL)
179 *iconPath = strdup((char *)sqlite3_column_text(stmt, 2));
180 if ((char *)sqlite3_column_text(stmt, 3) != NULL)
181 *msgDes = strdup((char *)sqlite3_column_text(stmt, 3));
187 int _ug_nfc_setting_db_get_pkgName(char *app_id, char **pkgName)
189 int db_ret = SQLITE_OK;
190 char query[DB_QUERY_LEN] = { 0, };
192 snprintf(query, DB_QUERY_LEN,
193 "select PkgName from %s where AppId =\"%s\"", DB_TABLE_NAME, app_id);
194 //LOGD("query [%s]", query);
196 db_ret = sqlite3_prepare_v2(g_predefined_item_db, query, strlen(query), &g_predefined_item_pstmt, NULL);
197 if (db_ret != SQLITE_OK)
199 LOGE("Failed to sqlite3_prepare_v2 error[%d]", db_ret);
200 _db_finalize_statement();
204 db_ret = sqlite3_step(g_predefined_item_pstmt);
205 if (db_ret != SQLITE_ROW)
207 LOGE("Failed to sqlite3_step error[%d]", db_ret);
208 _db_finalize_statement();
212 *pkgName = strdup((char *)sqlite3_column_text(g_predefined_item_pstmt, 0));
214 _db_finalize_statement();
219 int _ug_nfc_setting_db_close_table(int table_handle)
221 int db_ret = SQLITE_OK;
222 sqlite3_stmt *stmt = NULL;
224 stmt = (sqlite3_stmt *)table_handle;
228 db_ret = sqlite3_finalize(stmt);
229 if(db_ret != SQLITE_OK)
231 LOGE("Failed to sqlite3_finalize error[%d]", db_ret);