File: gda-vprovider-data-model.c

package info (click to toggle)
libgda5 5.2.4-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 75,256 kB
  • ctags: 49,218
  • sloc: ansic: 461,759; sh: 11,808; xml: 10,466; yacc: 5,160; makefile: 4,327; php: 1,416; java: 1,300; python: 896; sql: 879; perl: 116
file content (1768 lines) | stat: -rw-r--r-- 53,699 bytes parent folder | download
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
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
/*
 * Copyright (C) 2007 - 2012 Vivien Malerba <malerba@gnome-db.org>
 * Copyright (C) 2009 Bas Driessen <bas.driessen@xobas.com>
 * Copyright (C) 2010 David King <davidk@openismus.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA  02110-1301, USA.
 */

#include <glib/gi18n-lib.h>
#include <string.h>
#include "gda-vprovider-data-model.h"
#include "gda-vconnection-data-model.h"
#include "gda-vconnection-data-model-private.h"
#include <sqlite3.h>
#include <libgda/gda-connection-private.h>
#include <libgda/gda-connection-internal.h>
#include <libgda/gda-data-model-iter.h>
#include <libgda/gda-blob-op.h>
#include "../gda-sqlite.h"
#include <sql-parser/gda-statement-struct-util.h>
#include <libgda/gda-debug-macros.h>

#define GDA_DEBUG_VIRTUAL
#undef GDA_DEBUG_VIRTUAL

struct _GdaVproviderDataModelPrivate {
	int foo;
};

static void gda_vprovider_data_model_class_init (GdaVproviderDataModelClass *klass);
static void gda_vprovider_data_model_init       (GdaVproviderDataModel *prov, GdaVproviderDataModelClass *klass);
static void gda_vprovider_data_model_finalize   (GObject *object);
static GObjectClass  *parent_class = NULL;

static GdaConnection *gda_vprovider_data_model_create_connection (GdaServerProvider *provider);
static gboolean       gda_vprovider_data_model_open_connection (GdaServerProvider *provider, GdaConnection *cnc,
								GdaQuarkList *params, GdaQuarkList *auth,
								guint *task_id, GdaServerProviderAsyncCallback async_cb, 
								gpointer cb_data);
static gboolean       gda_vprovider_data_model_close_connection (GdaServerProvider *provider,
								 GdaConnection *cnc);
static GObject        *gda_vprovider_data_model_statement_execute (GdaServerProvider *provider, GdaConnection *cnc,
								   GdaStatement *stmt, GdaSet *params,
								   GdaStatementModelUsage model_usage,
								   GType *col_types, GdaSet **last_inserted_row,
								   guint *task_id, GdaServerProviderExecCallback async_cb,
								   gpointer cb_data, GError **error);
static const gchar   *gda_vprovider_data_model_get_name (GdaServerProvider *provider);

static GValue **create_gvalues_array_from_sqlite3_array (int argc, sqlite3_value **argv);


/*
 * GdaVproviderDataModel class implementation
 */
static void
gda_vprovider_data_model_class_init (GdaVproviderDataModelClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);
	GdaServerProviderClass *server_class = GDA_SERVER_PROVIDER_CLASS (klass);

	parent_class = g_type_class_peek_parent (klass);

	object_class->finalize = gda_vprovider_data_model_finalize;
	server_class->create_connection = gda_vprovider_data_model_create_connection;
	server_class->open_connection = gda_vprovider_data_model_open_connection;
	server_class->close_connection = gda_vprovider_data_model_close_connection;
	server_class->statement_execute = gda_vprovider_data_model_statement_execute;

	server_class->get_name = gda_vprovider_data_model_get_name;

	/* explicitly unimplement the DDL queries */
	server_class->supports_operation = NULL;
        server_class->create_operation = NULL;
        server_class->render_operation = NULL;
        server_class->perform_operation = NULL;
}

static void
gda_vprovider_data_model_init (GdaVproviderDataModel *prov, G_GNUC_UNUSED GdaVproviderDataModelClass *klass)
{
	prov->priv = g_new (GdaVproviderDataModelPrivate, 1);
}

static void
gda_vprovider_data_model_finalize (GObject *object)
{
	GdaVproviderDataModel *prov = (GdaVproviderDataModel *) object;

	g_return_if_fail (GDA_IS_VPROVIDER_DATA_MODEL (prov));

	/* free memory */
	g_free (prov->priv);
	prov->priv = NULL;

	/* chain to parent class */
	parent_class->finalize (object);
}

GType
gda_vprovider_data_model_get_type (void)
{
	static GType type = 0;

	if (G_UNLIKELY (type == 0)) {
		static GMutex registering;
		if (type == 0) {
			static GTypeInfo info = {
				sizeof (GdaVproviderDataModelClass),
				(GBaseInitFunc) NULL,
				(GBaseFinalizeFunc) NULL,
				(GClassInitFunc) gda_vprovider_data_model_class_init,
				NULL, NULL,
				sizeof (GdaVproviderDataModel),
				0,
				(GInstanceInitFunc) gda_vprovider_data_model_init,
				0
			};
			
		g_mutex_lock (&registering);
		if (type == 0)
			type = g_type_register_static (GDA_TYPE_VIRTUAL_PROVIDER, "GdaVproviderDataModel", &info, 0);
		g_mutex_unlock (&registering);
		}
	}

	return type;
}

/**
 * gda_vprovider_data_model_new
 *
 * Creates a new GdaVirtualProvider object which allows one to 
 * add and remove GdaDataModel objects as tables within a connection
 *
 * Returns: a new #GdaVirtualProvider object.
 */
GdaVirtualProvider *
gda_vprovider_data_model_new (void)
{
	GdaVirtualProvider *provider;

        provider = g_object_new (gda_vprovider_data_model_get_type (), NULL);
        return provider;
}

/*
 * Note about RowIDs and how SQLite uses them:
 *
 * SQLite considers that each virtual table has unique row IDs, absolute value available all the time.
 * When an UPDATE or DELETE statement is executed, SQLite does the following:
 *  - xBegin (table): request a transaction start
 *  - xOpen (table): create a new cursor
 *  - xFilter (cursor): initialize the cursor
 *  - moves the cursor one step at a time up to the end (xEof, xColumn and xNext). If it finds a
 *    row needing to be updated or deleted, it calls xRowid (cursor) to get the RowID
 *  - xClose (cursor): free the now useless cursor
 *  - calls xUpdate (table) as many times as needed (one time for each row where xRowid was called)
 *  - xSync (table)
 *  - xCommit (table)
 *
 * This does not work well with Libgda because RowID are not pre-defined. To circumvent this problem,
 * for each data model, the RowID as returned for each cursor is the row number in the data model, plus
 * an offset defined uniquely for each table:
 *   RowID (guint 64) = ID (guint32) << 32 + row (guint 31)
 */

#ifdef GDA_DEBUG_VIRTUAL
#define TRACE(table,cursor) g_print ("== %s (table=>%p cursor=>%p)\n", __FUNCTION__, (table), (cursor))
#else
#define TRACE(table,cursor)
#endif

typedef struct VirtualTable VirtualTable;
typedef struct VirtualCursor VirtualCursor;

struct VirtualTable {
	sqlite3_vtab                 base;
	GdaVconnectionDataModel     *cnc;
	GdaVConnectionTableData     *td;
	gboolean                     locked;

	guint32                      rows_offset;
};

#define MAX_VDATA_NUMBER 30

struct VirtualCursor {
	sqlite3_vtab_cursor      base; /* base.pVtab is a pointer to the sqlite3_vtab virtual table */
	VirtualFilteredData     *data; /* a ref is held here */
	gint                     row; /* starts at 0 */
};



static VirtualFilteredData *
virtual_filtered_data_new (VirtualTable *vtable, GdaDataModel *model,
			   int idxNum, const char *idxStr, int argc, sqlite3_value **argv)
{
	VirtualFilteredData *data;

	g_assert (model);
	data = g_new0 (VirtualFilteredData, 1);
	data->refcount = 1;
	data->reuseable = TRUE;
	data->idxNum = idxNum;
	data->idxStr = idxStr ? g_strdup (idxStr) : NULL;
	data->argc = argc;
	data->argv = create_gvalues_array_from_sqlite3_array (argc, argv);
	data->model = g_object_ref (model);
	if (GDA_IS_DATA_PROXY (model))
		data->iter = g_object_new (GDA_TYPE_DATA_MODEL_ITER,
					   "data-model", model, NULL);
	else
		data->iter = gda_data_model_create_iter (model);
	g_object_set (data->iter, "validate-changes", FALSE, NULL);

	gint n;
	n = gda_data_model_get_n_columns (model);
	n = (n >= 0) ? n : 1;
	data->values_array = g_array_new (FALSE, FALSE, sizeof (GValue));
	data->ncols = gda_data_model_get_n_columns (model);
	data->nrows = -1;
	data->rowid_offset = vtable->rows_offset;
	vtable->rows_offset ++;
	
#ifdef DEBUG_VCONTEXT
	g_print ("New VData %p for table [%s] idxNum=%d, idxStr=[%s], argc=%d\n", data, vtable->td->table_name,
		 idxNum, idxStr, argc);
	for (n= 0; n < data->argc; n++)
		g_print ("    [%s]\n", gda_value_stringify (data->argv [n]));
#endif
	return data;
}

static void
virtual_filtered_data_free (VirtualFilteredData *data)
{
	if (data->argv) {
		int i;
		for (i = 0; i < data->argc; i++)
			gda_value_free (data->argv [i]);
		g_free (data->argv);
	}
	g_free (data->idxStr);
	g_object_unref (data->model);
	if (data->iter)
		g_object_unref (data->iter);

	if (data->values_array) {
		guint i;
		for (i = 0; i < data->values_array->len; i++) {
			GValue *value;
			value = & g_array_index (data->values_array, GValue, i);
			g_value_reset (value);
		}
		g_array_free (data->values_array, TRUE);
	}
	g_free (data);

#ifdef DEBUG_VCONTEXT
	g_print ("Freed VData %p\n", data);
#endif
}

static VirtualFilteredData *
virtual_filtered_data_ref (VirtualFilteredData *data)
{
	data->refcount ++;
	return data;
}

void
_gda_vconnection_virtual_filtered_data_unref (VirtualFilteredData *data)
{
	data->refcount --;
	if (data->refcount == 0)
		virtual_filtered_data_free (data);
}

static void
virtual_cursor_free (VirtualCursor *cursor)
{
	if (!cursor)
		return;

	_gda_vconnection_virtual_filtered_data_unref (cursor->data);
	g_free (cursor);
}

/* module creation */
static int virtualCreate (sqlite3 *db, void *pAux, int argc, const char *const *argv, sqlite3_vtab **ppVtab, char **pzErr);
static int virtualConnect (sqlite3 *db, void *pAux, int argc, const char *const *argv, sqlite3_vtab **ppVtab, char **pzErr);
static int virtualDisconnect (sqlite3_vtab *pVtab);
static int virtualDestroy (sqlite3_vtab *pVtab);
static int virtualOpen (sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor);
static int virtualClose (sqlite3_vtab_cursor *cur);
static int virtualEof (sqlite3_vtab_cursor *cur);
static int virtualNext (sqlite3_vtab_cursor *cur);
static int virtualColumn (sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i);
static int virtualRowid (sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid);
static int virtualFilter (sqlite3_vtab_cursor *pVtabCursor, int idxNum, const char *idxStr, int argc, sqlite3_value **argv);
static int virtualBestIndex (sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo);
static int virtualUpdate (sqlite3_vtab *tab, int nData, sqlite3_value **apData, sqlite_int64 *pRowid);
static int virtualBegin (sqlite3_vtab *tab);
static int virtualSync (sqlite3_vtab *tab);
static int virtualCommit (sqlite3_vtab *tab);
static int virtualRollback (sqlite3_vtab *tab);
static int virtualRename (sqlite3_vtab *pVtab, const char *zNew);

static sqlite3_module Module = {
	1,                         /* iVersion */
	virtualCreate,
	virtualConnect,
	virtualBestIndex,
	virtualDisconnect, 
	virtualDestroy,
	virtualOpen,                  /* xOpen - open a cursor */
	virtualClose,                 /* xClose - close a cursor */
	virtualFilter,                /* xFilter - configure scan constraints */
	virtualNext,                  /* xNext - advance a cursor */
	virtualEof,                   /* xEof */
	virtualColumn,                /* xColumn - read data */
	virtualRowid,                 /* xRowid - read data */
	virtualUpdate,                /* xUpdate - write data */
	virtualBegin,                 /* xBegin - begin transaction */
	virtualSync,                  /* xSync - sync transaction */
	virtualCommit,                /* xCommit - commit transaction */
	virtualRollback,              /* xRollback - rollback transaction */
	NULL,                         /* xFindFunction - function overloading */
	virtualRename,                /* Rename - Notification that the table will be given a new name */
#if SQLITE_VERSION_NUMBER >= 3007007
	NULL,                         /* xSavepoint */  
	NULL,                         /* xRelease */
	NULL                          /* xRollbackTo */
#endif
};

static GdaConnection *
gda_vprovider_data_model_create_connection (GdaServerProvider *provider)
{
	GdaConnection *cnc;
	g_return_val_if_fail (GDA_IS_VPROVIDER_DATA_MODEL (provider), NULL);

	cnc = g_object_new (GDA_TYPE_VCONNECTION_DATA_MODEL, "provider", provider, NULL);

	return cnc;
}

static gboolean
gda_vprovider_data_model_open_connection (GdaServerProvider *provider, GdaConnection *cnc,
					  GdaQuarkList *params, GdaQuarkList *auth,
					  G_GNUC_UNUSED guint *task_id, GdaServerProviderAsyncCallback async_cb, G_GNUC_UNUSED gpointer cb_data)
{
	GdaQuarkList *m_params;

	g_return_val_if_fail (GDA_IS_VPROVIDER_DATA_MODEL (provider), FALSE);
	g_return_val_if_fail (GDA_IS_VCONNECTION_DATA_MODEL (cnc), FALSE);

	if (async_cb) {
		gda_connection_add_event_string (cnc, _("Provider does not support asynchronous connection open"));
                return FALSE;
	}

	if (params) {
		m_params = gda_quark_list_copy (params);
		gda_quark_list_add_from_string (m_params, "_IS_VIRTUAL=TRUE;EXTRA_FUNCTIONS=TRUE", TRUE);
	}
	else
		m_params = gda_quark_list_new_from_string ("_IS_VIRTUAL=TRUE;EXTRA_FUNCTIONS=TRUE");

	if (! GDA_SERVER_PROVIDER_CLASS (parent_class)->open_connection (GDA_SERVER_PROVIDER (provider), cnc, m_params,
									 auth, NULL, NULL, NULL)) {
		if (auth)
			gda_quark_list_protect_values (auth);
		gda_quark_list_free (m_params);
		return FALSE;
	}
	if (auth)
		gda_quark_list_protect_values (auth);
	gda_quark_list_free (m_params);

	SqliteConnectionData *scnc;
	scnc = (SqliteConnectionData*) gda_connection_internal_get_provider_data ((GdaConnection *) cnc);
	if (!scnc) {
		gda_connection_close_no_warning (cnc);
		gda_connection_add_event_string (cnc,
						 _("Connection is closed"));
		return FALSE;
	}

	/* Module to declare wirtual tables */
	if (SQLITE3_CALL (sqlite3_create_module) (scnc->connection, G_OBJECT_TYPE_NAME (provider), &Module, cnc) != SQLITE_OK)
		return FALSE;
	/*g_print ("==== Declared module for DB %p\n", scnc->connection);*/

	return TRUE;
}

static void
cnc_close_foreach_func (G_GNUC_UNUSED GdaDataModel *model, const gchar *table_name, GdaVconnectionDataModel *cnc)
{
	/*g_print ("---- FOREACH: Removing virtual table '%s'\n", table_name);*/
	GError *lerror = NULL;
	if (! gda_vconnection_data_model_remove (cnc, table_name, &lerror)) {
		g_warning ("Internal GdaVproviderDataModel error: %s",
			   lerror && lerror->message ? lerror->message : _("No detail"));
		g_clear_error (&lerror);
	}
}

