Better logic for db inserts
This commit is contained in:
parent
694d7ca62d
commit
3c13625e63
1 changed files with 7 additions and 8 deletions
|
@ -1,6 +1,6 @@
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from sqlalchemy import create_engine
|
from sqlalchemy import create_engine, Table, MetaData
|
||||||
|
|
||||||
from platypush.message.response import Response
|
from platypush.message.response import Response
|
||||||
|
|
||||||
|
@ -33,12 +33,13 @@ class DbPlugin(Plugin):
|
||||||
return self.engine
|
return self.engine
|
||||||
|
|
||||||
def execute(self, statement, engine=None, *args, **kwargs):
|
def execute(self, statement, engine=None, *args, **kwargs):
|
||||||
""" Executes a generic SQL statement """
|
""" Executes a raw SQL statement """
|
||||||
|
|
||||||
engine = self._get_engine(engine, *args, **kwargs)
|
engine = self._get_engine(engine, *args, **kwargs)
|
||||||
|
|
||||||
with engine.connect() as connection:
|
with engine.connect() as connection:
|
||||||
result = connection.execute(statement)
|
result = connection.execute(statement)
|
||||||
|
connection.commit()
|
||||||
|
|
||||||
return Response()
|
return Response()
|
||||||
|
|
||||||
|
@ -65,12 +66,10 @@ class DbPlugin(Plugin):
|
||||||
engine = self._get_engine(engine, *args, **kwargs)
|
engine = self._get_engine(engine, *args, **kwargs)
|
||||||
|
|
||||||
for record in records:
|
for record in records:
|
||||||
statement = 'INSERT INTO {}({}) VALUES({})'.format \
|
metadata = MetaData()
|
||||||
(table, ','.join(record.keys()),
|
table = Table(table, metadata, autoload=True, autoload_with=engine)
|
||||||
','.join([ ':' + key for key in record.keys() ]))
|
insert = table.insert().values(**record)
|
||||||
|
engine.execute(insert)
|
||||||
with engine.connect() as connection:
|
|
||||||
connection.execute(statement, **record)
|
|
||||||
|
|
||||||
return Response()
|
return Response()
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue