#!/usr/bin/python # -*- coding: utf-8 -*- # --------------------------------------------------------------------------------------------------------- # Author : Ulrich Carmesin # Source : gitea.ucarmesin.de # --------------------------------------------------------------------------------------------------------- import tools.db_abstract import mysql.connector import basic.constants as B import utils.data_const as D class DbFcts(tools.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, job): """ method to select rows from a database statement written in sql """ tdata = {} verify = -1+job.getDebugLevel("db_tool") cmd = "SELECT * FROM "+table+";" #mycursor = self.getConnector() #mycursor.execute(cmd) #myresult = mycursor.fetchall() tdata[B.DATA_NODE_HEADER] = [] for f in self.comp.conf[B.DATA_NODE_DDL][table][B.DATA_NODE_HEADER]: tdata[B.DATA_NODE_HEADER].append(f) myresult = [] for x in myresult: print(x) self.comp.m.logInfo(cmd) return tdata def deleteRows(self, job, table): """ method to delete rows from a database statement written in sql """ verify = -1+job.getDebugLevel("db_tool") cmd = "DELETE FROM "+table+";" self.comp.m.logInfo(cmd) def updateRows(self, statement, job): """ method to delete rows from a database statement written in sql """ raise Exception(B.EXCEPT_NOT_IMPLEMENT) def insertRows(self, job, table, rows): """ method to insert rows into a database the rows will be interpreted by the ddl of the component """ verify = -1+job.getDebugLevel("db_tool") cmd = "INSERT INTO "+table+";" header = "" for h in self.comp.conf[B.DATA_NODE_DDL][table][B.DATA_NODE_HEADER]: print(h) header += ", "+h cmd += " (" + header[1:]+" ) " rowvalues = "" for r in rows: print("r-----------------") print(r) rowvalues = "" cmd += "\n ( " for h in self.comp.conf[B.DATA_NODE_DDL][table][B.DATA_NODE_HEADER]: print("h "+h) if (h in r): rowvalues += ", "+self.getDbValue(self.comp.conf[B.DATA_NODE_DDL][table][B.DATA_NODE_DATA][h], r[h]) else: rowvalues += ", "+self.getDbValue(self.comp.conf[B.DATA_NODE_DDL][table][B.DATA_NODE_DATA][h], "") print("rv " + rowvalues) cmd += rowvalues[1:]+" )," cmd = cmd[0:-1]+";" self.comp.m.logInfo(cmd) def getCreateTable(self, table): sql = "CREATE TABLE IF NOT EXISTS " + self.job.conf[B.TOPIC_NODE_DB][B.ATTR_DB_DATABASE] + "."+table+" (" return sql def getSchemaAttribut(self, attr, atype): if atype == "id": return attr + " INTEGER PRIMARY KEY AUTO_INCREMENT" elif atype == D.TYPE_PK: return attr + " INTEGER PRIMARY KEY AUTO_INCREMENT" elif atype == D.TYPE_STR: return attr + " varchar(50)" elif atype == D.TYPE_STRING: return attr + " varchar(500)" elif atype in [D.TYPE_TEXT, D.TYPE_CLOB, D.TYPE_JLOB, D.TYPE_XLOB]: return attr + " text" elif atype == D.TYPE_INT: return attr + " integer" elif atype == D.TYPE_TIME: return attr + " datetime" def getSchemaSubtable(self, parent, attributes): sql = "CREATE TABLE IF NOT EXISTS "+\ self.job.conf[B.TOPIC_NODE_DB][B.ATTR_DB_DATABASE]+"."+self.getSubTableName(parent, attributes[0]["attr"]) + " (" sql += self.getSchemaAttribut(self.getSubTableId(parent, attributes[0]["attr"]), "id") + "," sql += self.getSchemaAttribut(parent+"id", D.TYPE_INT) + "," for a in attributes: sql += self.getSchemaAttribut(a["attr"], a["atype"]) + "," sql = sql[0:-1]+");" return sql def getSchemaIndex(self, table, attr): if table[0:4] == "idx_": table = table[4:] sql = "CREATE INDEX " sql += "idx_"+table+"_"+attr sql += " ON " + self.job.conf[B.TOPIC_NODE_DB][B.ATTR_DB_DATABASE] + "." + table sql += " ( " + attr + " );" return sql @staticmethod def execStatement(self, statement, conn=None): """ add-on-method to execute the statement this method should only called by the class itself """ raise Exception(B.EXCEPT_NOT_IMPLEMENT)