File: sqlite.c

package info (click to toggle)
libeventdb 0.90-4
  • links: PTS
  • area: main
  • in suites: lenny, squeeze
  • size: 2,176 kB
  • ctags: 920
  • sloc: sh: 8,859; ansic: 4,897; xml: 2,729; makefile: 76
file content (1648 lines) | stat: -rw-r--r-- 42,096 bytes parent folder | download | duplicates (2)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
/* sqlite.c - SQLITE backend.
   Copyright (C) 2006, 2007 Neal H. Walfield <neal@walfield.org>

   This file is part of GPE.

   GPE is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.

   GPE is distributed in the hope that it will be useful, but WITHOUT
   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
   or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
   License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA. */

#include <glib-object.h>
#include <glib.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <obstack.h>
#include <libintl.h>

#define _(x) gettext(x)

#define obstack_chunk_alloc malloc
#define obstack_chunk_free free

#include "event-db.h"

#include <sqlite.h>

/* The code is written using sqlite function names but they are
   relatively easily mapped to sqlite3 function names.  */
// #define USE_SQLITE3
#ifdef USE_SQLITE3

#include <sqlite3.h>

#define sqlite sqlite3
#define sqlite_close sqlite3_close
#define sqlite_exec sqlite3_exec
#define sqlite_exec_printf(handle_, query_, cb_, cookie_, err_, args_...) \
  ({ char *q_ = sqlite3_mprintf (query_ , ## args_); \
     int ret_ = sqlite3_exec (handle_, q_, cb_, cookie_, err_); \
     sqlite3_free (q_); \
     ret_; })
#define sqlite_last_insert_rowid sqlite3_last_insert_rowid
#define sqlite_create_aggregate(handle_, name_, args_, step_, final_, \
                                cookie_) \
  sqlite3_create_function (handle_, name_, args_, SQLITE_UTF8, cookie_, \
                           NULL, step_, final_);
#define sqlite_aggregate_context sqlite3_aggregate_context
#define sqlite_func sqlite3_context
#define sqlite_set_result_string(context_, str_, count) \
  sqlite3_result_text (context_, strdup (str_), count, free)

#else /* USE_SQLITE3 */

#define sqlite3_value const char 

#endif

/* Execute the sqlite_exec (or sqlite_exec_printf) statement.  If it
   fails because the database or table is locked, try a few times
   before completely failing.  */
#define SQLITE_TRY(statement) \
  ({ \
    int _tries = 0; \
    int _ret; \
    for (;;) \
      { \
        _ret = statement; \
        if ((_ret == SQLITE_BUSY || _ret == SQLITE_LOCKED) && _tries < 3) \
  	{ \
  	  g_main_context_iteration (NULL, FALSE); \
  	  sleep (1); \
  	  _tries ++; \
  	} \
        else \
  	  break; \
      } \
    _ret; \
  })

static gboolean
parse_date (char *s, time_t *t, gboolean *date_only)
{
  struct tm tm;
  char *p;

  memset (&tm, 0, sizeof (tm));
  tm.tm_isdst = -1;
  p = strptime (s, "%Y-%m-%d", &tm);
  if (p == NULL)
    {
      g_warning ("Unable to parse date: %s\n", s);
      return FALSE;
    }

  p = strptime (p, " %H:%M", &tm);
  if (p && *p == ':')
    /* Seconds used to be optional.  */
    p = strptime (p, ":%S", &tm);

  if (date_only)
    *date_only = (p == NULL) ? TRUE : FALSE;

  if (p)
    /* The time is in UTC.  */
    *t = timegm (&tm);
  else
    /* There is no time component but we want to identify the start of
       the day in the local time zone.  */
    *t = mktime (&tm);

  return TRUE;
}

typedef struct _SqliteDBClass SqliteDBClass;
typedef struct _SqliteDB SqliteDB;

extern GType sqlite_db_get_type (void);

#define TYPE_SQLITE_DB (sqlite_db_get_type ())
#define SQLITE_DB(obj) \
  (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_SQLITE_DB, SqliteDB))
#define SQLITE_DB_CLASS(klass) \
  (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_SQLITE_DB, SqliteDBClass))
#define IS_SQLITE_DB(obj) \
  (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_SQLITE_DB))
#define IS_SQLITE_DB_CLASS(klass) \
  (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_SQLITE_DB))
#define SQLITE_DB_GET_CLASS(obj) \
  (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_SQLITE_DB, SqliteDBClass))

struct _SqliteDBClass
{
  EventDBClass event_db_class;
};

struct _SqliteDB
{
  EventDB event_db;

  sqlite *sqliteh;
};


static void sqlite_db_class_init (gpointer klass, gpointer klass_data);
static void sqlite_db_init (GTypeInstance *instance, gpointer klass);
static void sqlite_db_dispose (GObject *obj);
static void sqlite_db_finalize (GObject *object);

static GObjectClass *sqlite_db_parent_class;

GType
sqlite_db_get_type (void)
{
  static GType type;

  if (! type)
    {
      static const GTypeInfo info =
      {
	sizeof (SqliteDBClass),
	NULL,
	NULL,
	sqlite_db_class_init,
	NULL,
	NULL,
	sizeof (struct _SqliteDB),
	0,
	sqlite_db_init
      };

      type = g_type_register_static (event_db_get_type (),
				     "SqliteEventDB", &info, 0);
    }

  return type;
}

