More refactor of junction

This commit is contained in:
wqy
2022-09-17 00:20:11 +08:00
parent 732990237b
commit 7e34c0bd86
2 changed files with 18 additions and 15 deletions

View File

@@ -1,10 +1,9 @@
from psycopg.rows import dict_row, Row from psycopg.rows import Row
from .connection import g_conn_dict as conn
from .s0_base import * from .s0_base import *
from .operation import * from .operation import *
from .change_set import ChangeSet from .change_set import ChangeSet
from .s24_coordinates import * from .s24_coordinates import *
from .utility import * import utility
def add_junction(name: str, id: str, x: float, y: float, elevation: float) -> ChangeSet: def add_junction(name: str, id: str, x: float, y: float, elevation: float) -> ChangeSet:
@@ -14,7 +13,7 @@ def add_junction(name: str, id: str, x: float, y: float, elevation: float) -> Ch
def _get_junction(name: str, id: str) -> Row | None: def _get_junction(name: str, id: str) -> Row | None:
return query(f"select elevation, demand, pattern from junctions where id = '{id}'") return utility.query(name, f"select elevation, demand, pattern from junctions where id = '{id}'")
def delete_junction(name: str, id: str) -> ChangeSet: def delete_junction(name: str, id: str) -> ChangeSet:
@@ -69,15 +68,7 @@ def _set_junction(name: str, id: str, key: str, key_type: str, value: str, optio
if row == None: if row == None:
return return
old = decorate(row[key]) return utility.update(name, JUNCTION, 'junctions', 'id', id, key, key_type, row[key], value, optional)
sql = f"update junctions set {key} = {value} where id = '{id}'"
undo = f'update junctions set {key} = {old} where id = "{id}"'
update(name, sql, undo)
change = ChangeSet()
change.update('junction', id, key, key_type, value)
return change
def set_junction_elevation(name: str, id: str, elevation: float) -> ChangeSet: def set_junction_elevation(name: str, id: str, elevation: float) -> ChangeSet:

View File

@@ -1,12 +1,15 @@
from psycopg.rows import dict_row, Row from psycopg.rows import dict_row, Row
from .connection import g_conn_dict as conn from .connection import g_conn_dict as conn
from .operation import * from .operation import *
from .change_set import ChangeSet
def query(name: str, sql: str) -> Row | None: def query(name: str, sql: str) -> Row | None:
with conn[name].cursor(row_factory=dict_row) as cur: with conn[name].cursor(row_factory=dict_row) as cur:
cur.execute(sql) cur.execute(sql)
return cur.fetchone() return cur.fetchone()
def decorate(value: str | None, type: str, optional: bool) -> str: def decorate(value: str | None, type: str, optional: bool) -> str:
if optional: if optional:
value = 'NULL' if value == None else value value = 'NULL' if value == None else value
@@ -14,8 +17,17 @@ def decorate(value: str | None, type: str, optional: bool) -> str:
value = f'"{value}"' if value != 'NULL' else value value = f'"{value}"' if value != 'NULL' else value
return value return value
def update(name: str, sql: str, undo_sql: str):
def update(name: str, type: str, table: str, id_key: str, id_value: str, key: str, key_type: str, raw_old_value: str, value: str, optional: bool = False) -> ChangeSet:
old = decorate(raw_old_value, key_type, optional)
with conn[name].cursor() as cur: with conn[name].cursor() as cur:
sql = f"update {table} set {key} = {value} where {id_key} = '{id_value}'"
cur.execute(sql) cur.execute(sql)
redo = sql.replace("'", '"') redo = sql.replace("'", '"')
add_operation(name, redo, undo_sql) undo = f'update {table} set {key} = {old} where {id_key} = "{id_value}"'
add_operation(name, redo, undo)
change = ChangeSet()
change.update(type, id, key, key_type, value)
return change