aboutsummaryrefslogtreecommitdiff
path: root/pyble.py
blob: b399a295f9817cfa13849749f2c8bd9ea400c014 (plain) (blame)
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
from orator import DatabaseManager, Model, Schema

class Part(Model):
    __timestamps__ = False
    __fillable__ = ['name']

class Book(Model):
    __timestamps__ = False
    __fillable__ = ['canonical_name']

class Verse(Model):
    __timestamps__ = False
    __fillable__ = ['book', 'chapter', 'nr']

class Translation(Model):
    __timestamps__ = False
    __fillable__ = ['name']

class TranslatedVerse(Model):
    __timestamps__ = False
    __table__ = 'translated_verses'
    __fillable__ = ['translation', 'book', 'chapter', 'nr', 'text']

class Passage(Model):
    __timestamps__ = False
    __fillable__ = ['fst', 'snd']

class CrossReference(Model):
    __timestamps__ = False
    __fillable__ = ['passage_id1', 'passage_id2', 'relevance']

def setup_tables(db):
    schema = Schema(db)

    with schema.create('parts') as table:
        table.string('name')
        table.primary('name')

    with schema.create('books') as table:
        table.string('canonical_name').primary()

    with schema.create('verses') as table:
        table.string('book')
        table.small_integer('chapter')
        table.small_integer('nr')
        table.foreign('book').references('canonical_name').on('books')
        table.primary(['book', 'chapter', 'nr'])

    with schema.create('translations') as table:
        table.string('name').primary()
    
    with schema.create('passages') as table:
        table.increments('id')
        table.string('fst_book')
        table.small_integer('fst_chapter')
        table.small_integer('fst_nr')
        table.string('snd_book')
        table.small_integer('snd_chapter')
        table.small_integer('snd_nr')
        table.foreign(['fst_book', 'fst_chapter', 'fst_nr'])\
                .references(['book', 'chapter', 'nr']).on('verses')\
                .on_delete('restrict').on_update('cascade')
        table.foreign(['snd_book', 'snd_chapter', 'snd_nr'])\
                .references(['book', 'chapter', 'nr']).on('verses')\
                .on_delete('restrict').on_update('cascade')
        table.unique(['fst_book', 'fst_chapter', 'fst_nr',
            'snd_book', 'snd_chapter', 'snd_nr'])

    with schema.create('crossreferences') as table:
        table.integer('passage_id1')
        table.integer('passage_id2')
        table.small_integer('relevance').default(0)
        table.foreign('passage_id1').references('id').on('passages')
        table.foreign('passage_id2').references('id').on('passages')
        table.primary(['passage_id1', 'passage_id2'])

    with schema.create('translated_verses') as table:
        table.string('translation')
        table.string('book')
        table.small_integer('chapter')
        table.small_integer('nr')
        table.string('text')
        table.foreign('translation').references('name').on('translations')
        table.foreign(['book', 'chapter', 'nr'])\
                .references(['book', 'chapter', 'nr']).on('verses')\
                .on_delete('restrict').on_update('cascade')
        table.primary(['translation', 'book', 'chapter', 'nr'])