|
@@ -1,194 +0,0 @@
|
1
|
|
-# -*- coding: utf-8 -*-
|
2
|
|
-
|
3
|
|
-import sqlalchemy as sql
|
4
|
|
-import re # Converting string to sqlalchemy types
|
5
|
|
-from Database import sqlutils
|
6
|
|
-
|
7
|
|
-
|
8
|
|
-def init_db(dbconfname='default', alchemy_logs=None, schema=None):
|
9
|
|
-
|
10
|
|
- dbe = sqlutils.get_engine(dbconfname, alchemy_logs)
|
11
|
|
- meta = sqlutils.meta(dbe)
|
12
|
|
- meta.reflect()
|
13
|
|
- meta.drop_all(dbe)
|
14
|
|
- # refresh meta (maybe useless)
|
15
|
|
- meta = sqlutils.meta(dbe)
|
16
|
|
- meta.reflect()
|
17
|
|
-
|
18
|
|
- if schema is None:
|
19
|
|
- schema = get_schema()
|
20
|
|
-
|
21
|
|
- for table in schema:
|
22
|
|
- topt = table.copy()
|
23
|
|
- del topt['columns']
|
24
|
|
- name = topt['name']
|
25
|
|
- del topt['name']
|
26
|
|
- cur_table = sql.Table(name, meta, **topt)
|
27
|
|
- for col in table['columns']:
|
28
|
|
- cur_col = create_column(**col)
|
29
|
|
- cur_table.append_column(cur_col)
|
30
|
|
-
|
31
|
|
- meta.create_all(bind=dbe)
|
32
|
|
-
|
33
|
|
-
|
34
|
|
-def get_schema():
|
35
|
|
- tables = []
|
36
|
|
-
|
37
|
|
- default_columns = [
|
38
|
|
- {"name": "uid", "type": "INTEGER", "extra": {"foreignkey": "uids.uid", "nullable": False, "primarykey": True}},
|
39
|
|
- {"name": "name", "type": "VARCHAR(50)", "extra": {"nullable": False, "unique": True}},
|
40
|
|
- {"name": "string", "type": "TEXT"},
|
41
|
|
- {"name": "help", "type": "TEXT"},
|
42
|
|
- {"name": "rank", "type": "INTEGER"},
|
43
|
|
- {"name": "date_create", "type": "DATETIME"},
|
44
|
|
- {"name": "date_update", "type": "DATETIME"},
|
45
|
|
- ]
|
46
|
|
-
|
47
|
|
- # Table listing all objects created by lodel, giving them an unique id
|
48
|
|
- uids = {
|
49
|
|
- "name": "uids",
|
50
|
|
- "columns": [
|
51
|
|
- {"name": "uid", "type": "INTEGER", "extra": {"nullable": False, "primarykey": True, 'autoincrement': True}},
|
52
|
|
- {"name": "table", "type": "VARCHAR(50)"}
|
53
|
|
- ]
|
54
|
|
- }
|
55
|
|
- tables.append(uids)
|
56
|
|
-
|
57
|
|
- # Table listing the classes
|
58
|
|
- em_class = {"name": "em_class"}
|
59
|
|
- em_class['columns'] = default_columns + [
|
60
|
|
- {"name": "classtype", "type": "VARCHAR(50)"},
|
61
|
|
- {"name": "sortcolumn", "type": "VARCHAR(50)", "extra": {"default": "rank"}},
|
62
|
|
- {"name": "icon", "type": "INTEGER"},
|
63
|
|
- ]
|
64
|
|
- tables.append(em_class)
|
65
|
|
-
|
66
|
|
- # Table listing the types
|
67
|
|
- em_type = {"name": "em_type"}
|
68
|
|
- em_type['columns'] = default_columns + [
|
69
|
|
- {"name": "class_id", "type": "INTEGER", "extra": {"foreignkey": "em_class.uid", "nullable": False}},
|
70
|
|
- {"name": "sortcolumn", "type": "VARCHAR(50)", "extra": {"default": "rank"}},
|
71
|
|
- {"name": "icon", "type": "INTEGER"},
|
72
|
|
- ]
|
73
|
|
- tables.append(em_type)
|
74
|
|
-
|
75
|
|
- # relation between types: which type can be a child of another
|
76
|
|
- em_type_hierarchy = {"name": "em_type_hierarchy"}
|
77
|
|
- em_type_hierarchy['columns'] = [
|
78
|
|
- {"name": "superior_id", "type": "INTEGER", "extra": {"foreignkey": "em_type.uid", "nullable": False, "primarykey": True}},
|
79
|
|
- {"name": "subordinate_id", "type": "INTEGER", "extra": {"foreignkey": "em_type.uid", "nullable": False, "primarykey": True}},
|
80
|
|
- {"name": "nature", "type": "VARCHAR(50)", "extra": {"primarykey": True}},
|
81
|
|
- ]
|
82
|
|
- tables.append(em_type_hierarchy)
|
83
|
|
-
|
84
|
|
- # Table listing the fieldgroups of a class
|
85
|
|
- em_fieldgroup = {"name": "em_fieldgroup"}
|
86
|
|
- em_fieldgroup['columns'] = default_columns + [
|
87
|
|
- {"name": "class_id", "type": "INTEGER", "extra": {"foreignkey": "em_class.uid", "nullable": False}},
|
88
|
|
- ]
|
89
|
|
- tables.append(em_fieldgroup)
|
90
|
|
-
|
91
|
|
- # Table listing the fields of a fieldgroup
|
92
|
|
- em_field = {"name": "em_field"}
|
93
|
|
- em_field['columns'] = default_columns + [
|
94
|
|
- {"name": "fieldtype", "type": "VARCHAR(50)", "extra": {"nullable": False}},
|
95
|
|
- {"name": "fieldgroup_id", "type": "INTEGER", "extra": {"foreignkey": "em_fieldgroup.uid", "nullable": False}},
|
96
|
|
- {"name": "rel_to_type_id", "type": "INTEGER", "extra": {"foreignkey": "em_type.uid", "nullable": True, "server_default": sql.text('NULL')}}, # if relational: type this field refer to
|
97
|
|
- {"name": "rel_field_id", "type": "INTEGER", "extra": {"foreignkey": "em_type.uid", "nullable": True, "server_default": sql.text('NULL')}}, # if relational: field that specify the rel_to_type_id
|
98
|
|
- {"name": "optional", "type": "BOOLEAN"},
|
99
|
|
- {"name": "internal", "type": "BOOLEAN"},
|
100
|
|
- {"name": "icon", "type": "INTEGER"},
|
101
|
|
- ]
|
102
|
|
- tables.append(em_field)
|
103
|
|
-
|
104
|
|
- # selected field for each type
|
105
|
|
- em_field_type = {"name": "em_field_type"}
|
106
|
|
- em_field_type['columns'] = [
|
107
|
|
- {"name": "type_id", "type": "INTEGER", "extra": {"foreignkey": "em_type.uid", "nullable": False, "primarykey": True}},
|
108
|
|
- {"name": "field_id", "type": "INTEGER", "extra": {"foreignkey": "em_field.uid", "nullable": False, "primarykey": True}},
|
109
|
|
- ]
|
110
|
|
- tables.append(em_field_type)
|
111
|
|
-
|
112
|
|
- # Table of the objects created by the user (instance of the types)
|
113
|
|
- objects = {
|
114
|
|
- "name": "objects",
|
115
|
|
- "columns": [
|
116
|
|
- {"name": "uid", "type": "INTEGER", "extra": {"foreignkey": "uids.uid", "nullable": False, "primarykey": True}},
|
117
|
|
- {"name": "string", "type": "VARCHAR(50)"},
|
118
|
|
- {"name": "class_id", "type": "INTEGER", "extra": {"foreignkey": "em_class.uid"}},
|
119
|
|
- {"name": "type_id", "type": "INTEGER", "extra": {"foreignkey": "em_type.uid"}},
|
120
|
|
- {"name": "date_create", "type": "DATETIME"},
|
121
|
|
- {"name": "date_update", "type": "DATETIME"},
|
122
|
|
- {"name": "history", "type": "TEXT"}
|
123
|
|
- ]
|
124
|
|
- }
|
125
|
|
- tables.append(objects)
|
126
|
|
-
|
127
|
|
- # Table listing all files
|
128
|
|
- files = {
|
129
|
|
- "name": "files",
|
130
|
|
- "columns": [
|
131
|
|
- {"name": "uid", "type": "INTEGER", "extra": {"foreignkey": "uids.uid", "nullable": False, "primarykey": True}},
|
132
|
|
- {"name": "field1", "type": "VARCHAR(50)"}
|
133
|
|
- ]
|
134
|
|
- }
|
135
|
|
- tables.append(files)
|
136
|
|
-
|
137
|
|
- return tables
|
138
|
|
-
|
139
|
|
-
|
140
|
|
-def create_column(**kwargs):
|
141
|
|
- #Converting parameters
|
142
|
|
- if 'type_' not in kwargs and 'type' in kwargs:
|
143
|
|
- kwargs['type_'] = string_to_sqla_type(kwargs['type'])
|
144
|
|
- del kwargs['type']
|
145
|
|
-
|
146
|
|
- if 'extra' in kwargs:
|
147
|
|
- # put the extra keys in kwargs
|
148
|
|
- for exname in kwargs['extra']:
|
149
|
|
- kwargs[exname] = kwargs['extra'][exname]
|
150
|
|
- del kwargs['extra']
|
151
|
|
-
|
152
|
|
- if 'foreignkey' in kwargs:
|
153
|
|
- # Instanciate a fk
|
154
|
|
- foreignkey = sql.ForeignKey(kwargs['foreignkey'])
|
155
|
|
- del kwargs['foreignkey']
|
156
|
|
- else:
|
157
|
|
- foreignkey = None
|
158
|
|
-
|
159
|
|
- if 'primarykey' in kwargs:
|
160
|
|
- # renaming primary_key in primarykey in kwargs
|
161
|
|
- kwargs['primary_key'] = kwargs['primarykey']
|
162
|
|
- del kwargs['primarykey']
|
163
|
|
-
|
164
|
|
- col = sql.Column(**kwargs)
|
165
|
|
-
|
166
|
|
- if foreignkey is not None:
|
167
|
|
- col.append_foreign_key(foreignkey)
|
168
|
|
-
|
169
|
|
- return col
|
170
|
|
-
|
171
|
|
-
|
172
|
|
-## Converts a string to an sqlAlchemy column type
|
173
|
|
-#
|
174
|
|
-# @param strtype str: string describing the type of the column
|
175
|
|
-# @return SqlAlchemy column type
|
176
|
|
-# @raise NameError
|
177
|
|
-def string_to_sqla_type(strtype):
|
178
|
|
- if 'VARCHAR' in strtype:
|
179
|
|
- return string_to_varchar(strtype)
|
180
|
|
- else:
|
181
|
|
- try:
|
182
|
|
- return getattr(sql, strtype)
|
183
|
|
- except AttributeError:
|
184
|
|
- raise NameError("Unknown type '" + strtype + "'")
|
185
|
|
-
|
186
|
|
-
|
187
|
|
-## Converts a string like 'VARCHAR(XX)' (with XX an integer) to a SqlAlchemy varchar type
|
188
|
|
-#
|
189
|
|
-# @param vstr str: String to convert
|
190
|
|
-# @return SqlAlchemy.VARCHAR
|
191
|
|
-def string_to_varchar(vstr):
|
192
|
|
- check_length = re.search(re.compile('VARCHAR\(([\d]+)\)', re.IGNORECASE), vstr)
|
193
|
|
- column_length = int(check_length.groups()[0]) if check_length else None
|
194
|
|
- return sql.VARCHAR(length=column_length)
|