Skip to content

Commit

Permalink
tmp
Browse files Browse the repository at this point in the history
  • Loading branch information
takaebato committed Feb 18, 2024
1 parent a14bc43 commit acb9af0
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 27 deletions.
4 changes: 3 additions & 1 deletion ext/sql_insight/src/ruby_api/crud_tables.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::ruby_api::{root, RbTableReference};
use magnus::{class, function, method, Error, Module, Object, RArray};
use magnus::{class, function, method, Error, IntoValueFromNative, Module, Object, RArray};
use std::cell::RefCell;

#[derive(Default, Clone)]
Expand Down Expand Up @@ -31,6 +31,8 @@ pub struct RbCrudTables {
inner: RefCell<RbCrudTablesInner>,
}

unsafe impl IntoValueFromNative for RbCrudTables {}

impl Default for RbCrudTables {
fn default() -> Self {
Self {
Expand Down
3 changes: 2 additions & 1 deletion ext/sql_insight/src/ruby_api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ use magnus::{Error, RModule, Ruby};

mod crud_tables;
mod ident;
mod pub_funcs;
mod table_reference;
mod tables;
mod pub_funcs;

pub use crud_tables::RbCrudTables;
pub use ident::RbIdent;
Expand All @@ -23,6 +23,7 @@ pub fn init() -> Result<(), Error> {
table_reference::init()?;
tables::init()?;
crud_tables::init()?;
pub_funcs::init()?;

Ok(())
}
34 changes: 18 additions & 16 deletions ext/sql_insight/src/ruby_api/pub_funcs.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
use magnus::{Error, exception, function, Object, RArray};
use sql_insight::NormalizerOptions;
use crate::ruby_api::root;
use crate::ruby_api::{RbCrudTables, RbTables};
use magnus::{exception, function, Error, Object, RArray};
use sql_insight::sqlparser::dialect;
use sql_insight::sqlparser::dialect::Dialect;
use crate::ruby_api::root;
use crate::ruby_api::{RbTables, RbCrudTables};
use sql_insight::NormalizerOptions;

fn get_dialect(dialect_name: &str) -> Result<Box<dyn Dialect>, sql_insight::error::Error> {
dialect::dialect_from_str(dialect_name)
.ok_or_else(|| sql_insight::error::Error::ArgumentError(format!("Dialect not found: {}", dialect_name)))
dialect::dialect_from_str(dialect_name).ok_or_else(|| {
sql_insight::error::Error::ArgumentError(format!("Dialect not found: {}", dialect_name))
})
}

fn rb_format(dialect_name: String, sql: String) -> Result<Vec<String>, Error> {
pub fn rb_format(dialect_name: String, sql: String) -> Result<Vec<String>, Error> {
match sql_insight::format(
get_dialect(dialect_name.as_str()).unwrap().as_ref(),
sql.as_str(),
Expand Down Expand Up @@ -40,17 +41,17 @@ fn rb_extract_tables(dialect_name: String, sql: String) -> Result<RArray, Error>
let mut results_of_tables = vec![];
for tables in result {
match tables {
Ok(tables) => results_of_tables.push(Ok(RbTables::from_tables(&tables).unwrap())),
Err(_error) => results_of_tables.push(Err(Error::new(exception::runtime_error(), error.to_string()))),
Ok(tables) => results_of_tables.push(RbTables::from_tables(&tables).unwrap()),
Err(_error) => (),
}
}
Ok(results_of_tables)
Ok(RArray::from_vec(results_of_tables))
}
Err(error) => Err(Error::new(exception::runtime_error(), error.to_string())),
}
}

fn rb_extract_crud_tables(dialect_name: String, sql: String) -> Result<Vec<Result<RbCrudTables, Error>>, Error> {
fn rb_extract_crud_tables(dialect_name: String, sql: String) -> Result<RArray, Error> {
match sql_insight::extract_crud_tables(
get_dialect(dialect_name.as_str()).unwrap().as_ref(),
sql.as_str(),
Expand All @@ -59,15 +60,16 @@ fn rb_extract_crud_tables(dialect_name: String, sql: String) -> Result<Vec<Resul
let mut results_of_crud_tables = vec![];
for crud_tables in result {
match crud_tables {
Ok(crud_tables) => results_of_crud_tables.push(Ok(RbCrudTables::from_crud_tables(&crud_tables).unwrap())),
Err(error) => results_of_crud_tables.push(Err(Error::new(exception::runtime_error(), error.to_string()))),
Ok(crud_tables) => results_of_crud_tables
.push(RbCrudTables::from_crud_tables(&crud_tables).unwrap()),
Err(error) => (),
}
}
Ok(RArray::from_vec(results_of_crud_tables))
}
Ok(results_of_crud_tables)
Err(error) => Err(Error::new(exception::runtime_error(), error.to_string())),
}
Err(error) => Err(Error::new(exception::runtime_error(), error.to_string())),
}
}

pub fn init() -> Result<(), Error> {
root().define_singleton_method("format", function!(rb_format, 2))?;
Expand Down
17 changes: 13 additions & 4 deletions ext/sql_insight/src/ruby_api/table_reference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,26 @@ impl RbTableReference {
name: name.clone(),
alias: alias.cloned(),
}
.into(),
.into(),
}
}

pub fn from_table_reference(table_reference: &TableReference) -> Result<Self, Error> {
Ok(Self {
inner: RefCell::new(RbTableReferenceInner {
catalog: table_reference.catalog.map(|ident| RbIdent::from_ident(&ident)),
schema: table_reference.schema.map(|ident| RbIdent::from_ident(&ident)),
catalog: table_reference
.catalog
.clone()
.map(|ident| RbIdent::from_ident(&ident)),
schema: table_reference
.schema
.clone()
.map(|ident| RbIdent::from_ident(&ident)),
name: RbIdent::from_ident(&table_reference.name),
alias: table_reference.alias.map(|ident| RbIdent::from_ident(&ident)),
alias: table_reference
.alias
.clone()
.map(|ident| RbIdent::from_ident(&ident)),
}),
})
}
Expand Down
12 changes: 7 additions & 5 deletions ext/sql_insight/src/ruby_api/tables.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::ruby_api::{root, RbTableReference};
use magnus::{class, function, method, Error, Module, Object};
use std::cell::RefCell;
use magnus::{class, function, method, Error, IntoValueFromNative, Module, Object};
use sql_insight::Tables;
use std::cell::RefCell;

#[derive(Default, Clone)]
struct RbTablesInner {
Expand All @@ -21,6 +21,8 @@ pub struct RbTables {
inner: RefCell<RbTablesInner>,
}

unsafe impl IntoValueFromNative for RbTables {}

impl Default for RbTables {
fn default() -> Self {
Self {
Expand All @@ -40,11 +42,11 @@ impl RbTables {
Ok(RbTables {
inner: RefCell::new(RbTablesInner {
tables: tables
.0.iter()
.0
.iter()
.map(|t| RbTableReference::from_table_reference(t).unwrap())
.collect(),
}
)
}),
})
}

Expand Down

0 comments on commit acb9af0

Please sign in to comment.