static int
event_load_callback (void *arg, int argc, char **argv, char **names)
{
  EventSource *ev = EVENT_SOURCE (arg);

  /* argv[0] is the UID and that is already set.  */
  int i = 1;
  parse_date (argv[i], &ev->event.start, &ev->untimed);

  i ++;
  if (argv[i])
    ev->duration = atoi (argv[i]);

  i ++;
  if (argv[i])
    ev->type = atoi (argv[i]);
  if (ev->type < 0 || ev->type >= RECUR_COUNT)
    {
      g_warning ("Event %ld has unknown recurrence type %d",
		 ev->uid, ev->type);
      ev->type = 0;
    }

  i ++;
  if (argv[i])
    parse_date (argv[i], &ev->end, NULL);
  i ++;
  if (argv[i])
    ev->alarm = atoi (argv[i]);
  i ++;
  if (argv[i])
    ev->calendar = atoi (argv[i]);

  i ++;
  ev->eventid = g_strdup (argv[i]);

  i ++;
  if (argv[i])
    ev->count = atoi (argv[i]);
  i ++;
  if (argv[i])
    ev->increment = atoi (argv[i]);

  i ++;
  if (argv[i])
    {
      if (strchr (argv[i], '-'))
	parse_date (argv[i], &ev->last_modified, NULL);
      else
	ev->last_modified = strtoul (argv[i], NULL, 10);
    }

  i ++;
  char *p = argv[i];
  while (p)
    {
      char *end = strchr (p, ',');
      char *token;
      if (end)
	/* Copy up to the comma.  */
	{
	  int len = end - p;
	  token = g_malloc (len + 1);
	  memcpy (token, p, len);
	  token[len] = 0;
	}
      else
	/* This is the last segment, just copy it.  */
	token = g_strdup (p);
      ev->byday = g_slist_prepend (ev->byday, token);

      if (end)
	p = end + 1;
      else
	break;
    }

  i ++;
  p = argv[i];
  while (p)
    {
      long rmtime = (long)atoi (p);
      ev->exceptions = g_slist_prepend (ev->exceptions, (void *) rmtime);

      p = strchr (p, ',');
      if (p)
	p ++;
    }

  return 0;
}

static void
do_events_enumerate (EventDB *edb,
		     time_t period_start, time_t period_end,
		     gboolean alarms,
		     int (*cb) (EventSource *ev),
		     GError **error)
{
  struct obstack query;
  obstack_init (&query);

#define obstack_grow_string(o, string) \
  obstack_grow (o, string, strlen (string))

  obstack_grow_string
    (&query,
     "select * from"
     " (select *,"
     /* If the event is untimed (i.e. if there is a time
	component).  */
     "   (case substr (start, 12, 5)"
     "      when '' then"
     "        '0 seconds'"
     "      else"
     "        'localtime'"
     "    end)"
     "   as LOCALTIME,");

  /* The start of the event: only required if there is end.  */
  if (period_end)
    {
      if (alarms)
	obstack_grow_string
	  (&query,
	   " julianday (start, '-' || alarm || ' seconds')");
      else
	obstack_grow_string
	  (&query,
	   " julianday (start)");
      obstack_grow_string
	(&query,
	 " as EVENT_START,");
    }

  /* The end of the event: always required as we always have a
     start.  */
  obstack_grow_string
    (&query,
     "  (case"
     "     when recur == 0 then");
  if (alarms)
    obstack_grow_string
      (&query,
       /* We are looking for alarms on a single shot event.  */
       "       julianday (start, '-' || alarm || 'seconds', '1 second')");
  else
    obstack_grow_string
      (&query,
       "       julianday (start,"
       /* For historical reasons, an untimed event which is 0 seconds
	  long is consider 24 hours long.  */
       "                  (case duration"
       "                    when 0 then"
       "                      24 * 60 * 60"
       "                    else"
       "                      duration"
       "                    end)"
       "                   || ' seconds')");

  obstack_grow_string
    (&query,
     "     else"
     "       julianday (rend)"
     "    end)"
     "   as EVENT_END");

  obstack_grow_string
    (&query,
     "   from events"
     /* Does PERIOD_START occur before the end of the event?  */
     "   where (rend == 0 or rend ISNULL"
     "          or julianday (");

  char buffer[20];
  sprintf (buffer, "%ld", period_start);
  obstack_grow_string (&query, buffer);

  obstack_grow_string
    (&query,
     ", 'unixepoch', 'localtime')"
     "             < julianday (EVENT_END, LOCALTIME))");

  if (period_end)
    {
      /* Does the event start before or at PERIOD_END?  */
      obstack_grow_string
	(&query,
	 "     and julianday (EVENT_START, LOCALTIME)"
	 "         <= julianday (");

      sprintf (buffer, "%ld", period_end);
      obstack_grow_string (&query, buffer);

      obstack_grow_string
	(&query,
	 ", 'unixepoch', 'localtime')");
    }

  if (alarms)
    obstack_grow_string
      (&query,
       "     and alarm > 0");

  obstack_grow_string
    (&query,
     ");");
  /* Add a trailing NULL.  */
  obstack_1grow (&query, 0);
  char *q = obstack_finish (&query);

  int callback (void *arg, int argc, char **argv, char **names)
    {
      EventSource *ev;

      int uid = atoi (argv[0]);

      ev = EVENT_SOURCE (g_hash_table_lookup (edb->events, GINT_TO_POINTER(uid)));
      if (ev)
	/* Already loaded, just add a reference and return it.  */
	g_object_ref (ev);
      else
	{
	  ev = EVENT_SOURCE (g_object_new (event_source_get_type (), NULL));
	  ev->edb = edb;
	  ev->uid = uid;
	  g_hash_table_insert (edb->events, (gpointer) ev->uid, ev);

	  event_load_callback (ev, argc, argv, names);
	}

      return cb (ev);
    }

  char *err_str;
  SQLITE_TRY (sqlite_exec_printf (SQLITE_DB (edb)->sqliteh, q,
				  callback, NULL, &err_str));
  obstack_free (&query, NULL);
  if (err_str)
    {
      SIGNAL_ERROR (edb, error, err_str);
      sqlite_freemem (err_str);
    }
}

static void
do_set_default_calendar (EventDB *edb, EventCalendar *ec, GError **error)
{
  char *err;
  if (SQLITE_TRY
      (sqlite_exec_printf
       (SQLITE_DB (edb)->sqliteh,
	"insert or replace into default_calendar values (%d);",
	NULL, NULL, &err, ec->uid)))
    {
      SIGNAL_ERROR (edb, error, err);
      sqlite_freemem (err);
    }
}

static gint
do_eventid_to_uid (EventDB *edb, const char *eventid, GError **error)
{
  guint uid = 0;
  int callback (void *arg, int argc, char *argv[], char **names)
    {
      uid = atoi (argv[0]);
      return 1;
    }
  char *err = NULL;
  SQLITE_TRY (sqlite_exec_printf (SQLITE_DB (edb)->sqliteh,
				  "select uid from events"
				  " where eventid='%q';",
				  callback, NULL, &err, eventid));
  if (err)
    {
      SIGNAL_ERROR (edb, error, err);
      sqlite_freemem (err);
      return 0;
    }

  return uid;
}

