Skip to content
Open
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
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
name: Format
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/checkout@v6

- uses: dtolnay/rust-toolchain@stable
with:
Expand All @@ -42,7 +42,7 @@ jobs:
name: Clippy
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/checkout@v6

- uses: dtolnay/rust-toolchain@stable
with:
Expand All @@ -57,7 +57,7 @@ jobs:
name: Test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/checkout@v6

- uses: dtolnay/rust-toolchain@stable

Expand Down
42 changes: 35 additions & 7 deletions packages/sqltk-codegen/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,32 @@ use proc_macro2::TokenStream;

use quote::{quote, ToTokens, TokenStreamExt};

use std::{collections::HashSet, fs::File, io::Write, path::PathBuf};
use std::{
collections::HashSet,
fs::File,
io::Write,
path::PathBuf,
process::{Command, Stdio},
};

fn rustfmt(source: &str) -> Option<String> {
let mut child = Command::new("rustfmt")
.args(["--edition", "2021", "--emit", "stdout"])
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
.ok()?;

child.stdin.as_mut()?.write_all(source.as_bytes()).ok()?;

let output = child.wait_with_output().ok()?;
if output.status.success() {
String::from_utf8(output.stdout).ok()
} else {
None
}
}

pub struct Codegen {
meta: SqlParserMetaQuery,
Expand Down Expand Up @@ -34,7 +59,7 @@ impl Codegen {
let transformable_impls_for_main_nodes = main_nodes.iter().map(|(type_path, type_def)| {
TransformableImpl::new(
type_path.clone(),
AstNode::SqlParserTypeDef(type_def.clone()),
AstNode::SqlParserTypeDef(Box::new(type_def.clone())),
)
});

Expand All @@ -57,9 +82,11 @@ impl Codegen {
let formatted = parsed.map(|parsed| prettyplease::unparse(&parsed));

match formatted {
Ok(formatted) => file
.write_all(formatted.as_bytes())
.unwrap_or_else(|_| panic!("Could not write to {}", &dest_file.display())),
Ok(formatted) => {
let canonical = rustfmt(&formatted).unwrap_or(formatted);
file.write_all(canonical.as_bytes())
.unwrap_or_else(|_| panic!("Could not write to {}", &dest_file.display()))
}
Err(_) => file
.write_all(generated_code.to_string().as_bytes())
.unwrap_or_else(|_| panic!("Could not write to {}", &dest_file.display())),
Expand Down Expand Up @@ -98,7 +125,7 @@ impl Codegen {
let visitable_impls_for_main_nodes = main_nodes.into_iter().map(|(type_path, type_def)| {
VisitableImpl::new(
type_path,
AstNode::SqlParserTypeDef(type_def),
AstNode::SqlParserTypeDef(Box::new(type_def)),
reachability.clone(),
terminal_nodes.clone(),
)
Expand Down Expand Up @@ -129,7 +156,8 @@ impl Codegen {
.expect("BUG! Generated Rust code could not be parsed"),
);

file.write_all(formatted.as_bytes())
let canonical = rustfmt(&formatted).unwrap_or(formatted);
file.write_all(canonical.as_bytes())
.unwrap_or_else(|_| panic!("Could not write to {}", &dest_file.display()));
}
}
2 changes: 1 addition & 1 deletion packages/sqltk-codegen/src/meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ pub struct SqlParserTypeDef {
}

pub enum AstNode {
SqlParserTypeDef(SqlParserTypeDef),
SqlParserTypeDef(Box<SqlParserTypeDef>),
TerminalNode(TerminalNode),
}

Expand Down
22 changes: 9 additions & 13 deletions packages/sqltk-codegen/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,25 +105,21 @@ impl SqlParserAstAnalyser {
trait_: Some((_, trait_path, _)),
self_ty,
..
}) => {
if is_impl_of_sqlparser_visit(trait_path) {
let type_path = syn_type_to_path(path, self_ty.deref());
if let Some(ty) = self
.internal_types
.get_mut(&InternalTypePath(type_path.clone()))
{
ty.has_visit_impl = true;
}
}) if is_impl_of_sqlparser_visit(trait_path) => {
let type_path = syn_type_to_path(path, self_ty.deref());
if let Some(ty) = self
.internal_types
.get_mut(&InternalTypePath(type_path.clone()))
{
ty.has_visit_impl = true;
}
}
Item::Use(ItemUse {
tree,
vis: Visibility::Public(_),
..
}) => {
if in_public_mod {
self.walk_use_tree(tree, &mut path.clone());
}
}) if in_public_mod => {
self.walk_use_tree(tree, &mut path.clone());
}
_ => {}
}
Expand Down
6 changes: 2 additions & 4 deletions packages/sqltk-codegen/src/sqlparser_node_extractor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@ use super::meta::SqlParserMeta;
use super::*;

pub fn extract() -> SqlParserMeta {
let sqlparser_features = vec!["visitor", "bigdecimal"];
let sqlparser_features = ["visitor", "bigdecimal"];

let sql_parser_dir = Path::new(
std::env::var("CARGO_MANIFEST_DIR").unwrap().as_str()
)
let sql_parser_dir = Path::new(std::env::var("CARGO_MANIFEST_DIR").unwrap().as_str())
.join("..")
.join("sqltk-parser");

Expand Down
2 changes: 1 addition & 1 deletion packages/sqltk-parser/src/ast/data_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub enum EnumMember {
/// ClickHouse allows to specify an integer value for each enum value.
///
/// [ClickHouse](https://clickhouse.com/docs/en/sql-reference/data-types/enum)
NamedValue(String, Expr),
NamedValue(String, Box<Expr>),
}

/// SQL data types
Expand Down
6 changes: 3 additions & 3 deletions packages/sqltk-parser/src/ast/dcl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ impl fmt::Display for RoleOption {
pub enum SetConfigValue {
Default,
FromCurrent,
Value(Expr),
Value(Box<Expr>),
}

/// RESET config option:
Expand Down Expand Up @@ -143,7 +143,7 @@ pub enum AlterRoleOperation {
},
Set {
config_name: ObjectName,
config_value: SetConfigValue,
config_value: Box<SetConfigValue>,
in_database: Option<ObjectName>,
},
Reset {
Expand Down Expand Up @@ -176,7 +176,7 @@ impl fmt::Display for AlterRoleOperation {
write!(f, "IN DATABASE {} ", database_name)?;
}

match config_value {
match config_value.as_ref() {
SetConfigValue::Default => write!(f, "SET {config_name} TO DEFAULT"),
SetConfigValue::FromCurrent => write!(f, "SET {config_name} FROM CURRENT"),
SetConfigValue::Value(expr) => write!(f, "SET {config_name} TO {expr}"),
Expand Down
12 changes: 6 additions & 6 deletions packages/sqltk-parser/src/ast/ddl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,8 +321,8 @@ pub enum AlterPolicyOperation {
},
Apply {
to: Option<Vec<Owner>>,
using: Option<Expr>,
with_check: Option<Expr>,
using: Box<Option<Expr>>,
with_check: Box<Option<Expr>>,
},
}

Expand All @@ -340,10 +340,10 @@ impl fmt::Display for AlterPolicyOperation {
if let Some(to) = to {
write!(f, " TO {}", display_comma_separated(to))?;
}
if let Some(using) = using {
if let Some(using) = using.as_ref() {
write!(f, " USING ({using})")?;
}
if let Some(with_check) = with_check {
if let Some(with_check) = with_check.as_ref() {
write!(f, " WITH CHECK ({with_check})")?;
}
Ok(())
Expand Down Expand Up @@ -1706,7 +1706,7 @@ pub enum ColumnOption {
/// ```
/// [MS SQL Server]: https://learn.microsoft.com/en-us/sql/t-sql/statements/create-table-transact-sql-identity-property
/// [Snowflake]: https://docs.snowflake.com/en/sql-reference/sql/create-table
Identity(IdentityPropertyKind),
Identity(Box<IdentityPropertyKind>),
/// SQLite specific: ON CONFLICT option on column definition
/// <https://www.sqlite.org/lang_conflict.html>
OnConflict(Keyword),
Expand Down Expand Up @@ -2114,7 +2114,7 @@ impl fmt::Display for Partition {
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
pub enum Deduplicate {
All,
ByExpression(Expr),
ByExpression(Box<Expr>),
}

impl fmt::Display for Deduplicate {
Expand Down
14 changes: 7 additions & 7 deletions packages/sqltk-parser/src/ast/helpers/stmt_data_loading.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ pub struct StageParamsObject {
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
pub enum StageLoadSelectItemKind {
SelectItem(SelectItem),
SelectItem(Box<SelectItem>),
StageLoadSelectItem(StageLoadSelectItem),
}

Expand Down Expand Up @@ -99,15 +99,15 @@ impl fmt::Display for StageParamsObject {

impl fmt::Display for StageLoadSelectItem {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if self.alias.is_some() {
write!(f, "{}.", self.alias.as_ref().unwrap())?;
if let Some(alias) = &self.alias {
write!(f, "{}.", alias)?;
}
write!(f, "${}", self.file_col_num)?;
if self.element.is_some() {
write!(f, ":{}", self.element.as_ref().unwrap())?;
if let Some(element) = &self.element {
write!(f, ":{}", element)?;
}
if self.item_as.is_some() {
write!(f, " AS {}", self.item_as.as_ref().unwrap())?;
if let Some(item_as) = &self.item_as {
write!(f, " AS {}", item_as)?;
}
Ok(())
}
Expand Down
Loading