static gboolean
gda_vprovider_data_model_close_connection (GdaServerProvider *provider, GdaConnection *cnc)
{
	g_return_val_if_fail (GDA_IS_VPROVIDER_DATA_MODEL (provider), FALSE);
	g_return_val_if_fail (GDA_IS_VCONNECTION_DATA_MODEL (cnc), FALSE);

	gda_vconnection_data_model_foreach (GDA_VCONNECTION_DATA_MODEL (cnc),
					    (GdaVconnectionDataModelFunc) cnc_close_foreach_func, cnc);

	return GDA_SERVER_PROVIDER_CLASS (parent_class)->close_connection (GDA_SERVER_PROVIDER (provider), cnc);
}

static GObject *
gda_vprovider_data_model_statement_execute (GdaServerProvider *provider, GdaConnection *cnc,
					    GdaStatement *stmt, GdaSet *params,
					    GdaStatementModelUsage model_usage,
					    GType *col_types, GdaSet **last_inserted_row,
					    guint *task_id, GdaServerProviderExecCallback async_cb,
					    gpointer cb_data, GError **error)
{
	GObject *retval;
	if (async_cb) {
                g_set_error (error, GDA_SERVER_PROVIDER_ERROR, GDA_SERVER_PROVIDER_METHOD_NON_IMPLEMENTED_ERROR,
                             "%s", _("Provider does not support asynchronous statement execution"));
                return NULL;
        }

	_gda_vconnection_set_working_obj ((GdaVconnectionDataModel*) cnc, (GObject*) stmt);
	retval = GDA_SERVER_PROVIDER_CLASS (parent_class)->statement_execute (provider, cnc, stmt, params,
									      model_usage, col_types,
									      last_inserted_row, task_id,
									      async_cb, cb_data, error);

	if (retval) {
		if (! GDA_IS_DATA_MODEL (retval))
			_gda_vconnection_set_working_obj ((GdaVconnectionDataModel*) cnc, NULL);

		gchar *sql;
		sql = gda_statement_to_sql (stmt, params, NULL);
		if (sql) {
			gchar *ptr = NULL;
			/* look for DROP TABLE to signal table removal */
			if (! g_ascii_strncasecmp (sql, "DROP", 4))
				ptr = sql + 4;
			else if (! g_ascii_strncasecmp (sql, "CREATE", 6))
				ptr = sql + 6;
			if (ptr) {
				/* skip spaces */
				for (; *ptr && (g_ascii_isspace (*ptr) || (*ptr == '\n')); ptr++);

				if (! g_ascii_strncasecmp (ptr, "TABLE", 5)) {
					/* skip spaces */
					gchar delim = 0;
					gchar *table_name, *quoted;
					for (ptr = ptr+5;
					     *ptr && (g_ascii_isspace (*ptr) || (*ptr == '\n'));
					     ptr++);
					if (*ptr == '\'') {
						delim = '\'';
						ptr++;
					}
					else if (*ptr == '"') {
						delim = '"';
						ptr++;
					}
					table_name = ptr;
					if (delim)
						for (; *ptr && (*ptr != delim) ; ptr++);
					else
						for (; *ptr && g_ascii_isalnum (*ptr); ptr++);
					*ptr = 0;
					quoted = _gda_connection_compute_table_virtual_name (GDA_CONNECTION (cnc), table_name);
					/*g_print ("%s() emits the 'vtable-dropped' signal for table [%s]\n",
					  __FUNCTION__, quoted);*/
					g_signal_emit_by_name (cnc, "vtable-dropped", quoted);
					g_free (quoted);
				}
			}
			g_free (sql);
		}
	}
	else
		_gda_vconnection_set_working_obj ((GdaVconnectionDataModel*) cnc, NULL);
	return retval;
}

static const gchar *
gda_vprovider_data_model_get_name (G_GNUC_UNUSED GdaServerProvider *provider)
{
	return "Virtual";
}

static int
virtualCreate (sqlite3 *db, void *pAux, int argc, const char *const *argv, sqlite3_vtab **ppVtab, char **pzErr)
{
	GdaVconnectionDataModel *cnc = GDA_VCONNECTION_DATA_MODEL (pAux);
	GString *sql;
	gint i, ncols;
	gchar *spec_name, *tmp;
	GdaVConnectionTableData *td;
	GHashTable *hash;

	TRACE (NULL, NULL);

	/* find Spec */
	g_assert (argc == 4);
	spec_name = g_strdup (argv[3]);
	i = strlen (spec_name);
	if (spec_name [i-1] == '\'')
		spec_name [i-1] = 0;
	if (*spec_name == '\'')
		memmove (spec_name, spec_name+1, i);

	td = _gda_vconnection_get_table_data_by_unique_name (cnc, spec_name);
	g_free (spec_name);
	if (!td) {
		/* wrong usage! */
		*pzErr = SQLITE3_CALL (sqlite3_mprintf) (_("Wrong usage of Libgda's virtual tables"));
		return SQLITE_ERROR;
	}

	/* preparations */
	if (td->spec->data_model) {
		ncols = gda_data_model_get_n_columns (td->spec->data_model);
		if (ncols <= 0) {
			*pzErr = SQLITE3_CALL (sqlite3_mprintf) (_("Data model must have at least one column"));
			return SQLITE_ERROR;
		}
		td->real_model = td->spec->data_model;
		g_object_ref (td->real_model);
	}

	GError *error = NULL;
	if (!td->columns && td->spec->create_columns_func)
		td->columns = td->spec->create_columns_func (td->spec, &error);
	if (!td->columns) {
		if (error && error->message) {
			int len = strlen (error->message) + 1;
			*pzErr = SQLITE3_CALL (sqlite3_malloc) (sizeof (gchar) * len);
			memcpy (*pzErr, error->message, len); /* Flawfinder: ignore */
		}
		else 
			*pzErr = SQLITE3_CALL (sqlite3_mprintf) (_("Could not compute virtual table's columns"));
		return SQLITE_ERROR;
	}
	ncols = g_list_length (td->columns);
	td->n_columns = ncols;

	/* create the CREATE TABLE statement */
	sql = g_string_new ("CREATE TABLE ");
	tmp = gda_connection_quote_sql_identifier (GDA_CONNECTION (cnc), argv[2]);
	g_string_append (sql, tmp);
	g_free (tmp);
	g_string_append (sql, " (");
	hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
	for (i = 0; i < ncols; i++) {
		GdaColumn *column;
		const gchar *name, *type;
		GType gtype;
		gchar *newcolname, *tmp;

		if (i != 0)
			g_string_append (sql, ", ");
		column = g_list_nth_data (td->columns, i);
		if (!column) {
			*pzErr = SQLITE3_CALL (sqlite3_mprintf) (_("Can't get data model description for column %d"), i);
			g_string_free (sql, TRUE);
			return SQLITE_ERROR;
		}

		name = gda_column_get_name (column);
		if (!name || !(*name))
			newcolname = g_strdup_printf ("_%d", i + 1);
		else {
			GString *string;
			gchar *ptr;
			string = g_string_new ("");
			newcolname = gda_sql_identifier_quote (name, GDA_CONNECTION (cnc), NULL, FALSE, FALSE);
			for (ptr = newcolname; *ptr; ptr++) {
				if ((*ptr == '_') ||
				    ((*ptr >= 'a') && (*ptr <= 'z')) ||
				    ((*ptr >= 'A') && (*ptr <= 'Z')))
					g_string_append_c (string, *ptr);
				else if ((*ptr >= '0') && (*ptr <= '9')) {
					if (ptr == name)
						g_string_append_c (string, '_');
					else
						g_string_append_c (string, *ptr);
				}
				else
					g_string_append_c (string, '_');
			}
			g_free (newcolname);
			newcolname = g_string_free (string, FALSE);
		}

		tmp = g_ascii_strdown (newcolname, -1);
		if (g_hash_table_lookup (hash, tmp)) {
			gint i;
			g_free (tmp);
			for (i = 0; ; i++) {
				gchar *nc2;
				nc2 = g_strdup_printf ("%s%d", newcolname, i);
				tmp = g_ascii_strdown (nc2, -1);
				if (! g_hash_table_lookup (hash, tmp)) {
					g_free (newcolname);
					newcolname = nc2;
					break;
				}
				g_free (tmp);
				g_free (nc2);
			}
		}

		gtype = gda_column_get_g_type (column);
		type = g_type_name (gtype);
		if (!type) {
			*pzErr = SQLITE3_CALL (sqlite3_mprintf) (_("Can't get data model's column type for column %d"), i);
			g_string_free (sql, TRUE);
			return SQLITE_ERROR;
		}
		else if (gtype == GDA_TYPE_BLOB)
			type = "blob";
		else if (gtype == GDA_TYPE_BINARY)
			type = "binary";
		else if (gtype == G_TYPE_STRING)
			type = "string";
		else if (gtype == G_TYPE_INT)
			type = "integer";
		else if (gtype == G_TYPE_UINT)
			type = "unsigned integer";
		else if ((gtype == G_TYPE_INT64) || (gtype == G_TYPE_LONG))
			type = "int64";
		else if ((gtype == G_TYPE_UINT64) || (gtype == G_TYPE_ULONG))
			type = "uint64";
		else if ((gtype == G_TYPE_DOUBLE) || (gtype == G_TYPE_FLOAT))
			type = "real";
		else if (gtype == G_TYPE_DATE)
			type = "date";
		else if (gtype == GDA_TYPE_TIME)
			type = "time";
		else if (gtype == GDA_TYPE_TIMESTAMP)
			type = "timestamp";
		else if (gtype == GDA_TYPE_SHORT)
			type = "short";
		else if (gtype == GDA_TYPE_USHORT)
			type = "unsigned short";
		else
			type = "text";

		g_string_append (sql, newcolname);
		g_hash_table_insert (hash, tmp, (gpointer) 0x01);
		g_free (newcolname);
		g_string_append_c (sql, ' ');
		g_string_append (sql, type);
		if (! gda_column_get_allow_null (column)) 
			g_string_append (sql, " NOT NULL");
		if (gtype == G_TYPE_STRING)
			g_string_append (sql, " COLLATE LOCALE");
	}
	g_hash_table_destroy (hash);

	/* add a hidden column which contains the row number of the data model */
	if (ncols != 0)
		g_string_append (sql, ", ");
	g_string_append (sql, "__gda_row_nb hidden integer");

	g_string_append_c (sql, ')');

	/* VirtualTable structure */
	VirtualTable *vtable;
	vtable = g_new0 (VirtualTable, 1);
	vtable->cnc = cnc;
	vtable->td = td;
	vtable->rows_offset = 0;
	*ppVtab = &(vtable->base);

	if (SQLITE3_CALL (sqlite3_declare_vtab) (db, sql->str) != SQLITE_OK) {
		*pzErr = SQLITE3_CALL (sqlite3_mprintf) (_("Can't declare virtual table (%s)"), sql->str);
		g_string_free (sql, TRUE);
		g_free (vtable);
		*ppVtab = NULL;
		return SQLITE_ERROR;
	}

	/*g_print ("VIRTUAL TABLE [%p]: %s\n", vtable, sql->str);*/
	g_string_free (sql, TRUE);

	return SQLITE_OK;
}