static gboolean
do_event_new (EventSource *ev, GError **error)
{
  char *err;
  if (SQLITE_TRY (sqlite_exec (SQLITE_DB (ev->edb)->sqliteh,
			       "begin transaction;",
			       NULL, NULL, &err)))
    goto error_pretransaction;

  if (sqlite_exec (SQLITE_DB (ev->edb)->sqliteh,
		   "insert into events (start) values (NULL);",
		   NULL, NULL, &err))
    goto error;

  int uid = sqlite_last_insert_rowid (SQLITE_DB (ev->edb)->sqliteh);

  if (sqlite_exec (SQLITE_DB (ev->edb)->sqliteh, "commit transaction",
		   NULL, NULL, &err))
    goto error;

  ev->uid = uid;
  return TRUE;

 error:
  sqlite_exec (SQLITE_DB (ev->edb)->sqliteh, "rollback transaction",
	       NULL, NULL, NULL);
 error_pretransaction:
  SIGNAL_ERROR (ev->edb, error, err);
  sqlite_freemem (err);
  return FALSE;
}

static gboolean
do_event_load (EventSource *ev, GError **error)
{
  char *err;

  char *q = sqlite_mprintf ("select * from events where uid=%d", ev->uid);

  sqlite_vm *stmt;
  const char *tail = NULL;
  if (sqlite_compile (SQLITE_DB (ev->edb)->sqliteh, q, &tail, &stmt,
		      &err))
    {
      SIGNAL_ERROR (ev->edb, error, 
		    "Loading data for event %ld: %s", ev->uid, err);
      sqlite_freemem (err);
      sqlite_freemem (q);
      return FALSE;
    }

  g_assert (tail == NULL || *tail == '\0');
  sqlite_freemem (q);

  gboolean found = FALSE;
  int argc;
  char **argv;
  char **names;
  if (SQLITE_TRY (sqlite_step (stmt, &argc, &argv, &names)) == SQLITE_ROW)
    {
      found = TRUE;
      event_load_callback (ev, argc, argv, names);
    }

  if (sqlite_finalize (stmt, &err))
    {
      SIGNAL_ERROR (ev->edb, error, 
		    "Loading data for event %ld: %s", ev->uid, err);
      sqlite_freemem (err);
      return FALSE;
    }

  return found;
}

static void
do_event_load_details (EventSource *ev, GError **error)
{
  int callback (void *arg, int argc, char *argv[], char **names)
    {
      if (argc == 2)
	{
	  EventSource *ev = arg;
	  if (!strcmp (argv[0], "summary") && !ev->summary)
	    ev->summary = g_strdup (argv[1]);
	  else if (!strcmp (argv[0], "description") && !ev->description)
	    ev->description = g_strdup (argv[1]);
	  else if (!strcmp (argv[0], "location") && !ev->location)
	    ev->location = g_strdup (argv[1]);
	  else if (!strcmp (argv[0], "sequence"))
	    ev->sequence = atoi (argv[1]);
	  else if (!strcmp (argv[0], "category"))
	    ev->categories = g_slist_prepend (ev->categories,
					      GINT_TO_POINTER(atoi (argv[1])));
	}
      return 0;
    }

  char *err;
  if (SQLITE_TRY
      (sqlite_exec_printf (SQLITE_DB (ev->edb)->sqliteh,
			   "select tag,value from calendar"
			   " where uid=%d"
			   "  and tag in ('summary',"
			   "              'description', 'location',"
			   "              'category', 'sequence')",
			   callback, ev, &err, ev->uid)))
    {
      SIGNAL_ERROR (ev->edb, error, err);
      sqlite_freemem (err);
      return;
    }
}

static gboolean
do_event_flush (EventSource *ev, GError **error)
{
  char *err;
  struct tm tm;

  if (SQLITE_TRY
      (sqlite_exec
       (SQLITE_DB (ev->edb)->sqliteh, "begin transaction", NULL, NULL, &err)))
    goto error;

  if (ev->untimed)
    localtime_r (&ev->event.start, &tm);
  else
    gmtime_r (&ev->event.start, &tm);
  char start[64];
  strftime (start, sizeof (start), 
	    ev->untimed ? "%Y-%m-%d" : "%Y-%m-%d %T", &tm);  

  char end[64];
  if (! ev->end)
    sprintf (end, "NULL");
  else
    {
      gmtime_r (&ev->end, &tm);
      strftime (end, sizeof (end), 
		ev->untimed ? "'%Y-%m-%d'" : "'%Y-%m-%d %T'", &tm); 
    }

  char *byday = NULL;
  if (ev->byday)
    {
      GSList *l;
      for (l = ev->byday; l; l = l->next)
	if (byday)
	  {
	    char *t = g_strdup_printf ("%s,%s", byday, (char *) l->data);
	    g_free (byday);
	    byday = t;
	  }
	else
	  byday = g_strdup (l->data);
    }

  char *exceptions = NULL;
  if (ev->exceptions)
    {
      GSList *l;
      for (l = ev->exceptions; l; l = l->next)
	if (exceptions)
	  {
	    char *t = g_strdup_printf ("%s,%ld", exceptions, (long) l->data);
	    g_free (exceptions);
	    exceptions = t;
	  }
	else
	  exceptions = g_strdup_printf ("%ld", (long) l->data);
    }

  if (sqlite_exec_printf
      (SQLITE_DB (ev->edb)->sqliteh,
       "update events set start='%q', duration=%u, recur=%d, rend=%s,"
       "  alarm=%d, calendar=%d, eventid='%q', rcount=%d,"
       "  rincrement=%d, modified=%u, byday='%q', rexceptions='%q'"
       " where uid=%u;",
       NULL, NULL, &err,
       start, ev->duration, ev->type, end, ev->alarm, ev->calendar,
       ev->eventid, ev->count, ev->increment, ev->last_modified,
       byday, exceptions, ev->uid))
    goto error;

  if (ev->details)
    {
      if (sqlite_exec_printf
	  (SQLITE_DB (ev->edb)->sqliteh,
	   "delete from calendar where uid=%d;"
	   "insert or replace into calendar values (%d, 'sequence', %d);"
	   "insert or replace into calendar values (%d, 'summary', '%q');"
	   "insert or replace into calendar values (%d, 'description', '%q');"
	   "insert or replace into calendar values (%d, 'location', '%q');",
	   NULL, NULL, &err,
	   ev->uid,
	   ev->uid, ev->sequence,
	   ev->uid, ev->summary ?: "",
	   ev->uid, ev->description ?: "",
	   ev->uid, ev->location ?: ""))
	goto error;

      GSList *i;
      for (i = ev->categories; i; i = i->next)
	if (sqlite_exec_printf
	    (SQLITE_DB (ev->edb)->sqliteh,
	     "insert into calendar values (%d, 'category', '%d');",
	     NULL, NULL, &err, ev->uid, (int) i->data))
	  goto error;
    }

  if (sqlite_exec (SQLITE_DB (ev->edb)->sqliteh, "commit transaction",
		   NULL, NULL, &err))
    goto error;

  ev->modified = FALSE;
  return TRUE;

error:
  sqlite_exec (SQLITE_DB (ev->edb)->sqliteh, "rollback transaction",
	       NULL, NULL, NULL);

  g_assert (err);
  SIGNAL_ERROR (ev->edb, error, err);
  sqlite_freemem (err);

  return FALSE;
}

