Module: Audrey::Engine::SQLite3::Init

Defined in:
lib/audrey/engine/sqlite3/init.rb

Overview

Audrey::Engine::SQLite3::Init

Class Method Summary collapse

Class Method Details

.build(p_dbh) ⇒ Object


build



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/audrey/engine/sqlite3/init.rb', line 10

def self.build(p_dbh)
  # create database file
  # @dbh = SQLite3::Database.new(engine.path)
  @dbh = p_dbh
  
  # go into transaction
  @dbh.transaction
  
  # temp tables
  # Audrey::Engine::SQLite3.session_tables @dbh
  
  # create and populate tables
  journal()
  settings()
  partitions()
  objects()
  relationships()
  views()
  sessions()
  
  # commit
  @dbh.commit
end

.journalObject


journal



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
# File 'lib/audrey/engine/sqlite3/init.rb', line 41

def self.journal
  # $tm.hrm
  
  # build
  @dbh.execute_batch "  -- create table\n  create table journal (\n    pk integer primary key autoincrement,\n    ts datetime default current_timestamp,\n    tbl varchar(15)  not null,\n    fpk varchar(36)  not null,\n    action varchar(1) not null,\n    dsc json\n  );\n  \n  -- cannot update journal\n  create trigger journal_bu\n    before update on journal\n  begin\n    select raise(abort, 'cannot-update-journal-record');\n  end;\n  \n  -- cannot delete journal\n  create trigger journal_bd\n    before delete on journal\n  begin\n    select raise(abort, 'cannot-delete-journal-record');\n  end;\n  SQL\nend\n"

.objectsObject


objects



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
# File 'lib/audrey/engine/sqlite3/init.rb', line 167

