@@ -1019,7 +1019,7 @@ def requery(
1019
1019
rows = self .driver .execute (query )
1020
1020
self .rows = rows
1021
1021
1022
- if len (self .rows .index ):
1022
+ if len (self .rows .index ) and self . pk_column is not None :
1023
1023
if "sort_order" not in self .rows .attrs :
1024
1024
# Store the sort order as a dictionary in the attrs of the DataFrame
1025
1025
sort_order = self .rows [self .pk_column ].to_list ()
@@ -7758,25 +7758,31 @@ def execute(
7758
7758
row [column_name ] = value
7759
7759
rows .append (row )
7760
7760
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 )
7762
7768
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 )
7765
7771
7766
7772
def column_info (self , table ):
7767
7773
meta_data = self .con .getMetaData ()
7768
7774
rs = meta_data .getColumns (None , None , table , None )
7769
- print ( "Getting column info..." )
7775
+
7770
7776
col_info = ColumnInfo (self , table )
7771
7777
pk_columns = [self .pk_column (table )]
7772
- print ( "pk_columns" , pk_columns )
7778
+
7773
7779
while rs .next ():
7774
7780
name = str (rs .getString ("column_name" ))
7775
7781
domain = str (rs .getString ("TYPE_NAME" )).upper ()
7776
7782
notnull = str (rs .getString ("IS_NULLABLE" )) == "NO"
7777
7783
default = str (rs .getString ("COLUMN_DEF" ))
7778
7784
pk = name in pk_columns
7779
- print ( "name" , name )
7785
+
7780
7786
col_info .append (
7781
7787
Column (
7782
7788
name = name , domain = domain , notnull = notnull , default = default , pk = pk
@@ -7786,7 +7792,6 @@ def column_info(self, table):
7786
7792
return col_info
7787
7793
7788
7794
def pk_column (self , table ):
7789
- print (f"Getting pk_column for { table } " )
7790
7795
meta_data = self .con .getMetaData ()
7791
7796
rs = meta_data .getPrimaryKeys (None , None , table )
7792
7797
if rs .next ():
@@ -7859,12 +7864,12 @@ def _get_column_definitions(self, table_name):
7859
7864
# Creates a comma separated list of column names and types to be used in a
7860
7865
# CREATE TABLE statement
7861
7866
columns = self .column_info (table_name )
7862
- print ( "columns" , columns )
7867
+
7863
7868
cols = ""
7864
7869
for c in columns :
7865
7870
cols += f"{ c ['name' ]} { c ['domain' ]} , "
7866
7871
cols = cols [:- 2 ]
7867
- print ( " \n cols \n " , cols )
7872
+
7868
7873
return cols
7869
7874
7870
7875
def duplicate_record (self , dataset : DataSet , children : bool ) -> pd .DataFrame :
@@ -7883,9 +7888,11 @@ def duplicate_record(self, dataset: DataSet, children: bool) -> pd.DataFrame:
7883
7888
description_column = dataset .description_column
7884
7889
7885
7890
# 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 += [
7889
7896
f"CREATE TABLE [{ tmp_table } ] ({ self ._get_column_definitions (table )} );" ,
7890
7897
(
7891
7898
f"INSERT INTO [{ tmp_table } ] (SELECT * FROM [{ table } ] "
@@ -7897,15 +7904,16 @@ def duplicate_record(self, dataset: DataSet, children: bool) -> pd.DataFrame:
7897
7904
),
7898
7905
f"UPDATE [{ tmp_table } ] SET { description_column } = { description } " ,
7899
7906
f"INSERT INTO [{ table } ] SELECT * FROM [{ tmp_table } ];" ,
7900
- f"DROP TABLE IF EXISTS [{ tmp_table } ]" ,
7907
+ f"DROP TABLE [{ tmp_table } ]" ,
7901
7908
]
7902
7909
for q in query :
7903
- res = self .execute (q )
7910
+ res = self .execute (q , auto_commit_rollback = True )
7904
7911
if res .attrs ["exception" ]:
7905
7912
return res
7906
7913
7907
7914
# 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" ]
7909
7917
7910
7918
# create list of which children we have duplicated
7911
7919
child_duplicated = []
@@ -7925,8 +7933,11 @@ def duplicate_record(self, dataset: DataSet, children: bool) -> pd.DataFrame:
7925
7933
7926
7934
# Update children's pk_columns to NULL and set correct parent
7927
7935
# 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 += [
7930
7941
(
7931
7942
f"CREATE TABLE [{ tmp_table } ] "
7932
7943
f"({ self ._get_column_definitions (table )} );"
@@ -7940,7 +7951,7 @@ def duplicate_record(self, dataset: DataSet, children: bool) -> pd.DataFrame:
7940
7951
f"UPDATE [{ tmp_child } ] SET { pk_column } = NULL;" ,
7941
7952
f"UPDATE [{ tmp_child } ] SET { fk_column } = { pk } " ,
7942
7953
f"INSERT INTO [{ child } ] SELECT * FROM [{ tmp_child } ];" ,
7943
- f"DROP TABLE IF EXISTS [{ tmp_child } ]" ,
7954
+ f"DROP TABLE [{ tmp_child } ]" ,
7944
7955
]
7945
7956
for q in queries :
7946
7957
res = self .execute (q )
@@ -7950,7 +7961,7 @@ def duplicate_record(self, dataset: DataSet, children: bool) -> pd.DataFrame:
7950
7961
child_duplicated .append (r .child_table )
7951
7962
# If we made it here, we can return the pk. Since the pk was stored earlier,
7952
7963
# we will just send and empty DataFrame
7953
- return Result .set (lastrowid = pk )
7964
+ return Result .set (lastrowid = lastrowid )
7954
7965
7955
7966
def insert_record (self , table : str , pk : int , pk_column : str , row : dict ):
7956
7967
# Remove the pk column
0 commit comments