-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb_wrapper.c
1427 lines (1178 loc) · 41.4 KB
/
db_wrapper.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "db_wrapper.h"
#include <sqlite3.h>
#include "db_util.h"
#include "db_cursor.h"
#include <printbuf.h>
#include <json_object.h>
#include <tg_utility.h>
#define DB_GET_PERFORMANCE 1
#define DB_MAX_QUERY_ROWS_ONE_TIME 5000
#define DB_MAX_HEAP_SIZE_FOR_ONE_DB 1024
struct _DatabaseHandle
{
sqlite3* sqlite;
};
struct _DatabaseStatement
{
sqlite3_stmt * sqlite_stmt;
};
static INT32 _db_error_code_convert(INT32 sqlite_err)
{
if (SQLITE_DONE==sqlite_err)
return DB_OK;
return sqlite_err;
}
static INT32 _db_print_explain_query_plan(sqlite3* sqlite,const CHAR* zSql);
static INT32 _db_print_explain(sqlite3* sqlite,const CHAR* zSql);
static void _db_analyze(sqlite3* sqlite);
INT32 db_open(const CHAR* path,DatabaseHandle** hdl_ouput)
{
DatabaseHandle* hdl = TG_CALLOC_V2(sizeof(DatabaseHandle));
INT32 sqlite_err;
return_val_if_fail(hdl,DB_NOMEM);
sqlite_err = sqlite3_open(path,(sqlite3 **)&hdl->sqlite);
if (DB_OK != sqlite_err)
{
TG_FREE(hdl);
}
else
{
*hdl_ouput = hdl;
sqlite3_soft_heap_limit(DB_MAX_HEAP_SIZE_FOR_ONE_DB);
}
return _db_error_code_convert(sqlite_err);
}
INT32 db_open_v2(const CHAR* path, INT32 flags,DatabaseHandle** hdl_ouput)
{
DatabaseHandle* hdl = TG_CALLOC_V2(sizeof(DatabaseHandle));
INT32 sqlite_err;
return_val_if_fail(hdl,DB_NOMEM);
sqlite_err = sqlite3_open_v2(path,(sqlite3 **)&hdl->sqlite,flags,NULL);
if (DB_OK != sqlite_err)
{
TG_FREE(hdl);
}
else
{
*hdl_ouput = hdl;
sqlite3_soft_heap_limit(DB_MAX_HEAP_SIZE_FOR_ONE_DB);
}
return _db_error_code_convert(sqlite_err);
}
INT32 db_close(DatabaseHandle* hdl)
{
INT32 sqlite_err;
return_val_if_fail((hdl && hdl->sqlite),DB_ERROR);
sqlite_err=sqlite3_close((sqlite3 *)hdl->sqlite);
if (sqlite_err==DB_OK)
TG_FREE(hdl);
return _db_error_code_convert(sqlite_err);
}
INT32 _db_execSQL_impl(sqlite3* sqlite,const CHAR* sql_str,DB_CALLBACK callback,void* arg)
{
CHAR* errmsg = NULL;
INT32 sqlite_err;
return_val_if_fail((sqlite),SQLITE_OK);
sqlite_err= sqlite3_exec(sqlite,sql_str,(sqlite3_callback)callback,arg,&errmsg);
if (SQLITE_OK!=sqlite_err)
{
DB_PRINTF("_db_execSQL_impl failure-error:%d; msg:%s\n",sqlite_err,errmsg);
sqlite3_free(errmsg);
}
return sqlite_err;
}
INT32 db_execSQL_v2(DatabaseHandle* hdl,const CHAR* sql_str,DB_CALLBACK callback,void* arg)
{
return_val_if_fail((hdl && hdl->sqlite),DB_ERROR);
return _db_error_code_convert(_db_execSQL_impl(hdl->sqlite,sql_str,callback,arg));
}
INT32 db_execSQL(DatabaseHandle* hdl,const CHAR* sql_str)
{
return_val_if_fail((hdl && hdl->sqlite),DB_ERROR);
return _db_error_code_convert(_db_execSQL_impl(hdl->sqlite,sql_str,NULL,NULL));
}
static INT32 _db_simple_execSQL_for_int(void* arg,INT32 count ,char** values, char** columns)
{
INT32* output_int = (INT32*)arg;
DB_ASSERT(count > 0);
*output_int = atoi(values[0]);
return 0;
}
INT32 db_simple_execSQL_for_int(DatabaseHandle* hdl,const CHAR* sql_str,INT32* output_int)
{
return db_execSQL_v2(hdl,sql_str,_db_simple_execSQL_for_int,(void*)output_int);
}
BOOL db_table_is_exist(DatabaseHandle* hdl,const CHAR* table_name)
{
INT32 err = DB_OK;
CHAR* sql_str = NULL;
INT32 count=0;
return_val_if_fail((hdl && hdl->sqlite && table_name),DB_ERROR);
sql_str = (char*)TG_CALLOC_V2(100+strlen(table_name));
return_val_if_fail(sql_str,DB_NOMEM);
sprintf(sql_str,"SELECT count(*) FROM sqlite_master WHERE type='table' and name='%s';",table_name);
err = db_simple_execSQL_for_int(hdl,sql_str,&count);
TG_FREE(sql_str);
return count!=0;
}
static INT32 _db_drop_impl(DatabaseHandle* hdl,const CHAR* tb_type,const CHAR* table_name)
{
INT32 sqlite_err = DB_OK;
CHAR* erro_msg = NULL;
CHAR* sql_str = NULL;
return_val_if_fail((hdl && hdl->sqlite && table_name),DB_ERROR);
sql_str = (char*)TG_CALLOC_V2(100+strlen(table_name));
return_val_if_fail(sql_str,DB_NOMEM);
sprintf(sql_str,"DROP %s IF EXISTS %s",tb_type,table_name);
sqlite_err=sqlite3_exec((sqlite3 *)hdl->sqlite,sql_str,NULL,NULL,&erro_msg);
TG_FREE(sql_str);
if (DB_OK!=sqlite_err)
{
DB_PRINTF("_db_drop_impl failure-error:%d; msg:%s\n",sqlite_err,erro_msg);
sqlite3_free(erro_msg);
}
return _db_error_code_convert(sqlite_err);
}
INT32 db_table_drop(DatabaseHandle* hdl,const CHAR* table_name)
{
return _db_drop_impl(hdl,"TABLE",table_name);
}
INT32 db_index_drop(DatabaseHandle* hdl,const CHAR* index_name)
{
return _db_drop_impl(hdl,"INDEX",index_name);
}
INT32 db_view_drop(DatabaseHandle* hdl,const CHAR* view_name)
{
return _db_drop_impl(hdl,"VIEW",view_name);
}
INT32 db_table_get_count_v2(DatabaseHandle* hdl,const CHAR* table_name,const CHAR* where_clause,INT32* count)
{
INT32 ret = DB_OK;
CHAR* sql_str = NULL;
INT32 len = 0;
return_val_if_fail(table_name,DB_ERROR);
len = 64+strlen(table_name);
if (where_clause)
len+=strlen(where_clause);
sql_str = (char*)TG_CALLOC_V2(len);
return_val_if_fail(sql_str,DB_NOMEM);
if (where_clause)
sprintf(sql_str,"SELECT count(rowid) FROM %s WHERE %s;",table_name,where_clause);
else
sprintf(sql_str,"SELECT count(*) FROM %s;",table_name);
ret = db_simple_execSQL_for_int(hdl,sql_str,count);
TG_FREE(sql_str);
return ret;
}
INT32 db_table_get_count(DatabaseHandle* hdl,const CHAR* table_name,INT32* count)
{
return db_table_get_count_v2(hdl,table_name,NULL,count);
}
INT32 db_last_changes(DatabaseHandle* hdl)
{
return_val_if_fail((hdl &&hdl->sqlite ),-1);
return sqlite3_changes(hdl->sqlite);
}
INT64 db_last_insert_rowid(DatabaseHandle* hdl)
{
return_val_if_fail((hdl &&hdl->sqlite ),-1);
return sqlite3_last_insert_rowid(hdl->sqlite);
}
INT32 db_last_error(DatabaseHandle* hdl)
{
return_val_if_fail((hdl &&hdl->sqlite ),-1);
return _db_error_code_convert(sqlite3_errcode(hdl->sqlite));
}
//for db statement
static DatabaseStatement* _db_stmt_create_impl(sqlite3_stmt * sqlite_stmt)
{
DatabaseStatement* stmt = NULL;
return_val_if_fail(( sqlite_stmt),NULL);
stmt=(DatabaseStatement*)TG_CALLOC_V2(sizeof(DatabaseStatement));
return_val_if_fail(( stmt),NULL);
stmt->sqlite_stmt = sqlite_stmt;
return stmt;
}
void db_stmt_destroy(DatabaseStatement* stmt)
{
TG_FREE(stmt);
}
static sqlite3_stmt * _db_stmt_compile_statement( DatabaseHandle* hdl,const CHAR* sql_string)
{
sqlite3 * sqlite_hdl = (sqlite3 *)hdl->sqlite;
INT32 sqlite_err;
sqlite3_stmt * stmt=NULL;
sqlite_err=sqlite3_prepare_v2(sqlite_hdl,sql_string,strlen(sql_string)+1,&stmt,NULL);
if ((DB_OK!=sqlite_err))
{
DB_PRINTF("db_compile_statement failure-error:%d; msg:%s; sql:%s\n",sqlite_err,sqlite3_errmsg(sqlite_hdl),sql_string);
return NULL;
}
return (DB_OK == sqlite_err)?stmt:NULL;
}
DatabaseStatement* db_stmt_build_statement( DatabaseHandle* hdl,const CHAR* sql_string)
{
sqlite3_stmt * stmt = NULL;
DatabaseStatement* db_stmt= NULL;
return_val_if_fail(( hdl && sql_string ),NULL);
stmt=_db_stmt_compile_statement(hdl,sql_string);
return_val_if_fail((stmt),NULL);
db_stmt=_db_stmt_create_impl(stmt);
if (!db_stmt)
{
sqlite3_finalize(stmt);
}
return db_stmt;
}
static printbuf* _db_stmt_build_insert_statement_impl(const CHAR* table,const CHAR* col_list,INT32 col_num)
{
//(contact_urn,tag_urn,value) values('%d','%d','%s')
printbuf* pb = NULL;
pb = printbuf_new_v2(256);
return_val_if_fail((pb), NULL);
sprintbuf(pb,"INSERT INTO %s(%s) VALUES(?",table,col_list);
col_num--;
while (col_num--)
{
printbuf_memappend(pb,",?",2);
}
printbuf_memappend(pb,");",2);
return pb;
}
DatabaseStatement* db_stmt_build_insert_statement( DatabaseHandle* hdl,const CHAR* table_name,const CHAR* col_list,INT32 col_num)
{
sqlite3_stmt * stmt = NULL;
printbuf* pb = NULL;
DatabaseStatement* db_stmt= NULL;
return_val_if_fail(( table_name && col_list && col_num>0),NULL);
pb=_db_stmt_build_insert_statement_impl(table_name,col_list,col_num);
return_val_if_fail((pb),NULL);
stmt=_db_stmt_compile_statement(hdl,pb->buf); //compile statememt
printbuf_free(pb);
return_val_if_fail((stmt),NULL);
db_stmt=_db_stmt_create_impl(stmt);
if (!db_stmt)
{
sqlite3_finalize(stmt);
}
return db_stmt;
}
static printbuf* _db_stmt_build_delete_statement_impl(const CHAR* table,DatabaseClause* clause)
{
//delete from table where
printbuf* pb = NULL;
return_val_if_fail((table), NULL);
pb = printbuf_new_v2(256);
return_val_if_fail((pb), NULL);
if (clause)
{
const CHAR* clause_str = db_clause_get_statement(clause);
DB_ASSERT((clause_str));
sprintbuf(pb,"DELETE FROM %s WHERE %s",table,clause_str);
}
else
sprintbuf(pb,"DELETE FROM %s",table);
return pb;
}
DatabaseStatement* db_stmt_build_delete_statement( DatabaseHandle* hdl,const CHAR* table_name,DatabaseClause* clause)
{
sqlite3_stmt * stmt = NULL;
printbuf* pb = NULL;
DatabaseStatement* db_stmt= NULL;
return_val_if_fail(( hdl && table_name ),NULL);
pb=_db_stmt_build_delete_statement_impl(table_name,clause);
return_val_if_fail((pb),NULL);
stmt=_db_stmt_compile_statement(hdl,pb->buf); //compile statememt
printbuf_free(pb);
return_val_if_fail((stmt),NULL);
db_stmt=_db_stmt_create_impl(stmt);
if (!db_stmt)
{
sqlite3_finalize(stmt);
}
return db_stmt;
}
#if 0
static printbuf* _db_stmt_build_query_statement_impl(const CHAR* col_list,const CHAR* table,DatabaseClause* clause)
{
//delete from table where
printbuf* pb = NULL;
return_val_if_fail((table), NULL);
pb = printbuf_new_v2(256);
return_val_if_fail((pb), NULL);
if (clause)
{
sprintbuf(pb,"SELECT %s from %s WHERE %s",col_list,table,db_clause_get_statement(clause));
}
else
sprintbuf(pb,"SELECT %s from %s",col_list,table);
return pb;
}
#endif
static printbuf* _db_stmt_build_update_statement_impl(const CHAR* table,ContentValues* values,DatabaseClause* clause)
{
//(contact_urn,tag_urn,value) values('%d','%d','%s')
printbuf* pb = NULL;
ContentValuesItor itor;
BOOL need_separator = FALSE;
INT32 value_num;
INT32 ret=0;
return_val_if_fail((table&&values), NULL);
ret=content_values_get_count(values,&value_num);
return_val_if_fail((value_num>0 && ret==ContentValues_SUCC), NULL);
pb = printbuf_new_v2(256);
return_val_if_fail((pb), NULL);
sprintbuf(pb,"UPDATE %s SET ",table);
for (itor=content_values_first(values);itor;itor=content_values_next(values,itor))
{
const CHAR* key = content_values_get_key(itor);
if (need_separator)
sprintbuf(pb, ",%s=?", key);
else
sprintbuf(pb, "%s=?", key);
need_separator=TRUE;
}
if (clause)
{
const CHAR* clause_str = db_clause_get_statement(clause);
DB_ASSERT((clause_str));
sprintbuf(pb," WHERE %s",clause_str);
}
return pb;
}
DatabaseStatement* db_stmt_build_update_statement( DatabaseHandle* hdl,const CHAR* table_name,ContentValues* values,DatabaseClause* clause)
{
sqlite3_stmt * stmt = NULL;
printbuf* pb = NULL;
DatabaseStatement* db_stmt= NULL;
return_val_if_fail(( hdl && table_name ),NULL);
pb=_db_stmt_build_update_statement_impl(table_name,values,clause);
return_val_if_fail((pb),NULL);
stmt=_db_stmt_compile_statement(hdl,pb->buf); //compile statememt
printbuf_free(pb);
return_val_if_fail((stmt),NULL);
db_stmt=_db_stmt_create_impl(stmt);
if (!db_stmt)
{
sqlite3_finalize(stmt);
}
return db_stmt;
}
static INT32 _db_stmt_bind_content_values_impl(sqlite3_stmt* stmt,ContentValues* values)
{
ContentValuesItor itor;
ContentValuesEntry_TYPE type;
UINT32 len;
INT32 value_num;
INT32 ret=0;
INT32 i;
ret=content_values_get_count(values,&value_num);
return_val_if_fail((value_num>0 && ret==ContentValues_SUCC), SQLITE_ERROR);
for (i=0,itor=content_values_first(values);i<value_num && itor;i++,itor=content_values_next(values,itor))
{
const void* value = content_values_get_value(itor,&type,&len);
if (value==NULL)
ret=sqlite3_bind_null(stmt,i+1);
else if (type==ContentValuesEntry_TYPE_INT)
ret=sqlite3_bind_int(stmt,i+1,*((INT32*)value));
else if (type==ContentValuesEntry_TYPE_DOUBLE)
ret=sqlite3_bind_double(stmt,i+1,*((double*)value));
else if (type==ContentValuesEntry_TYPE_BOOL)
{
BOOL b = *((BOOL*)value);
INT32 value = 0;
if (b)
value = 1;
ret=sqlite3_bind_int(stmt,i+1,value);
}
else if (type==ContentValuesEntry_TYPE_STRING)
{
if (len>0)
len--; //remove /0
ret=sqlite3_bind_text(stmt,i+1,(const CHAR*)value,len,NULL);
}
else if (type==ContentValuesEntry_TYPE_STRING_16)
{
if (len>0)
len-=2; //remove /0
ret=sqlite3_bind_text16(stmt,i+1,(const WCHAR*)value,len,NULL);
}
else if (type==ContentValuesEntry_TYPE_BLOB)
{
ret=sqlite3_bind_blob(stmt,i+1,value,len,NULL);
}
}
#if DB_DEBUG
if (DB_OK!=ret)
{
sqlite3 * sqlite_hdl = sqlite3_db_handle(stmt);
DB_PRINTF("_db_stmt_bind_values failure-error:%d; msg:%s\n",ret,sqlite3_errmsg(sqlite_hdl));
}
#endif
return ret;
}
INT32 db_stmt_bind_content_values(DatabaseStatement* stmt,ContentValues* values)
{
INT32 sqlite_err;
return_val_if_fail((stmt && stmt->sqlite_stmt), DB_ERROR);
return_val_if_fail(sqlite3_reset(stmt->sqlite_stmt)==DB_OK, DB_ERROR);
sqlite_err= _db_stmt_bind_content_values_impl(stmt->sqlite_stmt,values);
return _db_error_code_convert(sqlite_err);
}
static INT32 _db_stmt_bind_clause_impl(sqlite3_stmt* stmt,DatabaseClause* clause,INT32 start_pos)
{
UINT8 type;
INT32 len;
INT32 value_num;
BOOL ret=0;
INT32 i;
void* data= NULL;
value_num=db_clause_get_data_count(clause);
return_val_if_fail((value_num>0), 0);
for (i=0;i<value_num ;i++)
{
INT32 pos=start_pos+i+1;
ret = db_clause_get_data(clause,i,&type,&len,&data);
if (data==NULL)
ret=sqlite3_bind_null(stmt,pos);
else if (type==FIELD_TYPE_INTEGER)
ret=sqlite3_bind_int(stmt,pos,*((INT32*)data));
else if (type==FIELD_TYPE_FLOAT)
ret=sqlite3_bind_double(stmt,pos,*((double*)data));
else if (type==FIELD_TYPE_BOOL)
{
BOOL b = *((BOOL*)data);
INT32 value = 0;
if (b)
value = 1;
ret=sqlite3_bind_int(stmt,pos,value);
}
else if (type==FIELD_TYPE_STRING)
{
if (len>0)
len--; //remove /0
ret=sqlite3_bind_text(stmt,pos,(const CHAR*)data,len,NULL);
}
else if (type==FIELD_TYPE_STRING_16)
{
if (len>0)
len-=2; //remove /0
ret=sqlite3_bind_text16(stmt,pos,(const WCHAR*)data,len,NULL);
}
else if (type==FIELD_TYPE_BLOB)
{
ret=sqlite3_bind_blob(stmt,pos,data,len,NULL);
}
}
#if DB_DEBUG
if (DB_OK!=ret)
{
sqlite3 * sqlite_hdl = sqlite3_db_handle(stmt);
DB_PRINTF("_db_stmt_bind_clause_impl failure-error:%d; msg:%s\n",ret,sqlite3_errmsg(sqlite_hdl));
}
#endif
return ret;
}
INT32 db_stmt_bind_clause(DatabaseStatement* stmt,DatabaseClause* clause)
{
INT32 sqlite_err;
return_val_if_fail((stmt && stmt->sqlite_stmt), DB_ERROR);
return_val_if_fail(sqlite3_reset(stmt->sqlite_stmt)==DB_OK, DB_ERROR);
sqlite_err= _db_stmt_bind_clause_impl(stmt->sqlite_stmt,clause,0);
return _db_error_code_convert(sqlite_err);
}
INT32 db_stmt_bind_content_and_clause_values(DatabaseStatement* stmt,ContentValues* values,DatabaseClause* clause)
{
INT32 ret=db_stmt_bind_content_values(stmt,values);
INT32 value_num = 0;
INT32 sqlite_err;
return_val_if_fail((ret==DB_OK), ret);
content_values_get_count(values,&value_num);
sqlite_err= _db_stmt_bind_clause_impl(stmt->sqlite_stmt,clause,value_num);
return _db_error_code_convert(sqlite_err);
}
INT32 db_stmt_step(DatabaseStatement* stmt)
{
INT32 sqlite_err;
return_val_if_fail((stmt && stmt->sqlite_stmt), DB_ERROR);
sqlite_err=sqlite3_step(stmt->sqlite_stmt);
#if DB_DEBUG
if (SQLITE_DONE!=sqlite_err)
{
sqlite3 * sqlite_hdl = sqlite3_db_handle(stmt->sqlite_stmt);
DB_PRINTF("db_stmt_step failure-error:%d; msg:%s\n",sqlite_err,sqlite3_errmsg(sqlite_hdl));
}
#endif
return _db_error_code_convert(sqlite_err);
}
INT32 db_stmt_finalize(DatabaseStatement* stmt)
{
INT32 sqlite_err;
return_val_if_fail((stmt && stmt->sqlite_stmt), DB_ERROR);
sqlite_err=sqlite3_finalize(stmt->sqlite_stmt);
if (DB_OK==sqlite_err)
{
TG_FREE(stmt);
}
#if DB_DEBUG
else
{
sqlite3 * sqlite_hdl = sqlite3_db_handle(stmt->sqlite_stmt);
DB_PRINTF("db_stmt_finalize failure-error:%d; msg:%s\n",sqlite_err,sqlite3_errmsg(sqlite_hdl));
}
#endif
return _db_error_code_convert(sqlite_err);
}
static printbuf* _db_extract_col_list_from_content_values(ContentValues* values,INT32* count)
{
//(contact_urn,tag_urn,value) values('%d','%d','%s')
printbuf* pb = NULL;
ContentValuesItor itor;
BOOL need_separator = FALSE;
INT32 value_num;
INT32 ret;
return_val_if_fail((values), NULL);
ret=content_values_get_count(values,&value_num);
return_val_if_fail((value_num>0 && ret==ContentValues_SUCC), NULL);
pb = printbuf_new_v2(256);
return_val_if_fail((pb), NULL);
for (itor=content_values_first(values);itor;itor=content_values_next(values,itor))
{
const CHAR* key = content_values_get_key(itor);
if (need_separator)
sprintbuf(pb, ",%s", key);
else
sprintbuf(pb, "%s", key);
need_separator=TRUE;
}
*count = value_num;
return pb;
}
INT32 db_insert(DatabaseHandle* hdl,const CHAR* table_name, ContentValues* values)
{
INT32 ret = DB_OK;
DatabaseStatement* stmt = NULL;
printbuf* col_list_pb = NULL;
INT32 count = 0;
#if DB_GET_PERFORMANCE
UINT32 t1 = db_get_ticks();
UINT32 t2 = 0;
#endif
return_val_if_fail((hdl&&hdl->sqlite && table_name && values),DB_ERROR);
col_list_pb=_db_extract_col_list_from_content_values((ContentValues*)values,&count);
return_val_if_fail(col_list_pb&&count>0,DB_ERROR);
stmt= db_stmt_build_insert_statement( hdl, table_name,col_list_pb->buf,count);
printbuf_free(col_list_pb);
return_val_if_fail(stmt,DB_ERROR);
ret=db_stmt_bind_content_values(stmt,(ContentValues*)values); //bind vaules
if (ret!=DB_OK)
{
db_stmt_finalize(stmt); //clear statememt
return DB_ERROR;
}
ret=db_stmt_step(stmt); //execute statememt
ret=db_stmt_finalize(stmt); //clear statememt
#if DB_GET_PERFORMANCE
t2 = db_get_ticks();
DB_PRINTF("db_insert use %d\n",t2-t1);
#endif
return ret;
}
INT32 db_delete(DatabaseHandle* hdl,const CHAR* table_name, DatabaseWhereClause* clause)
{
INT32 ret = DB_OK;
DatabaseStatement* stmt = NULL;
#if DB_GET_PERFORMANCE
UINT32 t1 = db_get_ticks();
UINT32 t2 = 0;
#endif
return_val_if_fail((hdl&&hdl->sqlite && table_name),DB_ERROR);
stmt= db_stmt_build_delete_statement( hdl, table_name,clause);
return_val_if_fail(stmt,DB_ERROR);
ret=db_stmt_bind_clause(stmt,(DatabaseWhereClause*)clause); //bind vaules
if (ret!=DB_OK)
{
db_stmt_finalize(stmt); //clear statememt
return DB_ERROR;
}
ret=db_stmt_step(stmt); //execute statememt
ret=db_stmt_finalize(stmt); //clear statememt
#if DB_GET_PERFORMANCE
t2 = db_get_ticks();
DB_PRINTF("db_delete delete %d use %d\n",db_last_changes(hdl),t2-t1);
#endif
return ret;
}
INT32 db_delete_all(DatabaseHandle* hdl,const CHAR* table_name)
{
return db_delete(hdl,table_name,NULL);
}
INT32 db_update(DatabaseHandle* hdl,const CHAR* table_name, ContentValues* values, DatabaseWhereClause* clause)
{
INT32 ret = DB_OK;
DatabaseStatement* stmt = NULL;
return_val_if_fail((hdl&&hdl->sqlite && table_name&&values&&clause),DB_ERROR);
stmt= db_stmt_build_update_statement( hdl, table_name,values,clause);
return_val_if_fail(stmt,DB_ERROR);
ret=db_stmt_bind_content_and_clause_values(stmt,values,clause); //bind vaules
if (ret!=DB_OK)
{
db_stmt_finalize(stmt); //clear statememt
return DB_ERROR;
}
ret=db_stmt_step(stmt); //execute statememt
ret=db_stmt_finalize(stmt); //clear statememt
return ret;
}
static INT32 _db_skip_rows(sqlite3_stmt *statement, int max_rows)
{
INT32 retry_count = 0;
INT32 i = 0;
for (i = 0; i < max_rows; i++)
{
int err = sqlite3_step(statement);
if (err == SQLITE_ROW)
{
// do nothing
}
else if (err == SQLITE_DONE)
{
return i;
}
else if (err == SQLITE_LOCKED || err == SQLITE_BUSY)
{
if (retry_count > 50)
{
break;
}
// Sleep to give the thread holding the lock a chance to finish
UtaOsSleep(1000,1000);
retry_count++;
continue;
}
else
{
return -1;
}
}
return max_rows;
}
static INT32 _db_finish_program_and_get_row_count(sqlite3_stmt *statement)
{
INT32 num_rows = 0;
INT32 retry_count = 0;
while (TRUE)
{
INT32 err = sqlite3_step(statement);
if (err == SQLITE_ROW){
num_rows++;
}
else if (err == SQLITE_LOCKED || err == SQLITE_BUSY)
{
// The table is locked, retry
DB_PRINTF("_db_finish_program_and_get_row_count Database locked, retrying");
if (retry_count > 50)
{
DB_PRINTF("_db_finish_program_and_get_row_count Bailing on database busy rety");
break;
}
// Sleep to give the thread holding the lock a chance to finish
UtaOsSleep(1000,1000);
retry_count++;
continue;
}
else
{
// no need to throw exception
break;
}
}
sqlite3_reset(statement);
DB_PRINTF("_db_finish_program_and_get_row_count row %d", num_rows);
return num_rows;
}
static INT32 _db_fill_data_window( DatabaseHandle* hdl, sqlite3_stmt* statement,CursorWindow * window,INT32 start_pos,INT32 offset_param,INT32 max_read,INT32 last_pos)
{
INT32 err;
INT32 num_rows = last_pos;
INT32 num_columns;
INT32 retry_count;
#if DB_GET_PERFORMANCE
UINT32 t1 = db_get_ticks();
UINT32 t2,t3,t4;
#endif
return_val_if_fail((hdl &&hdl->sqlite && window),-1);
max_read += last_pos;
// Only do the binding if there is a valid offsetParam. If no binding needs to be done
// offsetParam will be set to 0, an invliad value.
if (offset_param > 0)
{
// Bind the offset parameter, telling the program which row to start with
err = sqlite3_bind_int(statement, offset_param, start_pos);
if (err != DB_OK)
{
DB_PRINTF("_db_fill_window failure-error:%d; msg:%s\n",err,sqlite3_errmsg(hdl->sqlite));
return -1;
}
}
num_columns = sqlite3_column_count(statement);
retry_count = 0;
if (start_pos > 0)
{
INT32 num = _db_skip_rows(statement, start_pos);
if (num < 0)
{
DB_PRINTF("_db_fill_window failure-error:db skip rows failure\n");
return -1;
}
else if (num < start_pos)
{
DB_PRINTF("_db_fill_window failure-error:start_pos %d > actual skip rows %d\n",start_pos, num);
return -1;
}
}
while (start_pos != 0 || num_rows < max_read)
{
INT32 i;
// DB_PRINTF("_db_fill_data_window start step row %d", start_pos + num_rows);
#if DB_GET_PERFORMANCE
t2 = db_get_ticks();
#endif
err = sqlite3_step(statement);
#if DB_GET_PERFORMANCE
if (num_rows==0)
{
t3 = db_get_ticks();
DB_PRINTF("_db_fill_window step 0 use %d\n",t3-t2);
}
#endif
if (err == SQLITE_ROW)
{
// DB_PRINTF("_db_fill_data_window Stepped row %d", start_pos + num_rows);
retry_count = 0;
// Allocate a new field directory for the row. This pointer is not reused
// since it mey be possible for it to be relocated on a call to alloc() when
// the field data is being allocated.
if (!cursor_window_alloc_row(window))
{
DB_PRINTF("_db_fill_data_window Failed allocating row at start_pos %d row %d", start_pos, num_rows);
return start_pos + num_rows + _db_finish_program_and_get_row_count(statement) + 1;
}
// Pack the row into the window
for (i = 0; i < num_columns; i++)
{
INT32 type = sqlite3_column_type(statement, i);
if (type == SQLITE_TEXT)
{
// TEXT data
#if DB_TEXT_FORMAT_UTF8
CHAR const * text = (UINT8 const *)sqlite3_column_text(statement, i);
// SQLite does not include the NULL terminator in size, but does
// ensure all strings are NULL terminated, so increase size by
// one to make sure we store the terminator.
size_t size = sqlite3_column_bytes(statement, i) + 1;
if (!cursor_window_put_string(window,i,text))
#else
WCHAR const * text = (WCHAR const *)sqlite3_column_text16(statement, i);
size_t size = sqlite3_column_bytes16(statement, i);
if (!cursor_window_put_string_16(window,i,text))
#endif
{
cursor_window_delete_last_row(window);
DB_PRINTF("_db_fill_data_window cursor_window_put_string Failed %u bytes for text/blob at col:%d", size, i);
return start_pos + num_rows + _db_finish_program_and_get_row_count(statement) + 1;
}
// DB_PRINTF("_db_fill_data_window cursor_window_put_string value:%s at col:%d", text, i);
}
else if (type == SQLITE_INTEGER)
{
// INTEGER data
INT64 value = sqlite3_column_int64(statement, i);
if (!cursor_window_put_int(window, i, value))
{
cursor_window_delete_last_row(window);
DB_PRINTF("_db_fill_data_window cursor_window_put_int fail value:%d at col:%d", value, i);
return start_pos + num_rows + _db_finish_program_and_get_row_count(statement) + 1;
}
// DB_PRINTF("_db_fill_data_window cursor_window_put_int value:%d at row:%d,col:%d", value, i);
}
else if (type == SQLITE_FLOAT)
{
// FLOAT data
double value = sqlite3_column_double(statement, i);
if (!cursor_window_put_double(window, i, value))
{
cursor_window_delete_last_row(window);
DB_PRINTF("_db_fill_data_window cursor_window_put_double fail value:%d at col:%d", value, i);
return start_pos + num_rows + _db_finish_program_and_get_row_count(statement) + 1;
}
// DB_PRINTF("_db_fill_data_window cursor_window_put_double value:%f at col:%d", value, i);
}
else if (type == SQLITE_BLOB)
{
// BLOB data
UINT8 const * blob = (UINT8 const *)sqlite3_column_blob(statement, i);
//size_t size = sqlite3_column_bytes16(statement, i); i do not know why android use utf16 funtions to store blob data
size_t size = sqlite3_column_bytes(statement, i);
if (!cursor_window_put_blob(window, i, blob,size))
{
cursor_window_delete_last_row(window);
DB_PRINTF("_db_fill_data_window cursor_window_put_blob fail at col:%d", i);
return start_pos + num_rows + _db_finish_program_and_get_row_count(statement) + 1;
}
// DB_PRINTF("_db_fill_data_window cursor_window_put_blob at col:%d", i);
}
else if (type == SQLITE_NULL)
{
// NULL field
cursor_window_put_null(window,i);
//DB_PRINTF("_db_fill_data_window cursor_window_put_null at col:%d", i);
}
else
{
// Unknown data
DB_PRINTF("_db_fill_data_window Unknown column type when filling database window");
break;
}
}
if (i < num_columns)