Parcourir la source

sql datasource: select/insert for relations

ArnAud il y a 9 ans
Parent
révision
98f02e1630
1 fichiers modifiés avec 37 ajouts et 41 suppressions
  1. 37
    41
      DataSource/MySQL/leapidatasource.py

+ 37
- 41
DataSource/MySQL/leapidatasource.py Voir le fichier

@@ -57,20 +57,17 @@ class LeDataSourceSQL(DummyDatasource):
57 57
             main_table = utils.common_tables['object']
58 58
             main_class = target_cls.leo_class()
59 59
             class_table = utils.object_table_name(main_class.__name__)
60
-            print (class_table)
61
-            main_lodel_id = self.datasource_utils.column_prefix(main_table, self.datasource_utils.field_lodel_id)
62
-            class_lodel_id = self.datasource_utils.column_prefix(class_table, self.datasource_utils.field_lodel_id)
60
+            main_lodel_id = utils.column_prefix(main_table, main_class.uidname())
61
+            class_lodel_id = utils.column_prefix(class_table, main_class.uidname())
63 62
             joins = [left_join(class_table, {main_lodel_id:class_lodel_id})]
64
-            fields = [(main_table, common_fields), (class_table, main_class.fieldlist())]
63
+            fields = [(main_table, target_cls.name2class('LeObject').fieldlist()), (class_table, main_class.fieldlist())]
65 64
 
66 65
         elif target_cls.is_lehierarch():
67
-            main_table = self.datasource_utils.relations_table_name
68
-            print(field_list)
69
-            field_list = target_cls.name2class('LeRelation').fieldlist()
70
-            print(field_list)
71
-            fields = [(main_table, relations_common_fields)]
66
+            main_table = utils.common_tables['relation']
67
+            fields = [(main_table, target_cls.name2class('LeRelation').fieldlist())]
72 68
         elif target_cls.is_lerel2type():
73
-            pass
69
+            main_table = utils.common_tables['relation']
70
+            fields = [(main_table, target_cls.name2class('LeRelation').fieldlist())]
74 71
         else:
75 72
             raise AttributeError("Target class '%s' in get() is not a Lodel Editorial Object !" % target_cls)
76 73
 
@@ -87,29 +84,19 @@ class LeDataSourceSQL(DummyDatasource):
87 84
         if offset:
88 85
             kwargs['offset'] = offset
89 86
 
90
-       # @todo implement that
91
-        #if rel_filters is not None and len(rel_filters) > 0:
92
-            #rel_filters = self._prepare_rel_filters(rel_filters)
93
-            #for rel_filter in rel_filters:
94
-                ## join condition
95
-                #relation_table_join_field = "%s.%s" % (self.datasource_utils.relations_table_name, self.RELATIONS_POSITIONS_FIELDS[rel_filter['position']])
96
-                #query_table_join_field = "%s.%s" % (query_table_name, self.datasource_utils.relations_field_nature)
97
-                #join_fields[query_table_join_field] = relation_table_join_field
98
-                ## adding "where" filters
99
-                #where_filters['%s.%s' % (self.datasource_utils.relations_table_name, self.datasource_utils.relations_field_nature)] = rel_filter['nature']
100
-                #where_filters[rel_filter['condition_key']] = rel_filter['condition_value']
87
+       # @todo implement relational filters
101 88
 
102 89
         # prefix filters'' column names, and prepare dict for mosql where {(fieldname, op): value}
103 90
         wheres = {(utils.find_prefix(name, fields), op):value for name,op,value in filters}
104 91
         query = select(main_table, select=prefixed_field_list, where=wheres, joins=joins, **kwargs)
105
-        print ('SQL', query)
92
+        #print ('SQL', query)
106 93
 
107 94
         # Executing the query
108 95
         cur = utils.query(self.connection, query)
109 96
         results = all_to_dicts(cur)
110 97
 
111 98
         # instanciate each row to editorial components
112
-        results = [target_cls.uid2leobj(datas['type_id'])(**datas) for datas in results]
99
+        results = [target_cls.object_from_data(datas) for datas in results]
113 100
         #print('results', results)
114 101
 
115 102
         return results
@@ -164,7 +151,7 @@ class LeDataSourceSQL(DummyDatasource):
164 151
     # @return the number of updated components
165 152
     # @todo implement other filters than lodel_id
166 153
     def update(self, target_cls, filters, rel_filters, **datas):
