Skip to content

fix: extract sql fn body properly #346

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 19, 2025
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
24 changes: 24 additions & 0 deletions crates/pgt_workspace/src/workspace/server/parsed_document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -404,3 +404,27 @@ impl<'a> StatementFilter<'a> for IdFilter {
*id == self.id
}
}

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

use pgt_fs::PgTPath;

#[test]
fn sql_function_body() {
let input = "CREATE FUNCTION add(integer, integer) RETURNS integer
AS 'select $1 + $2;'
LANGUAGE SQL
IMMUTABLE
RETURNS NULL ON NULL INPUT;";

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

let d = ParsedDocument::new(path, input.to_string(), 0);

let stmts = d.iter(DefaultMapper).collect::<Vec<_>>();

assert_eq!(stmts.len(), 2);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Love it!

should we add a test asserting the content as well?
and maybe a test using the $$ notation?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

not really necessary imo - we can trust pg_query here

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

will do it in a follow up

}
}
11 changes: 11 additions & 0 deletions crates/pgt_workspace/src/workspace/server/sql_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use pgt_text_size::TextRange;

use super::statement_identifier::StatementId;

#[derive(Debug, Clone)]
pub struct SQLFunctionBody {
pub range: TextRange,
pub body: String,
Expand Down Expand Up @@ -97,6 +98,16 @@ fn find_option_value(
.find_map(|arg| {
if let pgt_query_ext::NodeEnum::String(s) = arg {
Some(s.sval.clone())
} else if let pgt_query_ext::NodeEnum::List(l) = arg {
l.items.iter().find_map(|item_wrapper| {
if let Some(pgt_query_ext::NodeEnum::String(s)) =
item_wrapper.node.as_ref()
{
Some(s.sval.clone())
} else {
None
}
})
} else {
None
}
Expand Down