From 330d7794861099fbbf2fb6e49d7287565d12750a Mon Sep 17 00:00:00 2001 From: Matthew Bellew Date: Wed, 11 Feb 2026 16:43:37 -0800 Subject: [PATCH] Postgres validate after LabKey parse --- query/src/org/labkey/query/controllers/LabKeySql.md | 7 ++++--- .../org/labkey/query/controllers/QueryController.java | 11 ++++++++++- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/query/src/org/labkey/query/controllers/LabKeySql.md b/query/src/org/labkey/query/controllers/LabKeySql.md index 82f4e45ca78..e39099d2f76 100644 --- a/query/src/org/labkey/query/controllers/LabKeySql.md +++ b/query/src/org/labkey/query/controllers/LabKeySql.md @@ -94,12 +94,13 @@ You can write queries that access data from different folders within the LabKey LabKey SQL supports **parameterized queries** to improve security and reusability. * **Syntax:** - `PARAMETERS(param1, param2) SELECT * FROM table WHERE column = param1` + `PARAMETERS(param1 type, param2 type DEFAULT value) SELECT * FROM table WHERE column = param1` * **Functionality:** - The `PARAMETERS` keyword declares parameters that can be passed into the query. + The `PARAMETERS` keyword declares parameters that can be passed into the query. If a DEFAULT is + not specified, the value will default to NULL. * **Example:** A query with two parameters, `MinTemp` and `MinWeight`: ```sql - PARAMETERS(MinTemp double, MinWeight double) + PARAMETERS(MinTemp double, MinWeight double DEFAULT 0.0) SELECT ParticipantID, temperature_C, diff --git a/query/src/org/labkey/query/controllers/QueryController.java b/query/src/org/labkey/query/controllers/QueryController.java index 0d14f2a7b27..fe0fa0a6c18 100644 --- a/query/src/org/labkey/query/controllers/QueryController.java +++ b/query/src/org/labkey/query/controllers/QueryController.java @@ -132,6 +132,7 @@ import org.labkey.api.data.SchemaTableInfo; import org.labkey.api.data.ShowRows; import org.labkey.api.data.SimpleFilter; +import org.labkey.api.data.SqlExecutor; import org.labkey.api.data.SqlSelector; import org.labkey.api.data.TSVWriter; import org.labkey.api.data.Table; @@ -8920,9 +8921,17 @@ public Object execute(SqlPromptForm form, BindException errors) throws Exception if (warning.isPresent()) throw warning.get(); } + // if that worked, let have the DB check it too + if (ti.getSqlDialect().isPostgreSQL()) + { + // CONSIDER: will this work with LabKey SQL named parameters? + SQLFragment sql = new SQLFragment("PREPARE validate AS SELECT * FROM ").append(ti.getFromSQL("MYVALIDATEQUERY__")); + new SqlExecutor(ti.getSchema().getScope()).execute(sql); + } } - catch (QueryException x) + catch (Exception x) { + // CONSIDER remove line line/character information from DB errors as they won't match the LabKey SQL String validationPrompt = "That SQL caused the " + (x instanceof QueryParseWarning ? "warning" : "error") + " below, can you attempt to fix this?\n```" + x.getMessage() + "```"; responses = McpService.get().sendMessageEx(chatSession, validationPrompt); var newSqlResponse = extractSql(responses);