-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an experimental
transpile
command
This is still pretty basic.
- Loading branch information
Showing
3 changed files
with
55 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ | |
pub mod parse; | ||
pub mod sql_test; | ||
pub mod transpile; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
//! Transpile code to another dialect of SQL. | ||
use std::path::PathBuf; | ||
|
||
use clap::Parser; | ||
use tracing::instrument; | ||
|
||
use crate::{ | ||
ast::{parse_sql, Emit}, | ||
drivers, | ||
errors::{Context, Result}, | ||
}; | ||
|
||
/// Run SQL tests from a directory. | ||
#[derive(Debug, Parser)] | ||
pub struct TranspileOpt { | ||
/// An SQL file to transpile. | ||
sql_path: PathBuf, | ||
|
||
/// A database locator to run tests against. (For now, this must be a | ||
/// an actual database locator, and not the name of a dialect.) | ||
#[clap(long, visible_alias = "db", default_value = "sqlite3::memory:")] | ||
database: String, | ||
} | ||
|
||
/// Run our SQL test suite. | ||
#[instrument(skip(opt))] | ||
pub async fn cmd_transpile(opt: &TranspileOpt) -> Result<()> { | ||
// Get a database driver for our target. | ||
let locator = opt.database.parse::<Box<dyn drivers::Locator>>()?; | ||
let driver = locator.driver().await?; | ||
|
||
// Parse our SQL. | ||
let sql = tokio::fs::read_to_string(&opt.sql_path) | ||
.await | ||
.with_context(|| format!("could not read SQL file {}", opt.sql_path.display()))?; | ||
let ast = parse_sql(&opt.sql_path, &sql)?; | ||
let rewritten_ast = driver.rewrite_ast(&ast)?; | ||
|
||
// Print our rewritten AST. | ||
for statement in rewritten_ast.extra.native_setup_sql { | ||
println!("{};", statement); | ||
} | ||
let transpiled_sql = rewritten_ast.ast.emit_to_string(locator.target()); | ||
println!("{}", transpiled_sql); | ||
for statement in rewritten_ast.extra.native_teardown_sql { | ||
println!("{};", statement); | ||
} | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters