Skip to content

Commit 8aafb5a

Browse files
committed
Updated query parameter binding
1 parent 7250cf2 commit 8aafb5a

File tree

11 files changed

+52
-23
lines changed

11 files changed

+52
-23
lines changed

services/column-footprint-editor/macrostrat/column_footprint_editor/api/column_groups.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@ async def get(self, request):
2424

2525
if "id" in request.query_params:
2626
id_ = request.query_params["id"]
27-
sql = (
28-
"SELECT * FROM ${project_schema}.column_groups WHERE id = " + f"{id_};"
29-
)
27+
sql = "SELECT * FROM {project_schema}.column_groups WHERE id = " + f"{id_};"
3028

3129
try:
3230
df = project.db.exec_query(sql)
@@ -36,7 +34,7 @@ async def get(self, request):
3634
except error:
3735
return JSONResponse({"error": f"col_group {id_} does not exist"})
3836

39-
sql = """SELECT * from ${project_schema}.column_groups;"""
37+
sql = """SELECT * from {project_schema}.column_groups;"""
4038
try:
4139
df = project.db.exec_query(sql)
4240
col_groups = df.to_dict(orient="records")
@@ -49,7 +47,7 @@ async def get(self, request):
4947
async def post(self, request):
5048
"""Endpoint for new Column Groups"""
5149

52-
sql = """INSERT INTO ${project_schema}.column_groups(col_group, col_group_name, color)VALUES(
50+
sql = """INSERT INTO {project_schema}.column_groups(col_group, col_group_name, color)VALUES(
5351
:col_group,:col_group_name,:color
5452
) """
5553

@@ -62,7 +60,7 @@ async def post(self, request):
6260
try:
6361
project.db.run_sql(sql, params)
6462

65-
sql = "SELECT id FROM ${project_schema}.column_groups WHERE col_group = :col_group"
63+
sql = "SELECT id FROM {project_schema}.column_groups WHERE col_group = :col_group"
6664
res = project.db.exec_sql(sql, params=params, count=1)
6765
params["col_group_id"] = res.id
6866
except error:
@@ -76,7 +74,7 @@ async def put(self, request):
7674
"""Endpoint for Editing Existing Column Groups"""
7775

7876
sql = """
79-
UPDATE ${project_schema}.column_groups cg
77+
UPDATE {project_schema}.column_groups cg
8078
SET col_group = :col_group,
8179
col_group_name = :col_group_name,
8280
color = :color

services/column-footprint-editor/macrostrat/column_footprint_editor/api/geometries.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ async def get_csv(request):
202202
c.col_group_id,
203203
c.col_group,
204204
c.col_group_name
205-
from ${project_schema}.column_map_face c; """
205+
from {project_schema}.column_map_face c; """
206206
df = project.db.exec_query(sql)
207207
csv = df.to_csv()
208208

services/column-footprint-editor/macrostrat/column_footprint_editor/database/__init__.py

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
from macrostrat.database import Database as BaseDatabase
2-
from macrostrat.database.utils import get_sql_text
31
from pathlib import Path
2+
3+
from macrostrat.database import Database as BaseDatabase
4+
from macrostrat.database.utils import get_sql_text, _split_params, _render_query
5+
from psycopg2.sql import SQL
46
from sqlalchemy.orm import sessionmaker
57

68
from .sql_formatter import SqlFormatter
@@ -22,6 +24,13 @@
2224
project_info_insert = procedures / "project-meta-insert.sql"
2325
project_table = fixtures / "projects_table.sql"
2426

27+
color_description = here / "schema-updates" / "color-description.sql"
28+
29+
30+
from macrostrat.utils import get_logger
31+
32+
log = get_logger("uvicorn.error")
33+
2534

