Skip to content

Commit 76b4835

Browse files
committed
refs #281, Pandas partially working
Pretty big step backwards with this one. Working on cleaning up the sorting code had some unexpected consequences. I'm out of time for a while, so checking this in for now
1 parent 0a77f5e commit 76b4835

File tree

1 file changed

+37
-25
lines changed

1 file changed

+37
-25
lines changed

pysimplesql/pysimplesql.py

+37-25
Original file line numberDiff line numberDiff line change
@@ -1008,11 +1008,6 @@ def requery(
10081008
rows = self.driver.execute(query)
10091009
self.rows = rows
10101010

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-
10161011
# now we can restore the sort order
10171012
self.load_sort_settings(sort_settings)
10181013
self.sort(self.table)
@@ -1548,7 +1543,7 @@ def insert_record(
15481543
new_values[self.pk_column] = self.driver.next_pk(self.table, self.pk_column)
15491544

15501545
# 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)
15521547

15531548
# and move to the new record
15541549
self.set_by_pk(
@@ -2177,12 +2172,13 @@ def purge_virtual(self) -> None:
21772172
21782173
:returns: None
21792174
"""
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]
21862182

21872183
def sort_by_column(self, column: str, table: str, reverse=False) -> None:
21882184
"""
@@ -2273,18 +2269,12 @@ def load_sort_settings(self, sort_settings: list) -> None:
22732269

22742270
def sort_reset(self) -> None:
22752271
"""
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
22782273
22792274
:returns: None
22802275
"""
22812276
# 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)
22882278

22892279
def sort(self, table: str) -> None:
22902280
"""
@@ -2295,6 +2285,7 @@ def sort(self, table: str) -> None:
22952285
`ResultSet.sort_by_column()`
22962286
:returns: None
22972287
"""
2288+
pk = self.get_current_pk()
22982289
if self.rows.attrs["sort_column"] is None:
22992290
print("Sort column is None. Resetting sort.")
23002291
self.sort_reset()
@@ -2305,7 +2296,7 @@ def sort(self, table: str) -> None:
23052296
self.sort_by_column(
23062297
self.rows.attrs["sort_column"], table, self.rows.attrs["sort_reverse"]
23072298
)
2308-
self.rows.reset_index(drop=True, inplace=True)
2299+
self.set_by_pk(pk)
23092300

23102301
def sort_cycle(self, column: str, table: str) -> int:
23112302
"""
@@ -2331,6 +2322,27 @@ def sort_cycle(self, column: str, table: str) -> int:
23312322
self.sort(table)
23322323
return SORT_NONE
23332324

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+
23342346

23352347
class Form:
23362348

@@ -5849,10 +5861,10 @@ def default_row_dict(self, dataset: DataSet) -> dict:
58495861
rows = self.driver.execute(q)
58505862
if rows.attrs["exception"] is None:
58515863
try:
5852-
default = rows.fetchone()["val"]
5864+
default = rows.iloc[0]["val"]
58535865
except KeyError:
58545866
try:
5855-
default = rows.fetchone()["VAL"]
5867+
default = rows.iloc[0]["VAL"]
58565868
except KeyError:
58575869
default = ""
58585870
d[c.name] = default
@@ -6499,11 +6511,11 @@ def relationship_to_join_clause(self, r_obj: Relationship):
64996511

65006512
def min_pk(self, table: str, pk_column: str) -> int:
65016513
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"]
65036515

65046516
def max_pk(self, table: str, pk_column: str) -> int:
65056517
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"]
65076519

65086520
def generate_join_clause(self, dataset: DataSet) -> str:
65096521
"""

0 commit comments

Comments
 (0)