Browse Source

PEP8 + lint on migrationhandler and MySQL/utils

Yann Weber 9 years ago
parent
commit
be2fe9b7a5
2 changed files with 71 additions and 71 deletions
  1. 58
    64
      DataSource/MySQL/migrationhandler.py
  2. 13
    7
      DataSource/MySQL/utils.py

+ 58
- 64
DataSource/MySQL/migrationhandler.py View File

42
 class MysqlMigrationHandler(DummyMigrationHandler):
42
 class MysqlMigrationHandler(DummyMigrationHandler):
43
 
43
 
44
     ## @brief Construct a MysqlMigrationHandler
44
     ## @brief Construct a MysqlMigrationHandler
45
-    # @param host str : The db host
46
-    # @param user str : The db user
47
-    # @param password str : The db password
48
-    # @param db str : The db name
49
-    def __init__(self, module = pymysql, conn_args = None, db_engine='InnoDB', foreign_keys=True, debug=None, dryrun=False, drop_if_exists=False):
50
-        
45
+    # @param module : sql module
46
+    # @param conn_args dict : A dict containing connection options
47
+    def __init__(self, module=pymysql, conn_args=None, db_engine='InnoDB', foreign_keys=True, debug=None, dryrun=False, drop_if_exists=False):
51
         # Database connection
48
         # Database connection
52
         self._dbmodule = module
49
         self._dbmodule = module
53
         if conn_args is None:
50
         if conn_args is None:
54
             conn_args = copy.copy(Settings.get('datasource')['default'])
51
             conn_args = copy.copy(Settings.get('datasource')['default'])
55
             self._dbmodule = conn_args['module']
52
             self._dbmodule = conn_args['module']
56
             del conn_args['module']
53
             del conn_args['module']
57
-        self.db = self._dbmodule.connect(**conn_args)
58
-        
54
+        self.db_conn = self._dbmodule.connect(**conn_args)
59
         # Fetch options
55
         # Fetch options
60
         self.debug = Settings.get('debug') if debug is None else debug
56
         self.debug = Settings.get('debug') if debug is None else debug
61
         self.dryrun = dryrun
57
         self.dryrun = dryrun
62
         self.db_engine = db_engine
58
         self.db_engine = db_engine
63
         self.foreign_keys = foreign_keys if db_engine == 'InnoDB' else False
59
         self.foreign_keys = foreign_keys if db_engine == 'InnoDB' else False
64
         self.drop_if_exists = drop_if_exists
60
         self.drop_if_exists = drop_if_exists
65
-
66
         #Create default tables
61
         #Create default tables
67
         self._create_default_tables(self.drop_if_exists)
62
         self._create_default_tables(self.drop_if_exists)
68
 
63
 
93
                 elif new_state is None:
88
                 elif new_state is None:
94
                     #non relationnal EmField deletion
89
                     #non relationnal EmField deletion
95
                     if emfield.name not in EditorialModel.classtypes.common_fields.keys():
90
                     if emfield.name not in EditorialModel.classtypes.common_fields.keys():
96
-                        self.del_col_from_emfield(em, uid)
91
+                        self.delete_col_from_emfield(em, uid)
97
             else:
92
             else:
98
                 #relationnal field
93
                 #relationnal field
99
                 if initial_state is None:
94
                 if initial_state is None:
113
     # @note this function handles the table creation
108
     # @note this function handles the table creation
114
     # @param em Model : EditorialModel.model.Model instance
109
     # @param em Model : EditorialModel.model.Model instance
115
     # @param rfuid int : Relationnal field uid
110
     # @param rfuid int : Relationnal field uid
116
-    def add_relationnal_field(self, em, rfuid):
117
-        emfield = em.component(rfuid)
111
+    def add_relationnal_field(self, edmod, rfuid):
112
+        emfield = edmod.component(rfuid)
118
         if not isinstance(emfield, EditorialModel.fields.EmField):
