Native gui usable for data editing. Lots of fixes and improvenemts
This commit is contained in:
1 parent
a6abdaa614
commit
09387280fc
52 files changed
+1579
-294
No files matched your search
+38
-11
@@ -63,11 +63,18 @@ class Database:
|
||||
|
||||
def last_error(self):
|
||||
if not self.error:
|
||||
return(0, '')
|
||||
code = self.error.args[0]
|
||||
text = self.error.args[1]
|
||||
return('UnknownType', 0, '')
|
||||
|
||||
etype = type(self.error).__name__
|
||||
ecode = 0
|
||||
etext = str(self.error)
|
||||
|
||||
if len(self.error.args) >= 2 and isinstance(self.error.args[0], int):
|
||||
ecode = self.error.args[0]
|
||||
etext = str(self.error.args[1])
|
||||
|
||||
self.error = None
|
||||
return(code, text)
|
||||
return(etype, ecode, etext)
|
||||
|
||||
def load_enum(self, table, column):
|
||||
"""
|
||||
@@ -133,14 +140,15 @@ class Database:
|
||||
self.conn.commit()
|
||||
return self.cur.rowcount == 1
|
||||
|
||||
def load_setting(self, sno):
|
||||
def load_setting(self, sno, autocreate=True):
|
||||
sql = "SELECT valstr, valint FROM settings WHERE userid=0 AND sno=%s"
|
||||
self.cur.execute(sql, (sno,))
|
||||
row = self.cur.fetchone()
|
||||
if not row:
|
||||
sql = "INSERT INTO settings (userid, sno) VALUES (0, %s)"
|
||||
self.cur.execute(sql, (sno,))
|
||||
self.conn.commit()
|
||||
if autocreate:
|
||||
sql = "INSERT INTO settings (userid, sno) VALUES (0, %s)"
|
||||
self.cur.execute(sql, (sno,))
|
||||
self.conn.commit()
|
||||
return (None, None)
|
||||
return row
|
||||
|
||||
@@ -149,22 +157,41 @@ class Database:
|
||||
values = (valstr, valint, sno)
|
||||
return self.execute_write(sql, values, 1)
|
||||
|
||||
def get_free_number(self, table, column):
|
||||
"""
|
||||
Get first free number from column, so gaps are filled
|
||||
"""
|
||||
sql = (
|
||||
"SELECT MIN(n.{col}) AS nextval "
|
||||
"FROM ("
|
||||
"SELECT 1 AS {col} "
|
||||
"UNION ALL "
|
||||
"SELECT DISTINCT {col} + 1 "
|
||||
"FROM {tbl} "
|
||||
"WHERE {col} > 0"
|
||||
") AS n "
|
||||
"LEFT JOIN {tbl} AS m ON m.{col}=n.{col} "
|
||||
"WHERE m.{col} IS NULL"
|
||||
).format(tbl=table, col=column)
|
||||
self.cur.execute(sql)
|
||||
return self.cur.fetchone()[0] or 1
|
||||
|
||||
def execute_write(self, sql, values=(), expected_rows=None):
|
||||
"""
|
||||
Execute an INSERT, UPDATE, or DELETE statement.
|
||||
expected_rows:
|
||||
None = do not check the number of affected rows
|
||||
1 = expect exactly one affected row
|
||||
0 = expect no affected rows
|
||||
0 = expect no affected rows / no data changed
|
||||
"""
|
||||
try:
|
||||
self.cur.execute(sql, values)
|
||||
except MySQLdb.Error as e:
|
||||
self.rollback()
|
||||
self.conn.rollback()
|
||||
self.error = e
|
||||
return False
|
||||
if (expected_rows is not None and self.cur.rowcount != expected_rows):
|
||||
self.rollback()
|
||||
self.conn.rollback()
|
||||
self.error = RuntimeError(
|
||||
"Expected {} affected rows, got {}".format(
|
||||
expected_rows,
|
||||
|
||||
Reference in new issue
Block a user