Skip to content

Commit f904950

Browse files
committed
refs #281 MSAccess fixes
Duplicate now mostly working (selects wrong item after duplicate still). Had to remove quote_table stuff from the driver, as the variable is used in other places that do not expect it to be quoted - it's not needed anyway, as this is specific driver code and not generalized for all SQLDrivers
1 parent c53caa7 commit f904950

File tree

2 files changed

+30
-30
lines changed

2 files changed

+30
-30
lines changed
16 KB
Binary file not shown.

pysimplesql/pysimplesql.py

+30-30
Original file line numberDiff line numberDiff line change
@@ -7766,17 +7766,17 @@ def execute(
77667766
def column_info(self, table):
77677767
meta_data = self.con.getMetaData()
77687768
rs = meta_data.getColumns(None, None, table, None)
7769-
7769+
print("Getting column info...")
77707770
col_info = ColumnInfo(self, table)
77717771
pk_columns = [self.pk_column(table)]
7772-
7772+
print("pk_columns", pk_columns)
77737773
while rs.next():
7774-
name = str(rs.getString("COLUMN_NAME"))
7774+
name = str(rs.getString("column_name"))
77757775
domain = str(rs.getString("TYPE_NAME")).upper()
77767776
notnull = str(rs.getString("IS_NULLABLE")) == "NO"
77777777
default = str(rs.getString("COLUMN_DEF"))
77787778
pk = name in pk_columns
7779-
7779+
print("name", name)
77807780
col_info.append(
77817781
Column(
77827782
name=name, domain=domain, notnull=notnull, default=default, pk=pk
@@ -7786,10 +7786,11 @@ def column_info(self, table):
77867786
return col_info
77877787

77887788
def pk_column(self, table):
7789+
print(f"Getting pk_column for {table}")
77897790
meta_data = self.con.getMetaData()
77907791
rs = meta_data.getPrimaryKeys(None, None, table)
77917792
if rs.next():
7792-
return str(rs.getString("COLUMN_NAME"))
7793+
return str(rs.getString("column_name"))
77937794
return None
77947795

77957796
def get_tables(self):
@@ -7858,6 +7859,7 @@ def _get_column_definitions(self, table_name):
78587859
# Creates a comma separated list of column names and types to be used in a
78597860
# CREATE TABLE statement
78607861
columns = self.column_info(table_name)
7862+
print("columns", columns)
78617863
cols = ""
78627864
for c in columns:
78637865
cols += f"{c['name']} {c['domain']}, "
@@ -7875,35 +7877,35 @@ def duplicate_record(self, dataset: DataSet, children: bool) -> pd.DataFrame:
78757877
f"{lang.duplicate_prepend}"
78767878
f"{dataset.get_description_for_pk(dataset.get_current_pk())}"
78777879
)
7878-
table = self.quote_table(dataset.table)
7879-
tmp_table = self.quote_table(f"temp_{dataset.table}")
7880-
pk_column = self.quote_column(dataset.pk_column)
7881-
description_column = self.quote_column(dataset.description_column)
7880+
table = dataset.table
7881+
tmp_table = f"temp_{dataset.table}"
7882+
pk_column = dataset.pk_column
7883+
description_column = dataset.description_column
78827884

78837885
# Create tmp table, update pk column in temp and insert into table
78847886
f"WHERE {pk_column}={dataset.get_current(dataset.pk_column)}"
78857887
query = [
7886-
f"DROP TABLE IF EXISTS {tmp_table};",
7887-
f"CREATE TABLE {tmp_table} ({self._get_column_definitions(table)});",
7888+
f"DROP TABLE IF EXISTS [{tmp_table}];",
7889+
f"CREATE TABLE [{tmp_table}] ({self._get_column_definitions(table)});",
78887890
(
7889-
f"INSERT INTO {tmp_table} (SELECT * FROM {table} "
7891+
f"INSERT INTO [{tmp_table}] (SELECT * FROM [{table}] "
78907892
f"WHERE {pk_column}={dataset.get_current(dataset.pk_column)});"
78917893
),
78927894
(
7893-
f"UPDATE {tmp_table} SET {pk_column} = "
7895+
f"UPDATE [{tmp_table}] SET {pk_column} = "
78947896
f"{self.next_pk(dataset.table, dataset.pk_column)};"
78957897
),
7896-
f"UPDATE {tmp_table}]SET {description_column} = {description}",
7897-
f"INSERT INTO {table} SELECT * FROM {tmp_table};",
7898-
f"DROP TABLE IF EXISTS {tmp_table}",
7898+
f"UPDATE [{tmp_table}] SET {description_column} = {description}",
7899+
f"INSERT INTO [{table}] SELECT * FROM [{tmp_table}];",
7900+
f"DROP TABLE IF EXISTS [{tmp_table}]",
78997901
]
79007902
for q in query:
79017903
res = self.execute(q)
79027904
if res.attrs["exception"]:
79037905
return res
79047906

79057907
# Now we save the new pk
7906-
pk = res.lastrowid
7908+
pk = res.attrs["lastrowid"]
79077909

79087910
# create list of which children we have duplicated
79097911
child_duplicated = []
@@ -7916,31 +7918,29 @@ def duplicate_record(self, dataset: DataSet, children: bool) -> pd.DataFrame:
79167918
and r.on_update_cascade
79177919
and (r.child_table not in child_duplicated)
79187920
):
7919-
child = self.quote_table(r.child_table)
7920-
tmp_child = self.quote_table(f"temp_{r.child_table}")
7921-
pk_column = self.quote_column(
7922-
dataset.frm[r.child_table].pk_column
7923-
)
7924-
fk_column = self.quote_column(r.fk_column)
7921+
child = r.child_table
7922+
tmp_child = f"temp_{r.child_table}"
7923+
pk_column = dataset.frm[r.child_table].pk_column
7924+
fk_column = r.fk_column
79257925

79267926
# Update children's pk_columns to NULL and set correct parent
79277927
# PK value.
79287928
queries = [
7929-
f"DROP TABLE IF EXISTS {tmp_child}",
7929+
f"DROP TABLE IF EXISTS [{tmp_child}]",
79307930
(
7931-
f"CREATE TABLE {tmp_table} "
7931+
f"CREATE TABLE [{tmp_table}] "
79327932
f"({self._get_column_definitions(table)});"
79337933
),
79347934
(
7935-
f"INSERT INTO {tmp_table} (SELECT * FROM {table} "
7935+
f"INSERT INTO [{tmp_table}] (SELECT * FROM [{table}] "
79367936
f"WHERE {pk_column}="
79377937
f"{dataset.get_current(dataset.pk_column)});"
79387938
),
79397939
# don't next_pk(), because child can be plural.
7940-
f"UPDATE {tmp_child} SET {pk_column} = NULL;",
7941-
f"UPDATE {tmp_child} SET {fk_column} = {pk}",
7942-
f"INSERT INTO {child} SELECT * FROM {tmp_child};",
7943-
f"DROP TABLE IF EXISTS {tmp_child}",
7940+
f"UPDATE [{tmp_child}] SET {pk_column} = NULL;",
7941+
f"UPDATE [{tmp_child}] SET {fk_column} = {pk}",
7942+
f"INSERT INTO [{child}] SELECT * FROM [{tmp_child}];",
7943+
f"DROP TABLE IF EXISTS [{tmp_child}]",
79447944
]
79457945
for q in queries:
79467946
res = self.execute(q)

0 commit comments

Comments
 (0)