113
         if not isinstance(emfield, EditorialModel.fields.EmField):
119
             raise ValueError("The given uid is not an EmField uid")
114
             raise ValueError("The given uid is not an EmField uid")
120
 
115
 
121
-        r2tf = em.component(emfield.rel_field_id)
122
-        tname = self._r2t2table_name(em, r2tf)
116
+        r2tf = edmod.component(emfield.rel_field_id)
117
+        tname = self._r2t2table_name(edmod, r2tf)
123
         pkname, pkftype = self._relation_pk
118
         pkname, pkftype = self._relation_pk
124
 
119
 
125
         #If not exists create a relational table
120
         #If not exists create a relational table
130
         #Add the column
125
         #Add the column
131
         self._add_column(tname, emfield.name, emfield.fieldtype_instance())
126
         self._add_column(tname, emfield.name, emfield.fieldtype_instance())
132
         #Update table triggers
127
         #Update table triggers
133
-        self._generate_triggers(tname, self._r2type2cols(em, r2tf))
128
+        self._generate_triggers(tname, self._r2type2cols(edmod, r2tf))
134
 
129
 
135
     ## @brief Delete a rel2type attribute
130
     ## @brief Delete a rel2type attribute
136
     #
131
     #
138
     # @note this method handles the table deletion
133
     # @note this method handles the table deletion
139
     # @param em Model : EditorialModel.model.Model instance
134
     # @param em Model : EditorialModel.model.Model instance
140
     # @param rfuid int : Relationnal field uid
135
     # @param rfuid int : Relationnal field uid
141
-    def del_relationnal_field(self, em, rfuid):
142
-        emfield = em.component(rfuid)
136
+    def del_relationnal_field(self, edmod, rfuid):
137
+        emfield = edmod.component(rfuid)
143
         if not isinstance(emfield, EditorialModel.fields.EmField):
138
         if not isinstance(emfield, EditorialModel.fields.EmField):
144
             raise ValueError("The given uid is not an EmField uid")
139
             raise ValueError("The given uid is not an EmField uid")
145
 
140
 
146
-        r2tf = em.component(emfield.rel_field_id)
147
-        tname = self._r2t2table_name(em, r2tf)
141
+        r2tf = edmod.component(emfield.rel_field_id)
142
+        tname = self._r2t2table_name(edmod, r2tf)
148
 
143
 
149
-        if len(self._r2type2cols(em, r2tf)) == 1:
144
+        if len(self._r2type2cols(edmod, r2tf)) == 1:
150
             #The table can be deleted (no more attribute for this rel2type)
145
             #The table can be deleted (no more attribute for this rel2type)
151
             self._query("""DROP TABLE {table_name}""".format(table_name=tname))
146
             self._query("""DROP TABLE {table_name}""".format(table_name=tname))
152
         else:
147
         else:
153
             self._del_column(tname, emfield.name)
148
             self._del_column(tname, emfield.name)
154
             #Update table triggers
149
             #Update table triggers
155
-            self._generate_triggers(tname, self._r2type2cols(em, r2tf))
150
+            self._generate_triggers(tname, self._r2type2cols(edmod, r2tf))
156
 
151
 
157
     ## @brief Given an EmField uid add a column to the corresponding table
152
     ## @brief Given an EmField uid add a column to the corresponding table
158
     # @param em Model : A Model instance
153
     # @param em Model : A Model instance
159
     # @param uid int : An EmField uid
154
     # @param uid int : An EmField uid
160
-    def add_col_from_emfield(self, em, uid):
161
-        emfield = em.component(uid)
155
+    def add_col_from_emfield(self, edmod, uid):
156
+        emfield = edmod.component(uid)
162
         if not isinstance(emfield, EditorialModel.fields.EmField):
157
         if not isinstance(emfield, EditorialModel.fields.EmField):
163
             raise ValueError("The given uid is not an EmField uid")
158
             raise ValueError("The given uid is not an EmField uid")
164
 
159
 
