Stub out mentat::q_once. (#289) r=nalexander

* Leave a pointer to issue 288.
* Re-export mentat_db::types::DB from mentat_db.
* Parse EDN strings in the query parser.
* Export 'public' API from mentat_query_parser's top level.
* Stub out mentat::q_once.
This commit is contained in:
Richard Newman 2017-02-13 10:30:02 -08:00 committed by GitHub
parent 4e81733eed
commit 2e303f4837
6 changed files with 92 additions and 7 deletions

View file

@ -35,8 +35,11 @@ mod schema;
mod types;
mod values;
pub use types::DB;
use edn::symbols;
// TODO: replace with sqlite3_limit. #288.
pub const SQLITE_MAX_VARIABLE_NUMBER: usize = 999;
pub fn to_namespaced_keyword(s: &str) -> Option<symbols::NamespacedKeyword> {

View file

@ -158,6 +158,17 @@ fn parse_find_edn_map(map: BTreeMap<edn::Value, edn::Value>) -> QueryParseResult
parse_find_map(m)
}
impl From<edn::parse::ParseError> for QueryParseError {
fn from(err: edn::parse::ParseError) -> QueryParseError {
QueryParseError::EdnParseError(err)
}
}
pub fn parse_find_string(string: &str) -> QueryParseResult {
let expr = edn::parse::value(string)?;
parse_find(expr)
}
pub fn parse_find(expr: edn::Value) -> QueryParseResult {
// No `match` because scoping and use of `expr` in error handling is nuts.
if let edn::Value::Map(m) = expr {

View file

@ -17,3 +17,16 @@ extern crate mentat_parser_utils;
mod util;
mod parse;
pub mod find;
pub use find::{
parse_find,
parse_find_string,
};
pub use parse::{
QueryParseResult,
QueryParseError,
FindParseError,
WhereParseError,
NotAVariableError,
};

View file

@ -54,12 +54,6 @@ pub enum QueryParseError {
WithParseError(NotAVariableError),
}
impl From<edn::parse::ParseError> for QueryParseError {
fn from(err: edn::parse::ParseError) -> QueryParseError {
QueryParseError::EdnParseError(err)
}
}
impl From<WhereParseError> for QueryParseError {
fn from(err: WhereParseError) -> QueryParseError {
QueryParseError::WhereParseError(err)

View file

@ -13,14 +13,18 @@ extern crate slog;
#[macro_use]
extern crate slog_scope;
extern crate rusqlite;
extern crate edn;
extern crate mentat_core;
extern crate mentat_db;
extern crate mentat_query;
extern crate mentat_query_parser;
extern crate rusqlite;
use rusqlite::Connection;
pub mod ident;
pub mod query;
pub fn get_name() -> String {
info!("Called into mentat library"; "fn" => "get_name");

60
src/query.rs Normal file
View file

@ -0,0 +1,60 @@
// Copyright 2016 Mozilla
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use
// this file except in compliance with the License. You may obtain a copy of the
// License at http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software distributed
// 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.
use std::collections::HashMap;
use mentat_core::{
TypedValue,
};
use mentat_db::DB;
use mentat_query_parser::{
parse_find_string,
QueryParseError,
};
// TODO
pub type SQLiteConnection = ();
pub enum QueryResults {
Scalar(Option<TypedValue>),
Tuple(Vec<TypedValue>),
Coll(Vec<TypedValue>),
Rel(Vec<Vec<TypedValue>>),
}
pub enum QueryExecutionError {
ParseError(QueryParseError),
InvalidArgumentName(String),
}
impl From<QueryParseError> for QueryExecutionError {
fn from(err: QueryParseError) -> QueryExecutionError {
QueryExecutionError::ParseError(err)
}
}
pub type QueryExecutionResult = Result<QueryResults, QueryExecutionError>;
/// 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
/// immediately, blocking the current thread.
/// Returns a structure that corresponds to the kind of input query, populated with `TypedValue`
/// instances.
#[allow(unused_variables)]
pub fn q_once(sqlite: SQLiteConnection,
db: DB,
query: &str,
inputs: Option<HashMap<String, TypedValue>>) -> QueryExecutionResult {
// TODO: validate inputs.
let parsed = parse_find_string(query)?;
Ok(QueryResults::Scalar(Some(TypedValue::Boolean(true))))
}