static void
do_event_remove (EventSource *ev, GError **error)
{
  char *err;
  if (SQLITE_TRY
      (sqlite_exec_printf (SQLITE_DB (ev->edb)->sqliteh,
			   "begin transaction;"
			   "insert into events_deleted (eventid, calendar)"
			   " values ('%q', '%d');"
			   "delete from calendar where uid=%d;"
			   "delete from events where uid=%d;"
			   "commit transaction;",
			   NULL, NULL, &err, ev->eventid, ev->calendar,
			   ev->uid, ev->uid)))
    {
      sqlite_exec (SQLITE_DB (ev->edb)->sqliteh, "rollback transaction;",
		   NULL, NULL, NULL);
      SIGNAL_ERROR (ev->edb, error, err);
      sqlite_freemem (err);
    }
}

static GSList *
do_list_unacknowledged_alarms (EventDB *edb, GError **error)
{
  GSList *list = NULL;

  /* We can't remove stale rows in the callback as the database is
     locked.  We collect them here and then iterate over this list
     later.  */
  struct removal
  {
    unsigned int uid;
    time_t start;
  };
  GSList *removals = NULL;

  GError *e = NULL;

  int callback (void *arg, int argc, char **argv, char **names)
    {
      if (argc != 2)
	{
	  g_warning ("%s: expected 2 arguments, got %d", __func__, argc);
	  return 0;
	}

      unsigned int uid = atoi (argv[0]);
      time_t t = atoi (argv[1]);

      EventSource *ev = EVENT_SOURCE (event_db_find_by_uid (edb, uid, &e));
      if (e)
	{
	  g_assert (! ev);
	  return 1;
	}
      if (! ev)
	{
	  g_warning ("%s: event %s not found", __func__, argv[0]);
	  goto remove;
	}

      if (t == 0)
	{
	  g_warning ("%s: unacknowledged event %s has 0 start time (%s)!",
		     __func__, argv[0], argv[1]);
	  goto remove;
	}

      GSList *l = event_list (ev, t, t, 0, FALSE, &e);
      g_object_unref (ev);
      if (e)
	{
	  g_assert (! l);
	  return 1;
	}
      if (! l)
	{
	  g_warning ("%s: no instance of event %s at %s",
		     __func__, argv[0], argv[1]);
	  goto remove;
	}

      if (l->next)
	g_warning ("%s: multiple instantiations of event %s!",
		   __func__, argv[0]);

      list = g_slist_concat (list, l);

      return 0;

    remove:
      {
	struct removal *r = g_malloc (sizeof (struct removal));
	r->uid = uid;
	r->start = t;
	removals = g_slist_prepend (removals, r);

	return 0;
      }
    }

  char *err;
  int res = SQLITE_TRY
    (sqlite_exec (SQLITE_DB (edb)->sqliteh,
		  "select uid, start from alarms_unacknowledged",
		  callback, NULL, &err));
  int abort = 0;
  if (e)
    {
      abort = 1;

      SIGNAL_ERROR_GERROR (edb, error, e);

      g_slist_free (list);
      list = NULL;
    }
  if (res && res != SQLITE_ABORT)
    {
      abort = 1;

      SIGNAL_ERROR (edb, error, err);
      sqlite_freemem (err);

      g_slist_free (list);
      list = NULL;
    }

  /* Kill any stale entries.  */
  GSList *i;
  for (i = removals; i; i = g_slist_next (i))
    {
      struct removal *r = i->data;
      char *err;
      if (! abort && SQLITE_TRY
	  (sqlite_exec_printf (SQLITE_DB (edb)->sqliteh,
			       "delete from alarms_unacknowledged"
			       " where uid=%d and start=%d",
			       NULL, NULL, &err, r->uid, r->start)))
	{
	  g_warning ("%s: while removing stale entry uid=%d,start=%ld, %s",
		     __func__, r->uid, r->start, err);
	  g_free (err);
	}
      g_free (r);
    }
  g_slist_free (removals);

  return list;
}

static void
do_event_mark_unacknowledged (EventSource *ev, GError **error)
{
  int err;
  char *str;

  err = SQLITE_TRY (sqlite_exec_printf (SQLITE_DB (ev->edb)->sqliteh,
					"insert or replace"
					" into alarms_unacknowledged"
					" (uid, start) values (%d, %d)",
					NULL, NULL, &str,
					ev->uid, ev->event.start));
  if (err)
    {
      SIGNAL_ERROR (ev->edb, error, str);
      sqlite_freemem (str);
    }
}

static void
do_event_mark_acknowledged (EventSource *ev, GError **error)
{
  char *err;
  if (SQLITE_TRY (sqlite_exec_printf (SQLITE_DB (ev->edb)->sqliteh,
				      "delete from alarms_unacknowledged"
				      " where uid=%d and start=%d",
				      NULL, NULL, &err,
				      ev->uid, ev->event.start)))
    {
      SIGNAL_ERROR (ev->edb, error, 
		    "removing event %ld from unacknowledged list: %s",
		    ev->uid, err);
      sqlite_freemem (err);
    }
}

