|
|
@ -20,30 +20,46 @@ class DbFcts(utils.dbrel_tool.DbFcts): |
|
|
|
def __init__(self): |
|
|
|
pass |
|
|
|
|
|
|
|
def selectRows(self, table, job): |
|
|
|
def selectRows(self, table, job, where=""): |
|
|
|
""" 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() |
|
|
|
attr = self.getDbAttributes(B.SVAL_NULL) |
|
|
|
sql = "SELECT * FROM "+attr[B.ATTR_DB_DATABASE]+"."+table |
|
|
|
if len(where) > 3: |
|
|
|
sql += " "+where |
|
|
|
sql += ";" |
|
|
|
self.comp.m.logInfo(sql) |
|
|
|
connector = self.getConnector() |
|
|
|
mycursor = connector.cursor() |
|
|
|
mycursor.execute(sql) |
|
|
|
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 = [] |
|
|
|
tdata[B.DATA_NODE_DATA] = [] |
|
|
|
for x in myresult: |
|
|
|
print(x) |
|
|
|
self.comp.m.logInfo(cmd) |
|
|
|
r = {} |
|
|
|
i = 0 |
|
|
|
for f in self.comp.conf[B.DATA_NODE_DDL][table][B.DATA_NODE_HEADER]: |
|
|
|
r[f] = x[i] |
|
|
|
i += 1 |
|
|
|
tdata[B.DATA_NODE_DATA].append(r) |
|
|
|
self.comp.m.logInfo(str(tdata)) |
|
|
|
return tdata |
|
|
|
|
|
|
|
def deleteRows(self, table, job): |
|
|
|
def deleteRows(self, table, job, where=""): |
|
|
|
""" 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) |
|
|
|
attr = self.getDbAttributes(B.SVAL_NULL) |
|
|
|
sql = "DELETE FROM "+attr[B.ATTR_DB_DATABASE]+"."+table |
|
|
|
if len(where) > 3: |
|
|
|
sql += " "+where |
|
|
|
sql += ";" |
|
|
|
self.comp.m.logInfo(sql) |
|
|
|
self.execStatement(sql) |
|
|
|
|
|
|
|
def updateRows(self, statement, job): |
|
|
|
""" method to delete rows from a database |
|
|
@ -55,43 +71,61 @@ class DbFcts(utils.dbrel_tool.DbFcts): |
|
|
|
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 = "" |
|
|
|
attr = self.getDbAttributes(B.SVAL_NULL) |
|
|
|
sql = "INSERT INTO "+attr[B.ATTR_DB_DATABASE]+"."+table |
|
|
|
sql += " ( "+",".join(self.comp.conf[B.DATA_NODE_DDL][table][B.DATA_NODE_HEADER]) + " ) " |
|
|
|
sql += " VALUES ( " |
|
|
|
for x in self.comp.conf[B.DATA_NODE_DDL][table][B.DATA_NODE_HEADER]: |
|
|
|
sql += "%s, " |
|
|
|
sql = sql[0:-2] + " )" |
|
|
|
self.comp.m.logInfo(sql) |
|
|
|
values = [] |
|
|
|
for r in rows: |
|
|
|
print("r-----------------") |
|
|
|
print(r) |
|
|
|
rowvalues = "" |
|
|
|
cmd += "\n ( " |
|
|
|
rowvalues = [] |
|
|
|
for h in self.comp.conf[B.DATA_NODE_DDL][table][B.DATA_NODE_HEADER]: |
|
|
|
print("h "+h) |
|
|
|
if (self.comp.conf[B.DATA_NODE_DDL][table][h] == D.TYPE_PK): |
|
|
|
continue |
|
|
|
if (h in r): |
|
|
|
rowvalues += ", "+self.getDbValue(self.comp.conf[B.DATA_NODE_DDL][table][B.DATA_NODE_DATA][h], r[h]) |
|
|
|
rowvalues.append(self.getDbValue(self.comp.conf[B.DATA_NODE_DDL][table][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) |
|
|
|
rowvalues.append(self.getDbValue(self.comp.conf[B.DATA_NODE_DDL][table][h], "")) |
|
|
|
values.append( tuple(rowvalues)) |
|
|
|
self.comp.m.logInfo(str(values)) |
|
|
|
try: |
|
|
|
connector = self.getConnector() |
|
|
|
mycursor = connector.cursor() |
|
|
|
mycursor.executemany(sql, values) |
|
|
|
connector.commit() |
|
|
|
except Exception as e: |
|
|
|
self.comp.m.setError("") |
|
|
|
return |
|
|
|
self.comp.m.setMsg(str(len(values))+" rows inserted into "+table) |
|
|
|
|
|
|
|
|
|
|
|
def execStatement(self, dbconn, statement): |
|
|
|
def execStatement(self, statement): |
|
|
|
""" add-on-method to execute the statement |
|
|
|
this method should only called by the class itself """ |
|
|
|
print("execStatement "+statement) |
|
|
|
connector = mysql.connector.connect( |
|
|
|
host=dbconn["host"], |
|
|
|
user=dbconn["user"], |
|
|
|
password=dbconn["passwd"] |
|
|
|
) |
|
|
|
connector = self.getConnector() |
|
|
|
cursor = connector.cursor() |
|
|
|
try: |
|
|
|
cursor.execute(statement) |
|
|
|
except: |
|
|
|
connector.commit() |
|
|
|
except Exception as e: |
|
|
|
if "CREATE INDEX " in statement: |
|
|
|
return |
|
|
|
raise Exception("DB-Exception "+statement) |
|
|
|
raise Exception("DB-Exception "+statement+"\n"+e.__str__()) |
|
|
|
print("Statement executed "+statement) |
|
|
|
self.comp.m.setMsg("Statement executed") |
|
|
|
|
|
|
|
def getConnector(self): |
|
|
|
""" add-on-method to get the connector |
|
|
|
this method should only called by the class itself """ |
|
|
|
job = self.job # basic.program.Job.getInstance() |
|
|
|
attr = self.getDbAttributes(B.SVAL_NULL) |
|
|
|
cnx = mysql.connector.connect( |
|
|
|
host=attr[B.ATTR_DB_HOST], |
|
|
|
user=attr[B.ATTR_DB_USER], |
|
|
|
password=attr[B.ATTR_DB_PASSWD], |
|
|
|
database=attr[B.ATTR_DB_DATABASE] |
|
|
|
) |
|
|
|
return cnx |
|
|
|