static int
virtualConnect (sqlite3 *db, void *pAux, int argc, const char *const *argv, sqlite3_vtab **ppVtab, char **pzErr)
{
	TRACE (NULL, NULL);

	return virtualCreate (db, pAux, argc, argv, ppVtab, pzErr);
}

static int
virtualDisconnect (sqlite3_vtab *pVtab)
{
	/*VirtualTable *vtable = (VirtualTable *) pVtab;*/

	TRACE (pVtab, NULL);
	g_free (pVtab);

	return SQLITE_OK;
}

static int
virtualDestroy (sqlite3_vtab *pVtab)
{
	TRACE (pVtab, NULL);

	return virtualDisconnect (pVtab);
}

static int
virtualOpen (G_GNUC_UNUSED sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor)
{
	VirtualCursor *cursor;

	/* create empty cursor */
	cursor = g_new0 (VirtualCursor, 1);
	*ppCursor = (sqlite3_vtab_cursor*) cursor;
	TRACE (pVTab, cursor);

	return SQLITE_OK;
}

static int
virtualClose (sqlite3_vtab_cursor *cur)
{
	VirtualCursor *cursor = (VirtualCursor*) cur;
	/*VirtualTable *vtable = (VirtualTable*) cur->pVtab;*/

	TRACE (cur->pVtab, cur);

	virtual_cursor_free (cursor);

	return SQLITE_OK;
}

static int
virtualEof (sqlite3_vtab_cursor *cur)
{
	VirtualCursor *cursor = (VirtualCursor*) cur;

	TRACE (cur->pVtab, cur);

	if (cursor->data->iter)
		return FALSE;
	else {
		if (cursor->row >= cursor->data->nrows)
			return TRUE;
		else
			return FALSE;
	}
}

/*
 * handle data model exceptions and return appropriate code
 */
static int
handle_data_model_exception (sqlite3_vtab *pVtab, GdaDataModel *model)
{
	GError **exceptions;
	gint i;
	exceptions = gda_data_model_get_exceptions (model);
	if (!exceptions)
		return SQLITE_OK;

	GError *trunc_error = NULL;
	GError *fatal_error = NULL;
	for (i = 0; exceptions [i]; i++) {
		GError *e;
		e = exceptions [i];
		if ((e->domain == GDA_DATA_MODEL_ERROR) &&
		    (e->code == GDA_DATA_MODEL_TRUNCATED_ERROR))
			trunc_error = e;
		else {
			fatal_error = e;
			break;
		}
	}
	if (fatal_error || trunc_error) {
		GError *e;
		e = fatal_error;
		if (!e)
			e = trunc_error;
		if (pVtab->zErrMsg)
			SQLITE3_CALL (sqlite3_free) (pVtab->zErrMsg);
		pVtab->zErrMsg = SQLITE3_CALL (sqlite3_mprintf)
			("%s", e->message ? e->message : _("No detail"));
		if (fatal_error)
			return SQLITE_ERROR;
		else
			return SQLITE_IOERR_TRUNCATE;
	}
	return SQLITE_OK;
}

static int
virtualNext (sqlite3_vtab_cursor *cur)
{
	VirtualCursor *cursor = (VirtualCursor*) cur;
	VirtualFilteredData *data;
	/*VirtualTable *vtable = (VirtualTable*) cur->pVtab;*/

	TRACE (cur->pVtab, cur);

	data = cursor->data;
	if (!data) {
		cur->pVtab->zErrMsg = SQLITE3_CALL (sqlite3_mprintf)
			(_("Internal SQLite error: no data to iterate on"));
		return SQLITE_MISUSE;
	}

	cursor->row ++;
	if (data->iter && (gda_data_model_iter_get_row (data->iter) < cursor->row)) {
		/* move to next row */
		if (gda_data_model_iter_move_next (data->iter)) {
			int exc_res;
			exc_res = handle_data_model_exception (cur->pVtab, data->model);
			if (exc_res != SQLITE_OK)
				goto onerror;

			/* load data for row */
			GSList *list;
			gint count;
			for (count = 0, list = ((GdaSet*) data->iter)->holders; list;
			     count++, list = list->next) {
				GdaHolder *h = (GdaHolder*) list->data;
				GError *lerror = NULL;
				if (! gda_holder_is_valid_e (h, &lerror)) {
					GValue value = {0};
					g_value_init (&value, G_TYPE_ERROR);
					g_value_take_boxed (&value, lerror);
					g_array_append_val (data->values_array, value);
				}
				else {
					const GValue *cvalue;
					cvalue = gda_holder_get_value (h);
					if (cvalue && (G_VALUE_TYPE (cvalue) != 0)) {
						GValue copy = {0};
						g_value_init (&copy, G_VALUE_TYPE (cvalue));
						g_value_copy (cvalue, &copy);
						g_array_append_val (data->values_array, copy);
					}
					else {
						GValue value = {0};
						g_value_init (&value, G_TYPE_ERROR);
						g_value_take_boxed (&value, lerror);
						g_array_append_val (data->values_array, value);
					}
				}
			}
			g_assert (count == data->ncols);
		}
		else {
			int exc_res;
			exc_res = handle_data_model_exception (cur->pVtab, data->model);
			if (exc_res != SQLITE_OK)
				goto onerror;
			else {
				/* end of data */
				g_object_unref (data->iter);
				data->iter = NULL;
				data->nrows = cursor->row;
			}
		}
	}
	return SQLITE_OK;

 onerror:
	cursor->row--;
	g_object_unref (data->iter);
	data->iter = NULL;
	data->nrows = cursor->row;
	return SQLITE_ERROR;
}

/*
 * @cursor may be %NULL
 */
