-
Notifications
You must be signed in to change notification settings - Fork 1
test: check query validation #20
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,7 +23,7 @@ use crate::writer::SqlWriter; | |
|
|
||
| /// SQL value variants. | ||
| #[derive(Debug, Clone, PartialEq)] | ||
| #[expect(missing_docs)] | ||
| #[allow(missing_docs)] | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Clippy false positive - https://github.com/rust-lang/rust-clippy/issues/16534 |
||
| pub enum Value { | ||
| Bool(Option<bool>), | ||
| TinyInt(Option<i8>), | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| // Copyright 2025 FastLabs Developers | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| pub trait ValidateSql { | ||
| fn validate(self) -> Self; | ||
| } | ||
|
|
||
| impl ValidateSql for String { | ||
| #[track_caller] | ||
| fn validate(self) -> Self { | ||
| pg_query::parse(self.as_str()).unwrap(); | ||
tisonkun marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| self | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,13 +12,17 @@ | |
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| mod common; | ||
|
|
||
| use insta::assert_snapshot; | ||
| use pqb::expr::Expr; | ||
| use pqb::index::CreateIndex; | ||
| use pqb::table::ColumnDef; | ||
| use pqb::table::ColumnType; | ||
| use pqb::table::CreateTable; | ||
|
|
||
| use crate::common::ValidateSql; | ||
|
|
||
| #[test] | ||
| fn create_table_basic() { | ||
| assert_snapshot!( | ||
|
|
@@ -28,7 +32,8 @@ fn create_table_basic() { | |
| .column(ColumnDef::new("email").text().not_null()) | ||
| .column(ColumnDef::new("nickname").text().null()) | ||
| .column(ColumnDef::new("created_at").timestamp_with_time_zone()) | ||
| .to_sql(), | ||
| .to_sql() | ||
| .validate(), | ||
| @r#"CREATE TABLE "users" ( "id" bigint NOT NULL, "email" text NOT NULL, "nickname" text NULL, "created_at" timestamp with time zone )"# | ||
| ); | ||
| } | ||
|
|
@@ -42,7 +47,8 @@ fn create_table_if_not_exists_temporary() { | |
| .table("cache") | ||
| .column(ColumnDef::new("key").text().not_null()) | ||
| .column(ColumnDef::new("value").json_binary()) | ||
| .to_sql(), | ||
| .to_sql() | ||
| .validate(), | ||
| @r#"CREATE TEMPORARY TABLE IF NOT EXISTS "cache" ( "key" text NOT NULL, "value" jsonb )"# | ||
| ); | ||
| } | ||
|
|
@@ -55,7 +61,8 @@ fn create_table_primary_key_index() { | |
| .column(ColumnDef::new("id").int()) | ||
| .column(ColumnDef::new("name").text()) | ||
| .primary_key(CreateIndex::new().column("id")) | ||
| .to_sql(), | ||
| .to_sql() | ||
| .validate(), | ||
| @r#"CREATE TABLE "widgets" ( "id" integer, "name" text, PRIMARY KEY ("id") )"# | ||
| ); | ||
| } | ||
|
|
@@ -78,6 +85,7 @@ fn create_table_generated_column() { | |
| .generated_as_virtual(Expr::column("sum").div(Expr::value(2))), | ||
| ) | ||
| .to_sql(), | ||
| // .validate(), // TODO: Before PostgreSQL 18, STORED is the only supported kind and must be specified. | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Waiting for pganalyze/libpg_query#304 |
||
| @r#"CREATE TABLE "calc" ( "a" integer, "b" integer, "sum" integer GENERATED ALWAYS AS ("a" + "b") STORED, "avg" integer GENERATED ALWAYS AS ("sum" / 2) VIRTUAL )"# | ||
| ); | ||
| } | ||
|
|
@@ -116,7 +124,8 @@ fn create_table_all_column_types() { | |
| .column(ColumnDef::new("col_jsonb").json_binary()) | ||
| .column(ColumnDef::new("col_uuid").uuid()) | ||
| .column(ColumnDef::new("col_int_array").array_of(ColumnType::Int)) | ||
| .to_sql(), | ||
| .to_sql() | ||
| .validate(), | ||
| @r#"CREATE TABLE "all_types" ( "col_char" char(4), "col_varchar" varchar(10), "col_text" text, "col_bytea" bytea, "col_smallint" smallint, "col_int" integer, "col_bigint" bigint, "col_float" real, "col_double" double precision, "col_numeric" numeric(10, 2), "col_smallserial" smallserial, "col_serial" serial, "col_bigserial" bigserial, "col_int4range" int4range, "col_int8range" int8range, "col_numrange" numrange, "col_tsrange" tsrange, "col_tstzrange" tstzrange, "col_daterange" daterange, "col_datetime" timestamp without time zone, "col_timestamp" timestamp, "col_timestamptz" timestamp with time zone, "col_time" time, "col_date" date, "col_bool" bool, "col_json" json, "col_jsonb" jsonb, "col_uuid" uuid, "col_int_array" integer[] )"# | ||
| ); | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.