172
     ## @brief Given a class uid create the coressponding table
167
     ## @brief Given a class uid create the coressponding table
173
     # @param em Model : A Model instance
168
     # @param em Model : A Model instance
174
     # @param uid int : An EmField uid
169
     # @param uid int : An EmField uid
175
-    def create_emclass_table(self, em, uid, engine):
176
-        emclass = em.component(uid)
170
+    def create_emclass_table(self, edmod, uid, engine):
171
+        emclass = edmod.component(uid)
177
         if not isinstance(emclass, EditorialModel.classes.EmClass):
172
         if not isinstance(emclass, EditorialModel.classes.EmClass):
178
             raise ValueError("The given uid is not an EmClass uid")
173
             raise ValueError("The given uid is not an EmClass uid")
179
         pkname, pktype = self._object_pk
174
         pkname, pktype = self._object_pk
187
     ## @brief Given an EmClass uid delete the corresponding table
182
     ## @brief Given an EmClass uid delete the corresponding table
188
     # @param em Model : A Model instance
183
     # @param em Model : A Model instance
189
     # @param uid int : An EmField uid
184
     # @param uid int : An EmField uid
190
-    def delete_emclass_table(self, em, uid):
191
-        emclass = em.component(uid)
185
+    def delete_emclass_table(self, edmod, uid):
186
+        emclass = edmod.component(uid)
192
         if not isinstance(emclass, EditorialModel.classes.EmClass):
187
         if not isinstance(emclass, EditorialModel.classes.EmClass):
193
             raise ValueError("The give uid is not an EmClass uid")
188
             raise ValueError("The give uid is not an EmClass uid")
194
         tname = utils.object_table_name(emclass.name)
189
         tname = utils.object_table_name(emclass.name)
202
     ## @brief Given an EmField delete the corresponding column
197
     ## @brief Given an EmField delete the corresponding column
203
     # @param em Model : an @ref EditorialModel.model.Model instance
198
     # @param em Model : an @ref EditorialModel.model.Model instance
204
     # @param uid int : an EmField uid
199
     # @param uid int : an EmField uid
205
-    def delete_col_from_emfield(self, em, uid):
206
-        emfield = em.component(uid)
200
+    def delete_col_from_emfield(self, edmod, uid):
201
+        emfield = edmod.component(uid)
207
         if not isinstance(emfield, EditorialModel.fields.EmField):
202
         if not isinstance(emfield, EditorialModel.fields.EmField):
208
             raise ValueError("The given uid is not an EmField uid")
203
             raise ValueError("The given uid is not an EmField uid")
209
 
204
 
214
 
209
 
215
         self._del_column(tname, emfield.name)
210
         self._del_column(tname, emfield.name)
216
         # Refresh the table triggers
211
         # Refresh the table triggers
217
-        cols_ls = self._class2cols(emclass)
212
+        cols_l = self._class2cols(emclass)
218
         self._generate_triggers(tname, cols_l)
213
         self._generate_triggers(tname, cols_l)
219
 
214
 
220
     ## @brief Delete a column from a table
215
     ## @brief Delete a column from a table
230
     # @param em Model : A Model instance
225
     # @param em Model : A Model instance
231
     # @param emfield EmField : An EmField instance
226
     # @param emfield EmField : An EmField instance
232
     # @return a table name
227
     # @return a table name
233
-    def _r2t2table_name(self, em, emfield):
228
+    def _r2t2table_name(self, edmod, emfield):
234
         emclass = emfield.em_class
229
         emclass = emfield.em_class
235
-        emtype = em.component(emfield.rel_to_type_id)
230
+        emtype = edmod.component(emfield.rel_to_type_id)
236
         return utils.r2t_table_name(emclass.name, emtype.name)
231
         return utils.r2t_table_name(emclass.name, emtype.name)
237
 
232
 
238
     ## @brief Generate a columns_fieldtype dict given a rel2type EmField
233
     ## @brief Generate a columns_fieldtype dict given a rel2type EmField
