@@ -1008,11 +1008,6 @@ def requery(
1008
1008
rows = self .driver .execute (query )
1009
1009
self .rows = rows
1010
1010
1011
- if "sort_order" not in self .rows .attrs :
1012
- # Store the sort order as a dictionary in the attrs of the DataFrame
1013
- sort_order = self .rows [self .pk_column ].to_list ()
1014
- self .rows .attrs ["sort_order" ] = {self .pk_column : sort_order }
1015
-
1016
1011
# now we can restore the sort order
1017
1012
self .load_sort_settings (sort_settings )
1018
1013
self .sort (self .table )
@@ -1548,7 +1543,7 @@ def insert_record(
1548
1543
new_values [self .pk_column ] = self .driver .next_pk (self .table , self .pk_column )
1549
1544
1550
1545
# Insert the new values using ResultSet.insert_row(), marking the new row as virtual
1551
- self .rows . insert_row (new_values )
1546
+ self .insert_row (new_values )
1552
1547
1553
1548
# and move to the new record
1554
1549
self .set_by_pk (
@@ -2177,12 +2172,13 @@ def purge_virtual(self) -> None:
2177
2172
2178
2173
:returns: None
2179
2174
"""
2180
- virtual_rows = self .rows .attrs ["virtual" ][self .rows .attrs ["virtual" ]].index
2181
- self .rows .drop (virtual_rows , inplace = True )
2182
- self .rows .attrs ["original_index" ] = self .rows .attrs ["original_index" ].drop (
2183
- virtual_rows
2184
- )
2185
- self .rows .attrs ["virtual" ] = self .rows .attrs ["virtual" ].drop (virtual_rows )
2175
+ # remove the rows where virtual is True in place, along with the corresponding
2176
+ # virtual attribute
2177
+ # remove the rows where virtual is True in place, along with the corresponding virtual attribute
2178
+ idx_to_remove = [idx for idx , v in self .rows .attrs ["virtual" ].items () if v ]
2179
+ self .rows .drop (index = idx_to_remove , inplace = True )
2180
+ for idx in idx_to_remove :
2181
+ del self .rows .attrs ["virtual" ][idx ]
2186
2182
2187
2183
def sort_by_column (self , column : str , table : str , reverse = False ) -> None :
2188
2184
"""
@@ -2273,18 +2269,12 @@ def load_sort_settings(self, sort_settings: list) -> None:
2273
2269
2274
2270
def sort_reset (self ) -> None :
2275
2271
"""
2276
- Reset the sort order to the original when this ResultSet was created. Each
2277
- ResultRow has the original order stored.
2272
+ Reset the sort order to the original order as defined by the DataFram index
2278
2273
2279
2274
:returns: None
2280
2275
"""
2281
2276
# Restore the original sort order
2282
- sort_column = list (self .rows .attrs ["sort_order" ].keys ())[0 ]
2283
- sort_order = self .rows .attrs ["sort_order" ][sort_column ]
2284
- self .rows .loc [:, sort_column ] = pd .Categorical (
2285
- self .rows [sort_column ], categories = sort_order , ordered = True
2286
- )
2287
- self .rows .sort_values (sort_column , inplace = True )
2277
+ self .rows .sort_index (inplace = True )
2288
2278
2289
2279
def sort (self , table : str ) -> None :
2290
2280
"""
@@ -2295,6 +2285,7 @@ def sort(self, table: str) -> None:
2295
2285
`ResultSet.sort_by_column()`
2296
2286
:returns: None
2297
2287
"""
2288
+ pk = self .get_current_pk ()
2298
2289
if self .rows .attrs ["sort_column" ] is None :
2299
2290
print ("Sort column is None. Resetting sort." )
2300
2291
self .sort_reset ()
@@ -2305,7 +2296,7 @@ def sort(self, table: str) -> None:
2305
2296
self .sort_by_column (
2306
2297
self .rows .attrs ["sort_column" ], table , self .rows .attrs ["sort_reverse" ]
2307
2298
)
2308
- self .rows . reset_index ( drop = True , inplace = True )
2299
+ self .set_by_pk ( pk )
2309
2300
2310
2301
def sort_cycle (self , column : str , table : str ) -> int :
2311
2302
"""
@@ -2331,6 +2322,27 @@ def sort_cycle(self, column: str, table: str) -> int:
2331
2322
self .sort (table )
2332
2323
return SORT_NONE
2333
2324
2325
+ def insert_row (self , row : dict , idx : int = None ) -> None :
2326
+ """
2327
+ Insert a new virtual row into the DataFrame. Virtual rows are ones that exist
2328
+ in memory, but not in the database. When a save action is performed, virtual
2329
+ rows will be added into the database.
2330
+
2331
+ :param row: A dict representation of a row of data
2332
+ :param idx: The index where the row should be inserted (default to last index)
2333
+ :returns: None
2334
+ """
2335
+ row_series = pd .Series (row )
2336
+ self .rows .append (row_series , ignore_index = True )
2337
+ return
2338
+ if idx is None :
2339
+ idx = len (self .rows .index )
2340
+ idx_label = self .rows .index .max () + 1 if len (self .rows .index ) > 0 else 0
2341
+ self .rows .loc [idx_label ] = row_series
2342
+ self .rows .attrs ["sort_order" ][self .pk_column ].append (row [self .pk_column ])
2343
+ self .rows_attrs ["virtual" ].loc [idx_label ] = True
2344
+ self .rows .sort_index ()
2345
+
2334
2346
2335
2347
class Form :
2336
2348
@@ -5849,10 +5861,10 @@ def default_row_dict(self, dataset: DataSet) -> dict:
5849
5861
rows = self .driver .execute (q )
5850
5862
if rows .attrs ["exception" ] is None :
5851
5863
try :
5852
- default = rows .fetchone () ["val" ]
5864
+ default = rows .iloc [ 0 ] ["val" ]
5853
5865
except KeyError :
5854
5866
try :
5855
- default = rows .fetchone () ["VAL" ]
5867
+ default = rows .iloc [ 0 ] ["VAL" ]
5856
5868
except KeyError :
5857
5869
default = ""
5858
5870
d [c .name ] = default
@@ -6499,11 +6511,11 @@ def relationship_to_join_clause(self, r_obj: Relationship):
6499
6511
6500
6512
def min_pk (self , table : str , pk_column : str ) -> int :
6501
6513
rows = self .execute (f"SELECT MIN({ pk_column } ) as min_pk FROM { table } " )
6502
- return rows .fetchone () ["min_pk" ]
6514
+ return rows .iloc [ 0 ] ["min_pk" ]
6503
6515
6504
6516
def max_pk (self , table : str , pk_column : str ) -> int :
6505
6517
rows = self .execute (f"SELECT MAX({ pk_column } ) as max_pk FROM { table } " )
6506
- return rows .fetchone () ["max_pk" ]
6518
+ return rows .iloc [ 0 ] ["max_pk" ]
6507
6519
6508
6520
def generate_join_clause (self , dataset : DataSet ) -> str :
6509
6521
"""
0 commit comments