2635
class Database(BaseDatabase):
2736
"""
@@ -61,6 +70,18 @@ def exec_query(self, filename_or_query, **kwargs):
6170

6271
txt = get_sql_text(filename_or_query)
6372

73+
params = self.instance_params.copy()
74+
for k, v in kwargs.get("params", {}).items():
75+
params[k] = v
76+
77+
# Pre-fill bind parameters
78+
params, pre_bind_params = _split_params(params)
79+
if pre_bind_params is not None:
80+
query = SQL(txt).format(**pre_bind_params)
81+
txt = _render_query(query, self.engine)
82+
83+
kwargs["params"] = params
84+
log.info(params)
6485
return read_sql(txt, self.engine, **kwargs)
6586

6687
#################### db initialization methods ##########################
@@ -113,8 +134,17 @@ def remove_project(self, params={}):
113134

114135
################## db topology methods ##############################
115136

137+
def create_topology_tables(self):
138+
# Drop topology in case it already exists: (TODO: make this safer)
139+
run_topology_command(self, self.project_id, "delete")
140+
141+
run_topology_command(self, self.project_id, "create_tables")
142+
143+
# Create color-description tables
144+
self.run_sql_file(color_description)
145+
116146
def update_topology(self):
117-
run_topology_command(self.project_id, "update")
147+
run_topology_command(self, self.project_id, "update")
118148

119149
###################### Project-Free methods ########################
120150

services/column-footprint-editor/macrostrat/column_footprint_editor/database/queries/get-points.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
SELECT
2-
ST_AsGeoJSON(point) point,
2+
ST_AsGeoJSON(geometry) point,
33
id,
44
project_id,
55
col_id,

services/column-footprint-editor/macrostrat/column_footprint_editor/database/schema-updates/color-description.sql

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
-- Add a 'color' column to every column_group table and a 'description' column to every columns table.
1+
-- Add a 'color' column to every column_group table and a 'description' column to every columns table.
22
-- Loops through each project schema.
33
-- Updates the view's for each as well
44

55
do $$
6-
DECLARE
6+
DECLARE
77
table_ record;
88
BEGIN
99
FOR table_ IN
1010
SELECT * from pg_tables WHERE tablename LIKE 'column_groups'
1111
LOOP
1212
RAISE info '%.column_groups',table_.schemaname;
1313
EXECUTE
14-
'ALTER TABLE '|| table_.schemaname ||'.column_groups ADD COLUMN color text;'
15-
'ALTER TABLE '|| table_.schemaname ||'.columns ADD COLUMN description text;'
14+
'ALTER TABLE '|| table_.schemaname ||'.column_groups ADD COLUMN IF NOT EXISTS color text;'
15+
'ALTER TABLE '|| table_.schemaname ||'.columns ADD COLUMN IF NOT EXISTS description text;'
1616
'DROP VIEW IF EXISTS ' || table_.schemaname || '.column_map_face;'
1717
'CREATE VIEW ' || table_.schemaname || '.column_map_face AS
1818
WITH A as(

services/column-footprint-editor/macrostrat/column_footprint_editor/init_importer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def is_json(myjson):
3232
# importer.import_column_topology()
3333

3434
# project = Project(10)
35-
# sql = """ SELECT ST_AsGeoJSON(geometry) polygon, id, project_id, col_id, col_name, col_group_id, col_group, col_group_name, col_color from ${project_schema}.column_map_face c;"""
35+
# sql = """ SELECT ST_AsGeoJSON(geometry) polygon, id, project_id, col_id, col_name, col_group_id, col_group, col_group_name, col_color from {project_schema}.column_map_face c;"""
3636
# df = project.db.exec_query(sql)
3737

3838
# cols = df.to_dict(orient="records")

services/column-footprint-editor/macrostrat/column_footprint_editor/project/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import requests
21
from pathlib import Path
2+
3+
import requests
34
from psycopg2.sql import Identifier
45

56
from mapboard.topology_manager.database import _get_instance_params
@@ -59,7 +60,7 @@ def insert_project_info(self):
5960

6061
def insert_project_column_groups(self):
6162
route = f"{self.base_url}col_groups?project_id=eq.{self.id}"
62-
res = requests.get(route)
63+
res = requests.get(route, verify=False)
6364
data = res.json()
6465
for column in data:
6566
params = dict(

services/column-footprint-editor/macrostrat/column_footprint_editor/project/exporter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def export_new_project(self):
5252
## insert col-groups get ids and names (need project_id)
5353
## use hash-table to store {col-group-name: col-group-id}
5454
## for col insertions
55-
col_group_sql = """ SELECT col_group, col_group_name FROM ${project_schema}.column_groups; """
55+
col_group_sql = """ SELECT col_group, col_group_name FROM {project_schema}.column_groups; """
5656
col_groups = self.db.exec_sql(col_group_sql)
5757
col_group_lookup = {}
5858
for col_group in col_groups:

services/column-footprint-editor/macrostrat/column_footprint_editor/project/importer.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def __init__(self, project_id: int, name: str, description: str):
3030
def get_project_json(self):
3131
project_id = self.project.id
3232
url = f"{self.base_url}cols?project_id=eq.{project_id}"
33-
res = requests.get(url)
33+
res = requests.get(url, verify=False)
3434
data = res.json()
3535

3636
return data
@@ -65,6 +65,7 @@ def import_column_topology(self):
6565
self.project.insert_project_column_groups()
6666
self.columns_import()
6767
self.db.on_project_insert()
68+
self.db.create_topology_tables()
6869
self.db.update_topology()
6970
self.db.redump_linework_from_edge()
7071
self.db.update_topology()

services/column-footprint-editor/macrostrat/column_footprint_editor/settings.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from starlette.config import Config
2-
from os import path
32

43
config = Config(".env")
54

0 commit comments

Comments
 (0)