static void
do_acknowledge_alarms_through (EventDB *edb, time_t t, GError **error)
{
  int err;
  char *str;

  err = SQLITE_TRY
    (sqlite_exec_printf
     (SQLITE_DB (edb)->sqliteh,
      "insert or replace into alarms_fired_through values (%d);",
      NULL, NULL, &str, t));
  if (err)
    {
      SIGNAL_ERROR (edb, error, str);
      sqlite_freemem (str);
    }
}

static void
do_event_calendar_new (EventCalendar *ec, GError **error)
{
  char *err;
  if (SQLITE_TRY
      (sqlite_exec_printf
       (SQLITE_DB (ec->edb)->sqliteh,
	"insert into calendars values"
	" ('%q', '%q', '%q', '%q', '%q', %d, %d, %d, %d, %d,"
	"  %d, %d, %d, %d, %d, %d)",
	NULL, NULL, &err,
	ec->title ?: "", ec->description ?: "",
	ec->url ?: "",
	ec->username ?: "", ec->password ?: "",
	ec->parent_uid, ec->hidden,
	ec->has_color, ec->red, ec->green, ec->blue,
	ec->mode, ec->sync_interval,
	ec->last_pull, ec->last_push,
	ec->last_modified)))
    {
      SIGNAL_ERROR (ec->edb, error, err);
      sqlite_freemem (err);
      return;
    }

  ec->uid = sqlite_last_insert_rowid (SQLITE_DB (ec->edb)->sqliteh);
}

static void
do_event_calendar_flush (EventCalendar *ec, GError **error)
{
  char *err;
  if (SQLITE_TRY
      (sqlite_exec_printf (SQLITE_DB (ec->edb)->sqliteh,
			   "update calendars set"
			   "  title='%q', description='%q',"
			   "  url='%q', username='%q', password='%q',"
			   "  parent=%d, hidden=%d,"
			   "  has_color=%d, red=%d, green=%d, blue=%d,"
			   "  mode=%d, sync_interval=%d,"
			   "  last_pull=%d, last_push=%d,"
			   "  last_modified=%d"
			   " where ROWID=%d;",
			   NULL, NULL, &err,
			   ec->title ?: "", ec->description ?: "",
			   ec->url ?: "", ec->username ?: "",
			   ec->password ?: "",
			   ec->parent_uid, ec->hidden,
			   ec->has_color, ec->red, ec->green, ec->blue,
			   ec->mode, ec->sync_interval,
			   ec->last_push, ec->last_pull, ec->last_modified,
			   ec->uid)))
    {
      SIGNAL_ERROR (ec->edb, error,
		    "updating %s (%d): %s",
		    ec->description, ec->uid, err);
      sqlite_freemem (err);
    }
}

static void
do_event_calendar_delete (EventCalendar *ec, GError **error)
{
  char *err;
  if (SQLITE_TRY
      (sqlite_exec_printf
       (SQLITE_DB (ec->edb)->sqliteh,
	"begin transaction;"
	/* Remove the events.  */
	"delete from calendar where uid"
	" in (select uid from events where calendar=%d);"
	"delete from events where calendar=%d;"
	/* And the calendar.  */
	"delete from calendars where ROWID=%d;"
	"commit transaction",
	NULL, NULL, &err, ec->uid, ec->uid, ec->uid)))
    {
      sqlite_exec (SQLITE_DB (ec->edb)->sqliteh, "rollback transaction;",
		   NULL, NULL, NULL);
      SIGNAL_ERROR (ec->edb, error, err);
      sqlite_freemem (err);
    }
}

static GSList *
do_event_calendar_list_events (EventCalendar *ec, time_t start, time_t end,
			       GError **error)
{
  GSList *list = NULL;

  int callback (void *arg, int argc, char *argv[], char **names)
    {
      GError *e = NULL;
      Event *ev = event_db_find_by_uid (ec->edb, atoi (argv[0]), &e);
      if (e)
	{
	  g_assert (! ev);
	  SIGNAL_ERROR_GERROR (ec->edb, error, e);
	  return 1;
	}

      if (ev)
	list = g_slist_prepend (list, ev);
      return 0;
    }

  char *query = "select uid from events where calendar=%d";
  if (start || end)
    {
      char *s = NULL;
      if (start)
	s = g_strdup_printf (" and %ld <= modified", start);

      char *e = NULL;
      if (end)
	s = g_strdup_printf (" and modified <= %ld", end);

      query = g_strdup_printf ("%s%s%s", query, start ? s : "", end ? e : "");

      if (start)
	g_free (s);
      if (end)
	g_free (e);
    }

  char *err = NULL;
  SQLITE_TRY
    (sqlite_exec_printf (SQLITE_DB (ec->edb)->sqliteh,
			 query /* ... %d ... */,
			 callback, NULL, &err, ec->uid));
  if (start || end)
    g_free (query);

  if (error)
    {
      g_slist_free (list);
      if (err)
	sqlite_freemem (err);
      return NULL;
    }
  if (err)
    {
      SIGNAL_ERROR (ec->edb, error, err);
      g_slist_free (list);
      return NULL;
    }

  return list;
}

static GSList *
do_event_calendar_list_deleted (EventCalendar *ec, GError **error)
{
  GSList *list = NULL;

  int callback (void *arg, int argc, char *argv[], char **names)
    {
      EventSource *ev;
      
      if (argc != 3) 
          return 0;
      ev = EVENT_SOURCE (g_object_new (event_source_get_type (), NULL));
      g_object_ref (ec->edb);
      ev->edb = ec->edb;
      ev->uid = (-1) * atoi (argv[0]);
      ev->eventid = g_strdup (argv[1]);
      ev->calendar = atoi (argv[2]);
      ev->event.dead = 1;

      list = g_slist_prepend (list, ev);
      return 0;
    }
  char *err = NULL;
  SQLITE_TRY
    (sqlite_exec_printf (SQLITE_DB (ec->edb)->sqliteh,
			 "select uid, eventid, calendar"
			 " from events_deleted where calendar=%d;",
			 callback, NULL, &err, ec->uid));
  if (err)
    {
      SIGNAL_ERROR (ec->edb, error, err);
      sqlite_freemem (err);
      g_slist_free (list);
      return NULL;
    }

  return list;
}

