|
@@ -0,0 +1,87 @@
|
|
1
|
+# -*- coding: utf8 -*-
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+import EditorialModel
|
|
5
|
+
|
|
6
|
+## @brief Manages the accesses to a MySQL datasource
|
|
7
|
+class MySQL(object):
|
|
8
|
+
|
|
9
|
+ _relations_table_name = 'relation'
|
|
10
|
+ _relations_field_nature = 'nature'
|
|
11
|
+ _field_lodel_id = 'lodel_id'
|
|
12
|
+ _class_table_prefix = 'class_'
|
|
13
|
+ _objects_table_name = 'object'
|
|
14
|
+ _connections = {
|
|
15
|
+ 'default':{
|
|
16
|
+ 'module': pymysql,
|
|
17
|
+ 'host': '127.0.0.1',
|
|
18
|
+ 'user': 'lodel',
|
|
19
|
+ 'passwd': 'bruno',
|
|
20
|
+ 'db': 'lodel2'
|
|
21
|
+ }
|
|
22
|
+ }
|
|
23
|
+
|
|
24
|
+ @classmethod
|
|
25
|
+ ## @brief gets the table name from class name
|
|
26
|
+ # @param class_name str
|
|
27
|
+ # @return str
|
|
28
|
+ def get_table_name_from_class(cls, class_name):
|
|
29
|
+ return "%s%s" % (cls._class_table_prefix, class_name)
|
|
30
|
+
|
|
31
|
+ @classmethod
|
|
32
|
+ ## @brief gets the table name given a class, a type and a field names
|
|
33
|
+ # @param class_name str
|
|
34
|
+ # @param type_name str
|
|
35
|
+ # @param field_name str
|
|
36
|
+ # @return str
|
|
37
|
+ def get_r2t2table_name(cls, class_name, type_name, field_name):
|
|
38
|
+ return "%s_%s_%s" % (class_name, type_name, field_name)
|
|
39
|
+
|
|
40
|
+ @classmethod
|
|
41
|
+ ## @brief gets the fk name between two tables
|
|
42
|
+ # @param src_table_name str
|
|
43
|
+ # @param dst_table_name str
|
|
44
|
+ # @return str
|
|
45
|
+ def _fk_name(cls, src_table_name, dst_table_name):
|
|
46
|
+ return "fk_%s_%s" % (src_table_name, dst_table_name)
|
|
47
|
+
|
|
48
|
+ @classmethod
|
|
49
|
+ ## @brief Identifier escaping
|
|
50
|
+ # @param idname str : An SQL identifier
|
|
51
|
+ def escape_idname(cls, idname):
|
|
52
|
+ if '`' in idname:
|
|
53
|
+ raise ValueError("Invalid name : '%s'" % idname)
|
|
54
|
+ return '`%s`' % idname
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+ @classmethod
|
|
58
|
+ ## @brief Given a fieldtype, returns a MySQL type specifier
|
|
59
|
+ # @param emfieldType EmFieldType : A fieldtype
|
|
60
|
+ # @return str
|
|
61
|
+ def get_type_spec_from_fieldtype(cls, emfieldtype):
|
|
62
|
+
|
|
63
|
+ ftype = emfieldtype.ftype
|
|
64
|
+
|
|
65
|
+ if ftype == 'char' or ftype == 'str':
|
|
66
|
+ res = "VARCHAR(%d)" % emfieldtype.max_length
|
|
67
|
+ elif ftype == 'text':
|
|
68
|
+ res = 'TEXT'
|
|
69
|
+ elif ftype == 'datetime':
|
|
70
|
+ res = "DATETIME"
|
|
71
|
+ # client side workaround for only one column with CURRENT_TIMESTAMP : giving NULL to timestamp that don't allows NULL
|
|
72
|
+ # cf. https://dev.mysql.com/doc/refman/5.0/en/timestamp-initialization.html#idm139961275230400
|
|
73
|
+ # The solution for the migration handler is to create triggers :
|
|
74
|
+ # CREATE TRIGGER trigger_name BEFORE INSERT ON `my_super_table`
|
|
75
|
+ # FOR EACH ROW SET NEW.my_date_column = NOW();
|
|
76
|
+ # and
|
|
77
|
+ # CREATE TRIGGER trigger_name BEFORE UPDATE ON
|
|
78
|
+ elif ftype == 'bool':
|
|
79
|
+ res = "BOOL"
|
|
80
|
+ elif ftype == 'int':
|
|
81
|
+ res = "INT"
|
|
82
|
+ elif ftype == 'rel2type':
|
|
83
|
+ res = "INT"
|
|
84
|
+ else:
|
|
85
|
+ raise ValueError("Unsupported fieldtype ftype : %s" % ftype)
|
|
86
|
+
|
|
87
|
+ return res
|