Simplify query construction
This commit is contained in:
parent
94d3234902
commit
51d78819b7
1 changed files with 6 additions and 15 deletions
|
@ -83,24 +83,15 @@ class Database():
|
|||
def query(self, table, values=dict(), order_by=list()):
|
||||
sql = f"select * from {table.name}"
|
||||
|
||||
params = list()
|
||||
first = True
|
||||
|
||||
for key in values.keys():
|
||||
if first:
|
||||
sql += f" where {table.name}.{key} = ?"
|
||||
|
||||
first = False
|
||||
else:
|
||||
sql += f" and {table.name}.{key} = ?"
|
||||
|
||||
params.append(values[key])
|
||||
|
||||
first = True
|
||||
if len(values) > 0:
|
||||
sql += " where "
|
||||
sql += " and ".join([f"{table.name}.{k} = ?" for k in values])
|
||||
|
||||
if len(order_by) > 0:
|
||||
sql += " order by"
|
||||
|
||||
first = True
|
||||
|
||||
for column, order in order_by:
|
||||
if first:
|
||||
first = False
|
||||
|
@ -115,7 +106,7 @@ class Database():
|
|||
sql += f" {column} desc"
|
||||
|
||||
cr = DatabaseTableCursor(table, self.db.cursor())
|
||||
cr.execute(sql, params)
|
||||
cr.execute(sql, list(values.values()))
|
||||
|
||||
return cr
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue