Skip to content

Commit

Permalink
Add a run command
Browse files Browse the repository at this point in the history
This allows running a single SQL file.
  • Loading branch information
emk committed Nov 6, 2023
1 parent 985881a commit cbd1946
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/cmd/mod.rs
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;
45 changes: 45 additions & 0 deletions src/cmd/run.rs
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
}
4 changes: 4 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ mod util;

use cmd::{
parse::{cmd_parse, ParseOpt},
run::{cmd_run, RunOpt},
sql_test::{cmd_sql_test, SqlTestOpt},
transpile::{cmd_transpile, TranspileOpt},
};
Expand All @@ -28,6 +29,8 @@ use tracing::info_span;
enum Opt {
/// Parse SQL from a CSV file containing `id` and `query` columns.
Parse(ParseOpt),
/// Run an SQL file.
Run(RunOpt),
/// Run SQL tests from a directory.
SqlTest(SqlTestOpt),
/// Transpile BigQuery SQL to another dialect.
Expand All @@ -44,6 +47,7 @@ async fn main() {
let mut files = KnownFiles::new();
let result = match opt {
Opt::Parse(parse_opt) => cmd_parse(&mut files, &parse_opt),
Opt::Run(run_opt) => cmd_run(&mut files, &run_opt).await,
Opt::SqlTest(sql_test_opt) => cmd_sql_test(&mut files, &sql_test_opt).await,
Opt::Transpile(transpile_opt) => cmd_transpile(&mut files, &transpile_opt).await,
};
Expand Down

0 comments on commit cbd1946

Please sign in to comment.