|
@ -20,30 +20,46 @@ class DbFcts(utils.dbrel_tool.DbFcts): |
|
|
def __init__(self): |
|
|
def __init__(self): |
|
|
pass |
|
|
pass |
|
|
|
|
|
|
|
|
def selectRows(self, table, job): |
|
|
def selectRows(self, table, job, where=""): |
|
|
""" method to select rows from a database |
|
|
""" method to select rows from a database |
|
|
statement written in sql """ |
|
|
statement written in sql """ |
|
|
tdata = {} |
|
|
tdata = {} |
|
|
verify = -1+job.getDebugLevel("db_tool") |
|
|
verify = -1+job.getDebugLevel("db_tool") |
|
|
cmd = "SELECT * FROM "+table+";" |
|
|
attr = self.getDbAttributes(B.SVAL_NULL) |
|
|
#mycursor = self.getConnector() |
|
|
sql = "SELECT * FROM "+attr[B.ATTR_DB_DATABASE]+"."+table |
|
|
#mycursor.execute(cmd) |
|
|
if len(where) > 3: |
|
|
#myresult = mycursor.fetchall() |
|
|
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] = [] |
|
|
tdata[B.DATA_NODE_HEADER] = [] |
|
|
for f in self.comp.conf[B.DATA_NODE_DDL][table][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) |
|
|
tdata[B.DATA_NODE_HEADER].append(f) |
|
|
myresult = [] |
|
|
tdata[B.DATA_NODE_DATA] = [] |
|
|
for x in myresult: |
|
|
for x in myresult: |
|
|
print(x) |
|
|
r = {} |
|
|
self.comp.m.logInfo(cmd) |
|
|
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 |
|
|
return tdata |
|
|
|
|
|
|
|
|
def deleteRows(self, table, job): |
|
|
def deleteRows(self, table, job, where=""): |
|
|
""" method to delete rows from a database |
|
|
""" method to delete rows from a database |
|
|
statement written in sql """ |
|
|
statement written in sql """ |
|
|
verify = -1+job.getDebugLevel("db_tool") |
|
|
verify = -1+job.getDebugLevel("db_tool") |
|
|
cmd = "DELETE FROM "+table+";" |
|
|
attr = self.getDbAttributes(B.SVAL_NULL) |
|
|
self.comp.m.logInfo(cmd) |
|
|
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): |
|
|
def updateRows(self, statement, job): |
|
|
""" method to delete rows from a database |
|
|
""" 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 |
|
|
the rows will be interpreted by the ddl of the component |
|
|
""" |
|
|
""" |
|
|
verify = -1+job.getDebugLevel("db_tool") |
|
|
verify = -1+job.getDebugLevel("db_tool") |
|
|
cmd = "INSERT INTO "+table+";" |
|
|
attr = self.getDbAttributes(B.SVAL_NULL) |
|
|
header = "" |
|
|
sql = "INSERT INTO "+attr[B.ATTR_DB_DATABASE]+"."+table |
|
|
for h in self.comp.conf[B.DATA_NODE_DDL][table][B.DATA_NODE_HEADER]: |
|
|
sql += " ( "+",".join(self.comp.conf[B.DATA_NODE_DDL][table][B.DATA_NODE_HEADER]) + " ) " |
|
|
print(h) |
|
|
sql += " VALUES ( " |
|
|
header += ", "+h |
|
|
for x in self.comp.conf[B.DATA_NODE_DDL][table][B.DATA_NODE_HEADER]: |
|
|
cmd += " (" + header[1:]+" ) " |
|
|
sql += "%s, " |
|
|
rowvalues = "" |
|
|
sql = sql[0:-2] + " )" |
|
|
|
|
|
self.comp.m.logInfo(sql) |
|
|
|
|
|
values = [] |
|
|
for r in rows: |
|
|
for r in rows: |
|
|
print("r-----------------") |
|
|
rowvalues = [] |
|
|
print(r) |
|
|
|
|
|
rowvalues = "" |
|
|
|
|
|
cmd += "\n ( " |
|
|
|
|
|
for h in self.comp.conf[B.DATA_NODE_DDL][table][B.DATA_NODE_HEADER]: |
|
|
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): |
|
|
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: |
|
|
else: |
|
|
rowvalues += ", "+self.getDbValue(self.comp.conf[B.DATA_NODE_DDL][table][B.DATA_NODE_DATA][h], "") |
|
|
rowvalues.append(self.getDbValue(self.comp.conf[B.DATA_NODE_DDL][table][h], "")) |
|
|
print("rv " + rowvalues) |
|
|
values.append( tuple(rowvalues)) |
|
|
cmd += rowvalues[1:]+" )," |
|
|
self.comp.m.logInfo(str(values)) |
|
|
cmd = cmd[0:-1]+";" |
|
|
try: |
|
|
self.comp.m.logInfo(cmd) |
|
|
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 |
|
|
""" add-on-method to execute the statement |
|
|
this method should only called by the class itself """ |
|
|
this method should only called by the class itself """ |
|
|
print("execStatement "+statement) |
|
|
connector = self.getConnector() |
|
|
connector = mysql.connector.connect( |
|
|
|
|
|
host=dbconn["host"], |
|
|
|
|
|
user=dbconn["user"], |
|
|
|
|
|
password=dbconn["passwd"] |
|
|
|
|
|
) |
|
|
|
|
|
cursor = connector.cursor() |
|
|
cursor = connector.cursor() |
|
|
try: |
|
|
try: |
|
|
cursor.execute(statement) |
|
|
cursor.execute(statement) |
|
|
except: |
|
|
connector.commit() |
|
|
|
|
|
except Exception as e: |
|
|
if "CREATE INDEX " in statement: |
|
|
if "CREATE INDEX " in statement: |
|
|
return |
|
|
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 |
|
|