db_model.anubis
6.1 KB
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
/*
* Created by PyramIDE.
* User: フランスのトトロ
* Date: 01/01/2016
* Time: 1:34
* © Calexium
*/
The HayamiKi (早見木) project
Interfacing, generating and controling a database with Anubis
Author: David René
In order to generate such tools, DB_Model needs a formal
description of the database.
*** (1.2) 'MaybeNull'.
This type scheme is isomorphic to 'Maybe', and is used for representing data which
can have the value 'NULL'. We don't use 'Maybe' for this purpose because
we want to avoid the misleading 'Maybe(Maybe(...))'. Instead, we will have
sometimes 'Maybe(MaybeNull(...))', so that 'null' means 'NULL', and 'failure'
means an error.
public type MaybeNull($T):
null,
not_null($T value).
Conversion tools between date, time and datetime in ISO-8601 format and 'Int'
(number of seconds since the epoch) are provided in 'library/tools/ISO-8601.anubis'.
*** (1.3) 'HK_Model_Attr' (attributes of columns).
Possible attributes of columns: (each column has a list of such attributes)
Of course, 'NOT NULL' is not an alternative of 'HK_Model_Attr' because it is already coded into the
database type itself (type 'HK_Model_Field' above).
*** (3.7) 'DB_Model_Table' (describing a table).
Description of a table:
Description of a whole database:
// list of all tables in the database
//public define Maybe(String) convert_to_date (MaybeNull(ByteArray) b).
//public define MaybeNull(String) convert_to_date_or_null (MaybeNull(ByteArray) b).
*** (8.3.6) Converting 'integer?' and 'integer?_or_null' to and fro.
The next functions are used for integer16, integer32 and integer.
//public define Maybe(Int) convert_to_integer (MaybeNull(ByteArray) b).
//public define MaybeNull(Int) convert_to_integer_or_null (MaybeNull(ByteArray) b).
*** (8.3.7) Converting 'char/text' and 'char_or_null/text_or_null' to and fro.
The same functions are used for 'text/text_or_null' and 'vartext/vartext_or_null'.
Litteral texts must be 'prepared' before they can be included into
SQL commands (this amounts to doubling the single quotes).
public define MetaSQL_Prepared prepare_text (String t).
public define MetaSQL_Prepared prepare_text_or_null (MaybeNull(String) t).
Conversely, texts arrive from the database in the form of byte arrays which must be
converted to strings. The two functions for converting to 'text' and to 'text_or_null'
are almost the same one. The only difference is that 'null' is interpreted as an error in
the case of 'text'.
--- That's all for the public part !----------------------------------------------------------------
read tools/basis.anubis
read tools/base64.anubis
read system/string.anubis
read tools/ISO-8601.anubis
read calexium_lib/web/widgets/icons_set.anubis
*** [1] Tools.
*** [1.1] Concatenating strings which may not exist.
The concatenation function '+' for strings is defined in 'tools/basis.anubis'. Here we define
an extension of it to strings which may not exist (i.e. data of type 'Maybe(String)'). Of course,
the result is always of type 'Maybe(String)'.
define macro Maybe(String)
Maybe(String) s + String t
=
if s is
{
failure then failure,
success(s1) then success(s1+t)
}.
define macro Maybe(String)
String s + Maybe(String) t
=
if t is
{
failure then failure,
success(t1) then success(s+t1)
}.
define macro Maybe(String)
Maybe(String) s + Maybe(String) t
=
if s is
{
failure then failure,
success(s1) then if t is
{
failure then failure,
success(t1) then success(s1+t1)
}
}.
*** [2] Verifications concerning the description of the database and queries.
define Bool
has_forbidden_references
(
String table_name, // the first 3 arguments concern the current table.
MetaSQL_PrimKey pk,
List(HK_Model_Column) columns,
List(HK_Model_Column) forwards, // subsequent tables
List(String) backwards // list of backwards table names
) =
if columns is
{
[ ] then false, // no forbidden reference found
[col1 . other_cols] then
if is_forbidden_reference(table_name,col1,forwards,backwards)
then true
else has_forbidden_references(table_name,pk,other_cols,forwards,backwards)
}.
define Bool // returns 'true' if there is at least one forbidden cycle.
has_forbidden_cycles
(
String db_name,
List(HK_Model_Column) tables,
List(String) backwards // names of 'backwards' tables
) =
if tables is
{
[ ] then false, // no forbidden cycle found
[tab1 . other_tabs] then if tab1 is table(name1,pk1,cols1) then
if has_forbidden_references(name1,pk1,cols1,other_tabs,backwards)
then true
else has_forbidden_cycles(db_name,other_tabs,[name1 . backwards])
}.
define Bool
is_forbidden_column_name
(
String name
) =
name = "id" | name = "metasqlcheck".
define Bool
has_forbidden_column_names
(
HK_Model_Column tab
) =
if tab is table(_,_,cols) then
mapor((HK_Model_Column cs) |->
if cs is col(name,_,_) then is_forbidden_column_name(name),
cols).
define Bool
has_forbidden_column_names
(
DB_Model_DB db
) =
if db is database(_,tables) then
mapor(has_forbidden_column_names,tables).
*** [3] Generating the target file.
*** [3.1] Converting a database type into the corresponding Anubis type: