-
Notifications
You must be signed in to change notification settings - Fork 0
/
rdf_storage_sqlite_mro.c
1443 lines (1207 loc) · 60.3 KB
/
rdf_storage_sqlite_mro.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// rdf_storage_sqlite_mro.c
//
// Created by Marcus Rohrmoser on 19.05.14.
//
// Copyright (c) 2014-2015, Marcus Rohrmoser mobile Software, http://mro.name/me
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are permitted
// provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of conditions
// and the following disclaimer.
//
// 2. The software must not be used for military or intelligence or related purposes nor
// anything that's in conflict with human rights as declared in http://www.un.org/en/documents/udhr/ .
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
// IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
#include "rdf_storage_sqlite_mro.h"
const char *LIBRDF_STORAGE_SQLITE_MRO = "http://purl.mro.name/rdf/sqlite/";
#include <stdlib.h>
// #include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <redland.h>
#include <rdf_storage.h>
#include <sqlite3.h>
#include <stdint.h>
#if DEBUG
#undef NDEBUG
#else
#define NDEBUG 1
#endif
#include <assert.h>
#pragma mark Basic Types & Constants
typedef enum {
BOOL_NO = 0,
BOOL_YES = 1
} boolean_t;
typedef sqlite3_uint64 hash_t;
static const hash_t NULL_ID = 0;
static inline boolean_t isNULL_ID(const hash_t x)
{
return NULL_ID == x;
}
#define RET_ERROR 1
#define RET_OK 0
/** C-String type for URIs. */
typedef unsigned char *str_uri_t;
/** C-String type for blank identifiers. */
typedef unsigned char *str_blank_t;
/** C-String type for literal values. */
typedef unsigned char *str_lit_val_t;
/** C-String type for (xml) language ids. */
typedef char *str_lang_t;
/** index into synchronous_flags */
typedef enum {
SYNC_UNKONWN = -1,
SYNC_OFF = 0,
SYNC_NORMAL = 1,
SYNC_FULL = 2
} syncronous_flag_t;
static const char *const synchronous_flags[4] = {
"off", "normal", "full", NULL
};
typedef struct
{
sqlite3 *db;
librdf_digest *digest;
const char *name;
boolean_t is_new;
syncronous_flag_t synchronous;
boolean_t in_transaction;
// compiled statements, lazy init
sqlite3_stmt *stmt_txn_start;
sqlite3_stmt *stmt_txn_commit;
sqlite3_stmt *stmt_txn_rollback;
sqlite3_stmt *stmt_triple_find;
sqlite3_stmt *stmt_triple_insert;
sqlite3_stmt *stmt_triple_delete;
sqlite3_stmt *stmt_size;
}
instance_t;
#pragma mark librdf convenience
#define LIBRDF_MALLOC(type, size) (type)malloc(size)
#define LIBRDF_CALLOC(type, size, count) (type)calloc(count, size)
#define LIBRDF_FREE(type, ptr) free( (type)ptr )
// #define LIBRDF_BAD_CAST(t, v) (t)(v)
static inline instance_t *get_instance(librdf_storage *storage)
{
return (instance_t *)librdf_storage_get_instance(storage);
}
static inline librdf_world *get_world(librdf_storage *storage)
{
return librdf_storage_get_world(storage);
}
static inline void free_hash(librdf_hash *hash)
{
if( hash )
librdf_free_hash(hash);
}
static inline librdf_node_type node_type(librdf_node *node)
{
return NULL == node ? LIBRDF_NODE_TYPE_UNKNOWN : librdf_node_get_type(node);
}
/* Copy first 8 bytes of digest into 64bit using a method portable across big/little endianness.
*/
static hash_t digest_hash(librdf_digest *digest)
{
assert(digest && "must be set");
librdf_digest_final(digest);
assert(librdf_digest_get_digest_length(digest) >= sizeof(hash_t) && "digest length too small");
const int byte_count = 8;
const int bit_per_byte = 8;
assert(byte_count == sizeof(hash_t) && "made for 8-byte (64-bit) hashes");
uint8_t *diges = (uint8_t *)librdf_digest_get_digest(digest);
sqlite3_uint64 ret = 0; // enforce unsigned
for( int i = byte_count - 1; i >= 0; i-- ) {
ret <<= bit_per_byte;
ret += diges[i];
}
assert(!isNULL_ID(ret) && "null hash");
return (hash_t)ret;
}
static hash_t hash_uri(librdf_uri *uri, librdf_digest *digest)
{
if( !uri )
return NULL_ID;
assert(digest && "digest must be set.");
size_t len = 0;
const unsigned char *s = librdf_uri_as_counted_string(uri, &len);
assert(s && "uri NULL");
assert(len && "uri length 0");
librdf_digest_init(digest);
librdf_digest_update(digest, s, len);
return digest_hash(digest);
}
static hash_t node_hash_uri(librdf_node *node, librdf_digest *digest)
{
return hash_uri(LIBRDF_NODE_TYPE_RESOURCE == node_type(node) ? librdf_node_get_uri(node) : NULL, digest);
}
static hash_t node_hash_blank(librdf_node *node, librdf_digest *digest)
{
if( LIBRDF_NODE_TYPE_BLANK != node_type(node) )
return NULL_ID;
assert(digest && "digest must be set.");
size_t len = 0;
unsigned char *b = librdf_node_get_counted_blank_identifier(node, &len);
assert(b && "blank NULL");
assert(len && "blank len 0");
librdf_digest_init(digest);
librdf_digest_update(digest, b, len);
return digest_hash(digest);
}
static hash_t node_hash_literal(librdf_node *node, librdf_digest *digest)
{
if( LIBRDF_NODE_TYPE_LITERAL != node_type(node) )
return NULL_ID;
assert(digest && "digest must be set.");
librdf_digest_init(digest);
size_t len = 0;
unsigned char *s = librdf_node_get_literal_value_as_counted_string(node, &len);
assert(s && "literal without value");
librdf_digest_update(digest, s, len);
librdf_uri *uri = librdf_node_get_literal_value_datatype_uri(node);
if( uri ) {
const unsigned char *str = librdf_uri_as_counted_string(uri, &len);
librdf_digest_update(digest, str, len);
}
const char *l = librdf_node_get_literal_value_language(node);
if( l )
librdf_digest_update( digest, (unsigned char *)l, strlen(l) );
return digest_hash(digest);
}
/** https://stackoverflow.com/questions/2590677/how-do-i-combine-hash-values-in-c0x
*/
static inline hash_t hash_combine(const hash_t seed, const hash_t b)
{
// return seed ^ ( b + 0x9e3779b9 + (seed << 6) + (seed >> 2) );
// http://stackoverflow.com/a/4948967 and https://stackoverflow.com/questions/4948780/magic-number-in-boosthash-combine#comment35569848_4948967
// $ python -c "import math; print hex(int(2**64 / ((1 + math.sqrt(5)) / 2)))"
return seed ^ ( b + 0x9e3779b97f4a7800L + (seed << 6) + (seed >> 2) );
}
static hash_t hash_combine_stmt(const hash_t s_uri_id, const hash_t s_blank_id, const hash_t p_uri_id, const hash_t o_uri_id, const hash_t o_blank_id, const hash_t o_lit_id, const hash_t c_uri_id)
{
hash_t stmt_id = NULL_ID;
stmt_id = hash_combine(stmt_id, s_uri_id);
stmt_id = hash_combine(stmt_id, s_blank_id);
stmt_id = hash_combine(stmt_id, p_uri_id);
stmt_id = hash_combine(stmt_id, o_uri_id);
stmt_id = hash_combine(stmt_id, o_blank_id);
stmt_id = hash_combine(stmt_id, o_lit_id);
// stmt_id ^= o_type_id;
stmt_id = hash_combine(stmt_id, c_uri_id);
return stmt_id;
}
static hash_t stmt_hash(librdf_statement *stmt, librdf_node *context_node, librdf_digest *digest)
{
if( !stmt )
return NULL_ID;
librdf_node *s = librdf_statement_get_subject(stmt);
librdf_node *p = librdf_statement_get_predicate(stmt);
librdf_node *o = librdf_statement_get_object(stmt);
return hash_combine_stmt( node_hash_uri(s, digest), node_hash_blank(s, digest), node_hash_uri(p, digest), node_hash_uri(o, digest), node_hash_blank(o, digest), node_hash_literal(o, digest), node_hash_uri(context_node, digest) );
}
#pragma mark Sqlite Debug/Profile
/* https://sqlite.org/eqp.html#section_2
** Argument pStmt is a prepared SQL statement. This function compiles
** an EXPLAIN QUERY PLAN command to report on the prepared statement,
** and prints the report to stdout using printf().
*/
static int printExplainQueryPlan(sqlite3_stmt *pStmt)
{
const char *zSql = sqlite3_sql(pStmt);
if( zSql == 0 ) return SQLITE_ERROR;
char *zExplain = sqlite3_mprintf("EXPLAIN QUERY PLAN %s", zSql);
if( 0 == zExplain ) return SQLITE_NOMEM;
sqlite3_stmt *pExplain; /* Compiled EXPLAIN QUERY PLAN command */
const int rc = sqlite3_prepare_v2(sqlite3_db_handle(pStmt), zExplain, -1, &pExplain, 0);
sqlite3_free(zExplain);
if( SQLITE_OK != rc ) return rc;
while( SQLITE_ROW == sqlite3_step(pExplain) ) {
const int iSelectid = sqlite3_column_int(pExplain, 0);
const int iOrder = sqlite3_column_int(pExplain, 1);
const int iFrom = sqlite3_column_int(pExplain, 2);
const char *zDetail = (const char *)sqlite3_column_text(pExplain, 3);
printf("%d %d %d %s\n", iSelectid, iOrder, iFrom, zDetail);
}
return sqlite3_finalize(pExplain);
}
// http://stackoverflow.com/a/6618833
static void trace(void *context, const char *sql)
{
fprintf(stderr, "Query: %s\n", sql);
}
// http://stackoverflow.com/a/6618833
static void profile(void *context, const char *sql, const sqlite3_uint64 ns)
{
fprintf(stderr, "Query: %s\n", sql);
fprintf(stderr, "Execution Time: %llu ms\n", ns / 1000000);
}
#pragma mark Sqlite Convenience
/** Sqlite Result Code, e.g. SQLITE_OK */
typedef int sqlite_rc_t;
static sqlite_rc_t log_error(sqlite3 *db, const char *sql, const sqlite_rc_t rc)
{
if( SQLITE_OK != rc ) {
const char *errmsg = sqlite3_errmsg(db);
librdf_log(NULL, 0, LIBRDF_LOG_ERROR, LIBRDF_FROM_STORAGE, NULL, "SQLite SQL error - %s (%d)\n%s", errmsg, rc, sql);
}
return rc;
}
static sqlite3_stmt *prep_stmt(sqlite3 *db, sqlite3_stmt **stmt_p, const char *zSql)
{
assert(db && "db handle is NULL");
assert(stmt_p && "statement is NULL");
assert(zSql && "SQL is NULL");
if( *stmt_p ) {
assert(0 == strcmp(sqlite3_sql(*stmt_p), zSql) && "ouch");
const sqlite_rc_t rc0 = sqlite3_reset(*stmt_p);
assert(SQLITE_OK == rc0 && "couldn't reset SQL statement");
const sqlite_rc_t rc1 = sqlite3_clear_bindings(*stmt_p);
assert(SQLITE_OK == rc1 && "couldn't reset SQL statement");
} else {
const char *remainder = NULL;
const int len_zSql = (int)strlen(zSql) + 1;
const sqlite_rc_t rc0 = log_error( db, zSql, sqlite3_prepare_v2(db, zSql, len_zSql, stmt_p, &remainder) );
assert(SQLITE_OK == rc0 && "couldn't compile SQL statement");
assert(*remainder == '\0' && "had remainder");
}
assert(*stmt_p && "statement is NULL");
return *stmt_p;
}
static void finalize_stmt(sqlite3_stmt **pStmt)
{
if( NULL == *pStmt )
return;
sqlite3_reset(*pStmt);
const sqlite_rc_t rc = sqlite3_finalize(*pStmt);
assert(SQLITE_OK == rc && "ouch");
*pStmt = NULL;
}
static sqlite_rc_t exec_stmt(sqlite3 *db, const char *sql)
{
char *errmsg = NULL;
const sqlite_rc_t rc = sqlite3_exec(db, sql, NULL, NULL, &errmsg);
sqlite3_free(errmsg);
return rc;
}
static inline sqlite_rc_t bind_int(sqlite3_stmt *stmt, const char *name, const hash_t _id)
{
assert(stmt && "stmt mandatory");
assert(name && "name mandatory");
const int idx = sqlite3_bind_parameter_index(stmt, name);
return 0 == idx ? SQLITE_OK : sqlite3_bind_int64(stmt, idx, _id);
}
static inline sqlite_rc_t bind_text(sqlite3_stmt *stmt, const char *name, const unsigned char *text, const size_t text_len)
{
assert(stmt && "stmt mandatory");
assert(name && "name mandatory");
const int idx = sqlite3_bind_parameter_index(stmt, name);
return 0 == idx ? SQLITE_OK : ( text == NULL ? sqlite3_bind_null(stmt, idx) : sqlite3_bind_text(stmt, idx, (const char *)text, (int)text_len, SQLITE_STATIC) );
}
static inline sqlite_rc_t bind_null(sqlite3_stmt *stmt, const char *name)
{
return bind_text(stmt, name, NULL, 0);
}
static sqlite_rc_t bind_uri_id(sqlite3_stmt *stmt, librdf_digest *digest, const char *name, librdf_uri *uri, hash_t *value)
{
if( uri ) {
const hash_t v = hash_uri(uri, digest);
if( value ) *value = v;
return bind_int(stmt, name, v);
}
return bind_null(stmt, name);
}
static sqlite_rc_t bind_uri(sqlite3_stmt *stmt, const char *name, librdf_uri *uri)
{
if( uri ) {
size_t len = 0;
const unsigned char *str = librdf_uri_as_counted_string(uri, &len);
return bind_text(stmt, name, str, len);
}
return bind_null(stmt, name);
}
static sqlite_rc_t bind_node_uri_id(sqlite3_stmt *stmt, librdf_digest *digest, const char *name, librdf_node *node, hash_t *value)
{
return bind_uri_id(stmt, digest, name, LIBRDF_NODE_TYPE_RESOURCE == node_type(node) ? librdf_node_get_uri(node) : NULL, value);
}
static sqlite_rc_t bind_node_uri(sqlite3_stmt *stmt, const char *name, librdf_node *node)
{
return bind_uri(stmt, name, LIBRDF_NODE_TYPE_RESOURCE == node_type(node) ? librdf_node_get_uri(node) : NULL);
}
static sqlite_rc_t bind_node_blank_id(sqlite3_stmt *stmt, librdf_digest *digest, const char *name, librdf_node *node, hash_t *value)
{
if( LIBRDF_NODE_TYPE_BLANK == node_type(node) ) {
const hash_t v = node_hash_blank(node, digest);
if( value ) *value = v;
return bind_int(stmt, name, v);
}
return bind_null(stmt, name);
}
static sqlite_rc_t bind_node_blank(sqlite3_stmt *stmt, const char *name, librdf_node *node)
{
if( LIBRDF_NODE_TYPE_BLANK == node_type(node) ) {
size_t len = 0;
const unsigned char *str = librdf_node_get_counted_blank_identifier(node, &len);
return bind_text(stmt, name, str, len);
}
return bind_null(stmt, name);
}
static sqlite_rc_t bind_node_lit_id(sqlite3_stmt *stmt, librdf_digest *digest, const char *name, librdf_node *node, hash_t *value)
{
if( LIBRDF_NODE_TYPE_LITERAL == node_type(node) ) {
const hash_t v = node_hash_literal(node, digest);
if( value ) *value = v;
return bind_int(stmt, name, v);
}
return bind_null(stmt, name);
}
static sqlite_rc_t bind_stmt(instance_t *db_ctx, librdf_statement *statement, librdf_node *context_node, sqlite3_stmt *stmt)
{
librdf_node *s = librdf_statement_get_subject(statement);
librdf_node *p = librdf_statement_get_predicate(statement);
librdf_node *o = librdf_statement_get_object(statement);
sqlite_rc_t rc = SQLITE_OK;
hash_t s_uri_id = NULL_ID;
hash_t s_blank_id = NULL_ID;
hash_t p_uri_id = NULL_ID;
hash_t o_uri_id = NULL_ID;
hash_t o_blank_id = NULL_ID;
hash_t o_lit_id = NULL_ID;
hash_t o_type_id = NULL_ID;
hash_t c_uri_id = NULL_ID;
if( SQLITE_OK != ( rc = bind_node_uri_id(stmt, db_ctx->digest, ":s_uri_id", s, &s_uri_id) ) ) return rc;
if( SQLITE_OK != ( rc = bind_node_uri(stmt, ":s_uri", s) ) ) return rc;
if( SQLITE_OK != ( rc = bind_node_blank_id(stmt, db_ctx->digest, ":s_blank_id", s, &s_blank_id) ) ) return rc;
if( SQLITE_OK != ( rc = bind_node_blank(stmt, ":s_blank", s) ) ) return rc;
if( SQLITE_OK != ( rc = bind_node_uri_id(stmt, db_ctx->digest, ":p_uri_id", p, &p_uri_id) ) ) return rc;
if( SQLITE_OK != ( rc = bind_node_uri(stmt, ":p_uri", p) ) ) return rc;
if( SQLITE_OK != ( rc = bind_node_uri_id(stmt, db_ctx->digest, ":o_uri_id", o, &o_uri_id) ) ) return rc;
if( SQLITE_OK != ( rc = bind_node_uri(stmt, ":o_uri", o) ) ) return rc;
if( SQLITE_OK != ( rc = bind_node_blank_id(stmt, db_ctx->digest, ":o_blank_id", o, &o_blank_id) ) ) return rc;
if( SQLITE_OK != ( rc = bind_node_blank(stmt, ":o_blank", o) ) ) return rc;
if( SQLITE_OK != ( rc = bind_node_lit_id(stmt, db_ctx->digest, ":o_lit_id", o, &o_lit_id) ) ) return rc;
if( LIBRDF_NODE_TYPE_LITERAL == node_type(o) ) {
if( SQLITE_OK != ( rc = bind_uri_id(stmt, db_ctx->digest, ":o_datatype_id", librdf_node_get_literal_value_datatype_uri(o), &o_type_id) ) ) return rc;
if( SQLITE_OK != ( rc = bind_uri( stmt, ":o_datatype", librdf_node_get_literal_value_datatype_uri(o) ) ) ) return rc;
char *l = librdf_node_get_literal_value_language(o);
if( l )
if( SQLITE_OK != ( rc = bind_text( stmt, ":o_language", (unsigned char *)l, strlen(l) ) ) ) return rc;
size_t len = 0;
const unsigned char *str = librdf_node_get_literal_value_as_counted_string(o, &len);
if( SQLITE_OK != ( rc = bind_text(stmt, ":o_text", str, len) ) ) return rc;
}
if( SQLITE_OK != ( rc = bind_node_uri_id(stmt, db_ctx->digest, ":c_uri_id", context_node, &c_uri_id) ) ) return rc;
if( SQLITE_OK != ( rc = bind_node_uri(stmt, ":c_uri", context_node) ) ) return rc;
if( !librdf_statement_is_complete(statement) )
return SQLITE_OK;
const hash_t stmt_id = hash_combine_stmt(s_uri_id, s_blank_id, p_uri_id, o_uri_id, o_blank_id, o_lit_id, c_uri_id);
assert(stmt_id == stmt_hash(statement, context_node, db_ctx->digest) && "statement hash compatation mismatch");
if( SQLITE_OK != ( rc = bind_int(stmt, ":stmt_id", stmt_id) ) ) return rc;
return SQLITE_OK;
}
static inline const str_uri_t column_uri_string(sqlite3_stmt *stmt, const int iCol)
{
return (str_uri_t)sqlite3_column_text(stmt, iCol);
}
static inline const str_blank_t column_blank_string(sqlite3_stmt *stmt, const int iCol)
{
return (str_blank_t)sqlite3_column_text(stmt, iCol);
}
static inline const str_lang_t column_language(sqlite3_stmt *stmt, const int iCol)
{
return (str_lang_t)sqlite3_column_text(stmt, iCol);
}
static sqlite_rc_t transaction_start(librdf_storage *storage)
{
instance_t *db_ctx = get_instance(storage);
if( db_ctx->in_transaction )
return SQLITE_MISUSE;
const sqlite_rc_t rc = sqlite3_step( prep_stmt(db_ctx->db, &(db_ctx->stmt_txn_start), "BEGIN IMMEDIATE TRANSACTION;") );
db_ctx->in_transaction = SQLITE_DONE == rc;
assert(db_ctx->in_transaction != BOOL_NO && "ouch");
return SQLITE_DONE == rc ? SQLITE_OK : rc;
}
static sqlite_rc_t transaction_commit(librdf_storage *storage, const sqlite_rc_t begin)
{
if( begin != SQLITE_OK )
return SQLITE_OK;
instance_t *db_ctx = get_instance(storage);
if( BOOL_NO == db_ctx->in_transaction )
return SQLITE_MISUSE;
const sqlite_rc_t rc = sqlite3_step( prep_stmt(db_ctx->db, &(db_ctx->stmt_txn_commit), "COMMIT TRANSACTION;") );
db_ctx->in_transaction = !(SQLITE_DONE == rc);
assert(db_ctx->in_transaction == BOOL_NO && "ouch");
return SQLITE_DONE == rc ? SQLITE_OK : rc;
}
static sqlite_rc_t transaction_rollback(librdf_storage *storage, const sqlite_rc_t begin)
{
if( begin != SQLITE_OK )
return SQLITE_OK;
instance_t *db_ctx = get_instance(storage);
if( BOOL_NO == db_ctx->in_transaction )
return SQLITE_MISUSE;
const sqlite_rc_t rc = sqlite3_step( prep_stmt(db_ctx->db, &(db_ctx->stmt_txn_rollback), "ROLLBACK TRANSACTION;") );
db_ctx->in_transaction = !(SQLITE_DONE == rc);
assert(db_ctx->in_transaction == BOOL_NO && "ouch");
return SQLITE_DONE == rc ? SQLITE_OK : rc;
}
#pragma mark -
#pragma mark Internal Implementation
/** insert_triple_sql
*/
typedef enum {
IDX_S_URI = 9,
IDX_S_BLANK,
IDX_P_URI,
IDX_O_URI,
IDX_O_BLANK,
IDX_O_TEXT,
IDX_O_LANGUAGE,
IDX_O_DATATYPE,
IDX_C_URI
}
idx_triple_column_t;
static librdf_statement *find_statement(librdf_storage *storage, librdf_node *context_node, librdf_statement *statement, const boolean_t create)
{
assert(statement && "statement must be set.");
assert(librdf_statement_is_complete(statement) && "statement must be complete.");
instance_t *db_ctx = get_instance(storage);
if( !create ) {
const hash_t stmt_id = stmt_hash(statement, context_node, db_ctx->digest);
assert(!isNULL_ID(stmt_id) && "mustn't be nil");
sqlite3_stmt *stmt = prep_stmt(db_ctx->db, &(db_ctx->stmt_triple_find), "SELECT id FROM triple_relations WHERE id = :stmt_id");
if( SQLITE_OK != bind_int(stmt, ":stmt_id", stmt_id) )
return NULL;
return SQLITE_ROW == sqlite3_step(stmt) ? statement : NULL;
}
const char *insert_triple_sql = // generated via tools/sql2c.sh insert_triple.sql
"INSERT OR IGNORE INTO triples(" "\n" \
" id," "\n" \
" s_uri_id, s_uri," "\n" \
" s_blank_id, s_blank," "\n" \
" p_uri_id, p_uri," "\n" \
" o_uri_id, o_uri," "\n" \
" o_blank_id, o_blank," "\n" \
" o_lit_id, o_datatype_id, o_datatype, o_language, o_text," "\n" \
" c_uri_id, c_uri" "\n" \
") VALUES (" "\n" \
" :stmt_id," "\n" \
" :s_uri_id, :s_uri," "\n" \
" :s_blank_id, :s_blank," "\n" \
" :p_uri_id, :p_uri," "\n" \
" :o_uri_id, :o_uri," "\n" \
" :o_blank_id, :o_blank," "\n" \
" :o_lit_id, :o_datatype_id, :o_datatype, :o_language, :o_text," "\n" \
" :c_uri_id, :c_uri" "\n" \
")" "\n" \
;
sqlite3_stmt *stmt = prep_stmt(db_ctx->db, &(db_ctx->stmt_triple_insert), insert_triple_sql);
if( SQLITE_OK != bind_stmt(db_ctx, statement, context_node, stmt) )
return NULL;
const sqlite_rc_t rc = sqlite3_step(stmt);
return SQLITE_DONE == rc ? statement : NULL;
}
#pragma mark -
#pragma mark Public Interface
#pragma mark Lifecycle & Housekeeping
/** Create a new storage.
*
* Setup SQLIte connection instance but don't open yet.
*/
static int pub_init(librdf_storage *storage, const char *name, librdf_hash *options)
{
if( !name ) {
free_hash(options);
return RET_ERROR;
}
instance_t *db_ctx = LIBRDF_CALLOC( instance_t *, 1, sizeof(*db_ctx) );
if( !db_ctx ) {
free_hash(options);
return RET_ERROR;
}
librdf_storage_set_instance(storage, db_ctx);
const size_t name_len = strlen(name);
char *name_copy = LIBRDF_MALLOC(char *, name_len + 1);
if( !name_copy ) {
free_hash(options);
return RET_ERROR;
}
strncpy(name_copy, name, name_len + 1);
db_ctx->name = name_copy;
if( !( db_ctx->digest = librdf_new_digest(get_world(storage), "MD5") ) ) {
free_hash(options);
return RET_ERROR;
}
if( BOOL_NO != librdf_hash_get_as_boolean(options, "new") )
db_ctx->is_new = BOOL_YES; /* default is NOT NEW */
/* Redland default is "PRAGMA synchronous normal" */
db_ctx->synchronous = SYNC_NORMAL;
char *synchronous = synchronous = librdf_hash_get(options, "synchronous");
if( synchronous ) {
for( int i = 0; synchronous_flags[i]; i++ ) {
if( !strcmp(synchronous, synchronous_flags[i]) ) {
db_ctx->synchronous = i;
break;
}
}
LIBRDF_FREE(char *, synchronous);
}
free_hash(options);
return RET_OK;
}
static void pub_terminate(librdf_storage *storage)
{
instance_t *db_ctx = get_instance(storage);
if( db_ctx == NULL )
return;
if( db_ctx->name )
LIBRDF_FREE(char *, (void *)db_ctx->name);
if( db_ctx->digest )
librdf_free_digest(db_ctx->digest);
LIBRDF_FREE(instance_t *, db_ctx);
}
static int pub_close(librdf_storage *storage)
{
instance_t *db_ctx = get_instance(storage);
if( !db_ctx->db )
return RET_OK;
finalize_stmt( &(db_ctx->stmt_txn_start) );
finalize_stmt( &(db_ctx->stmt_txn_commit) );
finalize_stmt( &(db_ctx->stmt_txn_rollback) );
finalize_stmt( &(db_ctx->stmt_triple_find) );
finalize_stmt( &(db_ctx->stmt_triple_insert) );
finalize_stmt( &(db_ctx->stmt_triple_delete) );
finalize_stmt( &(db_ctx->stmt_size) );
const sqlite_rc_t rc = sqlite3_close(db_ctx->db);
if( SQLITE_OK == rc ) {
db_ctx->db = NULL;
return RET_OK;
}
char *errmsg = (char *)sqlite3_errmsg(db_ctx->db);
librdf_log(get_world(storage), 0, LIBRDF_LOG_ERROR, LIBRDF_FROM_STORAGE, NULL, "SQLite database %s close failed - %s", db_ctx->name, errmsg);
return rc;
}
static int pub_open(librdf_storage *storage, librdf_model *model)
{
instance_t *db_ctx = get_instance(storage);
const boolean_t file_exists = ( 0 == access(db_ctx->name, F_OK) );
if( db_ctx->is_new && file_exists )
unlink(db_ctx->name);
// open DB
db_ctx->db = NULL;
{
const sqlite_rc_t rc = sqlite3_open(db_ctx->name, &db_ctx->db);
if( SQLITE_OK != rc ) {
const char *errmsg = sqlite3_errmsg(db_ctx->db);
librdf_log(get_world(storage), 0, LIBRDF_LOG_ERROR, LIBRDF_FROM_STORAGE, NULL, "SQLite database %s open failed - %s", db_ctx->name, errmsg);
return rc;
}
// http://stackoverflow.com/a/6618833
// sqlite3_profile(db_ctx->db, &profile, NULL);
// sqlite3_profile(db_ctx->db, &trace, NULL);
}
// set DB session PRAGMAs
if( SYNC_OFF <= db_ctx->synchronous ) {
char sql[250];
const size_t len = snprintf(sql, sizeof(sql) - 1, "PRAGMA synchronous=%s;", synchronous_flags[db_ctx->synchronous]);
assert(len < sizeof(sql) && "buffer too small.");
const sqlite_rc_t rc = exec_stmt(db_ctx->db, sql);
if( SQLITE_OK != rc ) {
pub_close(storage);
return rc;
}
}
{
const char *const sqls[] = {
"PRAGMA foreign_keys = ON;",
"PRAGMA recursive_triggers = ON;",
"PRAGMA encoding = 'UTF-8';",
NULL
};
for( int v = 0; sqls[v]; v++ ) {
const sqlite_rc_t rc = exec_stmt(db_ctx->db, sqls[v]);
if( SQLITE_OK != rc ) {
pub_close(storage);
return rc;
}
}
}
// check & update schema (run migrations)
{
sqlite_rc_t rc = SQLITE_OK;
sqlite3_stmt *stmt = NULL;
prep_stmt(db_ctx->db, &stmt, "PRAGMA user_version;");
if( SQLITE_ROW != ( rc = sqlite3_step(stmt) ) ) {
sqlite3_finalize(stmt);
pub_close(storage);
return rc;
}
const int schema_version = sqlite3_column_int(stmt, 0);
if( SQLITE_DONE != ( rc = sqlite3_step(stmt) ) ) {
sqlite3_finalize(stmt);
pub_close(storage);
return rc;
}
sqlite3_finalize(stmt);
const char *schema_mig_to_1_sql = // generated via tools/sql2c.sh schema_mig_to_1.sql
"PRAGMA foreign_keys = ON;" "\n" \
"PRAGMA recursive_triggers = ON;" "\n" \
"PRAGMA encoding = 'UTF-8';" "\n" \
" -- URIs for subjects and objects" "\n" \
"CREATE TABLE so_uris (" "\n" \
" id INTEGER PRIMARY KEY" "\n" \
" ,uri TEXT NOT NULL -- UNIQUE -- redundant constraint (hash should do), could be dropped to save space" "\n" \
");" "\n" \
" -- blank node IDs for subjects and objects" "\n" \
"CREATE TABLE so_blanks (" "\n" \
" id INTEGER PRIMARY KEY" "\n" \
" ,blank TEXT NOT NULL -- UNIQUE -- redundant constraint (hash should do), could be dropped to save space" "\n" \
");" "\n" \
" -- URIs for predicates" "\n" \
"CREATE TABLE p_uris (" "\n" \
" id INTEGER PRIMARY KEY" "\n" \
" ,uri TEXT NOT NULL -- UNIQUE -- redundant constraint (hash should do), could be dropped to save space" "\n" \
");" "\n" \
" -- URIs for literal types" "\n" \
"CREATE TABLE t_uris (" "\n" \
" id INTEGER PRIMARY KEY" "\n" \
" ,uri TEXT NOT NULL -- UNIQUE -- redundant constraint (hash should do), could be dropped to save space" "\n" \
");" "\n" \
" -- literal values" "\n" \
"CREATE TABLE o_literals (" "\n" \
" id INTEGER PRIMARY KEY" "\n" \
" ,datatype_id INTEGER NULL REFERENCES t_uris(id)" "\n" \
" ,language TEXT NULL" "\n" \
" ,text TEXT NOT NULL" "\n" \
");" "\n" \
" -- CREATE UNIQUE INDEX o_literals_index ON o_literals (text,language,datatype_id); -- redundant constraint (hash should do), could be dropped to save space" "\n" \
" -- URIs for context" "\n" \
"CREATE TABLE c_uris (" "\n" \
" id INTEGER PRIMARY KEY" "\n" \
" ,uri TEXT NOT NULL -- UNIQUE -- redundant constraint (hash should do), could be dropped to save space" "\n" \
");" "\n" \
"CREATE TABLE triple_relations (" "\n" \
" id INTEGER PRIMARY KEY" "\n" \
" ,s_uri_id INTEGER NULL REFERENCES so_uris(id)" "\n" \
" ,s_blank_id INTEGER NULL REFERENCES so_blanks(id)" "\n" \
" ,p_uri_id INTEGER NOT NULL REFERENCES p_uris(id)" "\n" \
" ,o_uri_id INTEGER NULL REFERENCES so_uris(id)" "\n" \
" ,o_blank_id INTEGER NULL REFERENCES so_blanks(id)" "\n" \
" ,o_lit_id INTEGER NULL REFERENCES o_literals(id)" "\n" \
" ,c_uri_id INTEGER NULL REFERENCES c_uris(id)" "\n" \
" , CONSTRAINT null_subject CHECK ( -- ensure uri/blank are mutually exclusive" "\n" \
" (s_uri_id IS NOT NULL AND s_blank_id IS NULL) OR" "\n" \
" (s_uri_id IS NULL AND s_blank_id IS NOT NULL)" "\n" \
" )" "\n" \
" , CONSTRAINT null_object CHECK ( -- ensure uri/blank/literal are mutually exclusive" "\n" \
" (o_uri_id IS NOT NULL AND o_blank_id IS NULL AND o_lit_id IS NULL) OR" "\n" \
" (o_uri_id IS NULL AND o_blank_id IS NOT NULL AND o_lit_id IS NULL) OR" "\n" \
" (o_uri_id IS NULL AND o_blank_id IS NULL AND o_lit_id IS NOT NULL)" "\n" \
" )" "\n" \
");" "\n" \
" -- redundant constraint (hash should do), could be dropped to save space:" "\n" \
" -- CREATE UNIQUE INDEX triple_relations_index ON triple_relations(s_uri_id,s_blank_id,p_uri_id,o_uri_id,o_blank_id,o_lit_id,c_uri_id);" "\n" \
" -- optional indexes for lookup performance, mostly on DELETE." "\n" \
"CREATE INDEX triple_relations_index_s_uri_id ON triple_relations(s_uri_id); -- WHERE s_uri_id IS NOT NULL;" "\n" \
"CREATE INDEX triple_relations_index_s_blank_id ON triple_relations(s_blank_id); -- WHERE s_blank_id IS NOT NULL;" "\n" \
"CREATE INDEX triple_relations_index_p_uri_id ON triple_relations(p_uri_id); -- WHERE p_uri_id IS NOT NULL;" "\n" \
"CREATE INDEX triple_relations_index_o_uri_id ON triple_relations(o_uri_id); -- WHERE o_uri_id IS NOT NULL;" "\n" \
"CREATE INDEX triple_relations_index_o_blank_id ON triple_relations(o_blank_id); -- WHERE s_blank_id IS NOT NULL;" "\n" \
"CREATE INDEX triple_relations_index_o_lit_id ON triple_relations(o_lit_id); -- WHERE o_lit_id IS NOT NULL;" "\n" \
"CREATE INDEX o_literals_index_datatype_id ON o_literals(datatype_id); -- WHERE datatype_id IS NOT NULL;" "\n" \
"CREATE VIEW triples AS" "\n" \
"SELECT" "\n" \
" -- all *_id (hashes):" "\n" \
" triple_relations.id AS id" "\n" \
" ,s_uri_id" "\n" \
" ,s_blank_id" "\n" \
" ,p_uri_id" "\n" \
" ,o_uri_id" "\n" \
" ,o_blank_id" "\n" \
" ,o_lit_id" "\n" \
" ,o_literals.datatype_id AS o_datatype_id" "\n" \
" ,c_uri_id" "\n" \
" -- all joined values:" "\n" \
" ,s_uris.uri AS s_uri" "\n" \
" ,s_blanks.blank AS s_blank" "\n" \
" ,p_uris.uri AS p_uri" "\n" \
" ,o_uris.uri AS o_uri" "\n" \
" ,o_blanks.blank AS o_blank" "\n" \
" ,o_literals.text AS o_text" "\n" \
" ,o_literals.language AS o_language" "\n" \
" ,o_lit_uris.uri AS o_datatype" "\n" \
" ,c_uris.uri AS c_uri" "\n" \
"FROM triple_relations" "\n" \
"LEFT OUTER JOIN so_uris AS s_uris ON triple_relations.s_uri_id = s_uris.id" "\n" \
"LEFT OUTER JOIN so_blanks AS s_blanks ON triple_relations.s_blank_id = s_blanks.id" "\n" \
"INNER JOIN p_uris AS p_uris ON triple_relations.p_uri_id = p_uris.id" "\n" \
"LEFT OUTER JOIN so_uris AS o_uris ON triple_relations.o_uri_id = o_uris.id" "\n" \
"LEFT OUTER JOIN so_blanks AS o_blanks ON triple_relations.o_blank_id = o_blanks.id" "\n" \
"LEFT OUTER JOIN o_literals AS o_literals ON triple_relations.o_lit_id = o_literals.id" "\n" \
"LEFT OUTER JOIN t_uris AS o_lit_uris ON o_literals.datatype_id = o_lit_uris.id" "\n" \
"LEFT OUTER JOIN c_uris AS c_uris ON triple_relations.c_uri_id = c_uris.id" "\n" \
";" "\n" \
"CREATE TRIGGER triples_insert INSTEAD OF INSERT ON triples" "\n" \
"FOR EACH ROW BEGIN" "\n" \
" -- subject uri/blank" "\n" \
" INSERT OR IGNORE INTO so_uris (id,uri) VALUES (NEW.s_uri_id, NEW.s_uri);" "\n" \
" INSERT OR IGNORE INTO so_blanks (id,blank) VALUES (NEW.s_blank_id, NEW.s_blank);" "\n" \
" -- predicate uri" "\n" \
" INSERT OR IGNORE INTO p_uris (id,uri) VALUES (NEW.p_uri_id, NEW.p_uri);" "\n" \
" -- object uri/blank" "\n" \
" INSERT OR IGNORE INTO so_uris (id,uri) VALUES (NEW.o_uri_id, NEW.o_uri);" "\n" \
" INSERT OR IGNORE INTO so_blanks (id,blank) VALUES (NEW.o_blank_id, NEW.o_blank);" "\n" \
" -- object literal" "\n" \
" INSERT OR IGNORE INTO t_uris (id,uri) VALUES (NEW.o_datatype_id, NEW.o_datatype);" "\n" \
" INSERT OR IGNORE INTO o_literals(id,datatype_id,language,text) VALUES (NEW.o_lit_id, NEW.o_datatype_id, NEW.o_language, NEW.o_text);" "\n" \
" -- context uri" "\n" \
" INSERT OR IGNORE INTO c_uris (id,uri) VALUES (NEW.c_uri_id, NEW.c_uri);" "\n" \
" -- triple" "\n" \
" INSERT INTO triple_relations(id, s_uri_id, s_blank_id, p_uri_id, o_uri_id, o_blank_id, o_lit_id, c_uri_id)" "\n" \
" VALUES (NEW.id, NEW.s_uri_id, NEW.s_blank_id, NEW.p_uri_id, NEW.o_uri_id, NEW.o_blank_id, NEW.o_lit_id, NEW.c_uri_id);" "\n" \
"END;" "\n" \
"CREATE TRIGGER triples_delete INSTEAD OF DELETE ON triples" "\n" \
"FOR EACH ROW BEGIN" "\n" \
" -- triple" "\n" \
" DELETE FROM triple_relations WHERE id = OLD.id;" "\n" \
" -- subject uri/blank" "\n" \
" DELETE FROM so_uris WHERE (OLD.s_uri_id IS NOT NULL) AND (0 == (SELECT COUNT(id) FROM triple_relations WHERE s_uri_id = OLD.s_uri_id)) AND (id = OLD.s_uri_id);" "\n" \
" DELETE FROM so_blanks WHERE (OLD.s_blank_id IS NOT NULL) AND (0 == (SELECT COUNT(id) FROM triple_relations WHERE s_blank_id = OLD.s_blank_id)) AND (id = OLD.s_blank_id);" "\n" \
" -- predicate uri" "\n" \
" DELETE FROM p_uris WHERE (OLD.p_uri_id IS NOT NULL) AND (0 == (SELECT COUNT(id) FROM triple_relations WHERE p_uri_id = OLD.p_uri_id)) AND (id = OLD.p_uri_id);" "\n" \
" -- object uri/blank" "\n" \
" DELETE FROM so_uris WHERE (OLD.o_uri_id IS NOT NULL) AND (0 == (SELECT COUNT(id) FROM triple_relations WHERE o_uri_id = OLD.o_uri_id)) AND (id = OLD.o_uri_id);" "\n" \
" DELETE FROM so_blanks WHERE (OLD.o_blank_id IS NOT NULL) AND (0 == (SELECT COUNT(id) FROM triple_relations WHERE o_blank_id = OLD.o_blank_id)) AND (id = OLD.o_blank_id);" "\n" \
" -- object literal" "\n" \
" DELETE FROM o_literals WHERE (OLD.o_lit_id IS NOT NULL) AND (0 == (SELECT COUNT(id) FROM triple_relations WHERE o_lit_id = OLD.o_lit_id)) AND (id = OLD.o_lit_id);" "\n" \
" DELETE FROM t_uris WHERE (OLD.o_datatype_id IS NOT NULL) AND (0 == (SELECT COUNT(id) FROM o_literals WHERE datatype_id = OLD.o_datatype_id)) AND (id = OLD.o_datatype_id);" "\n" \
" -- context uri" "\n" \
" DELETE FROM c_uris WHERE (OLD.c_uri_id IS NOT NULL) AND (0 == (SELECT COUNT(id) FROM triple_relations WHERE c_uri_id = OLD.c_uri_id)) AND (id = OLD.c_uri_id);" "\n" \
"END;" "\n" \
"PRAGMA user_version=1;" "\n" \
;
const char *schema_mig_to_2_sql = // generated via tools/sql2c.sh schema_mig_to_2.sql
"DROP TRIGGER triples_delete;" "\n" \
"CREATE TRIGGER triples_delete INSTEAD OF DELETE ON triples" "\n" \
"FOR EACH ROW BEGIN" "\n" \
" -- subject uri/blank" "\n" \
" DELETE FROM so_uris WHERE (OLD.s_uri_id IS NOT NULL) AND (0 == (SELECT COUNT(id) FROM triple_relations WHERE s_uri_id = OLD.s_uri_id)) AND (id = OLD.s_uri_id);" "\n" \
" DELETE FROM so_blanks WHERE (OLD.s_blank_id IS NOT NULL) AND (0 == (SELECT COUNT(id) FROM triple_relations WHERE s_blank_id = OLD.s_blank_id)) AND (id = OLD.s_blank_id);" "\n" \
" -- predicate uri" "\n" \
" DELETE FROM p_uris WHERE (OLD.p_uri_id IS NOT NULL) AND (0 == (SELECT COUNT(id) FROM triple_relations WHERE p_uri_id = OLD.p_uri_id)) AND (id = OLD.p_uri_id);" "\n" \
" -- object uri/blank" "\n" \
" DELETE FROM so_uris WHERE (OLD.o_uri_id IS NOT NULL) AND (0 == (SELECT COUNT(id) FROM triple_relations WHERE o_uri_id = OLD.o_uri_id)) AND (id = OLD.o_uri_id);" "\n" \
" DELETE FROM so_blanks WHERE (OLD.o_blank_id IS NOT NULL) AND (0 == (SELECT COUNT(id) FROM triple_relations WHERE o_blank_id = OLD.o_blank_id)) AND (id = OLD.o_blank_id);" "\n" \
" -- object literal" "\n" \
" DELETE FROM o_literals WHERE (OLD.o_lit_id IS NOT NULL) AND (0 == (SELECT COUNT(id) FROM triple_relations WHERE o_lit_id = OLD.o_lit_id)) AND (id = OLD.o_lit_id);" "\n" \
" DELETE FROM t_uris WHERE (OLD.o_datatype_id IS NOT NULL) AND (0 == (SELECT COUNT(id) FROM o_literals WHERE datatype_id = OLD.o_datatype_id)) AND (id = OLD.o_datatype_id);" "\n" \
" -- context uri" "\n" \
" DELETE FROM c_uris WHERE (OLD.c_uri_id IS NOT NULL) AND (0 == (SELECT COUNT(id) FROM triple_relations WHERE c_uri_id = OLD.c_uri_id)) AND (id = OLD.c_uri_id);" "\n" \
" -- triple" "\n" \
" DELETE FROM triple_relations WHERE id = OLD.id;" "\n" \
"END;" "\n" \
"PRAGMA user_version=2;" "\n" \
;
const char *const migrations[] = {
schema_mig_to_1_sql,
schema_mig_to_2_sql,
NULL
};
{
const size_t mig_count = sizeof(migrations) / sizeof(migrations[0]) - 1;
assert(!migrations[mig_count] && "migrations must be NULL terminated.");
if( mig_count < schema_version ) {
// schema is more recent than this source file knows to handle.
pub_close(storage);
return RET_ERROR;
}
}
const sqlite_rc_t begin = transaction_start(storage);
for( int v = schema_version; migrations[v]; v++ ) {
if( SQLITE_OK != ( rc = exec_stmt(db_ctx->db, migrations[v]) ) ) {
transaction_rollback(storage, begin);
pub_close(storage);
return rc;
}
{
// assert new schema version
sqlite3_stmt *stmt = NULL;
prep_stmt(db_ctx->db, &stmt, "PRAGMA user_version");
if( SQLITE_ROW != ( rc = sqlite3_step(stmt) ) ) {
sqlite3_finalize(stmt);