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