static const GValue *
get_data_value (VirtualTable *vtable, VirtualCursor *cursor, gint row, gint64 rowid, gint col, GError **error)
{
	VirtualFilteredData *data = NULL;
	const GValue *value = NULL;

	if ((col < 0) || (col >= vtable->td->n_columns)) {
		g_set_error (error, 0, 0, _("Column %d out of range (0-%d)"), col, vtable->td->n_columns - 1);
		return NULL;
	}
	if (cursor) {
		data = cursor->data;
		g_assert (data);
	}
	else {
		g_assert (row < 0);
		row = (gint) (rowid & 0xFFFFFFFF);

		g_assert (vtable->td->context.current_vcontext);
		guint i;
		GArray *values_array = vtable->td->context.current_vcontext->context_data;
		for (i = 0; i < values_array->len; i++) {
			VirtualFilteredData *vd;
			vd = g_array_index (values_array, VirtualFilteredData*, i);
			if (vd->rowid_offset == (guint32) (rowid >> 32)) {
				data = vd;
				break;
			}
		}
	}

	if (data)
		value = & g_array_index (data->values_array, GValue, row * data->ncols + col);

	if (!value)
		g_set_error (error, 0, 0,
			     _("Could not find requested value at row %d and col %d"),
			     row, col);
#ifdef DEBUG_VCONTEXT
	g_print ("Read from [%s] [%s] %d x %d x %lld\n", vtable->td->table_name, gda_value_stringify (value), col, row, rowid);
#endif
	return value;
}

static int
virtualColumn (sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i)
{
	VirtualCursor *cursor = (VirtualCursor*) cur;

	TRACE (cur->pVtab, cur);
	
	if (i == ((VirtualTable*) cur->pVtab)->td->n_columns) {
		/* private hidden column, which returns the row number */
		SQLITE3_CALL (sqlite3_result_int) (ctx, cursor->row);
		return SQLITE_OK;
	}

	if (i >= cursor->data->ncols) {
		SQLITE3_CALL (sqlite3_result_text) (ctx, _("Column not found"), -1, SQLITE_TRANSIENT);
		return SQLITE_MISUSE;
	}
	
	const GValue *value;
	GError *lerror = NULL;
	value = get_data_value ((VirtualTable*) cur->pVtab, cursor, cursor->row, 0, i, &lerror);
	if (! value) {
		g_hash_table_insert (error_blobs_hash, lerror, GINT_TO_POINTER (1));
		SQLITE3_CALL (sqlite3_result_blob) (ctx, lerror, sizeof (GError), NULL);
	}
	else if (G_VALUE_TYPE (value) == G_TYPE_ERROR) {
		GError *lerror;
		lerror = g_value_get_boxed (value);
		//SQLITE3_CALL (sqlite3_result_error) (ctx, lerror && lerror->message ? lerror->message : _("No detail"), -1);
		if (lerror)
			lerror = g_error_copy (lerror);
		g_hash_table_insert (error_blobs_hash, lerror, GINT_TO_POINTER (1));
		SQLITE3_CALL (sqlite3_result_blob) (ctx, lerror, sizeof (GError), NULL);
	}
	else if (!value || gda_value_is_null (value))
		SQLITE3_CALL (sqlite3_result_null) (ctx);
	else  if (G_VALUE_TYPE (value) == G_TYPE_INT) 
		SQLITE3_CALL (sqlite3_result_int) (ctx, g_value_get_int (value));
	else if (G_VALUE_TYPE (value) == G_TYPE_INT64) 
		SQLITE3_CALL (sqlite3_result_int64) (ctx, g_value_get_int64 (value));
	else if (G_VALUE_TYPE (value) == G_TYPE_DOUBLE) 
		SQLITE3_CALL (sqlite3_result_double) (ctx, g_value_get_double (value));
	else if (G_VALUE_TYPE (value) == GDA_TYPE_BLOB) {
		GdaBlob *blob;
		GdaBinary *bin;
		blob = (GdaBlob *) gda_value_get_blob (value);
		bin = (GdaBinary *) blob;
		if (blob->op &&
		    (bin->binary_length != gda_blob_op_get_length (blob->op)))
			gda_blob_op_read_all (blob->op, blob);
		SQLITE3_CALL (sqlite3_result_blob) (ctx, blob->data.data, blob->data.binary_length, SQLITE_TRANSIENT);
	}
	else if (G_VALUE_TYPE (value) == GDA_TYPE_BINARY) {
		const GdaBinary *bin;
		bin = gda_value_get_binary (value);
		SQLITE3_CALL (sqlite3_result_blob) (ctx, bin->data, bin->binary_length, SQLITE_TRANSIENT);
	}
	else {
		gchar *str = gda_value_stringify (value);
		SQLITE3_CALL (sqlite3_result_text) (ctx, str, -1, SQLITE_TRANSIENT);
		g_free (str);
	}

	return SQLITE_OK;
}

static int
virtualRowid (sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid)
{
	VirtualCursor *cursor = (VirtualCursor*) cur;

	TRACE ((VirtualTable*) cur->pVtab, cur);

	*pRowid = ((sqlite_int64) cursor->row) + (((sqlite_int64) cursor->data->rowid_offset) << 32);
	return SQLITE_OK;
}

/* NEVER returns %NULL */
static GValue *
create_value_from_sqlite3_value_notype (sqlite3_value *svalue)
{
	GValue *value;
	value = g_new0 (GValue, 1);

	switch (SQLITE3_CALL (sqlite3_value_type) (svalue)) {
	case SQLITE_INTEGER:
		g_value_init (value, G_TYPE_INT64);
		g_value_set_int64 (value, SQLITE3_CALL (sqlite3_value_int64) (svalue));
		break;
	case SQLITE_FLOAT:
		g_value_init (value, G_TYPE_DOUBLE);
		g_value_set_double (value, SQLITE3_CALL (sqlite3_value_double) (svalue));
		break;
	case SQLITE_BLOB: {
		GdaBinary *bin;
		g_value_init (value, GDA_TYPE_BINARY);
		bin = g_new0 (GdaBinary, 1);
		bin->binary_length = SQLITE3_CALL (sqlite3_value_bytes) (svalue);
		if (bin->binary_length > 0) {
			bin->data = g_new (guchar, bin->binary_length);
			memcpy (bin->data, SQLITE3_CALL (sqlite3_value_blob) (svalue),
				bin->binary_length);
		}
		else
			bin->binary_length = 0;
		gda_value_take_binary (value, bin);
		break;
	}
	case SQLITE_NULL:
		gda_value_set_null (value);
		break;
	case SQLITE3_TEXT:
	default:
		g_value_init (value, G_TYPE_STRING);
		g_value_set_string (value, (gchar *) SQLITE3_CALL (sqlite3_value_text) (svalue));
		break;
	}
	return value;
}

/*
 * Returns: a new array of @argc values, and %NULL if @argc = 0
 */
static GValue **
create_gvalues_array_from_sqlite3_array (int argc, sqlite3_value **argv)
{
	GValue **array;
	gint i;
	if (argc == 0)
		return NULL;
	array = g_new (GValue *, argc);
	for (i = 0; i < argc; i++)
		array[i] = create_value_from_sqlite3_value_notype (argv[i]);
	return array;
}

static void
virtual_table_manage_real_data_model (VirtualTable *vtable, int idxNum, const char *idxStr,
				      int argc, sqlite3_value **argv)
{
	/*g_print ("================== %s (VTable=> %p, %s)\n", __FUNCTION__,
	  vtable, vtable->td->table_name);*/
	if (!vtable->td->spec->create_filtered_model_func && !vtable->td->spec->create_model_func)
		return;

	if (vtable->td->real_model) {
		g_object_unref (vtable->td->real_model);
		vtable->td->real_model = NULL;
	}

	/* actual data model creation */
	if (vtable->td->spec->create_filtered_model_func) {
		GValue **gargv;
		gargv = create_gvalues_array_from_sqlite3_array (argc, argv);
		vtable->td->real_model = vtable->td->spec->create_filtered_model_func (vtable->td->spec,
										       idxNum, idxStr,
										       argc, gargv);
		if (gargv) {
			gint i;
			for (i = 0; i < argc; i++)
				gda_value_free (gargv[i]);
			g_free (gargv);
		}
	}
	else if (vtable->td->spec->create_model_func)
		vtable->td->real_model = vtable->td->spec->create_model_func (vtable->td->spec);
	if (! vtable->td->real_model)
		return;

	/* columns if not yet created */
	if (! vtable->td->columns && vtable->td->spec->create_columns_func)
		vtable->td->columns = vtable->td->spec->create_columns_func (vtable->td->spec, NULL);

	if (vtable->td->columns) {
		/* columns */
		GList *list;
		guint i, ncols;
		ncols = gda_data_model_get_n_columns (vtable->td->real_model);
		g_assert (ncols == g_list_length (vtable->td->columns));
		for (i = 0, list = vtable->td->columns;
		     i < ncols;
		     i++, list = list->next) {
			GdaColumn *mcol = gda_data_model_describe_column (vtable->td->real_model, i);
			GdaColumn *ccol = (GdaColumn*) list->data;
			if (gda_column_get_g_type (mcol) == GDA_TYPE_NULL)
				gda_column_set_g_type (mcol, gda_column_get_g_type (ccol));
		}
	}

#ifdef GDA_DEBUG_VIRTUAL
	g_print ("Created real model %p for table %s\n", vtable->td->real_model, vtable->td->table_name);
#endif
}

