Skip to content

Commit 3f24c38

Browse files
committed
refs #281 MSAccess fixes
Duplicate now working. DROP TABLE IF EXISTS does not seem to work reliably through Jackcess, so added a workaround. The lastrowid was not being returned due to the nature of the executing a list of queries, so grabbed the inserted id after all of the queries were finished.
1 parent f904950 commit 3f24c38

File tree

2 files changed

+31
-20
lines changed

2 files changed

+31
-20
lines changed
52 KB
Binary file not shown.

pysimplesql/pysimplesql.py

+31-20
Original file line numberDiff line numberDiff line change
@@ -1019,7 +1019,7 @@ def requery(
10191019
rows = self.driver.execute(query)
10201020
self.rows = rows
10211021

1022-
if len(self.rows.index):
1022+
if len(self.rows.index) and self.pk_column is not None:
10231023
if "sort_order" not in self.rows.attrs:
10241024
# Store the sort order as a dictionary in the attrs of the DataFrame
10251025
sort_order = self.rows[self.pk_column].to_list()
@@ -7758,25 +7758,31 @@ def execute(
77587758
row[column_name] = value
77597759
rows.append(row)
77607760

7761-
return Result.set(rows, None, exception, column_info)
7761+
# Set the last row ID
7762+
lastrowid = None
7763+
if "insert" in query.lower():
7764+
res = self.execute("SELECT @@IDENTITY AS ID")
7765+
lastrowid = res.iloc[0]["ID"]
7766+
7767+
return Result.set(rows, lastrowid, exception, column_info)
77627768

7763-
affected_rows = stmt.getUpdateCount()
7764-
return Result.set([], affected_rows, exception, column_info)
7769+
stmt.getUpdateCount()
7770+
return Result.set([], None, exception, column_info)
77657771

77667772
def column_info(self, table):
77677773
meta_data = self.con.getMetaData()
77687774
rs = meta_data.getColumns(None, None, table, None)
7769-
print("Getting column info...")
7775+
77707776
col_info = ColumnInfo(self, table)
77717777
pk_columns = [self.pk_column(table)]
7772-
print("pk_columns", pk_columns)
7778+
77737779
while rs.next():
77747780
name = str(rs.getString("column_name"))
77757781
domain = str(rs.getString("TYPE_NAME")).upper()
77767782
notnull = str(rs.getString("IS_NULLABLE")) == "NO"
77777783
default = str(rs.getString("COLUMN_DEF"))
77787784
pk = name in pk_columns
7779-
print("name", name)
7785+
77807786
col_info.append(
77817787
Column(
77827788
name=name, domain=domain, notnull=notnull, default=default, pk=pk
@@ -7786,7 +7792,6 @@ def column_info(self, table):
77867792
return col_info
77877793

77887794
def pk_column(self, table):
7789-
print(f"Getting pk_column for {table}")
77907795
meta_data = self.con.getMetaData()
77917796
rs = meta_data.getPrimaryKeys(None, None, table)
77927797
if rs.next():
@@ -7859,12 +7864,12 @@ def _get_column_definitions(self, table_name):
78597864
# Creates a comma separated list of column names and types to be used in a
78607865
# CREATE TABLE statement
78617866
columns = self.column_info(table_name)
7862-
print("columns", columns)
7867+
78637868
cols = ""
78647869
for c in columns:
78657870
cols += f"{c['name']} {c['domain']}, "
78667871
cols = cols[:-2]
7867-
print("\ncols\n", cols)
7872+
78687873
return cols
78697874

78707875
def duplicate_record(self, dataset: DataSet, children: bool) -> pd.DataFrame:
@@ -7883,9 +7888,11 @@ def duplicate_record(self, dataset: DataSet, children: bool) -> pd.DataFrame:
78837888
description_column = dataset.description_column
78847889

78857890
# Create tmp table, update pk column in temp and insert into table
7886-
f"WHERE {pk_column}={dataset.get_current(dataset.pk_column)}"
7887-
query = [
7888-
f"DROP TABLE IF EXISTS [{tmp_table}];",
7891+
query = []
7892+
if tmp_table in self.get_tables():
7893+
query.append(f"DROP TABLE [{tmp_table}];")
7894+
7895+
query += [
78897896
f"CREATE TABLE [{tmp_table}] ({self._get_column_definitions(table)});",
78907897
(
78917898
f"INSERT INTO [{tmp_table}] (SELECT * FROM [{table}] "
@@ -7897,15 +7904,16 @@ def duplicate_record(self, dataset: DataSet, children: bool) -> pd.DataFrame:
78977904
),
78987905
f"UPDATE [{tmp_table}] SET {description_column} = {description}",
78997906
f"INSERT INTO [{table}] SELECT * FROM [{tmp_table}];",
7900-
f"DROP TABLE IF EXISTS [{tmp_table}]",
7907+
f"DROP TABLE [{tmp_table}]",
79017908
]
79027909
for q in query:
7903-
res = self.execute(q)
7910+
res = self.execute(q, auto_commit_rollback=True)
79047911
if res.attrs["exception"]:
79057912
return res
79067913

79077914
# Now we save the new pk
7908-
pk = res.attrs["lastrowid"]
7915+
res = self.execute("SELECT @@IDENTITY AS ID")
7916+
lastrowid = res.loc[0]["ID"]
79097917

79107918
# create list of which children we have duplicated
79117919
child_duplicated = []
@@ -7925,8 +7933,11 @@ def duplicate_record(self, dataset: DataSet, children: bool) -> pd.DataFrame:
79257933

79267934
# Update children's pk_columns to NULL and set correct parent
79277935
# PK value.
7928-
queries = [
7929-
f"DROP TABLE IF EXISTS [{tmp_child}]",
7936+
query = []
7937+
if tmp_child in self.get_tables():
7938+
query.append(f"DROP TABLE [{tmp_child}];")
7939+
7940+
query += [
79307941
(
79317942
f"CREATE TABLE [{tmp_table}] "
79327943
f"({self._get_column_definitions(table)});"
@@ -7940,7 +7951,7 @@ def duplicate_record(self, dataset: DataSet, children: bool) -> pd.DataFrame:
79407951
f"UPDATE [{tmp_child}] SET {pk_column} = NULL;",
79417952
f"UPDATE [{tmp_child}] SET {fk_column} = {pk}",
79427953
f"INSERT INTO [{child}] SELECT * FROM [{tmp_child}];",
7943-
f"DROP TABLE IF EXISTS [{tmp_child}]",
7954+
f"DROP TABLE [{tmp_child}]",
79447955
]
79457956
for q in queries:
79467957
res = self.execute(q)
@@ -7950,7 +7961,7 @@ def duplicate_record(self, dataset: DataSet, children: bool) -> pd.DataFrame:
79507961
child_duplicated.append(r.child_table)
79517962
# If we made it here, we can return the pk. Since the pk was stored earlier,
79527963
# we will just send and empty DataFrame
7953-
return Result.set(lastrowid=pk)
7964+
return Result.set(lastrowid=lastrowid)
79547965

79557966
def insert_record(self, table: str, pk: int, pk_column: str, row: dict):
79567967
# Remove the pk column

0 commit comments

Comments
 (0)