#!/usr/bin/python # -*- coding: utf-8 -*- # --------------------------------------------------------------------------------------------------------- # Author : Ulrich Carmesin # Source : gitea.ucarmesin.de # --------------------------------------------------------------------------------------------------------- import basic.program import utils.config_tool import utils.db_abstract import basic.constants as B import utils.path_tool import utils.file_tool import utils.tdata_tool class DbFcts(utils.db_abstract.DbFcts): """ This interface defines each necessary connection to any kind of database. The specific technique how to connect to the concrete DBMS has to be implemented in the specific tool. """ def __init__(self): pass def selectRows(self, table): """ method to select rows from a database statement written in sql """ sqlTable = utils.db_abstract.getSqlTable(self.comp, table) header = "" path = utils.path_tool.composePattern("{env.dompath}/"+sqlTable+".csv", self.comp) print(path) tdata = {} data = utils.tdata_tool.readCsv(self.comp.m, path, self.comp) tdata[B.DATA_NODE_HEADER] = self.comp.conf[B.DATA_NODE_DDL][table][B.DATA_NODE_HEADER] if B.DATA_NODE_TABLES in data \ and table in data[B.DATA_NODE_TABLES]\ and B.DATA_NODE_DATA in data[B.DATA_NODE_TABLES][table]: tdata[B.DATA_NODE_DATA] = data[B.DATA_NODE_TABLES][table][B.DATA_NODE_DATA] else: tdata[B.DATA_NODE_DATA] = {} return tdata def deleteRows(self, table): """ method to delete rows from a database statement written in sql """ job = basic.program.Job.getInstance() verify = -1+job.getDebugLevel("db_tool") sqlTable = utils.db_abstract.getSqlTable(self.comp, table) header = "" path = utils.path_tool.composePattern("{env.dompath}/"+sqlTable+".csv", self.comp) for h in self.comp.conf[B.DATA_NODE_DDL][table][B.DATA_NODE_HEADER]: print(h) header += ";"+h cmd = header[1:]+"\n" utils.file_tool.writeFileText(self.comp.m, path, cmd) self.comp.m.logInfo(cmd) def updateRows(self, statement): """ method to delete rows from a database statement written in sql """ raise Exception(B.EXCEPT_NOT_IMPLEMENT) def insertRows(self, table, rows): """ method to insert rows into a database the rows will be interpreted by the ddl of the component """ job = basic.program.Job.getInstance() verify = -1+job.getDebugLevel("db_tool") sqlTable = utils.db_abstract.getSqlTable(self.comp, table) header = "" path = utils.path_tool.composePattern("{env.dompath}/"+sqlTable+".csv", self.comp) if len(rows) == 0: return "" else: pass for h in self.comp.conf[B.DATA_NODE_DDL][table][B.DATA_NODE_HEADER]: print(h) header += ";"+h cmd = "table:" + table + header+"\n" print("HEADER : "+cmd) rowvalues = "" for r in rows: if not utils.db_abstract.isCompRow(self.comp, r): continue print("r-----------------") print(r) rowvalues = self.comp.name+":"+table for h in self.comp.conf[B.DATA_NODE_DDL][table][B.DATA_NODE_HEADER]: print("head "+h) if h in B.LIST_DB_ATTR: continue if B.DATA_NODE_DATA not in self.comp.conf[B.DATA_NODE_DDL][table]: rowvalues = "" break print("h "+h) if (h in r): rowvalues += ";"+str(self.getDbValue(self.comp.conf[B.DATA_NODE_DDL][table][B.DATA_NODE_DATA][h], r[h])) else: rowvalues += ";"+str(self.getDbValue(self.comp.conf[B.DATA_NODE_DDL][table][B.DATA_NODE_DATA][h], "")) print("rv " + rowvalues) cmd += rowvalues+"\n" utils.file_tool.writeFileText(self.comp.m, path, cmd) self.comp.m.logInfo(cmd) def getConnector(self): """ add-on-method to get the connector this method should only called by the class itself """ job = basic.program.Job.getInstance() return ""