mirror of
https://gitlab.com/datalifeit/trytond-edw_mssql
synced 2023-12-14 05:52:58 +01:00
parent
d3fb408c26
commit
2d241cff73
32
connector.py
32
connector.py
|
@ -2,6 +2,7 @@
|
||||||
# copyright notices and license terms.
|
# copyright notices and license terms.
|
||||||
from trytond.model import fields
|
from trytond.model import fields
|
||||||
from trytond.modules.edw.connector import BackendConnector
|
from trytond.modules.edw.connector import BackendConnector
|
||||||
|
from trytond.modules.edw.tools import evaluate_sql_domain
|
||||||
import pymssql
|
import pymssql
|
||||||
|
|
||||||
INSERT_QUERY = (
|
INSERT_QUERY = (
|
||||||
|
@ -17,11 +18,14 @@ CREATE_QUERY = (
|
||||||
""" where name='%(tablename)s' and xtype='U') """ +
|
""" where name='%(tablename)s' and xtype='U') """ +
|
||||||
""" CREATE TABLE [%(tablename)s] (%(fields)s);""")
|
""" CREATE TABLE [%(tablename)s] (%(fields)s);""")
|
||||||
|
|
||||||
DELETE_QUERY = (
|
DROP_QUERY = (
|
||||||
"""IF EXISTS (select * from sysobjects """ +
|
"""IF EXISTS (select * from sysobjects """ +
|
||||||
""" where name='%(tablename)s' and xtype='U') """ +
|
""" where name='%(tablename)s' and xtype='U') """ +
|
||||||
""" DROP TABLE [%(tablename)s]; """)
|
""" DROP TABLE [%(tablename)s]; """)
|
||||||
|
|
||||||
|
DELETE_QUERY = (
|
||||||
|
""" DELETE FROM \"%(tablename)s\" WHERE %(where)s; """)
|
||||||
|
|
||||||
|
|
||||||
class BackendConnectorMSsql(BackendConnector):
|
class BackendConnectorMSsql(BackendConnector):
|
||||||
"""MS SQL Connector"""
|
"""MS SQL Connector"""
|
||||||
|
@ -34,7 +38,7 @@ class BackendConnectorMSsql(BackendConnector):
|
||||||
user=self.username, password=self.password, database=database)
|
user=self.username, password=self.password, database=database)
|
||||||
return conn
|
return conn
|
||||||
|
|
||||||
def create_table(self, fields, tablename):
|
def create(self, fields, tablename):
|
||||||
query = self._get_create_query(fields, tablename)
|
query = self._get_create_query(fields, tablename)
|
||||||
self.execute_query(query)
|
self.execute_query(query)
|
||||||
|
|
||||||
|
@ -42,12 +46,15 @@ class BackendConnectorMSsql(BackendConnector):
|
||||||
query = self._get_insert_query(tablename, results)
|
query = self._get_insert_query(tablename, results)
|
||||||
self.execute_query(query, [tuple(r.values()) for r in results])
|
self.execute_query(query, [tuple(r.values()) for r in results])
|
||||||
|
|
||||||
def truncate(self, tablename):
|
def clean(self, tablename, domain=None):
|
||||||
query = self._get_truncate_query(tablename)
|
if not domain:
|
||||||
|
query = self._get_truncate_query(tablename)
|
||||||
|
else:
|
||||||
|
query = self._get_delete_query(tablename, domain)
|
||||||
self.execute_query(query)
|
self.execute_query(query)
|
||||||
|
|
||||||
def delete_table(self, tablename):
|
def drop(self, tablename):
|
||||||
query = self._get_delete_query(tablename)
|
query = self._get_drop_query(tablename)
|
||||||
self.execute_query(query)
|
self.execute_query(query)
|
||||||
|
|
||||||
def _get_create_query(self, fields, tablename):
|
def _get_create_query(self, fields, tablename):
|
||||||
|
@ -71,8 +78,14 @@ class BackendConnectorMSsql(BackendConnector):
|
||||||
def _get_truncate_query(self, tablename):
|
def _get_truncate_query(self, tablename):
|
||||||
return TRUNCATE_QUERY % {'tablename': tablename}
|
return TRUNCATE_QUERY % {'tablename': tablename}
|
||||||
|
|
||||||
def _get_delete_query(self, tablename):
|
def _get_drop_query(self, tablename):
|
||||||
return DELETE_QUERY % {'tablename': tablename}
|
return DROP_QUERY % {'tablename': tablename}
|
||||||
|
|
||||||
|
def _get_delete_query(self, tablename, where):
|
||||||
|
return DELETE_QUERY % {
|
||||||
|
'tablename': tablename,
|
||||||
|
'where': where or '1=1'
|
||||||
|
}
|
||||||
|
|
||||||
def get_mapped_types(self):
|
def get_mapped_types(self):
|
||||||
return {
|
return {
|
||||||
|
@ -107,3 +120,6 @@ class BackendConnectorMSsql(BackendConnector):
|
||||||
raise e
|
raise e
|
||||||
finally:
|
finally:
|
||||||
connection.close()
|
connection.close()
|
||||||
|
|
||||||
|
def evaluate_domain(self, model, domain):
|
||||||
|
return evaluate_sql_domain(model, domain)
|
||||||
|
|
Loading…
Reference in a new issue