Skip to content

Commit 10427c8

Browse files
committed
Working mysql/postgres sql drivers
Also fixed duplicate button always disabled Found other places where we needed to look at attrs instead of result.exception
1 parent 9d86d1b commit 10427c8

File tree

1 file changed

+49
-40
lines changed

1 file changed

+49
-40
lines changed

pysimplesql/pysimplesql.py

+49-40
Original file line numberDiff line numberDiff line change
@@ -1384,9 +1384,9 @@ def set_by_pk(
13841384
# don't update self/dependents if we are going to below anyway
13851385
self.prompt_save(update_elements=False)
13861386

1387-
# Move to the numerical index of where the primary key is located. If the pk
1388-
# value can't be found, move to the last index
1389-
idx = [i for i, value in enumerate(self.rows[self.pk_column]) if value == pk]
1387+
# Move to the numerical index of where the primary key is located.
1388+
# If the pk value can't be found, move to the last index
1389+
idx = self.rows.sort_index().index[self.rows[self.pk_column] == pk].tolist()
13901390
idx = idx[0] if idx else len(self.rows.index)
13911391
self.current_index = idx
13921392

@@ -1683,7 +1683,7 @@ def save_record(
16831683
result = self.driver.save_record(
16841684
self, q["changed_row"], q["where_clause"]
16851685
)
1686-
if result.exception is not None:
1686+
if result.attrs["exception"] is not None:
16871687
self.frm.popup.ok(
16881688
lang.dataset_save_keyed_fail_title,
16891689
lang.dataset_save_keyed_fail.format_map(
@@ -1850,21 +1850,21 @@ def delete_record(
18501850
# Delete child records first!
18511851
result = self.driver.delete_record(self, True)
18521852

1853-
if not isinstance(result, pd.DataFrame):
1854-
if result == DELETE_RECURSION_LIMIT_ERROR:
1855-
self.frm.popup.ok(
1856-
lang.delete_failed_title,
1857-
lang.delete_failed.format_map(
1858-
LangFormat(exception=lang.delete_recursion_limit_error)
1859-
),
1860-
)
1861-
elif result.exception is not None:
1862-
self.frm.popup.ok(
1863-
lang.delete_failed_title,
1864-
lang.delete_failed.format_map(
1865-
LangFormat(exception=result.exception)
1866-
),
1867-
)
1853+
if (
1854+
not isinstance(result, pd.DataFrame)
1855+
and result == DELETE_RECURSION_LIMIT_ERROR
1856+
):
1857+
self.frm.popup.ok(
1858+
lang.delete_failed_title,
1859+
lang.delete_failed.format_map(
1860+
LangFormat(exception=lang.delete_recursion_limit_error)
1861+
),
1862+
)
1863+
elif result.attrs["exception"] is not None:
1864+
self.frm.popup.ok(
1865+
lang.delete_failed_title,
1866+
lang.delete_failed.format_map(LangFormat(exception=result.exception)),
1867+
)
18681868

18691869
# callback
18701870
if "after_delete" in self.callbacks:
@@ -2379,7 +2379,7 @@ def insert_row(self, row: dict, idx: int = None) -> None:
23792379
if idx is None:
23802380
idx = len(self.rows.index)
23812381
idx_label = self.rows.index.max() if len(self.rows.index) > 0 else 0
2382-
self.rows.attrs["virtual"].loc[idx_label] = True
2382+
self.rows.attrs["virtual"].loc[idx_label] = 1 # True, series only holds int64
23832383

23842384

23852385
class Form:
@@ -3295,7 +3295,7 @@ def update_elements(
32953295

32963296
# Disable duplicate if no rows, edit protect, or current row virtual
32973297
elif ":table_duplicate" in m["event"]:
3298-
disable = (
3298+
disable = bool(
32993299
len(self[data_key].rows.index) == 0
33003300
or self._edit_protect
33013301
or self[data_key]
@@ -6533,7 +6533,7 @@ def duplicate_record(self, dataset: DataSet, children: bool) -> pd.DataFrame:
65336533
]
65346534
for q in queries:
65356535
res = self.execute(q)
6536-
if res.exception:
6536+
if res.attrs["exception"]:
65376537
return res
65386538

65396539
child_duplicated.append(r.child_table)
@@ -6914,7 +6914,7 @@ def save_record(
69146914
# Have SQlite save this record
69156915
result = super().save_record(dataset, changed_row, where_clause)
69166916

6917-
if result.exception is None:
6917+
if result.attrs["exception"] is None:
69186918
# No it is safe to write our data back out to the CSV file
69196919

69206920
# Update the DataSet object's DataFra,e with the changes, so then
@@ -7043,7 +7043,7 @@ def execute(
70437043

70447044
lastrowid = cursor.lastrowid if cursor.lastrowid else None
70457045

7046-
return pd.DataFrame(
7046+
return Result.set(
70477047
[dict(row) for row in rows], lastrowid, exception, column_info
70487048
)
70497049

@@ -7052,15 +7052,15 @@ def get_tables(self):
70527052
"SELECT TABLE_NAME FROM information_schema.tables WHERE table_schema = %s"
70537053
)
70547054
rows = self.execute(query, [self.database], silent=True)
7055-
return [row["TABLE_NAME"] for row in rows]
7055+
return list(rows["TABLE_NAME"])
70567056

70577057
def column_info(self, table):
70587058
# Return a list of column names
70597059
query = "DESCRIBE {}".format(table)
70607060
rows = self.execute(query, silent=True)
70617061
col_info = ColumnInfo(self, table)
70627062

7063-
for row in rows:
7063+
for _, row in rows.iterrows():
70647064
name = row["Field"]
70657065
# Check if the value is a bytes-like object, and decode if necessary
70667066
type_value = (
@@ -7084,9 +7084,10 @@ def column_info(self, table):
70847084

70857085
def pk_column(self, table):
70867086
query = "SHOW KEYS FROM {} WHERE Key_name = 'PRIMARY'".format(table)
7087-
cur = self.execute(query, silent=True)
7088-
row = cur.fetchone()
7089-
return row["Column_name"] if row else None
7087+
rows = self.execute(query, silent=True)
7088+
for _, row in rows.iterrows():
7089+
return row["Column_name"]
7090+
return None
70907091

70917092
def relationships(self):
70927093
# Return a list of dicts {from_table,to_table,from_column,to_column,requery}
@@ -7099,7 +7100,7 @@ def relationships(self):
70997100
)
71007101
rows = self.execute(query, (from_table,), silent=True)
71017102

7102-
for row in rows:
7103+
for _, row in rows.iterrows():
71037104
dic = {}
71047105
# Get the constraint information
71057106
on_update, on_delete = self.constraint(row["CONSTRAINT_NAME"])
@@ -7131,7 +7132,12 @@ def constraint(self, constraint_name):
71317132
f"'{constraint_name}'"
71327133
)
71337134
rows = self.execute(query, silent=True)
7134-
return rows[0]["UPDATE_RULE"], rows[0]["DELETE_RULE"]
7135+
for _, row in rows.iterrows():
7136+
if "UPDATE_RULE" in row:
7137+
update_rule = row["UPDATE_RULE"]
7138+
if "DELETE_RULE" in row:
7139+
delete_rule = row["DELETE_RULE"]
7140+
return update_rule, delete_rule
71357141

71367142

71377143
# --------------------------------------------------------------------------------------
@@ -7289,7 +7295,7 @@ def execute(
72897295
# In Postgres, the cursor does not return a lastrowid. We will not set it here,
72907296
# we will instead set it in save_records() due to the RETURNING stement of the
72917297
# query
7292-
return pd.DataFrame(
7298+
return Result.set(
72937299
[dict(row) for row in rows], exception=exception, column_info=column_info
72947300
)
72957301

@@ -7300,7 +7306,7 @@ def get_tables(self):
73007306
)
73017307
# query = "SELECT tablename FROM pg_tables WHERE table_schema='public'"
73027308
rows = self.execute(query, silent=True)
7303-
return [row["table_name"] for row in rows]
7309+
return list(rows["table_name"])
73047310

73057311
def column_info(self, table: str) -> ColumnInfo:
73067312
# Return a list of column names
@@ -7309,7 +7315,7 @@ def column_info(self, table: str) -> ColumnInfo:
73097315

73107316
col_info = ColumnInfo(self, table)
73117317
pk_column = self.pk_column(table)
7312-
for row in rows:
7318+
for _, row in rows.iterrows():
73137319
name = row["column_name"]
73147320
domain = row["data_type"].upper()
73157321
notnull = row["is_nullable"] != "YES"
@@ -7334,9 +7340,10 @@ def pk_column(self, table):
73347340
"kcu.constraint_name WHERE tc.constraint_type = 'PRIMARY KEY' AND "
73357341
f"tc.table_name = '{table}' "
73367342
)
7337-
cur = self.execute(query, silent=True)
7338-
row = cur.fetchone()
7339-
return row["column_name"] if row else None
7343+
rows = self.execute(query, silent=True)
7344+
for _, row in rows.iterrows():
7345+
return row["column_name"]
7346+
return None
73407347

73417348
def relationships(self):
73427349
# Return a list of dicts {from_table,to_table,from_column,to_column,requery}
@@ -7357,7 +7364,7 @@ def relationships(self):
73577364

73587365
rows = self.execute(query, (from_table,), silent=True)
73597366

7360-
for row in rows:
7367+
for _, row in rows.iterrows():
73617368
dic = {}
73627369
# Get the constraint information
73637370
# constraint = self.constraint(row['conname'])
@@ -7403,7 +7410,9 @@ def next_pk(self, table: str, pk_column: str) -> int:
74037410
# wrap the quoted string in singe quotes. Phew!
74047411
q = f"SELECT nextval('{seq}') LIMIT 1;"
74057412
rows = self.execute(q, silent=True)
7406-
return rows.fetchone()["nextval"]
7413+
for _, row in rows.iterrows():
7414+
return row["nextval"]
7415+
return None
74077416

74087417
def insert_record(self, table: str, pk: int, pk_column: str, row: dict):
74097418
# insert_record() for Postgres is a little different from the rest. Instead of
@@ -7419,7 +7428,7 @@ def insert_record(self, table: str, pk: int, pk_column: str, row: dict):
74197428
values = [value for key, value in row.items()]
74207429
result = self.execute(query, tuple(values))
74217430

7422-
result.lastid = pk
7431+
result.attrs["lastid"] = pk
74237432
return result
74247433

74257434
def execute_script(self, script):

0 commit comments

Comments
 (0)