Convert sql/ and query-sql/ to failure.

sql/query-sql
This commit is contained in:
Grisha Kruglov 2018-05-31 22:30:25 -04:00 committed by Nick Alexander
parent fb7d2357de
commit ce3ce1ccbf
3 changed files with 15 additions and 22 deletions

View file

@ -42,6 +42,7 @@ use mentat_sql::{
BuildQueryResult, BuildQueryResult,
QueryBuilder, QueryBuilder,
QueryFragment, QueryFragment,
SQLError,
SQLiteQueryBuilder, SQLiteQueryBuilder,
SQLQuery, SQLQuery,
}; };
@ -629,7 +630,7 @@ impl QueryFragment for SelectQuery {
} }
impl SelectQuery { impl SelectQuery {
pub fn to_sql_query(&self) -> mentat_sql::Result<SQLQuery> { pub fn to_sql_query(&self) -> Result<SQLQuery, SQLError> {
let mut builder = SQLiteQueryBuilder::new(); let mut builder = SQLiteQueryBuilder::new();
self.push_sql(&mut builder).map(|_| builder.finish()) self.push_sql(&mut builder).map(|_| builder.finish())
} }

View file

@ -4,7 +4,8 @@ version = "0.0.1"
workspace = ".." workspace = ".."
[dependencies] [dependencies]
error-chain = { git = "https://github.com/rnewman/error-chain", branch = "rnewman/sync" } failure = "0.1.1"
failure_derive = "0.1.1"
ordered-float = "0.5" ordered-float = "0.5"
[dependencies.rusqlite] [dependencies.rusqlite]

View file

@ -7,9 +7,9 @@
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
// CONDITIONS OF ANY KIND, either express or implied. See the License for the // CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License. // specific language governing permissions and limitations under the License.
extern crate failure;
#[macro_use] #[macro_use] extern crate failure_derive;
extern crate error_chain;
extern crate ordered_float; extern crate ordered_float;
extern crate rusqlite; extern crate rusqlite;
@ -29,25 +29,16 @@ use mentat_core::{
pub use rusqlite::types::Value; pub use rusqlite::types::Value;
error_chain! { #[derive(Debug, Fail)]
types { pub enum SQLError {
Error, ErrorKind, ResultExt, Result; #[fail(display = "invalid parameter name: {}", _0)]
} InvalidParameterName(String),
errors { #[fail(display = "parameter name could be generated: '{}'", _0)]
InvalidParameterName(name: String) { BindParamCouldBeGenerated(String)
description("invalid parameter name")
display("invalid parameter name: '{}'", name)
}
BindParamCouldBeGenerated(name: String) {
description("parameter name could be generated")
display("parameter name could be generated: '{}'", name)
}
}
} }
pub type BuildQueryResult = Result<()>; pub type BuildQueryResult = Result<(), SQLError>;
/// We want to accumulate values that will later be substituted into a SQL statement execution. /// We want to accumulate values that will later be substituted into a SQL statement execution.
/// This struct encapsulates the generated string and the _initial_ argument list. /// This struct encapsulates the generated string and the _initial_ argument list.
@ -213,12 +204,12 @@ impl QueryBuilder for SQLiteQueryBuilder {
// Do some validation first. // Do some validation first.
// This is not free, but it's probably worth it for now. // This is not free, but it's probably worth it for now.
if !name.chars().all(|c| char::is_alphanumeric(c) || c == '_') { if !name.chars().all(|c| char::is_alphanumeric(c) || c == '_') {
bail!(ErrorKind::InvalidParameterName(name.to_string())); return Err(SQLError::InvalidParameterName(name.to_string()))
} }
if name.starts_with(self.arg_prefix.as_str()) && if name.starts_with(self.arg_prefix.as_str()) &&
name.chars().skip(self.arg_prefix.len()).all(char::is_numeric) { name.chars().skip(self.arg_prefix.len()).all(char::is_numeric) {
bail!(ErrorKind::BindParamCouldBeGenerated(name.to_string())); return Err(SQLError::BindParamCouldBeGenerated(name.to_string()))
} }
self.push_sql("$"); self.push_sql("$");