def self.objects
  # build
  @dbh.execute_batch "  -- aclasses table\n  create table aclasses(\n    pk integer primary key autoincrement,\n    name varchar(255) unique not null,\n    check (length(name)>0)\n  );\n  \n  -- cannot update aclasses\n  create trigger aclasses_bu\n  before update on aclasses\n  begin\n    select raise(abort, 'cannot-update-aclasses');\n  end;\n  \n  -- base_objects table\n  create table base_objects (\n    pk varchar(36) primary key,\n    partition varchar(1) not null references partitions(pk),\n    root boolean,\n    hsa varchar(1) not null check(hsa in ('h', 's', 'a')),\n    aclass_pk integer not null references aclasses(pk) on delete restrict,\n    scalar text,\n    xt uuid references base_objects(pk) unique,\n    changed datetime default current_timestamp,\n    check (root=1)\n  );\n  \n  -- indexes\n  create unique index base_objects_partition_root on base_objects(partition, root);\n  create index base_objects_partition  on base_objects(partition);\n  create index base_objects_hsa        on base_objects(hsa);\n  create index base_objects_aclass_pk  on base_objects(aclass_pk);\n  create index base_objects_scalar     on base_objects(scalar);\n  create index base_objects_changed    on base_objects(changed);\n  \n  -- cannot update most fields\n  create trigger base_objects_bu_pk        before update of pk        on base_objects begin select raise(abort, 'cannot-update-object-pk');        end;\n  create trigger base_objects_bu_partition before update of partition on base_objects begin select raise(abort, 'cannot-update-object-partition'); end;\n  create trigger base_objects_bu_root      before update of root      on base_objects begin select raise(abort, 'cannot-update-object-root');      end;\n  create trigger base_objects_bu_hsa       before update of hsa       on base_objects begin select raise(abort, 'cannot-update-object-hsa');       end;\n  create trigger base_objects_bu_aclass_pk before update of aclass_pk on base_objects begin select raise(abort, 'cannot-update-object-aclass_pk'); end;\n  create trigger base_objects_bu_scalar    before update of scalar    on base_objects begin select raise(abort, 'cannot-update-object-scalar');    end;\n  \n  -- cannot update xt once it has been defined\n  create trigger base_objects_bu_xt\n    before update of xt on base_objects when (old.xt is not null)\n  begin\n    select raise(abort, 'cannot-update-object-xt-once-set');\n  end;\n  \n  -- cannot delete root object\n  create trigger base_objects_bd_root\n    before delete on base_objects when (old.root)\n  begin\n    select raise(abort, 'cannot-delete-root-object');\n  end;\n  \n  -- insert: journal\n  create trigger base_objects_ai_journal\n    after insert on base_objects when (select value from settings where pk='journal')\n  begin\n    insert into journal(tbl, fpk, action, dsc) values(\n      'o',\n      new.pk,\n      'i',\n      insert_object_journal(\n        new.partition,\n        new.root,\n        (select name from aclasses where pk=new.aclass_pk),\n        new.scalar,\n        new.xt\n      )\n    );\n  end;\n  \n  -- update: journal\n  create trigger base_objects_au_journal\n    after update on base_objects when (select value from settings where pk='journal')\n  begin\n    insert into journal(tbl, fpk, action, dsc) values('o', new.pk, 'u',\n      '{' ||\n      '\"x\":\"' || replace(new.xt, '\"', '\\\"') || '\"' ||\n      '}');\n  end;\n  \n  -- after delete on object with xt\n  create trigger base_objects_ad\n    after delete on base_objects\n  begin\n    delete from base_objects where pk=old.xt;\n  end;\n  \n  -- delete: journal\n  create trigger base_objects_ad_journal\n    after delete on base_objects when (select value from settings where pk='journal')\n  begin\n    insert into journal(tbl, fpk, action) values('o', old.pk, 'd');\n  end;\n  \n  -- initial aclass\n  insert into aclasses(name) values('Audrey::Object::Hash');\n  insert into aclasses(name) values('Audrey::Object::Root');\n  \n  -- add root objects\n  insert into base_objects(pk, partition, hsa, aclass_pk, root, changed) values('1', 'm', 'h', 2, 1, null);\n  insert into base_objects(pk, partition, hsa, aclass_pk, root, changed) values('2', 'r', 'h', 2, 1, null);\n  insert into base_objects(pk, partition, hsa, aclass_pk, root, changed) values('3', 'd', 'h', 2, 1, null);\n  \n  -- objects view\n  create view objects as\n    select\n      o.pk, o.partition, o.root, o.scalar, o.xt, o.changed, o.hsa,\n      (select name from aclasses where pk=o.aclass_pk) as aclass\n    from base_objects o\n    where o.partition = current_partition();\n    \n  -- insert into objects\n  create trigger objects_insert\n    instead of insert on objects\n  begin\n    -- notify database of change\n    -- select change_to_true(new.aclass);\n    \n    -- ensure aclass record\n    insert or ignore into\n    aclasses(name)\n    values(new.aclass);\n    \n    -- insert\n    insert into\n      base_objects(\n        pk,\n        partition,\n        root,\n        hsa,\n        aclass_pk,\n        scalar,\n        xt\n      )\n      \n      values(\n        new.pk,\n        current_partition(),\n        new.root,\n        new.hsa,\n        (select pk from aclasses where name=new.aclass),\n        new.scalar,\n        new.xt\n      );\n  end;\n  \n  /*\n  -- update objects\n  create trigger objects_update\n    instead of update of changed on objects\n  begin\n    update base_objects\n    set changed = new.changed\n    where pk = new.pk;\n  end;\n  */\n  \n  -- delete from objects\n  create trigger objects_delete\n    instead of delete on objects\n  begin\n    delete from base_objects where pk=old.pk and partition = current_partition();\n  end;\n  \n  -- end of sql\n  SQL\nend\n"

.partitionsObject


partitions



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
# File 'lib/audrey/engine/sqlite3/init.rb', line 114

