Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix: positional params
  • Loading branch information
psteinroe committed Aug 12, 2025
commit d51b9ca86835cb91af720ae080e5ddca5b884441
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/pgt_workspace/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ pgt_schema_cache = { workspace = true }
pgt_statement_splitter = { workspace = true }
pgt_suppressions = { workspace = true }
pgt_text_size.workspace = true
pgt_tokenizer = { workspace = true }
pgt_typecheck = { workspace = true }
pgt_workspace_macros = { workspace = true }
rustc-hash = { workspace = true }
Expand Down
5 changes: 2 additions & 3 deletions crates/pgt_workspace/src/workspace/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use document::{
TypecheckDiagnosticsMapper,
};
use futures::{StreamExt, stream};
use pg_query::convert_to_positional_params;
use pgt_analyse::{AnalyserOptions, AnalysisFilter};
use pgt_analyser::{Analyser, AnalyserConfig, AnalyserParams};
use pgt_diagnostics::{
Expand Down Expand Up @@ -468,7 +469,7 @@ impl Workspace for WorkspaceServer {
// Type checking
let typecheck_result = pgt_typecheck::check_sql(TypecheckParams {
conn: &pool,
sql: id.content(),
sql: convert_to_positional_params(id.content()).as_str(),
ast: &ast,
tree: &cst,
schema_cache: schema_cache.as_ref(),
Expand Down Expand Up @@ -511,8 +512,6 @@ impl Workspace for WorkspaceServer {
.await
.unwrap_or_else(|_| vec![]);

println!("{:#?}", plpgsql_check_results);

for d in plpgsql_check_results {
let r = d.span.map(|span| span + range.start());
diagnostics.push(
Expand Down
54 changes: 54 additions & 0 deletions crates/pgt_workspace/src/workspace/server.tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,3 +277,57 @@ async fn test_dedupe_diagnostics(test_db: PgPool) {
Some(TextRange::new(115.into(), 210.into()))
);
}

#[sqlx::test(migrator = "pgt_test_utils::MIGRATIONS")]
async fn test_positional_params(test_db: PgPool) {
let mut conf = PartialConfiguration::init();
conf.merge_with(PartialConfiguration {
db: Some(PartialDatabaseConfiguration {
database: Some(
test_db
.connect_options()
.get_database()
.unwrap()
.to_string(),
),
..Default::default()
}),
..Default::default()
});

let workspace = get_test_workspace(Some(conf)).expect("Unable to create test workspace");

let path = PgTPath::new("test.sql");

let setup_sql = r"
create table users (
id serial primary key,
name text not null,
email text not null
);
";
test_db.execute(setup_sql).await.expect("setup sql failed");

let content = r#"select * from users where id = @one and name = :two and email = :'three';"#;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:'three' – i want to meet the person who writes syntax like this haha

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

haha yeah I did for some time two years ago :D


workspace
.open_file(OpenFileParams {
path: path.clone(),
content: content.into(),
version: 1,
})
.expect("Unable to open test file");

let diagnostics = workspace
.pull_diagnostics(crate::workspace::PullDiagnosticsParams {
path: path.clone(),
categories: RuleCategories::all(),
max_diagnostics: 100,
only: vec![],
skip: vec![],
})
.expect("Unable to pull diagnostics")
.diagnostics;

assert_eq!(diagnostics.len(), 0, "Expected no diagnostic");
}
53 changes: 52 additions & 1 deletion crates/pgt_workspace/src/workspace/server/pg_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::sync::{Arc, Mutex};
use lru::LruCache;
use pgt_query_ext::diagnostics::*;
use pgt_text_size::TextRange;
use pgt_tokenizer::tokenize;

use super::statement_identifier::StatementId;

Expand Down Expand Up @@ -37,7 +38,7 @@ impl PgQueryStore {
}

let r = Arc::new(
pgt_query::parse(statement.content())
pgt_query::parse(&convert_to_positional_params(statement.content()))
.map_err(SyntaxDiagnostic::from)
.and_then(|ast| {
ast.into_root().ok_or_else(|| {
Expand Down Expand Up @@ -87,10 +88,60 @@ impl PgQueryStore {
}
}

/// Converts named parameters in a SQL query string to positional parameters.
///
/// This function scans the input SQL string for named parameters (e.g., `@param`, `:param`, `:'param'`)
/// and replaces them with positional parameters (e.g., `$1`, `$2`, etc.).
///
/// It maintains the original spacing of the named parameters in the output string.
///
/// Useful for preparing SQL queries for parsing or execution where named paramters are not supported.
pub fn convert_to_positional_params(text: &str) -> String {
let mut result = String::new();
let mut param_index = 1;
let mut position = 0;

for token in tokenize(text) {
let token_len = token.len as usize;
let token_text = &text[position..position + token_len];

if matches!(token.kind, pgt_tokenizer::TokenKind::NamedParam { .. }) {
let replacement = format!("${}", param_index);
let original_len = token_text.len();
let replacement_len = replacement.len();

result.push_str(&replacement);

// maintain original spacing
if replacement_len < original_len {
result.push_str(&" ".repeat(original_len - replacement_len));
}

param_index += 1;
} else {
result.push_str(token_text);
}

position += token_len;
}

result
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_convert_to_positional_params() {
let input = "select * from users where id = @one and name = :two and email = :'three';";
let result = convert_to_positional_params(input);
assert_eq!(
result,
"select * from users where id = $1 and name = $2 and email = $3;"
);
}

#[test]
fn test_plpgsql_syntax_error() {
let input = "
Expand Down
Loading