239
     # @param em Model : an @ref EditorialModel.model.Model instance
234
     # @param em Model : an @ref EditorialModel.model.Model instance
240
     # @param emfield EmField : and @ref EditorialModel.fields.EmField instance
235
     # @param emfield EmField : and @ref EditorialModel.fields.EmField instance
241
-    def _r2type2cols(self, em, emfield):
242
-        return {f.name: f.fieldtype_instance() for f in em.components('EmField') if f.rel_field_id == emfield.uid}
236
+    def _r2type2cols(self, edmod, emfield):
237
+        return {f.name: f.fieldtype_instance() for f in edmod.components('EmField') if f.rel_field_id == emfield.uid}
243
 
238
 
244
     ## @brief Generate a columns_fieldtype dict given an EmClass
239
     ## @brief Generate a columns_fieldtype dict given an EmClass
245
     # @param emclass EmClass : An EmClass instance
240
     # @param emclass EmClass : An EmClass instance
280
         sup_cname, sub_cname = self.get_sup_and_sub_cols()
275
         sup_cname, sub_cname = self.get_sup_and_sub_cols()
281
         self._add_fk(tname, object_tname, sup_cname, self._object_pk[0], 'fk_relations_superiors')
276
         self._add_fk(tname, object_tname, sup_cname, self._object_pk[0], 'fk_relations_superiors')
282
         self._add_fk(tname, object_tname, sub_cname, self._object_pk[0], 'fk_relations_subordinate')
277
         self._add_fk(tname, object_tname, sub_cname, self._object_pk[0], 'fk_relations_subordinate')
283
-    
278
+
284
     ## @brief Returns the fieldname for superior and subordinate in relation table
279
     ## @brief Returns the fieldname for superior and subordinate in relation table
285
     # @return a tuple (superior_name, subordinate_name)
280
     # @return a tuple (superior_name, subordinate_name)
286
     @classmethod
281
     @classmethod
293
                     sup = fname
288
                     sup = fname
294
                 else:
289
                 else:
295
                     sub = fname
290
                     sub = fname
296
-        return utils.column_name(sup),utils.column_name(sub)
291
+        return utils.column_name(sup), utils.column_name(sub)
297
 
292
 
298
     ## @brief Create a table with primary key
293
     ## @brief Create a table with primary key
299
     # @param table_name str : table name
294
     # @param table_name str : table name
355
         )
350
         )
356
         try:
351
         try:
357
             self._query(add_col)
352
             self._query(add_col)
358
-        except self._dbmodule.err.InternalError as e:
353
+        except self._dbmodule.err.InternalError:
359
             if drop_if_exists:
354
             if drop_if_exists:
360
                 self._del_column(table_name, col_name)
355
                 self._del_column(table_name, col_name)
361
                 self._add_column(table_name, col_name, col_fieldtype, drop_if_exists)
356
                 self._add_column(table_name, col_name, col_fieldtype, drop_if_exists)
370
     # @param dst_table_name str : The name of the table the FK will point on
365
     # @param dst_table_name str : The name of the table the FK will point on
371
     # @param src_col_name str : The name of the concerned column in the src_table
366
     # @param src_col_name str : The name of the concerned column in the src_table
372
     # @param dst_col_name str : The name of the concerned column in the dst_table
367
     # @param dst_col_name str : The name of the concerned column in the dst_table
373
-    def _add_fk(self, src_table_name, dst_table_name, src_col_name, dst_col_name, fk_name = None):
368
+    def _add_fk(self, src_table_name, dst_table_name, src_col_name, dst_col_name, fk_name=None):
374
         stname = utils.escape_idname(src_table_name)
369
         stname = utils.escape_idname(src_table_name)
375
         dtname = utils.escape_idname(dst_table_name)
370
         dtname = utils.escape_idname(dst_table_name)
376
         scname = utils.escape_idname(src_col_name)
