forked from juddmon/jpilot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibsqlite.c
More file actions
2018 lines (1729 loc) · 84.3 KB
/
libsqlite.c
File metadata and controls
2018 lines (1729 loc) · 84.3 KB
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
/* Store J-Pilot data into SQLite3 database directly whenever the data changes in J-Pilot
Elmar Klausmeier, 20-Sep-2022: Initial revision
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <sys/stat.h>
#include <sqlite3.h>
#include "log.h"
#include "address.h"
#include "datebook.h"
#include "calendar.h"
#include "memo.h"
#include "todo.h"
#include "password.h"
#include "prefs.h"
#include "libsqlite.h"
// Get bit 5 of x
#define IS_PRIVATE(x) ((((x) & 0xF0) >> 4) & 0x01)
// Check if strdup() failed: if pointer returned from SQLite is not NULL, then strdup() it and check for allocation failure
#define ALLOCN(x,y,E) { const char *s=y; if (s) { if ((s=strdup(s))!=NULL) x=(char*)s; else { sqlErr=E; goto errAlloc; } } }
// Run SQLite command/function x and store error text E
#define CHK(x,E) if ((sqlRet = x) != SQLITE_OK) { sqlErr=E; goto err; }
#define CHKDONE(x,E) if ((sqlRet = x) != SQLITE_DONE) { sqlErr=E; goto err; }
#define CHKROW(x,E) if ((sqlRet = x) != SQLITE_ROW) { sqlErr=E; goto err; }
// Replace empty strings with NULL
#define RE(x) ((x && x[0]=='\0') ? NULL : x)
static struct {
sqlite3 *conn; // file global database pointer
sqlite3_stmt // prepared SQL statements
*stmtAddrLabelSEL, *stmtAddrCategorySEL, *stmtPhoneLabelSEL,
*stmtAddrSELo1, *stmtAddrSELo2, *stmtAddrSELo3, *stmtAddrINS, *stmtAddrUPD, *stmtAddrDEL,
*stmtDatebookSEL, *stmtDatebookINS, *stmtDatebookUPD, *stmtDatebookDEL,
*stmtMemoCategorySEL, *stmtMemoSEL, *stmtMemoINS, *stmtMemoUPD, *stmtMemoDEL,
*stmtToDoCategorySEL, *stmtToDoSEL, *stmtToDoINS, *stmtToDoUPD, *stmtToDoDEL,
*stmtExpenseCategorySEL, *stmtExpenseTypeSEL, *stmtExpensePaymentSEL,
*stmtExpenseCurrencySEL,
*stmtExpenseSEL, *stmtExpenseINS, *stmtExpenseUPD, *stmtExpenseDEL,
*stmtPrefSEL, *stmtPrefDEL, *stmtPrefINS;
int maxIdAddr, maxIdDatebook, maxIdMemo, maxIdToDo, maxIdExpense;
} db;
// Copied from SQLite3 plugin
int jpsqlite_open(void) { // return database handle
char dbName[FILENAME_MAX]; // path + file-name
char sqlFile[FILENAME_MAX];
FILE *fp;
char *p0; // points to SQL string in jptables.sql
struct stat sqlFStat;
//sqlite3 *d = NULL;
int sqlRet = 0;
const char *sqlErr = "";
jp_get_home_file_name("jptables.db",dbName,FILENAME_MAX);
jp_logf(JP_LOG_DEBUG,"jpsqlite_open(): dbName=%s\n",dbName);
if (sqlite3_open_v2(dbName,&db.conn,SQLITE_OPEN_READWRITE,NULL) == SQLITE_OK)
return EXIT_SUCCESS;
// Now try to create new jptables.db by using SQL script
// in $HOME/.jpilot/plugins/jptables.sql
jp_get_home_file_name("plugins/jptables.sql",sqlFile,FILENAME_MAX);
jp_logf(JP_LOG_DEBUG,"jpsqlite_open(): sqlFile=%s\n",sqlFile);
if ( stat(sqlFile,&sqlFStat) ) {
jp_logf(JP_LOG_FATAL,
"jpsqlite_open(): Cannot stat SQL file jptables.sql in plugins directory: %s\n",
strerror(errno));
return EXIT_FAILURE;
}
if (sqlFStat.st_size == 0) {
jp_logf(JP_LOG_FATAL,"jpsqlite_open(): SQL file jptables.sql has no content\n");
return EXIT_FAILURE;
}
if ((fp = fopen(sqlFile,"r")) == NULL) {
jp_logf(JP_LOG_FATAL,
"jpsqlite_open(): Cannot open SQL file jptables.sql in plugins directory: %s\n",
strerror(errno));
return EXIT_FAILURE;
}
jp_logf(JP_LOG_DEBUG,"jpsqlite_open(): jptables.sql size=%ld\n",
sqlFStat.st_size);
if ((p0 = malloc(sqlFStat.st_size + 4)) == NULL) {
jp_logf(JP_LOG_FATAL,
"Cannot cache jptables.sql: %s\n",
strerror(errno));
return EXIT_FAILURE;
}
if (fread(p0,1,sqlFStat.st_size,fp) != sqlFStat.st_size) {
jp_logf(JP_LOG_FATAL,
"jpsqlite_open(): Cannot read jptables.sql: %s\n",
strerror(errno));
return EXIT_FAILURE;
}
p0[sqlFStat.st_size] = '\0';
// Open and create db
jp_logf(JP_LOG_GUI,"\nCreating new SQLite3 database file %s. This may take approx. 20 seconds.\n\n",dbName);
CHK(sqlite3_open(dbName,&db.conn),"IOP")
// Feed entire file content to SQLite3
CHK(sqlite3_exec(db.conn,p0,NULL,NULL,NULL),"IEXC");
free(p0);
if (fclose(fp)) {
jp_logf(JP_LOG_FATAL,
"jpsqlite_open(): Cannot close jptables.sql: %s\n", strerror(errno));
return EXIT_FAILURE;
}
jp_logf(JP_LOG_GUI,"SQLite3 database created successfully.\n");
return EXIT_SUCCESS;
err:
jp_logf(JP_LOG_FATAL,"jpsqlite_open(): SQLite3 ret=%d, error=%s, rolling back\n%s\n",
sqlRet, sqlErr, sqlite3_errmsg(db.conn));
return EXIT_FAILURE;
}
int jpsqlite_close(void) {
int sqlRet = 0;
const char *sqlErr = "";
jp_logf(JP_LOG_DEBUG,"jpsqlite_close()\n");
CHK(sqlite3_finalize(db.stmtAddrLabelSEL),"stmtAddrLabelSEL")
CHK(sqlite3_finalize(db.stmtAddrCategorySEL),"stmtAddrCategorySEL")
CHK(sqlite3_finalize(db.stmtPhoneLabelSEL),"stmtPhoneLabelSEL")
CHK(sqlite3_finalize(db.stmtAddrSELo1),"stmtAddrSELo1")
CHK(sqlite3_finalize(db.stmtAddrSELo2),"stmtAddrSELo2")
CHK(sqlite3_finalize(db.stmtAddrSELo3),"stmtAddrSELo3")
CHK(sqlite3_finalize(db.stmtAddrINS),"stmtAddrINS")
CHK(sqlite3_finalize(db.stmtAddrUPD),"stmtAddrUPD")
CHK(sqlite3_finalize(db.stmtAddrDEL),"stmtAddrDEL")
CHK(sqlite3_finalize(db.stmtDatebookSEL),"stmtDatebookSEL")
CHK(sqlite3_finalize(db.stmtDatebookINS),"stmtDatebookINS")
CHK(sqlite3_finalize(db.stmtDatebookUPD),"stmtDatebookUPD")
CHK(sqlite3_finalize(db.stmtDatebookDEL),"stmtDatebookDEL")
CHK(sqlite3_finalize(db.stmtMemoCategorySEL),"stmtMemoCategorySEL")
CHK(sqlite3_finalize(db.stmtMemoSEL),"stmtMemoSEL")
CHK(sqlite3_finalize(db.stmtMemoINS),"stmtMemoINS")
CHK(sqlite3_finalize(db.stmtMemoUPD),"stmtMemoUPD")
CHK(sqlite3_finalize(db.stmtMemoDEL),"stmtMemoDEL")
CHK(sqlite3_finalize(db.stmtToDoCategorySEL),"stmtToDoCategorySEL")
CHK(sqlite3_finalize(db.stmtToDoSEL),"stmtToDoSEL")
CHK(sqlite3_finalize(db.stmtToDoINS),"stmtToDoINS")
CHK(sqlite3_finalize(db.stmtToDoUPD),"stmtToDoUPD")
CHK(sqlite3_finalize(db.stmtToDoDEL),"stmtToDoDEL")
CHK(sqlite3_finalize(db.stmtExpenseCategorySEL),"stmtExpenseCategorySEL")
CHK(sqlite3_finalize(db.stmtExpenseTypeSEL),"stmtExpenseTypeSEL")
CHK(sqlite3_finalize(db.stmtExpensePaymentSEL),"stmtExpensePaymentSEL")
CHK(sqlite3_finalize(db.stmtExpenseCurrencySEL),"stmtExpenseCurrencySEL")
CHK(sqlite3_finalize(db.stmtExpenseSEL),"stmtExpenseSEL")
CHK(sqlite3_finalize(db.stmtExpenseINS),"stmtExpenseINS")
CHK(sqlite3_finalize(db.stmtExpenseUPD),"stmtExpenseUPD")
CHK(sqlite3_finalize(db.stmtExpenseDEL),"stmtExpenseDEL")
CHK(sqlite3_finalize(db.stmtPrefSEL),"stmtPrefSEL")
CHK(sqlite3_finalize(db.stmtPrefDEL),"stmtPrefDEL")
CHK(sqlite3_finalize(db.stmtPrefINS),"stmtPrefINS")
sqlRet = sqlite3_close(db.conn); // noop if db==NULL
if (sqlRet != SQLITE_OK) {
jp_logf(JP_LOG_FATAL,"Cannot close sqlite3 file, sqlRet=%d\n",sqlRet);
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
err:
jp_logf(JP_LOG_FATAL,"jpsqlite_close(): SQLite3 ret=%d, error=%s\n%s\n",
sqlRet, sqlErr, sqlite3_errmsg(db.conn));
sqlite3_close(db.conn); // noop if db==NULL
return EXIT_FAILURE;
}
int jpsqlite_prepareAllStmt(void) {
int sqlRet = 0;
const char *sqlErr = "";
jp_logf(JP_LOG_DEBUG,"jpsqlite_prepareAllStmt(): Start all prepared statements\n");
CHK(sqlite3_prepare_v2(db.conn,
"select Id, Label from AddrLabel order by Id",
-1, &db.stmtAddrLabelSEL, NULL), "jpAddrLabelSEL")
CHK(sqlite3_prepare_v2(db.conn,
"select Id, Label from AddrCategory order by Id",
-1, &db.stmtAddrCategorySEL, NULL), "jpAddrCategorySEL")
CHK(sqlite3_prepare_v2(db.conn,
"select Id, Label from PhoneLabel order by Id",
-1, &db.stmtPhoneLabelSEL, NULL), "jpPhoneLabelSEL")
// CATEGORY_ALL == 300
#define ADDRSELBASE \
"select Id, Category, Private, showPhone," \
" Lastname, Firstname, Title, Company," \
" PhoneLabel1, PhoneLabel2, PhoneLabel3, PhoneLabel4, PhoneLabel5," \
" Phone1, Phone2, Phone3, Phone4, Phone5," \
" Address, City, State, Zip, Country," \
" Custom1, Custom2, Custom3, Custom4, Note " \
"from Addr " \
"where (Private=:private0 or Private=:private1) " \
"and (Category=:category or 300=:category) "
CHK(sqlite3_prepare_v2(db.conn,ADDRSELBASE
"order by Lastname, Firstname, Company",
-1, &db.stmtAddrSELo1, NULL), "jpAddrSEL1")
CHK(sqlite3_prepare_v2(db.conn,ADDRSELBASE
"order by Firstname, Lastname, Company",
-1, &db.stmtAddrSELo2, NULL), "jpAddrSEL2")
CHK(sqlite3_prepare_v2(db.conn,ADDRSELBASE
"order by Company, Lastname, Firstname",
-1, &db.stmtAddrSELo3, NULL), "jpAddrSEL3")
CHK(sqlite3_prepare_v2(db.conn,
"insert into Addr ("
" Id, Category, Private, showPhone," // 1
" Lastname, Firstname, Title, Company," // 5
" PhoneLabel1, PhoneLabel2, PhoneLabel3," // 9
" PhoneLabel4, PhoneLabel5," // 12
" Phone1, Phone2, Phone3, Phone4, Phone5," // 14
" Address, City, State, Zip, Country," // 19
" Custom1, Custom2, Custom3, Custom4," // 24
" Note, InsertDate" // 28
") values ("
" :Id, :Category, :Private, :showPhone,"
" :Lastname, :Firstname, :Title, :Company,"
" :PhoneLabel1, :PhoneLabel2, :PhoneLabel3,"
" :PhoneLabel4, :PhoneLabel5,"
" :Phone1, :Phone2, :Phone3, :Phone4, :Phone5,"
" :Address, :City, :State, :Zip, :Country,"
" :Custom1, :Custom2, :Custom3, :Custom4,"
" :Note, strftime('%Y-%m-%dT%H:%M:%S', 'now')"
")",
-1, &db.stmtAddrINS, NULL), "jpAddrINS")
CHK(sqlite3_prepare_v2(db.conn,
"update Addr set"
" Category=:Category, Private=:Private, showPhone=:showPhone," // 1
" Lastname=:Lastname, Firstname=:Firstname, Title=:Title, Company=:Company," // 4
" PhoneLabel1=:PhoneLabel1, PhoneLabel2=:PhoneLabel2, PhoneLabel3=:PhoneLabel3," // 8
" PhoneLabel4=:PhoneLabel4, PhoneLabel5=:PhoneLabel5," // 11
" Phone1=:Phone1, Phone2=:Phone2, Phone3=:Phone3, Phone4=:Phone4, Phone5=:Phone5," // 13
" Address=:Address, City=:City, State=:State, Zip=:Zip, Country=:Country," // 18
" Custom1=:Custom1, Custom2=:Custom2, Custom3=:Custom3, Custom4=:Custom4," // 23
" Note=:Note, UpdateDate = strftime('%Y-%m-%dT%H:%M:%S', 'now') " // 27
"where Id = :Id", // 28
-1, &db.stmtAddrUPD, NULL), "jpAddrUPD")
CHK(sqlite3_prepare_v2(db.conn,
"delete from Addr where Id = :Id",
-1, &db.stmtAddrDEL, NULL), "jpAddrDEL")
CHK(sqlite3_prepare_v2(db.conn,
"select Id, Private, Timeless, Begin, End, Alarm," // 0
" Advance, AdvanceUnit, RepeatType, RepeatForever," // 6
" RepeatEnd, RepeatFreq, RepeatDay," // 10
" RepeatDaySu, RepeatDayMo, RepeatDayTu, RepeatDayWe," // 13
" RepeatDayTh, RepeatDayFr, RepeatDaySa," // 17
" Exceptions, Exception, Description, Note " // 20
"from Datebook "
"where (Private=0 or Private=:private1) "
"and ((Begin>=:now||'T00:00' and End<=:now||'T23:59' or RepeatType<>0) or :iNow=0) "
"order by substr(Begin,11), Begin, Id", // order by hh:mm
-1, &db.stmtDatebookSEL, NULL), "jpDatebookSEL")
CHK(sqlite3_prepare_v2(db.conn,
"insert into Datebook ("
" Id, Private, Timeless, Begin, End," // 1
" Alarm, Advance, AdvanceUnit, RepeatType," // 6
" RepeatForever, RepeatEnd, RepeatFreq," // 10
" RepeatDay," // 13
" RepeatDaySu, RepeatDayMo, RepeatDayTu," // 14
" RepeatDayWe, RepeatDayTh, RepeatDayFr," // 17
" RepeatDaySa, Exceptions, Exception," // 20
" Description, Note, InsertDate" // 23
") values ("
" :Id, :Private, :Timeless, :Begin, :End,"
" :Alarm, :Advance, :AdvanceUnit, :RepeatType,"
" :RepeatForever, :RepeatEnd, :RepeatFreq,"
" :RepeatDay,"
" :RepeatDaySu, :RepeatDayMo, :RepeatDayTu,"
" :RepeatDayWe, :RepeatDayTh, :RepeatDayFr,"
" :RepeatDaySa, :Exceptions, :Exception,"
" :Description, :Note,"
" strftime('%Y-%m-%dT%H:%M:%S', 'now')"
")",
-1, &db.stmtDatebookINS, NULL), "jpDatebookINS")
CHK(sqlite3_prepare_v2(db.conn,
"update Datebook set"
" Private=:Private, Timeless=:Timeless, Begin=:Begin, End=:End, " // 1
" Alarm=:Alarm, Advance=:Advance, AdvanceUnit=:AdvanceUnit, RepeatType=:RepeatType, " // 5
" RepeatForever=:RepeatForever, RepeatEnd=:RepeatEnd, RepeatFreq=:RepeatFreq, " // 9
" RepeatDay=:RepeatDay, " // 12
" RepeatDaySu=:RepeatDaySu, RepeatDayMo=:RepeatDayMo, RepeatDayTu=:RepeatDayTu, " // 13
" RepeatDayWe=:RepeatDayWe, RepeatDayTh=:RepeatDayTh, RepeatDayFr=:RepeatDayFr, " // 16
" RepeatDaySa=:RepeatDaySa, Exceptions=:Exceptions, Exception=:Exception, " // 19
" Description=:Description, Note=:Note, " // 22
" UpdateDate = strftime('%Y-%m-%dT%H:%M:%S', 'now') "
"where Id = :Id", // 22
-1, &db.stmtDatebookUPD, NULL), "jpDatebookUPD")
CHK(sqlite3_prepare_v2(db.conn,
"delete from Datebook where Id = :Id",
-1, &db.stmtDatebookDEL, NULL), "jpDatebookDEL")
CHK(sqlite3_prepare_v2(db.conn,
"select Id, Label from MemoCategory order by Id",
-1, &db.stmtMemoCategorySEL, NULL), "jpMemoCategorySEL")
// CATEGORY_ALL == 300
CHK(sqlite3_prepare_v2(db.conn,
"select Id, Category, Private, Text "
"from Memo "
"where (Private=:private0 or Private=:private1) "
"and (Category=:category or 300=:category) "
"order by Text",
-1, &db.stmtMemoSEL, NULL), "jpMemoSEL")
CHK(sqlite3_prepare_v2(db.conn,
"insert into Memo ("
" Id, Category, Private, Text, "
" InsertDate "
") values ("
" :Id, :Category, :Private, :Text, "
" strftime('%Y-%m-%dT%H:%M:%S', 'now') "
")",
-1, &db.stmtMemoINS, NULL), "jpMemoINS")
CHK(sqlite3_prepare_v2(db.conn,
"update Memo set"
" Category=:Category, Private=:Private, Text=:Text, "
" UpdateDate = strftime('%Y-%m-%dT%H:%M:%S', 'now') "
"where Id = :Id",
-1, &db.stmtMemoUPD, NULL), "jpMemoUPD")
CHK(sqlite3_prepare_v2(db.conn,
"delete from Memo where Id = :Id",
-1, &db.stmtMemoDEL, NULL), "jpMemoDEL")
CHK(sqlite3_prepare_v2(db.conn,
"select Id, Label from ToDoCategory order by Id",
-1, &db.stmtToDoCategorySEL, NULL), "jpToDoCategorySEL")
// CATEGORY_ALL == 300
CHK(sqlite3_prepare_v2(db.conn,
"select Id, Category, Private, Indefinite, Due,"
" Priority, Complete, Description, Note "
"from ToDo "
"where (Private=:private0 or Private=:private1) "
"and (Category=:category or 300=:category) "
"order by Priority asc, Due desc, Description asc",
-1, &db.stmtToDoSEL, NULL), "jpToDoSEL")
CHK(sqlite3_prepare_v2(db.conn,
"insert into ToDo ("
" Id, Category, Private, Indefinite, " // 1
" Due, Priority, Complete, " // 5
" Description, Note, " // 8
" InsertDate "
") values ("
" :Id, :Category, :Private, :Indefinite, "
" :Due, :Priority, :Complete, "
" :Description, :Note, "
" strftime('%Y-%m-%dT%H:%M:%S', 'now') "
")",
-1, &db.stmtToDoINS, NULL),"jpToDoINS")
CHK(sqlite3_prepare_v2(db.conn,
"update ToDo set"
" Category=:Category, Private=:Private, Indefinite=:Indefinite, " // 1
" Due=:Due, Priority=:Priority, Complete=:Complete, " // 4
" Description=:Description, Note=:Note, " // 7
" UpdateDate = strftime('%Y-%m-%dT%H:%M:%S', 'now') "
"where Id = :Id", // 9
-1, &db.stmtToDoUPD, NULL),"jpToDoUPD")
CHK(sqlite3_prepare_v2(db.conn,
"delete from ToDo where Id = :Id",
-1, &db.stmtToDoDEL, NULL), "jpToDoDEL")
CHK(sqlite3_prepare_v2(db.conn,
"select Id, Label from ExpenseCategory order by Id",
-1, &db.stmtExpenseCategorySEL, NULL), "jpExpenseCategorySEL")
CHK(sqlite3_prepare_v2(db.conn,
"select Id, Label from ExpenseType order by Id",
-1, &db.stmtExpenseTypeSEL, NULL), "jpExpenseTypeSEL")
CHK(sqlite3_prepare_v2(db.conn,
"select Id, Label from ExpensePayment order by Id",
-1, &db.stmtExpensePaymentSEL, NULL), "jpExpensePaymentSEL")
CHK(sqlite3_prepare_v2(db.conn,
"select Id, Label from ExpenseCurrency order by Id",
-1, &db.stmtExpenseCurrencySEL, NULL), "jpExpenseCurrencySEL")
CHK(sqlite3_prepare_v2(db.conn,
"select Id, Category, Date, Type, Payment, Currency,"
" Amount, Vendor, City, Attendees, Note "
"from Expense order by Id",
-1, &db.stmtExpenseSEL, NULL), "jpExpenseSEL")
CHK(sqlite3_prepare_v2(db.conn,
"insert into Expense ("
" Id, Category, Date, Type, Payment, " // 1
" Currency, Amount, Vendor, City, " // 6
" Attendees, Note, InsertDate " // 10
") values ("
" :Id, :Category, :Date, :Type, :Payment, "
" :Currency, :Amount, :Vendor, :City, "
" :Attendees, :Note, "
" strftime('%Y-%m-%dT%H:%M:%S', 'now') "
")",
-1, &db.stmtExpenseINS, NULL), "jpExpenseINS")
CHK(sqlite3_prepare_v2(db.conn,
"update Expense set"
" Category=:Category, Date=:Date, Type=:Type, Payment=:Payment, " // 1
" Currency=:Currency, Amount=:Amount, Vendor=:Vendor, City=:City, " // 5
" Attendees=:Attendees, Note=:Note, " // 9
" UpdateDate = strftime('%Y-%m-%dT%H:%M:%S', 'now') "
"where Id = :Id", // 11
-1, &db.stmtExpenseUPD, NULL), "jpExpenseUPD")
CHK(sqlite3_prepare_v2(db.conn,
"delete from Expense where Id = :Id",
-1, &db.stmtExpenseDEL, NULL), "jpExpenseDEL")
CHK(sqlite3_prepare_v2(db.conn,
"select Id, Name, Usertype, Filetype, iValue, sValue from Pref "
"order by Id, Name",
-1, &db.stmtPrefSEL, NULL), "jpPrefSEL")
CHK(sqlite3_prepare_v2(db.conn, "delete from Pref",
-1, &db.stmtPrefDEL, NULL), "jpPrefDEL")
CHK(sqlite3_prepare_v2(db.conn,
"insert into Pref ("
" Id, Name, Usertype, Filetype, iValue, sValue, InsertDate"
") values ("
" :Id, :Name, :Usertype, :Filetype, :iValue, :sValue, strftime('%Y-%m-%dT%H:%M:%S', 'now'))",
-1, &db.stmtPrefINS, NULL), "jpPrefINS")
jp_logf(JP_LOG_DEBUG,"jpsqlite_prepareAllStmt(): Finished all prepared statements\n");
// Read 1+max(Id) from various tables, which might later get INSERT's
sqlite3_stmt *stmtMaxIdSEL;
CHK(sqlite3_prepare_v2(db.conn,
"select"
" 1 + (select coalesce(max(Id),0) from Addr),"
" 1 + (select coalesce(max(Id),0) from Datebook),"
" 1 + (select coalesce(max(Id),0) from Memo),"
" 1 + (select coalesce(max(Id),0) from ToDo),"
" 1 + (select coalesce(max(Id),0) from Expense)",
-1, &stmtMaxIdSEL, NULL), "jpMaxIdSEL")
sqlRet = sqlite3_step(stmtMaxIdSEL);
if (sqlRet != SQLITE_ROW) goto err;
db.maxIdAddr = sqlite3_column_int(stmtMaxIdSEL,0);
db.maxIdDatebook = sqlite3_column_int(stmtMaxIdSEL,1);
db.maxIdMemo = sqlite3_column_int(stmtMaxIdSEL,2);
db.maxIdToDo = sqlite3_column_int(stmtMaxIdSEL,3);
db.maxIdExpense = sqlite3_column_int(stmtMaxIdSEL,4);
sqlite3_finalize(stmtMaxIdSEL);
jp_logf(JP_LOG_DEBUG,"jpsqlite_prepareAllStmt(): maxIdAddr=%d, maxIdDatebook=%d, maxIdMemo=%d, maxIdToDo=%d, maxIdExpense=%d\n",
db.maxIdAddr, db.maxIdDatebook, db.maxIdMemo, db.maxIdToDo, db.maxIdExpense);
return EXIT_SUCCESS;
err:
jp_logf(JP_LOG_FATAL,"jpsqlite_prepareAllStmt(): ret=%d, error=%s, giving up\n%s\n",
sqlRet, sqlErr, sqlite3_errmsg(db.conn));
//sqlite3_finalize(db.stmtXXX); // We cannot really do much here
//sqlite3_exec(db.conn,"ROLLBACK TRANSACTION",NULL,NULL,NULL);
return EXIT_FAILURE;
}
int jpsqlite_AddrUPD(struct Address *addr, PCRecType rt, unsigned char attrib, unsigned int *unique_id) {
int sqlRet = 0, errId;
const char *sqlErr = "";
jp_logf(JP_LOG_DEBUG,"jpsqlite_AddrUPD(): rt=%d, category=%d, unique_id=%u\n",rt,attrib&0x0F,*unique_id);
errId = *unique_id;
CHK(sqlite3_bind_int(db.stmtAddrUPD,1,attrib & 0x0F),"AddrUpdB1")
CHK(sqlite3_bind_int(db.stmtAddrUPD,2,IS_PRIVATE(attrib)),"AddrUpdB2")
CHK(sqlite3_bind_int(db.stmtAddrUPD,3,addr->showPhone),"AddrUpdB3")
CHK(sqlite3_bind_text(db.stmtAddrUPD,4,RE(addr->entry[entryLastname]),-1,SQLITE_STATIC),"AddrUpdB4")
CHK(sqlite3_bind_text(db.stmtAddrUPD,5,RE(addr->entry[entryFirstname]),-1,SQLITE_STATIC),"AddrUpdB5")
CHK(sqlite3_bind_text(db.stmtAddrUPD,6,RE(addr->entry[entryTitle]),-1,SQLITE_STATIC),"AddrUpdB6")
CHK(sqlite3_bind_text(db.stmtAddrUPD,7,RE(addr->entry[entryCompany]),-1,SQLITE_STATIC),"AddrUpdB7")
CHK(sqlite3_bind_int(db.stmtAddrUPD,8,addr->phoneLabel[0]),"AddrUpdB8")
CHK(sqlite3_bind_int(db.stmtAddrUPD,9,addr->phoneLabel[1]),"AddrUpdB9")
CHK(sqlite3_bind_int(db.stmtAddrUPD,10,addr->phoneLabel[2]),"AddrUpdB10")
CHK(sqlite3_bind_int(db.stmtAddrUPD,11,addr->phoneLabel[3]),"AddrUpdB11")
CHK(sqlite3_bind_int(db.stmtAddrUPD,12,addr->phoneLabel[4]),"AddrUpdB12")
CHK(sqlite3_bind_text(db.stmtAddrUPD,13,RE(addr->entry[entryPhone1]),-1,SQLITE_STATIC),"AddrUpdB13")
CHK(sqlite3_bind_text(db.stmtAddrUPD,14,RE(addr->entry[entryPhone2]),-1,SQLITE_STATIC),"AddrUpdB14")
CHK(sqlite3_bind_text(db.stmtAddrUPD,15,RE(addr->entry[entryPhone3]),-1,SQLITE_STATIC),"AddrUpdB15")
CHK(sqlite3_bind_text(db.stmtAddrUPD,16,RE(addr->entry[entryPhone4]),-1,SQLITE_STATIC),"AddrUpdB16")
CHK(sqlite3_bind_text(db.stmtAddrUPD,17,RE(addr->entry[entryPhone5]),-1,SQLITE_STATIC),"AddrUpdB17")
CHK(sqlite3_bind_text(db.stmtAddrUPD,18,RE(addr->entry[entryAddress]),-1,SQLITE_STATIC),"AddrUpdB18")
CHK(sqlite3_bind_text(db.stmtAddrUPD,19,RE(addr->entry[entryCity]),-1,SQLITE_STATIC),"AddrUpdB19")
CHK(sqlite3_bind_text(db.stmtAddrUPD,20,RE(addr->entry[entryState]),-1,SQLITE_STATIC),"AddrUpdB20")
CHK(sqlite3_bind_text(db.stmtAddrUPD,21,RE(addr->entry[entryZip]),-1,SQLITE_STATIC),"AddrUpdB21")
CHK(sqlite3_bind_text(db.stmtAddrUPD,22,RE(addr->entry[entryCountry]),-1,SQLITE_STATIC),"AddrUpdB22")
CHK(sqlite3_bind_text(db.stmtAddrUPD,23,RE(addr->entry[entryCustom1]),-1,SQLITE_STATIC),"AddrUpdB23")
CHK(sqlite3_bind_text(db.stmtAddrUPD,24,RE(addr->entry[entryCustom2]),-1,SQLITE_STATIC),"AddrUpdB24")
CHK(sqlite3_bind_text(db.stmtAddrUPD,25,RE(addr->entry[entryCustom3]),-1,SQLITE_STATIC),"AddrUpdB25")
CHK(sqlite3_bind_text(db.stmtAddrUPD,26,RE(addr->entry[entryCustom4]),-1,SQLITE_STATIC),"AddrUpdB26")
CHK(sqlite3_bind_text(db.stmtAddrUPD,27,RE(addr->entry[entryNote]),-1,SQLITE_STATIC),"AddrUpdB27")
CHK(sqlite3_bind_int(db.stmtAddrUPD,28,*unique_id),"AddrUpdB28")
CHKDONE(sqlite3_step(db.stmtAddrUPD),"AddrUpdST")
CHK(sqlite3_clear_bindings(db.stmtAddrUPD),"AddrUpdCL")
CHK(sqlite3_reset(db.stmtAddrUPD),"AddrUpdRST")
return EXIT_SUCCESS;
err:
jp_logf(JP_LOG_FATAL,"jpsqlite_AddrINS(): ret=%d, error=%s, Id=%d, rolling back\n%s\n",
sqlRet, sqlErr, errId, sqlite3_errmsg(db.conn));
sqlite3_finalize(db.stmtAddrUPD);
//sqlite3_exec(db.conn,"ROLLBACK TRANSACTION",NULL,NULL,NULL);
return EXIT_FAILURE;
}
int jpsqlite_AddrINS(struct Address *addr, PCRecType rt, unsigned char attrib, unsigned int *unique_id) {
int sqlRet = 0, errId;
const char *sqlErr = "";
if (rt == REPLACEMENT_PALM_REC)
return jpsqlite_AddrUPD(addr,rt,attrib,unique_id);
jp_logf(JP_LOG_DEBUG,"jpsqlite_AddrINS(): rt=%d, category=%d, unique_id=%u, maxIdAddr=%d\n",rt,attrib&0x0F,unique_id?*unique_id:(-1),db.maxIdAddr);
errId = db.maxIdAddr;
if (unique_id) *unique_id = db.maxIdAddr;
CHK(sqlite3_bind_int(db.stmtAddrINS,1,db.maxIdAddr),"AddrInsB1")
CHK(sqlite3_bind_int(db.stmtAddrINS,2,attrib & 0x0F),"AddrInsB2")
CHK(sqlite3_bind_int(db.stmtAddrINS,3,IS_PRIVATE(attrib)),"AddrInsB3")
CHK(sqlite3_bind_int(db.stmtAddrINS,4,addr->showPhone),"AddrInsB4")
CHK(sqlite3_bind_text(db.stmtAddrINS,5,RE(addr->entry[entryLastname]),-1,SQLITE_STATIC),"AddrInsB5")
CHK(sqlite3_bind_text(db.stmtAddrINS,6,RE(addr->entry[entryFirstname]),-1,SQLITE_STATIC),"AddrInsB6")
CHK(sqlite3_bind_text(db.stmtAddrINS,7,RE(addr->entry[entryTitle]),-1,SQLITE_STATIC),"AddrInsB7")
CHK(sqlite3_bind_text(db.stmtAddrINS,8,RE(addr->entry[entryCompany]),-1,SQLITE_STATIC),"AddrInsB8")
CHK(sqlite3_bind_int(db.stmtAddrINS,9,addr->phoneLabel[0]),"AddrInsB9")
CHK(sqlite3_bind_int(db.stmtAddrINS,10,addr->phoneLabel[1]),"AddrInsB10")
CHK(sqlite3_bind_int(db.stmtAddrINS,11,addr->phoneLabel[2]),"AddrInsB11")
CHK(sqlite3_bind_int(db.stmtAddrINS,12,addr->phoneLabel[3]),"AddrInsB12")
CHK(sqlite3_bind_int(db.stmtAddrINS,13,addr->phoneLabel[4]),"AddrInsB13")
CHK(sqlite3_bind_text(db.stmtAddrINS,14,RE(addr->entry[entryPhone1]),-1,SQLITE_STATIC),"AddrInsB14")
CHK(sqlite3_bind_text(db.stmtAddrINS,15,RE(addr->entry[entryPhone2]),-1,SQLITE_STATIC),"AddrInsB15")
CHK(sqlite3_bind_text(db.stmtAddrINS,16,RE(addr->entry[entryPhone3]),-1,SQLITE_STATIC),"AddrInsB16")
CHK(sqlite3_bind_text(db.stmtAddrINS,17,RE(addr->entry[entryPhone4]),-1,SQLITE_STATIC),"AddrInsB17")
CHK(sqlite3_bind_text(db.stmtAddrINS,18,RE(addr->entry[entryPhone5]),-1,SQLITE_STATIC),"AddrInsB18")
CHK(sqlite3_bind_text(db.stmtAddrINS,19,RE(addr->entry[entryAddress]),-1,SQLITE_STATIC),"AddrInsB19")
CHK(sqlite3_bind_text(db.stmtAddrINS,20,RE(addr->entry[entryCity]),-1,SQLITE_STATIC),"AddrInsB20")
CHK(sqlite3_bind_text(db.stmtAddrINS,21,RE(addr->entry[entryState]),-1,SQLITE_STATIC),"AddrInsB21")
CHK(sqlite3_bind_text(db.stmtAddrINS,22,RE(addr->entry[entryZip]),-1,SQLITE_STATIC),"AddrInsB22")
CHK(sqlite3_bind_text(db.stmtAddrINS,23,RE(addr->entry[entryCountry]),-1,SQLITE_STATIC),"AddrInsB23")
CHK(sqlite3_bind_text(db.stmtAddrINS,24,RE(addr->entry[entryCustom1]),-1,SQLITE_STATIC),"AddrInsB24")
CHK(sqlite3_bind_text(db.stmtAddrINS,25,RE(addr->entry[entryCustom2]),-1,SQLITE_STATIC),"AddrInsB24")
CHK(sqlite3_bind_text(db.stmtAddrINS,26,RE(addr->entry[entryCustom3]),-1,SQLITE_STATIC),"AddrInsB26")
CHK(sqlite3_bind_text(db.stmtAddrINS,27,RE(addr->entry[entryCustom4]),-1,SQLITE_STATIC),"AddrInsB27")
CHK(sqlite3_bind_text(db.stmtAddrINS,28,RE(addr->entry[entryNote]),-1,SQLITE_STATIC),"AddrInsB28")
CHKDONE(sqlite3_step(db.stmtAddrINS),"AddrInsST")
db.maxIdAddr += 1;
CHK(sqlite3_clear_bindings(db.stmtAddrINS),"AddrInsCL")
CHK(sqlite3_reset(db.stmtAddrINS),"AddrInsRST")
return EXIT_SUCCESS;
err:
jp_logf(JP_LOG_FATAL,"jpsqlite_AddrINS(): ret=%d, error=%s, Id=%d, rolling back\n%s\n",
sqlRet, sqlErr, errId, sqlite3_errmsg(db.conn));
sqlite3_finalize(db.stmtAddrINS);
//sqlite3_exec(db.conn,"ROLLBACK TRANSACTION",NULL,NULL,NULL);
return EXIT_FAILURE;
}
int jpsqlite_DatebookUPD(struct CalendarEvent *cale, PCRecType rt, unsigned char attrib, unsigned int *unique_id) {
int sqlRet = 0, errId, i, offset, nRealExcp, repeatForever;
const char *sqlErr = "";
char begin[32], end[32], repeatEnd[32], *pRepeatEnd, exceptionString[2048];
jp_logf(JP_LOG_DEBUG,"jpsqlite_DatebookUPD(): rt=%d, unique_id=%u\n",rt,*unique_id);
errId = *unique_id;
strftime(begin,32,"%FT%R",&cale->begin);
strftime(end,32,"%FT%R",&cale->end);
// Data cleansing
if (cale->repeatEnd.tm_year < 2 || cale->repeatEnd.tm_year > 2050
|| cale->repeatEnd.tm_year == 70 && cale->repeatEnd.tm_mon == 0 && cale->repeatEnd.tm_mday == 1) // 01-Jan-1970
pRepeatEnd = NULL;
else
strftime(pRepeatEnd=repeatEnd,32,"%F",&cale->repeatEnd);
CHK(sqlite3_bind_int(db.stmtDatebookUPD,1,IS_PRIVATE(attrib)),"DatebookUpdB1")
CHK(sqlite3_bind_int(db.stmtDatebookUPD,2,cale->event),"DatebookUpdB2")
CHK(sqlite3_bind_text(db.stmtDatebookUPD,3,begin,-1,SQLITE_STATIC),"DatebookUpdB3")
CHK(sqlite3_bind_text(db.stmtDatebookUPD,4,end,-1,SQLITE_STATIC),"DatebookUpdB4")
CHK(sqlite3_bind_int(db.stmtDatebookUPD,5,cale->alarm),"DatebookUpdB5")
CHK(sqlite3_bind_int(db.stmtDatebookUPD,6,cale->advance),"DatebookUpdB6")
CHK(sqlite3_bind_int(db.stmtDatebookUPD,7,cale->advanceUnits),"DatebookUpdB7")
CHK(sqlite3_bind_int(db.stmtDatebookUPD,8,cale->repeatType),"DatebookUpdB8")
// repeatForever is stored in inverse format as it is used in J-Pilot
// in SQLite: repeatForever=1: repeating event, repeatForever=0: non-repeating
// old: 1 - (cale->repeatForever & 0x01)
// pRepeatEnd && (cale->repeatEnd.tm_year*366 + cale->repeatEnd.tm_yday > cale->begin.tm_year*366 + cale->begin.tm_yday)
// && pRepeatEnd == NULL
repeatForever = cale->repeatForever & 0x01;
if (cale->repeatFrequency == 0 && cale->repeatDay == 0 && cale->repeatDays[0] == 0
&& cale->repeatDays[1] == 0 && cale->repeatDays[2] == 0 && cale->repeatDays[3] == 0
&& cale->repeatDays[4] == 0 && cale->repeatDays[5] == 0 && cale->repeatDays[6] == 0
|| cale->repeatFrequency && pRepeatEnd == NULL)
repeatForever = 1;
else if (pRepeatEnd && cale->repeatEnd.tm_year*366 + cale->repeatEnd.tm_yday > cale->end.tm_year*366 + cale->end.tm_yday)
repeatForever = 0;
CHK(sqlite3_bind_int(db.stmtDatebookUPD,9,repeatForever),"DatebookUpdB9")
CHK(sqlite3_bind_text(db.stmtDatebookUPD,10,pRepeatEnd,-1,SQLITE_STATIC),"DatebookUpdB10")
CHK(sqlite3_bind_int(db.stmtDatebookUPD,11,cale->repeatFrequency),"DatebookUpdB11")
CHK(sqlite3_bind_int(db.stmtDatebookUPD,12,cale->repeatDay),"DatebookUpdB12")
CHK(sqlite3_bind_int(db.stmtDatebookUPD,13,cale->repeatDays[0]),"DatebookUpdB13")
CHK(sqlite3_bind_int(db.stmtDatebookUPD,14,cale->repeatDays[1]),"DatebookUpdB14")
CHK(sqlite3_bind_int(db.stmtDatebookUPD,15,cale->repeatDays[2]),"DatebookUpdB15")
CHK(sqlite3_bind_int(db.stmtDatebookUPD,16,cale->repeatDays[3]),"DatebookUpdB16")
CHK(sqlite3_bind_int(db.stmtDatebookUPD,17,cale->repeatDays[4]),"DatebookUpdB17")
CHK(sqlite3_bind_int(db.stmtDatebookUPD,18,cale->repeatDays[5]),"DatebookUpdB18")
CHK(sqlite3_bind_int(db.stmtDatebookUPD,19,cale->repeatDays[6]),"DatebookUpdB19")
// Data cleansing: remove 1900-00-01 tm's
for (nRealExcp=0,i=0; i<cale->exceptions; ++i)
if (cale->exception[i].tm_year != 0) ++nRealExcp;
CHK(sqlite3_bind_int(db.stmtDatebookUPD,20,nRealExcp),"DatebookUpdB20")
if (nRealExcp * 11 > sizeof(exceptionString)) {
jp_logf(JP_LOG_FATAL,"jpsqlite_DatebookUPD(): too many exceptions");
exceptionString[0] = '\0';
} else {
for (i=0,offset=0; i<cale->exceptions; ++i) {
if (cale->exception[i].tm_year == 0) continue; // drop 1900-00-01 tm's
strftime(exceptionString+offset,11,"%F",cale->exception+i);
if (offset > 0) exceptionString[offset-1] = ' '; // change previous '\0' to space
offset += 11;
}
}
CHK(sqlite3_bind_text(db.stmtDatebookUPD,21,nRealExcp ? exceptionString : NULL,-1,SQLITE_STATIC),"DatebookUpdB21")
CHK(sqlite3_bind_text(db.stmtDatebookUPD,22,cale->description,-1,SQLITE_STATIC),"DatebookUpdB22")
CHK(sqlite3_bind_text(db.stmtDatebookUPD,23,cale->note,-1,SQLITE_STATIC),"DatebookUpdB23")
CHK(sqlite3_bind_int(db.stmtDatebookUPD,24,*unique_id),"DatebookUpdB24")
CHKDONE(sqlite3_step(db.stmtDatebookUPD),"DatebookUpdST")
db.maxIdDatebook += 1;
CHK(sqlite3_clear_bindings(db.stmtDatebookUPD),"DatebookUpdCL")
CHK(sqlite3_reset(db.stmtDatebookUPD),"DatebookUpdRST")
return EXIT_SUCCESS;
err:
jp_logf(JP_LOG_FATAL,"jpsqlite_DatebookUPD(): ret=%d, error=%s, Id=%d, rolling back\n%s\n",
sqlRet, sqlErr, errId, sqlite3_errmsg(db.conn));
sqlite3_finalize(db.stmtDatebookUPD);
//sqlite3_exec(db.conn,"ROLLBACK TRANSACTION",NULL,NULL,NULL);
return EXIT_FAILURE;
}
int jpsqlite_DatebookINS(struct CalendarEvent *cale, PCRecType rt, unsigned char attrib, unsigned int *unique_id) {
int sqlRet = 0, errId, i, offset, nRealExcp, repeatForever;
const char *sqlErr = "";
char begin[32], end[32], repeatEnd[32], *pRepeatEnd, exceptionString[2048];
jp_logf(JP_LOG_DEBUG,"jpsqlite_DatebookINS(): rt=%d, unique_id=%u, maxIdDatebook=%d\n",rt,unique_id?*unique_id:(-1),db.maxIdDatebook);
errId = db.maxIdDatebook; // ignore provided unique_id, instead get it from database
if (unique_id) *unique_id = db.maxIdDatebook;
strftime(begin,32,"%FT%R",&cale->begin);
strftime(end,32,"%FT%R",&cale->end);
// Data cleansing
if (cale->repeatEnd.tm_year < 2 || cale->repeatEnd.tm_year > 2050
|| cale->repeatEnd.tm_year == 70 && cale->repeatEnd.tm_mon == 0 && cale->repeatEnd.tm_mday == 1) // 01-Jan-1970
pRepeatEnd = NULL;
else
strftime(pRepeatEnd=repeatEnd,32,"%F",&cale->repeatEnd);
CHK(sqlite3_bind_int(db.stmtDatebookINS,1,db.maxIdDatebook),"DatebookInsB1")
CHK(sqlite3_bind_int(db.stmtDatebookINS,2,IS_PRIVATE(attrib)),"DatebookInsB2")
CHK(sqlite3_bind_int(db.stmtDatebookINS,3,cale->event),"DatebookInsB3")
CHK(sqlite3_bind_text(db.stmtDatebookINS,4,begin,-1,SQLITE_STATIC),"DatebookInsB4")
CHK(sqlite3_bind_text(db.stmtDatebookINS,5,end,-1,SQLITE_STATIC),"DatebookInsB5")
CHK(sqlite3_bind_int(db.stmtDatebookINS,6,cale->alarm),"DatebookInsB6")
CHK(sqlite3_bind_int(db.stmtDatebookINS,7,cale->advance),"DatebookInsB7")
CHK(sqlite3_bind_int(db.stmtDatebookINS,8,cale->advanceUnits),"DatebookInsB8")
CHK(sqlite3_bind_int(db.stmtDatebookINS,9,cale->repeatType),"DatebookInsB9")
// old: 1 - (cale->repeatForever & 0x01)
// pRepeatEnd && (cale->repeatEnd.tm_year*366 + cale->repeatEnd.tm_yday > cale->begin.tm_year*366 + cale->begin.tm_yday)
// && pRepeatEnd == NULL
if (cale->repeatFrequency == 0 && cale->repeatDay == 0 && cale->repeatDays[0] == 0
&& cale->repeatDays[1] == 0 && cale->repeatDays[2] == 0 && cale->repeatDays[3] == 0
&& cale->repeatDays[4] == 0 && cale->repeatDays[5] == 0 && cale->repeatDays[6] == 0
|| cale->repeatFrequency && pRepeatEnd == NULL)
repeatForever = 1;
else if (pRepeatEnd && cale->repeatEnd.tm_year*366 + cale->repeatEnd.tm_yday > cale->end.tm_year*366 + cale->end.tm_yday)
repeatForever = 0;
CHK(sqlite3_bind_int(db.stmtDatebookINS,10,repeatForever),"DatebookInsB10")
CHK(sqlite3_bind_text(db.stmtDatebookINS,11,pRepeatEnd,-1,SQLITE_STATIC),"DatebookInsB11")
CHK(sqlite3_bind_int(db.stmtDatebookINS,12,cale->repeatFrequency),"DatebookInsB12")
CHK(sqlite3_bind_int(db.stmtDatebookINS,13,cale->repeatDay),"DatebookInsB13")
CHK(sqlite3_bind_int(db.stmtDatebookINS,14,cale->repeatDays[0]),"DatebookInsB14")
CHK(sqlite3_bind_int(db.stmtDatebookINS,15,cale->repeatDays[1]),"DatebookInsB15")
CHK(sqlite3_bind_int(db.stmtDatebookINS,16,cale->repeatDays[2]),"DatebookInsB16")
CHK(sqlite3_bind_int(db.stmtDatebookINS,17,cale->repeatDays[3]),"DatebookInsB17")
CHK(sqlite3_bind_int(db.stmtDatebookINS,18,cale->repeatDays[4]),"DatebookInsB18")
CHK(sqlite3_bind_int(db.stmtDatebookINS,19,cale->repeatDays[5]),"DatebookInsB19")
CHK(sqlite3_bind_int(db.stmtDatebookINS,20,cale->repeatDays[6]),"DatebookInsB20")
// Data cleansing: remove 1900-00-01 tm's
for (nRealExcp=0,i=0; i<cale->exceptions; ++i)
if (cale->exception[i].tm_year != 0) ++nRealExcp;
CHK(sqlite3_bind_int(db.stmtDatebookINS,21,nRealExcp),"DatebookInsB21")
if (nRealExcp * 11 > sizeof(exceptionString)) {
jp_logf(JP_LOG_FATAL,"jpsqlite_DatebookINS(): too many exceptions");
exceptionString[0] = '\0';
} else {
for (i=0,offset=0; i<cale->exceptions; ++i) {
if (cale->exception[i].tm_year == 0) continue; // drop 1900-00-01 tm's
strftime(exceptionString+offset,11,"%F",cale->exception+i);
if (offset > 0) exceptionString[offset-1] = ' '; // change previous '\0' to space
offset += 11;
}
}
CHK(sqlite3_bind_text(db.stmtDatebookINS,22,nRealExcp ? exceptionString : NULL,-1,SQLITE_STATIC),"DatebookInsB22")
CHK(sqlite3_bind_text(db.stmtDatebookINS,23,cale->description,-1,SQLITE_STATIC),"DatebookInsB23")
CHK(sqlite3_bind_text(db.stmtDatebookINS,24,cale->note,-1,SQLITE_STATIC),"DatebookInsB24")
CHKDONE(sqlite3_step(db.stmtDatebookINS),"DatebookInsST")
db.maxIdDatebook += 1;
CHK(sqlite3_clear_bindings(db.stmtDatebookINS),"DatebookInsCL")
CHK(sqlite3_reset(db.stmtDatebookINS),"DatebookInsRST")
return EXIT_SUCCESS;
err:
jp_logf(JP_LOG_FATAL,"jpsqlite_DatebookINS(): ret=%d, error=%s, Id=%d, rolling back\n%s\n",
sqlRet, sqlErr, errId, sqlite3_errmsg(db.conn));
sqlite3_finalize(db.stmtDatebookINS);
//sqlite3_exec(db.conn,"ROLLBACK TRANSACTION",NULL,NULL,NULL);
return EXIT_FAILURE;
}
int jpsqlite_MemoUPD(struct Memo *memo, PCRecType rt, unsigned char attrib, unsigned int *unique_id) {
int sqlRet = 0, errId;
const char *sqlErr = "";
jp_logf(JP_LOG_DEBUG,"jpsqlite_MemoUPD(): rt=%d, category=%d, unique_id=%u\n",rt,attrib&0X0F,*unique_id);
CHK(sqlite3_bind_int(db.stmtMemoUPD,1,attrib & 0x0F),"MemoUpdB1")
CHK(sqlite3_bind_int(db.stmtMemoUPD,2,IS_PRIVATE(attrib)),"MemoUpdB2")
CHK(sqlite3_bind_text(db.stmtMemoUPD,3,memo->text,-1,SQLITE_STATIC),"MemoUpdB3")
CHK(sqlite3_bind_int(db.stmtMemoUPD,4,*unique_id),"MemoUpdB4")
CHKDONE(sqlite3_step(db.stmtMemoUPD),"MemoUpdST")
CHK(sqlite3_clear_bindings(db.stmtMemoUPD),"MemoUpdCL")
CHK(sqlite3_reset(db.stmtMemoUPD),"MemoUpdRST")
return EXIT_SUCCESS;
err:
jp_logf(JP_LOG_FATAL,"jpsqlite_MemoUPD(): ret=%d, error=%s, Id=%d, rolling back\n%s\n",
sqlRet, sqlErr, errId, sqlite3_errmsg(db.conn));
sqlite3_finalize(db.stmtMemoUPD);
//sqlite3_exec(db.conn,"ROLLBACK TRANSACTION",NULL,NULL,NULL);
return EXIT_FAILURE;
}
int jpsqlite_MemoINS(struct Memo *memo, PCRecType rt, unsigned char attrib, unsigned int *unique_id) {
int sqlRet = 0, errId;
const char *sqlErr = "";
jp_logf(JP_LOG_DEBUG,"jpsqlite_MemoINS(): rt=%d, category=%d, unique_id=%u, maxIdMemo=%d\n",rt,attrib&0X0F,unique_id?*unique_id:(-1),db.maxIdMemo);
errId = db.maxIdMemo;
if (unique_id) *unique_id = db.maxIdMemo;
CHK(sqlite3_bind_int(db.stmtMemoINS,1,db.maxIdMemo),"MemoInsB1")
CHK(sqlite3_bind_int(db.stmtMemoINS,2,attrib & 0x0F),"MemoInsB2")
CHK(sqlite3_bind_int(db.stmtMemoINS,3,IS_PRIVATE(attrib)),"MemoInsB3")
CHK(sqlite3_bind_text(db.stmtMemoINS,4,memo->text,-1,SQLITE_STATIC),"MemoInsB4")
CHKDONE(sqlite3_step(db.stmtMemoINS),"MemoInsST")
db.maxIdMemo += 1;
CHK(sqlite3_clear_bindings(db.stmtMemoINS),"MemoInsCL")
CHK(sqlite3_reset(db.stmtMemoINS),"MemoInsRST")
return EXIT_SUCCESS;
err:
jp_logf(JP_LOG_FATAL,"jpsqlite_MemoINS(): ret=%d, error=%s, Id=%d, rolling back\n%s\n",
sqlRet, sqlErr, errId, sqlite3_errmsg(db.conn));
sqlite3_finalize(db.stmtMemoINS);
//sqlite3_exec(db.conn,"ROLLBACK TRANSACTION",NULL,NULL,NULL);
return EXIT_FAILURE;
}
int jpsqlite_ToDoUPD(struct ToDo *todo, PCRecType rt, unsigned char attrib, unsigned int *unique_id) {
int sqlRet = 0, errId;
const char *sqlErr = "";
char due[32];
jp_logf(JP_LOG_DEBUG,"jpsqlite_ToDoUPD(): rt=%d, category=%d, unique_id=%u\n",rt,attrib&0X0F,*unique_id);
errId = *unique_id;
//if (rt != PALM_REC && rt != NEW_PC_REC && rt != REPLACEMENT_PALM_REC)
jp_logf(JP_LOG_DEBUG,"jpsqlite_ToDoUPD(): unique_id=%u rt=%d, category=%d |%.60s|\n",
*unique_id, rt, attrib & 0x0F, todo->description);
strftime(due,32,"%F",&todo->due);
CHK(sqlite3_bind_int(db.stmtToDoUPD,1,attrib & 0x0F),"ToDoUpdB1")
CHK(sqlite3_bind_int(db.stmtToDoUPD,2,IS_PRIVATE(attrib)),"ToDoUpdB2")
CHK(sqlite3_bind_int(db.stmtToDoUPD,3,todo->indefinite),"ToDoUpdB3")
CHK(sqlite3_bind_text(db.stmtToDoUPD,4,due,-1,SQLITE_STATIC),"ToDoUpdB4")
CHK(sqlite3_bind_int(db.stmtToDoUPD,5,todo->priority),"ToDoUpdB5")
CHK(sqlite3_bind_int(db.stmtToDoUPD,6,todo->complete),"ToDoUpdB6")
CHK(sqlite3_bind_text(db.stmtToDoUPD,7,todo->description,-1,SQLITE_STATIC),"ToDoUpdB7")
CHK(sqlite3_bind_text(db.stmtToDoUPD,8,todo->note,-1,SQLITE_STATIC),"ToDoUpdB8")
CHK(sqlite3_bind_int(db.stmtToDoUPD,9,*unique_id),"ToDoUpdB9")
CHKDONE(sqlite3_step(db.stmtToDoUPD),"ToDoUpdST")
CHK(sqlite3_clear_bindings(db.stmtToDoUPD),"ToDoUpdCL")
CHK(sqlite3_reset(db.stmtToDoUPD),"ToDoUpdRST")
return EXIT_SUCCESS;
err:
jp_logf(JP_LOG_FATAL,"jpsqlite_ToDoUPD(): ret=%d, error=%s, Id=%d, rolling back\n%s\n",
sqlRet, sqlErr, errId, sqlite3_errmsg(db.conn));
sqlite3_finalize(db.stmtToDoUPD);
//sqlite3_exec(db.conn,"ROLLBACK TRANSACTION",NULL,NULL,NULL);
return EXIT_FAILURE;
}
int jpsqlite_ToDoINS(struct ToDo *todo, PCRecType rt, unsigned char attrib, unsigned int *unique_id) {
int sqlRet = 0, errId;
const char *sqlErr = "";
char due[32];
jp_logf(JP_LOG_DEBUG,"jpsqlite_ToDoINS(): rt=%d, category=%d, unique_id=%u, maxIdToDo=%d\n",rt,attrib&0X0F,unique_id?*unique_id:(-1),db.maxIdToDo);
errId = db.maxIdToDo;
if (unique_id) *unique_id = db.maxIdToDo;
if (rt != PALM_REC
&& rt != NEW_PC_REC
&& rt != REPLACEMENT_PALM_REC)
jp_logf(JP_LOG_DEBUG,"jpsqlite_ToDoINS(): unique_id=%u rt=%d, category=%d |%.60s|\n",
*unique_id, rt, attrib & 0x0F, todo->description);
strftime(due,32,"%F",&todo->due);
CHK(sqlite3_bind_int(db.stmtToDoINS,1,db.maxIdToDo),"ToDoInsB1")
CHK(sqlite3_bind_int(db.stmtToDoINS,2,attrib & 0x0F),"ToDoInsB2")
CHK(sqlite3_bind_int(db.stmtToDoINS,3,IS_PRIVATE(attrib)),"ToDoInsB3")
CHK(sqlite3_bind_int(db.stmtToDoINS,4,todo->indefinite),"ToDoInsB4")
CHK(sqlite3_bind_text(db.stmtToDoINS,5,due,-1,SQLITE_STATIC),"ToDoInsB5")
CHK(sqlite3_bind_int(db.stmtToDoINS,6,todo->priority),"ToDoInsB6")
CHK(sqlite3_bind_int(db.stmtToDoINS,7,todo->complete),"ToDoInsB7")
CHK(sqlite3_bind_text(db.stmtToDoINS,8,todo->description,-1,SQLITE_STATIC),"ToDoInsB8")
CHK(sqlite3_bind_text(db.stmtToDoINS,9,todo->note,-1,SQLITE_STATIC),"ToDoInsB9")
CHKDONE(sqlite3_step(db.stmtToDoINS),"ToDoInsST")
db.maxIdToDo += 1;
CHK(sqlite3_clear_bindings(db.stmtToDoINS),"ToDoInsCL")
CHK(sqlite3_reset(db.stmtToDoINS),"ToDoInsRST")
return EXIT_SUCCESS;
err:
jp_logf(JP_LOG_FATAL,"jpsqlite_ToDoINS(): ret=%d, error=%s, Id=%d, rolling back\n%s\n",
sqlRet, sqlErr, errId, sqlite3_errmsg(db.conn));
sqlite3_finalize(db.stmtToDoINS);
//sqlite3_exec(db.conn,"ROLLBACK TRANSACTION",NULL,NULL,NULL);
return EXIT_FAILURE;
}
int jpsqlite_ExpenseUPD(struct Expense *ex, PCRecType rt, unsigned char attrib, unsigned int *unique_id) {
int sqlRet = 0, errId;
const char *sqlErr = "";
char date[32];
jp_logf(JP_LOG_DEBUG,"jpsqlite_ExpenseUPD(): rt=%d, category=%d, unique_id=%u\n",rt,attrib&0X0F,*unique_id);
CHK(sqlite3_bind_int(db.stmtExpenseUPD,1,attrib & 0x0F),"ExpenseUpdB1")
strftime(date,32,"%F",&(ex->date));
CHK(sqlite3_bind_text(db.stmtExpenseUPD,2,date,-1,SQLITE_STATIC),"ExpenseUpdB2")
CHK(sqlite3_bind_int(db.stmtExpenseUPD,3,ex->type),"ExpenseUpdB3")
CHK(sqlite3_bind_int(db.stmtExpenseUPD,4,ex->payment),"ExpenseUpdB4")
CHK(sqlite3_bind_int(db.stmtExpenseUPD,5,ex->currency),"ExpenseUpdB5")
CHK(sqlite3_bind_text(db.stmtExpenseUPD,6,ex->amount,-1,SQLITE_STATIC),"ExpenseUpdB6")
CHK(sqlite3_bind_text(db.stmtExpenseUPD,7,ex->vendor,-1,SQLITE_STATIC),"ExpenseUpdB7")
CHK(sqlite3_bind_text(db.stmtExpenseUPD,8,ex->city,-1,SQLITE_STATIC),"ExpenseUpdB8")
CHK(sqlite3_bind_text(db.stmtExpenseUPD,9,ex->attendees,-1,SQLITE_STATIC),"ExpenseUpdB9")
CHK(sqlite3_bind_text(db.stmtExpenseUPD,10,ex->note,-1,SQLITE_STATIC),"ExpenseUpdB10")
CHK(sqlite3_bind_int(db.stmtExpenseUPD,11,*unique_id),"ExpenseUpdB11")
CHKDONE(sqlite3_step(db.stmtExpenseUPD),"ExpenseUpdST")
CHK(sqlite3_clear_bindings(db.stmtExpenseUPD),"ExpenseUpdCL")
CHK(sqlite3_reset(db.stmtExpenseUPD),"ExpenseUpdRST")
return EXIT_SUCCESS;
err:
jp_logf(JP_LOG_FATAL,"jpsqlite_ExpenseUPD(): ret=%d, error=%s, Id=%d, rolling back\n%s\n",
sqlRet, sqlErr, errId, sqlite3_errmsg(db.conn));
sqlite3_finalize(db.stmtExpenseUPD);
//sqlite3_exec(db.conn,"ROLLBACK TRANSACTION",NULL,NULL,NULL);
return EXIT_FAILURE;
}
int jpsqlite_ExpenseINS(struct Expense *ex, unsigned char attrib) {
int sqlRet = 0, errId;
const char *sqlErr = "";
char date[32];
jp_logf(JP_LOG_DEBUG,"jpsqlite_ExpenseINS(): category=%d, maxIdExpense=%d\n",attrib&0X0F,db.maxIdExpense);
errId = db.maxIdExpense;
CHK(sqlite3_bind_int(db.stmtExpenseINS,1,db.maxIdExpense),"ExpenseInsB1")
CHK(sqlite3_bind_int(db.stmtExpenseINS,2,attrib & 0x0F),"ExpenseInsB2")
strftime(date,32,"%F",&(ex->date));
CHK(sqlite3_bind_text(db.stmtExpenseINS,3,date,-1,SQLITE_STATIC),"ExpenseInsB3")
CHK(sqlite3_bind_int(db.stmtExpenseINS,4,ex->type),"ExpenseInsB4")
CHK(sqlite3_bind_int(db.stmtExpenseINS,5,ex->payment),"ExpenseInsB5")
CHK(sqlite3_bind_int(db.stmtExpenseINS,6,ex->currency),"ExpenseInsB6")
CHK(sqlite3_bind_text(db.stmtExpenseINS,7,ex->amount,-1,SQLITE_STATIC),"ExpenseInsB7")
CHK(sqlite3_bind_text(db.stmtExpenseINS,8,ex->vendor,-1,SQLITE_STATIC),"ExpenseInsB8")
CHK(sqlite3_bind_text(db.stmtExpenseINS,9,ex->city,-1,SQLITE_STATIC),"ExpenseInsB9")
CHK(sqlite3_bind_text(db.stmtExpenseINS,10,ex->attendees,-1,SQLITE_STATIC),"ExpenseInsB10")
CHK(sqlite3_bind_text(db.stmtExpenseINS,11,ex->note,-1,SQLITE_STATIC),"ExpenseInsB11")
CHKDONE(sqlite3_step(db.stmtExpenseINS),"ExpenseInsST")
db.maxIdExpense += 1;
CHK(sqlite3_clear_bindings(db.stmtExpenseINS),"ExpenseInsCL")
CHK(sqlite3_reset(db.stmtExpenseINS),"ExpenseInsRST")
return EXIT_SUCCESS;
err:
jp_logf(JP_LOG_FATAL,"jpsqlite_ExpenseINS(): ret=%d, error=%s, Id=%d, rolling back\n%s\n",
sqlRet, sqlErr, errId, sqlite3_errmsg(db.conn));
sqlite3_finalize(db.stmtExpenseINS);
//sqlite3_exec(db.conn,"ROLLBACK TRANSACTION",NULL,NULL,NULL);
return EXIT_FAILURE;
}
int jpsqlite_Delete(AppType app_type, void *VP) {
int sqlRet = 0, errId;
const char *sqlErr = "";
MyAppointment *mappt;
//MyCalendarEvent *mcale;
MyAddress *maddr;
//MyContact *mcont;
MyToDo *mtodo;
MyMemo *mmemo;
struct MyExpense *mex;
jp_logf(JP_LOG_DEBUG,"jpsqlite_Delete(): app_type=%d\n",app_type);
switch (app_type) {
case DATEBOOK: