Use sql batch to optimize
This commit is contained in:
@@ -13,7 +13,7 @@ from .s8_tags import inp_in_tag_new
|
|||||||
from .s9_demands import inp_in_demand_new
|
from .s9_demands import inp_in_demand_new
|
||||||
from .s10_status import inp_in_status_new
|
from .s10_status import inp_in_status_new
|
||||||
from .s11_patterns import inp_in_pattern_new
|
from .s11_patterns import inp_in_pattern_new
|
||||||
from .s12_curves import inp_in_curve_new
|
from .s12_curves import CURVE_TYPE_PUMP, inp_in_curve_new
|
||||||
from .s13_controls import inp_in_control_new
|
from .s13_controls import inp_in_control_new
|
||||||
from .s14_rules import inp_in_rule_new
|
from .s14_rules import inp_in_rule_new
|
||||||
from .s15_energy import inp_in_energy_new
|
from .s15_energy import inp_in_energy_new
|
||||||
@@ -103,10 +103,12 @@ _level_4 = [
|
|||||||
VERTICES,
|
VERTICES,
|
||||||
]
|
]
|
||||||
|
|
||||||
|
def _get_offset(inp: str) -> tuple[dict[str, list[int]], bool]:
|
||||||
def _get_offset(inp: str) -> dict[str, list[int]]:
|
|
||||||
offset: dict[str, list[int]] = {}
|
offset: dict[str, list[int]] = {}
|
||||||
|
|
||||||
|
current = ''
|
||||||
|
demand_outside = False
|
||||||
|
|
||||||
with open(inp) as f:
|
with open(inp) as f:
|
||||||
while True:
|
while True:
|
||||||
line = f.readline()
|
line = f.readline()
|
||||||
@@ -120,20 +122,43 @@ def _get_offset(inp: str) -> dict[str, list[int]]:
|
|||||||
if s not in offset:
|
if s not in offset:
|
||||||
offset[s] = []
|
offset[s] = []
|
||||||
offset[s].append(f.tell())
|
offset[s].append(f.tell())
|
||||||
|
current = s
|
||||||
break
|
break
|
||||||
|
elif line != '' and line.startswith(';') == False:
|
||||||
|
if current == DEMANDS:
|
||||||
|
demand_outside = True
|
||||||
|
|
||||||
return offset
|
return (offset, demand_outside)
|
||||||
|
|
||||||
|
|
||||||
def print_time(desc: str) -> None:
|
def print_time(desc: str) -> datetime.datetime:
|
||||||
time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
|
now = datetime.datetime.now()
|
||||||
|
time = now.strftime('%Y-%m-%d %H:%M:%S')
|
||||||
print(f"{time}: {desc}")
|
print(f"{time}: {desc}")
|
||||||
|
return now
|
||||||
|
|
||||||
|
|
||||||
|
class SQLBatch:
|
||||||
|
def __init__(self, project: str, count: int = 100) -> None:
|
||||||
|
self.batch: list[str] = []
|
||||||
|
self.project = project
|
||||||
|
self.count = count
|
||||||
|
|
||||||
|
def add(self, sql: str) -> None:
|
||||||
|
self.batch.append(sql)
|
||||||
|
if len(self.batch) == self.count:
|
||||||
|
self.flush()
|
||||||
|
|
||||||
|
def flush(self) -> None:
|
||||||
|
write(self.project, ''.join(self.batch))
|
||||||
|
self.batch.clear()
|
||||||
|
|
||||||
|
|
||||||
def parse_file(project: str, inp: str) -> None:
|
def parse_file(project: str, inp: str) -> None:
|
||||||
print_time(f"start reading {inp}...")
|
start = print_time(f'Start reading "{inp}"...')
|
||||||
|
|
||||||
offset = _get_offset(inp)
|
print_time("First scan...")
|
||||||
|
offset, demand_outside = _get_offset(inp)
|
||||||
|
|
||||||
levels = _level_1 + _level_2 + _level_3 + _level_4
|
levels = _level_1 + _level_2 + _level_3 + _level_4
|
||||||
|
|
||||||
@@ -143,15 +168,21 @@ def parse_file(project: str, inp: str) -> None:
|
|||||||
if t[0] == _S:
|
if t[0] == _S:
|
||||||
sections[s] = []
|
sections[s] = []
|
||||||
|
|
||||||
pattern_desc_line = None
|
current_pattern = None
|
||||||
|
current_curve = None
|
||||||
curve_type_desc_line = None
|
curve_type_desc_line = None
|
||||||
demand_junction = None
|
|
||||||
|
|
||||||
|
sql_batch = SQLBatch(project)
|
||||||
|
|
||||||
|
print_time("Second scan...")
|
||||||
with open(inp) as f:
|
with open(inp) as f:
|
||||||
for s in levels:
|
for s in levels:
|
||||||
if s not in offset:
|
if s not in offset:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
if s == DEMANDS and demand_outside == False:
|
||||||
|
continue
|
||||||
|
|
||||||
print_time(f"[{s}]")
|
print_time(f"[{s}]")
|
||||||
|
|
||||||
is_s = _handler[s][0] == _S
|
is_s = _handler[s][0] == _S
|
||||||
@@ -177,39 +208,40 @@ def parse_file(project: str, inp: str) -> None:
|
|||||||
if line.startswith(';'):
|
if line.startswith(';'):
|
||||||
line = line.removeprefix(';')
|
line = line.removeprefix(';')
|
||||||
if s == PATTERNS: # ;desc
|
if s == PATTERNS: # ;desc
|
||||||
pattern_desc_line = line
|
pass
|
||||||
elif s == CURVES: # ;type: desc
|
elif s == CURVES: # ;type: desc
|
||||||
curve_type_desc_line = line
|
curve_type_desc_line = line
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if s == PATTERNS:
|
if s == PATTERNS:
|
||||||
if pattern_desc_line != None:
|
|
||||||
tokens = line.split()
|
|
||||||
write(project, f"insert into _pattern (id) values ('{tokens[0]}');")
|
|
||||||
pattern_desc_line = None
|
|
||||||
elif s == CURVES:
|
|
||||||
if curve_type_desc_line != None:
|
|
||||||
type_and_desc = curve_type_desc_line.split(':')
|
|
||||||
tokens = line.split()
|
|
||||||
write(project, f"insert into _curve (id, type) values ('{tokens[0]}', '{type_and_desc[0].strip()}');")
|
|
||||||
curve_type_desc_line = None
|
|
||||||
elif s == DEMANDS:
|
|
||||||
tokens = line.split()
|
tokens = line.split()
|
||||||
junction = str(tokens[0])
|
if current_pattern != tokens[0]:
|
||||||
if demand_junction != junction:
|
write(project, f"insert into _pattern (id) values ('{tokens[0]}');")
|
||||||
if try_read(project, f"select * from demands where junction = '{junction}'") != None:
|
current_pattern = tokens[0]
|
||||||
write(project, f"delete from demands where junction = '{junction}';")
|
elif s == CURVES:
|
||||||
demand_junction = junction
|
tokens = line.split()
|
||||||
|
if current_curve != tokens[0]:
|
||||||
handler(project, line)
|
type = CURVE_TYPE_PUMP
|
||||||
|
if curve_type_desc_line != None:
|
||||||
|
type = curve_type_desc_line.split(':')[0].strip()
|
||||||
|
write(project, f"insert into _curve (id, type) values ('{tokens[0]}', '{type}');")
|
||||||
|
current_curve = tokens[0]
|
||||||
|
curve_type_desc_line = None
|
||||||
|
|
||||||
|
if s == JUNCTIONS:
|
||||||
|
sql_batch.add(handler(line, demand_outside))
|
||||||
|
else:
|
||||||
|
sql_batch.add(handler(line))
|
||||||
|
|
||||||
f.seek(0)
|
f.seek(0)
|
||||||
|
|
||||||
if is_s:
|
if is_s:
|
||||||
handler(project, sections[s])
|
sql_batch.add(handler(sections[s]))
|
||||||
|
|
||||||
print_time(f"end reading {inp}")
|
sql_batch.flush()
|
||||||
|
|
||||||
|
end = print_time(f'End reading "{inp}"')
|
||||||
|
print(f"Total (in second): {(end-start).seconds}(s)")
|
||||||
|
|
||||||
|
|
||||||
def parse_inp(project: str, inp: str) -> None:
|
def parse_inp(project: str, inp: str) -> None:
|
||||||
|
|||||||
@@ -113,7 +113,7 @@ def inp_in_status(section: list[str]) -> ChangeSet:
|
|||||||
return cs
|
return cs
|
||||||
|
|
||||||
|
|
||||||
def inp_in_status_new(name: str, line: str) -> None:
|
def inp_in_status_new(line: str) -> str:
|
||||||
tokens = line.split()
|
tokens = line.split()
|
||||||
|
|
||||||
num = len(tokens)
|
num = len(tokens)
|
||||||
@@ -123,9 +123,9 @@ def inp_in_status_new(name: str, line: str) -> None:
|
|||||||
link = str(tokens[0])
|
link = str(tokens[0])
|
||||||
value = tokens[1].upper()
|
value = tokens[1].upper()
|
||||||
if value == LINK_STATUS_OPEN or value == LINK_STATUS_CLOSED or value == LINK_STATUS_ACTIVE:
|
if value == LINK_STATUS_OPEN or value == LINK_STATUS_CLOSED or value == LINK_STATUS_ACTIVE:
|
||||||
write(name, f"insert into status (link, status, setting) values ('{link}', '{value}', null);")
|
return f"insert into status (link, status, setting) values ('{link}', '{value}', null);"
|
||||||
else:
|
else:
|
||||||
write(name, f"insert into status (link, status, setting) values ('{link}', null, {float(value)});")
|
return f"insert into status (link, status, setting) values ('{link}', null, {float(value)});"
|
||||||
|
|
||||||
|
|
||||||
def inp_out_status(name: str) -> list[str]:
|
def inp_out_status(name: str) -> list[str]:
|
||||||
|
|||||||
@@ -126,10 +126,12 @@ def inp_in_pattern(section: list[str]) -> ChangeSet:
|
|||||||
return cs
|
return cs
|
||||||
|
|
||||||
|
|
||||||
def inp_in_pattern_new(name: str, line: str) -> None:
|
def inp_in_pattern_new(line: str) -> str:
|
||||||
tokens = line.split()
|
tokens = line.split()
|
||||||
|
sql = ''
|
||||||
for token in tokens[1:]:
|
for token in tokens[1:]:
|
||||||
write(name, f"insert into patterns (id, factor) values ('{tokens[0]}', {float(token)});")
|
sql += f"insert into patterns (id, factor) values ('{tokens[0]}', {float(token)});"
|
||||||
|
return sql
|
||||||
|
|
||||||
|
|
||||||
def inp_out_pattern(name: str) -> list[str]:
|
def inp_out_pattern(name: str) -> list[str]:
|
||||||
|
|||||||
@@ -159,9 +159,9 @@ def inp_in_curve(section: list[str]) -> ChangeSet:
|
|||||||
return cs
|
return cs
|
||||||
|
|
||||||
|
|
||||||
def inp_in_curve_new(name: str, line: str) -> None:
|
def inp_in_curve_new(line: str) -> str:
|
||||||
tokens = line.split()
|
tokens = line.split()
|
||||||
write(name, f"insert into curves (id, x, y) values ('{tokens[0]}', {float(tokens[1])}, {float(tokens[2])});")
|
return f"insert into curves (id, x, y) values ('{tokens[0]}', {float(tokens[1])}, {float(tokens[2])});"
|
||||||
|
|
||||||
|
|
||||||
def inp_out_curve(name: str) -> list[str]:
|
def inp_out_curve(name: str) -> list[str]:
|
||||||
|
|||||||
@@ -48,8 +48,8 @@ def inp_in_control(section: list[str]) -> ChangeSet:
|
|||||||
return ChangeSet()
|
return ChangeSet()
|
||||||
|
|
||||||
|
|
||||||
def inp_in_control_new(name: str, line: str) -> None:
|
def inp_in_control_new(line: str) -> str:
|
||||||
write(name, f"insert into controls (line) values ('{line}');")
|
return f"insert into controls (line) values ('{line}');"
|
||||||
|
|
||||||
|
|
||||||
def inp_out_control(name: str) -> list[str]:
|
def inp_out_control(name: str) -> list[str]:
|
||||||
|
|||||||
@@ -44,8 +44,8 @@ def inp_in_rule(section: list[str]) -> ChangeSet:
|
|||||||
return ChangeSet()
|
return ChangeSet()
|
||||||
|
|
||||||
|
|
||||||
def inp_in_rule_new(name: str, line: str) -> None:
|
def inp_in_rule_new(line: str) -> str:
|
||||||
write(name, f"insert into rules (line) values ('{line}');")
|
return f"insert into rules (line) values ('{line}');"
|
||||||
|
|
||||||
|
|
||||||
def inp_out_rule(name: str) -> list[str]:
|
def inp_out_rule(name: str) -> list[str]:
|
||||||
|
|||||||
@@ -169,7 +169,7 @@ def inp_in_energy(section: list[str]) -> ChangeSet:
|
|||||||
return cs
|
return cs
|
||||||
|
|
||||||
|
|
||||||
def inp_in_energy_new(name: str, line: str) -> None:
|
def inp_in_energy_new(line: str) -> str:
|
||||||
tokens = line.split()
|
tokens = line.split()
|
||||||
|
|
||||||
if tokens[0].upper() == 'PUMP':
|
if tokens[0].upper() == 'PUMP':
|
||||||
@@ -183,7 +183,7 @@ def inp_in_energy_new(name: str, line: str) -> None:
|
|||||||
if key == 'efficiency':
|
if key == 'efficiency':
|
||||||
key = 'effic'
|
key = 'effic'
|
||||||
|
|
||||||
write(name, f"insert into energy_pump_{key} (pump, {key}) values ('{pump}', {value});")
|
return f"insert into energy_pump_{key} (pump, {key}) values ('{pump}', {value});"
|
||||||
|
|
||||||
else:
|
else:
|
||||||
line = line.upper().strip()
|
line = line.upper().strip()
|
||||||
@@ -195,7 +195,9 @@ def inp_in_energy_new(name: str, line: str) -> None:
|
|||||||
if line.startswith('GLOBAL EFFICIENCY'):
|
if line.startswith('GLOBAL EFFICIENCY'):
|
||||||
value = line.removeprefix('GLOBAL EFFICIENCY').strip()
|
value = line.removeprefix('GLOBAL EFFICIENCY').strip()
|
||||||
|
|
||||||
write(name, f"update energy set value = '{value}' where key = '{key}';")
|
return f"update energy set value = '{value}' where key = '{key}';"
|
||||||
|
|
||||||
|
return ''
|
||||||
|
|
||||||
|
|
||||||
def inp_out_energy(name: str) -> list[str]:
|
def inp_out_energy(name: str) -> list[str]:
|
||||||
|
|||||||
@@ -89,7 +89,7 @@ def inp_in_emitter(section: list[str]) -> ChangeSet:
|
|||||||
return cs
|
return cs
|
||||||
|
|
||||||
|
|
||||||
def inp_in_emitter_new(name: str, line: str) -> None:
|
def inp_in_emitter_new(line: str) -> str:
|
||||||
tokens = line.split()
|
tokens = line.split()
|
||||||
|
|
||||||
num = len(tokens)
|
num = len(tokens)
|
||||||
@@ -99,7 +99,7 @@ def inp_in_emitter_new(name: str, line: str) -> None:
|
|||||||
junction = str(tokens[0])
|
junction = str(tokens[0])
|
||||||
coefficient = float(tokens[1])
|
coefficient = float(tokens[1])
|
||||||
|
|
||||||
write(name, f"insert into emitters (junction, coefficient) values ('{junction}', {coefficient});")
|
return f"insert into emitters (junction, coefficient) values ('{junction}', {coefficient});"
|
||||||
|
|
||||||
|
|
||||||
def inp_out_emitter(name: str) -> list[str]:
|
def inp_out_emitter(name: str) -> list[str]:
|
||||||
|
|||||||
@@ -86,7 +86,7 @@ def inp_in_quality(section: list[str]) -> ChangeSet:
|
|||||||
return cs
|
return cs
|
||||||
|
|
||||||
|
|
||||||
def inp_in_quality_new(name: str, line: str) -> None:
|
def inp_in_quality_new(line: str) -> str:
|
||||||
tokens = line.split()
|
tokens = line.split()
|
||||||
|
|
||||||
num = len(tokens)
|
num = len(tokens)
|
||||||
@@ -96,7 +96,7 @@ def inp_in_quality_new(name: str, line: str) -> None:
|
|||||||
node = str(tokens[0])
|
node = str(tokens[0])
|
||||||
quality = float(tokens[1])
|
quality = float(tokens[1])
|
||||||
|
|
||||||
write(name, f"insert into quality (node, quality) values ('{node}', {quality});")
|
return f"insert into quality (node, quality) values ('{node}', {quality});"
|
||||||
|
|
||||||
|
|
||||||
def inp_out_quality(name: str) -> list[str]:
|
def inp_out_quality(name: str) -> list[str]:
|
||||||
|
|||||||
@@ -131,7 +131,7 @@ def inp_in_source(section: list[str]) -> ChangeSet:
|
|||||||
return cs
|
return cs
|
||||||
|
|
||||||
|
|
||||||
def inp_in_source_new(name: str, line: str) -> None:
|
def inp_in_source_new(line: str) -> str:
|
||||||
tokens = line.split()
|
tokens = line.split()
|
||||||
|
|
||||||
num = len(tokens)
|
num = len(tokens)
|
||||||
@@ -144,7 +144,7 @@ def inp_in_source_new(name: str, line: str) -> None:
|
|||||||
pattern = str(tokens[3]) if num_without_desc >= 4 else None
|
pattern = str(tokens[3]) if num_without_desc >= 4 else None
|
||||||
pattern = f"'{pattern}'" if pattern != None else 'null'
|
pattern = f"'{pattern}'" if pattern != None else 'null'
|
||||||
|
|
||||||
write(name, f"insert into sources (node, type, strength, pattern) values ('{node}', '{s_type}', {strength}, {pattern});")
|
return f"insert into sources (node, type, strength, pattern) values ('{node}', '{s_type}', {strength}, {pattern});"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -222,26 +222,28 @@ def inp_in_reaction(section: list[str]) -> ChangeSet:
|
|||||||
return cs
|
return cs
|
||||||
|
|
||||||
|
|
||||||
def inp_in_reaction_new(name: str, line: str) -> None:
|
def inp_in_reaction_new(line: str) -> str:
|
||||||
tokens = line.split()
|
tokens = line.split()
|
||||||
token0 = tokens[0].upper()
|
token0 = tokens[0].upper()
|
||||||
if token0 == 'BULK' or token0 == 'WALL':
|
if token0 == 'BULK' or token0 == 'WALL':
|
||||||
pipe = tokens[1]
|
pipe = tokens[1]
|
||||||
key = token0.lower()
|
key = token0.lower()
|
||||||
value = tokens[2]
|
value = tokens[2]
|
||||||
write(name, f"insert into reactions_pipe_{key} (pipe, value) values ('{pipe}', {value});")
|
return f"insert into reactions_pipe_{key} (pipe, value) values ('{pipe}', {value});"
|
||||||
|
|
||||||
elif token0 == 'TANK':
|
elif token0 == 'TANK':
|
||||||
tank = tokens[1]
|
tank = tokens[1]
|
||||||
value = tokens[2]
|
value = tokens[2]
|
||||||
write(name, f"insert into reactions_tank (tank, value) values ('{tank}', {value});")
|
return f"insert into reactions_tank (tank, value) values ('{tank}', {value});"
|
||||||
|
|
||||||
else:
|
else:
|
||||||
line = line.upper().strip()
|
line = line.upper().strip()
|
||||||
for key in get_reaction_schema('').keys():
|
for key in get_reaction_schema('').keys():
|
||||||
if line.startswith(key):
|
if line.startswith(key):
|
||||||
value = line.removeprefix(key).strip()
|
value = line.removeprefix(key).strip()
|
||||||
write(name, f"update reactions set value = '{value}' where key = '{key}';")
|
return f"update reactions set value = '{value}' where key = '{key}';"
|
||||||
|
|
||||||
|
return ''
|
||||||
|
|
||||||
|
|
||||||
def inp_out_reaction(name: str) -> list[str]:
|
def inp_out_reaction(name: str) -> list[str]:
|
||||||
|
|||||||
@@ -40,13 +40,12 @@ def inp_in_title(section: list[str]) -> ChangeSet:
|
|||||||
return ChangeSet(g_update_prefix | {'type': 'title', 'value' : obj.value})
|
return ChangeSet(g_update_prefix | {'type': 'title', 'value' : obj.value})
|
||||||
|
|
||||||
|
|
||||||
def inp_in_title_new(name: str, section: list[str]) -> None:
|
def inp_in_title_new(section: list[str]) -> str:
|
||||||
if section == []:
|
if section == []:
|
||||||
return
|
return ''
|
||||||
|
|
||||||
title = '\n'.join(section)
|
title = '\n'.join(section)
|
||||||
sql = f"update title set value = '{title}';"
|
return f"update title set value = '{title}';"
|
||||||
write(name, sql)
|
|
||||||
|
|
||||||
|
|
||||||
def inp_out_title(name: str) -> list[str]:
|
def inp_out_title(name: str) -> list[str]:
|
||||||
|
|||||||
@@ -125,7 +125,7 @@ def inp_in_mixing(section: list[str]) -> ChangeSet:
|
|||||||
return cs
|
return cs
|
||||||
|
|
||||||
|
|
||||||
def inp_in_mixing_new(name: str, line: str) -> None:
|
def inp_in_mixing_new(line: str) -> str:
|
||||||
tokens = line.split()
|
tokens = line.split()
|
||||||
|
|
||||||
num = len(tokens)
|
num = len(tokens)
|
||||||
@@ -137,7 +137,7 @@ def inp_in_mixing_new(name: str, line: str) -> None:
|
|||||||
value = float(tokens[3]) if num_without_desc >= 4 else None
|
value = float(tokens[3]) if num_without_desc >= 4 else None
|
||||||
value = value if value != None else 'null'
|
value = value if value != None else 'null'
|
||||||
|
|
||||||
write(name, f"insert into mixing (tank, model, value) values ('{tank}', '{model}', {value});")
|
return f"insert into mixing (tank, model, value) values ('{tank}', '{model}', {value});"
|
||||||
|
|
||||||
|
|
||||||
def inp_out_mixing(name: str) -> list[str]:
|
def inp_out_mixing(name: str) -> list[str]:
|
||||||
|
|||||||
@@ -97,7 +97,8 @@ def inp_in_time(section: list[str]) -> ChangeSet:
|
|||||||
return ChangeSet()
|
return ChangeSet()
|
||||||
|
|
||||||
|
|
||||||
def inp_in_time_new(name: str, section: list[str]) -> None:
|
def inp_in_time_new(section: list[str]) -> str:
|
||||||
|
sql = ''
|
||||||
for s in section:
|
for s in section:
|
||||||
if s.startswith(';'):
|
if s.startswith(';'):
|
||||||
continue
|
continue
|
||||||
@@ -106,7 +107,8 @@ def inp_in_time_new(name: str, section: list[str]) -> None:
|
|||||||
for key in get_time_schema('').keys():
|
for key in get_time_schema('').keys():
|
||||||
if line.startswith(key):
|
if line.startswith(key):
|
||||||
value = line.removeprefix(key).strip()
|
value = line.removeprefix(key).strip()
|
||||||
write(name, f"update times set value = '{value}' where key = '{key}';")
|
sql += f"update times set value = '{value}' where key = '{key}';"
|
||||||
|
return sql
|
||||||
|
|
||||||
|
|
||||||
def inp_out_time(name: str) -> list[str]:
|
def inp_out_time(name: str) -> list[str]:
|
||||||
|
|||||||
@@ -22,8 +22,8 @@ def inp_in_report(section: list[str]) -> ChangeSet:
|
|||||||
return ChangeSet()
|
return ChangeSet()
|
||||||
|
|
||||||
|
|
||||||
def inp_in_report_new(name: str, section: list[str]) -> None:
|
def inp_in_report_new(section: list[str]) -> str:
|
||||||
return
|
return ''
|
||||||
|
|
||||||
|
|
||||||
def inp_out_report(name: str) -> list[str]:
|
def inp_out_report(name: str) -> list[str]:
|
||||||
|
|||||||
@@ -41,16 +41,18 @@ def inp_in_option(section: list[str]) -> ChangeSet:
|
|||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
def inp_in_option_new(name: str, section: list[str]) -> None:
|
def inp_in_option_new(section: list[str]) -> str:
|
||||||
|
sql = ''
|
||||||
result = inp_in_option(section)
|
result = inp_in_option(section)
|
||||||
for op in result.operations:
|
for op in result.operations:
|
||||||
for key in op.keys():
|
for key in op.keys():
|
||||||
if key == 'operation' or key == 'type':
|
if key == 'operation' or key == 'type':
|
||||||
continue
|
continue
|
||||||
if op['type'] == 'option':
|
if op['type'] == 'option':
|
||||||
write(name, f"update options set value = '{op[key]}' where key = '{key}';")
|
sql += f"update options set value = '{op[key]}' where key = '{key}';"
|
||||||
else:
|
else:
|
||||||
write(name, f"update options_v3 set value = '{op[key]}' where key = '{key}';")
|
sql += f"update options_v3 set value = '{op[key]}' where key = '{key}';"
|
||||||
|
return sql
|
||||||
|
|
||||||
|
|
||||||
def inp_out_option(name: str) -> list[str]:
|
def inp_out_option(name: str) -> list[str]:
|
||||||
|
|||||||
@@ -62,16 +62,18 @@ def inp_in_option_v3(section: list[str]) -> ChangeSet:
|
|||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
def inp_in_option_v3_new(name: str, section: list[str]) -> None:
|
def inp_in_option_v3_new(section: list[str]) -> str:
|
||||||
|
sql = ''
|
||||||
result = inp_in_option_v3(section)
|
result = inp_in_option_v3(section)
|
||||||
for op in result.operations:
|
for op in result.operations:
|
||||||
for key in op.keys():
|
for key in op.keys():
|
||||||
if key == 'operation' or key == 'type':
|
if key == 'operation' or key == 'type':
|
||||||
continue
|
continue
|
||||||
if op['type'] == 'option_v3':
|
if op['type'] == 'option_v3':
|
||||||
write(name, f"update options_v3 set value = '{op[key]}' where key = '{key}';")
|
sql += f"update options_v3 set value = '{op[key]}' where key = '{key}';"
|
||||||
else:
|
else:
|
||||||
write(name, f"update options set value = '{op[key]}' where key = '{key}';")
|
sql += f"update options set value = '{op[key]}' where key = '{key}';"
|
||||||
|
return sql
|
||||||
|
|
||||||
|
|
||||||
def inp_out_option_v3(name: str) -> list[str]:
|
def inp_out_option_v3(name: str) -> list[str]:
|
||||||
|
|||||||
@@ -27,11 +27,11 @@ def inp_in_coord(section: list[str]) -> dict[str, dict[str, float]]:
|
|||||||
return coords
|
return coords
|
||||||
|
|
||||||
|
|
||||||
def inp_in_coord_new(name: str, line: str) -> None:
|
def inp_in_coord_new(line: str) -> str:
|
||||||
tokens = line.split()
|
tokens = line.split()
|
||||||
node = tokens[0]
|
node = tokens[0]
|
||||||
coord = f"'({tokens[1]}, {tokens[2]})'"
|
coord = f"'({tokens[1]}, {tokens[2]})'"
|
||||||
write(name, f"insert into coordinates (node, coord) values ('{node}', {coord});")
|
return f"insert into coordinates (node, coord) values ('{node}', {coord});"
|
||||||
|
|
||||||
|
|
||||||
def inp_out_coord(name: str) -> list[str]:
|
def inp_out_coord(name: str) -> list[str]:
|
||||||
|
|||||||
@@ -98,12 +98,12 @@ def inp_in_vertex(section: list[str]) -> ChangeSet:
|
|||||||
return cs
|
return cs
|
||||||
|
|
||||||
|
|
||||||
def inp_in_vertex_new(name: str, line: str) -> None:
|
def inp_in_vertex_new(line: str) -> str:
|
||||||
tokens = line.split()
|
tokens = line.split()
|
||||||
link = tokens[0]
|
link = tokens[0]
|
||||||
x = float(tokens[1])
|
x = float(tokens[1])
|
||||||
y = float(tokens[2])
|
y = float(tokens[2])
|
||||||
write(name, f"insert into vertices (link, x, y) values ('{link}', {x}, {y});")
|
return f"insert into vertices (link, x, y) values ('{link}', {x}, {y});"
|
||||||
|
|
||||||
|
|
||||||
def inp_out_vertex(name: str) -> list[str]:
|
def inp_out_vertex(name: str) -> list[str]:
|
||||||
|
|||||||
@@ -124,7 +124,7 @@ def inp_in_label(section: list[str]) -> ChangeSet:
|
|||||||
return cs
|
return cs
|
||||||
|
|
||||||
|
|
||||||
def inp_in_label_new(name: str, line: str) -> None:
|
def inp_in_label_new(line: str) -> str:
|
||||||
tokens = line.split()
|
tokens = line.split()
|
||||||
|
|
||||||
num = len(tokens)
|
num = len(tokens)
|
||||||
@@ -137,7 +137,7 @@ def inp_in_label_new(name: str, line: str) -> None:
|
|||||||
node = str(tokens[3]) if num >= 4 else None
|
node = str(tokens[3]) if num >= 4 else None
|
||||||
node = f"'{node}'" if node != None else 'null'
|
node = f"'{node}'" if node != None else 'null'
|
||||||
|
|
||||||
write(name, f"insert into labels (x, y, label, node) values ({x}, {y}, '{label}', {node});")
|
return f"insert into labels (x, y, label, node) values ({x}, {y}, '{label}', {node});"
|
||||||
|
|
||||||
|
|
||||||
def inp_out_label(name: str) -> list[str]:
|
def inp_out_label(name: str) -> list[str]:
|
||||||
|
|||||||
@@ -38,13 +38,12 @@ def inp_in_backdrop(section: list[str]) -> ChangeSet:
|
|||||||
return ChangeSet()
|
return ChangeSet()
|
||||||
|
|
||||||
|
|
||||||
def inp_in_backdrop_new(name: str, section: list[str]) -> None:
|
def inp_in_backdrop_new(section: list[str]) -> str:
|
||||||
if section == []:
|
if section == []:
|
||||||
return
|
return ''
|
||||||
|
|
||||||
content = '\n'.join(section)
|
content = '\n'.join(section)
|
||||||
sql = f"update backdrop set content = '{content}';"
|
return f"update backdrop set content = '{content}';"
|
||||||
write(name, sql)
|
|
||||||
|
|
||||||
|
|
||||||
def inp_out_backdrop(name: str) -> list[str]:
|
def inp_out_backdrop(name: str) -> list[str]:
|
||||||
|
|||||||
@@ -152,7 +152,7 @@ def inp_in_junction(section: list[str]) -> ChangeSet:
|
|||||||
return cs
|
return cs
|
||||||
|
|
||||||
|
|
||||||
def inp_in_junction_new(name: str, line: str) -> None:
|
def inp_in_junction_new(line: str, demand_outside: bool) -> str:
|
||||||
tokens = line.split()
|
tokens = line.split()
|
||||||
|
|
||||||
num = len(tokens)
|
num = len(tokens)
|
||||||
@@ -166,9 +166,11 @@ def inp_in_junction_new(name: str, line: str) -> None:
|
|||||||
pattern = f"'{pattern}'" if pattern != None else 'null'
|
pattern = f"'{pattern}'" if pattern != None else 'null'
|
||||||
desc = str(tokens[-1]) if has_desc else None
|
desc = str(tokens[-1]) if has_desc else None
|
||||||
|
|
||||||
write(name, f"insert into _node (id, type) values ('{id}', 'junction');")
|
sql = f"insert into _node (id, type) values ('{id}', 'junction');insert into junctions (id, elevation) values ('{id}', {elevation});"
|
||||||
write(name, f"insert into junctions (id, elevation) values ('{id}', {elevation});")
|
if demand != None and demand_outside == False:
|
||||||
write(name, f"insert into demands (junction, demand, pattern) values ('{id}', {demand}, {pattern});")
|
sql += f"insert into demands (junction, demand, pattern) values ('{id}', {demand}, {pattern});"
|
||||||
|
|
||||||
|
return sql
|
||||||
|
|
||||||
|
|
||||||
def inp_out_junction(name: str) -> list[str]:
|
def inp_out_junction(name: str) -> list[str]:
|
||||||
|
|||||||
@@ -145,7 +145,7 @@ def inp_in_reservoir(section: list[str]) -> ChangeSet:
|
|||||||
return cs
|
return cs
|
||||||
|
|
||||||
|
|
||||||
def inp_in_reservoir_new(name: str, line: str) -> None:
|
def inp_in_reservoir_new(line: str) -> str:
|
||||||
tokens = line.split()
|
tokens = line.split()
|
||||||
|
|
||||||
num = len(tokens)
|
num = len(tokens)
|
||||||
@@ -158,8 +158,7 @@ def inp_in_reservoir_new(name: str, line: str) -> None:
|
|||||||
pattern = f"'{pattern}'" if pattern != None else 'null'
|
pattern = f"'{pattern}'" if pattern != None else 'null'
|
||||||
desc = str(tokens[-1]) if has_desc else None
|
desc = str(tokens[-1]) if has_desc else None
|
||||||
|
|
||||||
write(name, f"insert into _node (id, type) values ('{id}', 'reservoir');")
|
return f"insert into _node (id, type) values ('{id}', 'reservoir');insert into reservoirs (id, head, pattern) values ('{id}', {head}, {pattern});"
|
||||||
write(name, f"insert into reservoirs (id, head, pattern) values ('{id}', {head}, {pattern});")
|
|
||||||
|
|
||||||
|
|
||||||
def inp_out_reservoir(name: str) -> list[str]:
|
def inp_out_reservoir(name: str) -> list[str]:
|
||||||
|
|||||||
@@ -187,7 +187,7 @@ def inp_in_tank(section: list[str]) -> ChangeSet:
|
|||||||
return cs
|
return cs
|
||||||
|
|
||||||
|
|
||||||
def inp_in_tank_new(name: str, line: str) -> None:
|
def inp_in_tank_new(line: str) -> str:
|
||||||
tokens = line.split()
|
tokens = line.split()
|
||||||
|
|
||||||
num = len(tokens)
|
num = len(tokens)
|
||||||
@@ -207,8 +207,7 @@ def inp_in_tank_new(name: str, line: str) -> None:
|
|||||||
overflow = f"'{overflow}'" if overflow != None else 'null'
|
overflow = f"'{overflow}'" if overflow != None else 'null'
|
||||||
desc = str(tokens[-1]) if has_desc else None
|
desc = str(tokens[-1]) if has_desc else None
|
||||||
|
|
||||||
write(name, f"insert into _node (id, type) values ('{id}', 'tank');")
|
return f"insert into _node (id, type) values ('{id}', 'tank');insert into tanks (id, elevation, init_level, min_level, max_level, diameter, min_vol, vol_curve, overflow) values ('{id}', {elevation}, {init_level}, {min_level}, {max_level}, {diameter}, {min_vol}, {vol_curve}, {overflow});"
|
||||||
write(name, f"insert into tanks (id, elevation, init_level, min_level, max_level, diameter, min_vol, vol_curve, overflow) values ('{id}', {elevation}, {init_level}, {min_level}, {max_level}, {diameter}, {min_vol}, {vol_curve}, {overflow});")
|
|
||||||
|
|
||||||
|
|
||||||
def inp_out_tank(name: str) -> list[str]:
|
def inp_out_tank(name: str) -> list[str]:
|
||||||
|
|||||||
@@ -161,7 +161,7 @@ def inp_in_pipe(section: list[str]) -> ChangeSet:
|
|||||||
return cs
|
return cs
|
||||||
|
|
||||||
|
|
||||||
def inp_in_pipe_new(name: str, line: str) -> None:
|
def inp_in_pipe_new(line: str) -> str:
|
||||||
tokens = line.split()
|
tokens = line.split()
|
||||||
|
|
||||||
num = len(tokens)
|
num = len(tokens)
|
||||||
@@ -179,8 +179,7 @@ def inp_in_pipe_new(name: str, line: str) -> None:
|
|||||||
status = str(tokens[7].upper()) if num_without_desc >= 8 else PIPE_STATUS_OPEN
|
status = str(tokens[7].upper()) if num_without_desc >= 8 else PIPE_STATUS_OPEN
|
||||||
desc = str(tokens[-1]) if has_desc else None
|
desc = str(tokens[-1]) if has_desc else None
|
||||||
|
|
||||||
write(name, f"insert into _link (id, type) values ('{id}', 'pipe');")
|
return f"insert into _link (id, type) values ('{id}', 'pipe');insert into pipes (id, node1, node2, length, diameter, roughness, minor_loss, status) values ('{id}', '{node1}', '{node2}', {length}, {diameter}, {roughness}, {minor_loss}, '{status}');"
|
||||||
write(name, f"insert into pipes (id, node1, node2, length, diameter, roughness, minor_loss, status) values ('{id}', '{node1}', '{node2}', {length}, {diameter}, {roughness}, {minor_loss}, '{status}');")
|
|
||||||
|
|
||||||
|
|
||||||
def inp_out_pipe(name: str) -> list[str]:
|
def inp_out_pipe(name: str) -> list[str]:
|
||||||
|
|||||||
@@ -151,7 +151,7 @@ def inp_in_pump(section: list[str]) -> ChangeSet:
|
|||||||
return cs
|
return cs
|
||||||
|
|
||||||
|
|
||||||
def inp_in_pump_new(name: str, line: str) -> None:
|
def inp_in_pump_new(line: str) -> str:
|
||||||
tokens = line.split()
|
tokens = line.split()
|
||||||
|
|
||||||
num = len(tokens)
|
num = len(tokens)
|
||||||
@@ -174,8 +174,7 @@ def inp_in_pump_new(name: str, line: str) -> None:
|
|||||||
pattern = f"'{pattern}'" if pattern != None else 'null'
|
pattern = f"'{pattern}'" if pattern != None else 'null'
|
||||||
desc = str(tokens[-1]) if has_desc else None
|
desc = str(tokens[-1]) if has_desc else None
|
||||||
|
|
||||||
write(name, f"insert into _link (id, type) values ('{id}', 'pump');")
|
return f"insert into _link (id, type) values ('{id}', 'pump');insert into pumps (id, node1, node2, power, head, speed, pattern) values ('{id}', '{node1}', '{node2}', {power}, {head}, {speed}, {pattern});"
|
||||||
write(name, f"insert into pumps (id, node1, node2, power, head, speed, pattern) values ('{id}', '{node1}', '{node2}', {power}, {head}, {speed}, {pattern});")
|
|
||||||
|
|
||||||
|
|
||||||
def inp_out_pump(name: str) -> list[str]:
|
def inp_out_pump(name: str) -> list[str]:
|
||||||
|
|||||||
@@ -157,7 +157,7 @@ def inp_in_valve(section: list[str]) -> ChangeSet:
|
|||||||
return cs
|
return cs
|
||||||
|
|
||||||
|
|
||||||
def inp_in_valve_new(name: str, line: str) -> None:
|
def inp_in_valve_new(line: str) -> str:
|
||||||
tokens = line.split()
|
tokens = line.split()
|
||||||
|
|
||||||
num = len(tokens)
|
num = len(tokens)
|
||||||
@@ -173,8 +173,7 @@ def inp_in_valve_new(name: str, line: str) -> None:
|
|||||||
minor_loss = float(tokens[6])
|
minor_loss = float(tokens[6])
|
||||||
desc = str(tokens[-1]) if has_desc else None
|
desc = str(tokens[-1]) if has_desc else None
|
||||||
|
|
||||||
write(name, f"insert into _link (id, type) values ('{id}', 'valve');")
|
return f"insert into _link (id, type) values ('{id}', 'valve');insert into valves (id, node1, node2, diameter, type, setting, minor_loss) values ('{id}', '{node1}', '{node2}', {diameter}, '{v_type}', '{setting}', {minor_loss});"
|
||||||
write(name, f"insert into valves (id, node1, node2, diameter, type, setting, minor_loss) values ('{id}', '{node1}', '{node2}', {diameter}, '{v_type}', '{setting}', {minor_loss});")
|
|
||||||
|
|
||||||
|
|
||||||
def inp_out_valve(name: str) -> list[str]:
|
def inp_out_valve(name: str) -> list[str]:
|
||||||
|
|||||||
@@ -103,7 +103,7 @@ def inp_in_tag(section: list[str]) -> ChangeSet:
|
|||||||
return cs
|
return cs
|
||||||
|
|
||||||
|
|
||||||
def inp_in_tag_new(name: str, line: str) -> None:
|
def inp_in_tag_new(line: str) -> str:
|
||||||
tokens = line.split()
|
tokens = line.split()
|
||||||
|
|
||||||
num = len(tokens)
|
num = len(tokens)
|
||||||
@@ -115,9 +115,10 @@ def inp_in_tag_new(name: str, line: str) -> None:
|
|||||||
tag = str(tokens[2])
|
tag = str(tokens[2])
|
||||||
|
|
||||||
if t_type == TAG_TYPE_NODE:
|
if t_type == TAG_TYPE_NODE:
|
||||||
write(name, f"insert into tags_node (id, tag) values ('{id}', '{tag}');")
|
return f"insert into tags_node (id, tag) values ('{id}', '{tag}');"
|
||||||
elif t_type == TAG_TYPE_LINK:
|
elif t_type == TAG_TYPE_LINK:
|
||||||
write(name, f"insert into tags_link (id, tag) values ('{id}', '{tag}');")
|
return f"insert into tags_link (id, tag) values ('{id}', '{tag}');"
|
||||||
|
return ''
|
||||||
|
|
||||||
|
|
||||||
def inp_out_tag(name: str) -> list[str]:
|
def inp_out_tag(name: str) -> list[str]:
|
||||||
|
|||||||
@@ -97,7 +97,7 @@ def inp_in_demand(section: list[str]) -> ChangeSet:
|
|||||||
return cs
|
return cs
|
||||||
|
|
||||||
|
|
||||||
def inp_in_demand_new(name: str, line: str) -> None:
|
def inp_in_demand_new(line: str) -> str:
|
||||||
tokens = line.split()
|
tokens = line.split()
|
||||||
|
|
||||||
num = len(tokens)
|
num = len(tokens)
|
||||||
@@ -111,7 +111,7 @@ def inp_in_demand_new(name: str, line: str) -> None:
|
|||||||
category = str(tokens[3]) if num_without_desc >= 4 else None
|
category = str(tokens[3]) if num_without_desc >= 4 else None
|
||||||
category = f"'{category}'" if category != None else 'null'
|
category = f"'{category}'" if category != None else 'null'
|
||||||
|
|
||||||
write(name, f"insert into demands (junction, demand, pattern, category) values ('{junction}', {demand}, {pattern}, {category});")
|
return f"insert into demands (junction, demand, pattern, category) values ('{junction}', {demand}, {pattern}, {category});"
|
||||||
|
|
||||||
|
|
||||||
def inp_out_demand(name: str) -> list[str]:
|
def inp_out_demand(name: str) -> list[str]:
|
||||||
|
|||||||
Reference in New Issue
Block a user