static int
virtualFilter (sqlite3_vtab_cursor *pVtabCursor, int idxNum, const char *idxStr, int argc, sqlite3_value **argv)
{
	VirtualCursor *cursor = (VirtualCursor*) pVtabCursor;
	VirtualTable *vtable = (VirtualTable*) pVtabCursor->pVtab;

	TRACE (pVtabCursor->pVtab, pVtabCursor);
#ifdef GDA_DEBUG_VIRTUAL
	g_print ("\tidxStr=[%s], idxNum=[%d]\n", idxStr, idxNum);
#endif

	/* find a VirtualFilteredData corresponding to this filter */
	VirtualFilteredData *data = NULL;
	g_assert (vtable->td->context.current_vcontext);
	GArray *values_array = vtable->td->context.current_vcontext->context_data;

	if (values_array->len > 0) {
		guint i;
		for (i = 0; i < values_array->len; i++) {
			VirtualFilteredData *vd;
			vd = g_array_index (values_array, VirtualFilteredData*, i);
			if (vd->reuseable &&
			    (vd->idxNum == idxNum) &&
			    (vd->argc == argc) &&
			    ((!idxStr && !vd->idxStr) || (idxStr && vd->idxStr && !strcmp (idxStr, vd->idxStr)))) {
				GValue **avalues;
				gint i;
				gboolean equal = TRUE;
				avalues = create_gvalues_array_from_sqlite3_array (argc, argv);
				for (i = 0; i < argc; i++) {
					GValue *v1, *v2;
					v1 = vd->argv [i];
					v2 = avalues [i];

					if (! ((!v1 && !v2) ||
					       (v1 && v2 && (G_VALUE_TYPE (v1) == G_VALUE_TYPE (v2)) &&
						!gda_value_differ (v1, v2)))) {
						equal = FALSE;
						break;
					}
				}
				for (i = 0; i < argc; i++) {
					GValue *v2;
					v2 = avalues [i];
					if (v2)
						gda_value_free (v2);
				}
				g_free (avalues);
				
				if (equal) {
					data = vd;
					break;
				}
			}
		}
	}
#ifdef DEBUG_VCONTEXT
	if (data)
		g_print ("REUSE VData %p\n", data);
#endif

	if (!data) {
		virtual_table_manage_real_data_model (vtable, idxNum, idxStr, argc, argv);
		if (! vtable->td->real_model)
			return SQLITE_ERROR;
		data = virtual_filtered_data_new (vtable, vtable->td->real_model, idxNum, idxStr, argc, argv);
		g_array_prepend_val (values_array, data);
#ifdef DEBUG_VCONTEXT
		g_print ("VData %p prepended to array %p wt %d\n", data, values_array,
			 values_array->len);
#endif
		if (values_array->len > MAX_VDATA_NUMBER) {
			VirtualFilteredData *ldata;
			gint index;
			index = values_array->len - 1;
			ldata = g_array_index (values_array, VirtualFilteredData*, index);
			_gda_vconnection_virtual_filtered_data_unref (ldata);
			g_array_remove_index (values_array, index);
		}
	}
	
	if (cursor->data != data) {
		if (cursor->data)
			_gda_vconnection_virtual_filtered_data_unref (cursor->data);
		cursor->data = virtual_filtered_data_ref (data);
	}

	/* initialize cursor */
	cursor->row = -1;

	return virtualNext (pVtabCursor);
}

#ifdef GDA_DEBUG_VIRTUAL

static void
index_info_dump (sqlite3_index_info *pIdxInfo, gboolean dump_out)
{
	int nc;
	if (dump_out) {
		g_print ("Dump of OUT sqlite3_index_info [%p]\n", pIdxInfo);
		for (nc = 0; nc < pIdxInfo->nConstraint; nc++) {
			struct sqlite3_index_constraint_usage *cons;
			cons = &(pIdxInfo->aConstraintUsage[nc]);
			g_print ("sqlite3_index_constraint_usage[%d]\n", nc);
			g_print ("   argvIndex=%d\n", cons->argvIndex);
			g_print ("   omit=%d\n", cons->omit);
		}
		g_print ("idxNum=%d\n", pIdxInfo->idxNum);
		g_print ("orderByConsumed=%d\n", pIdxInfo->orderByConsumed);
	}
	else {
		g_print ("Dump of IN sqlite3_index_info [%p]\n", pIdxInfo);
		for (nc = 0; nc < pIdxInfo->nConstraint; nc++) {
			struct sqlite3_index_constraint *cons;
			cons = &(pIdxInfo->aConstraint[nc]);
			g_print ("sqlite3_index_constraint[%d]\n", nc);
			g_print ("   iColumn=%d\n", cons->iColumn);
			g_print ("   op=%d\n", cons->op);
			g_print ("   usable=%d\n", cons->usable);
			g_print ("   iTermOffset=%d\n", cons->iTermOffset);
		}
		
		for (nc = 0; nc < pIdxInfo->nOrderBy; nc++) {
			struct sqlite3_index_orderby *cons;
			cons = &(pIdxInfo->aOrderBy[nc]);
			g_print ("sqlite3_index_orderby[%d]\n", nc);
			g_print ("   iColumn=%d\n", cons->iColumn);
			g_print ("   desc=%d\n", cons->desc);
		}
	}
}
#endif

static void
map_sqlite3_info_to_gda_filter (sqlite3_index_info *info, GdaVconnectionDataModelFilter *filter)
{
	memset (filter, 0, sizeof (GdaVconnectionDataModelFilter));
	if (info->nConstraint > 0) {
		gint i, j;
		filter->aConstraint = g_new (struct GdaVirtualConstraint, info->nConstraint);
		filter->aConstraintUsage = g_new (struct GdaVirtualConstraintUsage, info->nConstraint);
		for (i = 0, j = 0; i < info->nConstraint; i++) {
			if (! info->aConstraint[i].usable)
				continue;

			filter->aConstraint[j].iColumn = info->aConstraint[i].iColumn;
			switch (info->aConstraint[i].op) {
			case SQLITE_INDEX_CONSTRAINT_EQ:
				filter->aConstraint[j].op = GDA_SQL_OPERATOR_TYPE_EQ;
				break;
			case SQLITE_INDEX_CONSTRAINT_GT:
				filter->aConstraint[j].op = GDA_SQL_OPERATOR_TYPE_GT;
				break;
			case SQLITE_INDEX_CONSTRAINT_LE:
				filter->aConstraint[j].op = GDA_SQL_OPERATOR_TYPE_LEQ;
				break;
			case SQLITE_INDEX_CONSTRAINT_LT:
				filter->aConstraint[j].op = GDA_SQL_OPERATOR_TYPE_LT;
				break;
			case SQLITE_INDEX_CONSTRAINT_GE:
				filter->aConstraint[j].op = GDA_SQL_OPERATOR_TYPE_GEQ;
				break;
			case SQLITE_INDEX_CONSTRAINT_MATCH:
				filter->aConstraint[j].op = GDA_SQL_OPERATOR_TYPE_REGEXP;
				break;
			case SQLITE_INDEX_CONSTRAINT_LIKE:
				filter->aConstraint[j].op = GDA_SQL_OPERATOR_TYPE_LIKE;
				break;
			default:
				g_assert_not_reached ();
			}

			filter->aConstraintUsage[j].argvIndex = 0;
			filter->aConstraintUsage[j].omit = FALSE;
			j++;
		}
		filter->nConstraint = j;
	}
	filter->nOrderBy = info->nOrderBy;
	if (filter->nOrderBy > 0) {
		gint i;
		filter->aOrderBy = g_new (struct GdaVirtualOrderby, filter->nOrderBy);
		for (i = 0; i < info->nOrderBy; i++) {
			filter->aOrderBy[i].iColumn = info->aOrderBy[i].iColumn;
			filter->aOrderBy[i].desc = info->aOrderBy[i].desc ? TRUE : FALSE;
		}
	}
	filter->idxNum = 0;
	filter->idxPointer = NULL;
	filter->orderByConsumed = FALSE;
	filter->estimatedCost = info->estimatedCost;
}

