Wire in the start of querying and error_chain at top level. (#349) r=nalexander

This commit is contained in:
Richard Newman 2017-02-23 21:16:19 -08:00
parent 48312e1ff0
commit d7f323d15d
4 changed files with 56 additions and 10 deletions

View file

@ -16,6 +16,10 @@ extern crate mentat_sql;
mod translate; mod translate;
mod types; mod types;
pub use types::{
Projection,
};
pub use translate::{ pub use translate::{
cc_to_exists, cc_to_exists,
cc_to_select, cc_to_select,

View file

@ -15,6 +15,7 @@ use rusqlite;
use edn; use edn;
use mentat_db; use mentat_db;
use mentat_query_parser; use mentat_query_parser;
use mentat_sql;
use mentat_tx_parser; use mentat_tx_parser;
error_chain! { error_chain! {
@ -30,6 +31,14 @@ error_chain! {
links { links {
DbError(mentat_db::Error, mentat_db::ErrorKind); DbError(mentat_db::Error, mentat_db::ErrorKind);
QueryParseError(mentat_query_parser::Error, mentat_query_parser::ErrorKind); QueryParseError(mentat_query_parser::Error, mentat_query_parser::ErrorKind);
SqlError(mentat_sql::Error, mentat_sql::ErrorKind);
TxParseError(mentat_tx_parser::Error, mentat_tx_parser::ErrorKind); TxParseError(mentat_tx_parser::Error, mentat_tx_parser::ErrorKind);
} }
errors {
InvalidArgumentName(name: String) {
description("invalid argument name")
display("invalid argument name: '{}'", name)
}
}
} }

View file

@ -10,8 +10,10 @@
#[macro_use] #[macro_use]
extern crate error_chain; extern crate error_chain;
#[macro_use] #[macro_use]
extern crate slog; extern crate slog;
#[macro_use] #[macro_use]
extern crate slog_scope; extern crate slog_scope;
@ -21,8 +23,10 @@ extern crate edn;
extern crate mentat_core; extern crate mentat_core;
extern crate mentat_db; extern crate mentat_db;
extern crate mentat_query; extern crate mentat_query;
extern crate mentat_query_parser;
extern crate mentat_query_algebrizer; extern crate mentat_query_algebrizer;
extern crate mentat_query_parser;
extern crate mentat_query_translator;
extern crate mentat_sql;
extern crate mentat_tx_parser; extern crate mentat_tx_parser;
use rusqlite::Connection; use rusqlite::Connection;

View file

@ -11,21 +11,28 @@
use std::collections::HashMap; use std::collections::HashMap;
use mentat_core::{ use mentat_core::{
Schema,
TypedValue, TypedValue,
}; };
use mentat_db::DB; use mentat_query_algebrizer::algebrize;
use mentat_query_parser::{ use mentat_query_parser::{
parse_find_string, parse_find_string,
}; };
use errors::{ use mentat_sql::{
Result, SQLQuery,
}; };
// TODO use mentat_query_translator::{
pub type SQLiteConnection = (); cc_to_select,
Projection,
};
use errors::Result;
use rusqlite;
pub enum QueryResults { pub enum QueryResults {
Scalar(Option<TypedValue>), Scalar(Option<TypedValue>),
@ -34,17 +41,39 @@ pub enum QueryResults {
Rel(Vec<Vec<TypedValue>>), Rel(Vec<Vec<TypedValue>>),
} }
pub type QueryExecutionResult = Result<QueryResults>;
/// Take an EDN query string, a reference to a open SQLite connection, a Mentat DB, and an optional /// Take an EDN query string, a reference to a open SQLite connection, a Mentat DB, and an optional
/// collection of input bindings (which should be keyed by `"?varname"`), and execute the query /// collection of input bindings (which should be keyed by `"?varname"`), and execute the query
/// immediately, blocking the current thread. /// immediately, blocking the current thread.
/// Returns a structure that corresponds to the kind of input query, populated with `TypedValue` /// Returns a structure that corresponds to the kind of input query, populated with `TypedValue`
/// instances. /// instances.
/// The caller is responsible for ensuring that the SQLite connection is in a transaction if
/// isolation is required.
#[allow(unused_variables)] #[allow(unused_variables)]
pub fn q_once(sqlite: SQLiteConnection, pub fn q_once<'sqlite, 'schema, 'query>
db: DB, (sqlite: &'sqlite rusqlite::Connection,
query: &str, schema: &'schema Schema,
inputs: Option<HashMap<String, TypedValue>>) -> Result<QueryResults> { query: &'query str,
inputs: Option<HashMap<String, TypedValue>>) -> QueryExecutionResult {
// TODO: validate inputs. // TODO: validate inputs.
let parsed = parse_find_string(query)?; let parsed = parse_find_string(query)?;
let algebrized = algebrize(schema, parsed);
let projection = Projection::Star;
let select = cc_to_select(projection, algebrized.cc);
let SQLQuery { sql, args } = select.to_sql_query()?;
/*
let mut statement = sqlite.prepare(sql.as_str())?;
let mut rows = if args.is_empty() {
statement.query(&[])?
} else {
statement.query_named(args.map(|(k, v)| (k.as_str(), &v)))?
};
*/
Ok(QueryResults::Scalar(Some(TypedValue::Boolean(true)))) Ok(QueryResults::Scalar(Some(TypedValue::Boolean(true))))
} }