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,
QueryBuilder,
QueryFragment,
SQLError,
SQLiteQueryBuilder,
SQLQuery,
};
@ -629,7 +630,7 @@ impl QueryFragment for 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();
self.push_sql(&mut builder).map(|_| builder.finish())
}

View file

@ -4,7 +4,8 @@ version = "0.0.1"
workspace = ".."
[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"
[dependencies.rusqlite]

View file

@ -7,9 +7,9 @@
// 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
// specific language governing permissions and limitations under the License.
extern crate failure;
#[macro_use]
extern crate error_chain;
#[macro_use] extern crate failure_derive;
extern crate ordered_float;
extern crate rusqlite;
@ -29,25 +29,16 @@ use mentat_core::{
pub use rusqlite::types::Value;
error_chain! {
types {
Error, ErrorKind, ResultExt, Result;
}
#[derive(Debug, Fail)]
pub enum SQLError {
#[fail(display = "invalid parameter name: {}", _0)]
InvalidParameterName(String),
errors {
InvalidParameterName(name: 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)
}
}
#[fail(display = "parameter name could be generated: '{}'", _0)]
BindParamCouldBeGenerated(String)
}
pub type BuildQueryResult = Result<()>;
pub type BuildQueryResult = Result<(), SQLError>;
/// 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.
@ -213,12 +204,12 @@ impl QueryBuilder for SQLiteQueryBuilder {
// Do some validation first.
// This is not free, but it's probably worth it for now.
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()) &&
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("$");