-
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.
This allows running a single SQL file.
- Loading branch information
Showing
3 changed files
with
50 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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
//! CLI subcommands. | ||
|
||
pub mod parse; | ||
pub mod run; | ||
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,45 @@ | ||
use std::path::PathBuf; | ||
|
||
use clap::Parser; | ||
use tracing::instrument; | ||
|
||
use crate::{ | ||
ast::parse_sql, drivers, errors::Result, infer::InferTypes, known_files::KnownFiles, | ||
scope::Scope, | ||
}; | ||
|
||
/// Run an SQL file using the specified database. | ||
#[derive(Debug, Parser)] | ||
pub struct RunOpt { | ||
/// 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. | ||
/// | ||
/// TODO: Deduplicate this with the transpile command. | ||
#[instrument(skip(opt))] | ||
pub async fn cmd_run(files: &mut KnownFiles, opt: &RunOpt) -> Result<()> { | ||
// Get a database driver for our target. | ||
let locator = opt.database.parse::<Box<dyn drivers::Locator>>()?; | ||
let mut driver = locator.driver().await?; | ||
|
||
// Parse our SQL. | ||
let file_id = files.add(&opt.sql_path)?; | ||
let mut ast = parse_sql(files, file_id)?; | ||
|
||
// Run the type checker, but do not fail on errors. | ||
let scope = Scope::root(); | ||
if let Err(err) = ast.infer_types(&scope) { | ||
err.emit(files); | ||
eprintln!("\nType checking failed. Manual fixes will probably be required!"); | ||
} | ||
|
||
// Run our AST. | ||
driver.execute_ast(&ast).await | ||
} |
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