Initialize Tizen 2.3
[framework/api/application.git] / src / preference.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 <stdio.h>
19 #include <stdlib.h>
20 #include <unistd.h>
21 #include <string.h>
22 #include <sqlite3.h>
23
24 #include <app_private.h>
25
26 #include <app_preference.h>
27 #include <app_preference_private.h>
28
29 #include <dlog.h>
30
31 #ifdef LOG_TAG
32 #undef LOG_TAG
33 #endif
34
35 #define LOG_TAG "CAPI_APPFW_APPLICATION_PREFERENCE"
36 #define DBG_MODE (1)
37
38 static sqlite3 *pref_db = NULL;
39 static bool is_update_hook_registered = false;
40 static pref_changed_cb_node_t *head = NULL;
41
42 static void _finish(void *data)
43 {
44         if (pref_db != NULL)
45         {
46                 sqlite3_close(pref_db);
47                 pref_db = NULL;
48         }
49 }
50
51 static int _initialize(void)
52 {
53         char data_path[TIZEN_PATH_MAX] = {0, };
54         char db_path[TIZEN_PATH_MAX] = {0, };
55         int ret;
56         char *errmsg;
57
58         if (app_get_data_directory(data_path, sizeof(data_path)) == NULL)
59         {
60                 LOGE("IO_ERROR(0x%08x) : fail to get data directory", PREFERENCE_ERROR_IO_ERROR);
61                 return PREFERENCE_ERROR_IO_ERROR;
62         }
63         snprintf(db_path, sizeof(db_path), "%s/%s", data_path, PREF_DB_NAME);
64
65         ret = sqlite3_open(db_path, &pref_db);
66         if (ret != SQLITE_OK)
67         {
68                 LOGE("IO_ERROR(0x%08x) : fail to open db(%s)", PREFERENCE_ERROR_IO_ERROR, sqlite3_errmsg(pref_db));
69                 pref_db = NULL;
70                 return PREFERENCE_ERROR_IO_ERROR;
71         }
72
73         ret = sqlite3_exec(pref_db, "CREATE TABLE IF NOT EXISTS pref ( pref_key TEXT PRIMARY KEY, pref_type TEXT, pref_data TEXT)",
74                        NULL, NULL, &errmsg);
75         if (ret != SQLITE_OK)
76         {
77                 LOGE("IO_ERROR(0x%08x) : fail to create db table(%s)", PREFERENCE_ERROR_IO_ERROR, errmsg);
78                 sqlite3_free(errmsg);
79                 sqlite3_close(pref_db);
80                 pref_db = NULL;
81                 return PREFERENCE_ERROR_IO_ERROR;
82         }
83
84         app_finalizer_add(_finish, NULL);
85
86         return PREFERENCE_ERROR_NONE;
87 }
88
89 //static int _write_data(const char *key, preference_type_e type, const char *data)
90 static int _write_data(const char *key, const char *type, const char *data)
91 {
92         int ret;
93         char *buf;
94         char *errmsg;
95         bool exist = false;
96
97         if (key == NULL || key[0] == '\0'  || data == NULL)
98         {
99                 LOGE("INVALID_PARAMETER(0x%08x)", PREFERENCE_ERROR_INVALID_PARAMETER);
100                 return PREFERENCE_ERROR_INVALID_PARAMETER;
101         }
102
103         /* insert data or update data if data already exist */
104         ret = preference_is_existing(key, &exist);
105         if (ret != PREFERENCE_ERROR_NONE)
106         {
107                 return ret;
108         }
109
110         // to use sqlite3_update_hook, we have to use INSERT/UPDATE operation instead of REPLACE operation
111         if (exist)
112         {
113                 buf = sqlite3_mprintf("UPDATE %s SET %s='%s', %s='%s' WHERE %s='%s';",
114                                                                 PREF_TBL_NAME, PREF_F_TYPE_NAME, type, PREF_F_DATA_NAME, data, PREF_F_KEY_NAME, key);
115         }
116         else
117         {
118                 buf = sqlite3_mprintf("INSERT INTO %s (%s, %s, %s) values (%Q, %Q, %Q);",
119                                                                 PREF_TBL_NAME, PREF_F_KEY_NAME, PREF_F_TYPE_NAME, PREF_F_DATA_NAME, key, type, data);
120         }
121
122         if (buf == NULL)
123         {
124                 LOGE("IO_ERROR(0x%08x) : fail to create query string", PREFERENCE_ERROR_IO_ERROR);
125                 return PREFERENCE_ERROR_IO_ERROR;
126         }
127
128         ret = sqlite3_exec(pref_db, buf, NULL, NULL, &errmsg);
129         sqlite3_free(buf);
130         if (ret != SQLITE_OK)
131         {
132                 LOGE("IO_ERROR(0x%08x): fail to write data(%s)", PREFERENCE_ERROR_IO_ERROR, errmsg);
133                 sqlite3_free(errmsg);
134                 return PREFERENCE_ERROR_IO_ERROR;
135         }
136
137         return PREFERENCE_ERROR_NONE;
138 }
139
140 //static int _read_data(const char *key, preference_type_e *type, char *data)
141 static int _read_data(const char *key, char *type, char *data)
142 {
143         int ret;
144         char *buf;
145         char **result;
146         int rows;
147         int columns;
148         char *errmsg;
149
150         if (key == NULL || key[0] == '\0'  || data == NULL)
151         {
152                 LOGE("INVALID_PARAMETER(0x%08x)", PREFERENCE_ERROR_INVALID_PARAMETER);
153                 return PREFERENCE_ERROR_INVALID_PARAMETER;
154         }
155
156         if (pref_db == NULL)
157         {
158                 if (_initialize() != PREFERENCE_ERROR_NONE)
159                 {
160                         LOGE("IO_ERROR(0x%08x) : fail to initialize db", PREFERENCE_ERROR_IO_ERROR);
161                         return PREFERENCE_ERROR_IO_ERROR;
162                 }
163         }
164
165         buf = sqlite3_mprintf("SELECT %s, %s, %s FROM %s WHERE %s=%Q;",
166                                                         PREF_F_KEY_NAME, PREF_F_TYPE_NAME, PREF_F_DATA_NAME, PREF_TBL_NAME, PREF_F_KEY_NAME, key);
167
168         if (buf == NULL)
169         {
170                 LOGE("IO_ERROR(0x%08x) : fail to create query string", PREFERENCE_ERROR_IO_ERROR);
171                 return PREFERENCE_ERROR_IO_ERROR;
172         }
173
174         ret = sqlite3_get_table(pref_db, buf, &result, &rows, &columns, &errmsg);
175         sqlite3_free(buf);
176         if (ret != SQLITE_OK)
177         {
178                 LOGE("IO_ERROR(0x%08x) : fail to read data (%s)", PREFERENCE_ERROR_IO_ERROR, errmsg);
179                 sqlite3_free(errmsg);
180                 return PREFERENCE_ERROR_IO_ERROR;
181         }
182
183         if (rows == 0)
184         {
185                 LOGE("NO_KEY(0x%08x) : fail to find given key(%s)", PREFERENCE_ERROR_NO_KEY, key);
186                 sqlite3_free_table(result);
187                 return PREFERENCE_ERROR_NO_KEY;
188         }
189
190         snprintf(type, 2, "%s", result[4]);                     // get type value
191         snprintf(data, BUF_LEN, "%s", result[5]);                       // get data value
192
193         sqlite3_free_table(result);
194
195         return PREFERENCE_ERROR_NONE;
196 }
197
198
199 int preference_set_int(const char *key, int value)
200 {
201         char type[2];
202         char data[BUF_LEN];
203         snprintf(type, 2, "%d", PREFERENCE_TYPE_INT);
204         snprintf(data, BUF_LEN, "%d", value);
205         return _write_data(key, type, data);
206 }
207
208 int preference_get_int(const char *key, int *value)
209 {
210         char type[2];
211         char data[BUF_LEN];
212         int ret;
213
214         ret = _read_data(key, type, data);
215         if (ret == PREFERENCE_ERROR_NONE)
216         {
217                 if (atoi(type) == PREFERENCE_TYPE_INT)
218                 {
219                         *value = atoi(data);
220                 }
221                 else
222                 {
223                         LOGE("INVALID_PARAMETER(0x%08x) : param type(%d)", PREFERENCE_ERROR_INVALID_PARAMETER, atoi(type));
224                         return PREFERENCE_ERROR_INVALID_PARAMETER;
225                 }
226         }
227
228         return ret;
229 }
230
231 int preference_set_double(const char *key, double value)
232 {
233         char type[2];
234         char data[BUF_LEN];
235         snprintf(type, 2, "%d", PREFERENCE_TYPE_DOUBLE);
236         snprintf(data, BUF_LEN, "%f", value);
237         return _write_data(key, type, data);
238 }
239
240 int preference_get_double(const char *key, double *value)
241 {
242         char type[2];
243         char data[BUF_LEN];
244
245         int ret;
246
247         ret = _read_data(key, type, data);
248         if (ret == PREFERENCE_ERROR_NONE)
249         {
250                 if (atoi(type) == PREFERENCE_TYPE_DOUBLE)
251                 {
252                         *value = atof(data);
253                 }
254                 else
255                 {
256                         LOGE("INVALID_PARAMETER(0x%08x) : param type(%d)", PREFERENCE_ERROR_INVALID_PARAMETER, atoi(type));
257                         return PREFERENCE_ERROR_INVALID_PARAMETER;
258                 }
259         }
260
261         return ret;
262 }
263
264 int preference_set_string(const char *key, const char *value)
265 {
266         char type[2];
267
268         snprintf(type, 2, "%d", PREFERENCE_TYPE_STRING);
269         if (strlen(value) > (BUF_LEN-1))
270         {
271                 LOGE("INVALID_PARAMETER(0x%08x) : param type(%d)", PREFERENCE_ERROR_INVALID_PARAMETER, atoi(type));
272                 return PREFERENCE_ERROR_INVALID_PARAMETER;
273         }
274         return _write_data(key, type, value);
275 }
276
277 int preference_get_string(const char *key, char **value)
278 {
279         char type[2];
280         char data[BUF_LEN];
281
282         int ret;
283
284         if (value == NULL)
285         {
286                 LOGE("INVALID_PARAMETER(0x%08x)", PREFERENCE_ERROR_INVALID_PARAMETER);
287                 return PREFERENCE_ERROR_INVALID_PARAMETER;
288         }
289
290         ret = _read_data(key, type, data);
291         if (ret == PREFERENCE_ERROR_NONE)
292         {
293                 if (atoi(type) == PREFERENCE_TYPE_STRING)
294                 {
295                         *value = strdup(data);
296                         if (value == NULL)
297                         {
298                                 LOGE("OUT_OF_MEMORY(0x%08x)", PREFERENCE_ERROR_OUT_OF_MEMORY);
299                                 return PREFERENCE_ERROR_OUT_OF_MEMORY;
300                         }
301                 }
302                 else
303                 {
304                         LOGE("INVALID_PARAMETER(0x%08x) : param type(%d)", PREFERENCE_ERROR_INVALID_PARAMETER, atoi(type));
305                         return PREFERENCE_ERROR_INVALID_PARAMETER;
306                 }
307         }
308
309         return ret;
310 }
311
312 int preference_set_boolean(const char *key, bool value)
313 {
314         char type[2];
315         char data[BUF_LEN];
316         snprintf(type, 2, "%d", PREFERENCE_TYPE_BOOLEAN);
317         snprintf(data, BUF_LEN, "%d", value);
318         return _write_data(key, type, data);
319 }
320
321 int preference_get_boolean(const char *key, bool *value)
322 {
323         char type[2];
324         char data[BUF_LEN];
325
326         int ret;
327
328         ret = _read_data(key, type, data);
329         if (ret == PREFERENCE_ERROR_NONE)
330         {
331                 if (atoi(type) == PREFERENCE_TYPE_BOOLEAN)
332                 {
333                         *value = (bool)atoi(data);
334                 }
335                 else
336                 {
337                         LOGE("INVALID_PARAMETER(0x%08x) : param type(%d)", PREFERENCE_ERROR_INVALID_PARAMETER, atoi(type));
338                         return PREFERENCE_ERROR_INVALID_PARAMETER;
339                 }
340         }
341
342         return ret;
343 }
344
345
346 // TODO: below operation is too heavy, let's find the light way to check.
347 int preference_is_existing(const char *key, bool *exist)
348 {
349         int ret;
350         char *buf;
351         char **result;
352         int rows;
353         int columns;
354         char *errmsg;
355
356         if (key == NULL  || key[0] == '\0'  || exist == NULL)
357         {
358                 LOGE("INVALID_PARAMETER(0x%08x)", PREFERENCE_ERROR_INVALID_PARAMETER);
359                 return PREFERENCE_ERROR_INVALID_PARAMETER;
360         }
361
362         if (pref_db == NULL)
363         {
364                 if (_initialize() != PREFERENCE_ERROR_NONE)
365                 {
366                         LOGE("IO_ERROR(0x%08x) : fail to initialize db", PREFERENCE_ERROR_IO_ERROR);
367                         return PREFERENCE_ERROR_IO_ERROR;
368                 }
369         }
370
371         /* check data is exist */
372         buf = sqlite3_mprintf("SELECT %s FROM %s WHERE %s=%Q;", PREF_F_KEY_NAME, PREF_TBL_NAME, PREF_F_KEY_NAME, key);
373
374         if (buf == NULL)
375         {
376                 LOGE("IO_ERROR(0x%08x) : fail to create query string", PREFERENCE_ERROR_IO_ERROR);
377                 return PREFERENCE_ERROR_IO_ERROR;
378         }
379
380         ret = sqlite3_get_table(pref_db, buf, &result, &rows, &columns, &errmsg);
381         sqlite3_free(buf);
382         if (ret != SQLITE_OK)
383         {
384                 LOGE("IO_ERROR(0x%08x) : fail to read data(%s)", PREFERENCE_ERROR_IO_ERROR, errmsg);
385                 sqlite3_free(errmsg);
386                 return PREFERENCE_ERROR_IO_ERROR;
387         }
388
389         if (rows > 0)
390         {
391                 *exist = true;
392         }
393         else
394         {
395                 *exist = false;
396         }
397
398         sqlite3_free_table(result);
399         return PREFERENCE_ERROR_NONE;
400 }
401
402 static pref_changed_cb_node_t* _find_node(const char *key)
403 {
404         pref_changed_cb_node_t *tmp_node;
405
406         if (key == NULL || key[0] == '\0' )
407         {
408                 return NULL;
409         }
410
411         tmp_node = head;
412
413         while (tmp_node)
414         {
415                 if (strcmp(tmp_node->key, key) == 0)
416                 {
417                         break;
418                 }
419                 tmp_node = tmp_node->next;
420         }
421
422         return tmp_node;
423 }
424
425
426 static int _add_node(const char *key, preference_changed_cb cb, void *user_data)
427 {
428         pref_changed_cb_node_t *tmp_node;
429
430         if (key == NULL  || key[0] == '\0'  || cb == NULL)
431         {
432                 LOGE("INVALID_PARAMETER(0x%08x)", PREFERENCE_ERROR_INVALID_PARAMETER);
433                 return PREFERENCE_ERROR_INVALID_PARAMETER;
434         }
435
436         tmp_node = _find_node(key);
437
438         if (tmp_node != NULL)
439         {
440                 tmp_node->cb = cb;
441                 tmp_node->user_data = user_data;
442         }
443         else
444         {
445                 tmp_node = (pref_changed_cb_node_t*)malloc(sizeof(pref_changed_cb_node_t));
446                 if (tmp_node == NULL)
447                 {
448                         LOGE("OUT_OF_MEMORY(0x%08x)", PREFERENCE_ERROR_OUT_OF_MEMORY);
449                         return PREFERENCE_ERROR_OUT_OF_MEMORY;
450                 }
451
452                 tmp_node->key = strdup(key);
453                 if (tmp_node->key == NULL)
454                 {
455                         free(tmp_node);
456                         LOGE("OUT_OF_MEMORY(0x%08x)", PREFERENCE_ERROR_OUT_OF_MEMORY);
457                         return PREFERENCE_ERROR_OUT_OF_MEMORY;
458                 }
459                 tmp_node->cb = cb;
460                 tmp_node->user_data = user_data;
461                 tmp_node->prev = NULL;
462                 tmp_node->next = head;
463                 head = tmp_node;
464         }
465
466         return PREFERENCE_ERROR_NONE;
467 }
468
469 static int _remove_node(const char *key)
470 {
471         pref_changed_cb_node_t *tmp_node;
472
473         if (key == NULL || key[0] == '\0' )
474         {
475                 LOGE("INVALID_PARAMETER(0x%08x)", PREFERENCE_ERROR_INVALID_PARAMETER);
476                 return PREFERENCE_ERROR_INVALID_PARAMETER;
477         }
478
479         tmp_node = _find_node(key);
480
481         if (tmp_node == NULL)
482         {
483                 return PREFERENCE_ERROR_NONE;
484         }
485
486         if (tmp_node->prev != NULL)
487         {
488                 tmp_node->prev->next = tmp_node->next;
489         }
490         else
491         {
492                 head = tmp_node->next;
493         }
494
495         if (tmp_node->next != NULL)
496         {
497                 tmp_node->next->prev = tmp_node->prev;
498         }
499
500         if (tmp_node->key)
501         {
502                 free(tmp_node->key);
503         }
504
505         free(tmp_node);
506
507         return PREFERENCE_ERROR_NONE;
508 }
509
510
511 static void _remove_all_node(void)
512 {
513         pref_changed_cb_node_t *tmp_node;
514
515         while (head)
516         {
517                 tmp_node = head;
518                 head = tmp_node->next;
519
520                 if (tmp_node->key)
521                 {
522                         free(tmp_node->key);
523                 }
524
525                 free(tmp_node);
526         }
527 }
528
529
530 static void _update_cb(void *data, int action, char const *db_name, char const *table_name, sqlite_int64 rowid)
531 {
532         int ret;
533         char *buf;
534         char **result;
535         int rows;
536         int columns;
537         char *errmsg;
538         pref_changed_cb_node_t *tmp_node;
539
540         // skip INSERT/DELETE event
541         if (action != SQLITE_UPDATE)
542         {
543                 return;
544         }
545
546         if (strcmp(table_name, PREF_TBL_NAME) != 0)
547         {
548                 SECURE_LOGE("given table name (%s) is not same", table_name);
549                 return;
550         }
551
552         buf = sqlite3_mprintf("SELECT %s FROM %s WHERE rowid='%lld';", PREF_F_KEY_NAME, PREF_TBL_NAME, rowid);
553         if (buf == NULL)
554         {
555                 return;
556         }
557         ret = sqlite3_get_table(pref_db, buf, &result, &rows, &columns, &errmsg);
558         sqlite3_free(buf);
559         if (ret != SQLITE_OK)
560         {
561                 LOGI("fail to read data(%s)", errmsg);
562                 sqlite3_free(errmsg);
563                 return;
564         }
565
566         if (rows == 0)
567         {
568                 sqlite3_free_table(result);
569                 return;
570         }
571
572         tmp_node = _find_node(result[1]);
573
574         if (tmp_node != NULL && tmp_node->cb != NULL)
575         {
576                 tmp_node->cb(result[1], tmp_node->user_data);
577         }
578
579         sqlite3_free_table(result);
580 }
581
582
583 int preference_remove(const char *key)
584 {
585         int ret;
586         char *buf;
587         char *errmsg;
588         bool exist;
589
590         ret = preference_is_existing(key, &exist);
591         if (ret != PREFERENCE_ERROR_NONE)
592         {
593                 return ret;
594         }
595
596         if (!exist)
597         {
598                 return PREFERENCE_ERROR_NONE;
599         }
600
601         /* insert data or update data if data already exist */
602         buf = sqlite3_mprintf("DELETE FROM %s WHERE %s = '%s';",
603                                                         PREF_TBL_NAME, PREF_F_KEY_NAME, key);
604
605         if (buf == NULL)
606         {
607                 LOGE("IO_ERROR(0x%08x) : fail to create query string", PREFERENCE_ERROR_IO_ERROR);
608                 return PREFERENCE_ERROR_IO_ERROR;
609         }
610
611         ret = sqlite3_exec(pref_db, buf, NULL, NULL, &errmsg);
612         sqlite3_free(buf);
613         if (ret != SQLITE_OK)
614         {
615                 LOGE("IO_ERROR(0x%08x) : fail to delete data (%s)", PREFERENCE_ERROR_IO_ERROR, errmsg);
616                 sqlite3_free(errmsg);
617                 return PREFERENCE_ERROR_IO_ERROR;
618         }
619
620         // if exist, remove changed cb
621          _remove_node(key);
622
623         return PREFERENCE_ERROR_NONE;
624 }
625
626
627 int preference_remove_all(void)
628 {
629         int ret;
630         char *buf;
631         char *errmsg;
632
633         if (pref_db == NULL)
634         {
635                 if (_initialize() != PREFERENCE_ERROR_NONE)
636                 {
637                         LOGE("IO_ERROR(0x%08x) : fail to initialize db", PREFERENCE_ERROR_IO_ERROR);
638                         return PREFERENCE_ERROR_IO_ERROR;
639                 }
640         }
641
642         /* insert data or update data if data already exist */
643         buf = sqlite3_mprintf("DELETE FROM %s;", PREF_TBL_NAME);
644         if (buf == NULL)
645         {
646                 LOGE("IO_ERROR(0x%08x) : fail to create query string", PREFERENCE_ERROR_IO_ERROR);
647                 return PREFERENCE_ERROR_IO_ERROR;
648         }
649
650         ret = sqlite3_exec(pref_db, buf, NULL, NULL, &errmsg);
651         sqlite3_free(buf);
652         if (ret != SQLITE_OK)
653         {
654                 LOGE("IO_ERROR(0x%08x) : fail to delete data (%s)", PREFERENCE_ERROR_IO_ERROR, errmsg);
655                 sqlite3_free(errmsg);
656                 return PREFERENCE_ERROR_IO_ERROR;
657         }
658
659         // if exist, remove changed cb
660         _remove_all_node();
661
662         return PREFERENCE_ERROR_NONE;
663 }
664
665
666 int preference_set_changed_cb(const char *key, preference_changed_cb callback, void *user_data)
667 {
668         int ret;
669         bool exist;
670
671         ret = preference_is_existing(key, &exist);
672         if (ret != PREFERENCE_ERROR_NONE)
673         {
674                 return ret;
675         }
676
677         if (!exist)
678         {
679                 LOGE("NO_KEY(0x%08x) : fail to find given key(%s)", PREFERENCE_ERROR_NO_KEY, key);
680                 return PREFERENCE_ERROR_NO_KEY;
681         }
682
683         if (!is_update_hook_registered)
684         {
685                 sqlite3_update_hook(pref_db, _update_cb, NULL);
686                 is_update_hook_registered = true;
687         }
688
689         return _add_node(key, callback, user_data);
690 }
691
692 int preference_unset_changed_cb(const char *key)
693 {
694         if (pref_db == NULL)
695         {
696                 if (_initialize() != PREFERENCE_ERROR_NONE)
697                 {
698                         return PREFERENCE_ERROR_IO_ERROR;
699                 }
700         }
701
702         return _remove_node(key);
703 }
704
705 int preference_foreach_item(preference_item_cb callback, void *user_data)
706 {
707         int ret;
708         char *buf;
709         char **result;
710         int rows;
711         int columns;
712         char *errmsg;
713         int i;
714
715         if (pref_db == NULL)
716         {
717                 if (_initialize() != PREFERENCE_ERROR_NONE)
718                 {
719                         LOGE("IO_ERROR(0x%08x) : fail to initialize db", PREFERENCE_ERROR_IO_ERROR);
720                         return PREFERENCE_ERROR_IO_ERROR;
721                 }
722         }
723
724         if (callback == NULL)
725         {
726                 LOGE("INVALID_PARAMETER(0x%08x)", PREFERENCE_ERROR_INVALID_PARAMETER);
727                 return PREFERENCE_ERROR_INVALID_PARAMETER;
728         }
729
730         buf = sqlite3_mprintf("SELECT %s FROM %s;", PREF_F_KEY_NAME, PREF_TBL_NAME);
731         if (buf == NULL)
732         {
733                 LOGE("IO_ERROR(0x%08x) : fail to create query string", PREFERENCE_ERROR_IO_ERROR);
734                 return PREFERENCE_ERROR_IO_ERROR;
735         }
736
737         ret = sqlite3_get_table(pref_db, buf, &result, &rows, &columns, &errmsg);
738         sqlite3_free(buf);
739         if (ret != SQLITE_OK)
740         {
741                 LOGE("IO_ERROR(0x%08x) : fail to read data (%s)", PREFERENCE_ERROR_IO_ERROR, errmsg);
742                 sqlite3_free(errmsg);
743                 return PREFERENCE_ERROR_IO_ERROR;
744         }
745
746         for (i = 1; i <= rows; i++)
747         {
748                 if (callback(result[i], user_data) != true)
749                 {
750                         break;
751                 }
752         }
753
754         sqlite3_free_table(result);
755
756         return PREFERENCE_ERROR_NONE;
757 }
758