4669e4ead351fc882d3e1cac4c3396c92ef6f4b0
[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)
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         ptr += sprintf(ptr, " FROM %s %s;", table_name, where_clause ? where_clause : "");
579
580         LOGD("Query: %s", query);
581
582         ret = sqlite3_prepare_v2(db, query, -1, &stmt, NULL);
583         free(query);
584         if (ret != SQLITE_OK) {
585                 close_database(db);
586                 return NULL;
587         }
588
589
590         while(sqlite3_step(stmt) == SQLITE_ROW) {
591                 col = 0;
592                 record = NULL;
593                 EINA_LIST_FOREACH(column_types, l, type) {
594                         if (!type) {
595                                 /**
596                                  * @note
597                                  * Not possible
598                                  */
599                                 continue;
600                         }
601
602                         if (!strncmp(type, "INTEGER", strlen("INTEGER"))) {
603                                 int temp;
604                                 int *val;
605
606                                 temp = sqlite3_column_int64(stmt, col);
607                                 val = malloc(sizeof(int));
608                                 if (!val) {
609                                         /**
610                                          * @todo
611                                          * Handling exceptional cases.
612                                          */
613                                 }
614                                 *val = temp;
615
616                                 record = eina_list_append(record, val);
617                         } else if (!strncmp(type, "TEXT", strlen("TEXT"))) {
618                                 char *val;
619                                 const char *text;
620
621                                 text = (const char *)sqlite3_column_text(stmt, col);
622                                 if (!text || text[0] == '\0') {
623                                         //val = strdup("_null_");
624                                         val = strdup("");
625                                 } else {
626                                         val = strdup(text);
627                                 }
628                                 if (!val) {
629                                         /**
630                                          * @todo
631                                          * Handling exceptional cases
632                                          */
633                                 } else {
634                                         record = eina_list_append(record, val);
635                                 }
636                         }
637                         col++;
638                 }
639                 result = eina_list_append(result, record);
640         }
641
642         sqlite3_finalize(stmt);
643
644         if (where_clause) {
645                 free(where_clause);
646                 where_clause = NULL;
647         }
648         close_database(db);
649         return result;
650 }
651
652
653 int tg_db_init(void)
654 {
655 #if 0
656         int ret;
657
658         if (s_info.db) {
659                 LOGD("Already initialized");
660                 return EINA_TRUE;
661         }
662
663         ret = sqlite3_open(s_info.database_name, &s_info.db);
664         if(ret != SQLITE_OK) {
665                 return EINA_FALSE;
666         }
667 #endif
668         return EINA_TRUE;
669
670 }
671
672 int tg_db_fini(void)
673 {
674 #if 0
675         if (!s_info.db) {
676                 return EINA_FALSE;
677         }
678
679         sqlite3_close(s_info.db);
680         s_info.db = NULL;
681 #endif
682         return EINA_TRUE;
683 }
684
685
686 Eina_List *tg_db_get_user_info(tgl_peer_id_t *user_id)
687 {
688         user_data_s *info;
689         Eina_List *result;
690         sqlite3_stmt *stmt;
691         int ret;
692         const char *val_text;
693         sqlite3* db = create_database(DEFAULT_TG_DATABASE_PATH);
694         ret = sqlite3_prepare_v2(db, "SELECT " \
695                                 "print_name, structure_version, " \
696                                 "photo_path, photo_id, " \
697                                 "first_name, last_name, " \
698                                 "phone, access_hash, " \
699                                 "real_first_name, real_last_name, " \
700                                 "username, online, last_seen " \
701                                 "FROM user_info_table WHERE user_id = ?", -1, &stmt, NULL);
702         if (ret != SQLITE_OK) {
703                 return NULL;
704         }
705
706         ret = sqlite3_bind_int(stmt, 1, user_id->id);
707         if (ret != SQLITE_OK) {
708                 sqlite3_finalize(stmt);
709                 return NULL;
710         }
711
712         result = NULL;
713         while (sqlite3_step(stmt) == SQLITE_ROW) {
714                 info = calloc(1, sizeof(*info));
715                 if (!info) {
716                         /**
717                          * @todo
718                          * Handling the OOM
719                          */
720                         LOGE("calloc: %d", errno);
721                         continue;
722                 }
723
724                 val_text = (const char *)sqlite3_column_text(stmt, 0);
725                 if (val_text && val_text[0] != '\0') {
726                         info->print_name = strdup(val_text);
727                         if (!info->print_name) {
728                                 LOGE("strdup: %d", errno);
729                         }
730                 }
731
732                 info->structure_version = sqlite3_column_int(stmt, 1);
733                 val_text = (const char *)sqlite3_column_text(stmt, 2);
734                 if (val_text && val_text[0] != '\0') {
735                         info->photo_path = strdup(val_text);
736                         if (!info->photo_path) {
737                                 LOGE("strdup: %d", errno);
738                         }
739                 }
740
741                 info->photo_id = sqlite3_column_int(stmt, 3);
742
743                 val_text = (const char *)sqlite3_column_text(stmt, 4);
744                 if (val_text && val_text[0] != '\0') {
745                         info->first_name = strdup(val_text);
746                         if (!info->first_name) {
747                                 LOGE("strdup: %d", errno);
748                         }
749                 }
750
751                 val_text = (const char *)sqlite3_column_text(stmt, 5);
752                 if (val_text && val_text[0] != '\0') {
753                         info->last_name = strdup(val_text);
754                         if (!info->last_name) {
755                                 LOGE("strdup: %d", errno);
756                         }
757                 }
758
759                 val_text = (const char *)sqlite3_column_text(stmt, 6);
760                 if (val_text && val_text[0] != '\0') {
761                         info->phone = strdup(val_text);
762                         if (!info->phone) {
763                                 LOGE("strdup: %d", errno);
764                         }
765                 }
766
767                 info->access_hash = sqlite3_column_int(stmt, 7);
768
769                 val_text = (const char *)sqlite3_column_text(stmt, 8);
770                 if (val_text && val_text[0] != '\0') {
771                         info->real_first_name = strdup(val_text);
772                         if (!info->real_first_name) {
773                                 LOGE("strdup: %d", errno);
774                         }
775                 }
776
777                 val_text = (const char *)sqlite3_column_text(stmt, 9);
778                 if (val_text && val_text[0] != '\0') {
779                         info->real_last_name = strdup(val_text);
780                         if (!info->real_last_name) {
781                                 LOGE("strdup: %d", errno);
782                         }
783                 }
784
785                 val_text = (const char *)sqlite3_column_text(stmt, 10);
786                 if (val_text && val_text[0] != '\0') {
787                         info->username = strdup(val_text);
788                         if (!info->username) {
789                                 LOGE("strdup: %d", errno);
790                         }
791                 }
792
793                 info->online = sqlite3_column_int(stmt, 11);
794                 info->last_seen = sqlite3_column_int(stmt, 12);
795
796                 memcpy(&info->user_id, user_id, sizeof(*user_id));
797
798                 result = eina_list_append(result, info);
799         }
800         close_database(db);
801         sqlite3_finalize(stmt);
802         return result;
803 }
804
805 Eina_List *tg_db_get_chat_info(const char *table_name)
806 {
807         Eina_List *result = NULL;
808         tg_chat_info_s *info;
809         sqlite3_stmt *stmt;
810         const char *tmp;
811         int ret;
812         sqlite3* db = create_database(DEFAULT_TG_DATABASE_PATH);
813         ret = sqlite3_prepare_v2(db, "SELECT chat_id, " \
814                         "flags, print_title, struct_version, " \
815                         "photo_id, photo_path, " \
816                         "title, " \
817                         "users_num, user_list_size, user_list_version, " \
818                         "inviter_id, chat_users, date, version, admin_id FROM ?", -1, &stmt, NULL);
819
820         if (ret != SQLITE_OK) {
821                 LOGE("Failed to create a stmt");
822                 return NULL;
823         }
824
825         ret = sqlite3_bind_text(stmt, 1, table_name, -1, SQLITE_TRANSIENT);
826         if (ret != SQLITE_OK) {
827                 LOGE("Failed to bind a text");
828                 return NULL;
829         }
830
831         while (sqlite3_step(stmt) == SQLITE_ROW) {
832                 info = calloc(1, sizeof(*info));
833                 if (!info) {
834                         LOGE("calloc: %d", errno);
835                         continue;
836                 }
837                 info->chat_id = sqlite3_column_int(stmt, 0);
838                 info->flags = sqlite3_column_int(stmt, 1);
839                 tmp = (const char *)sqlite3_column_text(stmt, 2);
840                 if (tmp && tmp[0] != '\0') {
841                         info->print_title = strdup(tmp);
842                         if (!info->print_title) {
843                                 LOGE("strdup: %d", errno);
844                         }
845                 }
846                 info->struct_version = sqlite3_column_int(stmt, 3);
847                 info->photo_id = sqlite3_column_int64(stmt, 4);
848                 tmp = (const char *)sqlite3_column_text(stmt, 5);
849                 if (tmp && tmp[0] != '\0') {
850                         info->photo_path = strdup(tmp);
851                         if (!info->photo_path) {
852                                 LOGE("strdup: %d", errno);
853                         }
854                 }
855                 tmp = (const char *)sqlite3_column_text(stmt, 6);
856                 if (tmp && tmp[0] != '\0') {
857                         info->title = strdup(tmp);
858                         if (!info->title) {
859                                 LOGE("strdup: %d", errno);
860                         }
861                 }
862                 info->users_num = sqlite3_column_int(stmt, 7);
863                 info->user_list_size = sqlite3_column_int(stmt, 8);
864                 info->user_list_version = sqlite3_column_int(stmt, 9);
865                 info->inviter_id = sqlite3_column_int(stmt, 10);
866
867                 tmp = (const char *)sqlite3_column_text(stmt, 11);
868                 if (tmp && tmp[0] != '\0') {
869                         char *ptr;
870
871                         ptr = strdup(tmp);
872                         if (ptr) {
873                                 char *tmp_ptr;
874                                 int i;
875
876                                 for (i = 0; i < info->users_num && *ptr && i < (sizeof(info->chat_users) / sizeof(int)); i++) {
877                                         tmp_ptr = ptr;
878
879                                         while (*tmp_ptr != ',' && *tmp_ptr != '\0') {
880                                                 tmp_ptr++;
881                                         }
882                                         *tmp_ptr = '\0';
883
884                                         info->chat_users[i] = atoi(ptr);
885                                         ptr = tmp_ptr + 1;
886                                 }
887
888                                 if (i != info->users_num) {
889                                         /**
890                                          * There is some problem.
891                                          * The count of loaded user is not correct.
892                                          * Fix it forcely.
893                                          */
894                                         LOGD("Update user_num: %d -> %d", info->users_num, i);
895                                         info->users_num = i;
896                                 }
897
898                                 free(ptr);
899                         }
900                 }
901
902                 info->date = sqlite3_column_int(stmt, 12);
903                 info->version = sqlite3_column_int(stmt, 13);
904                 info->admin_id = sqlite3_column_int(stmt, 14);
905                 result = eina_list_append(result, info);
906         }
907
908         sqlite3_finalize(stmt);
909         close_database(db);
910         return result;
911 }
912
913 tg_peer_info_s *tg_db_get_peer_info(const char *table, int peer_id)
914 {
915         tg_peer_info_s *info;
916         sqlite3_stmt *stmt;
917         const char *tmp;
918         int ret;
919         sqlite3* db = create_database(DEFAULT_TG_DATABASE_PATH);
920         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);
921         if (ret != SQLITE_OK) {
922                 return NULL;
923         }
924
925         ret = sqlite3_bind_text(stmt, 1, table, -1, SQLITE_TRANSIENT);
926         if (ret != SQLITE_OK) {
927                 sqlite3_finalize(stmt);
928                 return NULL;
929         }
930
931         ret = sqlite3_bind_int(stmt, 2, peer_id);
932         if (ret != SQLITE_OK) {
933                 sqlite3_finalize(stmt);
934                 return NULL;
935         }
936
937         if (sqlite3_step(stmt) != SQLITE_ROW) {
938                 sqlite3_finalize(stmt);
939                 return NULL;
940         }
941
942         info = calloc(1, sizeof(*info));
943         if (!info) {
944                 sqlite3_finalize(stmt);
945                 return NULL;
946         }
947
948         info->peer_id = peer_id;
949         info->peer_type = sqlite3_column_int(stmt, 0);
950         info->flags = sqlite3_column_int(stmt, 1);
951         info->last_msg_id = sqlite3_column_int64(stmt, 2);
952         info->last_msg_date = sqlite3_column_int(stmt, 3);
953         tmp = (const char *)sqlite3_column_text(stmt, 4);
954         if (tmp && tmp[0] != '\0') {
955                 info->print_name = strdup(tmp);
956                 if (!info->print_name) {
957                         LOGE("strdup: %d", errno);
958                 }
959         }
960         info->struct_version = sqlite3_column_int(stmt, 5);
961         info->no_of_unread_msgs = sqlite3_column_int(stmt, 6);
962         info->last_seen_time = sqlite3_column_int(stmt, 7);
963
964         tmp = (const char *)sqlite3_column_text(stmt, 8);
965         if (tmp && tmp[0] != '\0') {
966                 info->photo_path = strdup(tmp);
967                 if (!info->photo_path) {
968                         LOGE("strdup: %d\n", errno);
969                 }
970         }
971
972         info->photo_id = sqlite3_column_int64(stmt, 9);
973
974         sqlite3_finalize(stmt);
975         close_database(db);
976         return info;
977 }
978
979 Eina_List *tg_db_get_messages(const char *table_name)
980 {
981         Eina_List *result;
982         tg_message_s *info;
983         sqlite3_stmt *stmt;
984         const char *tmp;
985         int ret;
986         sqlite3* db = create_database(DEFAULT_TG_DATABASE_PATH);
987         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);
988         if (ret != SQLITE_OK) {
989                 return NULL;
990         }
991
992         ret = sqlite3_bind_text(stmt, 1, table_name, -1, SQLITE_TRANSIENT);
993         if (ret != SQLITE_OK) {
994                 sqlite3_finalize(stmt);
995                 return NULL;
996         }
997
998         result = NULL;
999         while (sqlite3_step(stmt) == SQLITE_OK) {
1000                 info = calloc(1, sizeof(*info));
1001                 if (!info) {
1002                         LOGE("calloc: %d", errno);
1003                         continue;
1004                 }
1005
1006                 info->msg_id = sqlite3_column_int(stmt, 0);
1007                 info->flags = sqlite3_column_int(stmt, 1);
1008                 info->fwd_from_id = sqlite3_column_int(stmt, 2);
1009                 info->fwd_date = sqlite3_column_int(stmt, 3);
1010                 info->from_id = sqlite3_column_int(stmt, 4);
1011                 info->to_id = sqlite3_column_int(stmt, 5);
1012                 info->out = sqlite3_column_int(stmt, 6);
1013                 info->unread = sqlite3_column_int(stmt, 7);
1014                 info->date = sqlite3_column_int(stmt, 8);
1015                 info->service = sqlite3_column_int(stmt, 9);
1016                 tmp = (const char *)sqlite3_column_text(stmt, 10);
1017                 if (tmp && tmp[0] != '\0') {
1018                         info->message = strdup(tmp);
1019                         if (!info->message) {
1020                                 LOGE("strdup: %d", errno);
1021                         }
1022                 }
1023                 info->msg_state = sqlite3_column_int(stmt, 11);
1024                 info->message_len = sqlite3_column_int(stmt, 12);
1025                 info->media_type = sqlite3_column_int(stmt, 13);
1026                 tmp = (const char *)sqlite3_column_text(stmt, 14);
1027                 if (tmp && tmp[0] != '\0') {
1028                         info->media_id = strdup(tmp);
1029                         if (!info->media_id) {
1030                                 LOGE("strdup: %d", errno);
1031                         }
1032                 }
1033                 info->unique_id = sqlite3_column_int(stmt, 15);
1034                 result = eina_list_append(result, info);
1035
1036         }
1037         sqlite3_finalize(stmt);
1038         close_database(db);
1039         return result;
1040 }
1041
1042 Eina_Bool delete_all_records(char *tablename)
1043 {
1044         if (!tablename) {
1045                 return EINA_FALSE;
1046         }
1047         char *var_query = (char*)malloc(strlen("DELETE FROM ") + strlen(tablename) + strlen(";") + 1);
1048         strcpy(var_query, "DELETE FROM ");
1049         strcat(var_query, tablename);
1050         strcat(var_query, ";");
1051         int ret;
1052         char* err_msg = 0;
1053         sqlite3* db = create_database(DEFAULT_TG_DATABASE_PATH);
1054         ret = sqlite3_exec(db,var_query, NULL, NULL, &err_msg);
1055         close_database(db);
1056         if( ret != SQLITE_OK ){
1057                 sqlite3_free(err_msg);
1058                 return EINA_FALSE;
1059         }
1060         free(var_query);
1061         return EINA_TRUE;
1062 }