From 5ac03bb9bf798cc099485339992a6445127bfe0a Mon Sep 17 00:00:00 2001 From: wqy Date: Thu, 1 Sep 2022 22:01:10 +0800 Subject: [PATCH 01/52] Refine script --- script/admin.py | 3 +- .../{base_create.sql => create/0.base.sql} | 0 .../{section_create => create}/1.title.sql | 0 .../{section_create => create}/10.status.sql | 0 .../11.patterns.sql | 0 .../{section_create => create}/12.curves.sql | 0 .../13.controls.sql | 0 .../{section_create => create}/14.rules.sql | 0 .../{section_create => create}/15.energy.sql | 0 .../16.emitters.sql | 0 .../{section_create => create}/17.quality.sql | 0 .../{section_create => create}/18.sources.sql | 0 .../19.reactions.sql | 0 .../2.junctions.sql | 0 .../{section_create => create}/20.mixing.sql | 0 .../{section_create => create}/21.times.sql | 0 .../{section_create => create}/22.report.sql | 0 .../{section_create => create}/23.options.sql | 0 .../24.coordinates.sql | 0 .../25.vertices.sql | 0 .../{section_create => create}/26.labels.sql | 0 .../27.backdrop.sql | 0 .../sql/{section_create => create}/28.end.sql | 0 .../3.reservoirs.sql | 0 .../{section_create => create}/4.tanks.sql | 0 .../{section_create => create}/5.pipes.sql | 0 .../{section_create => create}/6.pumps.sql | 0 .../{section_create => create}/7.valves.sql | 0 .../sql/{section_create => create}/8.tags.sql | 0 .../{section_create => create}/9.demands.sql | 0 script/sql/{base_drop.sql => drop/0.base.sql} | 0 script/sql/{section_drop => drop}/1.title.sql | 0 .../sql/{section_drop => drop}/10.status.sql | 0 .../{section_drop => drop}/11.patterns.sql | 0 .../sql/{section_drop => drop}/12.curves.sql | 0 .../{section_drop => drop}/13.controls.sql | 0 .../sql/{section_drop => drop}/14.rules.sql | 0 .../sql/{section_drop => drop}/15.energy.sql | 0 .../{section_drop => drop}/16.emitters.sql | 0 .../sql/{section_drop => drop}/17.quality.sql | 0 .../sql/{section_drop => drop}/18.sources.sql | 0 .../{section_drop => drop}/19.reactions.sql | 0 .../{section_drop => drop}/2.junctions.sql | 0 .../sql/{section_drop => drop}/20.mixing.sql | 0 .../sql/{section_drop => drop}/21.times.sql | 0 .../sql/{section_drop => drop}/22.report.sql | 0 .../sql/{section_drop => drop}/23.options.sql | 0 .../{section_drop => drop}/24.coordinates.sql | 0 .../{section_drop => drop}/25.vertices.sql | 0 .../sql/{section_drop => drop}/26.labels.sql | 0 .../{section_drop => drop}/27.backdrop.sql | 0 script/sql/{section_drop => drop}/28.end.sql | 0 .../{section_drop => drop}/3.reservoirs.sql | 0 script/sql/{section_drop => drop}/4.tanks.sql | 0 script/sql/{section_drop => drop}/5.pipes.sql | 0 script/sql/{section_drop => drop}/6.pumps.sql | 0 .../sql/{section_drop => drop}/7.valves.sql | 0 script/sql/{section_drop => drop}/8.tags.sql | 0 .../sql/{section_drop => drop}/9.demands.sql | 0 script/tjnetwork_admin.py | 130 +++++++++--------- 60 files changed, 70 insertions(+), 63 deletions(-) rename script/sql/{base_create.sql => create/0.base.sql} (100%) rename script/sql/{section_create => create}/1.title.sql (100%) rename script/sql/{section_create => create}/10.status.sql (100%) rename script/sql/{section_create => create}/11.patterns.sql (100%) rename script/sql/{section_create => create}/12.curves.sql (100%) rename script/sql/{section_create => create}/13.controls.sql (100%) rename script/sql/{section_create => create}/14.rules.sql (100%) rename script/sql/{section_create => create}/15.energy.sql (100%) rename script/sql/{section_create => create}/16.emitters.sql (100%) rename script/sql/{section_create => create}/17.quality.sql (100%) rename script/sql/{section_create => create}/18.sources.sql (100%) rename script/sql/{section_create => create}/19.reactions.sql (100%) rename script/sql/{section_create => create}/2.junctions.sql (100%) rename script/sql/{section_create => create}/20.mixing.sql (100%) rename script/sql/{section_create => create}/21.times.sql (100%) rename script/sql/{section_create => create}/22.report.sql (100%) rename script/sql/{section_create => create}/23.options.sql (100%) rename script/sql/{section_create => create}/24.coordinates.sql (100%) rename script/sql/{section_create => create}/25.vertices.sql (100%) rename script/sql/{section_create => create}/26.labels.sql (100%) rename script/sql/{section_create => create}/27.backdrop.sql (100%) rename script/sql/{section_create => create}/28.end.sql (100%) rename script/sql/{section_create => create}/3.reservoirs.sql (100%) rename script/sql/{section_create => create}/4.tanks.sql (100%) rename script/sql/{section_create => create}/5.pipes.sql (100%) rename script/sql/{section_create => create}/6.pumps.sql (100%) rename script/sql/{section_create => create}/7.valves.sql (100%) rename script/sql/{section_create => create}/8.tags.sql (100%) rename script/sql/{section_create => create}/9.demands.sql (100%) rename script/sql/{base_drop.sql => drop/0.base.sql} (100%) rename script/sql/{section_drop => drop}/1.title.sql (100%) rename script/sql/{section_drop => drop}/10.status.sql (100%) rename script/sql/{section_drop => drop}/11.patterns.sql (100%) rename script/sql/{section_drop => drop}/12.curves.sql (100%) rename script/sql/{section_drop => drop}/13.controls.sql (100%) rename script/sql/{section_drop => drop}/14.rules.sql (100%) rename script/sql/{section_drop => drop}/15.energy.sql (100%) rename script/sql/{section_drop => drop}/16.emitters.sql (100%) rename script/sql/{section_drop => drop}/17.quality.sql (100%) rename script/sql/{section_drop => drop}/18.sources.sql (100%) rename script/sql/{section_drop => drop}/19.reactions.sql (100%) rename script/sql/{section_drop => drop}/2.junctions.sql (100%) rename script/sql/{section_drop => drop}/20.mixing.sql (100%) rename script/sql/{section_drop => drop}/21.times.sql (100%) rename script/sql/{section_drop => drop}/22.report.sql (100%) rename script/sql/{section_drop => drop}/23.options.sql (100%) rename script/sql/{section_drop => drop}/24.coordinates.sql (100%) rename script/sql/{section_drop => drop}/25.vertices.sql (100%) rename script/sql/{section_drop => drop}/26.labels.sql (100%) rename script/sql/{section_drop => drop}/27.backdrop.sql (100%) rename script/sql/{section_drop => drop}/28.end.sql (100%) rename script/sql/{section_drop => drop}/3.reservoirs.sql (100%) rename script/sql/{section_drop => drop}/4.tanks.sql (100%) rename script/sql/{section_drop => drop}/5.pipes.sql (100%) rename script/sql/{section_drop => drop}/6.pumps.sql (100%) rename script/sql/{section_drop => drop}/7.valves.sql (100%) rename script/sql/{section_drop => drop}/8.tags.sql (100%) rename script/sql/{section_drop => drop}/9.demands.sql (100%) diff --git a/script/admin.py b/script/admin.py index 74d7066..49df07a 100644 --- a/script/admin.py +++ b/script/admin.py @@ -1,5 +1,6 @@ from tjnetwork_admin import * if __name__ == "__main__": - delete_template() + 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 100% rename from script/sql/section_create/1.title.sql rename to script/sql/create/1.title.sql 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 100% rename from script/sql/section_create/24.coordinates.sql rename to script/sql/create/24.coordinates.sql 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/section_create/28.end.sql b/script/sql/create/28.end.sql similarity index 100% rename from script/sql/section_create/28.end.sql rename to script/sql/create/28.end.sql 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/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/section_drop/1.title.sql b/script/sql/drop/1.title.sql similarity index 100% rename from script/sql/section_drop/1.title.sql rename to script/sql/drop/1.title.sql diff --git a/script/sql/section_drop/10.status.sql b/script/sql/drop/10.status.sql similarity index 100% rename from script/sql/section_drop/10.status.sql rename to script/sql/drop/10.status.sql diff --git a/script/sql/section_drop/11.patterns.sql b/script/sql/drop/11.patterns.sql similarity index 100% rename from script/sql/section_drop/11.patterns.sql rename to script/sql/drop/11.patterns.sql diff --git a/script/sql/section_drop/12.curves.sql b/script/sql/drop/12.curves.sql similarity index 100% rename from script/sql/section_drop/12.curves.sql rename to script/sql/drop/12.curves.sql diff --git a/script/sql/section_drop/13.controls.sql b/script/sql/drop/13.controls.sql similarity index 100% rename from script/sql/section_drop/13.controls.sql rename to script/sql/drop/13.controls.sql diff --git a/script/sql/section_drop/14.rules.sql b/script/sql/drop/14.rules.sql similarity index 100% rename from script/sql/section_drop/14.rules.sql rename to script/sql/drop/14.rules.sql diff --git a/script/sql/section_drop/15.energy.sql b/script/sql/drop/15.energy.sql similarity index 100% rename from script/sql/section_drop/15.energy.sql rename to script/sql/drop/15.energy.sql diff --git a/script/sql/section_drop/16.emitters.sql b/script/sql/drop/16.emitters.sql similarity index 100% rename from script/sql/section_drop/16.emitters.sql rename to script/sql/drop/16.emitters.sql diff --git a/script/sql/section_drop/17.quality.sql b/script/sql/drop/17.quality.sql similarity index 100% rename from script/sql/section_drop/17.quality.sql rename to script/sql/drop/17.quality.sql diff --git a/script/sql/section_drop/18.sources.sql b/script/sql/drop/18.sources.sql similarity index 100% rename from script/sql/section_drop/18.sources.sql rename to script/sql/drop/18.sources.sql diff --git a/script/sql/section_drop/19.reactions.sql b/script/sql/drop/19.reactions.sql similarity index 100% rename from script/sql/section_drop/19.reactions.sql rename to script/sql/drop/19.reactions.sql diff --git a/script/sql/section_drop/2.junctions.sql b/script/sql/drop/2.junctions.sql similarity index 100% rename from script/sql/section_drop/2.junctions.sql rename to script/sql/drop/2.junctions.sql diff --git a/script/sql/section_drop/20.mixing.sql b/script/sql/drop/20.mixing.sql similarity index 100% rename from script/sql/section_drop/20.mixing.sql rename to script/sql/drop/20.mixing.sql diff --git a/script/sql/section_drop/21.times.sql b/script/sql/drop/21.times.sql similarity index 100% rename from script/sql/section_drop/21.times.sql rename to script/sql/drop/21.times.sql diff --git a/script/sql/section_drop/22.report.sql b/script/sql/drop/22.report.sql similarity index 100% rename from script/sql/section_drop/22.report.sql rename to script/sql/drop/22.report.sql diff --git a/script/sql/section_drop/23.options.sql b/script/sql/drop/23.options.sql similarity index 100% rename from script/sql/section_drop/23.options.sql rename to script/sql/drop/23.options.sql diff --git a/script/sql/section_drop/24.coordinates.sql b/script/sql/drop/24.coordinates.sql similarity index 100% rename from script/sql/section_drop/24.coordinates.sql rename to script/sql/drop/24.coordinates.sql diff --git a/script/sql/section_drop/25.vertices.sql b/script/sql/drop/25.vertices.sql similarity index 100% rename from script/sql/section_drop/25.vertices.sql rename to script/sql/drop/25.vertices.sql diff --git a/script/sql/section_drop/26.labels.sql b/script/sql/drop/26.labels.sql similarity index 100% rename from script/sql/section_drop/26.labels.sql rename to script/sql/drop/26.labels.sql diff --git a/script/sql/section_drop/27.backdrop.sql b/script/sql/drop/27.backdrop.sql similarity index 100% rename from script/sql/section_drop/27.backdrop.sql rename to script/sql/drop/27.backdrop.sql diff --git a/script/sql/section_drop/28.end.sql b/script/sql/drop/28.end.sql similarity index 100% rename from script/sql/section_drop/28.end.sql rename to script/sql/drop/28.end.sql diff --git a/script/sql/section_drop/3.reservoirs.sql b/script/sql/drop/3.reservoirs.sql similarity index 100% rename from script/sql/section_drop/3.reservoirs.sql rename to script/sql/drop/3.reservoirs.sql diff --git a/script/sql/section_drop/4.tanks.sql b/script/sql/drop/4.tanks.sql similarity index 100% rename from script/sql/section_drop/4.tanks.sql rename to script/sql/drop/4.tanks.sql diff --git a/script/sql/section_drop/5.pipes.sql b/script/sql/drop/5.pipes.sql similarity index 100% rename from script/sql/section_drop/5.pipes.sql rename to script/sql/drop/5.pipes.sql diff --git a/script/sql/section_drop/6.pumps.sql b/script/sql/drop/6.pumps.sql similarity index 100% rename from script/sql/section_drop/6.pumps.sql rename to script/sql/drop/6.pumps.sql diff --git a/script/sql/section_drop/7.valves.sql b/script/sql/drop/7.valves.sql similarity index 100% rename from script/sql/section_drop/7.valves.sql rename to script/sql/drop/7.valves.sql diff --git a/script/sql/section_drop/8.tags.sql b/script/sql/drop/8.tags.sql similarity index 100% rename from script/sql/section_drop/8.tags.sql rename to script/sql/drop/8.tags.sql diff --git a/script/sql/section_drop/9.demands.sql b/script/sql/drop/9.demands.sql similarity index 100% rename from script/sql/section_drop/9.demands.sql rename to script/sql/drop/9.demands.sql diff --git a/script/tjnetwork_admin.py b/script/tjnetwork_admin.py index c32f6e2..219a40c 100644 --- a/script/tjnetwork_admin.py +++ b/script/tjnetwork_admin.py @@ -1,74 +1,74 @@ 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/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_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" + "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(f"create database project") - with pg.connect(conninfo=f"dbname=project") as conn: + 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: @@ -76,8 +76,14 @@ def create_template(): 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=f"dbname=project") as conn: + with pg.connect(conninfo="dbname=project") as conn: with conn.cursor() as cur: for sql in sql_drop: with open(sql, "r") as f: @@ -86,4 +92,4 @@ def delete_template(): conn.commit() with pg.connect(conninfo="dbname=postgres", autocommit=True) as conn: with conn.cursor() as cur: - cur.execute(f"drop database project") + cur.execute("drop database project") From a177edbfcbe8f35dd0b53e7b2e28989d34a90e22 Mon Sep 17 00:00:00 2001 From: wqy Date: Thu, 1 Sep 2022 22:03:09 +0800 Subject: [PATCH 02/52] Add base api --- api/_0_base.py | 71 ++++++++++++++++++++++++++++++++++++++++++++++ api/_connection.py | 3 ++ api/_project.py | 28 ++++++++++++++++++ api/test.py | 14 +++++++++ 4 files changed, 116 insertions(+) create mode 100644 api/_0_base.py create mode 100644 api/_connection.py create mode 100644 api/_project.py create mode 100644 api/test.py diff --git a/api/_0_base.py b/api/_0_base.py new file mode 100644 index 0000000..5e3f5b0 --- /dev/null +++ b/api/_0_base.py @@ -0,0 +1,71 @@ +from _connection import _conn_dict as conn + +_NODE = "_NODE" +_LINK = "_LINK" +_CURVE = "_CURVE" +_PATTERN = "_PATTERN" + +JUNCTION = "JUNCTION" +RESERVOIR = "RESERVOIR" +TANK = "TANK" +PIPE = "PIPE" +PUMP = "PUMP" +VALVE = "VALVE" + + +# add + +def add_node(name: str, id: str, type: str) -> None: + with conn[name].cursor() as cur: + cur.execute(f"INSERT INTO _NODE (ID, Type) VALUES ('{id}', '{type}')") + +def add_link(name: str, id: str, type: str) -> None: + with conn[name].cursor() as cur: + cur.execute(f"INSERT INTO _LINK (ID, Type) VALUES ('{id}', '{type}')") + +def add_curve(name: str, id: str) -> None: + with conn[name].cursor() as cur: + cur.execute(f"INSERT INTO _CURVE (ID) VALUES ('{id}')") + +def add_pattern(name: str, id: str) -> None: + with conn[name].cursor() as cur: + cur.execute(f"INSERT INTO _PATTERN (ID) VALUES ('{id}')") + + +# have + +def _have_impl(name: str, id: str, table: str) -> bool: + with conn[name].cursor() as cur: + cur.execute(f"SELECT * FROM {table} WHERE ID = '{id}'") + return cur.rowcount > 0 + +def have_node(name: str, id: str) -> bool: + return _have_impl(name, id, _NODE) + +def have_link(name: str, id: str) -> bool: + return _have_impl(name, id, _LINK) + +def have_curve(name: str, id: str) -> bool: + return _have_impl(name, id, _CURVE) + +def have_pattern(name: str, id: str) -> bool: + return _have_impl(name, id, _PATTERN) + + +# delete + +def _delete_impl(name: str, id: str, table: str) -> None: + with conn[name].cursor() as cur: + cur.execute(f"DELETE FROM {table} WHERE ID = '{id}'") + +def delete_node(name: str, id: str) -> None: + return _delete_impl(name, id, _NODE) + +def delete_link(name: str, id: str) -> None: + return _delete_impl(name, id, _LINK) + +def delete_curve(name: str, id: str) -> None: + return _delete_impl(name, id, _CURVE) + +def delete_pattern(name: str, id: str) -> None: + return _delete_impl(name, id, _PATTERN) diff --git a/api/_connection.py b/api/_connection.py new file mode 100644 index 0000000..fa81f21 --- /dev/null +++ b/api/_connection.py @@ -0,0 +1,3 @@ +import psycopg as pg + +_conn_dict : dict[str, pg.Connection] = {} \ No newline at end of file diff --git a/api/_project.py b/api/_project.py new file mode 100644 index 0000000..d0ef3c9 --- /dev/null +++ b/api/_project.py @@ -0,0 +1,28 @@ +import psycopg as pg +from _connection import _conn_dict as conn + +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 create_project(name: str) -> None: + with pg.connect(conninfo="dbname=postgres", autocommit=True) as conn: + with conn.cursor() as cur: + cur.execute(f"create database {name} with template = project") + +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/test.py b/api/test.py new file mode 100644 index 0000000..30ceed0 --- /dev/null +++ b/api/test.py @@ -0,0 +1,14 @@ +from _project import * +from _0_base import * + +open_project("net") +add_node("net", "n-1", JUNCTION) +if have_node("net", "n-1"): + delete_node("net", "n-1") +print(have_node("net", "n-1")) + +open_project("net") +add_link("net", "l-1", PIPE) +if have_link("net", "l-1"): + delete_link("net", "l-1") +print(have_link("net", "l-1")) \ No newline at end of file From c42d705232d1656653e44bc77552870a4a5171db Mon Sep 17 00:00:00 2001 From: wqy Date: Thu, 1 Sep 2022 22:06:22 +0800 Subject: [PATCH 03/52] Add base test --- api/_0_base_test.py | 27 +++++++++++++++++++++++++++ api/test.py | 14 -------------- 2 files changed, 27 insertions(+), 14 deletions(-) create mode 100644 api/_0_base_test.py delete mode 100644 api/test.py diff --git a/api/_0_base_test.py b/api/_0_base_test.py new file mode 100644 index 0000000..c7181f4 --- /dev/null +++ b/api/_0_base_test.py @@ -0,0 +1,27 @@ +from fileinput import close +from _project import * +from _0_base import * + +open_project("net") + +add_node("net", "n-1", JUNCTION) +if have_node("net", "n-1"): + delete_node("net", "n-1") +print(have_node("net", "n-1")) + +add_link("net", "l-1", PIPE) +if have_link("net", "l-1"): + delete_link("net", "l-1") +print(have_link("net", "l-1")) + +add_curve("net", "c-1") +if have_curve("net", "c-1"): + delete_curve("net", "c-1") +print(have_curve("net", "c-1")) + +add_pattern("net", "p-1") +if have_pattern("net", "p-1"): + delete_pattern("net", "p-1") +print(have_pattern("net", "p-1")) + +close_project("net") \ No newline at end of file diff --git a/api/test.py b/api/test.py deleted file mode 100644 index 30ceed0..0000000 --- a/api/test.py +++ /dev/null @@ -1,14 +0,0 @@ -from _project import * -from _0_base import * - -open_project("net") -add_node("net", "n-1", JUNCTION) -if have_node("net", "n-1"): - delete_node("net", "n-1") -print(have_node("net", "n-1")) - -open_project("net") -add_link("net", "l-1", PIPE) -if have_link("net", "l-1"): - delete_link("net", "l-1") -print(have_link("net", "l-1")) \ No newline at end of file From 5e2b5acd9a78c9fe4ad63c7ac58b1105b7cb1126 Mon Sep 17 00:00:00 2001 From: wqy Date: Thu, 1 Sep 2022 22:20:38 +0800 Subject: [PATCH 04/52] Add section [TITLE] --- api/_0_base_test.py | 1 - api/_1_title.py | 19 +++++++++++++++++++ api/_1_title_test.py | 12 ++++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 api/_1_title.py create mode 100644 api/_1_title_test.py diff --git a/api/_0_base_test.py b/api/_0_base_test.py index c7181f4..0c79cfd 100644 --- a/api/_0_base_test.py +++ b/api/_0_base_test.py @@ -1,4 +1,3 @@ -from fileinput import close from _project import * from _0_base import * diff --git a/api/_1_title.py b/api/_1_title.py new file mode 100644 index 0000000..b6f2ab9 --- /dev/null +++ b/api/_1_title.py @@ -0,0 +1,19 @@ +from _connection import _conn_dict as conn + +def have_title(name: str) -> bool: + with conn[name].cursor() as cur: + cur.execute(f"SELECT * FROM TITLE") + return cur.rowcount > 0 + +def set_title(name: str, value: str) -> None: + if have_title(name): + with conn[name].cursor() as cur: + cur.execute(f"UPDATE TITLE SET Value = {value}") + else: + with conn[name].cursor() as cur: + cur.execute(f"INSERT INTO TITLE (Value) VALUES ('{value}')") + +def unset_title(name: str) -> None: + with conn[name].cursor() as cur: + cur.execute(f"TRUNCATE TITLE") + return cur.rowcount > 0 diff --git a/api/_1_title_test.py b/api/_1_title_test.py new file mode 100644 index 0000000..51c2011 --- /dev/null +++ b/api/_1_title_test.py @@ -0,0 +1,12 @@ +from _project import * +from _1_title import * + +open_project("net") + +print(have_title("net")) +set_title("net", "xxx") +print(have_title("net")) +unset_title("net") +print(have_title("net")) + +close_project("net") \ No newline at end of file From 1c5d0984bf60311f3c29e3387e7f16f897e398c3 Mon Sep 17 00:00:00 2001 From: wqy Date: Thu, 1 Sep 2022 22:38:01 +0800 Subject: [PATCH 05/52] Get title --- api/_1_title.py | 11 ++++++++++- api/_1_title_test.py | 17 +++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/api/_1_title.py b/api/_1_title.py index b6f2ab9..b3571d5 100644 --- a/api/_1_title.py +++ b/api/_1_title.py @@ -1,4 +1,5 @@ from _connection import _conn_dict as conn +from psycopg.rows import dict_row def have_title(name: str) -> bool: with conn[name].cursor() as cur: @@ -8,11 +9,19 @@ def have_title(name: str) -> bool: def set_title(name: str, value: str) -> None: if have_title(name): with conn[name].cursor() as cur: - cur.execute(f"UPDATE TITLE SET Value = {value}") + cur.execute(f"UPDATE TITLE SET Value = '{value}'") else: with conn[name].cursor() as cur: cur.execute(f"INSERT INTO TITLE (Value) VALUES ('{value}')") +def get_title(name: str) -> str: + with conn[name].cursor(row_factory=dict_row) as cur: + cur.execute(f"SELECT * FROM TITLE") + if cur.rowcount > 0: + return cur.fetchone()['value'] + else: + return "" + def unset_title(name: str) -> None: with conn[name].cursor() as cur: cur.execute(f"TRUNCATE TITLE") diff --git a/api/_1_title_test.py b/api/_1_title_test.py index 51c2011..bc0f9af 100644 --- a/api/_1_title_test.py +++ b/api/_1_title_test.py @@ -4,9 +4,26 @@ from _1_title import * open_project("net") print(have_title("net")) +print(get_title("net")) + set_title("net", "xxx") print(have_title("net")) +print(get_title("net")) + +set_title("net", "xxxx") +print(have_title("net")) +print(get_title("net")) + unset_title("net") print(have_title("net")) +print(get_title("net")) + +set_title("net", "xxxx") +print(have_title("net")) +print(get_title("net")) + +unset_title("net") +print(have_title("net")) +print(get_title("net")) close_project("net") \ No newline at end of file From 1fa8a4ac2a55ab8b2901cd1e933af77a2f8486c7 Mon Sep 17 00:00:00 2001 From: wqy Date: Thu, 1 Sep 2022 22:56:19 +0800 Subject: [PATCH 06/52] Get base --- api/_0_base.py | 23 +++++++++++++++++++++++ api/_0_base_test.py | 4 ++++ 2 files changed, 27 insertions(+) diff --git a/api/_0_base.py b/api/_0_base.py index 5e3f5b0..5778388 100644 --- a/api/_0_base.py +++ b/api/_0_base.py @@ -1,4 +1,5 @@ from _connection import _conn_dict as conn +from psycopg.rows import dict_row _NODE = "_NODE" _LINK = "_LINK" @@ -52,6 +53,28 @@ def have_pattern(name: str, id: str) -> bool: return _have_impl(name, id, _PATTERN) +# get +def _get_impl(name: str, id: str, table: str) -> dict[str, str]: + with conn[name].cursor(row_factory=dict_row) as cur: + cur.execute(f"SELECT * FROM {table} WHERE ID = '{id}'") + if cur.rowcount > 0: + return cur.fetchone() + else: + return {} + +def get_node(name: str, id: str) -> dict[str, str]: + return _get_impl(name, id, _NODE) + +def get_link(name: str, id: str) -> dict[str, str]: + return _get_impl(name, id, _LINK) + +def get_curve(name: str, id: str) -> dict[str, str]: + return _get_impl(name, id, _CURVE) + +def get_pattern(name: str, id: str) -> dict[str, str]: + return _get_impl(name, id, _PATTERN) + + # delete def _delete_impl(name: str, id: str, table: str) -> None: diff --git a/api/_0_base_test.py b/api/_0_base_test.py index 0c79cfd..7743ce7 100644 --- a/api/_0_base_test.py +++ b/api/_0_base_test.py @@ -4,21 +4,25 @@ from _0_base import * open_project("net") add_node("net", "n-1", JUNCTION) +print(get_node("net", "n-1")) if have_node("net", "n-1"): delete_node("net", "n-1") print(have_node("net", "n-1")) add_link("net", "l-1", PIPE) +print(get_link("net", "l-1")) if have_link("net", "l-1"): delete_link("net", "l-1") print(have_link("net", "l-1")) add_curve("net", "c-1") +print(get_curve("net", "c-1")) if have_curve("net", "c-1"): delete_curve("net", "c-1") print(have_curve("net", "c-1")) add_pattern("net", "p-1") +print(get_pattern("net", "p-1")) if have_pattern("net", "p-1"): delete_pattern("net", "p-1") print(have_pattern("net", "p-1")) From b8eed02366bef077eff117fbf2006ca4243d97ce Mon Sep 17 00:00:00 2001 From: wqy Date: Thu, 1 Sep 2022 23:32:14 +0800 Subject: [PATCH 07/52] Start to implement database transaction tree --- script/sql/create/operation.sql | 17 +++++++++++++++++ script/sql/drop/operation.sql | 3 +++ 2 files changed, 20 insertions(+) create mode 100644 script/sql/create/operation.sql create mode 100644 script/sql/drop/operation.sql diff --git a/script/sql/create/operation.sql b/script/sql/create/operation.sql new file mode 100644 index 0000000..559a298 --- /dev/null +++ b/script/sql/create/operation.sql @@ -0,0 +1,17 @@ +CREATE TABLE OPERATION +( + ID SERIAL PRIMARY KEY +, REDO TEXT NOT NULL +, UNDO TEXT NOT NULL +, PARENT INTEGER REFERENCES OPERATION(ID) +, REDOCHILD INTEGER REFERENCES OPERATION(ID) +); + +INSERT INTO OPERATION (ID, REDO, UNDO) VALUES (0, '', ''); + +CREATE TABLE CURRENTOPERATION +( + ID INTEGER PRIMARY KEY REFERENCES OPERATION(ID) +); + +INSERT INTO CURRENTOPERATION (ID) VALUES (0); diff --git a/script/sql/drop/operation.sql b/script/sql/drop/operation.sql new file mode 100644 index 0000000..604b7f0 --- /dev/null +++ b/script/sql/drop/operation.sql @@ -0,0 +1,3 @@ +DROP TABLE CURRENTOPERATION; + +DROP TABLE OPERATION; From a7294bf6a385187ea8753f82a1a07fecfb3773a6 Mon Sep 17 00:00:00 2001 From: wqy Date: Fri, 2 Sep 2022 18:30:23 +0800 Subject: [PATCH 08/52] Update operation tree --- script/sql/create/operation.sql | 14 +++++++------- script/sql/drop/operation.sql | 2 +- script/tjnetwork_admin.py | 4 +++- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/script/sql/create/operation.sql b/script/sql/create/operation.sql index 559a298..9c9429d 100644 --- a/script/sql/create/operation.sql +++ b/script/sql/create/operation.sql @@ -1,17 +1,17 @@ CREATE TABLE OPERATION ( ID SERIAL PRIMARY KEY -, REDO TEXT NOT NULL -, UNDO TEXT NOT NULL -, PARENT INTEGER REFERENCES OPERATION(ID) -, REDOCHILD INTEGER REFERENCES OPERATION(ID) +, 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, '', ''); +INSERT INTO OPERATION (ID, Redo, Undo) VALUES (0, '', ''); -CREATE TABLE CURRENTOPERATION +CREATE TABLE CURRENT_OPERATION ( ID INTEGER PRIMARY KEY REFERENCES OPERATION(ID) ); -INSERT INTO CURRENTOPERATION (ID) VALUES (0); +INSERT INTO CURRENT_OPERATION (ID) VALUES (0); diff --git a/script/sql/drop/operation.sql b/script/sql/drop/operation.sql index 604b7f0..df5074c 100644 --- a/script/sql/drop/operation.sql +++ b/script/sql/drop/operation.sql @@ -1,3 +1,3 @@ -DROP TABLE CURRENTOPERATION; +DROP TABLE CURRENT_OPERATION; DROP TABLE OPERATION; diff --git a/script/tjnetwork_admin.py b/script/tjnetwork_admin.py index 219a40c..9758143 100644 --- a/script/tjnetwork_admin.py +++ b/script/tjnetwork_admin.py @@ -29,10 +29,12 @@ sql_create = [ "sql/create/25.vertices.sql", "sql/create/26.labels.sql", "sql/create/27.backdrop.sql", - "sql/create/28.end.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", From 248f56cbea8b0069cbca90daa6fa3a6b474bb542 Mon Sep 17 00:00:00 2001 From: wqy Date: Fri, 2 Sep 2022 18:30:46 +0800 Subject: [PATCH 09/52] Always has title --- script/sql/create/1.title.sql | 2 ++ 1 file changed, 2 insertions(+) diff --git a/script/sql/create/1.title.sql b/script/sql/create/1.title.sql index 0a48eff..c00e981 100644 --- a/script/sql/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 (''); From 4474f12d2cb5dd046d99257924ed95091785d781 Mon Sep 17 00:00:00 2001 From: wqy Date: Fri, 2 Sep 2022 18:32:22 +0800 Subject: [PATCH 10/52] Implement operation tree in database --- api/operation.py | 76 +++++++++++++++++++++++++++++++++++++++++++ api/test/operation.py | 25 ++++++++++++++ 2 files changed, 101 insertions(+) create mode 100644 api/operation.py create mode 100644 api/test/operation.py diff --git a/api/operation.py b/api/operation.py new file mode 100644 index 0000000..09bd709 --- /dev/null +++ b/api/operation.py @@ -0,0 +1,76 @@ +from psycopg.rows import dict_row +from api.connection import g_conn_dict as conn + +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: + cur.execute("select max(id) from operation") + parent = int(cur.fetchone()['max']) + cur.execute(f"insert into operation (id, redo, undo, parent) values (default, '{redo}', '{undo}', {parent})") + return parent + 1 + +# 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: str, child: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) -> None: + curr = _get_current_operation(name) + row = _query_undo(name, curr) + undo = row['undo'] + if undo == '': + print("nothing to undo!") + return + + parent = int(row['parent']) + _set_redo_child(name, parent, curr) + + _execute(name, undo) + _update_current_operation(name, curr, parent) + +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) diff --git a/api/test/operation.py b/api/test/operation.py new file mode 100644 index 0000000..d0b3b7e --- /dev/null +++ b/api/test/operation.py @@ -0,0 +1,25 @@ +from api.project import * +from api.s0_base import * + +p = "test_operation" + +if is_project_open(p): + close_project(p) + +if have_project(p): + delete_project(p) + +create_project(p) +open_project(p) + +add_node(p, "n-1", JUNCTION) +add_node(p, "n-2", JUNCTION) +add_node(p, "n-3", JUNCTION) +execute_undo(p) +add_node(p, "n-3", JUNCTION) +add_node(p, "n-4", JUNCTION) +execute_undo(p) +execute_redo(p) + +close_project(p) +delete_project(p) From bed574155859558ffaebb9ffaf370ced8d6e6609 Mon Sep 17 00:00:00 2001 From: wqy Date: Fri, 2 Sep 2022 18:33:33 +0800 Subject: [PATCH 11/52] Fix module in terms of python style --- api/_0_base.py | 94 ----------------------- api/_0_base_test.py | 30 -------- api/_1_title.py | 28 ------- api/_1_title_test.py | 29 ------- api/_connection.py | 3 - api/connection.py | 3 + api/{_project.py => project.py} | 2 +- api/s0_base.py | 131 ++++++++++++++++++++++++++++++++ api/s1_title.py | 11 +++ api/test/s0_base.py | 44 +++++++++++ api/test/s1_title.py | 24 ++++++ 11 files changed, 214 insertions(+), 185 deletions(-) delete mode 100644 api/_0_base.py delete mode 100644 api/_0_base_test.py delete mode 100644 api/_1_title.py delete mode 100644 api/_1_title_test.py delete mode 100644 api/_connection.py create mode 100644 api/connection.py rename api/{_project.py => project.py} (95%) create mode 100644 api/s0_base.py create mode 100644 api/s1_title.py create mode 100644 api/test/s0_base.py create mode 100644 api/test/s1_title.py diff --git a/api/_0_base.py b/api/_0_base.py deleted file mode 100644 index 5778388..0000000 --- a/api/_0_base.py +++ /dev/null @@ -1,94 +0,0 @@ -from _connection import _conn_dict as conn -from psycopg.rows import dict_row - -_NODE = "_NODE" -_LINK = "_LINK" -_CURVE = "_CURVE" -_PATTERN = "_PATTERN" - -JUNCTION = "JUNCTION" -RESERVOIR = "RESERVOIR" -TANK = "TANK" -PIPE = "PIPE" -PUMP = "PUMP" -VALVE = "VALVE" - - -# add - -def add_node(name: str, id: str, type: str) -> None: - with conn[name].cursor() as cur: - cur.execute(f"INSERT INTO _NODE (ID, Type) VALUES ('{id}', '{type}')") - -def add_link(name: str, id: str, type: str) -> None: - with conn[name].cursor() as cur: - cur.execute(f"INSERT INTO _LINK (ID, Type) VALUES ('{id}', '{type}')") - -def add_curve(name: str, id: str) -> None: - with conn[name].cursor() as cur: - cur.execute(f"INSERT INTO _CURVE (ID) VALUES ('{id}')") - -def add_pattern(name: str, id: str) -> None: - with conn[name].cursor() as cur: - cur.execute(f"INSERT INTO _PATTERN (ID) VALUES ('{id}')") - - -# have - -def _have_impl(name: str, id: str, table: str) -> bool: - with conn[name].cursor() as cur: - cur.execute(f"SELECT * FROM {table} WHERE ID = '{id}'") - return cur.rowcount > 0 - -def have_node(name: str, id: str) -> bool: - return _have_impl(name, id, _NODE) - -def have_link(name: str, id: str) -> bool: - return _have_impl(name, id, _LINK) - -def have_curve(name: str, id: str) -> bool: - return _have_impl(name, id, _CURVE) - -def have_pattern(name: str, id: str) -> bool: - return _have_impl(name, id, _PATTERN) - - -# get -def _get_impl(name: str, id: str, table: str) -> dict[str, str]: - with conn[name].cursor(row_factory=dict_row) as cur: - cur.execute(f"SELECT * FROM {table} WHERE ID = '{id}'") - if cur.rowcount > 0: - return cur.fetchone() - else: - return {} - -def get_node(name: str, id: str) -> dict[str, str]: - return _get_impl(name, id, _NODE) - -def get_link(name: str, id: str) -> dict[str, str]: - return _get_impl(name, id, _LINK) - -def get_curve(name: str, id: str) -> dict[str, str]: - return _get_impl(name, id, _CURVE) - -def get_pattern(name: str, id: str) -> dict[str, str]: - return _get_impl(name, id, _PATTERN) - - -# delete - -def _delete_impl(name: str, id: str, table: str) -> None: - with conn[name].cursor() as cur: - cur.execute(f"DELETE FROM {table} WHERE ID = '{id}'") - -def delete_node(name: str, id: str) -> None: - return _delete_impl(name, id, _NODE) - -def delete_link(name: str, id: str) -> None: - return _delete_impl(name, id, _LINK) - -def delete_curve(name: str, id: str) -> None: - return _delete_impl(name, id, _CURVE) - -def delete_pattern(name: str, id: str) -> None: - return _delete_impl(name, id, _PATTERN) diff --git a/api/_0_base_test.py b/api/_0_base_test.py deleted file mode 100644 index 7743ce7..0000000 --- a/api/_0_base_test.py +++ /dev/null @@ -1,30 +0,0 @@ -from _project import * -from _0_base import * - -open_project("net") - -add_node("net", "n-1", JUNCTION) -print(get_node("net", "n-1")) -if have_node("net", "n-1"): - delete_node("net", "n-1") -print(have_node("net", "n-1")) - -add_link("net", "l-1", PIPE) -print(get_link("net", "l-1")) -if have_link("net", "l-1"): - delete_link("net", "l-1") -print(have_link("net", "l-1")) - -add_curve("net", "c-1") -print(get_curve("net", "c-1")) -if have_curve("net", "c-1"): - delete_curve("net", "c-1") -print(have_curve("net", "c-1")) - -add_pattern("net", "p-1") -print(get_pattern("net", "p-1")) -if have_pattern("net", "p-1"): - delete_pattern("net", "p-1") -print(have_pattern("net", "p-1")) - -close_project("net") \ No newline at end of file diff --git a/api/_1_title.py b/api/_1_title.py deleted file mode 100644 index b3571d5..0000000 --- a/api/_1_title.py +++ /dev/null @@ -1,28 +0,0 @@ -from _connection import _conn_dict as conn -from psycopg.rows import dict_row - -def have_title(name: str) -> bool: - with conn[name].cursor() as cur: - cur.execute(f"SELECT * FROM TITLE") - return cur.rowcount > 0 - -def set_title(name: str, value: str) -> None: - if have_title(name): - with conn[name].cursor() as cur: - cur.execute(f"UPDATE TITLE SET Value = '{value}'") - else: - with conn[name].cursor() as cur: - cur.execute(f"INSERT INTO TITLE (Value) VALUES ('{value}')") - -def get_title(name: str) -> str: - with conn[name].cursor(row_factory=dict_row) as cur: - cur.execute(f"SELECT * FROM TITLE") - if cur.rowcount > 0: - return cur.fetchone()['value'] - else: - return "" - -def unset_title(name: str) -> None: - with conn[name].cursor() as cur: - cur.execute(f"TRUNCATE TITLE") - return cur.rowcount > 0 diff --git a/api/_1_title_test.py b/api/_1_title_test.py deleted file mode 100644 index bc0f9af..0000000 --- a/api/_1_title_test.py +++ /dev/null @@ -1,29 +0,0 @@ -from _project import * -from _1_title import * - -open_project("net") - -print(have_title("net")) -print(get_title("net")) - -set_title("net", "xxx") -print(have_title("net")) -print(get_title("net")) - -set_title("net", "xxxx") -print(have_title("net")) -print(get_title("net")) - -unset_title("net") -print(have_title("net")) -print(get_title("net")) - -set_title("net", "xxxx") -print(have_title("net")) -print(get_title("net")) - -unset_title("net") -print(have_title("net")) -print(get_title("net")) - -close_project("net") \ No newline at end of file diff --git a/api/_connection.py b/api/_connection.py deleted file mode 100644 index fa81f21..0000000 --- a/api/_connection.py +++ /dev/null @@ -1,3 +0,0 @@ -import psycopg as pg - -_conn_dict : dict[str, pg.Connection] = {} \ 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/_project.py b/api/project.py similarity index 95% rename from api/_project.py rename to api/project.py index d0ef3c9..4f176f3 100644 --- a/api/_project.py +++ b/api/project.py @@ -1,5 +1,5 @@ import psycopg as pg -from _connection import _conn_dict as conn +from api.connection import g_conn_dict as conn def have_project(name: str) -> bool: with pg.connect(conninfo="dbname=postgres", autocommit=True) as conn: diff --git a/api/s0_base.py b/api/s0_base.py new file mode 100644 index 0000000..8af8da7 --- /dev/null +++ b/api/s0_base.py @@ -0,0 +1,131 @@ +from psycopg.rows import dict_row +from api.connection import g_conn_dict as conn +from api.operation import * + +_NODE = "_node" +_LINK = "_link" +_CURVE = "_curve" +_PATTERN = "_pattern" + +JUNCTION = "JUNCTION" +RESERVOIR = "RESERVOIR" +TANK = "TANK" +PIPE = "PIPE" +PUMP = "PUMP" +VALVE = "VALVE" + + +# add + +def _add_id_type(name: str, id: str, type: str, table: str) -> None: + with conn[name].cursor() as cur: + sql = f"insert into {table} (id, type) values ('{id}', '{type}')" + cur.execute(sql) + redo = sql.replace("'", '"') + undo = f'delete from {table} where id = "{id}"' + add_operation(name, redo, undo) + +def _add_id(name: str, id: str, table: str) -> None: + with conn[name].cursor() as cur: + sql = f"insert into {table} (id) values ('{id}')" + cur.execute(sql) + redo = sql.replace("'", '"') + undo = f'delete from {table} where id = "{id}"' + add_operation(name, redo, undo) + +def add_node(name: str, id: str, type: str) -> None: + return _add_id_type(name, id, type, _NODE) + +def add_link(name: str, id: str, type: str) -> None: + return _add_id_type(name, id, type, _LINK) + +def add_curve(name: str, id: str) -> None: + return _add_id(name, id, _CURVE) + +def add_pattern(name: str, id: str) -> None: + return _add_id(name, id, _PATTERN) + + +# have + +def _have_impl(name: str, id: str, table: str) -> bool: + with conn[name].cursor() as cur: + cur.execute(f"select * from {table} where id = '{id}'") + return cur.rowcount > 0 + +def have_node(name: str, id: str) -> bool: + return _have_impl(name, id, _NODE) + +def have_link(name: str, id: str) -> bool: + return _have_impl(name, id, _LINK) + +def have_curve(name: str, id: str) -> bool: + return _have_impl(name, id, _CURVE) + +def have_pattern(name: str, id: str) -> bool: + return _have_impl(name, id, _PATTERN) + + +# get +def _get_impl(name: str, id: str, table: str) -> dict[str, str]: + with conn[name].cursor(row_factory=dict_row) as cur: + cur.execute(f"select * from {table} where id = '{id}'") + if cur.rowcount > 0: + return cur.fetchone() + else: + return {} + +def get_node(name: str, id: str) -> dict[str, str]: + return _get_impl(name, id, _NODE) + +def get_link(name: str, id: str) -> dict[str, str]: + return _get_impl(name, id, _LINK) + +def get_curve(name: str, id: str) -> dict[str, str]: + return _get_impl(name, id, _CURVE) + +def get_pattern(name: str, id: str) -> dict[str, str]: + return _get_impl(name, id, _PATTERN) + + +# delete + +def _delete_id_type(name: str, id: str, type: str, table: str) -> None: + with conn[name].cursor() as cur: + sql = f"delete from {table} where id = '{id}'" + cur.execute(sql) + redo = sql.replace("'", '"') + undo = f'insert into {table} (id, type) values ("{id}", "{type}")' + add_operation(name, redo, undo) + +def _delete_id(name:str, id: str, table: str) -> None: + with conn[name].cursor() as cur: + sql = f"delete from {table} where id = '{id}'" + cur.execute(sql) + redo = sql.replace("'", '"') + undo = f'insert into {table} (id) values ("{id}")' + add_operation(name, redo, undo) + +def delete_node(name: str, id: str) -> None: + row = get_node(name, id) + if row == {}: + return + return _delete_id_type(name, id, row['type'], _NODE) + +def delete_link(name: str, id: str) -> None: + row = get_link(name, id) + if row == {}: + return + return _delete_id_type(name, id, row['type'], _LINK) + +def delete_curve(name: str, id: str) -> None: + row = get_curve(name, id) + if row == {}: + return + return _delete_id(name, id, _CURVE) + +def delete_pattern(name: str, id: str) -> None: + row = get_pattern(name, id) + if row == {}: + return + return _delete_id(name, id, _PATTERN) diff --git a/api/s1_title.py b/api/s1_title.py new file mode 100644 index 0000000..7da5e2a --- /dev/null +++ b/api/s1_title.py @@ -0,0 +1,11 @@ +from psycopg.rows import dict_row +from api.connection import g_conn_dict as conn + +def set_title(name: str, value: str) -> None: + with conn[name].cursor() as cur: + cur.execute(f"update title set value = '{value}'") + +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'] diff --git a/api/test/s0_base.py b/api/test/s0_base.py new file mode 100644 index 0000000..9cc8270 --- /dev/null +++ b/api/test/s0_base.py @@ -0,0 +1,44 @@ +from api.project import * +from api.s0_base import * + +p = "test_s0_base" +n_1 = "n_1" +l_1 = "l_1" +c_1 = "c_1" +p_1 = "p_1" + +if is_project_open(p): + close_project(p) + +if have_project(p): + delete_project(p) + +create_project(p) +open_project(p) + +add_node(p, n_1, JUNCTION) +print(get_node(p, n_1)) +if have_node(p, n_1): + delete_node(p, n_1) +print(have_node(p, n_1)) + +add_link(p, l_1, PIPE) +print(get_link(p, l_1)) +if have_link(p, l_1): + delete_link(p, l_1) +print(have_link(p, l_1)) + +add_curve(p, c_1) +print(get_curve(p, c_1)) +if have_curve(p, c_1): + delete_curve(p, c_1) +print(have_curve(p, c_1)) + +add_pattern(p, p_1) +print(get_pattern(p, p_1)) +if have_pattern(p, p_1): + delete_pattern(p, p_1) +print(have_pattern(p, p_1)) + +close_project(p) +delete_project(p) diff --git a/api/test/s1_title.py b/api/test/s1_title.py new file mode 100644 index 0000000..8136359 --- /dev/null +++ b/api/test/s1_title.py @@ -0,0 +1,24 @@ +from api.project import * +from api.s1_title import * + +p = "test_s1_title" + +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)) + +set_title(p, "test") +print(get_title(p)) + +close_project(p) +delete_project(p) From d90f7d6f8ebc1f050a00ca5162cb4b04a62e0233 Mon Sep 17 00:00:00 2001 From: wqy Date: Fri, 2 Sep 2022 18:38:11 +0800 Subject: [PATCH 12/52] Add new api in terms of database rather than epanet/memory --- tjnetwork_new.py | 77 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 tjnetwork_new.py diff --git a/tjnetwork_new.py b/tjnetwork_new.py new file mode 100644 index 0000000..f0ac154 --- /dev/null +++ b/tjnetwork_new.py @@ -0,0 +1,77 @@ +import api.project as _p +import api.operation as _o +import api.s0_base as _0 +import api.s1_title as _1 + +JUNCTION = "JUNCTION" +RESERVOIR = "RESERVOIR" +TANK = "TANK" +PIPE = "PIPE" +PUMP = "PUMP" +VALVE = "VALVE" + +def have_project(name: str) -> bool: + return _p.have_project(name) + +def create_project(name: str) -> None: + return _p.create_project(name) + +def delete_project(name: str) -> None: + return _p.delete_project(name) + +def is_project_open(name: str) -> bool: + return _p.is_project_open(name) + +def open_project(name: str) -> None: + return _p.open_project(name) + +def close_project(name: str) -> None: + return _p.close_project(name) + +def undo(name: str) -> None: + return _o.execute_undo(name) + +def redo(name: str) -> None: + return _o.execute_redo(name) + +def add_node(name: str, node_id: str, node_type: str) -> None: + return _0.add_node(name, node_id, node_type) + +def have_node(name: str, node_id: str) -> bool: + return _0.have_node(name, node_id) + +def delete_node(name: str, node_id: str) -> None: + return _0.delete_node(name, node_id) + +def add_link(name: str, link_id: str, link_type: str) -> None: + return _0.add_link(name, link_id, link_type) + +def have_link(name: str, link_id: str) -> bool: + return _0.have_link(name, link_id) + +def delete_link(name: str, link_id: str) -> None: + return _0.delete_link(name, link_id) + +def add_curve(name: str, curve_id: str) -> None: + return _0.add_curve(name, curve_id) + +def have_curve(name: str, curve_id: str) -> bool: + return _0.have_curve(name, curve_id) + +def delete_curve(name: str, curve_id: str) -> None: + return _0.delete_curve(name, curve_id) + +def add_pattern(name: str, pattern_id: str) -> None: + return _0.add_pattern(name, pattern_id) + +def have_pattern(name: str, pattern_id: str) -> bool: + return _0.have_pattern(name, pattern_id) + +def delete_pattern(name: str, pattern_id: str) -> None: + return _0.delete_pattern(name, pattern_id) + +def set_title(name: str, title: str) -> None: + return _1.set_title(name, title) + +def get_title(name: str) -> str: + return _1.get_title(name) From 49bea820b8caa895b1dbe075fce12f29543934a2 Mon Sep 17 00:00:00 2001 From: wqy Date: Fri, 2 Sep 2022 19:01:59 +0800 Subject: [PATCH 13/52] Add api to query type --- api/operation.py | 2 +- api/s0_base.py | 18 ++++++++++++++++++ tjnetwork_new.py | 18 ++++++++++++++++++ 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/api/operation.py b/api/operation.py index 09bd709..fd7a157 100644 --- a/api/operation.py +++ b/api/operation.py @@ -34,7 +34,7 @@ def _query_redo(name: str, id: str) -> dict[str, str]: cur.execute(f"select redo from operation where id = {id}") return cur.fetchone()['redo'] -def _set_redo_child(name: str, id: str, child:str) -> None: +def _set_redo_child(name: str, id: str, child: str) -> None: with conn[name].cursor() as cur: cur.execute(f"update operation set redo_child = {child} where id = {id}") diff --git a/api/s0_base.py b/api/s0_base.py index 8af8da7..9ee9055 100644 --- a/api/s0_base.py +++ b/api/s0_base.py @@ -78,9 +78,27 @@ def _get_impl(name: str, id: str, table: str) -> dict[str, str]: def get_node(name: str, id: str) -> dict[str, str]: return _get_impl(name, id, _NODE) +def is_junction(name: str, id: str) -> bool: + return get_node(name, id)['type'] == JUNCTION + +def is_reservoir(name: str, id: str) -> bool: + return get_node(name, id)['type'] == RESERVOIR + +def is_tank(name: str, id: str) -> bool: + return get_node(name, id)['type'] == TANK + def get_link(name: str, id: str) -> dict[str, str]: return _get_impl(name, id, _LINK) +def is_pipe(name: str, id: str) -> bool: + return get_link(name, id)['type'] == PIPE + +def is_pump(name: str, id: str) -> bool: + return get_link(name, id)['type'] == PUMP + +def is_valve(name: str, id: str) -> bool: + return get_link(name, id)['type'] == VALVE + def get_curve(name: str, id: str) -> dict[str, str]: return _get_impl(name, id, _CURVE) diff --git a/tjnetwork_new.py b/tjnetwork_new.py index f0ac154..f44aeee 100644 --- a/tjnetwork_new.py +++ b/tjnetwork_new.py @@ -40,6 +40,15 @@ def add_node(name: str, node_id: str, node_type: str) -> None: def have_node(name: str, node_id: str) -> bool: return _0.have_node(name, node_id) +def is_junction(name: str, node_id: str) -> bool: + return _0.is_junction(name, node_id) + +def is_reservoir(name: str, node_id: str) -> bool: + return _0.is_reservoir(name, node_id) + +def is_tank(name: str, node_id: str) -> bool: + return _0.is_tank(name, node_id) + def delete_node(name: str, node_id: str) -> None: return _0.delete_node(name, node_id) @@ -49,6 +58,15 @@ def add_link(name: str, link_id: str, link_type: str) -> None: def have_link(name: str, link_id: str) -> bool: return _0.have_link(name, link_id) +def is_pipe(name: str, link_id: str) -> bool: + return _0.is_pipe(name, link_id) + +def is_pump(name: str, link_id: str) -> bool: + return _0.is_pump(name, link_id) + +def is_valve(name: str, link_id: str) -> bool: + return _0.is_valve(name, link_id) + def delete_link(name: str, link_id: str) -> None: return _0.delete_link(name, link_id) From 71c454c8cf7ac5008e6f941d2c08a192393731ba Mon Sep 17 00:00:00 2001 From: wqy Date: Fri, 2 Sep 2022 20:28:03 +0800 Subject: [PATCH 14/52] Multiple operation into a api --- api/s0_base.py | 47 ++++++++++++++++++++----------- script/sql/create/2.junctions.sql | 2 +- 2 files changed, 32 insertions(+), 17 deletions(-) diff --git a/api/s0_base.py b/api/s0_base.py index 9ee9055..a9b9e4b 100644 --- a/api/s0_base.py +++ b/api/s0_base.py @@ -14,23 +14,32 @@ PIPE = "PIPE" PUMP = "PUMP" VALVE = "VALVE" +nodeTables = { JUNCTION: "junctions", RESERVOIR: "reservoirs", TANK: "tanks" } +linkTables = { PIPE: "pipes", PUMP: "pumps", VALVE: "valves" } +typeTales = { _NODE: nodeTables, _LINK: linkTables } # add -def _add_id_type(name: str, id: str, type: str, table: str) -> None: +def _add_id_type(name: str, id: str, type: str, base_type: str) -> None: with conn[name].cursor() as cur: - sql = f"insert into {table} (id, type) values ('{id}', '{type}')" + sql = f"insert into {base_type} (id, type) values ('{id}', '{type}'); insert into {typeTales[base_type][type]} (id) values ('{id}');" + if base_type == _NODE: + sql += f" insert into coordinates (node) values ('{id}');" cur.execute(sql) + redo = sql.replace("'", '"') - undo = f'delete from {table} where id = "{id}"' + undo = "" + if base_type == _NODE: + undo = f'delete from coordinates where node = "{id}"; ' + undo += f'delete from {typeTales[base_type][type]} where id = "{id}"; delete from {base_type} where id = "{id}";' add_operation(name, redo, undo) -def _add_id(name: str, id: str, table: str) -> None: +def _add_id(name: str, id: str, base_type: str) -> None: with conn[name].cursor() as cur: - sql = f"insert into {table} (id) values ('{id}')" + sql = f"insert into {base_type} (id) values ('{id}')" cur.execute(sql) redo = sql.replace("'", '"') - undo = f'delete from {table} where id = "{id}"' + undo = f'delete from {base_type} where id = "{id}"' add_operation(name, redo, undo) def add_node(name: str, id: str, type: str) -> None: @@ -48,9 +57,9 @@ def add_pattern(name: str, id: str) -> None: # have -def _have_impl(name: str, id: str, table: str) -> bool: +def _have_impl(name: str, id: str, base_type: str) -> bool: with conn[name].cursor() as cur: - cur.execute(f"select * from {table} where id = '{id}'") + cur.execute(f"select * from {base_type} where id = '{id}'") return cur.rowcount > 0 def have_node(name: str, id: str) -> bool: @@ -67,9 +76,9 @@ def have_pattern(name: str, id: str) -> bool: # get -def _get_impl(name: str, id: str, table: str) -> dict[str, str]: +def _get_impl(name: str, id: str, base_type: str) -> dict[str, str]: with conn[name].cursor(row_factory=dict_row) as cur: - cur.execute(f"select * from {table} where id = '{id}'") + cur.execute(f"select * from {base_type} where id = '{id}'") if cur.rowcount > 0: return cur.fetchone() else: @@ -108,20 +117,26 @@ def get_pattern(name: str, id: str) -> dict[str, str]: # delete -def _delete_id_type(name: str, id: str, type: str, table: str) -> None: +def _delete_id_type(name: str, id: str, type: str, base_type: str) -> None: with conn[name].cursor() as cur: - sql = f"delete from {table} where id = '{id}'" + sql = "" + if base_type == _NODE: + sql = f"delete from coordinates where node = '{id}'; " + sql += f"delete from {typeTales[base_type][type]} where id = '{id}'; delete from {base_type} where id = '{id}';" cur.execute(sql) + redo = sql.replace("'", '"') - undo = f'insert into {table} (id, type) values ("{id}", "{type}")' + undo = f'insert into {base_type} (id, type) values ("{id}", "{type}"); insert into {typeTales[base_type][type]} (id) values ("{id}");' + if base_type == _NODE: + undo += f' insert into coordinates (node) values ("{id}");' add_operation(name, redo, undo) -def _delete_id(name:str, id: str, table: str) -> None: +def _delete_id(name:str, id: str, base_type: str) -> None: with conn[name].cursor() as cur: - sql = f"delete from {table} where id = '{id}'" + sql = f"delete from {base_type} where id = '{id}'" cur.execute(sql) redo = sql.replace("'", '"') - undo = f'insert into {table} (id) values ("{id}")' + undo = f'insert into {base_type} (id) values ("{id}")' add_operation(name, redo, undo) def delete_node(name: str, id: str) -> None: diff --git a/script/sql/create/2.junctions.sql b/script/sql/create/2.junctions.sql index 42ea2f3..a2abb89 100644 --- a/script/sql/create/2.junctions.sql +++ b/script/sql/create/2.junctions.sql @@ -3,7 +3,7 @@ CREATE TABLE JUNCTIONS ( ID VARCHAR(32) PRIMARY KEY REFERENCES _NODE(ID) -, Elevation NUMERIC NOT NULL +, Elevation NUMERIC NOT NULL DEFAULT(0.0) , Demand NUMERIC , Pattern VARCHAR(32) REFERENCES _PATTERN(ID) ); From 99958a24ca136e640f1d7bb5612b40ea616520d7 Mon Sep 17 00:00:00 2001 From: wqy Date: Fri, 2 Sep 2022 20:28:37 +0800 Subject: [PATCH 15/52] Update admin script => template --- script/admin.py | 6 ------ script/{tjnetwork_admin.py => template.py} | 5 +++++ 2 files changed, 5 insertions(+), 6 deletions(-) delete mode 100644 script/admin.py rename script/{tjnetwork_admin.py => template.py} (96%) diff --git a/script/admin.py b/script/admin.py deleted file mode 100644 index 49df07a..0000000 --- a/script/admin.py +++ /dev/null @@ -1,6 +0,0 @@ -from tjnetwork_admin import * - -if __name__ == "__main__": - if (have_template()): - delete_template() - create_template() diff --git a/script/tjnetwork_admin.py b/script/template.py similarity index 96% rename from script/tjnetwork_admin.py rename to script/template.py index 9758143..c219802 100644 --- a/script/tjnetwork_admin.py +++ b/script/template.py @@ -95,3 +95,8 @@ def delete_template(): 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() From 013d037b0a9d2d629653d37cbe995e6f032876ab Mon Sep 17 00:00:00 2001 From: wqy Date: Fri, 2 Sep 2022 20:29:22 +0800 Subject: [PATCH 16/52] Use __init__.py to manage api module --- api/__init__.py | 14 ++++++ api/operation.py | 2 +- api/project.py | 2 +- api/s0_base.py | 4 +- api/s1_title.py | 2 +- api/{test/operation.py => t_operation.py} | 4 +- api/{test/s0_base.py => t_s0_base.py} | 4 +- api/{test/s1_title.py => t_s1_title.py} | 4 +- tjnetwork_new.py | 61 +++++++++++------------ 9 files changed, 54 insertions(+), 43 deletions(-) create mode 100644 api/__init__.py rename api/{test/operation.py => t_operation.py} (87%) rename api/{test/s0_base.py => t_s0_base.py} (93%) rename api/{test/s1_title.py => t_s1_title.py} (84%) diff --git a/api/__init__.py b/api/__init__.py new file mode 100644 index 0000000..5fa4083 --- /dev/null +++ b/api/__init__.py @@ -0,0 +1,14 @@ +from api.s0_base import delete_curve, delete_node, delete_pattern +from project import have_project, create_project, delete_project +from project import is_project_open, open_project, close_project + +from operation import execute_undo as undo +from operation import execute_redo as redo + +from s0_base import add_node, add_link, add_curve, add_pattern +from s0_base import have_node, have_link, have_curve, have_pattern +from s0_base import is_junction, is_reservoir, is_tank +from s0_base import is_pipe, is_pump, is_valve +from s0_base import delete_node, delete_link, delete_curve, delete_pattern + +from s1_title import set_title, get_title \ No newline at end of file diff --git a/api/operation.py b/api/operation.py index fd7a157..2ef9e56 100644 --- a/api/operation.py +++ b/api/operation.py @@ -1,5 +1,5 @@ from psycopg.rows import dict_row -from api.connection import g_conn_dict as conn +from connection import g_conn_dict as conn def _get_current_operation(name: str) -> int: with conn[name].cursor(row_factory=dict_row) as cur: diff --git a/api/project.py b/api/project.py index 4f176f3..6f65b85 100644 --- a/api/project.py +++ b/api/project.py @@ -1,5 +1,5 @@ import psycopg as pg -from api.connection import g_conn_dict as conn +from connection import g_conn_dict as conn def have_project(name: str) -> bool: with pg.connect(conninfo="dbname=postgres", autocommit=True) as conn: diff --git a/api/s0_base.py b/api/s0_base.py index a9b9e4b..83be9fd 100644 --- a/api/s0_base.py +++ b/api/s0_base.py @@ -1,6 +1,6 @@ from psycopg.rows import dict_row -from api.connection import g_conn_dict as conn -from api.operation import * +from connection import g_conn_dict as conn +from operation import * _NODE = "_node" _LINK = "_link" diff --git a/api/s1_title.py b/api/s1_title.py index 7da5e2a..54b6bba 100644 --- a/api/s1_title.py +++ b/api/s1_title.py @@ -1,5 +1,5 @@ from psycopg.rows import dict_row -from api.connection import g_conn_dict as conn +from connection import g_conn_dict as conn def set_title(name: str, value: str) -> None: with conn[name].cursor() as cur: diff --git a/api/test/operation.py b/api/t_operation.py similarity index 87% rename from api/test/operation.py rename to api/t_operation.py index d0b3b7e..b83e041 100644 --- a/api/test/operation.py +++ b/api/t_operation.py @@ -1,5 +1,5 @@ -from api.project import * -from api.s0_base import * +from project import * +from s0_base import * p = "test_operation" diff --git a/api/test/s0_base.py b/api/t_s0_base.py similarity index 93% rename from api/test/s0_base.py rename to api/t_s0_base.py index 9cc8270..7c679e8 100644 --- a/api/test/s0_base.py +++ b/api/t_s0_base.py @@ -1,5 +1,5 @@ -from api.project import * -from api.s0_base import * +from project import * +from s0_base import * p = "test_s0_base" n_1 = "n_1" diff --git a/api/test/s1_title.py b/api/t_s1_title.py similarity index 84% rename from api/test/s1_title.py rename to api/t_s1_title.py index 8136359..700c931 100644 --- a/api/test/s1_title.py +++ b/api/t_s1_title.py @@ -1,5 +1,5 @@ -from api.project import * -from api.s1_title import * +from project import * +from s1_title import * p = "test_s1_title" diff --git a/tjnetwork_new.py b/tjnetwork_new.py index f44aeee..040e364 100644 --- a/tjnetwork_new.py +++ b/tjnetwork_new.py @@ -1,7 +1,4 @@ -import api.project as _p -import api.operation as _o -import api.s0_base as _0 -import api.s1_title as _1 +import api JUNCTION = "JUNCTION" RESERVOIR = "RESERVOIR" @@ -11,85 +8,85 @@ PUMP = "PUMP" VALVE = "VALVE" def have_project(name: str) -> bool: - return _p.have_project(name) + return api.have_project(name) def create_project(name: str) -> None: - return _p.create_project(name) + return api.create_project(name) def delete_project(name: str) -> None: - return _p.delete_project(name) + return api.delete_project(name) def is_project_open(name: str) -> bool: - return _p.is_project_open(name) + return api.is_project_open(name) def open_project(name: str) -> None: - return _p.open_project(name) + return api.open_project(name) def close_project(name: str) -> None: - return _p.close_project(name) + return api.close_project(name) def undo(name: str) -> None: - return _o.execute_undo(name) + return api.undo(name) def redo(name: str) -> None: - return _o.execute_redo(name) + return api.redo(name) def add_node(name: str, node_id: str, node_type: str) -> None: - return _0.add_node(name, node_id, node_type) + return api.add_node(name, node_id, node_type) def have_node(name: str, node_id: str) -> bool: - return _0.have_node(name, node_id) + return api.have_node(name, node_id) def is_junction(name: str, node_id: str) -> bool: - return _0.is_junction(name, node_id) + return api.is_junction(name, node_id) def is_reservoir(name: str, node_id: str) -> bool: - return _0.is_reservoir(name, node_id) + return api.is_reservoir(name, node_id) def is_tank(name: str, node_id: str) -> bool: - return _0.is_tank(name, node_id) + return api.is_tank(name, node_id) def delete_node(name: str, node_id: str) -> None: - return _0.delete_node(name, node_id) + return api.delete_node(name, node_id) def add_link(name: str, link_id: str, link_type: str) -> None: - return _0.add_link(name, link_id, link_type) + return api.add_link(name, link_id, link_type) def have_link(name: str, link_id: str) -> bool: - return _0.have_link(name, link_id) + return api.have_link(name, link_id) def is_pipe(name: str, link_id: str) -> bool: - return _0.is_pipe(name, link_id) + return api.is_pipe(name, link_id) def is_pump(name: str, link_id: str) -> bool: - return _0.is_pump(name, link_id) + return api.is_pump(name, link_id) def is_valve(name: str, link_id: str) -> bool: - return _0.is_valve(name, link_id) + return api.is_valve(name, link_id) def delete_link(name: str, link_id: str) -> None: - return _0.delete_link(name, link_id) + return api.delete_link(name, link_id) def add_curve(name: str, curve_id: str) -> None: - return _0.add_curve(name, curve_id) + return api.add_curve(name, curve_id) def have_curve(name: str, curve_id: str) -> bool: - return _0.have_curve(name, curve_id) + return api.have_curve(name, curve_id) def delete_curve(name: str, curve_id: str) -> None: - return _0.delete_curve(name, curve_id) + return api.delete_curve(name, curve_id) def add_pattern(name: str, pattern_id: str) -> None: - return _0.add_pattern(name, pattern_id) + return api.add_pattern(name, pattern_id) def have_pattern(name: str, pattern_id: str) -> bool: - return _0.have_pattern(name, pattern_id) + return api.have_pattern(name, pattern_id) def delete_pattern(name: str, pattern_id: str) -> None: - return _0.delete_pattern(name, pattern_id) + return api.delete_pattern(name, pattern_id) def set_title(name: str, title: str) -> None: - return _1.set_title(name, title) + return api.set_title(name, title) def get_title(name: str) -> str: - return _1.get_title(name) + return api.get_title(name) From 99bcdde8ca46b87bf2c759090b35ba4923847324 Mon Sep 17 00:00:00 2001 From: wqy Date: Fri, 2 Sep 2022 20:31:02 +0800 Subject: [PATCH 17/52] Clean __init__.py --- api/__init__.py | 1 - 1 file changed, 1 deletion(-) diff --git a/api/__init__.py b/api/__init__.py index 5fa4083..088d131 100644 --- a/api/__init__.py +++ b/api/__init__.py @@ -1,4 +1,3 @@ -from api.s0_base import delete_curve, delete_node, delete_pattern from project import have_project, create_project, delete_project from project import is_project_open, open_project, close_project From 069e49430ec0c392a39df8bd0cdf835787329edb Mon Sep 17 00:00:00 2001 From: wqy Date: Sat, 3 Sep 2022 00:31:46 +0800 Subject: [PATCH 18/52] Enhance table junctions and coordinates --- script/sql/create/2.junctions.sql | 2 +- script/sql/create/24.coordinates.sql | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/script/sql/create/2.junctions.sql b/script/sql/create/2.junctions.sql index a2abb89..42ea2f3 100644 --- a/script/sql/create/2.junctions.sql +++ b/script/sql/create/2.junctions.sql @@ -3,7 +3,7 @@ CREATE TABLE JUNCTIONS ( ID VARCHAR(32) PRIMARY KEY REFERENCES _NODE(ID) -, Elevation NUMERIC NOT NULL DEFAULT(0.0) +, Elevation NUMERIC NOT NULL , Demand NUMERIC , Pattern VARCHAR(32) REFERENCES _PATTERN(ID) ); diff --git a/script/sql/create/24.coordinates.sql b/script/sql/create/24.coordinates.sql index 2bad5dc..5b6ed6d 100644 --- a/script/sql/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); From 3eb7aa5a46b6a24093ed1118908637e89effa233 Mon Sep 17 00:00:00 2001 From: wqy Date: Sat, 3 Sep 2022 00:32:30 +0800 Subject: [PATCH 19/52] No undo redo for project api --- api/project.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/api/project.py b/api/project.py index 6f65b85..3fa1b83 100644 --- a/api/project.py +++ b/api/project.py @@ -1,6 +1,8 @@ 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: From 2955dc94d796cdebf85dc65ffe6da972fa694444 Mon Sep 17 00:00:00 2001 From: wqy Date: Sat, 3 Sep 2022 00:33:31 +0800 Subject: [PATCH 20/52] Remove some tests --- api/t_operation.py | 25 ------------------------- api/t_s0_base.py | 44 -------------------------------------------- 2 files changed, 69 deletions(-) delete mode 100644 api/t_operation.py delete mode 100644 api/t_s0_base.py diff --git a/api/t_operation.py b/api/t_operation.py deleted file mode 100644 index b83e041..0000000 --- a/api/t_operation.py +++ /dev/null @@ -1,25 +0,0 @@ -from project import * -from s0_base import * - -p = "test_operation" - -if is_project_open(p): - close_project(p) - -if have_project(p): - delete_project(p) - -create_project(p) -open_project(p) - -add_node(p, "n-1", JUNCTION) -add_node(p, "n-2", JUNCTION) -add_node(p, "n-3", JUNCTION) -execute_undo(p) -add_node(p, "n-3", JUNCTION) -add_node(p, "n-4", JUNCTION) -execute_undo(p) -execute_redo(p) - -close_project(p) -delete_project(p) diff --git a/api/t_s0_base.py b/api/t_s0_base.py deleted file mode 100644 index 7c679e8..0000000 --- a/api/t_s0_base.py +++ /dev/null @@ -1,44 +0,0 @@ -from project import * -from s0_base import * - -p = "test_s0_base" -n_1 = "n_1" -l_1 = "l_1" -c_1 = "c_1" -p_1 = "p_1" - -if is_project_open(p): - close_project(p) - -if have_project(p): - delete_project(p) - -create_project(p) -open_project(p) - -add_node(p, n_1, JUNCTION) -print(get_node(p, n_1)) -if have_node(p, n_1): - delete_node(p, n_1) -print(have_node(p, n_1)) - -add_link(p, l_1, PIPE) -print(get_link(p, l_1)) -if have_link(p, l_1): - delete_link(p, l_1) -print(have_link(p, l_1)) - -add_curve(p, c_1) -print(get_curve(p, c_1)) -if have_curve(p, c_1): - delete_curve(p, c_1) -print(have_curve(p, c_1)) - -add_pattern(p, p_1) -print(get_pattern(p, p_1)) -if have_pattern(p, p_1): - delete_pattern(p, p_1) -print(have_pattern(p, p_1)) - -close_project(p) -delete_project(p) From 9b2b54e9abd9d195f91ad46680f061ce2a24d557 Mon Sep 17 00:00:00 2001 From: wqy Date: Sat, 3 Sep 2022 00:33:58 +0800 Subject: [PATCH 21/52] Base is readonly --- api/s0_base.py | 155 ++++++++----------------------------------------- 1 file changed, 23 insertions(+), 132 deletions(-) diff --git a/api/s0_base.py b/api/s0_base.py index 83be9fd..bbefa7b 100644 --- a/api/s0_base.py +++ b/api/s0_base.py @@ -1,6 +1,5 @@ -from psycopg.rows import dict_row +from psycopg.rows import dict_row, Row from connection import g_conn_dict as conn -from operation import * _NODE = "_node" _LINK = "_link" @@ -14,151 +13,43 @@ PIPE = "PIPE" PUMP = "PUMP" VALVE = "VALVE" -nodeTables = { JUNCTION: "junctions", RESERVOIR: "reservoirs", TANK: "tanks" } -linkTables = { PIPE: "pipes", PUMP: "pumps", VALVE: "valves" } -typeTales = { _NODE: nodeTables, _LINK: linkTables } - -# add - -def _add_id_type(name: str, id: str, type: str, base_type: str) -> None: - with conn[name].cursor() as cur: - sql = f"insert into {base_type} (id, type) values ('{id}', '{type}'); insert into {typeTales[base_type][type]} (id) values ('{id}');" - if base_type == _NODE: - sql += f" insert into coordinates (node) values ('{id}');" - cur.execute(sql) - - redo = sql.replace("'", '"') - undo = "" - if base_type == _NODE: - undo = f'delete from coordinates where node = "{id}"; ' - undo += f'delete from {typeTales[base_type][type]} where id = "{id}"; delete from {base_type} where id = "{id}";' - add_operation(name, redo, undo) - -def _add_id(name: str, id: str, base_type: str) -> None: - with conn[name].cursor() as cur: - sql = f"insert into {base_type} (id) values ('{id}')" - cur.execute(sql) - redo = sql.replace("'", '"') - undo = f'delete from {base_type} where id = "{id}"' - add_operation(name, redo, undo) - -def add_node(name: str, id: str, type: str) -> None: - return _add_id_type(name, id, type, _NODE) - -def add_link(name: str, id: str, type: str) -> None: - return _add_id_type(name, id, type, _LINK) - -def add_curve(name: str, id: str) -> None: - return _add_id(name, id, _CURVE) - -def add_pattern(name: str, id: str) -> None: - return _add_id(name, id, _PATTERN) - - -# have - -def _have_impl(name: str, id: str, base_type: str) -> bool: - with conn[name].cursor() as cur: - cur.execute(f"select * from {base_type} where id = '{id}'") - return cur.rowcount > 0 - -def have_node(name: str, id: str) -> bool: - return _have_impl(name, id, _NODE) - -def have_link(name: str, id: str) -> bool: - return _have_impl(name, id, _LINK) - -def have_curve(name: str, id: str) -> bool: - return _have_impl(name, id, _CURVE) - -def have_pattern(name: str, id: str) -> bool: - return _have_impl(name, id, _PATTERN) - - -# get -def _get_impl(name: str, id: str, base_type: str) -> dict[str, str]: +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}'") - if cur.rowcount > 0: - return cur.fetchone() - else: - return {} + return cur.fetchone() -def get_node(name: str, id: str) -> dict[str, str]: - return _get_impl(name, id, _NODE) +def is_node(name: str, id: str) -> bool: + return _get_from(name, id, _NODE) != None def is_junction(name: str, id: str) -> bool: - return get_node(name, id)['type'] == JUNCTION + row = _get_from(name, id, _NODE) + return row != None and row['type'] == JUNCTION def is_reservoir(name: str, id: str) -> bool: - return get_node(name, id)['type'] == RESERVOIR + row = _get_from(name, id, _NODE) + return row != None and row['type'] == RESERVOIR def is_tank(name: str, id: str) -> bool: - return get_node(name, id)['type'] == TANK + row = _get_from(name, id, _NODE) + return row != None and row['type'] == TANK -def get_link(name: str, id: str) -> dict[str, str]: - return _get_impl(name, id, _LINK) +def is_link(name: str, id: str) -> bool: + return _get_from(name, id, _LINK) != {} def is_pipe(name: str, id: str) -> bool: - return get_link(name, id)['type'] == PIPE + row = _get_from(name, id, _LINK) + return row != None and row['type'] == PIPE def is_pump(name: str, id: str) -> bool: - return get_link(name, id)['type'] == PUMP + row = _get_from(name, id, _LINK) + return row != None and row['type'] == PUMP def is_valve(name: str, id: str) -> bool: - return get_link(name, id)['type'] == VALVE + row = _get_from(name, id, _LINK) + return row != None and row['type'] == VALVE -def get_curve(name: str, id: str) -> dict[str, str]: - return _get_impl(name, id, _CURVE) +def is_curve(name: str, id: str) -> bool: + return _get_from(name, id, _CURVE) != None -def get_pattern(name: str, id: str) -> dict[str, str]: - return _get_impl(name, id, _PATTERN) - - -# delete - -def _delete_id_type(name: str, id: str, type: str, base_type: str) -> None: - with conn[name].cursor() as cur: - sql = "" - if base_type == _NODE: - sql = f"delete from coordinates where node = '{id}'; " - sql += f"delete from {typeTales[base_type][type]} where id = '{id}'; delete from {base_type} where id = '{id}';" - cur.execute(sql) - - redo = sql.replace("'", '"') - undo = f'insert into {base_type} (id, type) values ("{id}", "{type}"); insert into {typeTales[base_type][type]} (id) values ("{id}");' - if base_type == _NODE: - undo += f' insert into coordinates (node) values ("{id}");' - add_operation(name, redo, undo) - -def _delete_id(name:str, id: str, base_type: str) -> None: - with conn[name].cursor() as cur: - sql = f"delete from {base_type} where id = '{id}'" - cur.execute(sql) - redo = sql.replace("'", '"') - undo = f'insert into {base_type} (id) values ("{id}")' - add_operation(name, redo, undo) - -def delete_node(name: str, id: str) -> None: - row = get_node(name, id) - if row == {}: - return - return _delete_id_type(name, id, row['type'], _NODE) - -def delete_link(name: str, id: str) -> None: - row = get_link(name, id) - if row == {}: - return - return _delete_id_type(name, id, row['type'], _LINK) - -def delete_curve(name: str, id: str) -> None: - row = get_curve(name, id) - if row == {}: - return - return _delete_id(name, id, _CURVE) - -def delete_pattern(name: str, id: str) -> None: - row = get_pattern(name, id) - if row == {}: - return - return _delete_id(name, id, _PATTERN) +def is_pattern(name: str, id: str) -> bool: + return _get_from(name, id, _PATTERN) != None From cce5f55a2468abd51dc13e5cca4c7db9e6bed32a Mon Sep 17 00:00:00 2001 From: wqy Date: Sat, 3 Sep 2022 00:34:25 +0800 Subject: [PATCH 22/52] Title need undo redo --- api/s1_title.py | 16 ++++++++++++---- api/t_s1_title.py | 7 +++++++ 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/api/s1_title.py b/api/s1_title.py index 54b6bba..7a322cd 100644 --- a/api/s1_title.py +++ b/api/s1_title.py @@ -1,11 +1,19 @@ from psycopg.rows import dict_row +from operation import * from connection import g_conn_dict as conn -def set_title(name: str, value: str) -> None: - with conn[name].cursor() as cur: - cur.execute(f"update title set value = '{value}'") - 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) -> None: + 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) diff --git a/api/t_s1_title.py b/api/t_s1_title.py index 700c931..e5fa4e1 100644 --- a/api/t_s1_title.py +++ b/api/t_s1_title.py @@ -1,3 +1,4 @@ +from operation import execute_undo from project import * from s1_title import * @@ -20,5 +21,11 @@ print(get_title(p)) set_title(p, "test") print(get_title(p)) +execute_undo(p) +print(get_title(p)) + +execute_undo(p) +print(get_title(p)) + close_project(p) delete_project(p) From 1a236640288170667150d6783d275560f75c0fba Mon Sep 17 00:00:00 2001 From: wqy Date: Sat, 3 Sep 2022 00:35:02 +0800 Subject: [PATCH 23/52] Support junctions api --- api/s2_junctions.py | 158 ++++++++++++++++++++++++++++++++++++++++++ api/t_s2_junctions.py | 52 ++++++++++++++ 2 files changed, 210 insertions(+) create mode 100644 api/s2_junctions.py create mode 100644 api/t_s2_junctions.py diff --git a/api/s2_junctions.py b/api/s2_junctions.py new file mode 100644 index 0000000..af140c6 --- /dev/null +++ b/api/s2_junctions.py @@ -0,0 +1,158 @@ +from psycopg.rows import dict_row, Row +from connection import g_conn_dict as conn +from s0_base import * +from operation import * + +def add_junction(name: str, id: str, x: float, y: float, elevation: float) -> None: + 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) + +def delete_junction(name: str, id: str) -> None: + 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) + +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) -> None: + 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) + +def set_junction_demand(name: str, id: str, demand: float) -> None: + 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) + +def set_junction_pattern(name: str, id: str, pattern: str) -> None: + 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) + +def set_junction_coord(name: str, id: str, x: float, y: float) -> None: + 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) diff --git a/api/t_s2_junctions.py b/api/t_s2_junctions.py new file mode 100644 index 0000000..7dff04e --- /dev/null +++ b/api/t_s2_junctions.py @@ -0,0 +1,52 @@ +from project import * +from s2_junctions import * + +p = "test_s2_junctions" + +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 + +execute_undo(p) +print(get_junction_demand(p, j)) # NULL + +execute_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} + +execute_undo(p) +print(get_junction_coord(p, j)) # {'x': 10.0, 'y': 20.0} + +close_project(p) +# delete_project(p) From e79f88754292c18b59f27015908c8210a9656965 Mon Sep 17 00:00:00 2001 From: wqy Date: Sat, 3 Sep 2022 00:35:37 +0800 Subject: [PATCH 24/52] Refine __init__.py --- api/__init__.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/api/__init__.py b/api/__init__.py index 088d131..2912787 100644 --- a/api/__init__.py +++ b/api/__init__.py @@ -4,10 +4,13 @@ from project import is_project_open, open_project, close_project from operation import execute_undo as undo from operation import execute_redo as redo -from s0_base import add_node, add_link, add_curve, add_pattern -from s0_base import have_node, have_link, have_curve, have_pattern -from s0_base import is_junction, is_reservoir, is_tank -from s0_base import is_pipe, is_pump, is_valve -from s0_base import delete_node, delete_link, delete_curve, delete_pattern +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 s1_title import set_title, get_title \ No newline at end of file +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 From 12e61b7b9c6aa7580e7cef85ce42ae435b43b6f1 Mon Sep 17 00:00:00 2001 From: wqy Date: Sat, 3 Sep 2022 00:43:09 +0800 Subject: [PATCH 25/52] Refine tjnetwork_new and add junction api --- tjnetwork_new.py | 76 ++++++++++++++++++++++++++++-------------------- 1 file changed, 45 insertions(+), 31 deletions(-) diff --git a/tjnetwork_new.py b/tjnetwork_new.py index 040e364..c51e5cf 100644 --- a/tjnetwork_new.py +++ b/tjnetwork_new.py @@ -7,6 +7,8 @@ PIPE = "PIPE" PUMP = "PUMP" VALVE = "VALVE" +# project + def have_project(name: str) -> bool: return api.have_project(name) @@ -25,17 +27,18 @@ def open_project(name: str) -> None: def close_project(name: str) -> None: return api.close_project(name) +# operation + def undo(name: str) -> None: return api.undo(name) def redo(name: str) -> None: return api.redo(name) -def add_node(name: str, node_id: str, node_type: str) -> None: - return api.add_node(name, node_id, node_type) +# 0.base -def have_node(name: str, node_id: str) -> bool: - return api.have_node(name, node_id) +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) @@ -46,14 +49,8 @@ def is_reservoir(name: str, node_id: str) -> bool: def is_tank(name: str, node_id: str) -> bool: return api.is_tank(name, node_id) -def delete_node(name: str, node_id: str) -> None: - return api.delete_node(name, node_id) - -def add_link(name: str, link_id: str, link_type: str) -> None: - return api.add_link(name, link_id, link_type) - -def have_link(name: str, link_id: str) -> bool: - return api.have_link(name, link_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) @@ -64,29 +61,46 @@ def is_pump(name: str, link_id: str) -> bool: def is_valve(name: str, link_id: str) -> bool: return api.is_valve(name, link_id) -def delete_link(name: str, link_id: str) -> None: - return api.delete_link(name, link_id) +def is_curve(name: str, curve_id: str) -> bool: + return api.is_curve(name, curve_id) -def add_curve(name: str, curve_id: str) -> None: - return api.add_curve(name, curve_id) - -def have_curve(name: str, curve_id: str) -> bool: - return api.have_curve(name, curve_id) - -def delete_curve(name: str, curve_id: str) -> None: - return api.delete_curve(name, curve_id) - -def add_pattern(name: str, pattern_id: str) -> None: - return api.add_pattern(name, pattern_id) - -def have_pattern(name: str, pattern_id: str) -> bool: - return api.have_pattern(name, pattern_id) - -def delete_pattern(name: str, pattern_id: str) -> None: - return api.delete_pattern(name, pattern_id) +def is_pattern(name: str, pattern_id: str) -> bool: + return api.is_pattern(name, pattern_id) +# 1.title def set_title(name: str, title: str) -> None: return api.set_title(name, title) def get_title(name: str) -> str: return api.get_title(name) + +# 2.junction +def add_junction(name: str, junction_id: str, x: float, y: float, elevation: float) -> None: + return api.add_junction(name, junction_id, x, y, elevation) + +def delete_junction(name: str, junction_id: str) -> None: + 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) -> None: + return api.set_junction_elevation(name, junction_id, elevation) + +def set_junction_demand(name: str, junction_id: str, demand: float) -> None: + return api.set_junction_demand(name, junction_id, demand) + +def set_junction_pattern(name: str, junction_id: str, pattern: str) -> None: + return api.set_junction_pattern(name, junction_id, pattern) + +def set_junction_coord(name: str, junction_id: str, x: float, y: float) -> None: + return api.set_junction_coord(name, junction_id, x, y) From 0eb42567d27621f296e9972dd92c7135b354a616 Mon Sep 17 00:00:00 2001 From: wqy Date: Sat, 3 Sep 2022 01:07:04 +0800 Subject: [PATCH 26/52] Format package --- api/__init__.py | 24 ++++++++++++------------ api/operation.py | 2 +- api/project.py | 2 +- api/s0_base.py | 2 +- api/s1_title.py | 4 ++-- api/s2_junctions.py | 6 +++--- 6 files changed, 20 insertions(+), 20 deletions(-) diff --git a/api/__init__.py b/api/__init__.py index 2912787..b3e9439 100644 --- a/api/__init__.py +++ b/api/__init__.py @@ -1,16 +1,16 @@ -from project import have_project, create_project, delete_project -from project import is_project_open, open_project, close_project +from .project import have_project, create_project, delete_project +from .project import is_project_open, open_project, close_project -from operation import execute_undo as undo -from operation import execute_redo as redo +from .operation import execute_undo as undo +from .operation import execute_redo as redo -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 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 s1_title import set_title, get_title +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 +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/operation.py b/api/operation.py index 2ef9e56..6a5536c 100644 --- a/api/operation.py +++ b/api/operation.py @@ -1,5 +1,5 @@ from psycopg.rows import dict_row -from connection import g_conn_dict as conn +from .connection import g_conn_dict as conn def _get_current_operation(name: str) -> int: with conn[name].cursor(row_factory=dict_row) as cur: diff --git a/api/project.py b/api/project.py index 3fa1b83..b82782a 100644 --- a/api/project.py +++ b/api/project.py @@ -1,5 +1,5 @@ import psycopg as pg -from connection import g_conn_dict as conn +from .connection import g_conn_dict as conn # no undo/redo diff --git a/api/s0_base.py b/api/s0_base.py index bbefa7b..91e999d 100644 --- a/api/s0_base.py +++ b/api/s0_base.py @@ -1,5 +1,5 @@ from psycopg.rows import dict_row, Row -from connection import g_conn_dict as conn +from .connection import g_conn_dict as conn _NODE = "_node" _LINK = "_link" diff --git a/api/s1_title.py b/api/s1_title.py index 7a322cd..2f086c1 100644 --- a/api/s1_title.py +++ b/api/s1_title.py @@ -1,6 +1,6 @@ from psycopg.rows import dict_row -from operation import * -from connection import g_conn_dict as conn +from .operation import * +from .connection import g_conn_dict as conn def get_title(name: str) -> str: with conn[name].cursor(row_factory=dict_row) as cur: diff --git a/api/s2_junctions.py b/api/s2_junctions.py index af140c6..79301ce 100644 --- a/api/s2_junctions.py +++ b/api/s2_junctions.py @@ -1,7 +1,7 @@ from psycopg.rows import dict_row, Row -from connection import g_conn_dict as conn -from s0_base import * -from operation import * +from .connection import g_conn_dict as conn +from .s0_base import * +from .operation import * def add_junction(name: str, id: str, x: float, y: float, elevation: float) -> None: if is_node(name, id): From cb429ad822a79150a2cfdccb46f5e2521c7888a3 Mon Sep 17 00:00:00 2001 From: wqy Date: Sat, 3 Sep 2022 01:07:27 +0800 Subject: [PATCH 27/52] Add demo for new api --- api/t_s1_title.py | 31 ---------------- api/t_s2_junctions.py | 52 -------------------------- new_demo.py | 85 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 85 insertions(+), 83 deletions(-) delete mode 100644 api/t_s1_title.py delete mode 100644 api/t_s2_junctions.py create mode 100644 new_demo.py diff --git a/api/t_s1_title.py b/api/t_s1_title.py deleted file mode 100644 index e5fa4e1..0000000 --- a/api/t_s1_title.py +++ /dev/null @@ -1,31 +0,0 @@ -from operation import execute_undo -from project import * -from s1_title import * - -p = "test_s1_title" - -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)) - -set_title(p, "test") -print(get_title(p)) - -execute_undo(p) -print(get_title(p)) - -execute_undo(p) -print(get_title(p)) - -close_project(p) -delete_project(p) diff --git a/api/t_s2_junctions.py b/api/t_s2_junctions.py deleted file mode 100644 index 7dff04e..0000000 --- a/api/t_s2_junctions.py +++ /dev/null @@ -1,52 +0,0 @@ -from project import * -from s2_junctions import * - -p = "test_s2_junctions" - -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 - -execute_undo(p) -print(get_junction_demand(p, j)) # NULL - -execute_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} - -execute_undo(p) -print(get_junction_coord(p, j)) # {'x': 10.0, 'y': 20.0} - -close_project(p) -# delete_project(p) diff --git a/new_demo.py b/new_demo.py new file mode 100644 index 0000000..553c9d4 --- /dev/null +++ b/new_demo.py @@ -0,0 +1,85 @@ +from tjnetwork_new import * + +def demo_1_title(): + p = "demo_1_title" + + 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" + + 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} + + close_project(p) + delete_project(p) + +if __name__ == "__main__": + demo_1_title() + demo_2_junctions() From 122f3ebddc8a29c0bd42bfcd24d90ddb5639ffd3 Mon Sep 17 00:00:00 2001 From: wqy Date: Sat, 3 Sep 2022 01:13:31 +0800 Subject: [PATCH 28/52] Format api file --- tjnetwork_new.py | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/tjnetwork_new.py b/tjnetwork_new.py index c51e5cf..73375fa 100644 --- a/tjnetwork_new.py +++ b/tjnetwork_new.py @@ -1,5 +1,10 @@ import api + +############################################################ +# enum +############################################################ + JUNCTION = "JUNCTION" RESERVOIR = "RESERVOIR" TANK = "TANK" @@ -7,7 +12,10 @@ PIPE = "PIPE" PUMP = "PUMP" VALVE = "VALVE" + +############################################################ # project +############################################################ def have_project(name: str) -> bool: return api.have_project(name) @@ -27,7 +35,10 @@ def open_project(name: str) -> None: def close_project(name: str) -> None: return api.close_project(name) + +############################################################ # operation +############################################################ def undo(name: str) -> None: return api.undo(name) @@ -35,7 +46,10 @@ def undo(name: str) -> None: def redo(name: str) -> None: return api.redo(name) -# 0.base + +############################################################ +# type +############################################################ def is_node(name: str, node_id: str) -> bool: return api.is_node(name, node_id) @@ -67,14 +81,22 @@ def is_curve(name: str, curve_id: str) -> bool: def is_pattern(name: str, pattern_id: str) -> bool: return api.is_pattern(name, pattern_id) -# 1.title + +############################################################ +# title 1.[TITLE] +############################################################ + def set_title(name: str, title: str) -> None: return api.set_title(name, title) def get_title(name: str) -> str: return api.get_title(name) -# 2.junction + +############################################################ +# junction 2.[JUNCTIONS] +############################################################ + def add_junction(name: str, junction_id: str, x: float, y: float, elevation: float) -> None: return api.add_junction(name, junction_id, x, y, elevation) From b5a8051c6131c0c7bdb1f25c8f7e5c5a49f3039c Mon Sep 17 00:00:00 2001 From: wqy Date: Sat, 3 Sep 2022 08:21:43 +0800 Subject: [PATCH 29/52] Fix operation tree bug --- api/operation.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/api/operation.py b/api/operation.py index 6a5536c..f0bb301 100644 --- a/api/operation.py +++ b/api/operation.py @@ -12,10 +12,10 @@ def _update_current_operation(name: str, old_id: int, id: int) -> None: def _add_redo_undo(name: str, redo: str, undo: str) -> int: with conn[name].cursor(row_factory=dict_row) as cur: - cur.execute("select max(id) from operation") - parent = int(cur.fetchone()['max']) + parent = _get_current_operation(name) cur.execute(f"insert into operation (id, redo, undo, parent) values (default, '{redo}', '{undo}', {parent})") - return parent + 1 + 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]: From efc33a7a8633cd78e98af312281dd08eba2d206b Mon Sep 17 00:00:00 2001 From: wqy Date: Sat, 3 Sep 2022 08:22:32 +0800 Subject: [PATCH 30/52] Support more operation in database --- script/sql/create/operation.sql | 11 +++++++++++ script/sql/drop/operation.sql | 4 ++++ 2 files changed, 15 insertions(+) diff --git a/script/sql/create/operation.sql b/script/sql/create/operation.sql index 9c9429d..893cebd 100644 --- a/script/sql/create/operation.sql +++ b/script/sql/create/operation.sql @@ -15,3 +15,14 @@ CREATE TABLE CURRENT_OPERATION ); 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) +); diff --git a/script/sql/drop/operation.sql b/script/sql/drop/operation.sql index df5074c..d69ce33 100644 --- a/script/sql/drop/operation.sql +++ b/script/sql/drop/operation.sql @@ -1,3 +1,7 @@ +DROP TABLE TRANSACTION_OPERATION; + +DROP TABLE SNAPSHOT_OPERATION; + DROP TABLE CURRENT_OPERATION; DROP TABLE OPERATION; From bdb08c6798cde6e091be81036ae3bfc0b577ee4b Mon Sep 17 00:00:00 2001 From: wqy Date: Sat, 3 Sep 2022 08:23:28 +0800 Subject: [PATCH 31/52] Support random check out different snapshot --- api/__init__.py | 1 + api/operation.py | 76 ++++++++++++++++++++++++++++++++++++++++++++++++ new_demo.py | 50 ++++++++++++++++++++++++++++--- tjnetwork_new.py | 6 ++++ 4 files changed, 129 insertions(+), 4 deletions(-) diff --git a/api/__init__.py b/api/__init__.py index b3e9439..5d348c6 100644 --- a/api/__init__.py +++ b/api/__init__.py @@ -3,6 +3,7 @@ from .project import is_project_open, open_project, close_project from .operation import execute_undo as undo from .operation import execute_redo as redo +from .operation import take_snapshot, pick_snapshot from .s0_base import is_node, is_junction, is_reservoir, is_tank from .s0_base import is_link, is_pipe, is_pump, is_valve diff --git a/api/operation.py b/api/operation.py index f0bb301..92c8d67 100644 --- a/api/operation.py +++ b/api/operation.py @@ -74,3 +74,79 @@ def execute_redo(name: str) -> None: _execute(name, redo) _update_current_operation(name, curr, child) + +# snapshot support to check out between different version of database +# snapshot is persistent +# since redo always remember the recently undo path + +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: + parent = _get_current_operation(name) + cur.execute(f"insert into snapshot_operation (id, tag) values ({curr}, '{tag}')") + + +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 pick_snapshot(name: str, tag: str) -> None: + if tag == None or tag == '': + print('Non empty tag is expected!') + 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}'") + if cur.rowcount < 1: + print('No such snapshot!') + return + 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/rollback will destroy transaction. +# can not undo a committed transaction or redo a rollback transaction. +# it may remove snapshot tag if snapshot in a rollback transaction + +def start_transaction(name: str) -> None: + pass + +def commit_transaction(name: str) -> None: + pass + +def rollback_transaction(name: str) -> None: + pass diff --git a/new_demo.py b/new_demo.py index 553c9d4..5974d8f 100644 --- a/new_demo.py +++ b/new_demo.py @@ -1,5 +1,40 @@ from tjnetwork_new import * +def demo_snapshot(): + p = "demo_snapshot" + + 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") + + pick_snapshot(p, "1-2-3-4") + + close_project(p) + # delete_project(p) + + def demo_1_title(): p = "demo_1_title" @@ -27,7 +62,8 @@ def demo_1_title(): print(get_title(p)) # close_project(p) - delete_project(p) + # delete_project(p) + def demo_2_junctions(): p = "demo_2_junctions" @@ -77,9 +113,15 @@ def demo_2_junctions(): 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) + # delete_project(p) + if __name__ == "__main__": - demo_1_title() - demo_2_junctions() + demo_snapshot() + # demo_1_title() + # demo_2_junctions() + pass diff --git a/tjnetwork_new.py b/tjnetwork_new.py index 73375fa..4e8632e 100644 --- a/tjnetwork_new.py +++ b/tjnetwork_new.py @@ -46,6 +46,12 @@ def undo(name: str) -> None: def redo(name: str) -> None: return api.redo(name) +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) + ############################################################ # type From eaefc61a871a2ac272f546e4dafee9d11e4611a7 Mon Sep 17 00:00:00 2001 From: wqy Date: Sat, 3 Sep 2022 08:28:13 +0800 Subject: [PATCH 32/52] Support to copy project --- api/__init__.py | 1 + api/project.py | 7 +++++-- tjnetwork_new.py | 3 +++ 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/api/__init__.py b/api/__init__.py index 5d348c6..44a98b8 100644 --- a/api/__init__.py +++ b/api/__init__.py @@ -1,5 +1,6 @@ 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 .operation import execute_undo as undo from .operation import execute_redo as redo diff --git a/api/project.py b/api/project.py index b82782a..f283c85 100644 --- a/api/project.py +++ b/api/project.py @@ -9,10 +9,13 @@ def have_project(name: str) -> bool: cur.execute(f"select * from pg_database where datname = '{name}'") return cur.rowcount > 0 -def create_project(name: str) -> None: +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 {name} with template = project") + 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: diff --git a/tjnetwork_new.py b/tjnetwork_new.py index 4e8632e..52cb0e5 100644 --- a/tjnetwork_new.py +++ b/tjnetwork_new.py @@ -35,6 +35,9 @@ def open_project(name: str) -> None: 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 From e68c1776c43a82911e670b5e6ac89631e6057183 Mon Sep 17 00:00:00 2001 From: wqy Date: Sat, 3 Sep 2022 08:31:17 +0800 Subject: [PATCH 33/52] Code refactor --- api/operation.py | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/api/operation.py b/api/operation.py index 92c8d67..53d1f45 100644 --- a/api/operation.py +++ b/api/operation.py @@ -1,6 +1,14 @@ from psycopg.rows import dict_row from .connection import g_conn_dict as conn +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") @@ -90,15 +98,6 @@ def take_snapshot(name: str, tag: str) -> None: parent = _get_current_operation(name) cur.execute(f"insert into snapshot_operation (id, tag) values ({curr}, '{tag}')") - -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 pick_snapshot(name: str, tag: str) -> None: if tag == None or tag == '': print('Non empty tag is expected!') From dc07e4811cff059cb3a5e3cfa322060b4ed29ea8 Mon Sep 17 00:00:00 2001 From: wqy Date: Sat, 3 Sep 2022 09:30:19 +0800 Subject: [PATCH 34/52] Drop if exists --- script/sql/create/operation.sql | 1 + script/sql/drop/1.title.sql | 2 +- script/sql/drop/10.status.sql | 12 ++++++------ script/sql/drop/11.patterns.sql | 2 +- script/sql/drop/12.curves.sql | 2 +- script/sql/drop/13.controls.sql | 8 ++++---- script/sql/drop/14.rules.sql | 2 +- script/sql/drop/15.energy.sql | 8 ++++---- script/sql/drop/16.emitters.sql | 2 +- script/sql/drop/17.quality.sql | 2 +- script/sql/drop/18.sources.sql | 4 ++-- script/sql/drop/19.reactions.sql | 18 +++++++++--------- script/sql/drop/2.junctions.sql | 2 +- script/sql/drop/20.mixing.sql | 4 ++-- script/sql/drop/21.times.sql | 2 +- script/sql/drop/22.report.sql | 2 +- script/sql/drop/23.options.sql | 2 +- script/sql/drop/24.coordinates.sql | 6 +++--- script/sql/drop/25.vertices.sql | 2 +- script/sql/drop/26.labels.sql | 2 +- script/sql/drop/27.backdrop.sql | 2 +- script/sql/drop/3.reservoirs.sql | 2 +- script/sql/drop/4.tanks.sql | 4 ++-- script/sql/drop/5.pipes.sql | 4 ++-- script/sql/drop/6.pumps.sql | 10 +++++----- script/sql/drop/7.valves.sql | 4 ++-- script/sql/drop/8.tags.sql | 4 ++-- script/sql/drop/9.demands.sql | 2 +- script/sql/drop/operation.sql | 8 ++++---- 29 files changed, 63 insertions(+), 62 deletions(-) diff --git a/script/sql/create/operation.sql b/script/sql/create/operation.sql index 893cebd..2f105e6 100644 --- a/script/sql/create/operation.sql +++ b/script/sql/create/operation.sql @@ -25,4 +25,5 @@ CREATE TABLE SNAPSHOT_OPERATION CREATE TABLE TRANSACTION_OPERATION ( ID INTEGER PRIMARY KEY REFERENCES OPERATION(ID) +, STRICT BOOLEAN NOT NULL DEFAULT FALSE ); diff --git a/script/sql/drop/1.title.sql b/script/sql/drop/1.title.sql index 215ecc6..7ee6d55 100644 --- a/script/sql/drop/1.title.sql +++ b/script/sql/drop/1.title.sql @@ -1,3 +1,3 @@ -- [TITLE] -DROP TABLE TITLE; +DROP TABLE IF EXISTS TITLE; diff --git a/script/sql/drop/10.status.sql b/script/sql/drop/10.status.sql index 970330e..322a72f 100644 --- a/script/sql/drop/10.status.sql +++ b/script/sql/drop/10.status.sql @@ -1,13 +1,13 @@ -- [STATUS] -DROP TABLE STATUS_LINK; +DROP TABLE IF EXISTS STATUS_LINK; -DROP TABLE STATUS_VALVE; +DROP TABLE IF EXISTS STATUS_VALVE; -DROP TYPE STATUS_VALVE_STATUS; +DROP TYPE IF EXISTS STATUS_VALVE_STATUS; -DROP TABLE STATUS_PUMP; +DROP TABLE IF EXISTS STATUS_PUMP; -DROP TABLE STATUS_PIPE; +DROP TABLE IF EXISTS STATUS_PIPE; -DROP TYPE STATUS_PIPE_PUMP_STATUS; +DROP TYPE IF EXISTS STATUS_PIPE_PUMP_STATUS; diff --git a/script/sql/drop/11.patterns.sql b/script/sql/drop/11.patterns.sql index 565c67e..fd08d97 100644 --- a/script/sql/drop/11.patterns.sql +++ b/script/sql/drop/11.patterns.sql @@ -1,3 +1,3 @@ -- [PATTERNS] -DROP TABLE PATTERNS; +DROP TABLE IF EXISTS PATTERNS; diff --git a/script/sql/drop/12.curves.sql b/script/sql/drop/12.curves.sql index 9535588..577464e 100644 --- a/script/sql/drop/12.curves.sql +++ b/script/sql/drop/12.curves.sql @@ -1,3 +1,3 @@ -- [CURVES] -DROP TABLE CURVES; +DROP TABLE IF EXISTS CURVES; diff --git a/script/sql/drop/13.controls.sql b/script/sql/drop/13.controls.sql index 5333175..476259d 100644 --- a/script/sql/drop/13.controls.sql +++ b/script/sql/drop/13.controls.sql @@ -1,9 +1,9 @@ -- [CONTROLS] -DROP TABLE CONTROLS_3; +DROP TABLE IF EXISTS CONTROLS_3; -DROP TABLE CONTROLS_2; +DROP TABLE IF EXISTS CONTROLS_2; -DROP TABLE CONTROLS_1; +DROP TABLE IF EXISTS CONTROLS_1; -DROP TYPE CONTROLS_1_PREP; +DROP TYPE IF EXISTS CONTROLS_1_PREP; diff --git a/script/sql/drop/14.rules.sql b/script/sql/drop/14.rules.sql index 7cc4bac..84e4fd8 100644 --- a/script/sql/drop/14.rules.sql +++ b/script/sql/drop/14.rules.sql @@ -1,3 +1,3 @@ -- [RULES] -DROP TABLE RULES; +DROP TABLE IF EXISTS RULES; diff --git a/script/sql/drop/15.energy.sql b/script/sql/drop/15.energy.sql index 1da9af8..5002d5c 100644 --- a/script/sql/drop/15.energy.sql +++ b/script/sql/drop/15.energy.sql @@ -1,9 +1,9 @@ -- [ENERGY] -DROP TABLE ENERGY_DEMAND_CHARGE; +DROP TABLE IF EXISTS ENERGY_DEMAND_CHARGE; -DROP TABLE ENERGY_PUMP; +DROP TABLE IF EXISTS ENERGY_PUMP; -DROP TABLE ENERGY_GLOBAL; +DROP TABLE IF EXISTS ENERGY_GLOBAL; -DROP TYPE ENERGY_PARAM; +DROP TYPE IF EXISTS ENERGY_PARAM; diff --git a/script/sql/drop/16.emitters.sql b/script/sql/drop/16.emitters.sql index 7aa46f8..40a0af8 100644 --- a/script/sql/drop/16.emitters.sql +++ b/script/sql/drop/16.emitters.sql @@ -1,3 +1,3 @@ -- [EMITTERS] -DROP TABLE EMITTERS; +DROP TABLE IF EXISTS EMITTERS; diff --git a/script/sql/drop/17.quality.sql b/script/sql/drop/17.quality.sql index c15ac7d..b72c290 100644 --- a/script/sql/drop/17.quality.sql +++ b/script/sql/drop/17.quality.sql @@ -1,3 +1,3 @@ -- [QUALITY] -DROP TABLE QUALITY; +DROP TABLE IF EXISTS QUALITY; diff --git a/script/sql/drop/18.sources.sql b/script/sql/drop/18.sources.sql index e11b7ea..0402ffe 100644 --- a/script/sql/drop/18.sources.sql +++ b/script/sql/drop/18.sources.sql @@ -1,5 +1,5 @@ -- [SOURCES] -DROP TABLE SOURCES; +DROP TABLE IF EXISTS SOURCES; -DROP TYPE SOURCES_TYPE; +DROP TYPE IF EXISTS SOURCES_TYPE; diff --git a/script/sql/drop/19.reactions.sql b/script/sql/drop/19.reactions.sql index bcc9dc5..2f6ba5b 100644 --- a/script/sql/drop/19.reactions.sql +++ b/script/sql/drop/19.reactions.sql @@ -1,19 +1,19 @@ -- [REACTIONS] -DROP TABLE REACTIONS_ROUGHNESS_CORRELATION; +DROP TABLE IF EXISTS REACTIONS_ROUGHNESS_CORRELATION; -DROP TABLE REACTIONS_LIMITING_POTENTIAL; +DROP TABLE IF EXISTS REACTIONS_LIMITING_POTENTIAL; -DROP TABLE REACTIONS_TANK; +DROP TABLE IF EXISTS REACTIONS_TANK; -DROP TABLE REACTIONS_PIPE; +DROP TABLE IF EXISTS REACTIONS_PIPE; -DROP TYPE REACTIONS_PIPE_PARAM; +DROP TYPE IF EXISTS REACTIONS_PIPE_PARAM; -DROP TABLE REACTIONS_GLOBAL; +DROP TABLE IF EXISTS REACTIONS_GLOBAL; -DROP TYPE REACTIONS_GLOBAL_PARAM; +DROP TYPE IF EXISTS REACTIONS_GLOBAL_PARAM; -DROP TABLE REACTIONS_ORDER; +DROP TABLE IF EXISTS REACTIONS_ORDER; -DROP TYPE REACTIONS_ORDER_PARAM; +DROP TYPE IF EXISTS REACTIONS_ORDER_PARAM; diff --git a/script/sql/drop/2.junctions.sql b/script/sql/drop/2.junctions.sql index 8fc23c4..ef0a609 100644 --- a/script/sql/drop/2.junctions.sql +++ b/script/sql/drop/2.junctions.sql @@ -1,3 +1,3 @@ -- [JUNCTIONS] -DROP TABLE JUNCTIONS; +DROP TABLE IF EXISTS JUNCTIONS; diff --git a/script/sql/drop/20.mixing.sql b/script/sql/drop/20.mixing.sql index 1434859..938d554 100644 --- a/script/sql/drop/20.mixing.sql +++ b/script/sql/drop/20.mixing.sql @@ -1,5 +1,5 @@ -- [MIXING] -DROP TABLE MIXING; +DROP TABLE IF EXISTS MIXING; -DROP TYPE MIXING_Model; +DROP TYPE IF EXISTS MIXING_Model; diff --git a/script/sql/drop/21.times.sql b/script/sql/drop/21.times.sql index 0d45aed..f051bd7 100644 --- a/script/sql/drop/21.times.sql +++ b/script/sql/drop/21.times.sql @@ -1,3 +1,3 @@ -- [TIMES] -DROP TABLE TIMES; +DROP TABLE IF EXISTS TIMES; diff --git a/script/sql/drop/22.report.sql b/script/sql/drop/22.report.sql index 58dcd51..790684d 100644 --- a/script/sql/drop/22.report.sql +++ b/script/sql/drop/22.report.sql @@ -1,3 +1,3 @@ -- [REPORT] -DROP TABLE REPORT; +DROP TABLE IF EXISTS REPORT; diff --git a/script/sql/drop/23.options.sql b/script/sql/drop/23.options.sql index 69e9714..7cc629a 100644 --- a/script/sql/drop/23.options.sql +++ b/script/sql/drop/23.options.sql @@ -1,3 +1,3 @@ -- [OPTIONS] -DROP TABLE OPTIONS; +DROP TABLE IF EXISTS OPTIONS; diff --git a/script/sql/drop/24.coordinates.sql b/script/sql/drop/24.coordinates.sql index 59f9f45..04d0465 100644 --- a/script/sql/drop/24.coordinates.sql +++ b/script/sql/drop/24.coordinates.sql @@ -1,7 +1,7 @@ -- [COORDINATES] -DROP INDEX COORDINATES_GIST; +DROP INDEX IF EXISTS COORDINATES_GIST; -DROP INDEX COORDINATES_SPGIST; +DROP INDEX IF EXISTS COORDINATES_SPGIST; -DROP TABLE COORDINATES; +DROP TABLE IF EXISTS COORDINATES; diff --git a/script/sql/drop/25.vertices.sql b/script/sql/drop/25.vertices.sql index 1352d0d..f74dd3e 100644 --- a/script/sql/drop/25.vertices.sql +++ b/script/sql/drop/25.vertices.sql @@ -1,3 +1,3 @@ -- [VERTICES] -DROP TABLE VERTICES; +DROP TABLE IF EXISTS VERTICES; diff --git a/script/sql/drop/26.labels.sql b/script/sql/drop/26.labels.sql index 70da104..aa1b2b2 100644 --- a/script/sql/drop/26.labels.sql +++ b/script/sql/drop/26.labels.sql @@ -1,3 +1,3 @@ -- [LABELS] -DROP TABLE LABELS; +DROP TABLE IF EXISTS LABELS; diff --git a/script/sql/drop/27.backdrop.sql b/script/sql/drop/27.backdrop.sql index 85f3bf2..408bb85 100644 --- a/script/sql/drop/27.backdrop.sql +++ b/script/sql/drop/27.backdrop.sql @@ -1,3 +1,3 @@ -- [BACKDROP] -DROP TABLE BACKDROP; +DROP TABLE IF EXISTS BACKDROP; diff --git a/script/sql/drop/3.reservoirs.sql b/script/sql/drop/3.reservoirs.sql index 4fd1e46..9864c18 100644 --- a/script/sql/drop/3.reservoirs.sql +++ b/script/sql/drop/3.reservoirs.sql @@ -1,3 +1,3 @@ -- [RESERVOIRS] -DROP TABLE RESERVOIRS; +DROP TABLE IF EXISTS RESERVOIRS; diff --git a/script/sql/drop/4.tanks.sql b/script/sql/drop/4.tanks.sql index f9a4c27..da0487d 100644 --- a/script/sql/drop/4.tanks.sql +++ b/script/sql/drop/4.tanks.sql @@ -1,5 +1,5 @@ -- [TANKS] -DROP TABLE TANKS; +DROP TABLE IF EXISTS TANKS; -DROP TYPE TANKS_OVERFLOW; +DROP TYPE IF EXISTS TANKS_OVERFLOW; diff --git a/script/sql/drop/5.pipes.sql b/script/sql/drop/5.pipes.sql index e01acd3..1e23612 100644 --- a/script/sql/drop/5.pipes.sql +++ b/script/sql/drop/5.pipes.sql @@ -1,5 +1,5 @@ -- [PIPES] -DROP TABLE PIPES; +DROP TABLE IF EXISTS PIPES; -DROP TYPE PIPES_STATUS; +DROP TYPE IF EXISTS PIPES_STATUS; diff --git a/script/sql/drop/6.pumps.sql b/script/sql/drop/6.pumps.sql index 2526df6..5ac9656 100644 --- a/script/sql/drop/6.pumps.sql +++ b/script/sql/drop/6.pumps.sql @@ -1,11 +1,11 @@ -- [PUMPS] -DROP TABLE PUMPS_PROPERTY_PATTERN; +DROP TABLE IF EXISTS PUMPS_PROPERTY_PATTERN; -DROP TABLE PUMPS_PROPERTY_HEAD; +DROP TABLE IF EXISTS PUMPS_PROPERTY_HEAD; -DROP TABLE PUMPS_PROPERTY_SPEED; +DROP TABLE IF EXISTS PUMPS_PROPERTY_SPEED; -DROP TABLE PUMPS_PROPERTY_POWER; +DROP TABLE IF EXISTS PUMPS_PROPERTY_POWER; -DROP TABLE PUMPS; +DROP TABLE IF EXISTS PUMPS; diff --git a/script/sql/drop/7.valves.sql b/script/sql/drop/7.valves.sql index ca001fe..c95b423 100644 --- a/script/sql/drop/7.valves.sql +++ b/script/sql/drop/7.valves.sql @@ -1,5 +1,5 @@ -- [VALVES] -DROP TABLE VALVES; +DROP TABLE IF EXISTS VALVES; -DROP TYPE VALVES_TYPE; +DROP TYPE IF EXISTS VALVES_TYPE; diff --git a/script/sql/drop/8.tags.sql b/script/sql/drop/8.tags.sql index 78ec688..df8e3d1 100644 --- a/script/sql/drop/8.tags.sql +++ b/script/sql/drop/8.tags.sql @@ -1,5 +1,5 @@ -- [TAGS] -DROP TABLE TAGS_LINK; +DROP TABLE IF EXISTS TAGS_LINK; -DROP TABLE TAGS_NODE; +DROP TABLE IF EXISTS TAGS_NODE; diff --git a/script/sql/drop/9.demands.sql b/script/sql/drop/9.demands.sql index 7276cca..ebe0209 100644 --- a/script/sql/drop/9.demands.sql +++ b/script/sql/drop/9.demands.sql @@ -1,3 +1,3 @@ -- [DEMANDS] -DROP TABLE DEMANDS; +DROP TABLE IF EXISTS DEMANDS; diff --git a/script/sql/drop/operation.sql b/script/sql/drop/operation.sql index d69ce33..998ee2f 100644 --- a/script/sql/drop/operation.sql +++ b/script/sql/drop/operation.sql @@ -1,7 +1,7 @@ -DROP TABLE TRANSACTION_OPERATION; +DROP TABLE IF EXISTS TRANSACTION_OPERATION; -DROP TABLE SNAPSHOT_OPERATION; +DROP TABLE IF EXISTS SNAPSHOT_OPERATION; -DROP TABLE CURRENT_OPERATION; +DROP TABLE IF EXISTS CURRENT_OPERATION; -DROP TABLE OPERATION; +DROP TABLE IF EXISTS OPERATION; From 555cab36276d6319f5c8891e182c20462a4af81b Mon Sep 17 00:00:00 2001 From: wqy Date: Sat, 3 Sep 2022 09:30:57 +0800 Subject: [PATCH 35/52] Support transaction to commit or rollback, this will modify operation tree --- api/__init__.py | 1 + api/operation.py | 73 ++++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 66 insertions(+), 8 deletions(-) diff --git a/api/__init__.py b/api/__init__.py index 44a98b8..0854a57 100644 --- a/api/__init__.py +++ b/api/__init__.py @@ -5,6 +5,7 @@ from .project import copy_project from .operation import execute_undo as undo from .operation import execute_redo as redo from .operation import take_snapshot, pick_snapshot +from .operation import have_transaction, start_transaction, commit_transaction, rollback_transaction from .s0_base import is_node, is_junction, is_reservoir, is_tank from .s0_base import is_link, is_pipe, is_pump, is_valve diff --git a/api/operation.py b/api/operation.py index 53d1f45..b2d8ccd 100644 --- a/api/operation.py +++ b/api/operation.py @@ -1,6 +1,24 @@ -from psycopg.rows import dict_row +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 id 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: + cur.execute(f"delete from operation where id = {id}") + return int(cur.fetchone()['id']) + def _get_parents(name: str, id: int) -> list[int]: ids = [id] with conn[name].cursor(row_factory=dict_row) as cur: @@ -42,7 +60,7 @@ def _query_redo(name: str, id: str) -> dict[str, str]: cur.execute(f"select redo from operation where id = {id}") return cur.fetchone()['redo'] -def _set_redo_child(name: str, id: str, child: str) -> None: +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}") @@ -56,8 +74,19 @@ def add_operation(name: str, redo: str, undo: str) -> None: old = _get_current_operation(name) _update_current_operation(name, old, curr) -def execute_undo(name: str) -> None: +def execute_undo(name: str, discard: bool = False) -> None: curr = _get_current_operation(name) + + # transaction control + tran = _get_current_transaction(name) + if int(tran['id']) >= 0: + if bool(tran['strict']): # strict mode disallow undo + print("Do not allow to undo in strict transaction mode!") + return + elif tran <= curr: # normal mode disallow undo start point + print("Do not allow to undo transaction start point!") + return + row = _query_undo(name, curr) undo = row['undo'] if undo == '': @@ -65,11 +94,14 @@ def execute_undo(name: str) -> None: return parent = int(row['parent']) - _set_redo_child(name, parent, curr) + _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) @@ -141,11 +173,36 @@ def pick_snapshot(name: str, tag: str) -> None: # can not undo a committed transaction or redo a rollback transaction. # it may remove snapshot tag if snapshot in a rollback transaction -def start_transaction(name: str) -> None: - pass +def have_transaction(name: str) -> bool: + return _get_current_transaction_id(name) >= 0 + +def start_transaction(name: str, strict: bool = False) -> None: + if have_transaction(name): + print("Only support single transaction now, please commit/rollback 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: - pass + if not have_transaction(name): + print("No active transaction!") + return + + _remove_transaction(name) def rollback_transaction(name: str) -> None: - pass + tran = _get_current_transaction_id(name) + if tran >= 0: + print("No active transaction!") + return + + 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) From 5f3db140675228229fa9434ef74fabb391a05aac Mon Sep 17 00:00:00 2001 From: wqy Date: Sat, 3 Sep 2022 09:37:31 +0800 Subject: [PATCH 36/52] More snapshot api --- api/__init__.py | 2 +- api/operation.py | 13 +++++++++---- tjnetwork_new.py | 3 +++ 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/api/__init__.py b/api/__init__.py index 0854a57..9ea241c 100644 --- a/api/__init__.py +++ b/api/__init__.py @@ -4,7 +4,7 @@ from .project import copy_project from .operation import execute_undo as undo from .operation import execute_redo as redo -from .operation import take_snapshot, pick_snapshot +from .operation import have_snapshot, take_snapshot, pick_snapshot from .operation import have_transaction, start_transaction, commit_transaction, rollback_transaction from .s0_base import is_node, is_junction, is_reservoir, is_tank diff --git a/api/operation.py b/api/operation.py index b2d8ccd..969362f 100644 --- a/api/operation.py +++ b/api/operation.py @@ -119,6 +119,11 @@ def execute_redo(name: str) -> None: # 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!') @@ -127,7 +132,6 @@ def take_snapshot(name: str, tag: str) -> None: curr = _get_current_operation(name) with conn[name].cursor() as cur: - parent = _get_current_operation(name) cur.execute(f"insert into snapshot_operation (id, tag) values ({curr}, '{tag}')") def pick_snapshot(name: str, tag: str) -> None: @@ -135,14 +139,15 @@ def pick_snapshot(name: str, tag: str) -> None: 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}'") - if cur.rowcount < 1: - print('No such snapshot!') - return target = int(cur.fetchone()['id']) if target in curr_parents: # target -> curr for i in range(curr_parents.index(target)): diff --git a/tjnetwork_new.py b/tjnetwork_new.py index 52cb0e5..005292a 100644 --- a/tjnetwork_new.py +++ b/tjnetwork_new.py @@ -49,6 +49,9 @@ def undo(name: str) -> None: def redo(name: str) -> None: return api.redo(name) +def have_snapshot(name: str, tag: str) -> None: + return api.have_snapshot(name, tag) + def take_snapshot(name: str, tag: str) -> None: return api.take_snapshot(name, tag) From bf188494f561590cdad7ed50fccb5d19fed78ad7 Mon Sep 17 00:00:00 2001 From: wqy Date: Sat, 3 Sep 2022 09:52:08 +0800 Subject: [PATCH 37/52] Enhance transaction --- api/operation.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/api/operation.py b/api/operation.py index 969362f..33c0446 100644 --- a/api/operation.py +++ b/api/operation.py @@ -16,6 +16,9 @@ def _remove_transaction(name: str) -> None: def _remove_operation(name: str, id: int) -> None: with conn[name].cursor(row_factory=dict_row) as cur: + # can not be >= 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}") return int(cur.fetchone()['id']) @@ -83,7 +86,7 @@ def execute_undo(name: str, discard: bool = False) -> None: if bool(tran['strict']): # strict mode disallow undo print("Do not allow to undo in strict transaction mode!") return - elif tran <= curr: # normal mode disallow undo start point + elif tran <= curr: # normal mode disallow undo start point, and there is foreign key constraint print("Do not allow to undo transaction start point!") return @@ -175,7 +178,8 @@ def pick_snapshot(name: str, tag: str) -> None: execute_redo(name) # transaction is volatile, commit/rollback will destroy transaction. -# can not undo a committed transaction or redo a rollback transaction. +# can not redo a rollback transaction. +# but can undo a committed transaction... inconsistent... # it may remove snapshot tag if snapshot in a rollback transaction def have_transaction(name: str) -> bool: From 8430b0278536ebe7afd3bb67c77938664395def0 Mon Sep 17 00:00:00 2001 From: wqy Date: Sat, 3 Sep 2022 09:52:36 +0800 Subject: [PATCH 38/52] Expose transaction api --- api/__init__.py | 2 +- api/operation.py | 2 +- tjnetwork_new.py | 13 ++++++++++++- 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/api/__init__.py b/api/__init__.py index 9ea241c..ef57338 100644 --- a/api/__init__.py +++ b/api/__init__.py @@ -5,7 +5,7 @@ from .project import copy_project 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, rollback_transaction +from .operation import have_transaction, start_transaction, commit_transaction, abort_transaction from .s0_base import is_node, is_junction, is_reservoir, is_tank from .s0_base import is_link, is_pipe, is_pump, is_valve diff --git a/api/operation.py b/api/operation.py index 33c0446..57c2fd9 100644 --- a/api/operation.py +++ b/api/operation.py @@ -202,7 +202,7 @@ def commit_transaction(name: str) -> None: _remove_transaction(name) -def rollback_transaction(name: str) -> None: +def abort_transaction(name: str) -> None: tran = _get_current_transaction_id(name) if tran >= 0: print("No active transaction!") diff --git a/tjnetwork_new.py b/tjnetwork_new.py index 005292a..0d123a3 100644 --- a/tjnetwork_new.py +++ b/tjnetwork_new.py @@ -49,7 +49,7 @@ def undo(name: str) -> None: def redo(name: str) -> None: return api.redo(name) -def have_snapshot(name: str, tag: str) -> None: +def have_snapshot(name: str, tag: str) -> bool: return api.have_snapshot(name, tag) def take_snapshot(name: str, tag: str) -> None: @@ -58,6 +58,17 @@ def take_snapshot(name: str, tag: str) -> None: 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 From a44977974d062abc3a8ae459dca3135d15baed29 Mon Sep 17 00:00:00 2001 From: wqy Date: Sat, 3 Sep 2022 10:19:08 +0800 Subject: [PATCH 39/52] Add transaction demo --- api/operation.py | 29 ++++++++++++++++------------- new_demo.py | 43 ++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 56 insertions(+), 16 deletions(-) diff --git a/api/operation.py b/api/operation.py index 57c2fd9..cedb82c 100644 --- a/api/operation.py +++ b/api/operation.py @@ -3,7 +3,7 @@ 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 id from transaction_operation") + cur.execute(f"select * from transaction_operation") return cur.fetchone() def _get_current_transaction_id(name: str) -> int: @@ -20,7 +20,6 @@ def _remove_operation(name: str, id: int) -> None: 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}") - return int(cur.fetchone()['id']) def _get_parents(name: str, id: int) -> list[int]: ids = [id] @@ -81,14 +80,15 @@ def execute_undo(name: str, discard: bool = False) -> None: curr = _get_current_operation(name) # transaction control - tran = _get_current_transaction(name) - if int(tran['id']) >= 0: - if bool(tran['strict']): # strict mode disallow undo - print("Do not allow to undo in strict transaction mode!") - return - elif tran <= curr: # normal mode disallow undo start point, and there is foreign key constraint - print("Do not allow to undo transaction start point!") - return + 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'] @@ -183,7 +183,9 @@ def pick_snapshot(name: str, tag: str) -> None: # it may remove snapshot tag if snapshot in a rollback transaction def have_transaction(name: str) -> bool: - return _get_current_transaction_id(name) >= 0 + 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): @@ -203,11 +205,12 @@ def commit_transaction(name: str) -> None: _remove_transaction(name) def abort_transaction(name: str) -> None: - tran = _get_current_transaction_id(name) - if tran >= 0: + 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) diff --git a/new_demo.py b/new_demo.py index 5974d8f..d14cc57 100644 --- a/new_demo.py +++ b/new_demo.py @@ -1,5 +1,6 @@ from tjnetwork_new import * + def demo_snapshot(): p = "demo_snapshot" @@ -13,8 +14,8 @@ def demo_snapshot(): 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-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") @@ -35,6 +36,41 @@ def demo_snapshot(): # delete_project(p) +def demo_transaction(): + p = "demo_transaction" + + 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") + + abort_transaction(p) + + print(have_snapshot(p, "1")) + print(have_snapshot(p, "2")) + print(have_snapshot(p, "3")) + print(have_snapshot(p, "4")) + + close_project(p) + # delete_project(p) + + def demo_1_title(): p = "demo_1_title" @@ -121,7 +157,8 @@ def demo_2_junctions(): if __name__ == "__main__": - demo_snapshot() + # demo_snapshot() + demo_transaction() # demo_1_title() # demo_2_junctions() pass From 0832e557fe3adf03ee0c8beafb930a7ef4a22ab4 Mon Sep 17 00:00:00 2001 From: wqy Date: Sat, 3 Sep 2022 10:27:51 +0800 Subject: [PATCH 40/52] Update comment --- api/operation.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/operation.py b/api/operation.py index cedb82c..335fee1 100644 --- a/api/operation.py +++ b/api/operation.py @@ -16,7 +16,7 @@ def _remove_transaction(name: str) -> None: def _remove_operation(name: str, id: int) -> None: with conn[name].cursor(row_factory=dict_row) as cur: - # can not be >= since there is a tree ! + # 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}") From f16a16991e523f6d7da784ecbe68011e3938aeb8 Mon Sep 17 00:00:00 2001 From: wqy Date: Sat, 3 Sep 2022 10:33:27 +0800 Subject: [PATCH 41/52] Update comment --- api/operation.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/api/operation.py b/api/operation.py index 335fee1..50fca6a 100644 --- a/api/operation.py +++ b/api/operation.py @@ -177,10 +177,10 @@ def pick_snapshot(name: str, tag: str) -> None: cur.execute(f"update operation set redo_child = '{target}' where id = '{target_parents[1]}'") execute_redo(name) -# transaction is volatile, commit/rollback will destroy transaction. -# can not redo a rollback transaction. +# 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 tag if snapshot in a rollback transaction +# 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: @@ -189,7 +189,7 @@ def have_transaction(name: str) -> bool: def start_transaction(name: str, strict: bool = False) -> None: if have_transaction(name): - print("Only support single transaction now, please commit/rollback current transaction!") + print("Only support single transaction now, please commit/abort current transaction!") return curr = _get_current_operation(name) From c3dcf70a04d4cfc476022dc1ce48392d3a56a91e Mon Sep 17 00:00:00 2001 From: wqy Date: Sat, 3 Sep 2022 10:38:15 +0800 Subject: [PATCH 42/52] Reuse s0 defined enum --- api/__init__.py | 1 + tjnetwork_new.py | 12 ++++++------ 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/api/__init__.py b/api/__init__.py index ef57338..499884c 100644 --- a/api/__init__.py +++ b/api/__init__.py @@ -7,6 +7,7 @@ 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 diff --git a/tjnetwork_new.py b/tjnetwork_new.py index 0d123a3..d23f032 100644 --- a/tjnetwork_new.py +++ b/tjnetwork_new.py @@ -5,12 +5,12 @@ import api # enum ############################################################ -JUNCTION = "JUNCTION" -RESERVOIR = "RESERVOIR" -TANK = "TANK" -PIPE = "PIPE" -PUMP = "PUMP" -VALVE = "VALVE" +JUNCTION = api.JUNCTION +RESERVOIR = api.RESERVOIR +TANK = api.TANK +PIPE = api.PIPE +PUMP = api.PUMP +VALVE = api.VALVE ############################################################ From 8fe2eebe031c65c82ae9f0ee4f239f4cfaf1f500 Mon Sep 17 00:00:00 2001 From: wqy Date: Sat, 3 Sep 2022 11:08:05 +0800 Subject: [PATCH 43/52] Get api --- api/__init__.py | 1 + api/s0_base.py | 20 ++++++++++++++++++++ tjnetwork_new.py | 12 ++++++++++++ 3 files changed, 33 insertions(+) diff --git a/api/__init__.py b/api/__init__.py index 499884c..4ea2a94 100644 --- a/api/__init__.py +++ b/api/__init__.py @@ -12,6 +12,7 @@ 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 diff --git a/api/s0_base.py b/api/s0_base.py index 91e999d..87350a7 100644 --- a/api/s0_base.py +++ b/api/s0_base.py @@ -53,3 +53,23 @@ def is_curve(name: str, id: str) -> bool: 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/tjnetwork_new.py b/tjnetwork_new.py index d23f032..f7a3845 100644 --- a/tjnetwork_new.py +++ b/tjnetwork_new.py @@ -104,6 +104,18 @@ def is_curve(name: str, curve_id: str) -> bool: 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] From 45bb0483cea9e59d026bf36fae0663d2ca128d5e Mon Sep 17 00:00:00 2001 From: wqy Date: Sat, 3 Sep 2022 11:12:54 +0800 Subject: [PATCH 44/52] Refine demo --- api/operation.py | 2 +- new_demo.py | 34 +++++++++++++++++++++++++++------- 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/api/operation.py b/api/operation.py index 50fca6a..ac457f3 100644 --- a/api/operation.py +++ b/api/operation.py @@ -118,7 +118,7 @@ def execute_redo(name: str) -> None: _execute(name, redo) _update_current_operation(name, curr, child) -# snapshot support to check out between different version of database +# snapshot support to checkout between different version of database # snapshot is persistent # since redo always remember the recently undo path diff --git a/new_demo.py b/new_demo.py index d14cc57..02f25d8 100644 --- a/new_demo.py +++ b/new_demo.py @@ -4,6 +4,8 @@ from tjnetwork_new import * def demo_snapshot(): p = "demo_snapshot" + print(p) + if is_project_open(p): close_project(p) @@ -30,8 +32,14 @@ def demo_snapshot(): 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) @@ -39,6 +47,8 @@ def demo_snapshot(): def demo_transaction(): p = "demo_transaction" + print(p) + if is_project_open(p): close_project(p) @@ -60,12 +70,18 @@ def demo_transaction(): 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(have_snapshot(p, "1")) - print(have_snapshot(p, "2")) - print(have_snapshot(p, "3")) - print(have_snapshot(p, "4")) + 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) @@ -74,6 +90,8 @@ def demo_transaction(): def demo_1_title(): p = "demo_1_title" + print(p) + if is_project_open(p): close_project(p) @@ -104,6 +122,8 @@ def demo_1_title(): def demo_2_junctions(): p = "demo_2_junctions" + print(p) + if is_project_open(p): close_project(p) @@ -157,8 +177,8 @@ def demo_2_junctions(): if __name__ == "__main__": - # demo_snapshot() + demo_snapshot() demo_transaction() - # demo_1_title() - # demo_2_junctions() + demo_1_title() + demo_2_junctions() pass From 19b690b66589033a8a155cc045e92597aa42b904 Mon Sep 17 00:00:00 2001 From: wqy Date: Sat, 3 Sep 2022 14:04:44 +0800 Subject: [PATCH 45/52] Start database api work --- script/script/api/create/1.title.sql | 22 ++++++++++ script/script/table/create/0.base.sql | 25 +++++++++++ script/script/table/create/1.title.sql | 8 ++++ script/script/table/create/10.status.sql | 29 +++++++++++++ script/script/table/create/11.patterns.sql | 7 ++++ script/script/table/create/12.curves.sql | 8 ++++ script/script/table/create/13.controls.sql | 30 +++++++++++++ script/script/table/create/14.rules.sql | 6 +++ script/script/table/create/15.energy.sql | 24 +++++++++++ script/script/table/create/16.emitters.sql | 7 ++++ script/script/table/create/17.quality.sql | 7 ++++ script/script/table/create/18.sources.sql | 11 +++++ script/script/table/create/19.reactions.sql | 42 +++++++++++++++++++ script/script/table/create/2.junctions.sql | 9 ++++ script/script/table/create/20.mixing.sql | 10 +++++ script/script/table/create/21.times.sql | 9 ++++ script/script/table/create/22.report.sql | 9 ++++ script/script/table/create/23.options.sql | 9 ++++ script/script/table/create/24.coordinates.sql | 10 +++++ script/script/table/create/25.vertices.sql | 8 ++++ script/script/table/create/26.labels.sql | 9 ++++ script/script/table/create/27.backdrop.sql | 6 +++ script/script/table/create/28.end.sql | 1 + script/script/table/create/3.reservoirs.sql | 8 ++++ script/script/table/create/4.tanks.sql | 16 +++++++ script/script/table/create/5.pipes.sql | 15 +++++++ script/script/table/create/6.pumps.sql | 32 ++++++++++++++ script/script/table/create/7.valves.sql | 14 +++++++ script/script/table/create/8.tags.sql | 13 ++++++ script/script/table/create/9.demands.sql | 9 ++++ script/script/table/create/namespace.sql | 1 + script/script/table/create/operation.sql | 29 +++++++++++++ script/script/table/drop/0.base.sql | 11 +++++ script/script/table/drop/1.title.sql | 3 ++ script/script/table/drop/10.status.sql | 13 ++++++ script/script/table/drop/11.patterns.sql | 3 ++ script/script/table/drop/12.curves.sql | 3 ++ script/script/table/drop/13.controls.sql | 9 ++++ script/script/table/drop/14.rules.sql | 3 ++ script/script/table/drop/15.energy.sql | 9 ++++ script/script/table/drop/16.emitters.sql | 3 ++ script/script/table/drop/17.quality.sql | 3 ++ script/script/table/drop/18.sources.sql | 5 +++ script/script/table/drop/19.reactions.sql | 19 +++++++++ script/script/table/drop/2.junctions.sql | 3 ++ script/script/table/drop/20.mixing.sql | 5 +++ script/script/table/drop/21.times.sql | 3 ++ script/script/table/drop/22.report.sql | 3 ++ script/script/table/drop/23.options.sql | 3 ++ script/script/table/drop/24.coordinates.sql | 7 ++++ script/script/table/drop/25.vertices.sql | 3 ++ script/script/table/drop/26.labels.sql | 3 ++ script/script/table/drop/27.backdrop.sql | 3 ++ script/script/table/drop/28.end.sql | 1 + script/script/table/drop/3.reservoirs.sql | 3 ++ script/script/table/drop/4.tanks.sql | 5 +++ script/script/table/drop/5.pipes.sql | 5 +++ script/script/table/drop/6.pumps.sql | 11 +++++ script/script/table/drop/7.valves.sql | 5 +++ script/script/table/drop/8.tags.sql | 5 +++ script/script/table/drop/9.demands.sql | 3 ++ script/script/table/drop/namespace.sql | 1 + script/script/table/drop/operation.sql | 7 ++++ 63 files changed, 606 insertions(+) create mode 100644 script/script/api/create/1.title.sql create mode 100644 script/script/table/create/0.base.sql create mode 100644 script/script/table/create/1.title.sql create mode 100644 script/script/table/create/10.status.sql create mode 100644 script/script/table/create/11.patterns.sql create mode 100644 script/script/table/create/12.curves.sql create mode 100644 script/script/table/create/13.controls.sql create mode 100644 script/script/table/create/14.rules.sql create mode 100644 script/script/table/create/15.energy.sql create mode 100644 script/script/table/create/16.emitters.sql create mode 100644 script/script/table/create/17.quality.sql create mode 100644 script/script/table/create/18.sources.sql create mode 100644 script/script/table/create/19.reactions.sql create mode 100644 script/script/table/create/2.junctions.sql create mode 100644 script/script/table/create/20.mixing.sql create mode 100644 script/script/table/create/21.times.sql create mode 100644 script/script/table/create/22.report.sql create mode 100644 script/script/table/create/23.options.sql create mode 100644 script/script/table/create/24.coordinates.sql create mode 100644 script/script/table/create/25.vertices.sql create mode 100644 script/script/table/create/26.labels.sql create mode 100644 script/script/table/create/27.backdrop.sql create mode 100644 script/script/table/create/28.end.sql create mode 100644 script/script/table/create/3.reservoirs.sql create mode 100644 script/script/table/create/4.tanks.sql create mode 100644 script/script/table/create/5.pipes.sql create mode 100644 script/script/table/create/6.pumps.sql create mode 100644 script/script/table/create/7.valves.sql create mode 100644 script/script/table/create/8.tags.sql create mode 100644 script/script/table/create/9.demands.sql create mode 100644 script/script/table/create/namespace.sql create mode 100644 script/script/table/create/operation.sql create mode 100644 script/script/table/drop/0.base.sql create mode 100644 script/script/table/drop/1.title.sql create mode 100644 script/script/table/drop/10.status.sql create mode 100644 script/script/table/drop/11.patterns.sql create mode 100644 script/script/table/drop/12.curves.sql create mode 100644 script/script/table/drop/13.controls.sql create mode 100644 script/script/table/drop/14.rules.sql create mode 100644 script/script/table/drop/15.energy.sql create mode 100644 script/script/table/drop/16.emitters.sql create mode 100644 script/script/table/drop/17.quality.sql create mode 100644 script/script/table/drop/18.sources.sql create mode 100644 script/script/table/drop/19.reactions.sql create mode 100644 script/script/table/drop/2.junctions.sql create mode 100644 script/script/table/drop/20.mixing.sql create mode 100644 script/script/table/drop/21.times.sql create mode 100644 script/script/table/drop/22.report.sql create mode 100644 script/script/table/drop/23.options.sql create mode 100644 script/script/table/drop/24.coordinates.sql create mode 100644 script/script/table/drop/25.vertices.sql create mode 100644 script/script/table/drop/26.labels.sql create mode 100644 script/script/table/drop/27.backdrop.sql create mode 100644 script/script/table/drop/28.end.sql create mode 100644 script/script/table/drop/3.reservoirs.sql create mode 100644 script/script/table/drop/4.tanks.sql create mode 100644 script/script/table/drop/5.pipes.sql create mode 100644 script/script/table/drop/6.pumps.sql create mode 100644 script/script/table/drop/7.valves.sql create mode 100644 script/script/table/drop/8.tags.sql create mode 100644 script/script/table/drop/9.demands.sql create mode 100644 script/script/table/drop/namespace.sql create mode 100644 script/script/table/drop/operation.sql diff --git a/script/script/api/create/1.title.sql b/script/script/api/create/1.title.sql new file mode 100644 index 0000000..31340b8 --- /dev/null +++ b/script/script/api/create/1.title.sql @@ -0,0 +1,22 @@ +-- get_title() +create function get_title() returns text as +$$ + select value from title; +$$ language sql; + +-- update_title() +create function update_title(new_title text) returns void as +$$ +declare + old_title text; + redo text; + undo text; +begin + select get_title() into old_title; + update title set value = new_title where value = old_title; + + redo := concat('update title set value = ''', new_title, ''' where value = ''', old_title, ''''); + undo := concat('update title set value = ''', old_title, ''' where value = ''', new_title, ''''); + insert into operation values (default, redo, undo); +end; +$$ language plpgsql; diff --git a/script/script/table/create/0.base.sql b/script/script/table/create/0.base.sql new file mode 100644 index 0000000..5ae4026 --- /dev/null +++ b/script/script/table/create/0.base.sql @@ -0,0 +1,25 @@ +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..c00e981 --- /dev/null +++ b/script/script/table/create/1.title.sql @@ -0,0 +1,8 @@ +-- [TITLE] + +CREATE TABLE TITLE +( + Value TEXT +); + +INSERT INTO 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..34da1f7 --- /dev/null +++ b/script/script/table/create/10.status.sql @@ -0,0 +1,29 @@ +-- [STATUS] + +CREATE TYPE STATUS_PIPE_PUMP_STATUS AS ENUM ('OPEN', 'CLOSED'); + +CREATE TABLE STATUS_PIPE +( + ID VARCHAR(32) PRIMARY KEY REFERENCES PIPES(ID) +, Status STATUS_PIPE_PUMP_STATUS NOT NULL +); + +CREATE TABLE STATUS_PUMP +( + ID VARCHAR(32) PRIMARY KEY REFERENCES PUMPS(ID) +, Status STATUS_PIPE_PUMP_STATUS NOT NULL +); + +CREATE TYPE STATUS_VALVE_STATUS AS ENUM ('OPEN', 'CLOSED', 'ACTIVE'); + +CREATE TABLE STATUS_VALVE +( + ID VARCHAR(32) PRIMARY KEY REFERENCES VALVES(ID) +, Status STATUS_VALVE_STATUS NOT NULL +); + +CREATE TABLE STATUS_LINK +( + ID VARCHAR(32) PRIMARY KEY REFERENCES _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..dfb23b0 --- /dev/null +++ b/script/script/table/create/11.patterns.sql @@ -0,0 +1,7 @@ +-- [PATTERNS] + +CREATE TABLE PATTERNS +( + ID VARCHAR(32) REFERENCES _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..ac2460e --- /dev/null +++ b/script/script/table/create/12.curves.sql @@ -0,0 +1,8 @@ +-- [CURVES] + +CREATE TABLE CURVES +( + ID VARCHAR(32) REFERENCES _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..aa107eb --- /dev/null +++ b/script/script/table/create/13.controls.sql @@ -0,0 +1,30 @@ +-- [CONTROLS] + +CREATE TYPE CONTROLS_1_PREP AS ENUM ('ABOVE', 'BELOW'); + +-- LINK linkID status IF NODE nodeID ABOVE / BELOW value +CREATE TABLE CONTROLS_1 +( + LinkID VARCHAR(32) PRIMARY KEY REFERENCES _LINK(ID) +, Status TEXT NOT NULL -- OPEN / CLOSED, a pump speed setting, or a control valve setting +, NodeID VARCHAR(32) REFERENCES _NODE(ID) NOT NULL +, Prep CONTROLS_1_PREP NOT NULL +, Value NUMERIC NOT NULL +); + +-- LINK linkID status AT TIME time +CREATE TABLE CONTROLS_2 +( + LinkID VARCHAR(32) PRIMARY KEY REFERENCES _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 CONTROLS_3 +( + LinkID VARCHAR(32) PRIMARY KEY REFERENCES _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..7d288ca --- /dev/null +++ b/script/script/table/create/14.rules.sql @@ -0,0 +1,6 @@ +-- [RULES] + +CREATE TABLE 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..a73a3db --- /dev/null +++ b/script/script/table/create/15.energy.sql @@ -0,0 +1,24 @@ +-- [ENERGY] + +CREATE TYPE ENERGY_PARAM AS ENUM ('PRICE', 'PATTERN', 'EFFIC'); + +-- GLOBAL PRICE / PATTERN / EFFIC value +CREATE TABLE ENERGY_GLOBAL +( + Param ENERGY_PARAM NOT NULL +, Value NUMERIC NOT NULL +); + +-- PUMP pumpID PRICE / PATTERN / EFFIC value +CREATE TABLE ENERGY_PUMP +( + ID VARCHAR(32) REFERENCES PUMPS(ID) NOT NULL +, Param ENERGY_PARAM NOT NULL +, Value NUMERIC NOT NULL +); + +-- DEMAND CHARGE value +CREATE TABLE 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..b1fe8b4 --- /dev/null +++ b/script/script/table/create/16.emitters.sql @@ -0,0 +1,7 @@ +-- [EMITTERS] + +CREATE TABLE EMITTERS +( + Junction VARCHAR(32) PRIMARY KEY REFERENCES 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..939e309 --- /dev/null +++ b/script/script/table/create/17.quality.sql @@ -0,0 +1,7 @@ +-- [QUALITY] + +CREATE TABLE QUALITY +( + Node VARCHAR(32) PRIMARY KEY REFERENCES _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..1c2ec64 --- /dev/null +++ b/script/script/table/create/18.sources.sql @@ -0,0 +1,11 @@ +-- [SOURCES] + +CREATE TYPE SOURCES_TYPE AS ENUM ('CONCEN', 'MASS', 'FLOWPACED', 'SETPOINT'); + +CREATE TABLE SOURCES +( + Node VARCHAR(32) PRIMARY KEY REFERENCES _NODE(ID) +, Type SOURCES_TYPE NOT NULL +, Strength NUMERIC NOT NULL +, TimePattern VARCHAR(32) REFERENCES _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..0d8d13b --- /dev/null +++ b/script/script/table/create/19.reactions.sql @@ -0,0 +1,42 @@ +-- [REACTIONS] + +CREATE TYPE REACTIONS_ORDER_PARAM AS ENUM ('BULK', 'WALL', 'TANK'); + +CREATE TABLE REACTIONS_ORDER +( + Key REACTIONS_ORDER_PARAM NOT NULL +, Value NUMERIC NOT NULL +); + +CREATE TYPE REACTIONS_GLOBAL_PARAM AS ENUM ('BULK', 'WALL'); + +CREATE TABLE REACTIONS_GLOBAL +( + Key REACTIONS_GLOBAL_PARAM NOT NULL +, Value NUMERIC NOT NULL +); + +CREATE TYPE REACTIONS_PIPE_PARAM AS ENUM ('BULK', 'WALL'); + +CREATE TABLE REACTIONS_PIPE +( + Key REACTIONS_PIPE_PARAM NOT NULL +, Pipe VARCHAR(32) REFERENCES PIPES(ID) NOT NULL +, Value NUMERIC NOT NULL +); + +CREATE TABLE REACTIONS_TANK +( + Tank VARCHAR(32) REFERENCES TANKS(ID) NOT NULL +, Value NUMERIC NOT NULL +); + +CREATE TABLE REACTIONS_LIMITING_POTENTIAL +( + Value NUMERIC NOT NULL +); + +CREATE TABLE 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..42ea2f3 --- /dev/null +++ b/script/script/table/create/2.junctions.sql @@ -0,0 +1,9 @@ +-- [JUNCTIONS] + +CREATE TABLE JUNCTIONS +( + ID VARCHAR(32) PRIMARY KEY REFERENCES _NODE(ID) +, Elevation NUMERIC NOT NULL +, Demand NUMERIC +, Pattern VARCHAR(32) REFERENCES _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..b6675bb --- /dev/null +++ b/script/script/table/create/20.mixing.sql @@ -0,0 +1,10 @@ +-- [MIXING] + +CREATE TYPE MIXING_Model AS ENUM ('MIXED', '2COMP', 'FIFO', 'LIFO'); + +CREATE TABLE MIXING +( + Tank VARCHAR(32) PRIMARY KEY REFERENCES TANKS(ID) +, Model 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..a68e504 --- /dev/null +++ b/script/script/table/create/21.times.sql @@ -0,0 +1,9 @@ +-- [TIMES] + +-- TODO: constraint + +CREATE TABLE 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..c2435d3 --- /dev/null +++ b/script/script/table/create/22.report.sql @@ -0,0 +1,9 @@ +-- [REPORT] + +-- TODO: constraint + +CREATE TABLE 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..16d50a4 --- /dev/null +++ b/script/script/table/create/23.options.sql @@ -0,0 +1,9 @@ +-- [OPTIONS] + +-- TODO: constraint + +CREATE TABLE 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..5b6ed6d --- /dev/null +++ b/script/script/table/create/24.coordinates.sql @@ -0,0 +1,10 @@ +-- [COORDINATES] + +CREATE TABLE COORDINATES +( + Node VARCHAR(32) PRIMARY KEY REFERENCES _NODE(ID) +, Coord POINT NOT NULL +); + +CREATE INDEX COORDINATES_SPGIST ON COORDINATES USING SPGIST(Coord); +CREATE INDEX COORDINATES_GIST ON 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..3f509c6 --- /dev/null +++ b/script/script/table/create/25.vertices.sql @@ -0,0 +1,8 @@ +-- [VERTICES] + +CREATE TABLE VERTICES +( + Link VARCHAR(32) REFERENCES _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..cf95e00 --- /dev/null +++ b/script/script/table/create/26.labels.sql @@ -0,0 +1,9 @@ +-- [LABELS] + +CREATE TABLE LABELS +( + X NUMERIC NOT NULL +, Y NUMERIC NOT NULL +, Label TEXT NOT NULL +, AnchorNode VARCHAR(32) REFERENCES _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..4216368 --- /dev/null +++ b/script/script/table/create/27.backdrop.sql @@ -0,0 +1,6 @@ +-- [BACKDROP] + +CREATE TABLE BACKDROP +( + Content TEXT PRIMARY KEY +); diff --git a/script/script/table/create/28.end.sql b/script/script/table/create/28.end.sql new file mode 100644 index 0000000..3054ad0 --- /dev/null +++ b/script/script/table/create/28.end.sql @@ -0,0 +1 @@ +-- [END] \ No newline at end of file diff --git a/script/script/table/create/3.reservoirs.sql b/script/script/table/create/3.reservoirs.sql new file mode 100644 index 0000000..476c793 --- /dev/null +++ b/script/script/table/create/3.reservoirs.sql @@ -0,0 +1,8 @@ +-- [RESERVOIRS] + +CREATE TABLE RESERVOIRS +( + ID VARCHAR(32) PRIMARY KEY REFERENCES _NODE(ID) +, Head NUMERIC NOT NULL +, Pattern VARCHAR(32) REFERENCES _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..df0bdf4 --- /dev/null +++ b/script/script/table/create/4.tanks.sql @@ -0,0 +1,16 @@ +-- [TANKS] + +CREATE TYPE TANKS_OVERFLOW AS ENUM ('YES', 'NO'); + +CREATE TABLE TANKS +( + ID VARCHAR(32) PRIMARY KEY REFERENCES _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 _CURVE(ID) +, Overflow 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..fb1dc2d --- /dev/null +++ b/script/script/table/create/5.pipes.sql @@ -0,0 +1,15 @@ +-- [PIPES] + +CREATE TYPE PIPES_STATUS AS ENUM ('OPEN', 'CLOSED', 'CV'); + +CREATE TABLE PIPES +( + ID VARCHAR(32) PRIMARY KEY REFERENCES _LINK(ID) +, Node1 VARCHAR(32) REFERENCES _NODE(ID) NOT NULL +, Node2 VARCHAR(32) REFERENCES _NODE(ID) NOT NULL +, Length NUMERIC NOT NULL +, Diameter NUMERIC NOT NULL +, Roughness NUMERIC NOT NULL +, MinorLoss NUMERIC NOT NULL +, Status 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..b701708 --- /dev/null +++ b/script/script/table/create/6.pumps.sql @@ -0,0 +1,32 @@ +-- [PUMPS] + +CREATE TABLE PUMPS +( + ID VARCHAR(32) PRIMARY KEY REFERENCES _LINK(ID) +, Node1 VARCHAR(32) REFERENCES _NODE(ID) NOT NULL +, Node2 VARCHAR(32) REFERENCES _NODE(ID) NOT NULL +); + +CREATE TABLE PUMPS_PROPERTY_POWER +( + ID VARCHAR PRIMARY KEY REFERENCES PUMPS(ID) +, Value VARCHAR(32) NOT NULL +); + +CREATE TABLE PUMPS_PROPERTY_SPEED +( + ID VARCHAR PRIMARY KEY REFERENCES PUMPS(ID) +, Value VARCHAR(32) NOT NULL +); + +CREATE TABLE PUMPS_PROPERTY_HEAD +( + ID VARCHAR PRIMARY KEY REFERENCES PUMPS(ID) +, Head VARCHAR(32) REFERENCES _CURVE(ID) NOT NULL +); + +CREATE TABLE PUMPS_PROPERTY_PATTERN +( + ID VARCHAR PRIMARY KEY REFERENCES PUMPS(ID) +, Pattern VARCHAR(32) REFERENCES _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..7159aac --- /dev/null +++ b/script/script/table/create/7.valves.sql @@ -0,0 +1,14 @@ +-- [VALVES] + +CREATE TYPE VALVES_TYPE AS ENUM ('PRV', 'PSV', 'PBV', 'FCV', 'TCV', 'GPV'); + +CREATE TABLE VALVES +( + ID VARCHAR(32) PRIMARY KEY REFERENCES _LINK(ID) +, Node1 VARCHAR(32) REFERENCES _NODE(ID) NOT NULL +, Node2 VARCHAR(32) REFERENCES _NODE(ID) NOT NULL +, Diameter NUMERIC NOT NULL +, Type 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..0cc496c --- /dev/null +++ b/script/script/table/create/8.tags.sql @@ -0,0 +1,13 @@ +-- [TAGS] + +CREATE TABLE TAGS_NODE +( + ID VARCHAR(32) PRIMARY KEY REFERENCES _NODE(ID) +, Tag TEXT NOT NULL +); + +CREATE TABLE TAGS_LINK +( + ID VARCHAR(32) PRIMARY KEY REFERENCES _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..355354d --- /dev/null +++ b/script/script/table/create/9.demands.sql @@ -0,0 +1,9 @@ +-- [DEMANDS] + +CREATE TABLE DEMANDS +( + Junction VARCHAR(32) REFERENCES JUNCTIONS(ID) NOT NULL +, Demand NUMERIC NOT NULL +, Pattern VARCHAR(32) REFERENCES _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..2f105e6 --- /dev/null +++ b/script/script/table/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/script/table/drop/0.base.sql b/script/script/table/drop/0.base.sql new file mode 100644 index 0000000..3b57bb0 --- /dev/null +++ b/script/script/table/drop/0.base.sql @@ -0,0 +1,11 @@ +DROP TABLE IF EXISTS _PATTERN; + +DROP TABLE IF EXISTS _CURVE; + +DROP TABLE IF EXISTS _LINK; + +DROP TABLE IF EXISTS _NODE; + +DROP TYPE IF EXISTS _LINK_TYPE; + +DROP TYPE IF EXISTS _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..7ee6d55 --- /dev/null +++ b/script/script/table/drop/1.title.sql @@ -0,0 +1,3 @@ +-- [TITLE] + +DROP TABLE IF EXISTS 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..322a72f --- /dev/null +++ b/script/script/table/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/script/table/drop/11.patterns.sql b/script/script/table/drop/11.patterns.sql new file mode 100644 index 0000000..fd08d97 --- /dev/null +++ b/script/script/table/drop/11.patterns.sql @@ -0,0 +1,3 @@ +-- [PATTERNS] + +DROP TABLE IF EXISTS 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..577464e --- /dev/null +++ b/script/script/table/drop/12.curves.sql @@ -0,0 +1,3 @@ +-- [CURVES] + +DROP TABLE IF EXISTS 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..476259d --- /dev/null +++ b/script/script/table/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/script/table/drop/14.rules.sql b/script/script/table/drop/14.rules.sql new file mode 100644 index 0000000..84e4fd8 --- /dev/null +++ b/script/script/table/drop/14.rules.sql @@ -0,0 +1,3 @@ +-- [RULES] + +DROP TABLE IF EXISTS 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..5002d5c --- /dev/null +++ b/script/script/table/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/script/table/drop/16.emitters.sql b/script/script/table/drop/16.emitters.sql new file mode 100644 index 0000000..40a0af8 --- /dev/null +++ b/script/script/table/drop/16.emitters.sql @@ -0,0 +1,3 @@ +-- [EMITTERS] + +DROP TABLE IF EXISTS 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..b72c290 --- /dev/null +++ b/script/script/table/drop/17.quality.sql @@ -0,0 +1,3 @@ +-- [QUALITY] + +DROP TABLE IF EXISTS 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..0402ffe --- /dev/null +++ b/script/script/table/drop/18.sources.sql @@ -0,0 +1,5 @@ +-- [SOURCES] + +DROP TABLE IF EXISTS SOURCES; + +DROP TYPE IF EXISTS 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..2f6ba5b --- /dev/null +++ b/script/script/table/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/script/table/drop/2.junctions.sql b/script/script/table/drop/2.junctions.sql new file mode 100644 index 0000000..ef0a609 --- /dev/null +++ b/script/script/table/drop/2.junctions.sql @@ -0,0 +1,3 @@ +-- [JUNCTIONS] + +DROP TABLE IF EXISTS 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..938d554 --- /dev/null +++ b/script/script/table/drop/20.mixing.sql @@ -0,0 +1,5 @@ +-- [MIXING] + +DROP TABLE IF EXISTS MIXING; + +DROP TYPE IF EXISTS 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..f051bd7 --- /dev/null +++ b/script/script/table/drop/21.times.sql @@ -0,0 +1,3 @@ +-- [TIMES] + +DROP TABLE IF EXISTS 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..790684d --- /dev/null +++ b/script/script/table/drop/22.report.sql @@ -0,0 +1,3 @@ +-- [REPORT] + +DROP TABLE IF EXISTS 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..7cc629a --- /dev/null +++ b/script/script/table/drop/23.options.sql @@ -0,0 +1,3 @@ +-- [OPTIONS] + +DROP TABLE IF EXISTS 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..04d0465 --- /dev/null +++ b/script/script/table/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/script/table/drop/25.vertices.sql b/script/script/table/drop/25.vertices.sql new file mode 100644 index 0000000..f74dd3e --- /dev/null +++ b/script/script/table/drop/25.vertices.sql @@ -0,0 +1,3 @@ +-- [VERTICES] + +DROP TABLE IF EXISTS 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..aa1b2b2 --- /dev/null +++ b/script/script/table/drop/26.labels.sql @@ -0,0 +1,3 @@ +-- [LABELS] + +DROP TABLE IF EXISTS 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..408bb85 --- /dev/null +++ b/script/script/table/drop/27.backdrop.sql @@ -0,0 +1,3 @@ +-- [BACKDROP] + +DROP TABLE IF EXISTS BACKDROP; diff --git a/script/script/table/drop/28.end.sql b/script/script/table/drop/28.end.sql new file mode 100644 index 0000000..3054ad0 --- /dev/null +++ b/script/script/table/drop/28.end.sql @@ -0,0 +1 @@ +-- [END] \ No newline at end of file diff --git a/script/script/table/drop/3.reservoirs.sql b/script/script/table/drop/3.reservoirs.sql new file mode 100644 index 0000000..9864c18 --- /dev/null +++ b/script/script/table/drop/3.reservoirs.sql @@ -0,0 +1,3 @@ +-- [RESERVOIRS] + +DROP TABLE IF EXISTS 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..da0487d --- /dev/null +++ b/script/script/table/drop/4.tanks.sql @@ -0,0 +1,5 @@ +-- [TANKS] + +DROP TABLE IF EXISTS TANKS; + +DROP TYPE IF EXISTS 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..1e23612 --- /dev/null +++ b/script/script/table/drop/5.pipes.sql @@ -0,0 +1,5 @@ +-- [PIPES] + +DROP TABLE IF EXISTS PIPES; + +DROP TYPE IF EXISTS 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..5ac9656 --- /dev/null +++ b/script/script/table/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/script/table/drop/7.valves.sql b/script/script/table/drop/7.valves.sql new file mode 100644 index 0000000..c95b423 --- /dev/null +++ b/script/script/table/drop/7.valves.sql @@ -0,0 +1,5 @@ +-- [VALVES] + +DROP TABLE IF EXISTS VALVES; + +DROP TYPE IF EXISTS 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..df8e3d1 --- /dev/null +++ b/script/script/table/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/script/table/drop/9.demands.sql b/script/script/table/drop/9.demands.sql new file mode 100644 index 0000000..ebe0209 --- /dev/null +++ b/script/script/table/drop/9.demands.sql @@ -0,0 +1,3 @@ +-- [DEMANDS] + +DROP TABLE IF EXISTS 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..998ee2f --- /dev/null +++ b/script/script/table/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; From 7d2cc5c1fe7655bab9742af10c95edc8a402aea9 Mon Sep 17 00:00:00 2001 From: wqy Date: Sat, 3 Sep 2022 14:38:49 +0800 Subject: [PATCH 46/52] Put all table & enum into namespace(postgres schema) 'tj' --- script/script/table/create/0.base.sql | 14 ++++- script/script/table/create/1.title.sql | 8 +-- script/script/table/create/10.status.sql | 38 ++++++------ script/script/table/create/11.patterns.sql | 8 +-- script/script/table/create/12.curves.sql | 10 +-- script/script/table/create/13.controls.sql | 40 ++++++------ script/script/table/create/14.rules.sql | 6 +- script/script/table/create/15.energy.sql | 35 ++++++----- script/script/table/create/16.emitters.sql | 8 +-- script/script/table/create/17.quality.sql | 8 +-- script/script/table/create/18.sources.sql | 22 ++++--- script/script/table/create/19.reactions.sql | 61 +++++++++++-------- script/script/table/create/2.junctions.sql | 12 ++-- script/script/table/create/20.mixing.sql | 20 +++--- script/script/table/create/21.times.sql | 10 +-- script/script/table/create/22.report.sql | 10 +-- script/script/table/create/23.options.sql | 10 +-- script/script/table/create/24.coordinates.sql | 12 ++-- script/script/table/create/25.vertices.sql | 10 +-- script/script/table/create/26.labels.sql | 12 ++-- script/script/table/create/27.backdrop.sql | 6 +- script/script/table/create/3.reservoirs.sql | 10 +-- script/script/table/create/4.tanks.sql | 30 +++++---- script/script/table/create/5.pipes.sql | 29 +++++---- script/script/table/create/6.pumps.sql | 34 +++++------ script/script/table/create/7.valves.sql | 30 +++++---- script/script/table/create/8.tags.sql | 14 ++--- script/script/table/create/9.demands.sql | 12 ++-- script/script/table/create/operation.sql | 32 +++++----- script/script/table/drop/0.base.sql | 12 ++-- script/script/table/drop/1.title.sql | 4 +- script/script/table/drop/10.status.sql | 14 ++--- script/script/table/drop/11.patterns.sql | 4 +- script/script/table/drop/12.curves.sql | 4 +- script/script/table/drop/13.controls.sql | 10 +-- script/script/table/drop/14.rules.sql | 4 +- script/script/table/drop/15.energy.sql | 10 +-- script/script/table/drop/16.emitters.sql | 4 +- script/script/table/drop/17.quality.sql | 4 +- script/script/table/drop/18.sources.sql | 6 +- script/script/table/drop/19.reactions.sql | 20 +++--- script/script/table/drop/2.junctions.sql | 4 +- script/script/table/drop/20.mixing.sql | 6 +- script/script/table/drop/21.times.sql | 4 +- script/script/table/drop/22.report.sql | 4 +- script/script/table/drop/23.options.sql | 4 +- script/script/table/drop/24.coordinates.sql | 8 +-- script/script/table/drop/25.vertices.sql | 4 +- script/script/table/drop/26.labels.sql | 4 +- script/script/table/drop/27.backdrop.sql | 4 +- script/script/table/drop/3.reservoirs.sql | 4 +- script/script/table/drop/4.tanks.sql | 6 +- script/script/table/drop/5.pipes.sql | 6 +- script/script/table/drop/6.pumps.sql | 12 ++-- script/script/table/drop/7.valves.sql | 6 +- script/script/table/drop/8.tags.sql | 6 +- script/script/table/drop/9.demands.sql | 4 +- script/script/table/drop/operation.sql | 8 +-- 58 files changed, 401 insertions(+), 340 deletions(-) diff --git a/script/script/table/create/0.base.sql b/script/script/table/create/0.base.sql index 5ae4026..91b48c0 100644 --- a/script/script/table/create/0.base.sql +++ b/script/script/table/create/0.base.sql @@ -1,6 +1,16 @@ -create type tj.node_type as enum ('junction', 'reservoir', 'tank'); +create type tj.node_type as enum +( + 'junction' +, 'reservoir' +, 'tank' +); -create type tj.link_type as enum ('pipe', 'pump', 'valve'); +create type tj.link_type as enum +( + 'pipe' +, 'pump' +, 'valve' +); create table tj.node ( diff --git a/script/script/table/create/1.title.sql b/script/script/table/create/1.title.sql index c00e981..8ba6426 100644 --- a/script/script/table/create/1.title.sql +++ b/script/script/table/create/1.title.sql @@ -1,8 +1,8 @@ --- [TITLE] +-- [title] -CREATE TABLE TITLE +create table tj.title ( - Value TEXT + value text ); -INSERT INTO TITLE (Value) VALUES (''); +insert into tj.title (value) values (''); diff --git a/script/script/table/create/10.status.sql b/script/script/table/create/10.status.sql index 34da1f7..c736c76 100644 --- a/script/script/table/create/10.status.sql +++ b/script/script/table/create/10.status.sql @@ -1,29 +1,33 @@ --- [STATUS] +-- [status] -CREATE TYPE STATUS_PIPE_PUMP_STATUS AS ENUM ('OPEN', 'CLOSED'); - -CREATE TABLE STATUS_PIPE +create type tj.status_pipe_pump_status as enum ( - ID VARCHAR(32) PRIMARY KEY REFERENCES PIPES(ID) -, Status STATUS_PIPE_PUMP_STATUS NOT NULL + 'open' +, 'closed' ); -CREATE TABLE STATUS_PUMP +create table tj.status_pipe ( - ID VARCHAR(32) PRIMARY KEY REFERENCES PUMPS(ID) -, Status STATUS_PIPE_PUMP_STATUS NOT NULL + id varchar(32) primary key references tj.pipes(id) +, status tj.status_pipe_pump_status not null ); -CREATE TYPE STATUS_VALVE_STATUS AS ENUM ('OPEN', 'CLOSED', 'ACTIVE'); - -CREATE TABLE STATUS_VALVE +create table tj.status_pump ( - ID VARCHAR(32) PRIMARY KEY REFERENCES VALVES(ID) -, Status STATUS_VALVE_STATUS NOT NULL + id varchar(32) primary key references tj.pumps(id) +, status tj.status_pipe_pump_status not null ); -CREATE TABLE STATUS_LINK +create type tj.status_valve_status as enum ('open', 'closed', 'active'); + +create table tj.status_valve ( - ID VARCHAR(32) PRIMARY KEY REFERENCES _LINK(ID) -, Setting NUMERIC NOT NULL + 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 index dfb23b0..8cd9ad6 100644 --- a/script/script/table/create/11.patterns.sql +++ b/script/script/table/create/11.patterns.sql @@ -1,7 +1,7 @@ --- [PATTERNS] +-- [patterns] -CREATE TABLE PATTERNS +create table tj.patterns ( - ID VARCHAR(32) REFERENCES _PATTERN(ID) NOT NULL -, Multipliers NUMERIC NOT NULL + 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 index ac2460e..2467450 100644 --- a/script/script/table/create/12.curves.sql +++ b/script/script/table/create/12.curves.sql @@ -1,8 +1,8 @@ --- [CURVES] +-- [curves] -CREATE TABLE CURVES +create table tj.curves ( - ID VARCHAR(32) REFERENCES _CURVE(ID) NOT NULL -, X NUMERIC NOT NULL -, Y NUMERIC NOT NULL + 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 index aa107eb..ab34bb0 100644 --- a/script/script/table/create/13.controls.sql +++ b/script/script/table/create/13.controls.sql @@ -1,30 +1,30 @@ --- [CONTROLS] +-- [controls] -CREATE TYPE CONTROLS_1_PREP AS ENUM ('ABOVE', 'BELOW'); +create type tj.controls_1_prep as enum ('above', 'below'); --- LINK linkID status IF NODE nodeID ABOVE / BELOW value -CREATE TABLE CONTROLS_1 +-- link linkid status if node nodeid above / below value +create table tj.controls_1 ( - LinkID VARCHAR(32) PRIMARY KEY REFERENCES _LINK(ID) -, Status TEXT NOT NULL -- OPEN / CLOSED, a pump speed setting, or a control valve setting -, NodeID VARCHAR(32) REFERENCES _NODE(ID) NOT NULL -, Prep CONTROLS_1_PREP NOT NULL -, Value NUMERIC NOT NULL + 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 CONTROLS_2 +-- link linkid status at time time +create table tj.controls_2 ( - LinkID VARCHAR(32) PRIMARY KEY REFERENCES _LINK(ID) -, Status TEXT NOT NULL -- OPEN / CLOSED, a pump speed setting, or a control valve setting -, Time INTERVAL HOUR + 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 CONTROLS_3 +-- link linkid status at clocktime clocktime am / pm +create table tj.controls_3 ( - LinkID VARCHAR(32) PRIMARY KEY REFERENCES _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 + 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 index 7d288ca..617da6b 100644 --- a/script/script/table/create/14.rules.sql +++ b/script/script/table/create/14.rules.sql @@ -1,6 +1,6 @@ --- [RULES] +-- [rules] -CREATE TABLE RULES +create table tj.rules ( - Content TEXT PRIMARY KEY + content text primary key ); diff --git a/script/script/table/create/15.energy.sql b/script/script/table/create/15.energy.sql index a73a3db..1544968 100644 --- a/script/script/table/create/15.energy.sql +++ b/script/script/table/create/15.energy.sql @@ -1,24 +1,29 @@ --- [ENERGY] +-- [energy] -CREATE TYPE ENERGY_PARAM AS ENUM ('PRICE', 'PATTERN', 'EFFIC'); - --- GLOBAL PRICE / PATTERN / EFFIC value -CREATE TABLE ENERGY_GLOBAL +create type tj.energy_param as enum ( - Param ENERGY_PARAM NOT NULL -, Value NUMERIC NOT NULL + 'price' +, 'pattern' +, 'effic' ); --- PUMP pumpID PRICE / PATTERN / EFFIC value -CREATE TABLE ENERGY_PUMP +-- global price / pattern / effic value +create table tj.energy_global ( - ID VARCHAR(32) REFERENCES PUMPS(ID) NOT NULL -, Param ENERGY_PARAM NOT NULL -, Value NUMERIC NOT NULL + param tj.energy_param not null +, value numeric not null ); --- DEMAND CHARGE value -CREATE TABLE ENERGY_DEMAND_CHARGE +-- pump pumpid price / pattern / effic value +create table tj.energy_pump ( - Value NUMERIC NOT NULL + 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 index b1fe8b4..448b9e1 100644 --- a/script/script/table/create/16.emitters.sql +++ b/script/script/table/create/16.emitters.sql @@ -1,7 +1,7 @@ --- [EMITTERS] +-- [emitters] -CREATE TABLE EMITTERS +create table tj.emitters ( - Junction VARCHAR(32) PRIMARY KEY REFERENCES JUNCTIONS(ID) -, Coefficient NUMERIC NOT NULL + 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 index 939e309..e972b30 100644 --- a/script/script/table/create/17.quality.sql +++ b/script/script/table/create/17.quality.sql @@ -1,7 +1,7 @@ --- [QUALITY] +-- [quality] -CREATE TABLE QUALITY +create table tj.quality ( - Node VARCHAR(32) PRIMARY KEY REFERENCES _NODE(ID) -, InitialQual NUMERIC NOT NULL + 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 index 1c2ec64..25d480d 100644 --- a/script/script/table/create/18.sources.sql +++ b/script/script/table/create/18.sources.sql @@ -1,11 +1,17 @@ --- [SOURCES] +-- [sources] -CREATE TYPE SOURCES_TYPE AS ENUM ('CONCEN', 'MASS', 'FLOWPACED', 'SETPOINT'); - -CREATE TABLE SOURCES +create type tj.sources_type as enum ( - Node VARCHAR(32) PRIMARY KEY REFERENCES _NODE(ID) -, Type SOURCES_TYPE NOT NULL -, Strength NUMERIC NOT NULL -, TimePattern VARCHAR(32) REFERENCES _PATTERN(ID) + '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 index 0d8d13b..7ddab5d 100644 --- a/script/script/table/create/19.reactions.sql +++ b/script/script/table/create/19.reactions.sql @@ -1,42 +1,55 @@ --- [REACTIONS] +-- [reactions] -CREATE TYPE REACTIONS_ORDER_PARAM AS ENUM ('BULK', 'WALL', 'TANK'); - -CREATE TABLE REACTIONS_ORDER +create type tj.reactions_order_param as enum ( - Key REACTIONS_ORDER_PARAM NOT NULL -, Value NUMERIC NOT NULL + 'bulk' +, 'wall' +, 'tank' ); -CREATE TYPE REACTIONS_GLOBAL_PARAM AS ENUM ('BULK', 'WALL'); - -CREATE TABLE REACTIONS_GLOBAL +create table tj.reactions_order ( - Key REACTIONS_GLOBAL_PARAM NOT NULL -, Value NUMERIC NOT NULL + key tj.reactions_order_param not null +, value numeric not null ); -CREATE TYPE REACTIONS_PIPE_PARAM AS ENUM ('BULK', 'WALL'); - -CREATE TABLE REACTIONS_PIPE +create type tj.reactions_global_param as enum ( - Key REACTIONS_PIPE_PARAM NOT NULL -, Pipe VARCHAR(32) REFERENCES PIPES(ID) NOT NULL -, Value NUMERIC NOT NULL + 'bulk' +, 'wall' ); -CREATE TABLE REACTIONS_TANK +create table tj.reactions_global ( - Tank VARCHAR(32) REFERENCES TANKS(ID) NOT NULL -, Value NUMERIC NOT NULL + key tj.reactions_global_param not null +, value numeric not null ); -CREATE TABLE REACTIONS_LIMITING_POTENTIAL +create type tj.reactions_pipe_param as enum ( - Value NUMERIC NOT NULL + 'bulk' +, 'wall' ); -CREATE TABLE REACTIONS_ROUGHNESS_CORRELATION +create table tj.reactions_pipe ( - Value NUMERIC NOT NULL + 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 index 42ea2f3..6397246 100644 --- a/script/script/table/create/2.junctions.sql +++ b/script/script/table/create/2.junctions.sql @@ -1,9 +1,9 @@ --- [JUNCTIONS] +-- [junctions] -CREATE TABLE JUNCTIONS +create table tj.junctions ( - ID VARCHAR(32) PRIMARY KEY REFERENCES _NODE(ID) -, Elevation NUMERIC NOT NULL -, Demand NUMERIC -, Pattern VARCHAR(32) REFERENCES _PATTERN(ID) + 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 index b6675bb..65acf21 100644 --- a/script/script/table/create/20.mixing.sql +++ b/script/script/table/create/20.mixing.sql @@ -1,10 +1,16 @@ --- [MIXING] +-- [mixing] -CREATE TYPE MIXING_Model AS ENUM ('MIXED', '2COMP', 'FIFO', 'LIFO'); - -CREATE TABLE MIXING +create type tj.mixing_model as enum ( - Tank VARCHAR(32) PRIMARY KEY REFERENCES TANKS(ID) -, Model MIXING_Model NOT NULL -, Value NUMERIC + '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 index a68e504..f66ce2d 100644 --- a/script/script/table/create/21.times.sql +++ b/script/script/table/create/21.times.sql @@ -1,9 +1,9 @@ --- [TIMES] +-- [times] --- TODO: constraint +-- todo: constraint -CREATE TABLE TIMES +create table tj.times ( - Key TEXT NOT NULL -, Value TEXT NOT NULL + 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 index c2435d3..4434a05 100644 --- a/script/script/table/create/22.report.sql +++ b/script/script/table/create/22.report.sql @@ -1,9 +1,9 @@ --- [REPORT] +-- [report] --- TODO: constraint +-- todo: constraint -CREATE TABLE REPORT +create table tj.report ( - Key TEXT NOT NULL -, Value TEXT NOT NULL + 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 index 16d50a4..cab34f4 100644 --- a/script/script/table/create/23.options.sql +++ b/script/script/table/create/23.options.sql @@ -1,9 +1,9 @@ --- [OPTIONS] +-- [options] --- TODO: constraint +-- todo: constraint -CREATE TABLE OPTIONS +create table tj.options ( - Key TEXT NOT NULL -, Value TEXT NOT NULL + 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 index 5b6ed6d..4804896 100644 --- a/script/script/table/create/24.coordinates.sql +++ b/script/script/table/create/24.coordinates.sql @@ -1,10 +1,10 @@ --- [COORDINATES] +-- [coordinates] -CREATE TABLE COORDINATES +create table tj.coordinates ( - Node VARCHAR(32) PRIMARY KEY REFERENCES _NODE(ID) -, Coord POINT NOT NULL + node varchar(32) primary key references tj.node(id) +, coord point not null ); -CREATE INDEX COORDINATES_SPGIST ON COORDINATES USING SPGIST(Coord); -CREATE INDEX COORDINATES_GIST ON COORDINATES USING GIST(Coord); +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 index 3f509c6..7b4d403 100644 --- a/script/script/table/create/25.vertices.sql +++ b/script/script/table/create/25.vertices.sql @@ -1,8 +1,8 @@ --- [VERTICES] +-- [vertices] -CREATE TABLE VERTICES +create table tj.vertices ( - Link VARCHAR(32) REFERENCES _LINK(ID) NOT NULL -, X NUMERIC NOT NULL -, Y NUMERIC NOT NULL + 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 index cf95e00..3b41fce 100644 --- a/script/script/table/create/26.labels.sql +++ b/script/script/table/create/26.labels.sql @@ -1,9 +1,9 @@ --- [LABELS] +-- [labels] -CREATE TABLE LABELS +create table tj.labels ( - X NUMERIC NOT NULL -, Y NUMERIC NOT NULL -, Label TEXT NOT NULL -, AnchorNode VARCHAR(32) REFERENCES _NODE(ID) + 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 index 4216368..aea6d3b 100644 --- a/script/script/table/create/27.backdrop.sql +++ b/script/script/table/create/27.backdrop.sql @@ -1,6 +1,6 @@ --- [BACKDROP] +-- [backdrop] -CREATE TABLE BACKDROP +create table tj.backdrop ( - Content TEXT PRIMARY KEY + content text primary key ); diff --git a/script/script/table/create/3.reservoirs.sql b/script/script/table/create/3.reservoirs.sql index 476c793..e7562ec 100644 --- a/script/script/table/create/3.reservoirs.sql +++ b/script/script/table/create/3.reservoirs.sql @@ -1,8 +1,8 @@ --- [RESERVOIRS] +-- [reservoirs] -CREATE TABLE RESERVOIRS +create table tj.reservoirs ( - ID VARCHAR(32) PRIMARY KEY REFERENCES _NODE(ID) -, Head NUMERIC NOT NULL -, Pattern VARCHAR(32) REFERENCES _PATTERN(ID) + 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 index df0bdf4..5a9a815 100644 --- a/script/script/table/create/4.tanks.sql +++ b/script/script/table/create/4.tanks.sql @@ -1,16 +1,20 @@ --- [TANKS] +-- [tanks] -CREATE TYPE TANKS_OVERFLOW AS ENUM ('YES', 'NO'); - -CREATE TABLE TANKS +create type tj.tanks_overflow as enum ( - ID VARCHAR(32) PRIMARY KEY REFERENCES _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 _CURVE(ID) -, Overflow TANKS_OVERFLOW + '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 index fb1dc2d..d7ec73e 100644 --- a/script/script/table/create/5.pipes.sql +++ b/script/script/table/create/5.pipes.sql @@ -1,15 +1,20 @@ --- [PIPES] +-- [pipes] -CREATE TYPE PIPES_STATUS AS ENUM ('OPEN', 'CLOSED', 'CV'); - -CREATE TABLE PIPES +create type tj.pipes_status as enum ( - ID VARCHAR(32) PRIMARY KEY REFERENCES _LINK(ID) -, Node1 VARCHAR(32) REFERENCES _NODE(ID) NOT NULL -, Node2 VARCHAR(32) REFERENCES _NODE(ID) NOT NULL -, Length NUMERIC NOT NULL -, Diameter NUMERIC NOT NULL -, Roughness NUMERIC NOT NULL -, MinorLoss NUMERIC NOT NULL -, Status PIPES_STATUS NOT NULL + '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 index b701708..0f5b0cf 100644 --- a/script/script/table/create/6.pumps.sql +++ b/script/script/table/create/6.pumps.sql @@ -1,32 +1,32 @@ --- [PUMPS] +-- [pumps] -CREATE TABLE PUMPS +create table tj.pumps ( - ID VARCHAR(32) PRIMARY KEY REFERENCES _LINK(ID) -, Node1 VARCHAR(32) REFERENCES _NODE(ID) NOT NULL -, Node2 VARCHAR(32) REFERENCES _NODE(ID) NOT NULL + 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 PUMPS_PROPERTY_POWER +create table tj.pumps_property_power ( - ID VARCHAR PRIMARY KEY REFERENCES PUMPS(ID) -, Value VARCHAR(32) NOT NULL + id varchar primary key references tj.pumps(id) +, value varchar(32) not null ); -CREATE TABLE PUMPS_PROPERTY_SPEED +create table tj.pumps_property_speed ( - ID VARCHAR PRIMARY KEY REFERENCES PUMPS(ID) -, Value VARCHAR(32) NOT NULL + id varchar primary key references tj.pumps(id) +, value varchar(32) not null ); -CREATE TABLE PUMPS_PROPERTY_HEAD +create table tj.pumps_property_head ( - ID VARCHAR PRIMARY KEY REFERENCES PUMPS(ID) -, Head VARCHAR(32) REFERENCES _CURVE(ID) NOT NULL + id varchar primary key references tj.pumps(id) +, head varchar(32) references tj.curve(id) not null ); -CREATE TABLE PUMPS_PROPERTY_PATTERN +create table tj.pumps_property_pattern ( - ID VARCHAR PRIMARY KEY REFERENCES PUMPS(ID) -, Pattern VARCHAR(32) REFERENCES _PATTERN(ID) NOT NULL + 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 index 7159aac..5aa584d 100644 --- a/script/script/table/create/7.valves.sql +++ b/script/script/table/create/7.valves.sql @@ -1,14 +1,22 @@ --- [VALVES] +-- [valves] -CREATE TYPE VALVES_TYPE AS ENUM ('PRV', 'PSV', 'PBV', 'FCV', 'TCV', 'GPV'); - -CREATE TABLE VALVES +create type tj.valves_type as enum ( - ID VARCHAR(32) PRIMARY KEY REFERENCES _LINK(ID) -, Node1 VARCHAR(32) REFERENCES _NODE(ID) NOT NULL -, Node2 VARCHAR(32) REFERENCES _NODE(ID) NOT NULL -, Diameter NUMERIC NOT NULL -, Type VALVES_TYPE NOT NULL -, Setting NUMERIC NOT NULL -, MinorLoss NUMERIC NOT NULL + '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 index 0cc496c..d98132c 100644 --- a/script/script/table/create/8.tags.sql +++ b/script/script/table/create/8.tags.sql @@ -1,13 +1,13 @@ --- [TAGS] +-- [tags] -CREATE TABLE TAGS_NODE +create table tj.tags_node ( - ID VARCHAR(32) PRIMARY KEY REFERENCES _NODE(ID) -, Tag TEXT NOT NULL + id varchar(32) primary key references tj.node(id) +, tag text not null ); -CREATE TABLE TAGS_LINK +create table tj.tags_link ( - ID VARCHAR(32) PRIMARY KEY REFERENCES _LINK(ID) -, Tag TEXT NOT NULL + 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 index 355354d..e908227 100644 --- a/script/script/table/create/9.demands.sql +++ b/script/script/table/create/9.demands.sql @@ -1,9 +1,9 @@ --- [DEMANDS] +-- [demands] -CREATE TABLE DEMANDS +create table tj.demands ( - Junction VARCHAR(32) REFERENCES JUNCTIONS(ID) NOT NULL -, Demand NUMERIC NOT NULL -, Pattern VARCHAR(32) REFERENCES _PATTERN(ID) -, Category TEXT NOT NULL + 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/operation.sql b/script/script/table/create/operation.sql index 2f105e6..524ef9e 100644 --- a/script/script/table/create/operation.sql +++ b/script/script/table/create/operation.sql @@ -1,29 +1,29 @@ -CREATE TABLE OPERATION +create table tj.operation ( - ID SERIAL PRIMARY KEY -, Redo TEXT NOT NULL -, Undo TEXT NOT NULL -, Parent INTEGER REFERENCES OPERATION(ID) -, Redo_Child INTEGER REFERENCES OPERATION(ID) + 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 OPERATION (ID, Redo, Undo) VALUES (0, '', ''); +insert into tj.operation (id, redo, undo) values (0, '', ''); -CREATE TABLE CURRENT_OPERATION +create table tj.current_operation ( - ID INTEGER PRIMARY KEY REFERENCES OPERATION(ID) + id integer primary key references tj.operation(id) ); -INSERT INTO CURRENT_OPERATION (ID) VALUES (0); +insert into tj.current_operation (id) values (0); -CREATE TABLE SNAPSHOT_OPERATION +create table tj.snapshot_operation ( - ID INTEGER PRIMARY KEY REFERENCES OPERATION(ID) -, Tag TEXT NOT NULL UNIQUE + id integer primary key references tj.operation(id) +, tag text not null unique ); -CREATE TABLE TRANSACTION_OPERATION +create table tj.transaction_operation ( - ID INTEGER PRIMARY KEY REFERENCES OPERATION(ID) -, STRICT BOOLEAN NOT NULL DEFAULT FALSE + 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 index 3b57bb0..41c4f41 100644 --- a/script/script/table/drop/0.base.sql +++ b/script/script/table/drop/0.base.sql @@ -1,11 +1,11 @@ -DROP TABLE IF EXISTS _PATTERN; +drop table if exists tj.pattern; -DROP TABLE IF EXISTS _CURVE; +drop table if exists tj.curve; -DROP TABLE IF EXISTS _LINK; +drop table if exists tj.link; -DROP TABLE IF EXISTS _NODE; +drop table if exists tj.node; -DROP TYPE IF EXISTS _LINK_TYPE; +drop type if exists tj.link_type; -DROP TYPE IF EXISTS _NODE_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 index 7ee6d55..a03e80a 100644 --- a/script/script/table/drop/1.title.sql +++ b/script/script/table/drop/1.title.sql @@ -1,3 +1,3 @@ --- [TITLE] +-- [title] -DROP TABLE IF EXISTS 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 index 322a72f..232ca4d 100644 --- a/script/script/table/drop/10.status.sql +++ b/script/script/table/drop/10.status.sql @@ -1,13 +1,13 @@ --- [STATUS] +-- [status] -DROP TABLE IF EXISTS STATUS_LINK; +drop table if exists tj.status_link; -DROP TABLE IF EXISTS STATUS_VALVE; +drop table if exists tj.status_valve; -DROP TYPE IF EXISTS STATUS_VALVE_STATUS; +drop type if exists tj.status_valve_status; -DROP TABLE IF EXISTS STATUS_PUMP; +drop table if exists tj.status_pump; -DROP TABLE IF EXISTS STATUS_PIPE; +drop table if exists tj.status_pipe; -DROP TYPE IF EXISTS STATUS_PIPE_PUMP_STATUS; +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 index fd08d97..5fb2b47 100644 --- a/script/script/table/drop/11.patterns.sql +++ b/script/script/table/drop/11.patterns.sql @@ -1,3 +1,3 @@ --- [PATTERNS] +-- [patterns] -DROP TABLE IF EXISTS 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 index 577464e..4874ee8 100644 --- a/script/script/table/drop/12.curves.sql +++ b/script/script/table/drop/12.curves.sql @@ -1,3 +1,3 @@ --- [CURVES] +-- [curves] -DROP TABLE IF EXISTS 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 index 476259d..f6c0502 100644 --- a/script/script/table/drop/13.controls.sql +++ b/script/script/table/drop/13.controls.sql @@ -1,9 +1,9 @@ --- [CONTROLS] +-- [controls] -DROP TABLE IF EXISTS CONTROLS_3; +drop table if exists tj.controls_3; -DROP TABLE IF EXISTS CONTROLS_2; +drop table if exists tj.controls_2; -DROP TABLE IF EXISTS CONTROLS_1; +drop table if exists tj.controls_1; -DROP TYPE IF EXISTS CONTROLS_1_PREP; +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 index 84e4fd8..a920e6a 100644 --- a/script/script/table/drop/14.rules.sql +++ b/script/script/table/drop/14.rules.sql @@ -1,3 +1,3 @@ --- [RULES] +-- [rules] -DROP TABLE IF EXISTS 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 index 5002d5c..abf2e4a 100644 --- a/script/script/table/drop/15.energy.sql +++ b/script/script/table/drop/15.energy.sql @@ -1,9 +1,9 @@ --- [ENERGY] +-- [energy] -DROP TABLE IF EXISTS ENERGY_DEMAND_CHARGE; +drop table if exists tj.energy_demand_charge; -DROP TABLE IF EXISTS ENERGY_PUMP; +drop table if exists tj.energy_pump; -DROP TABLE IF EXISTS ENERGY_GLOBAL; +drop table if exists tj.energy_global; -DROP TYPE IF EXISTS ENERGY_PARAM; +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 index 40a0af8..ec23cd3 100644 --- a/script/script/table/drop/16.emitters.sql +++ b/script/script/table/drop/16.emitters.sql @@ -1,3 +1,3 @@ --- [EMITTERS] +-- [emitters] -DROP TABLE IF EXISTS 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 index b72c290..673cd2f 100644 --- a/script/script/table/drop/17.quality.sql +++ b/script/script/table/drop/17.quality.sql @@ -1,3 +1,3 @@ --- [QUALITY] +-- [quality] -DROP TABLE IF EXISTS 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 index 0402ffe..e9487c8 100644 --- a/script/script/table/drop/18.sources.sql +++ b/script/script/table/drop/18.sources.sql @@ -1,5 +1,5 @@ --- [SOURCES] +-- [sources] -DROP TABLE IF EXISTS SOURCES; +drop table if exists tj.sources; -DROP TYPE IF EXISTS SOURCES_TYPE; +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 index 2f6ba5b..0dff3f0 100644 --- a/script/script/table/drop/19.reactions.sql +++ b/script/script/table/drop/19.reactions.sql @@ -1,19 +1,19 @@ --- [REACTIONS] +-- [reactions] -DROP TABLE IF EXISTS REACTIONS_ROUGHNESS_CORRELATION; +drop table if exists tj.reactions_roughness_correlation; -DROP TABLE IF EXISTS REACTIONS_LIMITING_POTENTIAL; +drop table if exists tj.reactions_limiting_potential; -DROP TABLE IF EXISTS REACTIONS_TANK; +drop table if exists tj.reactions_tank; -DROP TABLE IF EXISTS REACTIONS_PIPE; +drop table if exists tj.reactions_pipe; -DROP TYPE IF EXISTS REACTIONS_PIPE_PARAM; +drop type if exists tj.reactions_pipe_param; -DROP TABLE IF EXISTS REACTIONS_GLOBAL; +drop table if exists tj.reactions_global; -DROP TYPE IF EXISTS REACTIONS_GLOBAL_PARAM; +drop type if exists tj.reactions_global_param; -DROP TABLE IF EXISTS REACTIONS_ORDER; +drop table if exists tj.reactions_order; -DROP TYPE IF EXISTS REACTIONS_ORDER_PARAM; +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 index ef0a609..0b436c4 100644 --- a/script/script/table/drop/2.junctions.sql +++ b/script/script/table/drop/2.junctions.sql @@ -1,3 +1,3 @@ --- [JUNCTIONS] +-- [junctions] -DROP TABLE IF EXISTS 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 index 938d554..f2adc25 100644 --- a/script/script/table/drop/20.mixing.sql +++ b/script/script/table/drop/20.mixing.sql @@ -1,5 +1,5 @@ --- [MIXING] +-- [mixing] -DROP TABLE IF EXISTS MIXING; +drop table if exists tj.mixing; -DROP TYPE IF EXISTS MIXING_Model; +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 index f051bd7..b56a5cf 100644 --- a/script/script/table/drop/21.times.sql +++ b/script/script/table/drop/21.times.sql @@ -1,3 +1,3 @@ --- [TIMES] +-- [times] -DROP TABLE IF EXISTS 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 index 790684d..03f771b 100644 --- a/script/script/table/drop/22.report.sql +++ b/script/script/table/drop/22.report.sql @@ -1,3 +1,3 @@ --- [REPORT] +-- [report] -DROP TABLE IF EXISTS 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 index 7cc629a..dc8c63c 100644 --- a/script/script/table/drop/23.options.sql +++ b/script/script/table/drop/23.options.sql @@ -1,3 +1,3 @@ --- [OPTIONS] +-- [options] -DROP TABLE IF EXISTS 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 index 04d0465..e5d50b4 100644 --- a/script/script/table/drop/24.coordinates.sql +++ b/script/script/table/drop/24.coordinates.sql @@ -1,7 +1,7 @@ --- [COORDINATES] +-- [coordinates] -DROP INDEX IF EXISTS COORDINATES_GIST; +drop index if exists tj.coordinates_gist; -DROP INDEX IF EXISTS COORDINATES_SPGIST; +drop index if exists tj.coordinates_spgist; -DROP TABLE IF EXISTS COORDINATES; +drop table if exists tj.coordinates; diff --git a/script/script/table/drop/25.vertices.sql b/script/script/table/drop/25.vertices.sql index f74dd3e..a060c4a 100644 --- a/script/script/table/drop/25.vertices.sql +++ b/script/script/table/drop/25.vertices.sql @@ -1,3 +1,3 @@ --- [VERTICES] +-- [vertices] -DROP TABLE IF EXISTS 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 index aa1b2b2..4f9f743 100644 --- a/script/script/table/drop/26.labels.sql +++ b/script/script/table/drop/26.labels.sql @@ -1,3 +1,3 @@ --- [LABELS] +-- [labels] -DROP TABLE IF EXISTS 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 index 408bb85..ba47de7 100644 --- a/script/script/table/drop/27.backdrop.sql +++ b/script/script/table/drop/27.backdrop.sql @@ -1,3 +1,3 @@ --- [BACKDROP] +-- [backdrop] -DROP TABLE IF EXISTS BACKDROP; +drop table if exists tj.backdrop; diff --git a/script/script/table/drop/3.reservoirs.sql b/script/script/table/drop/3.reservoirs.sql index 9864c18..c9a1e25 100644 --- a/script/script/table/drop/3.reservoirs.sql +++ b/script/script/table/drop/3.reservoirs.sql @@ -1,3 +1,3 @@ --- [RESERVOIRS] +-- [reservoirs] -DROP TABLE IF EXISTS 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 index da0487d..0a5e589 100644 --- a/script/script/table/drop/4.tanks.sql +++ b/script/script/table/drop/4.tanks.sql @@ -1,5 +1,5 @@ --- [TANKS] +-- [tanks] -DROP TABLE IF EXISTS TANKS; +drop table if exists tj.tanks; -DROP TYPE IF EXISTS TANKS_OVERFLOW; +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 index 1e23612..0cdc8dc 100644 --- a/script/script/table/drop/5.pipes.sql +++ b/script/script/table/drop/5.pipes.sql @@ -1,5 +1,5 @@ --- [PIPES] +-- [pipes] -DROP TABLE IF EXISTS PIPES; +drop table if exists tj.pipes; -DROP TYPE IF EXISTS PIPES_STATUS; +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 index 5ac9656..18d697b 100644 --- a/script/script/table/drop/6.pumps.sql +++ b/script/script/table/drop/6.pumps.sql @@ -1,11 +1,11 @@ --- [PUMPS] +-- [pumps] -DROP TABLE IF EXISTS PUMPS_PROPERTY_PATTERN; +drop table if exists tj.pumps_property_pattern; -DROP TABLE IF EXISTS PUMPS_PROPERTY_HEAD; +drop table if exists tj.pumps_property_head; -DROP TABLE IF EXISTS PUMPS_PROPERTY_SPEED; +drop table if exists tj.pumps_property_speed; -DROP TABLE IF EXISTS PUMPS_PROPERTY_POWER; +drop table if exists tj.pumps_property_power; -DROP TABLE IF EXISTS PUMPS; +drop table if exists tj.pumps; diff --git a/script/script/table/drop/7.valves.sql b/script/script/table/drop/7.valves.sql index c95b423..0c2c2d1 100644 --- a/script/script/table/drop/7.valves.sql +++ b/script/script/table/drop/7.valves.sql @@ -1,5 +1,5 @@ --- [VALVES] +-- [valves] -DROP TABLE IF EXISTS VALVES; +drop table if exists tj.valves; -DROP TYPE IF EXISTS VALVES_TYPE; +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 index df8e3d1..e277759 100644 --- a/script/script/table/drop/8.tags.sql +++ b/script/script/table/drop/8.tags.sql @@ -1,5 +1,5 @@ --- [TAGS] +-- [tags] -DROP TABLE IF EXISTS TAGS_LINK; +drop table if exists tj.tags_link; -DROP TABLE IF EXISTS TAGS_NODE; +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 index ebe0209..8478a8b 100644 --- a/script/script/table/drop/9.demands.sql +++ b/script/script/table/drop/9.demands.sql @@ -1,3 +1,3 @@ --- [DEMANDS] +-- [demands] -DROP TABLE IF EXISTS DEMANDS; +drop table if exists tj.demands; diff --git a/script/script/table/drop/operation.sql b/script/script/table/drop/operation.sql index 998ee2f..cf3dc7b 100644 --- a/script/script/table/drop/operation.sql +++ b/script/script/table/drop/operation.sql @@ -1,7 +1,7 @@ -DROP TABLE IF EXISTS TRANSACTION_OPERATION; +drop table if exists tj.transaction_operation; -DROP TABLE IF EXISTS SNAPSHOT_OPERATION; +drop table if exists tj.snapshot_operation; -DROP TABLE IF EXISTS CURRENT_OPERATION; +drop table if exists tj.current_operation; -DROP TABLE IF EXISTS OPERATION; +drop table if exists tj.operation; From 2b23d52ed36dc77ee5ced3898044736faa94c406 Mon Sep 17 00:00:00 2001 From: wqy Date: Sat, 3 Sep 2022 14:50:51 +0800 Subject: [PATCH 47/52] Can not add schema for index... --- script/script/table/create/24.coordinates.sql | 4 ++-- script/script/table/drop/24.coordinates.sql | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/script/script/table/create/24.coordinates.sql b/script/script/table/create/24.coordinates.sql index 4804896..9b2af3b 100644 --- a/script/script/table/create/24.coordinates.sql +++ b/script/script/table/create/24.coordinates.sql @@ -6,5 +6,5 @@ create table tj.coordinates , 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); +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/drop/24.coordinates.sql b/script/script/table/drop/24.coordinates.sql index e5d50b4..0d74d3e 100644 --- a/script/script/table/drop/24.coordinates.sql +++ b/script/script/table/drop/24.coordinates.sql @@ -1,7 +1,7 @@ -- [coordinates] -drop index if exists tj.coordinates_gist; +drop index if exists tj_coordinates_gist; -drop index if exists tj.coordinates_spgist; +drop index if exists tj_coordinates_spgist; drop table if exists tj.coordinates; From 133feef4d2bfe79ff95a7123476be41bafa95b2c Mon Sep 17 00:00:00 2001 From: wqy Date: Sat, 3 Sep 2022 14:51:13 +0800 Subject: [PATCH 48/52] Script for tj_project template --- script/script/template.py | 104 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 script/script/template.py diff --git a/script/script/template.py b/script/script/template.py new file mode 100644 index 0000000..0b795b5 --- /dev/null +++ b/script/script/template.py @@ -0,0 +1,104 @@ +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" +] + +sql_drop = [ + "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() From 03a0d6cf64461287e162746dca6588f6844e4346 Mon Sep 17 00:00:00 2001 From: wqy Date: Sat, 3 Sep 2022 17:07:46 +0800 Subject: [PATCH 49/52] Implement undo in by PL/pgSQL --- script/script/api/create/1.title.sql | 23 ++++--- script/script/api/create/operation.sql | 87 ++++++++++++++++++++++++++ script/script/api/create/project.sql | 9 +++ script/script/api/drop/1.title.sql | 3 + script/script/api/drop/operation.sql | 7 +++ script/script/api/plpgsql.sql | 6 ++ script/script/template.py | 6 +- 7 files changed, 131 insertions(+), 10 deletions(-) create mode 100644 script/script/api/create/operation.sql create mode 100644 script/script/api/create/project.sql create mode 100644 script/script/api/drop/1.title.sql create mode 100644 script/script/api/drop/operation.sql create mode 100644 script/script/api/plpgsql.sql diff --git a/script/script/api/create/1.title.sql b/script/script/api/create/1.title.sql index 31340b8..abbf5e6 100644 --- a/script/script/api/create/1.title.sql +++ b/script/script/api/create/1.title.sql @@ -1,22 +1,27 @@ -- get_title() -create function get_title() returns text as +create function tj.get_title() returns text as $$ - select value from title; -$$ language sql; +declare + title text; +begin + select value into title from tj.title; + return title; +end; +$$ language plpgsql; -- update_title() -create function update_title(new_title text) returns void as +create function tj.set_title(new_title text) returns void as $$ declare old_title text; redo text; undo text; begin - select get_title() into old_title; - update title set value = new_title where value = old_title; + select tj.get_title() into old_title; + update tj.title set value = new_title where value = old_title; - redo := concat('update title set value = ''', new_title, ''' where value = ''', old_title, ''''); - undo := concat('update title set value = ''', old_title, ''' where value = ''', new_title, ''''); - insert into operation values (default, redo, undo); + 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..ebb5edf --- /dev/null +++ b/script/script/api/create/operation.sql @@ -0,0 +1,87 @@ +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; \ 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..951fa22 --- /dev/null +++ b/script/script/api/drop/operation.sql @@ -0,0 +1,7 @@ +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/template.py b/script/script/template.py index 0b795b5..7ed9a98 100644 --- a/script/script/template.py +++ b/script/script/template.py @@ -31,10 +31,14 @@ sql_create = [ "table/create/26.labels.sql", "table/create/27.backdrop.sql", "table/create/28.end.sql", - "table/create/operation.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", From 57728681b6138e53bc3a3da09e996701571675d8 Mon Sep 17 00:00:00 2001 From: wqy Date: Sat, 3 Sep 2022 17:29:50 +0800 Subject: [PATCH 50/52] Implement redo by PL/pgSQL --- script/script/api/create/1.title.sql | 2 +- script/script/api/create/operation.sql | 22 ++++++++++++++++++++++ script/script/api/drop/operation.sql | 2 ++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/script/script/api/create/1.title.sql b/script/script/api/create/1.title.sql index abbf5e6..fb81579 100644 --- a/script/script/api/create/1.title.sql +++ b/script/script/api/create/1.title.sql @@ -9,7 +9,7 @@ begin end; $$ language plpgsql; --- update_title() +-- set_title() create function tj.set_title(new_title text) returns void as $$ declare diff --git a/script/script/api/create/operation.sql b/script/script/api/create/operation.sql index ebb5edf..ad56cdf 100644 --- a/script/script/api/create/operation.sql +++ b/script/script/api/create/operation.sql @@ -84,4 +84,26 @@ 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/drop/operation.sql b/script/script/api/drop/operation.sql index 951fa22..1692da5 100644 --- a/script/script/api/drop/operation.sql +++ b/script/script/api/drop/operation.sql @@ -1,3 +1,5 @@ +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; From c99a1d728b51573765effed16730c3cfc7678a00 Mon Sep 17 00:00:00 2001 From: wqy Date: Tue, 6 Sep 2022 21:33:09 +0800 Subject: [PATCH 51/52] Try change set to record result --- api/__init__.py | 2 ++ api/change_set.py | 14 ++++++++++++++ tjnetwork_new.py | 7 +++++++ 3 files changed, 23 insertions(+) create mode 100644 api/change_set.py diff --git a/api/__init__.py b/api/__init__.py index 4ea2a94..408e40e 100644 --- a/api/__init__.py +++ b/api/__init__.py @@ -2,6 +2,8 @@ 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 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/tjnetwork_new.py b/tjnetwork_new.py index f7a3845..e3661a8 100644 --- a/tjnetwork_new.py +++ b/tjnetwork_new.py @@ -1,6 +1,13 @@ import api +############################################################ +# ChangeSet +############################################################ + +ChangeSet = api.ChangeSet + + ############################################################ # enum ############################################################ From 3a694d5e030118da04322b8c78b3bde065dcae97 Mon Sep 17 00:00:00 2001 From: wqy Date: Tue, 6 Sep 2022 21:36:20 +0800 Subject: [PATCH 52/52] Make use of change set --- api/s1_title.py | 7 ++++++- api/s2_junctions.py | 42 ++++++++++++++++++++++++++++++++++++------ tjnetwork_new.py | 15 ++++++++------- 3 files changed, 50 insertions(+), 14 deletions(-) diff --git a/api/s1_title.py b/api/s1_title.py index 2f086c1..3d0e3a3 100644 --- a/api/s1_title.py +++ b/api/s1_title.py @@ -1,13 +1,14 @@ 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) -> None: +def set_title(name: str, value: str) -> ChangeSet: old = get_title(name) with conn[name].cursor() as cur: @@ -17,3 +18,7 @@ def set_title(name: str, value: str) -> None: 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 index 79301ce..0c77405 100644 --- a/api/s2_junctions.py +++ b/api/s2_junctions.py @@ -2,8 +2,9 @@ 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) -> None: +def add_junction(name: str, id: str, x: float, y: float, elevation: float) -> ChangeSet: if is_node(name, id): return @@ -19,7 +20,12 @@ def add_junction(name: str, id: str, x: float, y: float, elevation: float) -> No undo += f' delete from _node where id = "{id}";' add_operation(name, redo, undo) -def delete_junction(name: str, id: str) -> None: + change = ChangeSet() + change.add('junction', id) + return change + + +def delete_junction(name: str, id: str) -> ChangeSet: if not is_junction(name, id): return @@ -52,6 +58,11 @@ def delete_junction(name: str, id: str) -> None: 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}'") @@ -91,7 +102,7 @@ def get_junction_coord(name: str, id: str) -> dict[str, float] | None: coord = str(row['coord']) return _to_point(coord) -def set_junction_elevation(name: str, id: str, elevation: float) -> None: +def set_junction_elevation(name: str, id: str, elevation: float) -> ChangeSet: if not is_junction(name, id): return @@ -106,7 +117,12 @@ def set_junction_elevation(name: str, id: str, elevation: float) -> None: undo = f'update junctions set elevation = {old} where id = "{id}"' add_operation(name, redo, undo) -def set_junction_demand(name: str, id: str, demand: float) -> None: + 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 @@ -121,7 +137,12 @@ def set_junction_demand(name: str, id: str, demand: float) -> None: undo = f'update junctions set demand = {old} where id = "{id}"' add_operation(name, redo, undo) -def set_junction_pattern(name: str, id: str, pattern: str) -> None: + 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): @@ -140,7 +161,12 @@ def set_junction_pattern(name: str, id: str, pattern: str) -> None: undo = f'update junctions set pattern = {old} where id = "{id}"' add_operation(name, redo, undo) -def set_junction_coord(name: str, id: str, x: float, y: float) -> None: + 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 @@ -156,3 +182,7 @@ def set_junction_coord(name: str, id: str, x: float, y: float) -> None: 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/tjnetwork_new.py b/tjnetwork_new.py index e3661a8..c47d573 100644 --- a/tjnetwork_new.py +++ b/tjnetwork_new.py @@ -1,3 +1,4 @@ +from msilib.schema import CheckBox import api @@ -128,7 +129,7 @@ def get_patterns(name: str) -> list[str]: # title 1.[TITLE] ############################################################ -def set_title(name: str, title: str) -> None: +def set_title(name: str, title: str) -> ChangeSet: return api.set_title(name, title) def get_title(name: str) -> str: @@ -139,10 +140,10 @@ def get_title(name: str) -> str: # junction 2.[JUNCTIONS] ############################################################ -def add_junction(name: str, junction_id: str, x: float, y: float, elevation: float) -> None: +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) -> None: +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: @@ -157,14 +158,14 @@ def get_junction_pattern(name: str, junction_id: str) -> str | None: 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) -> None: +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) -> None: +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) -> None: +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) -> None: +def set_junction_coord(name: str, junction_id: str, x: float, y: float) -> ChangeSet: return api.set_junction_coord(name, junction_id, x, y)