Browse Source

Implement the LeDatasource.insert() method + user friendly LeObject.insert() datas arg

Yann Weber 9 years ago
parent
commit
bdd85d9bde
2 changed files with 30 additions and 9 deletions
  1. 27
    9
      leobject/datasources/ledatasourcesql.py
  2. 3
    0
      leobject/leobject.py

+ 27
- 9
leobject/datasources/ledatasourcesql.py View File

@@ -32,13 +32,31 @@ class LeDataSourceSQL(DummyDatasource):
32 32
     # @return int : lodel_id of the created object
33 33
     # @todo add the returning clause and the insertion in "object"
34 34
     def insert(self, letype, leclass, datas):
35
-        query_table_name = self._get_table_name_from_class_name(leclass.__name__)
36
-
37
-        query = insert(query_table_name, datas)
38
-        with self.connection as cur:
39
-            cur.execute(query)
40
-
41
-        return True
35
+        if isinstance(datas, list):
36
+            res = list()
37
+            for data in datas:
38
+                res.append(self.insert(letype, leclass, data))
39
+        elif isinstance(datas, dict):
40
+            
41
+            with self.connection as cur:
42
+                object_datas = {'class_id': leclass._class_id, 'type_id': letype._type_id}
43
+                if cur.execute(insert(self.OBJECTS_TABLE_NAME, object_datas)) != 1:
44
+                    raise RuntimeError('SQL error')
45
+                    
46
+                if cur.execute('SELECT last_insert_id() as lodel_id') != 1:
47
+                    raise RuntimeError('SQL error')
48
+                    
49
+                lodel_id, = cur.fetchone()
50
+
51
+                print("Object inserted : %s with lodel_id %s"%(object_datas, lodel_id))
52
+                datas[self.LODEL_ID_FIELD] = lodel_id
53
+                query_table_name = self._get_table_name_from_class_name(leclass.__name__)
54
+                query = insert(query_table_name, datas)
55
+
56
+                if cur.execute(query) != 1:
57
+                    raise RuntimeError('SQL error')
58
+
59
+            return lodel_id
42 60
 
43 61
     ## @brief search for a collection of objects
44 62
     # @param leclass LeClass
@@ -145,7 +163,7 @@ class LeDataSourceSQL(DummyDatasource):
145 163
     # @params classname str
146 164
     # @return str
147 165
     def _get_table_name_from_class_name(self, classname):
148
-        return classname if self.CLASS_TABLE_PREFIX in classname else "%s%s" % (self.CLASS_TABLE_PREFIX, classname)
166
+        return (classname if self.CLASS_TABLE_PREFIX in classname else "%s%s" % (self.CLASS_TABLE_PREFIX, classname)).lower()
149 167
 
150 168
     ## @brief prepares the relational filters
151 169
     # @params rel_filters : (("superior"|"subordinate"), operator, value)
@@ -179,4 +197,4 @@ class LeDataSourceSQL(DummyDatasource):
179 197
                 prepared_filter_value = filter_item[2]
180 198
                 prepared_filters[prepared_filter_key] = prepared_filter_value
181 199
 
182
-        return prepared_filters
200
+        return prepared_filters

+ 3
- 0
leobject/leobject.py View File

@@ -54,6 +54,9 @@ class _LeObject(object):
54 54
     # @see leobject.datasources.dummy.DummyDatasource.insert(), leobject.letype.LeType.insert()
55 55
     @classmethod
56 56
     def insert(cls, letype, datas):
57
+        if isinstance(datas, dict):
58
+            datas = [datas]
59
+
57 60
         if cls == _LeObject:
58 61
             raise NotImplementedError("Abstract method")
59 62
         letype,leclass = cls._prepare_targets(letype)

Loading…
Cancel
Save