Skip to content

Commit

Permalink
feat: non-interactive mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Banyc committed Dec 9, 2023
1 parent f84a9c0 commit 1475599
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "dfsql"
version = "0.3.0"
version = "0.3.1"
edition = "2021"
description = "SQL REPL for Data Frames"
license = "MIT"
Expand Down
27 changes: 26 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use std::{
borrow::Cow,
collections::HashMap,
io::{BufRead, BufReader, Write},
io::{BufRead, BufReader, Read, Write},
path::{Path, PathBuf},
};

use anyhow::{bail, Context};
use clap::Parser;
use dfsql::{df::apply, sql};
use fancy_regex::Regex;
use handler::{HandleLineResult, LineExecutor};
use polars::prelude::*;
Expand All @@ -19,6 +20,8 @@ const SQL_EXTENSION: &str = "dfsql";

#[derive(Debug, Parser)]
pub struct Cli {
/// `.dfsql` file to execute
sql: Option<PathBuf>,
/// Input file containing a data frame
#[clap(short, long)]
input: PathBuf,
Expand Down Expand Up @@ -47,6 +50,28 @@ impl Cli {
let df = read_df_file(path, self.infer_schema_length)?;
others.insert(name.to_string(), df);
}
if let Some(sql_file) = &self.sql {
// Non-interactive mode
if self.lazy {
bail!(
"`lazy` option is unavailable if a `.{SQL_EXTENSION}` is provided via the argument `sql`"
);
}

let mut file = std::fs::File::options().read(true).open(sql_file)?;
let mut src = String::new();
file.read_to_string(&mut src)?;
drop(file);
let Some(s) = sql::parse(&src) else {
return Ok(());
};
let df = apply(df, &s, &others)?.collect()?;
match &self.output {
Some(output) => write_df_output(df, output)?,
None => println!("{df}"),
}
return Ok(());
}
let mut handler = LineExecutor::new(df.clone(), others);
let mut rl = Editor::new()?;
if !self.lazy {
Expand Down

0 comments on commit 1475599

Please sign in to comment.