Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions query/src/org/labkey/query/controllers/LabKeySql.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
11 changes: 10 additions & 1 deletion query/src/org/labkey/query/controllers/QueryController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down