/*
 * Also frees @filter's dynamically allocated parts
 */
static void
map_consume_gda_filter_to_sqlite3_info (GdaVconnectionDataModelFilter *filter, sqlite3_index_info *info)
{
	g_assert (filter->nConstraint == info->nConstraint);
	if (info->nConstraint > 0) {
		gint i, j;
		for (i = 0, j = 0; i < info->nConstraint; i++) {
			if (! info->aConstraint[i].usable)
				continue;
			info->aConstraintUsage[i].argvIndex = filter->aConstraintUsage[j].argvIndex;
			info->aConstraintUsage[i].omit = filter->aConstraintUsage[j].omit ? 1 : 0;
			j++;
		}
		g_free (filter->aConstraint);
		g_free (filter->aConstraintUsage);

	}
	if (filter->nOrderBy > 0)
		g_free (filter->aOrderBy);
	info->idxNum = filter->idxNum;
	info->idxStr = (char*) filter->idxPointer;
	info->needToFreeIdxStr = 0;
	info->orderByConsumed = filter->orderByConsumed ? 1 : 0;
	info->estimatedCost = filter->estimatedCost;
}

static int
virtualBestIndex (sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo)
{
	VirtualTable *vtable = (VirtualTable *) tab;

	TRACE (tab, NULL);
#ifdef GDA_DEBUG_VIRTUAL
	index_info_dump (pIdxInfo, FALSE);
#endif

	if (vtable->td->spec->create_filter_func) {
		GdaVconnectionDataModelFilter filter;
		map_sqlite3_info_to_gda_filter (pIdxInfo, &filter);
		vtable->td->spec->create_filter_func (vtable->td->spec, &filter);
		map_consume_gda_filter_to_sqlite3_info (&filter, pIdxInfo);
#ifdef GDA_DEBUG_VIRTUAL
		index_info_dump (pIdxInfo, TRUE);
#endif
	}

	return SQLITE_OK;
}

/*
 *
 * Returns: >= 0 if Ok, -1 on error
 */
static gint
param_name_to_number (gint maxrows, const gchar *str)
{
	gchar *endptr [1];
	long int i;
	i = strtol (str, endptr, 10);
	if ((**endptr == '\0') && (i < maxrows) && (i >= 0))
		return i;
	else
		return -1;
}

/*
 * optype value: see virtualUpdate()
 */
static int
update_data_select_model (sqlite3_vtab *tab, gint optype, int nData, sqlite3_value **apData)
{
	VirtualTable *vtable = (VirtualTable *) tab;

	/* determine parameters required to execute MOD statement */
	GdaStatement *stmt = NULL;
	ParamType ptype;
	switch (optype) {
	case 1:
		ptype = PARAMS_DELETE;
		if (! vtable->td->modif_stmt [ptype])
			g_object_get (vtable->td->real_model, "delete-stmt", &stmt, NULL);
		break;
	case 2:
		ptype = PARAMS_INSERT;
		if (! vtable->td->modif_stmt [ptype])
			g_object_get (vtable->td->real_model, "insert-stmt", &stmt, NULL);
		break;
	case 3:
		ptype = PARAMS_UPDATE;
		if (! vtable->td->modif_stmt [ptype])
			g_object_get (vtable->td->real_model, "update-stmt", &stmt, NULL);
		break;
	default:
		g_assert_not_reached ();
	}
		
	if (! vtable->td->modif_stmt [ptype]) {
		if (! stmt) {
			tab->zErrMsg = SQLITE3_CALL (sqlite3_mprintf)
				(_("No statement specified to modify the data"));
			return SQLITE_READONLY;
		}
		
		GdaSet *params;
		if (! gda_statement_get_parameters (stmt, &params, NULL) || !params) {
			tab->zErrMsg = SQLITE3_CALL (sqlite3_mprintf)
				(_("Invalid statement specified to modify the data"));
			g_object_unref (stmt);
			return SQLITE_READONLY;
		}
		vtable->td->modif_stmt [ptype] = stmt;
		vtable->td->modif_params [ptype] = params;
	}
	stmt = vtable->td->modif_stmt [ptype];
		
	/* bind parameters */
	GSList *list;

	for (list = vtable->td->modif_params [ptype]->holders; list; list = list->next) {
		const gchar *id;
		GdaHolder *holder = GDA_HOLDER (list->data);
		gboolean holder_value_set = FALSE;
			
		id = gda_holder_get_id (holder);
		if (!id) {
			tab->zErrMsg = SQLITE3_CALL (sqlite3_mprintf)
				(_("Invalid parameter in statement to modify the data"));
			return SQLITE_READONLY;
		}
		if (*id == '+' && id[1]) {
			long int i;
			i = param_name_to_number (vtable->td->n_columns, id+1);
			if (i >= 0) {
				GType type;
				GValue *value;
				type = gda_column_get_g_type (gda_data_model_describe_column (vtable->td->real_model, i));
				if ((type != GDA_TYPE_NULL) && SQLITE3_CALL (sqlite3_value_text) (apData [i+2]))
					value = gda_value_new_from_string ((const gchar*) SQLITE3_CALL (sqlite3_value_text) (apData [i+2]), type);
				else
					value = gda_value_new_null ();
				if (gda_holder_take_value (holder, value, NULL))
					holder_value_set = TRUE;
			}
		}
		else if (*id == '-') {
			gint64 rowid = SQLITE3_CALL (sqlite3_value_int64) (apData [0]);
			long int i;
			const GValue *value;

			i = param_name_to_number (vtable->td->n_columns, id+1);
			value = get_data_value (vtable, NULL, -1, rowid, i, NULL);
			if (value && gda_holder_set_value (holder, value, NULL))
				holder_value_set = TRUE;
		}
			
		if (! holder_value_set) {
			GdaSet *exec_set;
			GdaHolder *eh;
			g_object_get (vtable->td->real_model, "exec-params",
				      &exec_set, NULL);
			if (! exec_set) {
				/* can't give value to param named @id */
				tab->zErrMsg = SQLITE3_CALL (sqlite3_mprintf)
					(_("Invalid parameter in statement to modify the data"));
				return SQLITE_READONLY;
			}
			eh = gda_set_get_holder (exec_set, id);
			if (! eh ||
			    ! gda_holder_set_bind (holder, eh, NULL)) {
				/* can't give value to param named @id */
				tab->zErrMsg = SQLITE3_CALL (sqlite3_mprintf)
					(_("Invalid parameter in statement to modify the data"));
				return SQLITE_READONLY;
			}
		}
	}

	GdaConnection *cnc;
	cnc = gda_data_select_get_connection (GDA_DATA_SELECT (vtable->td->real_model));

	GError *lerror = NULL;
#ifdef GDA_DEBUG_NO
	gchar *sql;
	sql = gda_statement_to_sql (stmt, NULL, NULL);
	g_print ("SQL: [%s] ", sql);
	g_free (sql);
	sql = gda_statement_to_sql_extended (stmt, cnc, vtable->td->modif_params [ptype], GDA_STATEMENT_SQL_PRETTY, NULL, &lerror);
	if (sql) {
		g_print ("With params: [%s]\n", sql);
		g_free (sql);
	}
	else {
		g_print ("params ERROR [%s]\n", lerror && lerror->message ? lerror->message : "No detail");
	}
	g_clear_error (&lerror);
#endif
		
	if (!cnc ||
	    (gda_connection_statement_execute_non_select (cnc, stmt,
							  vtable->td->modif_params [ptype],
							  NULL, &lerror) == -1)) {
		/* failed to execute */
		tab->zErrMsg = SQLITE3_CALL (sqlite3_mprintf)
			(_("Failed to modify data: %s"),
			 lerror && lerror->message ? lerror->message : _("No detail"));
		g_clear_error (&lerror);
		return SQLITE_READONLY;
	}
	return SQLITE_OK;
}