static void 
do_event_calendar_flush_deleted (EventCalendar *ec, GError **error)
{
  char *err = NULL;
  SQLITE_TRY
    (sqlite_exec_printf (SQLITE_DB (ec->edb)->sqliteh,
			 "delete from events_deleted where calendar=%d;",
			 NULL, NULL, &err, ec->uid));
  if (err)
    {
      SIGNAL_ERROR (ec->edb, error, err);
      sqlite_freemem (err);
    }
}

static void
sqlite_db_class_init (gpointer klass, gpointer klass_data)
{
  sqlite_db_parent_class = g_type_class_peek_parent (klass);

  GObjectClass *object_class;

  object_class = G_OBJECT_CLASS (klass);
  object_class->finalize = sqlite_db_finalize;
  object_class->dispose = sqlite_db_dispose;

  EventDBClass *edb = EVENT_DB_CLASS (klass);

  edb->events_enumerate = do_events_enumerate;
  edb->eventid_to_uid = do_eventid_to_uid;
  edb->set_default_calendar = do_set_default_calendar;

  edb->event_new = do_event_new;
  edb->event_load = do_event_load;
  edb->event_load_details = do_event_load_details;
  edb->event_flush = do_event_flush;
  edb->event_remove = do_event_remove;

  edb->list_unacknowledged_alarms = do_list_unacknowledged_alarms;
  edb->event_mark_unacknowledged = do_event_mark_unacknowledged;
  edb->event_mark_acknowledged = do_event_mark_acknowledged;
  edb->acknowledge_alarms_through = do_acknowledge_alarms_through;

  edb->event_calendar_new = do_event_calendar_new;
  edb->event_calendar_flush = do_event_calendar_flush;
  edb->event_calendar_delete = do_event_calendar_delete;
  edb->event_calendar_list_events = do_event_calendar_list_events;
  edb->event_calendar_list_deleted = do_event_calendar_list_deleted;
  edb->event_calendar_flush_deleted = do_event_calendar_flush_deleted;
}

static void
sqlite_db_init (GTypeInstance *instance, gpointer klass)
{
}

static void
sqlite_db_dispose (GObject *obj)
{
  /* Chain up to the parent class */
  G_OBJECT_CLASS (sqlite_db_parent_class)->dispose (obj);
}

static void
sqlite_db_finalize (GObject *object)
{
  SqliteDB *db = SQLITE_DB (object);

  G_OBJECT_CLASS (sqlite_db_parent_class)->finalize (object);

  sqlite_close (db->sqliteh);
}