371
         scname = utils.escape_idname(src_col_name)
384
         self._query("""ALTER TABLE {src_table}
379
         self._query("""ALTER TABLE {src_table}
385
 ADD CONSTRAINT {fk_name}
380
 ADD CONSTRAINT {fk_name}
386
 FOREIGN KEY ({src_col}) references {dst_table}({dst_col});""".format(
381
 FOREIGN KEY ({src_col}) references {dst_table}({dst_col});""".format(
387
-            fk_name=utils.escape_idname(fk_name),
388
-            src_table=stname,
389
-            src_col=scname,
390
-            dst_table=dtname,
391
-            dst_col=dcname
392
-        ))
382
+    fk_name=utils.escape_idname(fk_name),
383
+    src_table=stname,
384
+    src_col=scname,
385
+    dst_table=dtname,
386
+    dst_col=dcname
387
+))
393
 
388
 
394
     ## @brief Given a source and a destination table, delete the corresponding FK
389
     ## @brief Given a source and a destination table, delete the corresponding FK
395
     # @param src_table_name str : The name of the table where the FK is
390
     # @param src_table_name str : The name of the table where the FK is
396
     # @param dst_table_name str : The name of the table the FK point on
391
     # @param dst_table_name str : The name of the table the FK point on
397
     # @warning fails silently
392
     # @warning fails silently
398
-    def _del_fk(self, src_table_name, dst_table_name, fk_name = None):
393
+    def _del_fk(self, src_table_name, dst_table_name, fk_name=None):
399
         if fk_name is None:
394
         if fk_name is None:
400
             fk_name = utils.escape_idname(utils.get_fk_name(src_table_name, dst_table_name))
395
             fk_name = utils.escape_idname(utils.get_fk_name(src_table_name, dst_table_name))
401
         try:
396
         try:
402
             self._query("""ALTER TABLE {src_table}
397
             self._query("""ALTER TABLE {src_table}
403
 DROP FOREIGN KEY {fk_name}""".format(
398
 DROP FOREIGN KEY {fk_name}""".format(
404
-                src_table=utils.escape_idname(src_table_name),
405
-                fk_name=fk_name
406
-            ))
399
+    src_table=utils.escape_idname(src_table_name),
400
+    fk_name=fk_name
401
+))
407
         except self._dbmodule.err.InternalError:
402
         except self._dbmodule.err.InternalError:
408
             # If the FK don't exists we do not care
403
             # If the FK don't exists we do not care
409
             pass
404
             pass
448
         if len(col_val_l) > 0:
443
         if len(col_val_l) > 0:
449
             trig_q = """CREATE TRIGGER {trigger_name} BEFORE {moment} ON {table_name}
444
             trig_q = """CREATE TRIGGER {trigger_name} BEFORE {moment} ON {table_name}
450
 FOR EACH ROW SET {col_val_list};""".format(
445
 FOR EACH ROW SET {col_val_list};""".format(
451
-                trigger_name=trigger_name,
452
-                table_name=utils.escape_idname(table_name),
453
-                moment=moment, col_val_list=col_val_l
454
-            )
446
+    trigger_name=trigger_name,
447
+    table_name=utils.escape_idname(table_name),
448
+    moment=moment, col_val_list=col_val_l
449
+)
455
             self._query(trig_q)
450
             self._query(trig_q)
456
 
451
 
457
     ## @brief Identifier escaping
452
     ## @brief Identifier escaping
519
         for uid in [c.uid for c in model.components('EmClass')]:
514
         for uid in [c.uid for c in model.components('EmClass')]:
520
             try:
515
             try:
521
                 self.delete_emclass_table(model, uid)
516
                 self.delete_emclass_table(model, uid)
522
-            except self._dbmodule.err.InternalError as e:
523
-                print(e)
517
+            except self._dbmodule.err.InternalError as expt:
518
+                print(expt)
524
 
519
 
