mirror of
https://github.com/yweber/lodel2.git
synced 2026-07-06 23:50:48 +02:00
SQL: do queries in one function
This commit is contained in:
parent
f99f74fcb1
commit
9fdb73106d
2 changed files with 19 additions and 14 deletions
|
|
@ -18,6 +18,17 @@ class MySQL(object):
|
|||
## @brief Lodel_id for the hierachy root
|
||||
leroot_lodel_id = 0
|
||||
|
||||
@staticmethod
|
||||
## @brief Exec a query
|
||||
# @param query str : SQL query
|
||||
def query(connection, query):
|
||||
with connection as cur:
|
||||
try:
|
||||
cur.execute(query)
|
||||
except Exception as err:
|
||||
raise err
|
||||
return cur
|
||||
|
||||
@classmethod
|
||||
## @brief gets the table name from class name
|
||||
# @param class_name str
|
||||
|
|
|
|||
|
|
@ -46,19 +46,14 @@ class LeDataSourceSQL(DummyDatasource):
|
|||
return res if len(res)>1 else res[0]
|
||||
elif isinstance(datas, dict):
|
||||
|
||||
with self.connection as cur:
|
||||
object_datas = {'class_id': leclass._class_id, 'type_id': letype._type_id}
|
||||
if cur.execute(insert(self.datasource_utils.objects_table_name, object_datas)) != 1:
|
||||
raise RuntimeError('SQL error')
|
||||
|
||||
cur = self.datasource_utils.query(self.connection, insert(self.datasource_utils.objects_table_name, object_datas))
|
||||
lodel_id = cur.lastrowid
|
||||
|
||||
datas[self.datasource_utils.field_lodel_id] = lodel_id
|
||||
query_table_name = self.datasource_utils.get_table_name_from_class(leclass.__name__)
|
||||
query = insert(query_table_name, datas)
|
||||
|
||||
if cur.execute(query) != 1:
|
||||
raise RuntimeError('SQL error')
|
||||
self.datasource_utils.query(self.connection, insert(query_table_name, datas))
|
||||
|
||||
return lodel_id
|
||||
|
||||
|
|
@ -95,8 +90,7 @@ class LeDataSourceSQL(DummyDatasource):
|
|||
query = select(query_table_name, where=where_filters, select=field_list)
|
||||
|
||||
# Executing the query
|
||||
with self.connection as cur:
|
||||
cur.execute(query)
|
||||
cur = self.datasource_utils.query(self.connection, query)
|
||||
results = all_to_dicts(cur)
|
||||
|
||||
return results
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue