diff --git a/api/__init__.py b/api/__init__.py new file mode 100644 index 0000000..408e40e --- /dev/null +++ b/api/__init__.py @@ -0,0 +1,23 @@ +from .project import have_project, create_project, delete_project +from .project import is_project_open, open_project, close_project +from .project import copy_project + +from .change_set import ChangeSet + +from .operation import execute_undo as undo +from .operation import execute_redo as redo +from .operation import have_snapshot, take_snapshot, pick_snapshot +from .operation import have_transaction, start_transaction, commit_transaction, abort_transaction + +from .s0_base import JUNCTION, RESERVOIR, TANK, PIPE, PUMP, VALVE +from .s0_base import is_node, is_junction, is_reservoir, is_tank +from .s0_base import is_link, is_pipe, is_pump, is_valve +from .s0_base import is_curve +from .s0_base import is_pattern +from .s0_base import get_nodes, get_links, get_curves, get_patterns + +from .s1_title import set_title, get_title + +from .s2_junctions import add_junction, delete_junction +from .s2_junctions import get_junction_elevation, get_junction_demand, get_junction_pattern, get_junction_coord +from .s2_junctions import set_junction_elevation, set_junction_demand, set_junction_pattern, set_junction_coord \ No newline at end of file diff --git a/api/change_set.py b/api/change_set.py new file mode 100644 index 0000000..efedeff --- /dev/null +++ b/api/change_set.py @@ -0,0 +1,14 @@ +class ChangeSet: + def __init__(self) -> None: + self.added : list[dict[str, str]] = {} + self.deleted : list[dict[str, str]] = {} + self.updated : list[dict[str, str]] = {} + + def add(self, type: str, id: str) -> None: + self.added.append({ 'type': type, 'id': id }) + + def delete(self, type: str, id: str) -> None: + self.deleted.append({ 'type': type, 'id': id }) + + def update(self, type: str, id: str, property: str) -> None: + self.updated.append({ 'type': type, 'id': id, 'property': property }) \ No newline at end of file diff --git a/api/connection.py b/api/connection.py new file mode 100644 index 0000000..b42b481 --- /dev/null +++ b/api/connection.py @@ -0,0 +1,3 @@ +import psycopg as pg + +g_conn_dict : dict[str, pg.Connection] = {} \ No newline at end of file diff --git a/api/operation.py b/api/operation.py new file mode 100644 index 0000000..ac457f3 --- /dev/null +++ b/api/operation.py @@ -0,0 +1,220 @@ +from psycopg.rows import dict_row, Row +from .connection import g_conn_dict as conn + +def _get_current_transaction(name: str) -> Row | None: + with conn[name].cursor(row_factory=dict_row) as cur: + cur.execute(f"select * from transaction_operation") + return cur.fetchone() + +def _get_current_transaction_id(name: str) -> int: + row = _get_current_transaction(name) + return int(row['id']) + +def _remove_transaction(name: str) -> None: + with conn[name].cursor() as cur: + cur.execute(f"delete from transaction_operation") + +def _remove_operation(name: str, id: int) -> None: + with conn[name].cursor(row_factory=dict_row) as cur: + # can not be >= to cascade delete since there is a tree ! + cur.execute(f"delete from transaction_operation where id = {id}") # this should not happen + cur.execute(f"delete from snapshot_operation where id = {id}") # this may happen + cur.execute(f"delete from operation where id = {id}") + +def _get_parents(name: str, id: int) -> list[int]: + ids = [id] + with conn[name].cursor(row_factory=dict_row) as cur: + while ids[-1] != 0: + cur.execute(f"select parent from operation where id = {ids[-1]}") + ids.append(int(cur.fetchone()['parent'])) + return ids + +def _get_current_operation(name: str) -> int: + with conn[name].cursor(row_factory=dict_row) as cur: + cur.execute(f"select id from current_operation") + return int(cur.fetchone()['id']) + +def _update_current_operation(name: str, old_id: int, id: int) -> None: + with conn[name].cursor() as cur: + cur.execute(f"update current_operation set id = {id} where id = {old_id}") + +def _add_redo_undo(name: str, redo: str, undo: str) -> int: + with conn[name].cursor(row_factory=dict_row) as cur: + parent = _get_current_operation(name) + cur.execute(f"insert into operation (id, redo, undo, parent) values (default, '{redo}', '{undo}', {parent})") + cur.execute("select max(id) from operation") + return int(cur.fetchone()['max']) + +# execute curr undo +def _query_undo(name: str, id: str) -> dict[str, str]: + with conn[name].cursor(row_factory=dict_row) as cur: + cur.execute(f"select undo, parent from operation where id = {id}") + return cur.fetchone() + +# execute next redo +def _query_redo_child(name: str, id: str) -> str: + with conn[name].cursor(row_factory=dict_row) as cur: + cur.execute(f"select redo_child from operation where id = {id}") + return cur.fetchone()['redo_child'] + +def _query_redo(name: str, id: str) -> dict[str, str]: + with conn[name].cursor(row_factory=dict_row) as cur: + cur.execute(f"select redo from operation where id = {id}") + return cur.fetchone()['redo'] + +def _set_redo_child(name: str, id: int, child: int | str) -> None: + with conn[name].cursor() as cur: + cur.execute(f"update operation set redo_child = {child} where id = {id}") + +def _execute(name: str, sql: str) -> None: + with conn[name].cursor() as cur: + sql = sql.replace("\"", "\'") + cur.execute(sql) + +def add_operation(name: str, redo: str, undo: str) -> None: + curr = _add_redo_undo(name, redo, undo) + old = _get_current_operation(name) + _update_current_operation(name, old, curr) + +def execute_undo(name: str, discard: bool = False) -> None: + curr = _get_current_operation(name) + + # transaction control + if have_transaction(name): + tran = _get_current_transaction(name) + if tran != None and int(tran['id']) >= 0: + if bool(tran['strict']): # strict mode disallow undo + print("Do not allow to undo in strict transaction mode!") + return + elif int(tran['id']) >= curr: # normal mode disallow undo start point, and there is foreign key constraint + print("Do not allow to undo transaction start point!") + return + + row = _query_undo(name, curr) + undo = row['undo'] + if undo == '': + print("nothing to undo!") + return + + parent = int(row['parent']) + _set_redo_child(name, parent, 'NULL' if discard else curr) + + _execute(name, undo) + _update_current_operation(name, curr, parent) + + if discard: + _remove_operation(name, curr) + +def execute_redo(name: str) -> None: + curr = _get_current_operation(name) + redoChild = _query_redo_child(name, curr) + if redoChild == None: + print("nothing to redo!") + return + + child = int(redoChild) + redo = _query_redo(name, child) + + _execute(name, redo) + _update_current_operation(name, curr, child) + +# snapshot support to checkout between different version of database +# snapshot is persistent +# since redo always remember the recently undo path + +def have_snapshot(name: str, tag: str) -> bool: + with conn[name].cursor(row_factory=dict_row) as cur: + cur.execute(f"select id from snapshot_operation where tag = '{tag}'") + return cur.rowcount > 0 + +def take_snapshot(name: str, tag: str) -> None: + if tag == None or tag == '': + print('Non empty tag is expected!') + return + + curr = _get_current_operation(name) + + with conn[name].cursor() as cur: + cur.execute(f"insert into snapshot_operation (id, tag) values ({curr}, '{tag}')") + +def pick_snapshot(name: str, tag: str) -> None: + if tag == None or tag == '': + print('Non empty tag is expected!') + return + + if not have_snapshot(name, tag): + print('No such snapshot!') + return + + curr = _get_current_operation(name) + curr_parents = _get_parents(name, curr) + + with conn[name].cursor(row_factory=dict_row) as cur: + cur.execute(f"select id from snapshot_operation where tag = '{tag}'") + target = int(cur.fetchone()['id']) + if target in curr_parents: # target -> curr + for i in range(curr_parents.index(target)): + execute_undo(name) + else: + target_parents = _get_parents(name, target) + if curr in target_parents: # curr -> target + for i in range(target_parents.index(curr)): + execute_redo(name) + else: + ancestor_index = -1 + while curr_parents[ancestor_index] == target_parents[ancestor_index]: + ancestor_index -= 1 + + # ancestor -> curr + ancestor = curr_parents[ancestor_index + 1] # ancestor_index + 1 is common parent + for i in range(curr_parents.index(ancestor)): + execute_undo(name) + # ancestor -> redo, need assign redo_child + while target_parents[ancestor_index] != target: + cur.execute(f"update operation set redo_child = '{target_parents[ancestor_index]}' where id = '{target_parents[ancestor_index + 1]}'") + execute_redo(name) + ancestor_index -= 1 + cur.execute(f"update operation set redo_child = '{target}' where id = '{target_parents[1]}'") + execute_redo(name) + +# transaction is volatile, commit/abort will destroy transaction. +# can not redo an aborted transaction. +# but can undo a committed transaction... inconsistent... +# it may remove snapshot if it is in aborted transaction + +def have_transaction(name: str) -> bool: + with conn[name].cursor(row_factory=dict_row) as cur: + cur.execute(f"select * from transaction_operation") + return cur.rowcount > 0 + +def start_transaction(name: str, strict: bool = False) -> None: + if have_transaction(name): + print("Only support single transaction now, please commit/abort current transaction!") + return + + curr = _get_current_operation(name) + + with conn[name].cursor() as cur: + cur.execute(f"insert into transaction_operation (id, strict) values ({curr}, {strict});") + +def commit_transaction(name: str) -> None: + if not have_transaction(name): + print("No active transaction!") + return + + _remove_transaction(name) + +def abort_transaction(name: str) -> None: + if not have_transaction(name): + print("No active transaction!") + return + + tran = _get_current_transaction_id(name) + + curr = _get_current_operation(name) + curr_parents = _get_parents(name, curr) + + for i in range(curr_parents.index(tran)): + execute_undo(name, True) + + _remove_transaction(name) diff --git a/api/project.py b/api/project.py new file mode 100644 index 0000000..f283c85 --- /dev/null +++ b/api/project.py @@ -0,0 +1,33 @@ +import psycopg as pg +from .connection import g_conn_dict as conn + +# no undo/redo + +def have_project(name: str) -> bool: + with pg.connect(conninfo="dbname=postgres", autocommit=True) as conn: + with conn.cursor() as cur: + cur.execute(f"select * from pg_database where datname = '{name}'") + return cur.rowcount > 0 + +def copy_project(source: str, new: str) -> None: + with pg.connect(conninfo="dbname=postgres", autocommit=True) as conn: + with conn.cursor() as cur: + cur.execute(f"create database {new} with template = {source}") + +def create_project(name: str) -> None: + return copy_project('project', name) + +def delete_project(name: str) -> None: + with pg.connect(conninfo="dbname=postgres", autocommit=True) as conn: + with conn.cursor() as cur: + cur.execute(f"drop database {name}") + +def open_project(name: str) -> None: + conn[name] = pg.connect(conninfo=f"dbname={name}", autocommit=True) + +def is_project_open(name: str) -> bool: + return name in conn + +def close_project(name: str) -> None: + conn[name].close() + del conn[name] diff --git a/api/s0_base.py b/api/s0_base.py new file mode 100644 index 0000000..87350a7 --- /dev/null +++ b/api/s0_base.py @@ -0,0 +1,75 @@ +from psycopg.rows import dict_row, Row +from .connection import g_conn_dict as conn + +_NODE = "_node" +_LINK = "_link" +_CURVE = "_curve" +_PATTERN = "_pattern" + +JUNCTION = "JUNCTION" +RESERVOIR = "RESERVOIR" +TANK = "TANK" +PIPE = "PIPE" +PUMP = "PUMP" +VALVE = "VALVE" + +def _get_from(name: str, id: str, base_type: str) -> Row | None: + with conn[name].cursor(row_factory=dict_row) as cur: + cur.execute(f"select * from {base_type} where id = '{id}'") + return cur.fetchone() + +def is_node(name: str, id: str) -> bool: + return _get_from(name, id, _NODE) != None + +def is_junction(name: str, id: str) -> bool: + row = _get_from(name, id, _NODE) + return row != None and row['type'] == JUNCTION + +def is_reservoir(name: str, id: str) -> bool: + row = _get_from(name, id, _NODE) + return row != None and row['type'] == RESERVOIR + +def is_tank(name: str, id: str) -> bool: + row = _get_from(name, id, _NODE) + return row != None and row['type'] == TANK + +def is_link(name: str, id: str) -> bool: + return _get_from(name, id, _LINK) != {} + +def is_pipe(name: str, id: str) -> bool: + row = _get_from(name, id, _LINK) + return row != None and row['type'] == PIPE + +def is_pump(name: str, id: str) -> bool: + row = _get_from(name, id, _LINK) + return row != None and row['type'] == PUMP + +def is_valve(name: str, id: str) -> bool: + row = _get_from(name, id, _LINK) + return row != None and row['type'] == VALVE + +def is_curve(name: str, id: str) -> bool: + return _get_from(name, id, _CURVE) != None + +def is_pattern(name: str, id: str) -> bool: + return _get_from(name, id, _PATTERN) != None + +def _get_all(name: str, base_type: str) -> list[str]: + ids : list[str] = [] + with conn[name].cursor(row_factory=dict_row) as cur: + cur.execute(f"select id from {base_type} order by id") + for record in cur: + ids.append(record['id']) + return ids + +def get_nodes(name: str) -> list[str]: + return _get_all(name, _NODE) + +def get_links(name: str) -> list[str]: + return _get_all(name, _LINK) + +def get_curves(name: str) -> list[str]: + return _get_all(name, _CURVE) + +def get_patterns(name: str) -> list[str]: + return _get_all(name, _PATTERN) diff --git a/api/s1_title.py b/api/s1_title.py new file mode 100644 index 0000000..3d0e3a3 --- /dev/null +++ b/api/s1_title.py @@ -0,0 +1,24 @@ +from psycopg.rows import dict_row +from .operation import * +from .connection import g_conn_dict as conn +from .change_set import ChangeSet + +def get_title(name: str) -> str: + with conn[name].cursor(row_factory=dict_row) as cur: + cur.execute(f"select * from title") + return cur.fetchone()['value'] + +def set_title(name: str, value: str) -> ChangeSet: + old = get_title(name) + + with conn[name].cursor() as cur: + sql = f"update title set value = '{value}'" + cur.execute(sql) + + redo = sql.replace("'", '"') + undo = f'update title set value = "{old}"' + add_operation(name, redo, undo) + + change = ChangeSet() + change.update('title', 'null', 'value') + return change diff --git a/api/s2_junctions.py b/api/s2_junctions.py new file mode 100644 index 0000000..0c77405 --- /dev/null +++ b/api/s2_junctions.py @@ -0,0 +1,188 @@ +from psycopg.rows import dict_row, Row +from .connection import g_conn_dict as conn +from .s0_base import * +from .operation import * +from .change_set import ChangeSet + +def add_junction(name: str, id: str, x: float, y: float, elevation: float) -> ChangeSet: + if is_node(name, id): + return + + with conn[name].cursor() as cur: + sql = f"insert into _node (id, type) values ('{id}', 'JUNCTION');" + sql += f" insert into junctions (id, elevation) values ('{id}', {elevation});" + sql += f" insert into coordinates (node, coord) values ('{id}', '({x}, {y})');" + cur.execute(sql) + + redo = sql.replace("'", '"') + undo = f'delete from coordinates where node = "{id}";' + undo += f' delete from junctions where id = "{id}";' + undo += f' delete from _node where id = "{id}";' + add_operation(name, redo, undo) + + change = ChangeSet() + change.add('junction', id) + return change + + +def delete_junction(name: str, id: str) -> ChangeSet: + if not is_junction(name, id): + return + + with conn[name].cursor(row_factory=dict_row) as cur: + cur.execute(f"select * from junctions where id = '{id}'") + row = cur.fetchone() + if row == None: + return + + elevation = row['elevation'] + demand = 'NULL' if row['demand'] == None else row['demand'] + pattern = 'NULL' if row['pattern'] == None else row['pattern'] + pattern = f'"{pattern}"' if pattern != 'NULL' else pattern + + cur.execute(f"select * from coordinates where id = '{id}'") + row = cur.fetchone() + if row == None: + return + + coord = row['coord'] + + sql = f"delete from coordinates where node = '{id}';" + sql += f" delete from junctions where id = '{id}';" + sql += f" delete from _node where id = '{id}';" + cur.execute(sql) + + redo = sql.replace("'", '"') + undo = f'insert into _node (id, type) values ("{id}", "{type}");' + undo += f" insert into junctions (id, elevation, demand, pattern) values ('{id}', {elevation}, {demand}, {pattern});" + undo += f" insert into coordinates (node, coord) values ('{id}', '{coord}');" + add_operation(name, redo, undo) + + change = ChangeSet() + change.delete('junction', id) + return change + + +def _get_junction(name: str, id: str) -> Row | None: + with conn[name].cursor(row_factory=dict_row) as cur: + cur.execute(f"select elevation, demand, pattern from junctions where id = '{id}'") + return cur.fetchone() + +def get_junction_elevation(name: str, id: str) -> float | None: + row = _get_junction(name, id) + return float(row['elevation']) if row != None else None + +def get_junction_demand(name: str, id: str) -> float | str | None: + row = _get_junction(name, id) + if row != None: + return float(row['demand']) if row['demand'] != None else 'NULL' + else: + return None + +def get_junction_pattern(name: str, id: str) -> str | None: + row = _get_junction(name, id) + if row != None: + return row['pattern'] if row['pattern'] != None else 'NULL' + else: + return None + +def _to_point(coord: str) -> dict[str, float]: + coord = coord.removeprefix('(') + coord = coord.removesuffix(')') + coord = coord.split(',') + return { 'x': float(coord[0]), 'y': float(coord[1]) } + +def get_junction_coord(name: str, id: str) -> dict[str, float] | None: + with conn[name].cursor(row_factory=dict_row) as cur: + cur.execute(f"select * from coordinates where node = '{id}'") + row = cur.fetchone() + if row == None: + return None + + coord = str(row['coord']) + return _to_point(coord) + +def set_junction_elevation(name: str, id: str, elevation: float) -> ChangeSet: + if not is_junction(name, id): + return + + old = get_junction_elevation(name, id) + if old == None: + return + + with conn[name].cursor() as cur: + sql = f"update junctions set elevation = {elevation} where id = '{id}'" + cur.execute(sql) + redo = sql.replace("'", '"') + undo = f'update junctions set elevation = {old} where id = "{id}"' + add_operation(name, redo, undo) + + change = ChangeSet() + change.update('junction', id, 'elevation') + return change + + +def set_junction_demand(name: str, id: str, demand: float) -> ChangeSet: + if not is_junction(name, id): + return + + old = get_junction_demand(name, id) + if old == None: + return + + with conn[name].cursor() as cur: + sql = f"update junctions set demand = {demand} where id = '{id}'" + cur.execute(sql) + redo = sql.replace("'", '"') + undo = f'update junctions set demand = {old} where id = "{id}"' + add_operation(name, redo, undo) + + change = ChangeSet() + change.update('junction', id, 'demand') + return change + + +def set_junction_pattern(name: str, id: str, pattern: str) -> ChangeSet: + if not is_junction(name, id): + return + if not is_pattern(name, id): + return + + old = get_junction_pattern(name, id) + if old == None: + return + + old = f'"{old}"' if old != 'NULL' else old + + with conn[name].cursor() as cur: + sql = f"update junctions set pattern = '{pattern}' where id = '{id}'" + cur.execute(sql) + redo = sql.replace("'", '"') + undo = f'update junctions set pattern = {old} where id = "{id}"' + add_operation(name, redo, undo) + + change = ChangeSet() + change.update('junction', id, 'pattern') + return change + + +def set_junction_coord(name: str, id: str, x: float, y: float) -> ChangeSet: + if not is_junction(name, id): + return + + old = get_junction_coord(name, id) + if old == None: + return + old_x, old_y = old['x'], old['y'] + + with conn[name].cursor() as cur: + sql = f"update coordinates set coord = '({x},{y})' where node = '{id}'" + cur.execute(sql) + + redo = sql.replace("'", '"') + undo = f'update coordinates set coord = "({old_x},{old_y})" where node = "{id}"' + add_operation(name, redo, undo) + + change = ChangeSet() + change.update('junction', id, 'coord') + return change diff --git a/new_demo.py b/new_demo.py new file mode 100644 index 0000000..02f25d8 --- /dev/null +++ b/new_demo.py @@ -0,0 +1,184 @@ +from tjnetwork_new import * + + +def demo_snapshot(): + p = "demo_snapshot" + + print(p) + + if is_project_open(p): + close_project(p) + + if have_project(p): + delete_project(p) + + create_project(p) + open_project(p) + + add_junction(p, 'j-1', 10.0, 20.0, 30.0) + add_junction(p, 'j-2', 10.0, 20.0, 30.0) + add_junction(p, 'j-3', 10.0, 20.0, 30.0) + add_junction(p, 'j-4', 10.0, 20.0, 30.0) + take_snapshot(p, "1-2-3-4") + + undo(p) + undo(p) + undo(p) + undo(p) + + add_junction(p, 'j-5', 10.0, 20.0, 30.0) + add_junction(p, 'j-6', 10.0, 20.0, 30.0) + add_junction(p, 'j-7', 10.0, 20.0, 30.0) + add_junction(p, 'j-8', 10.0, 20.0, 30.0) + take_snapshot(p, "5-6-7-8") + + print("before checkout, it should be 5, 6, 7, 8") + print(get_nodes(p)) + + pick_snapshot(p, "1-2-3-4") + + print("after checkout, it should be 1, 2, 3, 4") + print(get_nodes(p)) + + close_project(p) + # delete_project(p) + + +def demo_transaction(): + p = "demo_transaction" + + print(p) + + if is_project_open(p): + close_project(p) + + if have_project(p): + delete_project(p) + + create_project(p) + open_project(p) + + add_junction(p, 'j-1', 10.0, 20.0, 30.0) + take_snapshot(p, "1") + add_junction(p, 'j-2', 10.0, 20.0, 30.0) + take_snapshot(p, "2") + + start_transaction(p) + + add_junction(p, 'j-3', 10.0, 20.0, 30.0) + take_snapshot(p, "3") + add_junction(p, 'j-4', 10.0, 20.0, 30.0) + take_snapshot(p, "4") + + print("before rollback, it should be 1, 2, 3, 4") + print(get_nodes(p)) + + print("after rollback, it should be 1, 2") + abort_transaction(p) + + print(get_nodes(p)) + + print(f"have snapshot 1: {have_snapshot(p, '1')}") + print(f"have snapshot 2: {have_snapshot(p, '2')}") + print(f"have snapshot 3: {have_snapshot(p, '3')}") + print(f"have snapshot 4: {have_snapshot(p, '4')}") + + close_project(p) + # delete_project(p) + + +def demo_1_title(): + p = "demo_1_title" + + print(p) + + if is_project_open(p): + close_project(p) + + if have_project(p): + delete_project(p) + + create_project(p) + open_project(p) + + print(get_title(p)) # + + set_title(p, "title") + print(get_title(p)) # title + + set_title(p, "test") + print(get_title(p)) # test + + undo(p) + print(get_title(p)) # title + + undo(p) + print(get_title(p)) # + + close_project(p) + # delete_project(p) + + +def demo_2_junctions(): + p = "demo_2_junctions" + + print(p) + + if is_project_open(p): + close_project(p) + + if have_project(p): + delete_project(p) + + create_project(p) + open_project(p) + + j = 'j-1' + print(get_junction_coord(p, j)) # None + print(get_junction_elevation(p, j)) # None + print(get_junction_demand(p, j)) # None + print(get_junction_pattern(p, j)) # None + + add_junction(p, j, 10.0, 20.0, 30.0) + print(get_junction_coord(p, j)) # {'x': 10.0, 'y': 20.0} + print(get_junction_elevation(p, j)) # 30.0 + print(get_junction_demand(p, j)) # NULL + print(get_junction_pattern(p, j)) # NULL + + set_junction_demand(p, j, 100.0) + print(get_junction_demand(p, j)) # 100.0 + + undo(p) + print(get_junction_demand(p, j)) # NULL + + undo(p) + print(get_junction_coord(p, j)) # None + print(get_junction_elevation(p, j)) # None + print(get_junction_demand(p, j)) # None + print(get_junction_pattern(p, j)) # None + + add_junction(p, j, 10.0, 20.0, 30.0) + print(get_junction_coord(p, j)) # {'x': 10.0, 'y': 20.0} + print(get_junction_elevation(p, j)) # 30.0 + print(get_junction_demand(p, j)) # NULL + print(get_junction_pattern(p, j)) # NULL + + set_junction_coord(p, j, 100.0, 200.0) + print(get_junction_coord(p, j)) # {'x': 100.0, 'y': 200.0} + + undo(p) + print(get_junction_coord(p, j)) # {'x': 10.0, 'y': 20.0} + + redo(p) + print(get_junction_coord(p, j)) # {'x': 100.0, 'y': 200.0} + + close_project(p) + # delete_project(p) + + +if __name__ == "__main__": + demo_snapshot() + demo_transaction() + demo_1_title() + demo_2_junctions() + pass diff --git a/script/admin.py b/script/admin.py deleted file mode 100644 index 74d7066..0000000 --- a/script/admin.py +++ /dev/null @@ -1,5 +0,0 @@ -from tjnetwork_admin import * - -if __name__ == "__main__": - delete_template() - create_template() diff --git a/script/script/api/create/1.title.sql b/script/script/api/create/1.title.sql new file mode 100644 index 0000000..fb81579 --- /dev/null +++ b/script/script/api/create/1.title.sql @@ -0,0 +1,27 @@ +-- get_title() +create function tj.get_title() returns text as +$$ +declare + title text; +begin + select value into title from tj.title; + return title; +end; +$$ language plpgsql; + +-- set_title() +create function tj.set_title(new_title text) returns void as +$$ +declare + old_title text; + redo text; + undo text; +begin + select tj.get_title() into old_title; + update tj.title set value = new_title where value = old_title; + + redo := concat('update tj.title set value = ''', new_title, ''' where value = ''', old_title, ''''); + undo := concat('update tj.title set value = ''', old_title, ''' where value = ''', new_title, ''''); + perform tj.add_operation(redo, undo); +end; +$$ language plpgsql; diff --git a/script/script/api/create/operation.sql b/script/script/api/create/operation.sql new file mode 100644 index 0000000..ad56cdf --- /dev/null +++ b/script/script/api/create/operation.sql @@ -0,0 +1,109 @@ +create function tj.add_operation(redo text, undo text) returns void as +$$ +declare + parent_id int; + curr_id int; +begin + select id into parent_id from tj.current_operation; + insert into tj.operation (id, redo, undo, parent) values (default, redo, undo, parent_id); + select max(id) into curr_id from tj.operation; + update tj.current_operation set id = curr_id where id = parent_id; +end; +$$ language plpgsql; + +create function tj.have_transaction() returns boolean as +$$ +declare + tran_count int; +begin + select count(*) into tran_count from tj.transaction_operation; + return tran_count > 0; +end; +$$ language plpgsql; + +create function tj.execute_undo(discard boolean) returns void as +$$ +declare + curr_id int; + have_tran boolean; + tran_id int; + strict_mode boolean; + undo_sql text; + parent_id int; +begin + select id into curr_id from tj.current_operation; + + select tj.have_transaction() into have_tran; + if have_tran then + select strict into strict_mode from tj.transaction_operation; + if strict_mode then + return; -- strict mode disallow undo + else + select id into tran_id from tj.transaction_operation; + if tran_id >= curr_id then + return; -- # normal mode disallow undo start point, and there is foreign key constraint + end if; + end if; + end if; + + select undo into undo_sql from tj.operation where id = curr_id; + if undo_sql = '' then + return; + end if; + + select parent into parent_id from tj.operation where id = curr_id; + if discard then + update tj.operation set redo_child = null where id = parent_id; + else + update tj.operation set redo_child = curr_id where id = parent_id; + end if; + + execute undo_sql; + + update tj.current_operation set id = parent_id where id = curr_id; + + if discard then + delete from tj.transaction_operation where id = curr_id; + delete from tj.snapshot_operation where id = curr_id; + delete from tj.operation where id = curr_id; + end if; +end; +$$ language plpgsql; + +create function tj.undo() returns void as +$$ +declare +begin + perform tj.execute_undo(false); +end; +$$ language plpgsql; + +create function tj.discard_undo() returns void as +$$ +declare +begin + perform tj.execute_undo(true); +end; +$$ language plpgsql; + +create function tj.redo() returns void as +$$ +declare + curr_id int; + child_id int; + redo_sql text; +begin + select id into curr_id from tj.current_operation; + + select redo_child into child_id from tj.operation where id = curr_id; + if child_id = null then + return; + end if; + + select redo into redo_sql from tj.operation where id = child_id; + + execute redo_sql; + + update tj.current_operation set id = child_id where id = curr_id; +end; +$$ language plpgsql; \ No newline at end of file diff --git a/script/script/api/create/project.sql b/script/script/api/create/project.sql new file mode 100644 index 0000000..50e8197 --- /dev/null +++ b/script/script/api/create/project.sql @@ -0,0 +1,9 @@ +create function tj.have_project(in_name text) returns boolean as +$$ +declare + db_count int; +begin + select count(*) into db_count from pg_database where datname = in_name; + return db_count > 0; +end; +$$ language plpgsql; diff --git a/script/script/api/drop/1.title.sql b/script/script/api/drop/1.title.sql new file mode 100644 index 0000000..9bef302 --- /dev/null +++ b/script/script/api/drop/1.title.sql @@ -0,0 +1,3 @@ +drop function if exists tj.set_title; + +drop function if exists tj.get_title; \ No newline at end of file diff --git a/script/script/api/drop/operation.sql b/script/script/api/drop/operation.sql new file mode 100644 index 0000000..1692da5 --- /dev/null +++ b/script/script/api/drop/operation.sql @@ -0,0 +1,9 @@ +drop function if exists tj.redo; + +drop function if exists tj.discard_undo; +drop function if exists tj.undo; +drop function if exists tj.execute_undo; + +drop function if exists tj.have_transaction; + +drop function if exists tj.add_operation; \ No newline at end of file diff --git a/script/script/api/plpgsql.sql b/script/script/api/plpgsql.sql new file mode 100644 index 0000000..06c96e0 --- /dev/null +++ b/script/script/api/plpgsql.sql @@ -0,0 +1,6 @@ +create function xxx() returns void as +$$ +declare +begin +end; +$$ language plpgsql; \ No newline at end of file diff --git a/script/script/table/create/0.base.sql b/script/script/table/create/0.base.sql new file mode 100644 index 0000000..91b48c0 --- /dev/null +++ b/script/script/table/create/0.base.sql @@ -0,0 +1,35 @@ +create type tj.node_type as enum +( + 'junction' +, 'reservoir' +, 'tank' +); + +create type tj.link_type as enum +( + 'pipe' +, 'pump' +, 'valve' +); + +create table tj.node +( + id varchar(32) primary key +, type tj.node_type not null +); + +create table tj.link +( + id varchar(32) primary key +, type tj.link_type not null +); + +create table tj.curve +( + id varchar(32) primary key +); + +create table tj.pattern +( + id varchar(32) primary key +); diff --git a/script/script/table/create/1.title.sql b/script/script/table/create/1.title.sql new file mode 100644 index 0000000..8ba6426 --- /dev/null +++ b/script/script/table/create/1.title.sql @@ -0,0 +1,8 @@ +-- [title] + +create table tj.title +( + value text +); + +insert into tj.title (value) values (''); diff --git a/script/script/table/create/10.status.sql b/script/script/table/create/10.status.sql new file mode 100644 index 0000000..c736c76 --- /dev/null +++ b/script/script/table/create/10.status.sql @@ -0,0 +1,33 @@ +-- [status] + +create type tj.status_pipe_pump_status as enum +( + 'open' +, 'closed' +); + +create table tj.status_pipe +( + id varchar(32) primary key references tj.pipes(id) +, status tj.status_pipe_pump_status not null +); + +create table tj.status_pump +( + id varchar(32) primary key references tj.pumps(id) +, status tj.status_pipe_pump_status not null +); + +create type tj.status_valve_status as enum ('open', 'closed', 'active'); + +create table tj.status_valve +( + id varchar(32) primary key references tj.valves(id) +, status tj.status_valve_status not null +); + +create table tj.status_link +( + id varchar(32) primary key references tj.link(id) +, setting numeric not null +); diff --git a/script/script/table/create/11.patterns.sql b/script/script/table/create/11.patterns.sql new file mode 100644 index 0000000..8cd9ad6 --- /dev/null +++ b/script/script/table/create/11.patterns.sql @@ -0,0 +1,7 @@ +-- [patterns] + +create table tj.patterns +( + id varchar(32) references tj.pattern(id) not null +, multipliers numeric not null +); diff --git a/script/script/table/create/12.curves.sql b/script/script/table/create/12.curves.sql new file mode 100644 index 0000000..2467450 --- /dev/null +++ b/script/script/table/create/12.curves.sql @@ -0,0 +1,8 @@ +-- [curves] + +create table tj.curves +( + id varchar(32) references tj.curve(id) not null +, x numeric not null +, y numeric not null +); diff --git a/script/script/table/create/13.controls.sql b/script/script/table/create/13.controls.sql new file mode 100644 index 0000000..ab34bb0 --- /dev/null +++ b/script/script/table/create/13.controls.sql @@ -0,0 +1,30 @@ +-- [controls] + +create type tj.controls_1_prep as enum ('above', 'below'); + +-- link linkid status if node nodeid above / below value +create table tj.controls_1 +( + linkid varchar(32) primary key references tj.link(id) +, status text not null -- open / closed, a pump speed setting, or a control valve setting +, nodeid varchar(32) references tj.node(id) not null +, prep tj.controls_1_prep not null +, value numeric not null +); + +-- link linkid status at time time +create table tj.controls_2 +( + linkid varchar(32) primary key references tj.link(id) +, status text not null -- open / closed, a pump speed setting, or a control valve setting +, time interval hour +); + +-- link linkid status at clocktime clocktime am / pm +create table tj.controls_3 +( + linkid varchar(32) primary key references tj.link(id) +, status text not null -- open / closed, a pump speed setting, or a control valve setting +, clocktimehour interval hour -- get am/pm from it +, clocktimemin interval minute +); diff --git a/script/script/table/create/14.rules.sql b/script/script/table/create/14.rules.sql new file mode 100644 index 0000000..617da6b --- /dev/null +++ b/script/script/table/create/14.rules.sql @@ -0,0 +1,6 @@ +-- [rules] + +create table tj.rules +( + content text primary key +); diff --git a/script/script/table/create/15.energy.sql b/script/script/table/create/15.energy.sql new file mode 100644 index 0000000..1544968 --- /dev/null +++ b/script/script/table/create/15.energy.sql @@ -0,0 +1,29 @@ +-- [energy] + +create type tj.energy_param as enum +( + 'price' +, 'pattern' +, 'effic' +); + +-- global price / pattern / effic value +create table tj.energy_global +( + param tj.energy_param not null +, value numeric not null +); + +-- pump pumpid price / pattern / effic value +create table tj.energy_pump +( + id varchar(32) references tj.pumps(id) not null +, param tj.energy_param not null +, value numeric not null +); + +-- demand charge value +create table tj.energy_demand_charge +( + value numeric not null +); diff --git a/script/script/table/create/16.emitters.sql b/script/script/table/create/16.emitters.sql new file mode 100644 index 0000000..448b9e1 --- /dev/null +++ b/script/script/table/create/16.emitters.sql @@ -0,0 +1,7 @@ +-- [emitters] + +create table tj.emitters +( + junction varchar(32) primary key references tj.junctions(id) +, coefficient numeric not null +); diff --git a/script/script/table/create/17.quality.sql b/script/script/table/create/17.quality.sql new file mode 100644 index 0000000..e972b30 --- /dev/null +++ b/script/script/table/create/17.quality.sql @@ -0,0 +1,7 @@ +-- [quality] + +create table tj.quality +( + node varchar(32) primary key references tj.node(id) +, initialqual numeric not null +); diff --git a/script/script/table/create/18.sources.sql b/script/script/table/create/18.sources.sql new file mode 100644 index 0000000..25d480d --- /dev/null +++ b/script/script/table/create/18.sources.sql @@ -0,0 +1,17 @@ +-- [sources] + +create type tj.sources_type as enum +( + 'concen' +, 'mass' +, 'flowpaced' +, 'setpoint' +); + +create table tj.sources +( + node varchar(32) primary key references tj.node(id) +, type tj.sources_type not null +, strength numeric not null +, timepattern varchar(32) references tj.pattern(id) +); diff --git a/script/script/table/create/19.reactions.sql b/script/script/table/create/19.reactions.sql new file mode 100644 index 0000000..7ddab5d --- /dev/null +++ b/script/script/table/create/19.reactions.sql @@ -0,0 +1,55 @@ +-- [reactions] + +create type tj.reactions_order_param as enum +( + 'bulk' +, 'wall' +, 'tank' +); + +create table tj.reactions_order +( + key tj.reactions_order_param not null +, value numeric not null +); + +create type tj.reactions_global_param as enum +( + 'bulk' +, 'wall' +); + +create table tj.reactions_global +( + key tj.reactions_global_param not null +, value numeric not null +); + +create type tj.reactions_pipe_param as enum +( + 'bulk' +, 'wall' +); + +create table tj.reactions_pipe +( + key tj.reactions_pipe_param not null +, pipe varchar(32) references tj.pipes(id) not null +, value numeric not null +); + +create table tj.reactions_tank +( + tank varchar(32) references tj.tanks(id) not null +, value numeric not null +); + +create table tj.reactions_limiting_potential +( + value numeric not null +); + +create table tj.reactions_roughness_correlation +( + value numeric not null +); diff --git a/script/script/table/create/2.junctions.sql b/script/script/table/create/2.junctions.sql new file mode 100644 index 0000000..6397246 --- /dev/null +++ b/script/script/table/create/2.junctions.sql @@ -0,0 +1,9 @@ +-- [junctions] + +create table tj.junctions +( + id varchar(32) primary key references tj.node(id) +, elevation numeric not null +, demand numeric +, pattern varchar(32) references tj.pattern(id) +); diff --git a/script/script/table/create/20.mixing.sql b/script/script/table/create/20.mixing.sql new file mode 100644 index 0000000..65acf21 --- /dev/null +++ b/script/script/table/create/20.mixing.sql @@ -0,0 +1,16 @@ +-- [mixing] + +create type tj.mixing_model as enum +( + 'mixed' +, '2comp' +, 'fifo' +, 'lifo' +); + +create table tj.mixing +( + tank varchar(32) primary key references tj.tanks(id) +, model tj.mixing_model not null +, value numeric +); diff --git a/script/script/table/create/21.times.sql b/script/script/table/create/21.times.sql new file mode 100644 index 0000000..f66ce2d --- /dev/null +++ b/script/script/table/create/21.times.sql @@ -0,0 +1,9 @@ +-- [times] + +-- todo: constraint + +create table tj.times +( + key text not null +, value text not null +); diff --git a/script/script/table/create/22.report.sql b/script/script/table/create/22.report.sql new file mode 100644 index 0000000..4434a05 --- /dev/null +++ b/script/script/table/create/22.report.sql @@ -0,0 +1,9 @@ +-- [report] + +-- todo: constraint + +create table tj.report +( + key text not null +, value text not null +); diff --git a/script/script/table/create/23.options.sql b/script/script/table/create/23.options.sql new file mode 100644 index 0000000..cab34f4 --- /dev/null +++ b/script/script/table/create/23.options.sql @@ -0,0 +1,9 @@ +-- [options] + +-- todo: constraint + +create table tj.options +( + key text not null +, value text not null +); diff --git a/script/script/table/create/24.coordinates.sql b/script/script/table/create/24.coordinates.sql new file mode 100644 index 0000000..9b2af3b --- /dev/null +++ b/script/script/table/create/24.coordinates.sql @@ -0,0 +1,10 @@ +-- [coordinates] + +create table tj.coordinates +( + node varchar(32) primary key references tj.node(id) +, coord point not null +); + +create index tj_coordinates_spgist on tj.coordinates using spgist(coord); +create index tj_coordinates_gist on tj.coordinates using gist(coord); diff --git a/script/script/table/create/25.vertices.sql b/script/script/table/create/25.vertices.sql new file mode 100644 index 0000000..7b4d403 --- /dev/null +++ b/script/script/table/create/25.vertices.sql @@ -0,0 +1,8 @@ +-- [vertices] + +create table tj.vertices +( + link varchar(32) references tj.link(id) not null +, x numeric not null +, y numeric not null +); diff --git a/script/script/table/create/26.labels.sql b/script/script/table/create/26.labels.sql new file mode 100644 index 0000000..3b41fce --- /dev/null +++ b/script/script/table/create/26.labels.sql @@ -0,0 +1,9 @@ +-- [labels] + +create table tj.labels +( + x numeric not null +, y numeric not null +, label text not null +, anchornode varchar(32) references tj.node(id) +); diff --git a/script/script/table/create/27.backdrop.sql b/script/script/table/create/27.backdrop.sql new file mode 100644 index 0000000..aea6d3b --- /dev/null +++ b/script/script/table/create/27.backdrop.sql @@ -0,0 +1,6 @@ +-- [backdrop] + +create table tj.backdrop +( + content text primary key +); diff --git a/script/sql/section_create/28.end.sql b/script/script/table/create/28.end.sql similarity index 100% rename from script/sql/section_create/28.end.sql rename to script/script/table/create/28.end.sql diff --git a/script/script/table/create/3.reservoirs.sql b/script/script/table/create/3.reservoirs.sql new file mode 100644 index 0000000..e7562ec --- /dev/null +++ b/script/script/table/create/3.reservoirs.sql @@ -0,0 +1,8 @@ +-- [reservoirs] + +create table tj.reservoirs +( + id varchar(32) primary key references tj.node(id) +, head numeric not null +, pattern varchar(32) references tj.pattern(id) +); diff --git a/script/script/table/create/4.tanks.sql b/script/script/table/create/4.tanks.sql new file mode 100644 index 0000000..5a9a815 --- /dev/null +++ b/script/script/table/create/4.tanks.sql @@ -0,0 +1,20 @@ +-- [tanks] + +create type tj.tanks_overflow as enum +( + 'yes' +, 'no' +); + +create table tj.tanks +( + id varchar(32) primary key references tj.node(id) +, elevation numeric not null +, initlevel numeric not null +, minlevel numeric not null +, maxlevel numeric not null +, diameter numeric not null +, minvol numeric not null +, volcurve varchar(32) references tj.curve(id) +, overflow tj.tanks_overflow +); diff --git a/script/script/table/create/5.pipes.sql b/script/script/table/create/5.pipes.sql new file mode 100644 index 0000000..d7ec73e --- /dev/null +++ b/script/script/table/create/5.pipes.sql @@ -0,0 +1,20 @@ +-- [pipes] + +create type tj.pipes_status as enum +( + 'open' +, 'closed' +, 'cv' +); + +create table tj.pipes +( + id varchar(32) primary key references tj.link(id) +, node1 varchar(32) references tj.node(id) not null +, node2 varchar(32) references tj.node(id) not null +, length numeric not null +, diameter numeric not null +, roughness numeric not null +, minorloss numeric not null +, status tj.pipes_status not null +); diff --git a/script/script/table/create/6.pumps.sql b/script/script/table/create/6.pumps.sql new file mode 100644 index 0000000..0f5b0cf --- /dev/null +++ b/script/script/table/create/6.pumps.sql @@ -0,0 +1,32 @@ +-- [pumps] + +create table tj.pumps +( + id varchar(32) primary key references tj.link(id) +, node1 varchar(32) references tj.node(id) not null +, node2 varchar(32) references tj.node(id) not null +); + +create table tj.pumps_property_power +( + id varchar primary key references tj.pumps(id) +, value varchar(32) not null +); + +create table tj.pumps_property_speed +( + id varchar primary key references tj.pumps(id) +, value varchar(32) not null +); + +create table tj.pumps_property_head +( + id varchar primary key references tj.pumps(id) +, head varchar(32) references tj.curve(id) not null +); + +create table tj.pumps_property_pattern +( + id varchar primary key references tj.pumps(id) +, pattern varchar(32) references tj.pattern(id) not null +); diff --git a/script/script/table/create/7.valves.sql b/script/script/table/create/7.valves.sql new file mode 100644 index 0000000..5aa584d --- /dev/null +++ b/script/script/table/create/7.valves.sql @@ -0,0 +1,22 @@ +-- [valves] + +create type tj.valves_type as enum +( + 'prv' +, 'psv' +, 'pbv' +, 'fcv' +, 'tcv' +, 'gpv' +); + +create table tj.valves +( + id varchar(32) primary key references tj.link(id) +, node1 varchar(32) references tj.node(id) not null +, node2 varchar(32) references tj.node(id) not null +, diameter numeric not null +, type tj.valves_type not null +, setting numeric not null +, minorloss numeric not null +); diff --git a/script/script/table/create/8.tags.sql b/script/script/table/create/8.tags.sql new file mode 100644 index 0000000..d98132c --- /dev/null +++ b/script/script/table/create/8.tags.sql @@ -0,0 +1,13 @@ +-- [tags] + +create table tj.tags_node +( + id varchar(32) primary key references tj.node(id) +, tag text not null +); + +create table tj.tags_link +( + id varchar(32) primary key references tj.link(id) +, tag text not null +); diff --git a/script/script/table/create/9.demands.sql b/script/script/table/create/9.demands.sql new file mode 100644 index 0000000..e908227 --- /dev/null +++ b/script/script/table/create/9.demands.sql @@ -0,0 +1,9 @@ +-- [demands] + +create table tj.demands +( + junction varchar(32) references tj.junctions(id) not null +, demand numeric not null +, pattern varchar(32) references tj.pattern(id) +, category text not null +); diff --git a/script/script/table/create/namespace.sql b/script/script/table/create/namespace.sql new file mode 100644 index 0000000..fc993ed --- /dev/null +++ b/script/script/table/create/namespace.sql @@ -0,0 +1 @@ +create schema tj; \ No newline at end of file diff --git a/script/script/table/create/operation.sql b/script/script/table/create/operation.sql new file mode 100644 index 0000000..524ef9e --- /dev/null +++ b/script/script/table/create/operation.sql @@ -0,0 +1,29 @@ +create table tj.operation +( + id serial primary key +, redo text not null +, undo text not null +, parent integer references tj.operation(id) +, redo_child integer references tj.operation(id) +); + +insert into tj.operation (id, redo, undo) values (0, '', ''); + +create table tj.current_operation +( + id integer primary key references tj.operation(id) +); + +insert into tj.current_operation (id) values (0); + +create table tj.snapshot_operation +( + id integer primary key references tj.operation(id) +, tag text not null unique +); + +create table tj.transaction_operation +( + id integer primary key references tj.operation(id) +, strict boolean not null default false +); diff --git a/script/script/table/drop/0.base.sql b/script/script/table/drop/0.base.sql new file mode 100644 index 0000000..41c4f41 --- /dev/null +++ b/script/script/table/drop/0.base.sql @@ -0,0 +1,11 @@ +drop table if exists tj.pattern; + +drop table if exists tj.curve; + +drop table if exists tj.link; + +drop table if exists tj.node; + +drop type if exists tj.link_type; + +drop type if exists tj.node_type; diff --git a/script/script/table/drop/1.title.sql b/script/script/table/drop/1.title.sql new file mode 100644 index 0000000..a03e80a --- /dev/null +++ b/script/script/table/drop/1.title.sql @@ -0,0 +1,3 @@ +-- [title] + +drop table if exists tj.title; diff --git a/script/script/table/drop/10.status.sql b/script/script/table/drop/10.status.sql new file mode 100644 index 0000000..232ca4d --- /dev/null +++ b/script/script/table/drop/10.status.sql @@ -0,0 +1,13 @@ +-- [status] + +drop table if exists tj.status_link; + +drop table if exists tj.status_valve; + +drop type if exists tj.status_valve_status; + +drop table if exists tj.status_pump; + +drop table if exists tj.status_pipe; + +drop type if exists tj.status_pipe_pump_status; diff --git a/script/script/table/drop/11.patterns.sql b/script/script/table/drop/11.patterns.sql new file mode 100644 index 0000000..5fb2b47 --- /dev/null +++ b/script/script/table/drop/11.patterns.sql @@ -0,0 +1,3 @@ +-- [patterns] + +drop table if exists tj.patterns; diff --git a/script/script/table/drop/12.curves.sql b/script/script/table/drop/12.curves.sql new file mode 100644 index 0000000..4874ee8 --- /dev/null +++ b/script/script/table/drop/12.curves.sql @@ -0,0 +1,3 @@ +-- [curves] + +drop table if exists tj.curves; diff --git a/script/script/table/drop/13.controls.sql b/script/script/table/drop/13.controls.sql new file mode 100644 index 0000000..f6c0502 --- /dev/null +++ b/script/script/table/drop/13.controls.sql @@ -0,0 +1,9 @@ +-- [controls] + +drop table if exists tj.controls_3; + +drop table if exists tj.controls_2; + +drop table if exists tj.controls_1; + +drop type if exists tj.controls_1_prep; diff --git a/script/script/table/drop/14.rules.sql b/script/script/table/drop/14.rules.sql new file mode 100644 index 0000000..a920e6a --- /dev/null +++ b/script/script/table/drop/14.rules.sql @@ -0,0 +1,3 @@ +-- [rules] + +drop table if exists tj.rules; diff --git a/script/script/table/drop/15.energy.sql b/script/script/table/drop/15.energy.sql new file mode 100644 index 0000000..abf2e4a --- /dev/null +++ b/script/script/table/drop/15.energy.sql @@ -0,0 +1,9 @@ +-- [energy] + +drop table if exists tj.energy_demand_charge; + +drop table if exists tj.energy_pump; + +drop table if exists tj.energy_global; + +drop type if exists tj.energy_param; diff --git a/script/script/table/drop/16.emitters.sql b/script/script/table/drop/16.emitters.sql new file mode 100644 index 0000000..ec23cd3 --- /dev/null +++ b/script/script/table/drop/16.emitters.sql @@ -0,0 +1,3 @@ +-- [emitters] + +drop table if exists tj.emitters; diff --git a/script/script/table/drop/17.quality.sql b/script/script/table/drop/17.quality.sql new file mode 100644 index 0000000..673cd2f --- /dev/null +++ b/script/script/table/drop/17.quality.sql @@ -0,0 +1,3 @@ +-- [quality] + +drop table if exists tj.quality; diff --git a/script/script/table/drop/18.sources.sql b/script/script/table/drop/18.sources.sql new file mode 100644 index 0000000..e9487c8 --- /dev/null +++ b/script/script/table/drop/18.sources.sql @@ -0,0 +1,5 @@ +-- [sources] + +drop table if exists tj.sources; + +drop type if exists tj.sources_type; diff --git a/script/script/table/drop/19.reactions.sql b/script/script/table/drop/19.reactions.sql new file mode 100644 index 0000000..0dff3f0 --- /dev/null +++ b/script/script/table/drop/19.reactions.sql @@ -0,0 +1,19 @@ +-- [reactions] + +drop table if exists tj.reactions_roughness_correlation; + +drop table if exists tj.reactions_limiting_potential; + +drop table if exists tj.reactions_tank; + +drop table if exists tj.reactions_pipe; + +drop type if exists tj.reactions_pipe_param; + +drop table if exists tj.reactions_global; + +drop type if exists tj.reactions_global_param; + +drop table if exists tj.reactions_order; + +drop type if exists tj.reactions_order_param; diff --git a/script/script/table/drop/2.junctions.sql b/script/script/table/drop/2.junctions.sql new file mode 100644 index 0000000..0b436c4 --- /dev/null +++ b/script/script/table/drop/2.junctions.sql @@ -0,0 +1,3 @@ +-- [junctions] + +drop table if exists tj.junctions; diff --git a/script/script/table/drop/20.mixing.sql b/script/script/table/drop/20.mixing.sql new file mode 100644 index 0000000..f2adc25 --- /dev/null +++ b/script/script/table/drop/20.mixing.sql @@ -0,0 +1,5 @@ +-- [mixing] + +drop table if exists tj.mixing; + +drop type if exists tj.mixing_model; diff --git a/script/script/table/drop/21.times.sql b/script/script/table/drop/21.times.sql new file mode 100644 index 0000000..b56a5cf --- /dev/null +++ b/script/script/table/drop/21.times.sql @@ -0,0 +1,3 @@ +-- [times] + +drop table if exists tj.times; diff --git a/script/script/table/drop/22.report.sql b/script/script/table/drop/22.report.sql new file mode 100644 index 0000000..03f771b --- /dev/null +++ b/script/script/table/drop/22.report.sql @@ -0,0 +1,3 @@ +-- [report] + +drop table if exists tj.report; diff --git a/script/script/table/drop/23.options.sql b/script/script/table/drop/23.options.sql new file mode 100644 index 0000000..dc8c63c --- /dev/null +++ b/script/script/table/drop/23.options.sql @@ -0,0 +1,3 @@ +-- [options] + +drop table if exists tj.options; diff --git a/script/script/table/drop/24.coordinates.sql b/script/script/table/drop/24.coordinates.sql new file mode 100644 index 0000000..0d74d3e --- /dev/null +++ b/script/script/table/drop/24.coordinates.sql @@ -0,0 +1,7 @@ +-- [coordinates] + +drop index if exists tj_coordinates_gist; + +drop index if exists tj_coordinates_spgist; + +drop table if exists tj.coordinates; diff --git a/script/script/table/drop/25.vertices.sql b/script/script/table/drop/25.vertices.sql new file mode 100644 index 0000000..a060c4a --- /dev/null +++ b/script/script/table/drop/25.vertices.sql @@ -0,0 +1,3 @@ +-- [vertices] + +drop table if exists tj.vertices; diff --git a/script/script/table/drop/26.labels.sql b/script/script/table/drop/26.labels.sql new file mode 100644 index 0000000..4f9f743 --- /dev/null +++ b/script/script/table/drop/26.labels.sql @@ -0,0 +1,3 @@ +-- [labels] + +drop table if exists tj.labels; diff --git a/script/script/table/drop/27.backdrop.sql b/script/script/table/drop/27.backdrop.sql new file mode 100644 index 0000000..ba47de7 --- /dev/null +++ b/script/script/table/drop/27.backdrop.sql @@ -0,0 +1,3 @@ +-- [backdrop] + +drop table if exists tj.backdrop; diff --git a/script/sql/section_drop/28.end.sql b/script/script/table/drop/28.end.sql similarity index 100% rename from script/sql/section_drop/28.end.sql rename to script/script/table/drop/28.end.sql diff --git a/script/script/table/drop/3.reservoirs.sql b/script/script/table/drop/3.reservoirs.sql new file mode 100644 index 0000000..c9a1e25 --- /dev/null +++ b/script/script/table/drop/3.reservoirs.sql @@ -0,0 +1,3 @@ +-- [reservoirs] + +drop table if exists tj.reservoirs; diff --git a/script/script/table/drop/4.tanks.sql b/script/script/table/drop/4.tanks.sql new file mode 100644 index 0000000..0a5e589 --- /dev/null +++ b/script/script/table/drop/4.tanks.sql @@ -0,0 +1,5 @@ +-- [tanks] + +drop table if exists tj.tanks; + +drop type if exists tj.tanks_overflow; diff --git a/script/script/table/drop/5.pipes.sql b/script/script/table/drop/5.pipes.sql new file mode 100644 index 0000000..0cdc8dc --- /dev/null +++ b/script/script/table/drop/5.pipes.sql @@ -0,0 +1,5 @@ +-- [pipes] + +drop table if exists tj.pipes; + +drop type if exists tj.pipes_status; diff --git a/script/script/table/drop/6.pumps.sql b/script/script/table/drop/6.pumps.sql new file mode 100644 index 0000000..18d697b --- /dev/null +++ b/script/script/table/drop/6.pumps.sql @@ -0,0 +1,11 @@ +-- [pumps] + +drop table if exists tj.pumps_property_pattern; + +drop table if exists tj.pumps_property_head; + +drop table if exists tj.pumps_property_speed; + +drop table if exists tj.pumps_property_power; + +drop table if exists tj.pumps; diff --git a/script/script/table/drop/7.valves.sql b/script/script/table/drop/7.valves.sql new file mode 100644 index 0000000..0c2c2d1 --- /dev/null +++ b/script/script/table/drop/7.valves.sql @@ -0,0 +1,5 @@ +-- [valves] + +drop table if exists tj.valves; + +drop type if exists tj.valves_type; diff --git a/script/script/table/drop/8.tags.sql b/script/script/table/drop/8.tags.sql new file mode 100644 index 0000000..e277759 --- /dev/null +++ b/script/script/table/drop/8.tags.sql @@ -0,0 +1,5 @@ +-- [tags] + +drop table if exists tj.tags_link; + +drop table if exists tj.tags_node; diff --git a/script/script/table/drop/9.demands.sql b/script/script/table/drop/9.demands.sql new file mode 100644 index 0000000..8478a8b --- /dev/null +++ b/script/script/table/drop/9.demands.sql @@ -0,0 +1,3 @@ +-- [demands] + +drop table if exists tj.demands; diff --git a/script/script/table/drop/namespace.sql b/script/script/table/drop/namespace.sql new file mode 100644 index 0000000..effb087 --- /dev/null +++ b/script/script/table/drop/namespace.sql @@ -0,0 +1 @@ +drop schema if exists tj; \ No newline at end of file diff --git a/script/script/table/drop/operation.sql b/script/script/table/drop/operation.sql new file mode 100644 index 0000000..cf3dc7b --- /dev/null +++ b/script/script/table/drop/operation.sql @@ -0,0 +1,7 @@ +drop table if exists tj.transaction_operation; + +drop table if exists tj.snapshot_operation; + +drop table if exists tj.current_operation; + +drop table if exists tj.operation; diff --git a/script/script/template.py b/script/script/template.py new file mode 100644 index 0000000..7ed9a98 --- /dev/null +++ b/script/script/template.py @@ -0,0 +1,108 @@ +import psycopg as pg + +sql_create = [ + "table/create/namespace.sql", + "table/create/0.base.sql", + "table/create/1.title.sql", + "table/create/2.junctions.sql", + "table/create/3.reservoirs.sql", + "table/create/4.tanks.sql", + "table/create/5.pipes.sql", + "table/create/6.pumps.sql", + "table/create/7.valves.sql", + "table/create/8.tags.sql", + "table/create/9.demands.sql", + "table/create/10.status.sql", + "table/create/11.patterns.sql", + "table/create/12.curves.sql", + "table/create/13.controls.sql", + "table/create/14.rules.sql", + "table/create/15.energy.sql", + "table/create/16.emitters.sql", + "table/create/17.quality.sql", + "table/create/18.sources.sql", + "table/create/19.reactions.sql", + "table/create/20.mixing.sql", + "table/create/21.times.sql", + "table/create/22.report.sql", + "table/create/23.options.sql", + "table/create/24.coordinates.sql", + "table/create/25.vertices.sql", + "table/create/26.labels.sql", + "table/create/27.backdrop.sql", + "table/create/28.end.sql", + "table/create/operation.sql", + "api/create/operation.sql", + "api/create/1.title.sql" +] + +sql_drop = [ + "api/drop/1.title.sql", + "api/drop/operation.sql", + "table/drop/operation.sql", + "table/drop/28.end.sql", + "table/drop/27.backdrop.sql", + "table/drop/26.labels.sql", + "table/drop/25.vertices.sql", + "table/drop/24.coordinates.sql", + "table/drop/23.options.sql", + "table/drop/22.report.sql", + "table/drop/21.times.sql", + "table/drop/20.mixing.sql", + "table/drop/19.reactions.sql", + "table/drop/18.sources.sql", + "table/drop/17.quality.sql", + "table/drop/16.emitters.sql", + "table/drop/15.energy.sql", + "table/drop/14.rules.sql", + "table/drop/13.controls.sql", + "table/drop/12.curves.sql", + "table/drop/11.patterns.sql", + "table/drop/10.status.sql", + "table/drop/9.demands.sql", + "table/drop/8.tags.sql", + "table/drop/7.valves.sql", + "table/drop/6.pumps.sql", + "table/drop/5.pipes.sql", + "table/drop/4.tanks.sql", + "table/drop/3.reservoirs.sql", + "table/drop/2.junctions.sql", + "table/drop/1.title.sql", + "table/drop/0.base.sql", + "table/drop/namespace.sql" +] + +def create_template(): + with pg.connect(conninfo="dbname=postgres", autocommit=True) as conn: + with conn.cursor() as cur: + cur.execute("create database tj_project") + with pg.connect(conninfo="dbname=tj_project") as conn: + with conn.cursor() as cur: + for sql in sql_create: + with open(sql, "r") as f: + cur.execute(f.read()) + print(f'executed {sql}') + conn.commit() + +def have_template(): + with pg.connect(conninfo="dbname=postgres", autocommit=True) as conn: + with conn.cursor() as cur: + cur.execute("select * from pg_database where datname = 'tj_project'") + return cur.rowcount > 0 + +def delete_template(): + with pg.connect(conninfo="dbname=tj_project") as conn: + with conn.cursor() as cur: + for sql in sql_drop: + with open(sql, "r") as f: + cur.execute(f.read()) + print(f'executed {sql}') + conn.commit() + with pg.connect(conninfo="dbname=postgres", autocommit=True) as conn: + with conn.cursor() as cur: + cur.execute("drop database tj_project") + +if __name__ == "__main__": + if (have_template()): + delete_template() + create_template() diff --git a/script/sql/base_create.sql b/script/sql/create/0.base.sql similarity index 100% rename from script/sql/base_create.sql rename to script/sql/create/0.base.sql diff --git a/script/sql/section_create/1.title.sql b/script/sql/create/1.title.sql similarity index 55% rename from script/sql/section_create/1.title.sql rename to script/sql/create/1.title.sql index 0a48eff..c00e981 100644 --- a/script/sql/section_create/1.title.sql +++ b/script/sql/create/1.title.sql @@ -4,3 +4,5 @@ CREATE TABLE TITLE ( Value TEXT ); + +INSERT INTO TITLE (Value) VALUES (''); diff --git a/script/sql/section_create/10.status.sql b/script/sql/create/10.status.sql similarity index 100% rename from script/sql/section_create/10.status.sql rename to script/sql/create/10.status.sql diff --git a/script/sql/section_create/11.patterns.sql b/script/sql/create/11.patterns.sql similarity index 100% rename from script/sql/section_create/11.patterns.sql rename to script/sql/create/11.patterns.sql diff --git a/script/sql/section_create/12.curves.sql b/script/sql/create/12.curves.sql similarity index 100% rename from script/sql/section_create/12.curves.sql rename to script/sql/create/12.curves.sql diff --git a/script/sql/section_create/13.controls.sql b/script/sql/create/13.controls.sql similarity index 100% rename from script/sql/section_create/13.controls.sql rename to script/sql/create/13.controls.sql diff --git a/script/sql/section_create/14.rules.sql b/script/sql/create/14.rules.sql similarity index 100% rename from script/sql/section_create/14.rules.sql rename to script/sql/create/14.rules.sql diff --git a/script/sql/section_create/15.energy.sql b/script/sql/create/15.energy.sql similarity index 100% rename from script/sql/section_create/15.energy.sql rename to script/sql/create/15.energy.sql diff --git a/script/sql/section_create/16.emitters.sql b/script/sql/create/16.emitters.sql similarity index 100% rename from script/sql/section_create/16.emitters.sql rename to script/sql/create/16.emitters.sql diff --git a/script/sql/section_create/17.quality.sql b/script/sql/create/17.quality.sql similarity index 100% rename from script/sql/section_create/17.quality.sql rename to script/sql/create/17.quality.sql diff --git a/script/sql/section_create/18.sources.sql b/script/sql/create/18.sources.sql similarity index 100% rename from script/sql/section_create/18.sources.sql rename to script/sql/create/18.sources.sql diff --git a/script/sql/section_create/19.reactions.sql b/script/sql/create/19.reactions.sql similarity index 100% rename from script/sql/section_create/19.reactions.sql rename to script/sql/create/19.reactions.sql diff --git a/script/sql/section_create/2.junctions.sql b/script/sql/create/2.junctions.sql similarity index 100% rename from script/sql/section_create/2.junctions.sql rename to script/sql/create/2.junctions.sql diff --git a/script/sql/section_create/20.mixing.sql b/script/sql/create/20.mixing.sql similarity index 100% rename from script/sql/section_create/20.mixing.sql rename to script/sql/create/20.mixing.sql diff --git a/script/sql/section_create/21.times.sql b/script/sql/create/21.times.sql similarity index 100% rename from script/sql/section_create/21.times.sql rename to script/sql/create/21.times.sql diff --git a/script/sql/section_create/22.report.sql b/script/sql/create/22.report.sql similarity index 100% rename from script/sql/section_create/22.report.sql rename to script/sql/create/22.report.sql diff --git a/script/sql/section_create/23.options.sql b/script/sql/create/23.options.sql similarity index 100% rename from script/sql/section_create/23.options.sql rename to script/sql/create/23.options.sql diff --git a/script/sql/section_create/24.coordinates.sql b/script/sql/create/24.coordinates.sql similarity index 82% rename from script/sql/section_create/24.coordinates.sql rename to script/sql/create/24.coordinates.sql index 2bad5dc..5b6ed6d 100644 --- a/script/sql/section_create/24.coordinates.sql +++ b/script/sql/create/24.coordinates.sql @@ -3,7 +3,7 @@ CREATE TABLE COORDINATES ( Node VARCHAR(32) PRIMARY KEY REFERENCES _NODE(ID) -, Coord POINT NOT NULL DEFAULT(POINT(0.0, 0.0)) +, Coord POINT NOT NULL ); CREATE INDEX COORDINATES_SPGIST ON COORDINATES USING SPGIST(Coord); diff --git a/script/sql/section_create/25.vertices.sql b/script/sql/create/25.vertices.sql similarity index 100% rename from script/sql/section_create/25.vertices.sql rename to script/sql/create/25.vertices.sql diff --git a/script/sql/section_create/26.labels.sql b/script/sql/create/26.labels.sql similarity index 100% rename from script/sql/section_create/26.labels.sql rename to script/sql/create/26.labels.sql diff --git a/script/sql/section_create/27.backdrop.sql b/script/sql/create/27.backdrop.sql similarity index 100% rename from script/sql/section_create/27.backdrop.sql rename to script/sql/create/27.backdrop.sql diff --git a/script/sql/create/28.end.sql b/script/sql/create/28.end.sql new file mode 100644 index 0000000..3054ad0 --- /dev/null +++ b/script/sql/create/28.end.sql @@ -0,0 +1 @@ +-- [END] \ No newline at end of file diff --git a/script/sql/section_create/3.reservoirs.sql b/script/sql/create/3.reservoirs.sql similarity index 100% rename from script/sql/section_create/3.reservoirs.sql rename to script/sql/create/3.reservoirs.sql diff --git a/script/sql/section_create/4.tanks.sql b/script/sql/create/4.tanks.sql similarity index 100% rename from script/sql/section_create/4.tanks.sql rename to script/sql/create/4.tanks.sql diff --git a/script/sql/section_create/5.pipes.sql b/script/sql/create/5.pipes.sql similarity index 100% rename from script/sql/section_create/5.pipes.sql rename to script/sql/create/5.pipes.sql diff --git a/script/sql/section_create/6.pumps.sql b/script/sql/create/6.pumps.sql similarity index 100% rename from script/sql/section_create/6.pumps.sql rename to script/sql/create/6.pumps.sql diff --git a/script/sql/section_create/7.valves.sql b/script/sql/create/7.valves.sql similarity index 100% rename from script/sql/section_create/7.valves.sql rename to script/sql/create/7.valves.sql diff --git a/script/sql/section_create/8.tags.sql b/script/sql/create/8.tags.sql similarity index 100% rename from script/sql/section_create/8.tags.sql rename to script/sql/create/8.tags.sql diff --git a/script/sql/section_create/9.demands.sql b/script/sql/create/9.demands.sql similarity index 100% rename from script/sql/section_create/9.demands.sql rename to script/sql/create/9.demands.sql diff --git a/script/sql/create/operation.sql b/script/sql/create/operation.sql new file mode 100644 index 0000000..2f105e6 --- /dev/null +++ b/script/sql/create/operation.sql @@ -0,0 +1,29 @@ +CREATE TABLE OPERATION +( + ID SERIAL PRIMARY KEY +, Redo TEXT NOT NULL +, Undo TEXT NOT NULL +, Parent INTEGER REFERENCES OPERATION(ID) +, Redo_Child INTEGER REFERENCES OPERATION(ID) +); + +INSERT INTO OPERATION (ID, Redo, Undo) VALUES (0, '', ''); + +CREATE TABLE CURRENT_OPERATION +( + ID INTEGER PRIMARY KEY REFERENCES OPERATION(ID) +); + +INSERT INTO CURRENT_OPERATION (ID) VALUES (0); + +CREATE TABLE SNAPSHOT_OPERATION +( + ID INTEGER PRIMARY KEY REFERENCES OPERATION(ID) +, Tag TEXT NOT NULL UNIQUE +); + +CREATE TABLE TRANSACTION_OPERATION +( + ID INTEGER PRIMARY KEY REFERENCES OPERATION(ID) +, STRICT BOOLEAN NOT NULL DEFAULT FALSE +); diff --git a/script/sql/base_drop.sql b/script/sql/drop/0.base.sql similarity index 100% rename from script/sql/base_drop.sql rename to script/sql/drop/0.base.sql diff --git a/script/sql/drop/1.title.sql b/script/sql/drop/1.title.sql new file mode 100644 index 0000000..7ee6d55 --- /dev/null +++ b/script/sql/drop/1.title.sql @@ -0,0 +1,3 @@ +-- [TITLE] + +DROP TABLE IF EXISTS TITLE; diff --git a/script/sql/drop/10.status.sql b/script/sql/drop/10.status.sql new file mode 100644 index 0000000..322a72f --- /dev/null +++ b/script/sql/drop/10.status.sql @@ -0,0 +1,13 @@ +-- [STATUS] + +DROP TABLE IF EXISTS STATUS_LINK; + +DROP TABLE IF EXISTS STATUS_VALVE; + +DROP TYPE IF EXISTS STATUS_VALVE_STATUS; + +DROP TABLE IF EXISTS STATUS_PUMP; + +DROP TABLE IF EXISTS STATUS_PIPE; + +DROP TYPE IF EXISTS STATUS_PIPE_PUMP_STATUS; diff --git a/script/sql/drop/11.patterns.sql b/script/sql/drop/11.patterns.sql new file mode 100644 index 0000000..fd08d97 --- /dev/null +++ b/script/sql/drop/11.patterns.sql @@ -0,0 +1,3 @@ +-- [PATTERNS] + +DROP TABLE IF EXISTS PATTERNS; diff --git a/script/sql/drop/12.curves.sql b/script/sql/drop/12.curves.sql new file mode 100644 index 0000000..577464e --- /dev/null +++ b/script/sql/drop/12.curves.sql @@ -0,0 +1,3 @@ +-- [CURVES] + +DROP TABLE IF EXISTS CURVES; diff --git a/script/sql/drop/13.controls.sql b/script/sql/drop/13.controls.sql new file mode 100644 index 0000000..476259d --- /dev/null +++ b/script/sql/drop/13.controls.sql @@ -0,0 +1,9 @@ +-- [CONTROLS] + +DROP TABLE IF EXISTS CONTROLS_3; + +DROP TABLE IF EXISTS CONTROLS_2; + +DROP TABLE IF EXISTS CONTROLS_1; + +DROP TYPE IF EXISTS CONTROLS_1_PREP; diff --git a/script/sql/drop/14.rules.sql b/script/sql/drop/14.rules.sql new file mode 100644 index 0000000..84e4fd8 --- /dev/null +++ b/script/sql/drop/14.rules.sql @@ -0,0 +1,3 @@ +-- [RULES] + +DROP TABLE IF EXISTS RULES; diff --git a/script/sql/drop/15.energy.sql b/script/sql/drop/15.energy.sql new file mode 100644 index 0000000..5002d5c --- /dev/null +++ b/script/sql/drop/15.energy.sql @@ -0,0 +1,9 @@ +-- [ENERGY] + +DROP TABLE IF EXISTS ENERGY_DEMAND_CHARGE; + +DROP TABLE IF EXISTS ENERGY_PUMP; + +DROP TABLE IF EXISTS ENERGY_GLOBAL; + +DROP TYPE IF EXISTS ENERGY_PARAM; diff --git a/script/sql/drop/16.emitters.sql b/script/sql/drop/16.emitters.sql new file mode 100644 index 0000000..40a0af8 --- /dev/null +++ b/script/sql/drop/16.emitters.sql @@ -0,0 +1,3 @@ +-- [EMITTERS] + +DROP TABLE IF EXISTS EMITTERS; diff --git a/script/sql/drop/17.quality.sql b/script/sql/drop/17.quality.sql new file mode 100644 index 0000000..b72c290 --- /dev/null +++ b/script/sql/drop/17.quality.sql @@ -0,0 +1,3 @@ +-- [QUALITY] + +DROP TABLE IF EXISTS QUALITY; diff --git a/script/sql/drop/18.sources.sql b/script/sql/drop/18.sources.sql new file mode 100644 index 0000000..0402ffe --- /dev/null +++ b/script/sql/drop/18.sources.sql @@ -0,0 +1,5 @@ +-- [SOURCES] + +DROP TABLE IF EXISTS SOURCES; + +DROP TYPE IF EXISTS SOURCES_TYPE; diff --git a/script/sql/drop/19.reactions.sql b/script/sql/drop/19.reactions.sql new file mode 100644 index 0000000..2f6ba5b --- /dev/null +++ b/script/sql/drop/19.reactions.sql @@ -0,0 +1,19 @@ +-- [REACTIONS] + +DROP TABLE IF EXISTS REACTIONS_ROUGHNESS_CORRELATION; + +DROP TABLE IF EXISTS REACTIONS_LIMITING_POTENTIAL; + +DROP TABLE IF EXISTS REACTIONS_TANK; + +DROP TABLE IF EXISTS REACTIONS_PIPE; + +DROP TYPE IF EXISTS REACTIONS_PIPE_PARAM; + +DROP TABLE IF EXISTS REACTIONS_GLOBAL; + +DROP TYPE IF EXISTS REACTIONS_GLOBAL_PARAM; + +DROP TABLE IF EXISTS REACTIONS_ORDER; + +DROP TYPE IF EXISTS REACTIONS_ORDER_PARAM; diff --git a/script/sql/drop/2.junctions.sql b/script/sql/drop/2.junctions.sql new file mode 100644 index 0000000..ef0a609 --- /dev/null +++ b/script/sql/drop/2.junctions.sql @@ -0,0 +1,3 @@ +-- [JUNCTIONS] + +DROP TABLE IF EXISTS JUNCTIONS; diff --git a/script/sql/drop/20.mixing.sql b/script/sql/drop/20.mixing.sql new file mode 100644 index 0000000..938d554 --- /dev/null +++ b/script/sql/drop/20.mixing.sql @@ -0,0 +1,5 @@ +-- [MIXING] + +DROP TABLE IF EXISTS MIXING; + +DROP TYPE IF EXISTS MIXING_Model; diff --git a/script/sql/drop/21.times.sql b/script/sql/drop/21.times.sql new file mode 100644 index 0000000..f051bd7 --- /dev/null +++ b/script/sql/drop/21.times.sql @@ -0,0 +1,3 @@ +-- [TIMES] + +DROP TABLE IF EXISTS TIMES; diff --git a/script/sql/drop/22.report.sql b/script/sql/drop/22.report.sql new file mode 100644 index 0000000..790684d --- /dev/null +++ b/script/sql/drop/22.report.sql @@ -0,0 +1,3 @@ +-- [REPORT] + +DROP TABLE IF EXISTS REPORT; diff --git a/script/sql/drop/23.options.sql b/script/sql/drop/23.options.sql new file mode 100644 index 0000000..7cc629a --- /dev/null +++ b/script/sql/drop/23.options.sql @@ -0,0 +1,3 @@ +-- [OPTIONS] + +DROP TABLE IF EXISTS OPTIONS; diff --git a/script/sql/drop/24.coordinates.sql b/script/sql/drop/24.coordinates.sql new file mode 100644 index 0000000..04d0465 --- /dev/null +++ b/script/sql/drop/24.coordinates.sql @@ -0,0 +1,7 @@ +-- [COORDINATES] + +DROP INDEX IF EXISTS COORDINATES_GIST; + +DROP INDEX IF EXISTS COORDINATES_SPGIST; + +DROP TABLE IF EXISTS COORDINATES; diff --git a/script/sql/drop/25.vertices.sql b/script/sql/drop/25.vertices.sql new file mode 100644 index 0000000..f74dd3e --- /dev/null +++ b/script/sql/drop/25.vertices.sql @@ -0,0 +1,3 @@ +-- [VERTICES] + +DROP TABLE IF EXISTS VERTICES; diff --git a/script/sql/drop/26.labels.sql b/script/sql/drop/26.labels.sql new file mode 100644 index 0000000..aa1b2b2 --- /dev/null +++ b/script/sql/drop/26.labels.sql @@ -0,0 +1,3 @@ +-- [LABELS] + +DROP TABLE IF EXISTS LABELS; diff --git a/script/sql/drop/27.backdrop.sql b/script/sql/drop/27.backdrop.sql new file mode 100644 index 0000000..408bb85 --- /dev/null +++ b/script/sql/drop/27.backdrop.sql @@ -0,0 +1,3 @@ +-- [BACKDROP] + +DROP TABLE IF EXISTS BACKDROP; diff --git a/script/sql/drop/28.end.sql b/script/sql/drop/28.end.sql new file mode 100644 index 0000000..3054ad0 --- /dev/null +++ b/script/sql/drop/28.end.sql @@ -0,0 +1 @@ +-- [END] \ No newline at end of file diff --git a/script/sql/drop/3.reservoirs.sql b/script/sql/drop/3.reservoirs.sql new file mode 100644 index 0000000..9864c18 --- /dev/null +++ b/script/sql/drop/3.reservoirs.sql @@ -0,0 +1,3 @@ +-- [RESERVOIRS] + +DROP TABLE IF EXISTS RESERVOIRS; diff --git a/script/sql/drop/4.tanks.sql b/script/sql/drop/4.tanks.sql new file mode 100644 index 0000000..da0487d --- /dev/null +++ b/script/sql/drop/4.tanks.sql @@ -0,0 +1,5 @@ +-- [TANKS] + +DROP TABLE IF EXISTS TANKS; + +DROP TYPE IF EXISTS TANKS_OVERFLOW; diff --git a/script/sql/drop/5.pipes.sql b/script/sql/drop/5.pipes.sql new file mode 100644 index 0000000..1e23612 --- /dev/null +++ b/script/sql/drop/5.pipes.sql @@ -0,0 +1,5 @@ +-- [PIPES] + +DROP TABLE IF EXISTS PIPES; + +DROP TYPE IF EXISTS PIPES_STATUS; diff --git a/script/sql/drop/6.pumps.sql b/script/sql/drop/6.pumps.sql new file mode 100644 index 0000000..5ac9656 --- /dev/null +++ b/script/sql/drop/6.pumps.sql @@ -0,0 +1,11 @@ +-- [PUMPS] + +DROP TABLE IF EXISTS PUMPS_PROPERTY_PATTERN; + +DROP TABLE IF EXISTS PUMPS_PROPERTY_HEAD; + +DROP TABLE IF EXISTS PUMPS_PROPERTY_SPEED; + +DROP TABLE IF EXISTS PUMPS_PROPERTY_POWER; + +DROP TABLE IF EXISTS PUMPS; diff --git a/script/sql/drop/7.valves.sql b/script/sql/drop/7.valves.sql new file mode 100644 index 0000000..c95b423 --- /dev/null +++ b/script/sql/drop/7.valves.sql @@ -0,0 +1,5 @@ +-- [VALVES] + +DROP TABLE IF EXISTS VALVES; + +DROP TYPE IF EXISTS VALVES_TYPE; diff --git a/script/sql/drop/8.tags.sql b/script/sql/drop/8.tags.sql new file mode 100644 index 0000000..df8e3d1 --- /dev/null +++ b/script/sql/drop/8.tags.sql @@ -0,0 +1,5 @@ +-- [TAGS] + +DROP TABLE IF EXISTS TAGS_LINK; + +DROP TABLE IF EXISTS TAGS_NODE; diff --git a/script/sql/drop/9.demands.sql b/script/sql/drop/9.demands.sql new file mode 100644 index 0000000..ebe0209 --- /dev/null +++ b/script/sql/drop/9.demands.sql @@ -0,0 +1,3 @@ +-- [DEMANDS] + +DROP TABLE IF EXISTS DEMANDS; diff --git a/script/sql/drop/operation.sql b/script/sql/drop/operation.sql new file mode 100644 index 0000000..998ee2f --- /dev/null +++ b/script/sql/drop/operation.sql @@ -0,0 +1,7 @@ +DROP TABLE IF EXISTS TRANSACTION_OPERATION; + +DROP TABLE IF EXISTS SNAPSHOT_OPERATION; + +DROP TABLE IF EXISTS CURRENT_OPERATION; + +DROP TABLE IF EXISTS OPERATION; diff --git a/script/sql/section_drop/1.title.sql b/script/sql/section_drop/1.title.sql deleted file mode 100644 index 215ecc6..0000000 --- a/script/sql/section_drop/1.title.sql +++ /dev/null @@ -1,3 +0,0 @@ --- [TITLE] - -DROP TABLE TITLE; diff --git a/script/sql/section_drop/10.status.sql b/script/sql/section_drop/10.status.sql deleted file mode 100644 index 970330e..0000000 --- a/script/sql/section_drop/10.status.sql +++ /dev/null @@ -1,13 +0,0 @@ --- [STATUS] - -DROP TABLE STATUS_LINK; - -DROP TABLE STATUS_VALVE; - -DROP TYPE STATUS_VALVE_STATUS; - -DROP TABLE STATUS_PUMP; - -DROP TABLE STATUS_PIPE; - -DROP TYPE STATUS_PIPE_PUMP_STATUS; diff --git a/script/sql/section_drop/11.patterns.sql b/script/sql/section_drop/11.patterns.sql deleted file mode 100644 index 565c67e..0000000 --- a/script/sql/section_drop/11.patterns.sql +++ /dev/null @@ -1,3 +0,0 @@ --- [PATTERNS] - -DROP TABLE PATTERNS; diff --git a/script/sql/section_drop/12.curves.sql b/script/sql/section_drop/12.curves.sql deleted file mode 100644 index 9535588..0000000 --- a/script/sql/section_drop/12.curves.sql +++ /dev/null @@ -1,3 +0,0 @@ --- [CURVES] - -DROP TABLE CURVES; diff --git a/script/sql/section_drop/13.controls.sql b/script/sql/section_drop/13.controls.sql deleted file mode 100644 index 5333175..0000000 --- a/script/sql/section_drop/13.controls.sql +++ /dev/null @@ -1,9 +0,0 @@ --- [CONTROLS] - -DROP TABLE CONTROLS_3; - -DROP TABLE CONTROLS_2; - -DROP TABLE CONTROLS_1; - -DROP TYPE CONTROLS_1_PREP; diff --git a/script/sql/section_drop/14.rules.sql b/script/sql/section_drop/14.rules.sql deleted file mode 100644 index 7cc4bac..0000000 --- a/script/sql/section_drop/14.rules.sql +++ /dev/null @@ -1,3 +0,0 @@ --- [RULES] - -DROP TABLE RULES; diff --git a/script/sql/section_drop/15.energy.sql b/script/sql/section_drop/15.energy.sql deleted file mode 100644 index 1da9af8..0000000 --- a/script/sql/section_drop/15.energy.sql +++ /dev/null @@ -1,9 +0,0 @@ --- [ENERGY] - -DROP TABLE ENERGY_DEMAND_CHARGE; - -DROP TABLE ENERGY_PUMP; - -DROP TABLE ENERGY_GLOBAL; - -DROP TYPE ENERGY_PARAM; diff --git a/script/sql/section_drop/16.emitters.sql b/script/sql/section_drop/16.emitters.sql deleted file mode 100644 index 7aa46f8..0000000 --- a/script/sql/section_drop/16.emitters.sql +++ /dev/null @@ -1,3 +0,0 @@ --- [EMITTERS] - -DROP TABLE EMITTERS; diff --git a/script/sql/section_drop/17.quality.sql b/script/sql/section_drop/17.quality.sql deleted file mode 100644 index c15ac7d..0000000 --- a/script/sql/section_drop/17.quality.sql +++ /dev/null @@ -1,3 +0,0 @@ --- [QUALITY] - -DROP TABLE QUALITY; diff --git a/script/sql/section_drop/18.sources.sql b/script/sql/section_drop/18.sources.sql deleted file mode 100644 index e11b7ea..0000000 --- a/script/sql/section_drop/18.sources.sql +++ /dev/null @@ -1,5 +0,0 @@ --- [SOURCES] - -DROP TABLE SOURCES; - -DROP TYPE SOURCES_TYPE; diff --git a/script/sql/section_drop/19.reactions.sql b/script/sql/section_drop/19.reactions.sql deleted file mode 100644 index bcc9dc5..0000000 --- a/script/sql/section_drop/19.reactions.sql +++ /dev/null @@ -1,19 +0,0 @@ --- [REACTIONS] - -DROP TABLE REACTIONS_ROUGHNESS_CORRELATION; - -DROP TABLE REACTIONS_LIMITING_POTENTIAL; - -DROP TABLE REACTIONS_TANK; - -DROP TABLE REACTIONS_PIPE; - -DROP TYPE REACTIONS_PIPE_PARAM; - -DROP TABLE REACTIONS_GLOBAL; - -DROP TYPE REACTIONS_GLOBAL_PARAM; - -DROP TABLE REACTIONS_ORDER; - -DROP TYPE REACTIONS_ORDER_PARAM; diff --git a/script/sql/section_drop/2.junctions.sql b/script/sql/section_drop/2.junctions.sql deleted file mode 100644 index 8fc23c4..0000000 --- a/script/sql/section_drop/2.junctions.sql +++ /dev/null @@ -1,3 +0,0 @@ --- [JUNCTIONS] - -DROP TABLE JUNCTIONS; diff --git a/script/sql/section_drop/20.mixing.sql b/script/sql/section_drop/20.mixing.sql deleted file mode 100644 index 1434859..0000000 --- a/script/sql/section_drop/20.mixing.sql +++ /dev/null @@ -1,5 +0,0 @@ --- [MIXING] - -DROP TABLE MIXING; - -DROP TYPE MIXING_Model; diff --git a/script/sql/section_drop/21.times.sql b/script/sql/section_drop/21.times.sql deleted file mode 100644 index 0d45aed..0000000 --- a/script/sql/section_drop/21.times.sql +++ /dev/null @@ -1,3 +0,0 @@ --- [TIMES] - -DROP TABLE TIMES; diff --git a/script/sql/section_drop/22.report.sql b/script/sql/section_drop/22.report.sql deleted file mode 100644 index 58dcd51..0000000 --- a/script/sql/section_drop/22.report.sql +++ /dev/null @@ -1,3 +0,0 @@ --- [REPORT] - -DROP TABLE REPORT; diff --git a/script/sql/section_drop/23.options.sql b/script/sql/section_drop/23.options.sql deleted file mode 100644 index 69e9714..0000000 --- a/script/sql/section_drop/23.options.sql +++ /dev/null @@ -1,3 +0,0 @@ --- [OPTIONS] - -DROP TABLE OPTIONS; diff --git a/script/sql/section_drop/24.coordinates.sql b/script/sql/section_drop/24.coordinates.sql deleted file mode 100644 index 59f9f45..0000000 --- a/script/sql/section_drop/24.coordinates.sql +++ /dev/null @@ -1,7 +0,0 @@ --- [COORDINATES] - -DROP INDEX COORDINATES_GIST; - -DROP INDEX COORDINATES_SPGIST; - -DROP TABLE COORDINATES; diff --git a/script/sql/section_drop/25.vertices.sql b/script/sql/section_drop/25.vertices.sql deleted file mode 100644 index 1352d0d..0000000 --- a/script/sql/section_drop/25.vertices.sql +++ /dev/null @@ -1,3 +0,0 @@ --- [VERTICES] - -DROP TABLE VERTICES; diff --git a/script/sql/section_drop/26.labels.sql b/script/sql/section_drop/26.labels.sql deleted file mode 100644 index 70da104..0000000 --- a/script/sql/section_drop/26.labels.sql +++ /dev/null @@ -1,3 +0,0 @@ --- [LABELS] - -DROP TABLE LABELS; diff --git a/script/sql/section_drop/27.backdrop.sql b/script/sql/section_drop/27.backdrop.sql deleted file mode 100644 index 85f3bf2..0000000 --- a/script/sql/section_drop/27.backdrop.sql +++ /dev/null @@ -1,3 +0,0 @@ --- [BACKDROP] - -DROP TABLE BACKDROP; diff --git a/script/sql/section_drop/3.reservoirs.sql b/script/sql/section_drop/3.reservoirs.sql deleted file mode 100644 index 4fd1e46..0000000 --- a/script/sql/section_drop/3.reservoirs.sql +++ /dev/null @@ -1,3 +0,0 @@ --- [RESERVOIRS] - -DROP TABLE RESERVOIRS; diff --git a/script/sql/section_drop/4.tanks.sql b/script/sql/section_drop/4.tanks.sql deleted file mode 100644 index f9a4c27..0000000 --- a/script/sql/section_drop/4.tanks.sql +++ /dev/null @@ -1,5 +0,0 @@ --- [TANKS] - -DROP TABLE TANKS; - -DROP TYPE TANKS_OVERFLOW; diff --git a/script/sql/section_drop/5.pipes.sql b/script/sql/section_drop/5.pipes.sql deleted file mode 100644 index e01acd3..0000000 --- a/script/sql/section_drop/5.pipes.sql +++ /dev/null @@ -1,5 +0,0 @@ --- [PIPES] - -DROP TABLE PIPES; - -DROP TYPE PIPES_STATUS; diff --git a/script/sql/section_drop/6.pumps.sql b/script/sql/section_drop/6.pumps.sql deleted file mode 100644 index 2526df6..0000000 --- a/script/sql/section_drop/6.pumps.sql +++ /dev/null @@ -1,11 +0,0 @@ --- [PUMPS] - -DROP TABLE PUMPS_PROPERTY_PATTERN; - -DROP TABLE PUMPS_PROPERTY_HEAD; - -DROP TABLE PUMPS_PROPERTY_SPEED; - -DROP TABLE PUMPS_PROPERTY_POWER; - -DROP TABLE PUMPS; diff --git a/script/sql/section_drop/7.valves.sql b/script/sql/section_drop/7.valves.sql deleted file mode 100644 index ca001fe..0000000 --- a/script/sql/section_drop/7.valves.sql +++ /dev/null @@ -1,5 +0,0 @@ --- [VALVES] - -DROP TABLE VALVES; - -DROP TYPE VALVES_TYPE; diff --git a/script/sql/section_drop/8.tags.sql b/script/sql/section_drop/8.tags.sql deleted file mode 100644 index 78ec688..0000000 --- a/script/sql/section_drop/8.tags.sql +++ /dev/null @@ -1,5 +0,0 @@ --- [TAGS] - -DROP TABLE TAGS_LINK; - -DROP TABLE TAGS_NODE; diff --git a/script/sql/section_drop/9.demands.sql b/script/sql/section_drop/9.demands.sql deleted file mode 100644 index 7276cca..0000000 --- a/script/sql/section_drop/9.demands.sql +++ /dev/null @@ -1,3 +0,0 @@ --- [DEMANDS] - -DROP TABLE DEMANDS; diff --git a/script/template.py b/script/template.py new file mode 100644 index 0000000..c219802 --- /dev/null +++ b/script/template.py @@ -0,0 +1,102 @@ +import psycopg as pg + +sql_create = [ + "sql/create/0.base.sql", + "sql/create/1.title.sql", + "sql/create/2.junctions.sql", + "sql/create/3.reservoirs.sql", + "sql/create/4.tanks.sql", + "sql/create/5.pipes.sql", + "sql/create/6.pumps.sql", + "sql/create/7.valves.sql", + "sql/create/8.tags.sql", + "sql/create/9.demands.sql", + "sql/create/10.status.sql", + "sql/create/11.patterns.sql", + "sql/create/12.curves.sql", + "sql/create/13.controls.sql", + "sql/create/14.rules.sql", + "sql/create/15.energy.sql", + "sql/create/16.emitters.sql", + "sql/create/17.quality.sql", + "sql/create/18.sources.sql", + "sql/create/19.reactions.sql", + "sql/create/20.mixing.sql", + "sql/create/21.times.sql", + "sql/create/22.report.sql", + "sql/create/23.options.sql", + "sql/create/24.coordinates.sql", + "sql/create/25.vertices.sql", + "sql/create/26.labels.sql", + "sql/create/27.backdrop.sql", + "sql/create/28.end.sql", + "sql/create/operation.sql" +] + +sql_drop = [ + "sql/drop/operation.sql", + "sql/drop/28.end.sql", + "sql/drop/27.backdrop.sql", + "sql/drop/26.labels.sql", + "sql/drop/25.vertices.sql", + "sql/drop/24.coordinates.sql", + "sql/drop/23.options.sql", + "sql/drop/22.report.sql", + "sql/drop/21.times.sql", + "sql/drop/20.mixing.sql", + "sql/drop/19.reactions.sql", + "sql/drop/18.sources.sql", + "sql/drop/17.quality.sql", + "sql/drop/16.emitters.sql", + "sql/drop/15.energy.sql", + "sql/drop/14.rules.sql", + "sql/drop/13.controls.sql", + "sql/drop/12.curves.sql", + "sql/drop/11.patterns.sql", + "sql/drop/10.status.sql", + "sql/drop/9.demands.sql", + "sql/drop/8.tags.sql", + "sql/drop/7.valves.sql", + "sql/drop/6.pumps.sql", + "sql/drop/5.pipes.sql", + "sql/drop/4.tanks.sql", + "sql/drop/3.reservoirs.sql", + "sql/drop/2.junctions.sql", + "sql/drop/1.title.sql", + "sql/drop/0.base.sql" +] + +def create_template(): + with pg.connect(conninfo="dbname=postgres", autocommit=True) as conn: + with conn.cursor() as cur: + cur.execute("create database project") + with pg.connect(conninfo="dbname=project") as conn: + with conn.cursor() as cur: + for sql in sql_create: + with open(sql, "r") as f: + cur.execute(f.read()) + print(f'executed {sql}') + conn.commit() + +def have_template(): + with pg.connect(conninfo="dbname=postgres", autocommit=True) as conn: + with conn.cursor() as cur: + cur.execute("select * from pg_database where datname = 'project'") + return cur.rowcount > 0 + +def delete_template(): + with pg.connect(conninfo="dbname=project") as conn: + with conn.cursor() as cur: + for sql in sql_drop: + with open(sql, "r") as f: + cur.execute(f.read()) + print(f'executed {sql}') + conn.commit() + with pg.connect(conninfo="dbname=postgres", autocommit=True) as conn: + with conn.cursor() as cur: + cur.execute("drop database project") + +if __name__ == "__main__": + if (have_template()): + delete_template() + create_template() diff --git a/script/tjnetwork_admin.py b/script/tjnetwork_admin.py deleted file mode 100644 index c32f6e2..0000000 --- a/script/tjnetwork_admin.py +++ /dev/null @@ -1,89 +0,0 @@ -import psycopg as pg - -sql_create = [ - "sql/base_create.sql", - "sql/section_create/1.title.sql", - "sql/section_create/2.junctions.sql", - "sql/section_create/3.reservoirs.sql", - "sql/section_create/4.tanks.sql", - "sql/section_create/5.pipes.sql", - "sql/section_create/6.pumps.sql", - "sql/section_create/7.valves.sql", - "sql/section_create/8.tags.sql", - "sql/section_create/9.demands.sql", - "sql/section_create/10.status.sql", - "sql/section_create/11.patterns.sql", - "sql/section_create/12.curves.sql", - "sql/section_create/13.controls.sql", - "sql/section_create/14.rules.sql", - "sql/section_create/15.energy.sql", - "sql/section_create/16.emitters.sql", - "sql/section_create/17.quality.sql", - "sql/section_create/18.sources.sql", - "sql/section_create/19.reactions.sql", - "sql/section_create/20.mixing.sql", - "sql/section_create/21.times.sql", - "sql/section_create/22.report.sql", - "sql/section_create/23.options.sql", - "sql/section_create/24.coordinates.sql", - "sql/section_create/25.vertices.sql", - "sql/section_create/26.labels.sql", - "sql/section_create/27.backdrop.sql", - "sql/section_create/28.end.sql" -] - -sql_drop = [ - "sql/section_drop/28.end.sql", - "sql/section_drop/27.backdrop.sql", - "sql/section_drop/26.labels.sql", - "sql/section_drop/25.vertices.sql", - "sql/section_drop/24.coordinates.sql", - "sql/section_drop/23.options.sql", - "sql/section_drop/22.report.sql", - "sql/section_drop/21.times.sql", - "sql/section_drop/20.mixing.sql", - "sql/section_drop/19.reactions.sql", - "sql/section_drop/18.sources.sql", - "sql/section_drop/17.quality.sql", - "sql/section_drop/16.emitters.sql", - "sql/section_drop/15.energy.sql", - "sql/section_drop/14.rules.sql", - "sql/section_drop/13.controls.sql", - "sql/section_drop/12.curves.sql", - "sql/section_drop/11.patterns.sql", - "sql/section_drop/10.status.sql", - "sql/section_drop/9.demands.sql", - "sql/section_drop/8.tags.sql", - "sql/section_drop/7.valves.sql", - "sql/section_drop/6.pumps.sql", - "sql/section_drop/5.pipes.sql", - "sql/section_drop/4.tanks.sql", - "sql/section_drop/3.reservoirs.sql", - "sql/section_drop/2.junctions.sql", - "sql/section_drop/1.title.sql", - "sql/base_drop.sql" -] - -def create_template(): - with pg.connect(conninfo="dbname=postgres", autocommit=True) as conn: - with conn.cursor() as cur: - cur.execute(f"create database project") - with pg.connect(conninfo=f"dbname=project") as conn: - with conn.cursor() as cur: - for sql in sql_create: - with open(sql, "r") as f: - cur.execute(f.read()) - print(f'executed {sql}') - conn.commit() - -def delete_template(): - with pg.connect(conninfo=f"dbname=project") as conn: - with conn.cursor() as cur: - for sql in sql_drop: - with open(sql, "r") as f: - cur.execute(f.read()) - print(f'executed {sql}') - conn.commit() - with pg.connect(conninfo="dbname=postgres", autocommit=True) as conn: - with conn.cursor() as cur: - cur.execute(f"drop database project") diff --git a/tjnetwork_new.py b/tjnetwork_new.py new file mode 100644 index 0000000..c47d573 --- /dev/null +++ b/tjnetwork_new.py @@ -0,0 +1,171 @@ +from msilib.schema import CheckBox +import api + + +############################################################ +# ChangeSet +############################################################ + +ChangeSet = api.ChangeSet + + +############################################################ +# enum +############################################################ + +JUNCTION = api.JUNCTION +RESERVOIR = api.RESERVOIR +TANK = api.TANK +PIPE = api.PIPE +PUMP = api.PUMP +VALVE = api.VALVE + + +############################################################ +# project +############################################################ + +def have_project(name: str) -> bool: + return api.have_project(name) + +def create_project(name: str) -> None: + return api.create_project(name) + +def delete_project(name: str) -> None: + return api.delete_project(name) + +def is_project_open(name: str) -> bool: + return api.is_project_open(name) + +def open_project(name: str) -> None: + return api.open_project(name) + +def close_project(name: str) -> None: + return api.close_project(name) + +def copy_project(source: str, new: str) -> None: + return api.copy_project(source, new) + + +############################################################ +# operation +############################################################ + +def undo(name: str) -> None: + return api.undo(name) + +def redo(name: str) -> None: + return api.redo(name) + +def have_snapshot(name: str, tag: str) -> bool: + return api.have_snapshot(name, tag) + +def take_snapshot(name: str, tag: str) -> None: + return api.take_snapshot(name, tag) + +def pick_snapshot(name: str, tag: str) -> None: + return api.pick_snapshot(name, tag) + +def have_transaction(name: str) -> bool: + return api.have_transaction(name) + +def start_transaction(name: str, strict: bool = False) -> None: + return api.start_transaction(name, strict) + +def commit_transaction(name: str) -> None: + return api.commit_transaction(name) + +def abort_transaction(name: str) -> None: + return api.abort_transaction(name) + +############################################################ +# type +############################################################ + +def is_node(name: str, node_id: str) -> bool: + return api.is_node(name, node_id) + +def is_junction(name: str, node_id: str) -> bool: + return api.is_junction(name, node_id) + +def is_reservoir(name: str, node_id: str) -> bool: + return api.is_reservoir(name, node_id) + +def is_tank(name: str, node_id: str) -> bool: + return api.is_tank(name, node_id) + +def is_link(name: str, link_id: str) -> bool: + return api.is_link(name, link_id) + +def is_pipe(name: str, link_id: str) -> bool: + return api.is_pipe(name, link_id) + +def is_pump(name: str, link_id: str) -> bool: + return api.is_pump(name, link_id) + +def is_valve(name: str, link_id: str) -> bool: + return api.is_valve(name, link_id) + +def is_curve(name: str, curve_id: str) -> bool: + return api.is_curve(name, curve_id) + +def is_pattern(name: str, pattern_id: str) -> bool: + return api.is_pattern(name, pattern_id) + +def get_nodes(name: str) -> list[str]: + return api.get_nodes(name) + +def get_links(name: str) -> list[str]: + return api.get_links(name) + +def get_curves(name: str) -> list[str]: + return api.get_curves(name) + +def get_patterns(name: str) -> list[str]: + return api.get_patterns(name) + + +############################################################ +# title 1.[TITLE] +############################################################ + +def set_title(name: str, title: str) -> ChangeSet: + return api.set_title(name, title) + +def get_title(name: str) -> str: + return api.get_title(name) + + +############################################################ +# junction 2.[JUNCTIONS] +############################################################ + +def add_junction(name: str, junction_id: str, x: float, y: float, elevation: float) -> ChangeSet: + return api.add_junction(name, junction_id, x, y, elevation) + +def delete_junction(name: str, junction_id: str) -> ChangeSet: + return api.delete_junction(name, junction_id) + +def get_junction_elevation(name: str, junction_id: str) -> float | None: + return api.get_junction_elevation(name, junction_id) + +def get_junction_demand(name: str, junction_id: str) -> float | str | None: + return api.get_junction_demand(name, junction_id) + +def get_junction_pattern(name: str, junction_id: str) -> str | None: + return api.get_junction_pattern(name, junction_id) + +def get_junction_coord(name: str, junction_id: str) -> dict[str, float] | None: + return api.get_junction_coord(name, junction_id) + +def set_junction_elevation(name: str, junction_id: str, elevation: float) -> ChangeSet: + return api.set_junction_elevation(name, junction_id, elevation) + +def set_junction_demand(name: str, junction_id: str, demand: float) -> ChangeSet: + return api.set_junction_demand(name, junction_id, demand) + +def set_junction_pattern(name: str, junction_id: str, pattern: str) -> ChangeSet: + return api.set_junction_pattern(name, junction_id, pattern) + +def set_junction_coord(name: str, junction_id: str, x: float, y: float) -> ChangeSet: + return api.set_junction_coord(name, junction_id, x, y)