def self.partitions
  # $tm.hrm
  
  # build
  @dbh.execute_batch "    -- create table\n    create table partitions (\n      pk varchar(1) primary key,\n      name varchar(30) unique not null\n    );\n    \n    -- cannot update pk\n    create trigger partitions_bu_pk\n      before update of pk on partitions\n    begin\n      select raise(abort, 'cannot-update-partition-pk');\n    end;\n    \n    -- create partitions\n    insert into partitions(pk, name) values('m', 'main');\n    insert into partitions(pk, name) values('r', 'roles');\n    insert into partitions(pk, name) values('d', 'design');\n    \n    -- cannot insert partitions\n    create trigger partitions_bi\n      before insert on partitions\n    begin\n      select raise(abort, 'cannot-insert-new-partition');\n    end;\n    \n    -- cannot update\n    create trigger partitions_bu\n      before update on partitions\n    begin\n      select raise(abort, 'cannot-update-partition');\n    end;\n    \n    -- cannot delete partitions\n    create trigger partitions_bd\n      before delete on partitions\n    begin\n      select raise(abort, 'cannot-delete-partition');\n    end;\n  SQL\nend\n"

.relationshipsObject


relationships TODO: Need to rework relationships table so that ord can be changed.



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
# File 'lib/audrey/engine/sqlite3/init.rb', line 351

def self.relationships()
  # $tm.hrm
  
  # build
  @dbh.execute_batch "  -- create table\n  create table relationships (\n    pk varchar(36) primary key,\n    parent varchar(36) not null references base_objects(pk) on delete cascade,\n    child varchar(36) not null references base_objects(pk) on delete cascade,\n    ord integer not null,\n    hkey varchar(255),\n    check(typeof(ord)='integer')\n  );\n  \n  -- indexes\n  create unique index relationships_parent_hkey on relationships(parent, hkey);\n  create index relationships_parent on relationships(parent);\n  \n  -- must be in same partition\n  create trigger relationships_bi_partitions\n    before insert on relationships\n  begin\n    select case\n      when (select partition from base_objects where pk=new.parent) <> (select partition from base_objects where pk=new.child) then\n        raise(abort, 'relationship-objects-not-in-same-partition')\n      end;\n  end;\n  \n  -- parent must be array or hash\n  create trigger relationships_bi_hsa\n    before insert on relationships\n  begin\n    select case\n      when (select hsa from objects where pk=new.parent) not in ('h', 'a') then\n        raise(abort, 'relationship-parent-base-class-does-not-support-relatonships')\n    end;\n  end;\n  \n  -- cannot update\n  create trigger relationships_bu\n    before update on relationships\n  begin\n    select raise(abort, 'cannot-update-relationships');\n  end;\n  \n  -- insert: journal\n  create trigger relationships_ai_journal\n    after insert on relationships when (select value from settings where pk='journal')\n  begin\n    insert into journal(tbl, fpk, action, dsc) values('r', new.pk, 'i',\n      '{' ||\n      '\"p\":\"' || replace(new.parent, '\"', '\\\"') || '\",' ||\n      '\"c\":\"' || replace(new.child, '\"', '\\\"') || '\",' ||\n      '\"ord\":' || new.ord || ',' ||\n      '\"hk\":\"' || replace(new.hkey, '\"', '\\\"') || '\"' ||\n      '}');\n  end;\n  \n  -- insert: changed\n  create trigger relationships_ai_changed\n    after insert on relationships\n  begin\n    update base_objects set changed=current_timestamp where pk=new.parent;\n  end;\n  \n  -- journal: update\n  create trigger relationships_au_journal\n    after update on relationships when (select value from settings where pk='journal')\n  begin\n    insert into journal(tbl, fpk, action, dsc) values('r', new.pk, 'u',\n      '{' ||\n      '\"c\":\"' || replace(new.child, '\"', '\\\"') || '\",' ||\n      '\"ord\":' || new.ord || ',' ||\n      '\"hk\":\"' || replace(new.hkey, '\"', '\\\"') || '\"' ||\n      '}');\n  end;\n  \n  -- delete: journal\n  create trigger relationships_ad_journal\n    after delete on relationships when (select value from settings where pk='journal')\n  begin\n    insert into journal(tbl, fpk, action) values('r', old.pk, 'd');\n  end;\n  \n  -- delete: changed\n  create trigger relationships_ad_changed\n    after delete on relationships\n  begin\n    update base_objects set changed=current_timestamp where pk=old.parent;\n  end;\n  \n  -- delete parent if child is graph\n  -- KLUDGE: Cannot figure out why, but if this routine attempts to delete\n  -- from objects view, the delete never happens. So deleting directly\n  -- from base_objects.\n  create trigger relationships_ad_graph\n    after delete on relationships when (select graph_field((select aclass from objects where pk=old.parent), old.hkey))\n  begin\n    delete from base_objects where pk=old.parent;\n  end;\n  \n  -- end of sql\n  SQL\nend\n"