525
-        for tname in [MySQL.get_r2t2table_name(f.em_class.name, model.component(f.rel_to_type_id).name) for f in model.components('EmField') if f.fieldtype == 'rel2type']:
520
+        for tname in [utils.r2t_table_name(f.em_class.name, model.component(f.rel_to_type_id).name) for f in model.components('EmField') if f.fieldtype == 'rel2type']:
526
             try:
521
             try:
527
                 self._query("DROP TABLE %s;" % tname)
522
                 self._query("DROP TABLE %s;" % tname)
528
-            except self._dbmodule.err.InternalError as e:
529
-                print(e)
523
+            except self._dbmodule.err.InternalError as expt:
524
+                print(expt)
530
 
525
 
531
-        for tname in [MySQL.relations_table_name, MySQL.objects_table_name]:
526
+        for tname in [utils.common_tables['relation'], utils.common_tables['relation']]:
532
             try:
527
             try:
533
                 self._query("DROP TABLE %s;" % tname)
528
                 self._query("DROP TABLE %s;" % tname)
534
-            except self._dbmodule.err.InternalError as e:
535
-                print(e)
529
+            except self._dbmodule.err.InternalError as expt:
530
+                print(expt)
536
 
531
 
537
     ## @brief Return primary key name & fieldtype for relation or object
532
     ## @brief Return primary key name & fieldtype for relation or object
538
     @classmethod
533
     @classmethod
543
                 finfo_cp = copy.copy(finfo)
538
                 finfo_cp = copy.copy(finfo)
544
                 del(finfo_cp['fieldtype'])
539
                 del(finfo_cp['fieldtype'])
545
                 return (utils.column_name(fname), fto(**finfo_cp))
540
                 return (utils.column_name(fname), fto(**finfo_cp))
546
-        raise RuntimeError("No primary key found in common fields : %s"%common_fields)
541
+        raise RuntimeError("No primary key found in common fields : %s" % common_fields)
547
 
542
 
548
     ## @brief Exec a query
543
     ## @brief Exec a query
549
     # @param query str : SQL query
544
     # @param query str : SQL query
551
         if self.debug:
546
         if self.debug:
552
             print(query + "\n")
547
             print(query + "\n")
553
         if not self.dryrun:
548
         if not self.dryrun:
554
-            with self.db.cursor() as cur:
549
+            with self.db_conn.cursor() as cur:
555
                 cur.execute(query)
550
                 cur.execute(query)
556
-        self.db.commit()  # autocommit
551
+        self.db_conn.commit()  # autocommit
557
 
552
 
558
     ## @brief Given a common field name return an EmFieldType instance
553
     ## @brief Given a common field name return an EmFieldType instance
559
     # @param cname str : Common field name
554
     # @param cname str : Common field name
578
     ## @brief Returns a dict { colname:fieldtype } of relation table columns
573
     ## @brief Returns a dict { colname:fieldtype } of relation table columns
579
     @property
574
     @property
580
     def _relation_cols(self):
575
     def _relation_cols(self):
581
-        from_name = EditorialModel.fieldtypes.generic.GenericFieldType.from_name
582
         res = dict()
576
         res = dict()
583
         for fieldname, fieldinfo in EditorialModel.classtypes.relations_common_fields.items():
577
         for fieldname, fieldinfo in EditorialModel.classtypes.relations_common_fields.items():
584
             finfo = copy.copy(fieldinfo)
578
             finfo = copy.copy(fieldinfo)

+ 13
- 7
DataSource/MySQL/utils.py View File

1
 # -*- coding: utf8 -*-
1
 # -*- coding: utf8 -*-
2
 
2
 