EventDB *
event_db_new (const char *fname, GError **error)
{
  EventDB *edb = EVENT_DB (g_object_new (TYPE_SQLITE_DB, NULL));
  char *err = NULL;

#ifdef USE_SQLITE3
  int e = sqlite3_open (fname, &SQLITE_DB (edb)->sqliteh);
  if (e)
    {
      err = g_strdup (sqlite3_errmsg (SQLITE_DB (edb)->sqliteh));
      sqlite3_close (SQLITE_DB (edb)->sqliteh);
      SQLITE_DB (edb)->sqliteh = NULL;
    }
#else
  SQLITE_DB (edb)->sqliteh = sqlite_open (fname, 0, &err);
#endif
  if (err)
    goto error_before_transaction;

  if (SQLITE_TRY
      (sqlite_exec (SQLITE_DB (edb)->sqliteh, "begin transaction;",
		    NULL, NULL, &err)))
    goto error_before_transaction;

  /* Get the calendar db version.  */
  sqlite_exec (SQLITE_DB (edb)->sqliteh,
	       "create table calendar_dbinfo (version integer NOT NULL)",
	       NULL, NULL, &err);
  int version = -1;
  int dbinfo_callback (void *arg, int argc, char **argv, char **names)
    {
      if (argc == 1)
	version = atoi (argv[0]);

      return 0;
    }
  /* If the calendar_dbinfo table doesn't exist then we understand
     this to mean that this DB is uninitialized.  */
  int e = sqlite_exec (SQLITE_DB (edb)->sqliteh,
		       "select version from calendar_dbinfo",
		       dbinfo_callback, NULL, &err);
#ifdef USE_SQLITE3
  /* The database may be in SQLITE2 format.  Try to convert it.  */

  if (e)
    goto error;
#else
  if (e)
    goto error;
#endif

  if (version == 2)
    /* Databases with this version come from a relatively widely
       distributed pre-release of libeventdb with a bug such that the
       calendar table accumulates lots of duplicate entries because we
       kept appending modifications instead of replacing the records.
       Clean this up.  */
    {
      if (sqlite_exec
	  (SQLITE_DB (edb)->sqliteh,
	   "create temp table foo as"
	   "  select * from calendar"
	   "    where _ROWID_ in"
	   "      (select max(_ROWID_) from calendar"
	   "         where tag not in ('rexceptions', 'byday', 'category')"
	   "         group by uid, tag)"
	   "  union"
	   "    select DISTINCT * from calendar"
	   "      where tag in ('rexceptions', 'byday', 'category');"
	   "drop table calendar;"
	   "create table calendar as select * from foo;"
	   "drop table foo;",
	   NULL, NULL, &err))
	goto error;
    }

  if (version > 4)
    {
      err = g_strdup_printf
	(_("Unable to read database file: unknown version: %d"),
	 version);
      goto error;
    }

  if (version < 1)
    /* Create calendar table.  This name is actually a misnomer: it
       actually contains the ancillary data which accompanies
       events.  We use the name for historical reasons.  */
    sqlite_exec (SQLITE_DB (edb)->sqliteh,
		 "create table calendar"
		 " (uid INTEGER not NULL, tag STRING, value STRING);",
		 NULL, NULL, NULL);

  if (version == 0)
    /* Convert a version 0 DB to a verion 1 DB.  */
    {
      if (sqlite_exec
	  (SQLITE_DB (edb)->sqliteh,
	   /* Create calendar_urn.  */
	   "create table calendar_urn (uid INTEGER PRIMARY KEY);"
	   /* Populate it.  */
	   "insert into calendar_urn select _ROWID_ from events;"
	   /* And populate it.  */
	   "insert into calendar "
	   " select _ROWID_, 'start' string, start from events"
	   " union select _ROWID_, 'duration' string, duration from events"
	   " union select _ROWID_, 'alarm' string, alarmtime from events"
	   " union select _ROWID_, 'summary' string, summary from events"
	   " union select _ROWID_, 'description' string, description"
	   "  from events;"
	   "drop table events;"
	   "delete from calendar_dbinfo;"
	   "insert into calendar_dbinfo (version) values (1);",
	   NULL, NULL, &err))
	{
	  char *s = g_strdup_printf
	    ("%s: Converting a version 0 to a version 1 db: %s\n",
	     __func__, err);
	  g_free (err);
	  err = s;
	  goto error;
	}
      else
	version = 1;
    }

  /* Add an SQL convenience aggregate function, cat, which assembles a
     comma separated list of values.  */
  void cat_step (sqlite_func *context, int argc, sqlite3_value **argv_)
    {
      char **argv = (char **) argv_;

      if (! argv[0])
	/* Ignore NULL values.  */
	return;

      char **s;
      s = sqlite_aggregate_context (context, sizeof (*s));

      if (*s)
	{
	  char *t;
	  t = g_strdup_printf ("%s,%s", *s, argv[0]);
	  g_free (*s);
	  *s = t;
	}
      else
	*s = g_strdup (argv[0]);
    }
  void cat_finalize (sqlite_func *context)
    {
      char **s = sqlite_aggregate_context (context, sizeof (*s));
      if (*s)
	{
	  printf ("Returning: %s\n", *s);
	  sqlite_set_result_string (context, *s, -1);
	  g_free (*s);
	}
    }

  sqlite_create_aggregate (SQLITE_DB (edb)->sqliteh, "cat", 1,
			   cat_step, cat_finalize, NULL);

#define SELECT_FIELD(field) \
  "(select uid, value from calendar where tag='" field "')"

  if (version == 1)
    /* Version 1 versions of the database stored some information in
       the calendar table.  We've since rearranged this.  */
    {
      if (sqlite_exec
	  (SQLITE_DB (edb)->sqliteh,
	   "create temp table events as select * from "
	   " ((((" SELECT_FIELD ("start")
	   "     left join " SELECT_FIELD ("duration") " using (uid))"
	   "    left join " SELECT_FIELD ("recur") " using (uid))"
	   "   left join " SELECT_FIELD ("rend") " using (uid))"
	   "  left join " SELECT_FIELD ("alarm") " using (uid))"
	   " left join " SELECT_FIELD ("calendar") " using (uid);"
	   "delete from calendar where tag='start'"
	   " or tag='duration' or tag='recur' or tag='rend'"
	   " or tag='alarm' or tag='calendar';"
	   "drop table calendar_urn;",
	   NULL, NULL, &err))
	{
	  char *s = g_strdup_printf
	    ("%s: Converting a version 1 to a version 2 db: %s\n",
	     __func__, err);
	  g_free (err);
	  err = s;
	  goto error;
	}

      /* Convert the "rdaymask" bitmask to a "byday" list.  */
      struct info
      {
	guint uid;
	char *s;
      };
      GSList *list = NULL;

      char *days[] = { "MO", "TU", "WE", "TH", "FR", "SA", "SU" };

      int callback (void *arg, int argc, char **argv, char **names)
	{
	  if (! argv[1])
	    return 0;

	  int daymask = atoi (argv[1]);
	  if (! daymask)
	    return 0;

	  struct info *info = g_malloc0 (sizeof (*info));
	  list = g_slist_prepend (list, info);
	  info->uid = atoi (argv[0]);

	  int i;
	  for (i = 0; i < 7; i ++)
	    if ((1 << i) & daymask)
	      {
		if (info->s)
		  {
		    char *t = g_strdup_printf ("%s,%s", info->s, days[i]);
		    g_free (info->s);
		    info->s = t;
		  }
		else
		  info->s = g_strdup (days[i]);
	      }

	  return 0;
	}
      sqlite_exec (SQLITE_DB (edb)->sqliteh,
		   "select uid, value from calendar where tag='rdaymask';"
		   "delete from calendar where tag='rdaymask'", 
		   callback, NULL, NULL);

      GSList *i;
      for (i = list; i; i = i->next)
	{
	  struct info *info = i->data;

	  sqlite_exec_printf
	    (SQLITE_DB (edb)->sqliteh,
	     "update events set byday='%q' where uid='%d';",
	     NULL, NULL, NULL, info->s, info->uid);

	  g_free (info->s);
	  g_free (info);
	}
      g_slist_free (list);
    }
  if (version == 1 || version == 2)
    /* In the version 3 format, we have moved even more data from the
       calendar table to the event table.  */
    {
      if (sqlite_exec
	  (SQLITE_DB (edb)->sqliteh,
	   "create temp table foo as select * from"
	   " (((((((select * from events)"
	   "       left join " SELECT_FIELD ("eventid") " using (uid))"
	   "      left join " SELECT_FIELD ("rcount") " using (uid))"
	   "     left join " SELECT_FIELD ("rincrement") " using (uid))"
	   "    left join " SELECT_FIELD ("modified") " using (uid))"
	   "   left join (select uid, cat(value) from calendar"
	   "              where tag='byday' group by uid, tag) using (uid))"
	   "  left join (select uid, cat(value) from calendar"
	   "             where tag='rexceptions' group by uid, tag)"
	   "  using (uid));"
	   "drop table events;"
	   "delete from calendar"
	   "  where tag in ('eventid', 'rcount', 'rincrement', 'modified',"
	   "                'byday', 'rexceptions');",
	   NULL, NULL, &err))
	goto error;
    }

  if (version < 3)
    /* Create the events table.  */
    {
      if (sqlite_exec
	  (SQLITE_DB (edb)->sqliteh,
	   "create table events"
	   " (uid INTEGER PRIMARY KEY, start DATE, duration INTEGER, "
	   "  recur INTEGER, rend DATE, alarm INTEGER, calendar INTEGER,"
	   "  eventid STRING, rcount INTEGER, rincrement INTEGER,"
	   "  modified DATE, byday STRING, rexceptions STRING);",
	   NULL, NULL, &err))
	goto error;

      sqlite_exec (SQLITE_DB (edb)->sqliteh,
		   "create index events_enumerate_index"
		   " on events (start, duration, rend, alarm);",
		   NULL, NULL, &err);
    }

  if (version == 1 || version == 2)
    {
      if (sqlite_exec
	  (SQLITE_DB (edb)->sqliteh,
	   "insert into events select * from foo;"
	   "drop table foo;",
	   NULL, NULL, &err))
	goto error;
    }

  /* Read EDB->ALARMS_FIRED_THROUGH.  */
  edb->alarms_fired_through = time (NULL);
  if (version < 2)
    sqlite_exec (SQLITE_DB (edb)->sqliteh,
		 "create table alarms_fired_through (time INTEGER)",
		 NULL, NULL, NULL);

  int alarms_fired_through_callback (void *arg, int argc, char **argv,
				     char **names)
    {
      EventDB *edb = EVENT_DB (arg);
      if (argc == 1)
	{
	  int t = atoi (argv[0]);
	  if (t > 0)
	    edb->alarms_fired_through = t;
	}

      return 0;
    }
  if (sqlite_exec (SQLITE_DB (edb)->sqliteh,
		   "select time from alarms_fired_through",
		   alarms_fired_through_callback, edb, &err))
    goto error;

  /* Unacknowledged alarms.  */

  /* A table of events whose alarm fired before
     EDB->ALARMS_FIRED_THROUGH but were not yet acknowledged.  */
  if (version < 2)
    sqlite_exec (SQLITE_DB (edb)->sqliteh,
		 "create table alarms_unacknowledged"
		 " (uid INTEGER, start INTEGER NOT NULL)",
		 NULL, NULL, NULL);


  /* The default calendar.  */

  /* This table definately exists in a version 2 DB and may exist in a
     version 1 DB depending on the revision.  */
  if (version < 2)
    sqlite_exec (SQLITE_DB (edb)->sqliteh,
		 "create table default_calendar (default_calendar INTEGER)",
		 NULL, NULL, NULL);
  int default_calendar_callback (void *arg, int argc, char **argv,
				 char **names)
    {
      EventDB *edb = EVENT_DB (arg);
      if (argc == 1)
	edb->default_calendar = atoi (argv[0]);

      return 0;
    }
  edb->default_calendar = EVENT_CALENDAR_NO_PARENT;
  if (sqlite_exec (SQLITE_DB (edb)->sqliteh,
		   "select default_calendar from default_calendar",
		   default_calendar_callback, edb, &err))
    goto error;


  /* Calendars.  */

  sqlite_exec (SQLITE_DB (edb)->sqliteh,
	       "create table calendars"
	       " (title TEXT, description TEXT,"
	       "  url TEXT, username TEXT, password TEXT,"
	       "  parent INTEGER, hidden INTEGER,"
	       "  has_color INTEGER, red INTEGER, green INTEGER, blue INTEGER,"
	       "  mode INTEGER, sync_interval INTEGER,"
	       "  last_pull INTEGER, last_push INTEGER,"
	       "  last_modified)",
	       NULL, NULL, NULL);
  int load_calendars_callback (void *arg, int argc, char **argv,
			       char **names)
    {
      if (argc != 17)
	{
	  g_critical ("%s: Expected 17 arguments, got %d arguments",
		      __func__, argc);
	  return 0;
	}

      EventCalendar *ec
	= EVENT_CALENDAR (g_object_new (event_calendar_get_type (), NULL));
      ec->edb = edb;

      char **v = argv;
      ec->uid = atoi (*(v ++));
      ec->title = g_strdup (*(v ++));
      ec->description = g_strdup (*(v ++));
      ec->url = g_strdup (*(v ++));
      ec->username = g_strdup (*(v ++));
      ec->password = g_strdup (*(v ++));
      ec->parent_uid = atoi (*(v ++));
      ec->hidden = atoi (*(v ++));
      ec->has_color = atoi (*(v ++));
      ec->red = atoi (*(v ++));
      ec->green = atoi (*(v ++));
      ec->blue = atoi (*(v ++));
      ec->mode = atoi (*(v ++));
      ec->sync_interval = atoi (*(v ++));
      ec->last_pull = atoi (*(v ++));
      ec->last_push = atoi (*(v ++));
      ec->last_modified = atoi (*(v ++));

      edb->calendars = g_slist_prepend (edb->calendars, ec);

      return 0;
    }
  if (sqlite_exec (SQLITE_DB (edb)->sqliteh,
		   "select ROWID, title, description,"
		   "  url, username, password,"
		   "  parent, hidden,"
		   "  has_color, red, green, blue,"
		   "  mode, sync_interval, last_pull, last_push, last_modified"
		   " from calendars", load_calendars_callback, NULL, &err))
    {
      char *s = g_strdup_printf ("%s: Reading calendars: %s",
				 __func__, err);
      g_free (err);
      err = s;
      goto error;
    }

  /* If the default calendar was not set, set it now (after we've read
     in the calendars).  */
  if (edb->default_calendar == EVENT_CALENDAR_NO_PARENT)
    {
      EventCalendar *ec = event_db_get_default_calendar (edb, NULL, error);
      if (! ec)
	goto error;
      g_object_unref (ec);
    }
    
  if (version < 4)
    sqlite_exec (SQLITE_DB (edb)->sqliteh,
		 "create table events_deleted"
		 " (uid INTEGER PRIMARY KEY, eventid STRING NOT NULL, calendar INTEGER);",
		 NULL, NULL, NULL);
    

  /* Update the version information as appropriate.  */
  if (version < 4)
    {
      if (sqlite_exec (SQLITE_DB (edb)->sqliteh,
		       "delete from calendar_dbinfo;"
		       "insert into calendar_dbinfo (version) values (4);",
		       NULL, NULL, &err))
	goto error;
    }

  /* All done.  */
  sqlite_exec (SQLITE_DB (edb)->sqliteh, "commit transaction;",
	       NULL, NULL, NULL);

  return edb;
 error:
  sqlite_exec (SQLITE_DB (edb)->sqliteh, "rollback transaction;",
	       NULL, NULL, NULL);
 error_before_transaction:
  if (err)
    {
      SIGNAL_ERROR (edb, error, err);
      free (err);
    }

  if (SQLITE_DB (edb)->sqliteh)
    sqlite_close (SQLITE_DB (edb)->sqliteh);

  g_object_unref (edb);

  return NULL;
}