Patch for selecting limited records of messages
[apps/native/telegram-tizen.git] / TelegramTizen / db_manager / tg_db_manager.c
1 /**
2  * @file tg_db_manager.c
3  * @date Jun 1, 2015
4  * @auhtor sandeep
5  */
6
7 #include "tg_db_manager.h"
8 #include <malloc.h>
9 #include <errno.h>
10 #include <string.h>
11 #include "tg_common.h"
12
13 int errno;
14
15 static struct _info {
16         sqlite3 *db;
17         const char *database_name;
18 } s_info = {
19                 .db = NULL,
20                 .database_name = DEFAULT_TG_DATABASE_PATH,
21 };
22
23
24 sqlite3* create_database(char* database_name)
25 {
26         if(!database_name)
27                 return NULL;
28
29         int ret;
30         sqlite3 *db;
31         ret = sqlite3_open(database_name, &db);
32         //ret = sqlite3_open_v2(database_name, &db, SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE, NULL);
33         if(ret) {
34                 return NULL;
35         }
36         return db;
37 }
38
39 Eina_Bool close_database(sqlite3* db)
40 {
41         if(db) {
42                 sqlite3_close(db);
43                 return EINA_TRUE;
44         }
45         return EINA_FALSE;
46 }
47
48 Eina_Bool create_table(const char* table_name, Eina_List* column_names, Eina_List* column_types)
49 {
50         if(!table_name || ! column_names || !column_types) {
51                 return EINA_FALSE;
52         }
53         sqlite3* db = create_database(DEFAULT_TG_DATABASE_PATH);
54         int ret = 0 ;
55         char* err_msg = 0;
56         int col_count = eina_list_count(column_names);
57
58         int str_len = strlen("CREATE TABLE IF NOT EXISTS ") + strlen(table_name) + strlen("(") + 1;
59         char* var_query = (char*)malloc(str_len);
60         strcpy(var_query,"CREATE TABLE IF NOT EXISTS ");
61         strcat(var_query, table_name);
62         strcat(var_query, "(");
63
64         for(int col = 0; col < col_count ; col++) {
65                 char* col_name = eina_list_nth(column_names, col);
66                 var_query = realloc(var_query, strlen(var_query)+strlen(col_name) + 1);
67                 strcat(var_query, col_name);
68                 var_query = realloc(var_query, strlen(var_query)+strlen(" ") + 1);
69                 strcat(var_query, " ");
70                 char* col_type = eina_list_nth(column_types, col);
71                 var_query = realloc(var_query, strlen(var_query)+strlen(col_type) + 1);
72                 strcat(var_query, col_type);
73                 if(col < col_count - 1){
74                         var_query = realloc(var_query, strlen(var_query) + 2);
75                         strcat(var_query, ",");
76                 } else {
77                         var_query = realloc(var_query, strlen(var_query) + 3);
78                         strcat(var_query, ");");
79                 }
80         }
81
82         ret = sqlite3_exec(db,var_query, NULL, NULL, &err_msg);
83         free(var_query);
84         close_database(db);
85         if( ret != SQLITE_OK ){
86                 sqlite3_free(err_msg);
87                 return EINA_FALSE;
88         }
89         return EINA_TRUE;
90 }
91
92 Eina_Bool insert_table(const char* table_name, Eina_List* column_names, Eina_List* column_types, Eina_List* column_values)
93 {
94         if( !table_name || ! column_names || !column_types || !column_values) {
95                 return EINA_FALSE;
96         }
97         sqlite3* db = create_database(DEFAULT_TG_DATABASE_PATH);
98         int ret = 0 ;
99         char* err_msg = 0;
100         int col_count = eina_list_count(column_names);
101
102         int str_len = strlen("INSERT INTO ") + strlen(table_name) + strlen("(") + 1;
103         char* var_query = (char*)malloc(str_len);
104         strcpy(var_query,"INSERT INTO ");
105         strcat(var_query, table_name);
106         strcat(var_query, "(");
107
108         for(int col = 0; col < col_count ; col++) {
109
110                 char* col_name = eina_list_nth(column_names, col);
111                 var_query = realloc(var_query, strlen(var_query)+strlen(col_name) + 1);
112                 strcat(var_query, col_name);
113
114                 if(col < col_count - 1){
115                         var_query = realloc(var_query, strlen(var_query) + 2);
116                         strcat(var_query, ",");
117                 } else {
118                         var_query = realloc(var_query, strlen(var_query) + strlen(") VALUES (") + 1);
119                         strcat(var_query, ") VALUES (");
120                 }
121         }
122
123         int col_val_count = eina_list_count(column_values);
124
125         for(int col = 0; col < col_val_count ; col++) {
126
127                 char* col_type = eina_list_nth(column_types, col);
128                 char* col_value = NULL;
129                 if(!strcmp(col_type, "INTEGER") || !strcmp(col_type, "INTEGER PRIMARY KEY NOT NULL")) {
130                         int* tmp_value = eina_list_nth(column_values, col);
131
132 #if 0
133                         long long val = *tmp_value;
134                         col_value = (char*)malloc(50);
135                         //sprintf(col_value, "%lld", *tmp_value);
136                         sprintf(col_value, "%lld", val);
137 #endif
138
139                         int act_val = (*tmp_value);
140                         col_value = (char*)malloc(50);
141                         sprintf(col_value, "%d", act_val);
142
143                         var_query = realloc(var_query, strlen(var_query)+strlen(col_value) + 1);
144                         strcat(var_query, col_value);
145
146                 } else if(!strcmp(col_type, "TEXT") || !strcmp(col_type, "TEXT PRIMARY KEY NOT NULL")) {
147                         char* tmp_value = eina_list_nth(column_values, col);
148                         col_value = (char*)malloc(strlen(tmp_value) + 1);
149                         strcpy(col_value,tmp_value);
150
151                         var_query = realloc(var_query, strlen(var_query)+strlen("'") + 1);
152                         strcat(var_query, "'");
153
154                         var_query = realloc(var_query, strlen(var_query)+strlen(col_value) + 1);
155                         strcat(var_query, col_value);
156
157                         var_query = realloc(var_query, strlen(var_query)+strlen("'") + 1);
158                         strcat(var_query, "'");
159                 }
160
161                 if(col < col_count - 1){
162                         var_query = realloc(var_query, strlen(var_query) + 3);
163                         strcat(var_query, ", ");
164                 } else {
165                         var_query = realloc(var_query, strlen(var_query) + 3);
166                         strcat(var_query, ");");
167                 }
168                 free(col_value);
169         }
170
171         ret = sqlite3_exec(db,var_query, NULL, NULL, &err_msg);
172         close_database(db);
173         if( ret != SQLITE_OK ){
174                 sqlite3_free(err_msg);
175                 return EINA_FALSE;
176         }
177         return EINA_TRUE;
178
179 }
180
181
182 Eina_Bool update_table(const char* table_name, Eina_List* column_names, Eina_List* column_types, Eina_List* column_values, const char* where_clause)
183 {
184         if(!table_name || ! column_names || !column_types || !column_values) {
185                 return EINA_FALSE;
186         }
187         sqlite3* db = create_database(DEFAULT_TG_DATABASE_PATH);
188         int ret = 0 ;
189         char* err_msg = 0;
190         int col_count = eina_list_count(column_names);
191
192         int str_len = strlen("UPDATE ") + strlen(table_name) + strlen(" SET ") + 1;
193         char* var_query = (char*)malloc(str_len);
194         strcpy(var_query,"UPDATE ");
195         strcat(var_query, table_name);
196         strcat(var_query, " SET ");
197
198
199         int col_val_count = eina_list_count(column_values);
200
201         for(int col = 0; col < col_val_count ; col++) {
202                 char* col_name = eina_list_nth(column_names, col);
203                 char* col_type = eina_list_nth(column_types, col);
204
205                 char* col_value = NULL;
206                 if(!strcmp(col_type, "INTEGER") || !strcmp(col_type, "INTEGER PRIMARY KEY NOT NULL")) {
207                         int* tmp_value = eina_list_nth(column_values, col);
208
209                         int act_val = (*tmp_value);
210                         col_value = (char*)malloc(50);
211                         sprintf(col_value, "%d", act_val);
212
213                         var_query = realloc(var_query, strlen(var_query) + strlen(col_name) + strlen(" = ") + strlen(col_value) + 1);
214                         strcat(var_query, col_name);
215                         strcat(var_query, " = ");
216                         strcat(var_query, col_value);
217
218                 } else if(!strcmp(col_type, "TEXT") || !strcmp(col_type, "TEXT PRIMARY KEY NOT NULL")) {
219
220                         char* tmp_value = eina_list_nth(column_values, col);
221                         col_value = (char*)malloc(strlen(tmp_value) + 1);
222                         strcpy(col_value,tmp_value);
223
224                         var_query = realloc(var_query, strlen(var_query)+ strlen(col_name) + strlen(" = ")+strlen("'") + 1);
225                         strcat(var_query, col_name);
226                         strcat(var_query, " = ");
227                         strcat(var_query, "'");
228
229                         var_query = realloc(var_query, strlen(var_query)+strlen(col_value) + 1);
230                         strcat(var_query, col_value);
231
232                         var_query = realloc(var_query, strlen(var_query)+strlen("'") + 1);
233                         strcat(var_query, "'");
234                 }
235
236                 if(col < col_count - 1){
237                         var_query = realloc(var_query, strlen(var_query) + 3);
238                         strcat(var_query, ", ");
239                 } else {
240                         var_query = realloc(var_query, strlen(var_query) + 2);
241                         strcat(var_query, " ");
242                 }
243                 free(col_value);
244         }
245
246         var_query = realloc(var_query, strlen(var_query) + strlen(" WHERE ") + strlen(where_clause) + 2);
247         strcat(var_query, " WHERE ");
248         strcat(var_query, where_clause);
249         strcat(var_query, ";");
250
251         ret = sqlite3_exec(db, var_query, NULL, NULL, &err_msg);
252
253         close_database(db);
254
255         if( ret != SQLITE_OK ){
256                 sqlite3_free(err_msg);
257                 return EINA_FALSE;
258         }
259         return EINA_TRUE;
260
261 }
262
263 Eina_Bool get_values_from_table(const char* table_name, Eina_List* column_names, int (*callback)(void*,int,char**,char**), const char* where_clause, void* data_to_callback)
264 {
265         if (!table_name) {
266                 return EINA_FALSE;
267         }
268         sqlite3* db = create_database(DEFAULT_TG_DATABASE_PATH);
269         /*****No rows identification*****/
270
271         char* row_cnt_qry = (char*)malloc(strlen("SELECT COUNT(*) FROM ") + strlen(table_name) + strlen(";") +1);
272         strcpy(row_cnt_qry, "SELECT COUNT(*) FROM ");
273         strcat(row_cnt_qry, table_name);
274         strcat(row_cnt_qry, ";");
275
276         int no_of_rows = 0;
277         //ret = sqlite3_exec(s_info.db,var_query, callback,(void*)s_info.db, &err_msg);
278
279         sqlite3_stmt *stmt;
280         if (sqlite3_prepare_v2(db, row_cnt_qry, -1, &stmt, NULL) == SQLITE_OK) {
281                 if (sqlite3_step(stmt) == SQLITE_ERROR) {
282                         no_of_rows = 0;
283                 } else {
284                         no_of_rows = sqlite3_column_int(stmt, 0);
285                 }
286                 sqlite3_finalize(stmt);
287         }
288         close_database(db);
289         free(row_cnt_qry);
290         if(no_of_rows <= 0) {
291                 return EINA_FALSE;
292         }
293
294         /********************************/
295         db = create_database(DEFAULT_TG_DATABASE_PATH);
296         int ret = 0 ;
297         char* err_msg = 0;
298         //int col_count = eina_list_count(column_names);
299
300         int str_len = strlen("SELECT ") + 1;
301         char* var_query = (char*)malloc(str_len);
302         strcpy(var_query,"SELECT ");
303         if(!column_names) {
304                 var_query = realloc(var_query, strlen(var_query) + 3);
305                 strcat(var_query, "* ");
306         } else {
307
308                 int col_count = eina_list_count(column_names);
309                 char* col_name = NULL;
310                 for(int col = 0; col < col_count ; col++) {
311                         col_name = eina_list_nth(column_names, col);
312                         var_query = realloc(var_query, strlen(var_query)+strlen(col_name) + 1);
313                         strcat(var_query, col_name);
314                         col_name = NULL;
315                         var_query = realloc(var_query, strlen(var_query)+strlen(" ") + 1);
316
317                         if(col < col_count - 1){
318                                 var_query = realloc(var_query, strlen(var_query) + 3);
319                                 strcat(var_query, ", ");
320                         } else {
321                                 var_query = realloc(var_query, strlen(var_query) + 2);
322                                 strcat(var_query, " ");
323                         }
324                 }
325
326         }
327
328         var_query = realloc(var_query, strlen(var_query) + strlen("FROM ") + 2);
329         strcat(var_query, "FROM ");
330         var_query = realloc(var_query, strlen(var_query) + strlen(table_name) + 1);
331         strcat(var_query, table_name);
332
333         if (where_clause) {
334                 var_query = realloc(var_query, strlen(var_query)+strlen(" WHERE ") + 1);
335                 strcat(var_query, " WHERE ");
336                 var_query = realloc(var_query, strlen(var_query)+strlen(where_clause) + 1);
337                 strcat(var_query, where_clause);
338         }
339
340         var_query = realloc(var_query, strlen(var_query) + 2);
341         strcat(var_query, ";");
342
343
344         ret = sqlite3_exec(db,var_query, callback,(void*)data_to_callback, &err_msg);
345         close_database(db);
346         if( ret != SQLITE_OK ){
347                 sqlite3_free(err_msg);
348                 return EINA_FALSE;
349         }
350
351         free(var_query);
352         return EINA_TRUE;
353 }
354
355 int get_number_of_rows(char* table_name, char* where_clause)
356 {
357         int no_of_rows = 0;
358         if (!table_name) {
359                 return no_of_rows;
360         }
361         sqlite3* db = create_database(DEFAULT_TG_DATABASE_PATH);
362         char* row_cnt_qry = (char*)malloc(strlen("SELECT COUNT(*) FROM ") + strlen(table_name) + 1);
363         strcpy(row_cnt_qry, "SELECT COUNT(*) FROM ");
364         strcat(row_cnt_qry, table_name);
365
366         if (where_clause) {
367                 row_cnt_qry = realloc(row_cnt_qry, strlen(row_cnt_qry)+strlen(" WHERE ") + 1);
368                 strcat(row_cnt_qry, " WHERE ");
369                 row_cnt_qry = realloc(row_cnt_qry, strlen(row_cnt_qry)+strlen(where_clause) + 1);
370                 strcat(row_cnt_qry, where_clause);
371         }
372         row_cnt_qry = realloc(row_cnt_qry, strlen(row_cnt_qry) + 2);
373         strcat(row_cnt_qry, ";");
374
375         sqlite3_stmt *stmt;
376         if (sqlite3_prepare_v2(db, row_cnt_qry, -1, &stmt, NULL) == SQLITE_OK) {
377                 if (sqlite3_step(stmt) == SQLITE_ERROR) {
378                         no_of_rows = 0;
379                 } else {
380                         no_of_rows = sqlite3_column_int(stmt, 0);
381                 }
382                 sqlite3_finalize(stmt);
383         }
384         close_database(db);
385         free(row_cnt_qry);
386         return no_of_rows;
387 }
388
389 Eina_List* get_values_from_table_sync_order_by(const char* table_name, Eina_List* column_names, Eina_List* column_types, const char* order_column, Eina_Bool is_asc, const char* where_clause)
390 {
391         Eina_List* query_vals = NULL;
392
393         if (!table_name) {
394                 return NULL;
395         }
396         sqlite3* db = create_database(DEFAULT_TG_DATABASE_PATH);
397         /*****No rows identification*****/
398
399         char* row_cnt_qry = (char*)malloc(strlen("SELECT COUNT(*) FROM ") + strlen(table_name) + strlen(";") +1);
400         strcpy(row_cnt_qry, "SELECT COUNT(*) FROM ");
401         strcat(row_cnt_qry, table_name);
402         strcat(row_cnt_qry, ";");
403
404         int no_of_rows = 0;
405         //ret = sqlite3_exec(s_info.db,var_query, callback,(void*)s_info.db, &err_msg);
406
407         sqlite3_stmt *stmt;
408         if (sqlite3_prepare_v2(db, row_cnt_qry, -1, &stmt, NULL) == SQLITE_OK) {
409                 if (sqlite3_step(stmt) == SQLITE_ERROR) {
410                         no_of_rows = 0;
411                 } else {
412                         no_of_rows = sqlite3_column_int(stmt, 0);
413                 }
414                 sqlite3_finalize(stmt);
415         }
416         close_database(db);
417         free(row_cnt_qry);
418         if(no_of_rows <= 0) {
419                 return NULL;
420         }
421
422         /********************************/
423         db = create_database(DEFAULT_TG_DATABASE_PATH);
424         int ret = 0 ;
425         char* err_msg = 0;
426         //int col_count = eina_list_count(column_names);
427
428         int str_len = strlen("SELECT ") + 1;
429         char* var_query = (char*)malloc(str_len);
430         strcpy(var_query,"SELECT ");
431         if(!column_names) {
432                 var_query = realloc(var_query, strlen(var_query) + 3);
433                 strcat(var_query, "* ");
434         } else {
435
436                 int col_count = eina_list_count(column_names);
437                 char* col_name = NULL;
438                 for(int col = 0; col < col_count ; col++) {
439                         col_name = eina_list_nth(column_names, col);
440                         var_query = realloc(var_query, strlen(var_query)+strlen(col_name) + 1);
441                         strcat(var_query, col_name);
442                         col_name = NULL;
443                         var_query = realloc(var_query, strlen(var_query)+strlen(" ") + 1);
444
445                         if(col < col_count - 1){
446                                 var_query = realloc(var_query, strlen(var_query) + 3);
447                                 strcat(var_query, ", ");
448                         } else {
449                                 var_query = realloc(var_query, strlen(var_query) + 2);
450                                 strcat(var_query, " ");
451                         }
452                 }
453
454         }
455
456         var_query = realloc(var_query, strlen(var_query) + strlen("FROM ") + 2);
457         strcat(var_query, "FROM ");
458         var_query = realloc(var_query, strlen(var_query) + strlen(table_name) + 1);
459         strcat(var_query, table_name);
460
461         if (where_clause) {
462                 var_query = realloc(var_query, strlen(var_query)+strlen(" WHERE ") + 1);
463                 strcat(var_query, " WHERE ");
464                 var_query = realloc(var_query, strlen(var_query)+strlen(where_clause) + 1);
465                 strcat(var_query, where_clause);
466         }
467
468         if (order_column) {
469                 var_query = realloc(var_query, strlen(var_query)+strlen(" ORDER BY ") + 1);
470                 strcat(var_query, " ORDER BY ");
471                 var_query = realloc(var_query, strlen(var_query)+strlen(order_column) + 1);
472                 strcat(var_query, order_column);
473                 if (is_asc) {
474                         var_query = realloc(var_query, strlen(var_query)+strlen(" COLLATE NOCASE ASC ") + 1);
475                         strcat(var_query, " COLLATE NOCASE ASC ");
476                 } else {
477                         var_query = realloc(var_query, strlen(var_query)+strlen(" COLLATE NOCASE DESC ") + 1);
478                         strcat(var_query, " COLLATE NOCASE DESC ");
479                 }
480         }
481
482
483
484         var_query = realloc(var_query, strlen(var_query) + 2);
485         strcat(var_query, ";");
486
487
488         ret = sqlite3_prepare_v2(db, var_query, -1, &stmt, 0);
489         if( ret != SQLITE_OK ){
490                 sqlite3_free(err_msg);
491                 return NULL;
492         }
493
494         while(sqlite3_step(stmt) == SQLITE_ROW) {
495                 int col_val_count = eina_list_count(column_names);
496                 Eina_List* row_vals = NULL;
497                 for(int col = 0; col < col_val_count ; col++) {
498                         char* col_type = eina_list_nth(column_types, col);
499                         if(!strcmp(col_type, "INTEGER") || !strcmp(col_type, "INTEGER PRIMARY KEY NOT NULL")) {
500                                 int temp = sqlite3_column_int64(stmt, col);
501                                 int* val_int = (int*)malloc(sizeof(int));
502                                 *val_int = temp;
503                                 row_vals = eina_list_append(row_vals, val_int);
504                         } else if(!strcmp(col_type, "TEXT") || !strcmp(col_type, "TEXT PRIMARY KEY NOT NULL")) {
505                                 char *temp_val = (char*)sqlite3_column_text(stmt, col);
506                                 char *val_text = NULL;
507                                 if (temp_val) {
508                                         val_text = strdup(temp_val);
509                                 } else {
510                                         val_text = strdup("");
511                                 }
512                                 row_vals = eina_list_append(row_vals, val_text);
513                         }
514                 }
515                 query_vals = eina_list_append(query_vals, row_vals);
516         }
517     close_database(db);
518         free(var_query);
519         return query_vals;
520
521 }
522
523 Eina_List* get_values_from_table_sync(const char* table_name, Eina_List* column_names, Eina_List* column_types, const char* wc, int limit, int offset)
524 {
525         sqlite3_stmt *stmt;
526         const char *name;
527         int query_len;
528         Eina_List *l;
529         Eina_List *result = NULL;
530         Eina_List *record;
531         char *query;
532         char *ptr;
533         int ret;
534         const char *type;
535         int col;
536         sqlite3* db = create_database(DEFAULT_TG_DATABASE_PATH);
537         char* where_clause = NULL;
538
539         if (wc) {
540                 where_clause = (char*)malloc(strlen("WHERE ") + strlen(wc) + 1);
541                 strcpy(where_clause, "WHERE ");
542                 strcat(where_clause, wc);
543         }
544
545
546         if (!table_name || !column_names) {
547                 return NULL;
548         }
549
550         query_len = strlen("SELECT ");
551         EINA_LIST_FOREACH(column_names, l, name) {
552                 query_len += strlen(name) + 2; /* ", " */
553         }
554
555         query_len += strlen(" FROM ");
556         query_len += strlen(table_name) + 1;
557         query_len += (where_clause ? strlen(where_clause) : 0);
558         query_len += 2;
559
560         query = malloc(query_len);
561         if (!query) {
562                 return NULL;
563         }
564
565         strcpy(query, "SELECT ");
566         ptr = query +strlen("SELECT ");
567
568         if (column_names) {
569                 EINA_LIST_FOREACH(column_names, l, name) {
570                         ptr += sprintf(ptr, "%s, ", name);
571                 }
572                 ptr -= 2; /* Delete last two characters, ", " */
573         } else {
574                 strcpy(ptr, "*");
575                 ptr++;
576         }
577
578         if (limit == -1 || offset == -1) {
579                 ptr += sprintf(ptr, " FROM %s %s;", table_name, where_clause ? where_clause : "");
580         } else {
581                 ptr += sprintf(ptr, " FROM %s %s LIMIT %d OFFSET %d;", table_name, where_clause ? where_clause : "", limit, offset);
582         }
583
584
585         LOGD("Query: %s", query);
586
587         ret = sqlite3_prepare_v2(db, query, -1, &stmt, NULL);
588         free(query);
589         if (ret != SQLITE_OK) {
590                 close_database(db);
591                 return NULL;
592         }
593
594
595         while(sqlite3_step(stmt) == SQLITE_ROW) {
596                 col = 0;
597                 record = NULL;
598                 EINA_LIST_FOREACH(column_types, l, type) {
599                         if (!type) {
600                                 /**
601                                  * @note
602                                  * Not possible
603                                  */
604                                 continue;
605                         }
606
607                         if (!strncmp(type, "INTEGER", strlen("INTEGER"))) {
608                                 int temp;
609                                 int *val;
610
611                                 temp = sqlite3_column_int64(stmt, col);
612                                 val = malloc(sizeof(int));
613                                 if (!val) {
614                                         /**
615                                          * @todo
616                                          * Handling exceptional cases.
617                                          */
618                                 }
619                                 *val = temp;
620
621                                 record = eina_list_append(record, val);
622                         } else if (!strncmp(type, "TEXT", strlen("TEXT"))) {
623                                 char *val;
624                                 const char *text;
625
626                                 text = (const char *)sqlite3_column_text(stmt, col);
627                                 if (!text || text[0] == '\0') {
628                                         //val = strdup("_null_");
629                                         val = strdup("");
630                                 } else {
631                                         val = strdup(text);
632                                 }
633                                 if (!val) {
634                                         /**
635                                          * @todo
636                                          * Handling exceptional cases
637                                          */
638                                 } else {
639                                         record = eina_list_append(record, val);
640                                 }
641                         }
642                         col++;
643                 }
644                 result = eina_list_append(result, record);
645         }
646
647         sqlite3_finalize(stmt);
648
649         if (where_clause) {
650                 free(where_clause);
651                 where_clause = NULL;
652         }
653         close_database(db);
654         return result;
655 }
656
657
658 int tg_db_init(void)
659 {
660 #if 0
661         int ret;
662
663         if (s_info.db) {
664                 LOGD("Already initialized");
665                 return EINA_TRUE;
666         }
667
668         ret = sqlite3_open(s_info.database_name, &s_info.db);
669         if(ret != SQLITE_OK) {
670                 return EINA_FALSE;
671         }
672 #endif
673         return EINA_TRUE;
674
675 }
676
677 int tg_db_fini(void)
678 {
679 #if 0
680         if (!s_info.db) {
681                 return EINA_FALSE;
682         }
683
684         sqlite3_close(s_info.db);
685         s_info.db = NULL;
686 #endif
687         return EINA_TRUE;
688 }
689
690
691 Eina_List *tg_db_get_user_info(tgl_peer_id_t *user_id)
692 {
693         user_data_s *info;
694         Eina_List *result;
695         sqlite3_stmt *stmt;
696         int ret;
697         const char *val_text;
698         sqlite3* db = create_database(DEFAULT_TG_DATABASE_PATH);
699         ret = sqlite3_prepare_v2(db, "SELECT " \
700                                 "print_name, structure_version, " \
701                                 "photo_path, photo_id, " \
702                                 "first_name, last_name, " \
703                                 "phone, access_hash, " \
704                                 "real_first_name, real_last_name, " \
705                                 "username, online, last_seen " \
706                                 "FROM user_info_table WHERE user_id = ?", -1, &stmt, NULL);
707         if (ret != SQLITE_OK) {
708                 return NULL;
709         }
710
711         ret = sqlite3_bind_int(stmt, 1, user_id->id);
712         if (ret != SQLITE_OK) {
713                 sqlite3_finalize(stmt);
714                 return NULL;
715         }
716
717         result = NULL;
718         while (sqlite3_step(stmt) == SQLITE_ROW) {
719                 info = calloc(1, sizeof(*info));
720                 if (!info) {
721                         /**
722                          * @todo
723                          * Handling the OOM
724                          */
725                         LOGE("calloc: %d", errno);
726                         continue;
727                 }
728
729                 val_text = (const char *)sqlite3_column_text(stmt, 0);
730                 if (val_text && val_text[0] != '\0') {
731                         info->print_name = strdup(val_text);
732                         if (!info->print_name) {
733                                 LOGE("strdup: %d", errno);
734                         }
735                 }
736
737                 info->structure_version = sqlite3_column_int(stmt, 1);
738                 val_text = (const char *)sqlite3_column_text(stmt, 2);
739                 if (val_text && val_text[0] != '\0') {
740                         info->photo_path = strdup(val_text);
741                         if (!info->photo_path) {
742                                 LOGE("strdup: %d", errno);
743                         }
744                 }
745
746                 info->photo_id = sqlite3_column_int(stmt, 3);
747
748                 val_text = (const char *)sqlite3_column_text(stmt, 4);
749                 if (val_text && val_text[0] != '\0') {
750                         info->first_name = strdup(val_text);
751                         if (!info->first_name) {
752                                 LOGE("strdup: %d", errno);
753                         }
754                 }
755
756                 val_text = (const char *)sqlite3_column_text(stmt, 5);
757                 if (val_text && val_text[0] != '\0') {
758                         info->last_name = strdup(val_text);
759                         if (!info->last_name) {
760                                 LOGE("strdup: %d", errno);
761                         }
762                 }
763
764                 val_text = (const char *)sqlite3_column_text(stmt, 6);
765                 if (val_text && val_text[0] != '\0') {
766                         info->phone = strdup(val_text);
767                         if (!info->phone) {
768                                 LOGE("strdup: %d", errno);
769                         }
770                 }
771
772                 info->access_hash = sqlite3_column_int(stmt, 7);
773
774                 val_text = (const char *)sqlite3_column_text(stmt, 8);
775                 if (val_text && val_text[0] != '\0') {
776                         info->real_first_name = strdup(val_text);
777                         if (!info->real_first_name) {
778                                 LOGE("strdup: %d", errno);
779                         }
780                 }
781
782                 val_text = (const char *)sqlite3_column_text(stmt, 9);
783                 if (val_text && val_text[0] != '\0') {
784                         info->real_last_name = strdup(val_text);
785                         if (!info->real_last_name) {
786                                 LOGE("strdup: %d", errno);
787                         }
788                 }
789
790                 val_text = (const char *)sqlite3_column_text(stmt, 10);
791                 if (val_text && val_text[0] != '\0') {
792                         info->username = strdup(val_text);
793                         if (!info->username) {
794                                 LOGE("strdup: %d", errno);
795                         }
796                 }
797
798                 info->online = sqlite3_column_int(stmt, 11);
799                 info->last_seen = sqlite3_column_int(stmt, 12);
800
801                 memcpy(&info->user_id, user_id, sizeof(*user_id));
802
803                 result = eina_list_append(result, info);
804         }
805         close_database(db);
806         sqlite3_finalize(stmt);
807         return result;
808 }
809
810 Eina_List *tg_db_get_chat_info(const char *table_name)
811 {
812         Eina_List *result = NULL;
813         tg_chat_info_s *info;
814         sqlite3_stmt *stmt;
815         const char *tmp;
816         int ret;
817         sqlite3* db = create_database(DEFAULT_TG_DATABASE_PATH);
818         ret = sqlite3_prepare_v2(db, "SELECT chat_id, " \
819                         "flags, print_title, struct_version, " \
820                         "photo_id, photo_path, " \
821                         "title, " \
822                         "users_num, user_list_size, user_list_version, " \
823                         "inviter_id, chat_users, date, version, admin_id FROM ?", -1, &stmt, NULL);
824
825         if (ret != SQLITE_OK) {
826                 LOGE("Failed to create a stmt");
827                 return NULL;
828         }
829
830         ret = sqlite3_bind_text(stmt, 1, table_name, -1, SQLITE_TRANSIENT);
831         if (ret != SQLITE_OK) {
832                 LOGE("Failed to bind a text");
833                 return NULL;
834         }
835
836         while (sqlite3_step(stmt) == SQLITE_ROW) {
837                 info = calloc(1, sizeof(*info));
838                 if (!info) {
839                         LOGE("calloc: %d", errno);
840                         continue;
841                 }
842                 info->chat_id = sqlite3_column_int(stmt, 0);
843                 info->flags = sqlite3_column_int(stmt, 1);
844                 tmp = (const char *)sqlite3_column_text(stmt, 2);
845                 if (tmp && tmp[0] != '\0') {
846                         info->print_title = strdup(tmp);
847                         if (!info->print_title) {
848                                 LOGE("strdup: %d", errno);
849                         }
850                 }
851                 info->struct_version = sqlite3_column_int(stmt, 3);
852                 info->photo_id = sqlite3_column_int64(stmt, 4);
853                 tmp = (const char *)sqlite3_column_text(stmt, 5);
854                 if (tmp && tmp[0] != '\0') {
855                         info->photo_path = strdup(tmp);
856                         if (!info->photo_path) {
857                                 LOGE("strdup: %d", errno);
858                         }
859                 }
860                 tmp = (const char *)sqlite3_column_text(stmt, 6);
861                 if (tmp && tmp[0] != '\0') {
862                         info->title = strdup(tmp);
863                         if (!info->title) {
864                                 LOGE("strdup: %d", errno);
865                         }
866                 }
867                 info->users_num = sqlite3_column_int(stmt, 7);
868                 info->user_list_size = sqlite3_column_int(stmt, 8);
869                 info->user_list_version = sqlite3_column_int(stmt, 9);
870                 info->inviter_id = sqlite3_column_int(stmt, 10);
871
872                 tmp = (const char *)sqlite3_column_text(stmt, 11);
873                 if (tmp && tmp[0] != '\0') {
874                         char *ptr;
875
876                         ptr = strdup(tmp);
877                         if (ptr) {
878                                 char *tmp_ptr;
879                                 int i;
880
881                                 for (i = 0; i < info->users_num && *ptr && i < (sizeof(info->chat_users) / sizeof(int)); i++) {
882                                         tmp_ptr = ptr;
883
884                                         while (*tmp_ptr != ',' && *tmp_ptr != '\0') {
885                                                 tmp_ptr++;
886                                         }
887                                         *tmp_ptr = '\0';
888
889                                         info->chat_users[i] = atoi(ptr);
890                                         ptr = tmp_ptr + 1;
891                                 }
892
893                                 if (i != info->users_num) {
894                                         /**
895                                          * There is some problem.
896                                          * The count of loaded user is not correct.
897                                          * Fix it forcely.
898                                          */
899                                         LOGD("Update user_num: %d -> %d", info->users_num, i);
900                                         info->users_num = i;
901                                 }
902
903                                 free(ptr);
904                         }
905                 }
906
907                 info->date = sqlite3_column_int(stmt, 12);
908                 info->version = sqlite3_column_int(stmt, 13);
909                 info->admin_id = sqlite3_column_int(stmt, 14);
910                 result = eina_list_append(result, info);
911         }
912
913         sqlite3_finalize(stmt);
914         close_database(db);
915         return result;
916 }
917
918 tg_peer_info_s *tg_db_get_peer_info(const char *table, int peer_id)
919 {
920         tg_peer_info_s *info;
921         sqlite3_stmt *stmt;
922         const char *tmp;
923         int ret;
924         sqlite3* db = create_database(DEFAULT_TG_DATABASE_PATH);
925         ret = sqlite3_prepare_v2(db, "SELECT peer_type, flags, last_msg_id, last_msg_date, print_name, struct_version, no_of_unread_msgs, last_seen_time, photo_path, photo_id FROM ? WHERE peer_id = ?", -1, &stmt, NULL);
926         if (ret != SQLITE_OK) {
927                 return NULL;
928         }
929
930         ret = sqlite3_bind_text(stmt, 1, table, -1, SQLITE_TRANSIENT);
931         if (ret != SQLITE_OK) {
932                 sqlite3_finalize(stmt);
933                 return NULL;
934         }
935
936         ret = sqlite3_bind_int(stmt, 2, peer_id);
937         if (ret != SQLITE_OK) {
938                 sqlite3_finalize(stmt);
939                 return NULL;
940         }
941
942         if (sqlite3_step(stmt) != SQLITE_ROW) {
943                 sqlite3_finalize(stmt);
944                 return NULL;
945         }
946
947         info = calloc(1, sizeof(*info));
948         if (!info) {
949                 sqlite3_finalize(stmt);
950                 return NULL;
951         }
952
953         info->peer_id = peer_id;
954         info->peer_type = sqlite3_column_int(stmt, 0);
955         info->flags = sqlite3_column_int(stmt, 1);
956         info->last_msg_id = sqlite3_column_int64(stmt, 2);
957         info->last_msg_date = sqlite3_column_int(stmt, 3);
958         tmp = (const char *)sqlite3_column_text(stmt, 4);
959         if (tmp && tmp[0] != '\0') {
960                 info->print_name = strdup(tmp);
961                 if (!info->print_name) {
962                         LOGE("strdup: %d", errno);
963                 }
964         }
965         info->struct_version = sqlite3_column_int(stmt, 5);
966         info->no_of_unread_msgs = sqlite3_column_int(stmt, 6);
967         info->last_seen_time = sqlite3_column_int(stmt, 7);
968
969         tmp = (const char *)sqlite3_column_text(stmt, 8);
970         if (tmp && tmp[0] != '\0') {
971                 info->photo_path = strdup(tmp);
972                 if (!info->photo_path) {
973                         LOGE("strdup: %d\n", errno);
974                 }
975         }
976
977         info->photo_id = sqlite3_column_int64(stmt, 9);
978
979         sqlite3_finalize(stmt);
980         close_database(db);
981         return info;
982 }
983
984 Eina_List *tg_db_get_messages(const char *table_name)
985 {
986         Eina_List *result;
987         tg_message_s *info;
988         sqlite3_stmt *stmt;
989         const char *tmp;
990         int ret;
991         sqlite3* db = create_database(DEFAULT_TG_DATABASE_PATH);
992         ret = sqlite3_prepare_v2(db, "SELECT msg_id, flags, fwd_from_id, fwd_date, from_id, to_id, out, unread, date, service, message, msg_state, message_len, media_type, media_id, unique_id FROM ?", -1, &stmt, NULL);
993         if (ret != SQLITE_OK) {
994                 return NULL;
995         }
996
997         ret = sqlite3_bind_text(stmt, 1, table_name, -1, SQLITE_TRANSIENT);
998         if (ret != SQLITE_OK) {
999                 sqlite3_finalize(stmt);
1000                 return NULL;
1001         }
1002
1003         result = NULL;
1004         while (sqlite3_step(stmt) == SQLITE_OK) {
1005                 info = calloc(1, sizeof(*info));
1006                 if (!info) {
1007                         LOGE("calloc: %d", errno);
1008                         continue;
1009                 }
1010
1011                 info->msg_id = sqlite3_column_int(stmt, 0);
1012                 info->flags = sqlite3_column_int(stmt, 1);
1013                 info->fwd_from_id = sqlite3_column_int(stmt, 2);
1014                 info->fwd_date = sqlite3_column_int(stmt, 3);
1015                 info->from_id = sqlite3_column_int(stmt, 4);
1016                 info->to_id = sqlite3_column_int(stmt, 5);
1017                 info->out = sqlite3_column_int(stmt, 6);
1018                 info->unread = sqlite3_column_int(stmt, 7);
1019                 info->date = sqlite3_column_int(stmt, 8);
1020                 info->service = sqlite3_column_int(stmt, 9);
1021                 tmp = (const char *)sqlite3_column_text(stmt, 10);
1022                 if (tmp && tmp[0] != '\0') {
1023                         info->message = strdup(tmp);
1024                         if (!info->message) {
1025                                 LOGE("strdup: %d", errno);
1026                         }
1027                 }
1028                 info->msg_state = sqlite3_column_int(stmt, 11);
1029                 info->message_len = sqlite3_column_int(stmt, 12);
1030                 info->media_type = sqlite3_column_int(stmt, 13);
1031                 tmp = (const char *)sqlite3_column_text(stmt, 14);
1032                 if (tmp && tmp[0] != '\0') {
1033                         info->media_id = strdup(tmp);
1034                         if (!info->media_id) {
1035                                 LOGE("strdup: %d", errno);
1036                         }
1037                 }
1038                 info->unique_id = sqlite3_column_int(stmt, 15);
1039                 result = eina_list_append(result, info);
1040
1041         }
1042         sqlite3_finalize(stmt);
1043         close_database(db);
1044         return result;
1045 }
1046
1047 Eina_Bool delete_all_records(char *tablename)
1048 {
1049         if (!tablename) {
1050                 return EINA_FALSE;
1051         }
1052         char *var_query = (char*)malloc(strlen("DELETE FROM ") + strlen(tablename) + strlen(";") + 1);
1053         strcpy(var_query, "DELETE FROM ");
1054         strcat(var_query, tablename);
1055         strcat(var_query, ";");
1056         int ret;
1057         char* err_msg = 0;
1058         sqlite3* db = create_database(DEFAULT_TG_DATABASE_PATH);
1059         ret = sqlite3_exec(db,var_query, NULL, NULL, &err_msg);
1060         close_database(db);
1061         if( ret != SQLITE_OK ){
1062                 sqlite3_free(err_msg);
1063                 return EINA_FALSE;
1064         }
1065         free(var_query);
1066         return EINA_TRUE;
1067 }