-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
132 additions
and
120 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
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
This file was deleted.
Oops, something went wrong.
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
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
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
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,18 +1,45 @@ | ||
use pythonize::pythonize; | ||
|
||
|
||
|
||
use pyo3::exceptions::PyValueError; | ||
use pyo3::prelude::*; | ||
|
||
use pyo3::wrap_pyfunction; | ||
|
||
mod sqloxide; | ||
use sqloxide::{restore_ast, parse_sql}; | ||
use sqlparser::dialect::dialect_from_str; | ||
use sqlparser::dialect::*; | ||
use sqlparser::parser::Parser; | ||
|
||
|
||
/// Function to parse SQL statements from a string. Returns a list with | ||
/// one item per query statement. | ||
/// | ||
/// Available `dialects`: https://github.com/sqlparser-rs/sqlparser-rs/blob/main/src/dialect/mod.rs#L189-L206 | ||
#[pyfunction] | ||
#[pyo3(text_signature = "(sql, dialect)")] | ||
fn parse_sql(py: Python, sql: String, dialect: String) -> PyResult<PyObject> { | ||
let chosen_dialect = dialect_from_str(dialect).unwrap_or_else(|| { | ||
println!("The dialect you chose was not recognized, falling back to 'generic'"); | ||
Box::new(GenericDialect {}) | ||
}); | ||
let parse_result = Parser::parse_sql(&*chosen_dialect, &sql); | ||
|
||
let output = match parse_result { | ||
Ok(statements) => pythonize(py, &statements).map_err(|e| { | ||
let msg = e.to_string(); | ||
PyValueError::new_err(format!("Python object serialization failed.\n\t{msg}")) | ||
})?, | ||
Err(e) => { | ||
let msg = e.to_string(); | ||
return Err(PyValueError::new_err(format!( | ||
"Query parsing failed.\n\t{msg}" | ||
))); | ||
} | ||
}; | ||
|
||
Ok(output.into()) | ||
} | ||
|
||
|
||
#[pymodule] | ||
fn compute(_py: Python, m: &PyModule) -> PyResult<()> { | ||
fn compute(_py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> { | ||
m.add_function(wrap_pyfunction!(parse_sql, m)?)?; | ||
m.add_function(wrap_pyfunction!(restore_ast, m)?)?; | ||
|
||
Ok(()) | ||
} |
This file was deleted.
Oops, something went wrong.
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