167
-        print(target_cls, filters, rel_filters, datas)
154
+        #print(target_cls, filters, rel_filters, datas)
168 155
         # it is a LeType
169 156
         if not target_cls.is_letype():
170 157
             raise AttributeError("'%s' is not a LeType, it is not possible to update it" % target_cls)
@@ -199,37 +186,46 @@ class LeDataSourceSQL(DummyDatasource):
199 186
     def insert(self, target_cls, **datas):
200 187
         # it is a LeType
201 188
         if target_cls.is_letype():
202
-            main_table = self.datasource_utils.objects_table_name
189
+            main_table = utils.common_tables['object']
203 190
             main_datas = {'class_id':target_cls._leclass._class_id, 'type_id':target_cls._type_id}
204
-            main_fields = common_fields
205
-            class_table = self.datasource_utils.get_table_name_from_class(target_cls._leclass.__name__)
206
-            fk_name = self.datasource_utils.field_lodel_id
191
+            main_fields = target_cls.name2class('LeObject').fieldlist()
192
+
193
+            class_table = utils.object_table_name(target_cls.leo_class().__name__)
194
+            fk_name = target_cls.uidname()
207 195
         # it is a hierarchy
208 196
         elif target_cls.is_lehierarch():
209
-            main_table = self.datasource_utils.relations_table_name
210
-            main_datas = {'id_sup':datas['lesup'].lodel_id, 'id_sub':datas['lesub'].lodel_id}
211
-            main_fields = relations_common_fields
197
+            main_table = utils.common_tables['relation']
198
+            main_datas = {
199
+                utils.column_name(target_cls._lesup_name): datas[target_cls._lesup_name].lodel_id,
200
+                utils.column_name(target_cls._lesub_name): datas[target_cls._lesub_name].lodel_id
201
+            }
202
+            main_fields = target_cls.name2class('LeRelation').fieldlist()
212 203
             class_table = False
213 204
         # it is a relation
214 205
         elif target_cls.is_lerel2type():
215
-            main_table = self.datasource_utils.relations_table_name
216
-            main_datas = {'id_sup':datas['lesup'].lodel_id, 'id_sub':datas['lesub'].lodel_id}
217
-            main_fields = relations_common_fields
218
-            class_table = self.datasource_utils.get_r2t2table_name(datas['lesup']._leclass.__name__, datas['lesub'].__class__.__name__)
219
-            del(datas['lesup'], datas['lesub'])
220
-            fk_name = self.datasource_utils.relations_pkname
206
+            main_table = utils.common_tables['relation']
207
+            main_datas = {
208
+                utils.column_name(target_cls._lesup_name): datas[target_cls._lesup_name].lodel_id,
209
+                utils.column_name(target_cls._lesub_name): datas[target_cls._lesub_name].lodel_id
210
+            }
211
+            main_fields = target_cls.name2class('LeRelation').fieldlist()
212
+
213
+            superior_class = datas['superior'].leo_class()
214
+            class_table = utils.r2t_table_name(superior_class.__name__, datas['subordinate'].__class__.__name__)
215
+            fk_name = superior_class.name2class('LeRelation').uidname()
221 216
         else:
222 217
             raise AttributeError("'%s' is not a LeType or a LeRelation, it is not possible to insert it" % target_cls)
223 218
 
224
-        # find main table and main table datas
219
+        # extract main table datas from datas
225 220
         for main_column_name in main_fields:
226 221
             if main_column_name in datas:
227
-                main_datas[main_column_name] = datas[main_column_name]
222
+                if main_column_name not in main_datas:
223
+                    main_datas[main_column_name] = datas[main_column_name]
228 224
                 del(datas[main_column_name])
229 225
 
230 226
         sql = insert(main_table, main_datas)
231 227
         #print (sql)
232
-        cur = self.datasource_utils.query(self.connection, sql)
228
+        cur = utils.query(self.connection, sql)
233 229
         lodel_id = cur.lastrowid
234 230
 
235 231
         if class_table:
@@ -237,7 +233,7 @@ class LeDataSourceSQL(DummyDatasource):
237 233
             datas[fk_name] = lodel_id
238 234
             sql = insert(class_table, datas)
239 235
             #print (sql)
240
-            self.datasource_utils.query(self.connection, sql)
236
+            utils.query(self.connection, sql)
241 237
 
242 238
         return lodel_id
243 239
 

Loading…
Annuler
Enregistrer