/*
 *    apData[0]  apData[1]  apData[2..]
 *
 *    INTEGER                              DELETE            
 *
 *    INTEGER    NULL       (nCol args)    UPDATE (do not set rowid)
 *    INTEGER    INTEGER    (nCol args)    UPDATE (with SET rowid = <arg1>)
 *
 *    NULL       NULL       (nCol args)    INSERT INTO (automatic rowid value)
 *    NULL       INTEGER    (nCol args)    INSERT (incl. rowid value)
 */
static int
virtualUpdate (sqlite3_vtab *tab, int nData, sqlite3_value **apData, sqlite_int64 *pRowid)
{
	VirtualTable *vtable = (VirtualTable *) tab;
	const gchar *api_misuse_error = NULL;
	gint optype; /* 1 => DELETE
		      * 2 => INSERT
		      * 3 => UPDATE
		      */

	TRACE (tab, NULL);

	g_assert (vtable->td->context.current_vcontext);
	GArray *values_array = vtable->td->context.current_vcontext->context_data;
	if (values_array) {
		guint i;
		for (i = 0; i < values_array->len; i++) {
			VirtualFilteredData *data;
			data = g_array_index (values_array, VirtualFilteredData*, i);
			data->reuseable = FALSE;
		}
	}

	/* determine operation type */
	if (nData == 1)
		optype = 1;
	else if ((nData > 1) && (SQLITE3_CALL (sqlite3_value_type) (apData[0]) == SQLITE_NULL)) {
		optype = 2;
		if (SQLITE3_CALL (sqlite3_value_type) (apData[1]) != SQLITE_NULL) {
			/* argc>1 and argv[0] is not NULL: rowid is imposed by SQLite
			 * which is not supported */
			return SQLITE_READONLY;
		}	}
	else if ((nData > 1) && (SQLITE3_CALL (sqlite3_value_type) (apData[0]) == SQLITE_INTEGER)) {
		optype = 3;
		if (SQLITE3_CALL (sqlite3_value_int) (apData[0]) != 
		    SQLITE3_CALL (sqlite3_value_int) (apData[1])) {
			/* argc>1 and argv[0]==argv[1]: rowid is imposed by SQLite 
			 * which is not supported */
			return SQLITE_READONLY;
		}
	}
	else
		return SQLITE_MISUSE;

	/* handle data model */
	if (! vtable->td->real_model) {
		virtual_table_manage_real_data_model (vtable, -1, NULL, 0, NULL);
		if (! vtable->td->real_model)
			return SQLITE_ERROR;
	}

	GdaDataModelAccessFlags access_flags;
	access_flags = gda_data_model_get_access_flags (vtable->td->real_model);
	if (((optype == 1) && ! (access_flags & GDA_DATA_MODEL_ACCESS_DELETE)) ||
	    ((optype == 2) && ! (access_flags & GDA_DATA_MODEL_ACCESS_INSERT)) ||
	    ((optype == 3) && ! (access_flags & GDA_DATA_MODEL_ACCESS_UPDATE))) {
		/* we can't use vtable->td->real_model because it can't be accessed correctly */
		if (GDA_IS_DATA_SELECT (vtable->td->real_model))
			return update_data_select_model (tab, optype, nData, apData);
		else {
			TO_IMPLEMENT;
			tab->zErrMsg = SQLITE3_CALL (sqlite3_mprintf)
				(_("Data model representing the table is read only"));
			return SQLITE_READONLY;
		}
	}

	/* REM: when using the values of apData[], the limit is
	 * (nData -1 ) and not nData because the last column of the corresponding CREATE TABLE ...
	 * is an internal hidden field which does not correspond to any column of the real data model
	 */

	if (optype == 1) {
		/* DELETE */
		if (SQLITE3_CALL (sqlite3_value_type) (apData[0]) == SQLITE_INTEGER) {
			gint rowid = SQLITE3_CALL (sqlite3_value_int) (apData [0]);
			return gda_data_model_remove_row (vtable->td->real_model, rowid, NULL) ?
				SQLITE_OK : SQLITE_READONLY;
		}
		else {
			api_misuse_error = "argc==1 and argv[0] is not an integer";
			goto api_misuse;
		}
	}
	else if (optype == 2) {
		/* INSERT */
		gint newrow, i;
		GList *values = NULL;
		
		for (i = 2; i < (nData - 1); i++) {
			GType type;
			GValue *value;
			type = gda_column_get_g_type (gda_data_model_describe_column (vtable->td->real_model, i - 2));
			if ((type != GDA_TYPE_NULL) && SQLITE3_CALL (sqlite3_value_text) (apData [i]))
				value = gda_value_new_from_string ((const gchar*) SQLITE3_CALL (sqlite3_value_text) (apData [i]), type);
			else
				value = gda_value_new_null ();
			/*g_print ("TXT #%s# => value %p (type=%s) apData[]=%p\n",
			  SQLITE3_CALL (sqlite3_value_text) (apData [i]), value,
			  g_type_name (type), apData[i]);*/
			values = g_list_prepend (values, value);
		}
		values = g_list_reverse (values);

		newrow = gda_data_model_append_values (vtable->td->real_model, values, NULL);
		g_list_foreach (values, (GFunc) gda_value_free, NULL);
		g_list_free (values);
		if (newrow < 0)
			return SQLITE_READONLY;

		*pRowid = newrow;
	}
	else if (optype == 3) {
		/* UPDATE */
		gint i;

		
		for (i = 2; i < (nData - 1); i++) {
			GValue *value;
			GType type;
			gint rowid = SQLITE3_CALL (sqlite3_value_int) (apData [0]);
			gboolean res;
			GError *error = NULL;

			/*g_print ("%d => %s\n", i, SQLITE3_CALL (sqlite3_value_text) (apData [i]));*/
			type = gda_column_get_g_type (gda_data_model_describe_column (vtable->td->real_model, i - 2));
			value = gda_value_new_from_string ((const gchar*) SQLITE3_CALL (sqlite3_value_text) (apData [i]), type);
			res = gda_data_model_set_value_at (vtable->td->real_model, i - 2, rowid,
							   value, &error);
			gda_value_free (value);
			if (!res) {
				/*g_print ("Error: %s\n", error && error->message ? error->message : "???");*/
				g_clear_error (&error);
				return SQLITE_READONLY;
			}
		}
		return SQLITE_OK;
	}
	else {
		api_misuse_error = "argc>1 and argv[0] is not NULL and not an integer";
		goto api_misuse;
	}

	return SQLITE_OK;

 api_misuse:
	g_warning ("Error in the xUpdate SQLite's virtual method: %s\n"
		   "this is an SQLite error, please report it", api_misuse_error);
	return SQLITE_ERROR;
}

static int
virtualBegin (G_GNUC_UNUSED sqlite3_vtab *tab)
{
	VirtualTable *vtable = (VirtualTable *) tab;
	TRACE (tab, NULL);

	if (vtable->locked)
		return SQLITE_ERROR;
	else {
		vtable->locked = TRUE;
		return SQLITE_OK;
	}
}

static int
virtualSync (G_GNUC_UNUSED sqlite3_vtab *tab)
{
	TRACE (tab, NULL);
	/* no documentation currently available, don't do anything */
	return SQLITE_OK;
}

static int
virtualCommit (G_GNUC_UNUSED sqlite3_vtab *tab)
{
	VirtualTable *vtable = (VirtualTable *) tab;
	TRACE (tab, NULL);

	vtable->locked = FALSE;
	return SQLITE_OK;
}

static int
virtualRollback (G_GNUC_UNUSED sqlite3_vtab *tab)
{	
	VirtualTable *vtable = (VirtualTable *) tab;
	TRACE (tab, NULL);

	vtable->locked = FALSE;
	return SQLITE_OK;
}

static int
virtualRename (G_GNUC_UNUSED sqlite3_vtab *pVtab, G_GNUC_UNUSED const char *zNew)
{
	TRACE (pVtab, NULL);
	/* not yet analysed and implemented */
	return SQLITE_OK;
}