Skip to content

Commit ac75285

Browse files
1BADragonCedarMatt
andauthored
Add to_sql module. (#49)
* Add to_sql module * wip * Test passing * Add tests to makefile --------- Co-authored-by: matt <[email protected]>
1 parent 9fb1a6c commit ac75285

File tree

13 files changed

+1198
-6
lines changed

13 files changed

+1198
-6
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[workspace]
2-
members = ["rscel", "python", "wasm", "rscel-macro"]
2+
members = ["rscel", "python", "wasm", "rscel-macro", "extensions/to_sql"]
33
default-members = ["rscel"]
44
resolver = "2"
55

Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ all: wasm-binding python-binding build
3636
run-tests:
3737
RSCEL_TEST_PROTO=1 cargo test -q $(CARGO_ARGS)
3838

39+
run-no-sql-tests:
40+
cargo test --manifest-path=extensions/to_sql/Cargo.toml -q
41+
3942
run-no-feature-tests:
4043
cargo test -q --no-default-features $(CARGO_ARGS)
4144

@@ -55,4 +58,4 @@ run-wasm-tests:
5558
$(MAKE_COMMAND) -C wasm wasm-tests
5659

5760
.PHONY: run-all-tests
58-
run-all-tests: run-tests run-no-feature-tests run-python-tests run-wasm-tests
61+
run-all-tests: run-tests run-no-feature-tests run-no-sql-tests run-python-tests run-wasm-tests

extensions/to_sql/Cargo.toml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[package]
2+
name = "rscel-to-sql"
3+
version = { workspace = true }
4+
edition = { workspace = true }
5+
description = { workspace = true }
6+
license = { workspace = true }
7+
readme = "../README.md"
8+
9+
[dependencies]
10+
rscel = { path = "../../rscel" }
11+
12+
[dev-dependencies]
13+
test-case = "3.3.1"
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
use rscel::Program;
2+
use rscel_to_sql::IntoSqlBuilder;
3+
use std::env;
4+
5+
fn main() {
6+
let args = env::args().collect::<Vec<_>>();
7+
8+
if args.len() < 2 {
9+
panic!("No program passed")
10+
}
11+
12+
let p = Program::from_source(&args[1]).expect("Failed to compile program");
13+
14+
let sql_builder = p
15+
.ast()
16+
.unwrap()
17+
.into_sql_builder()
18+
.expect("Failed to generate SQL builder");
19+
let sql = sql_builder.to_sql().expect("Failed to generate SQL");
20+
21+
println!("{}", sql);
22+
}

extensions/to_sql/src/ast.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
use rscel::AstNode;
2+
3+
use crate::error::ToSqlError;
4+
use crate::traits::{IntoSqlBuilder, SqlBuilder};
5+
6+
impl<T: IntoSqlBuilder> IntoSqlBuilder for AstNode<T> {
7+
fn into_sql_builder(&self) -> Result<Box<dyn SqlBuilder>, ToSqlError> {
8+
self.node().into_sql_builder()
9+
}
10+
}

extensions/to_sql/src/error.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#[derive(Debug)]
2+
pub enum ToSqlError {
3+
Unsupported(String),
4+
}
5+
6+
pub type ToSqlResult<T> = Result<T, ToSqlError>;
7+
8+
impl ToSqlError {
9+
pub fn unsupported(error_msg: &str) -> Self {
10+
ToSqlError::Unsupported(error_msg.to_owned())
11+
}
12+
}
13+
14+
impl std::fmt::Display for ToSqlError {
15+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
16+
match self {
17+
ToSqlError::Unsupported(msg) => write!(f, "Unsupported: {}", msg),
18+
}
19+
}
20+
}
21+
22+
impl std::error::Error for ToSqlError {}

0 commit comments

Comments
 (0)