.sessionsObject


sessions



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
# File 'lib/audrey/engine/sqlite3/init.rb', line 513

def self.sessions
  # $tm.hrm
  
  # build
  @dbh.execute_batch "  -- sessions\n  create table sessions (\n    pk varchar(36) primary key,\n    pw varchar(36)\n  );\n  \n  -- cannot update sessions\n  create trigger sessions_bu\n    before update on sessions\n  begin\n    select raise(abort, 'cannot-update-sessions');\n  end;\n  \n  -- results\n  create table results (\n    pk varchar(36) primary key,\n    session varchar(36) not null references sessions(pk) on delete cascade\n  );\n  \n  -- cannot update results\n  create trigger results_bu\n    before update on results\n  begin\n    select raise(abort, 'cannot-update-results');\n  end;\n  \n  -- result_rows\n  create table result_rows (\n    pk varchar(36) primary key,\n    result varchar(36) not null references results(pk) on delete cascade,\n    object varchar(36) not null references base_objects(pk) on delete cascade\n  );\n  \n  -- cannot update result_rows\n  create trigger result_rows_bu\n    before update on result_rows\n  begin\n    select raise(abort, 'cannot-update-result_rows');\n  end;\n  \n  -- end of SQL\n  SQL\nend\n"

.settingsObject


settings



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
# File 'lib/audrey/engine/sqlite3/init.rb', line 79

def self.settings
  # $tm.hrm
  
  # build
  @dbh.execute_batch "  -- create table\n  create table settings (\n    pk varchar(5) primary key,\n    value varchar(64) not null\n  );\n  \n  -- cannot update pk\n  create trigger settings_bu_pk\n    before update of pk on settings\n  begin\n    select raise(abort, 'cannot-update-setting-pk');\n  end;\n  \n  -- add setting for journaling\n  insert into settings(pk, value) values('journal', 1);\n  \n  -- create version setting\n  insert into settings(pk, value) values('version', '0.1');\n  \n  -- end sql\n  SQL\nend\n"

.viewsObject


views



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
# File 'lib/audrey/engine/sqlite3/init.rb', line 464

def self.views
  # $tm.hrm
  
  # build
  @dbh.execute_batch "  -- hash_pairs\n  create view hash_pairs as\n  select c.partition, r.parent, r.child, r.hkey, c.scalar, r.ord\n  from objects c, relationships r\n  where (c.pk = r.child) and (r.hkey is not null)\n  order by ord;\n  \n  -- fcos\n  create view fcos as\n    select pk\n    from objects\n    where aclass_fco(aclass);\n  \n  -- fco_descendants\n  create view fco_descendants as\n    with recursive ancestor(child) as (\n      select child from relationships where parent in (select pk from fcos)\n      union\n      select r.child from ancestor a inner join relationships r on r.parent = a.child\n    )\n    select child from ancestor;\n    \n  -- fco_traced\n  create view fco_traced as\n    select distinct pk\n    from objects\n    where aclass_fco(aclass) or (pk in (select child from fco_descendants));\n  \n  -- fco_not_traced\n  create view fco_not_traced as\n    select pk from objects\n    where pk not in (select pk from fco_traced);\n    \n  -- end of SQL\n  SQL\nend\n"