3
-from Lodel.settings import Settings
4
-
5
 common_tables = {
3
 common_tables = {
6
     'relation': 'relation',
4
     'relation': 'relation',
7
     'object': 'object'
5
     'object': 'object'
17
 ## @brief Lodel_id for the hierachy root
15
 ## @brief Lodel_id for the hierachy root
18
 leroot_lodel_id = 0
16
 leroot_lodel_id = 0
19
 
17
 
18
+
20
 ## @brief Return a table name given a EmClass or LeClass name
19
 ## @brief Return a table name given a EmClass or LeClass name
21
 # @param class_name str : The class name
20
 # @param class_name str : The class name
22
 # @return a table name
21
 # @return a table name
23
 def object_table_name(class_name):
22
 def object_table_name(class_name):
24
-    return ("%s%s"%(table_preffix['object'], class_name)).lower()
23
+    return ("%s%s" % (table_preffix['object'], class_name)).lower()
24
+
25
 
25
 
26
 ## @brief Return a table name given a class name and a type name
26
 ## @brief Return a table name given a class name and a type name
27
 # @param class_name str : The (Em|Le)Class name
27
 # @param class_name str : The (Em|Le)Class name
28
 # @param type_name str : The (Em|Le)Type name
28
 # @param type_name str : The (Em|Le)Type name
29
 # @return a table name
29
 # @return a table name
30
 def r2t_table_name(class_name, type_name):
30
 def r2t_table_name(class_name, type_name):
31
-    return ("%s%s_%s"%(table_preffix['relation'], class_name, type_name)).lower()
31
+    return ("%s%s_%s" % (table_preffix['relation'], class_name, type_name)).lower()
32
+
32
 
33
 
33
 ## @brief Return a column name given a field name
34
 ## @brief Return a column name given a field name
34
 # @param field_name : The EmField or LeObject field name
35
 # @param field_name : The EmField or LeObject field name
35
 # @return A column name
36
 # @return A column name
36
 def column_name(field_name):
37
 def column_name(field_name):
37
-    return field_name.lower();
38
+    return field_name.lower()
39
+
38
 
40
 
39
 ## @brief gets the fk name between two tables
41
 ## @brief gets the fk name between two tables
40
 # @param src_table_name str
42
 # @param src_table_name str
43
 def get_fk_name(src_table_name, dst_table_name):
45
 def get_fk_name(src_table_name, dst_table_name):
44
     return ("fk_%s_%s" % (src_table_name, dst_table_name)).lower()
46
     return ("fk_%s_%s" % (src_table_name, dst_table_name)).lower()
45
 
47
 
48
+
46
 ## @brief Exec a query
49
 ## @brief Exec a query
47
 # @param query str : SQL query
50
 # @param query str : SQL query
48
-def query(connection, query):
51
+def query(connection, query_string):
49
     with connection as cur:
52
     with connection as cur:
50
         try:
53
         try:
51
-            cur.execute(query)
54
+            cur.execute(query_string)
52
         except Exception as err:
55
         except Exception as err:
53
             raise err
56
             raise err
54
         return cur
57
         return cur
55
 
58
 
59
+
56
 ## @brief Identifier escaping
60
 ## @brief Identifier escaping
57
 # @param idname str : An SQL identifier
61
 # @param idname str : An SQL identifier
58
 def escape_idname(idname):
62
 def escape_idname(idname):
60
         raise ValueError("Invalid name : '%s'" % idname)
64
         raise ValueError("Invalid name : '%s'" % idname)
61
     return '`%s`' % idname
65
     return '`%s`' % idname
62
 
66
 
67
+
63
 ## Brief add table prefix to a column name
68
 ## Brief add table prefix to a column name
64
 # @param name string: column name to prefix
69
 # @param name string: column name to prefix
65
 # @param prefixes dict(prefix:list(name,))
70
 # @param prefixes dict(prefix:list(name,))
71
             return column_prefix(prefix, name)
76
             return column_prefix(prefix, name)
72
     return name
77
     return name
73
 
78
 
79
+
74
 ## prefix a column name with the table name
80
 ## prefix a column name with the table name
75
 def column_prefix(table, column):
81
 def column_prefix(table, column):
76
     return '%s.%s' % (table, column)
82
     return '%s.%s' % (table, column)

Loading…
Cancel
Save