2017-02-13 18:30:02 +00:00
|
|
|
// 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.
|
|
|
|
|
2017-03-06 22:40:10 +00:00
|
|
|
use rusqlite;
|
|
|
|
use rusqlite::types::ToSql;
|
|
|
|
|
2018-01-20 00:44:39 +00:00
|
|
|
use std::rc::Rc;
|
|
|
|
|
2020-01-14 15:46:21 +00:00
|
|
|
use core_traits::{Binding, Entid, KnownEntid, TypedValue};
|
2018-08-08 17:35:06 +00:00
|
|
|
|
2020-01-14 15:46:21 +00:00
|
|
|
use mentat_core::{HasSchema, Schema};
|
2017-02-13 18:30:02 +00:00
|
|
|
|
2017-04-17 20:14:30 +00:00
|
|
|
use mentat_query_algebrizer::{
|
2020-01-14 15:46:21 +00:00
|
|
|
algebrize_with_inputs, parse_find_string, AlgebraicQuery, EmptyBecause, FindQuery,
|
2017-04-17 20:14:30 +00:00
|
|
|
};
|
|
|
|
|
2020-01-14 15:46:21 +00:00
|
|
|
pub use mentat_query_algebrizer::QueryInputs;
|
2017-02-13 18:30:02 +00:00
|
|
|
|
2020-01-14 15:46:21 +00:00
|
|
|
pub use edn::query::{Keyword, PlainSymbol, Variable};
|
2017-03-06 22:40:10 +00:00
|
|
|
|
2018-08-08 18:23:07 +00:00
|
|
|
use edn::query::{
|
2020-01-14 15:46:21 +00:00
|
|
|
Element, FindSpec, Pattern, PatternNonValuePlace, PatternValuePlace, WhereClause,
|
2017-12-11 19:08:10 +00:00
|
|
|
};
|
|
|
|
|
2020-01-14 15:46:21 +00:00
|
|
|
use mentat_query_projector::{ConstantProjector, Projector};
|
2018-01-24 03:02:25 +00:00
|
|
|
|
2020-01-14 15:46:21 +00:00
|
|
|
use mentat_query_projector::translate::{query_to_select, ProjectedSelect};
|
2017-02-24 05:16:19 +00:00
|
|
|
|
2020-01-14 15:46:21 +00:00
|
|
|
use mentat_sql::SQLQuery;
|
2018-08-08 20:03:27 +00:00
|
|
|
|
2020-01-14 15:46:21 +00:00
|
|
|
pub use mentat_query_algebrizer::Known;
|
2018-02-14 00:51:21 +00:00
|
|
|
|
2017-03-06 22:40:10 +00:00
|
|
|
pub use mentat_query_projector::{
|
2020-01-14 15:46:21 +00:00
|
|
|
QueryOutput, // Includes the columns/find spec.
|
|
|
|
QueryResults, // The results themselves.
|
2018-04-24 22:04:00 +00:00
|
|
|
RelResult,
|
2017-03-06 22:40:10 +00:00
|
|
|
};
|
2017-02-13 18:30:02 +00:00
|
|
|
|
2020-01-14 15:46:21 +00:00
|
|
|
use public_traits::errors::{MentatError, Result};
|
2017-02-13 18:30:02 +00:00
|
|
|
|
2018-01-30 22:11:41 +00:00
|
|
|
pub type QueryExecutionResult = Result<QueryOutput>;
|
2018-01-24 03:02:25 +00:00
|
|
|
pub type PreparedResult<'sqlite> = Result<PreparedQuery<'sqlite>>;
|
|
|
|
|
|
|
|
pub enum PreparedQuery<'sqlite> {
|
|
|
|
Empty {
|
|
|
|
find_spec: Rc<FindSpec>,
|
|
|
|
},
|
2018-03-06 17:01:20 +00:00
|
|
|
Constant {
|
|
|
|
select: ConstantProjector,
|
|
|
|
},
|
2018-01-24 03:02:25 +00:00
|
|
|
Bound {
|
|
|
|
statement: rusqlite::Statement<'sqlite>,
|
2018-05-04 19:56:00 +00:00
|
|
|
schema: Schema,
|
|
|
|
connection: &'sqlite rusqlite::Connection,
|
2018-01-24 03:02:25 +00:00
|
|
|
args: Vec<(String, Rc<rusqlite::types::Value>)>,
|
2020-01-14 15:46:21 +00:00
|
|
|
projector: Box<dyn Projector>,
|
2018-01-24 03:02:25 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'sqlite> PreparedQuery<'sqlite> {
|
2020-01-14 15:46:21 +00:00
|
|
|
pub fn run<T>(&mut self, _inputs: T) -> QueryExecutionResult
|
|
|
|
where
|
|
|
|
T: Into<Option<QueryInputs>>,
|
|
|
|
{
|
2018-01-24 03:02:25 +00:00
|
|
|
match self {
|
2020-08-06 03:03:58 +00:00
|
|
|
PreparedQuery::Empty { ref find_spec } => Ok(QueryOutput::empty(find_spec)),
|
|
|
|
PreparedQuery::Constant { ref select } => {
|
2018-03-06 17:01:20 +00:00
|
|
|
select.project_without_rows().map_err(|e| e.into())
|
2020-01-14 15:46:21 +00:00
|
|
|
}
|
2020-08-06 03:03:58 +00:00
|
|
|
PreparedQuery::Bound {
|
2020-01-14 15:46:21 +00:00
|
|
|
ref mut statement,
|
|
|
|
ref schema,
|
|
|
|
ref connection,
|
|
|
|
ref args,
|
|
|
|
ref projector,
|
|
|
|
} => {
|
2018-01-24 03:02:25 +00:00
|
|
|
let rows = run_statement(statement, args)?;
|
2020-01-14 15:46:21 +00:00
|
|
|
projector
|
|
|
|
.project(schema, connection, rows)
|
|
|
|
.map_err(|e| e.into())
|
2018-01-24 03:02:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-02-24 05:16:19 +00:00
|
|
|
|
2017-12-06 22:34:48 +00:00
|
|
|
pub trait IntoResult {
|
2018-04-24 22:08:38 +00:00
|
|
|
fn into_scalar_result(self) -> Result<Option<Binding>>;
|
|
|
|
fn into_coll_result(self) -> Result<Vec<Binding>>;
|
|
|
|
fn into_tuple_result(self) -> Result<Option<Vec<Binding>>>;
|
|
|
|
fn into_rel_result(self) -> Result<RelResult<Binding>>;
|
2017-12-06 22:34:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl IntoResult for QueryExecutionResult {
|
2018-04-24 22:08:38 +00:00
|
|
|
fn into_scalar_result(self) -> Result<Option<Binding>> {
|
2017-12-06 22:34:48 +00:00
|
|
|
self?.into_scalar().map_err(|e| e.into())
|
|
|
|
}
|
|
|
|
|
2018-04-24 22:08:38 +00:00
|
|
|
fn into_coll_result(self) -> Result<Vec<Binding>> {
|
2017-12-06 22:34:48 +00:00
|
|
|
self?.into_coll().map_err(|e| e.into())
|
|
|
|
}
|
|
|
|
|
2018-04-24 22:08:38 +00:00
|
|
|
fn into_tuple_result(self) -> Result<Option<Vec<Binding>>> {
|
2017-12-06 22:34:48 +00:00
|
|
|
self?.into_tuple().map_err(|e| e.into())
|
|
|
|
}
|
|
|
|
|
2018-04-24 22:08:38 +00:00
|
|
|
fn into_rel_result(self) -> Result<RelResult<Binding>> {
|
2017-12-06 22:34:48 +00:00
|
|
|
self?.into_rel().map_err(|e| e.into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-20 00:44:39 +00:00
|
|
|
/// A struct describing information about how Mentat would execute a query.
|
|
|
|
pub enum QueryExplanation {
|
|
|
|
/// A query known in advance to be empty, and why we believe that.
|
|
|
|
KnownEmpty(EmptyBecause),
|
2018-03-06 17:01:20 +00:00
|
|
|
|
|
|
|
/// A query known in advance to return a constant value.
|
|
|
|
KnownConstant,
|
|
|
|
|
2018-01-20 00:44:39 +00:00
|
|
|
/// A query that takes actual work to execute.
|
|
|
|
ExecutionPlan {
|
|
|
|
/// The translated query and any bindings.
|
|
|
|
query: SQLQuery,
|
|
|
|
/// The output of SQLite's `EXPLAIN QUERY PLAN`.
|
|
|
|
steps: Vec<QueryPlanStep>,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A single row in the output of SQLite's `EXPLAIN QUERY PLAN`.
|
|
|
|
/// See https://www.sqlite.org/eqp.html for an explanation of each field.
|
|
|
|
pub struct QueryPlanStep {
|
|
|
|
pub select_id: i32,
|
|
|
|
pub order: i32,
|
|
|
|
pub from: i32,
|
|
|
|
pub detail: String,
|
|
|
|
}
|
|
|
|
|
2020-01-14 15:46:21 +00:00
|
|
|
fn algebrize_query<T>(known: Known, query: FindQuery, inputs: T) -> Result<AlgebraicQuery>
|
|
|
|
where
|
|
|
|
T: Into<Option<QueryInputs>>,
|
2018-01-20 00:44:39 +00:00
|
|
|
{
|
2020-08-06 03:03:58 +00:00
|
|
|
let algebrized = algebrize_with_inputs(known, query, 0, inputs.into().unwrap_or_default())?;
|
2018-01-20 00:44:39 +00:00
|
|
|
let unbound = algebrized.unbound_variables();
|
|
|
|
// Because we are running once, we can check that all of our `:in` variables are bound at this point.
|
|
|
|
// If they aren't, the user has made an error -- perhaps writing the wrong variable in `:in`, or
|
|
|
|
// not binding in the `QueryInput`.
|
|
|
|
if !unbound.is_empty() {
|
2020-01-14 15:46:21 +00:00
|
|
|
bail!(MentatError::UnboundVariables(
|
|
|
|
unbound.into_iter().map(|v| v.to_string()).collect()
|
|
|
|
));
|
2018-01-20 00:44:39 +00:00
|
|
|
}
|
|
|
|
Ok(algebrized)
|
|
|
|
}
|
|
|
|
|
2020-01-14 15:46:21 +00:00
|
|
|
fn fetch_values<'sqlite>(
|
|
|
|
sqlite: &'sqlite rusqlite::Connection,
|
|
|
|
known: Known,
|
|
|
|
entity: Entid,
|
|
|
|
attribute: Entid,
|
|
|
|
only_one: bool,
|
|
|
|
) -> QueryExecutionResult {
|
2017-12-11 19:08:10 +00:00
|
|
|
let v = Variable::from_valid_name("?v");
|
|
|
|
|
|
|
|
// This should never fail.
|
|
|
|
// TODO: it should be possible to algebrize with variable entity and attribute,
|
|
|
|
// particularly with known type, allowing the use of prepared statements.
|
2020-01-14 15:46:21 +00:00
|
|
|
let pattern = Pattern::simple(
|
|
|
|
PatternNonValuePlace::Entid(entity),
|
|
|
|
PatternNonValuePlace::Entid(attribute),
|
|
|
|
PatternValuePlace::Variable(v.clone()),
|
|
|
|
)
|
|
|
|
.unwrap();
|
2017-12-11 19:08:10 +00:00
|
|
|
|
|
|
|
let element = Element::Variable(v);
|
2020-01-14 15:46:21 +00:00
|
|
|
let spec = if only_one {
|
|
|
|
FindSpec::FindScalar(element)
|
|
|
|
} else {
|
|
|
|
FindSpec::FindColl(element)
|
|
|
|
};
|
|
|
|
let query = FindQuery::simple(spec, vec![WhereClause::Pattern(pattern)]);
|
2017-12-11 19:08:10 +00:00
|
|
|
|
2018-02-14 00:51:21 +00:00
|
|
|
let algebrized = algebrize_query(known, query, None)?;
|
2017-12-11 19:08:10 +00:00
|
|
|
|
2018-05-04 19:56:00 +00:00
|
|
|
run_algebrized_query(known, sqlite, algebrized)
|
2017-12-11 19:08:10 +00:00
|
|
|
}
|
|
|
|
|
2018-05-11 16:52:17 +00:00
|
|
|
fn lookup_attribute(schema: &Schema, attribute: &Keyword) -> Result<KnownEntid> {
|
2020-01-14 15:46:21 +00:00
|
|
|
schema
|
|
|
|
.get_entid(attribute)
|
2020-08-06 03:03:58 +00:00
|
|
|
.ok_or_else(|| MentatError::UnknownAttribute(attribute.name().into()))
|
2017-12-11 19:08:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Return a single value for the provided entity and attribute.
|
|
|
|
/// If the attribute is multi-valued, an arbitrary value is returned.
|
|
|
|
/// If no value is present for that entity, `None` is returned.
|
|
|
|
/// If `attribute` isn't an attribute, `None` is returned.
|
2020-08-06 03:03:58 +00:00
|
|
|
#[allow(clippy::extra_unused_lifetimes)]
|
2020-01-14 15:46:21 +00:00
|
|
|
pub fn lookup_value<'sqlite, 'schema, 'cache, E, A>(
|
|
|
|
sqlite: &'sqlite rusqlite::Connection,
|
|
|
|
known: Known,
|
|
|
|
entity: E,
|
|
|
|
attribute: A,
|
|
|
|
) -> Result<Option<TypedValue>>
|
|
|
|
where
|
|
|
|
E: Into<Entid>,
|
|
|
|
A: Into<Entid>,
|
|
|
|
{
|
2018-02-07 18:56:12 +00:00
|
|
|
let entid = entity.into();
|
|
|
|
let attrid = attribute.into();
|
2018-02-14 00:51:21 +00:00
|
|
|
|
|
|
|
if known.is_attribute_cached_forward(attrid) {
|
2020-01-14 15:46:21 +00:00
|
|
|
Ok(known
|
|
|
|
.get_value_for_entid(known.schema, attrid, entid)
|
|
|
|
.cloned())
|
2018-02-14 00:51:21 +00:00
|
|
|
} else {
|
2018-04-24 22:08:38 +00:00
|
|
|
fetch_values(sqlite, known, entid, attrid, true)
|
|
|
|
.into_scalar_result()
|
|
|
|
// Safe to unwrap: we never retrieve structure.
|
2018-06-18 18:05:35 +00:00
|
|
|
.map(|r| r.map(|v| v.into_scalar().unwrap()))
|
2018-02-07 18:56:12 +00:00
|
|
|
}
|
2017-12-11 19:08:10 +00:00
|
|
|
}
|
2017-03-07 04:18:38 +00:00
|
|
|
|
2020-01-14 15:46:21 +00:00
|
|
|
pub fn lookup_values<'sqlite, E, A>(
|
|
|
|
sqlite: &'sqlite rusqlite::Connection,
|
|
|
|
known: Known,
|
|
|
|
entity: E,
|
|
|
|
attribute: A,
|
|
|
|
) -> Result<Vec<TypedValue>>
|
|
|
|
where
|
|
|
|
E: Into<Entid>,
|
|
|
|
A: Into<Entid>,
|
|
|
|
{
|
2018-02-07 18:56:12 +00:00
|
|
|
let entid = entity.into();
|
|
|
|
let attrid = attribute.into();
|
2018-02-14 00:51:21 +00:00
|
|
|
|
|
|
|
if known.is_attribute_cached_forward(attrid) {
|
2020-01-14 15:46:21 +00:00
|
|
|
Ok(known
|
|
|
|
.get_values_for_entid(known.schema, attrid, entid)
|
|
|
|
.cloned()
|
2020-08-06 03:03:58 +00:00
|
|
|
.unwrap_or_else(Vec::new))
|
2018-02-14 00:51:21 +00:00
|
|
|
} else {
|
2018-04-24 22:08:38 +00:00
|
|
|
fetch_values(sqlite, known, entid, attrid, false)
|
|
|
|
.into_coll_result()
|
|
|
|
// Safe to unwrap: we never retrieve structure.
|
2018-06-18 18:05:35 +00:00
|
|
|
.map(|v| v.into_iter().map(|x| x.into_scalar().unwrap()).collect())
|
2018-02-07 18:56:12 +00:00
|
|
|
}
|
2017-12-11 19:08:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Return a single value for the provided entity and attribute.
|
|
|
|
/// If the attribute is multi-valued, an arbitrary value is returned.
|
|
|
|
/// If no value is present for that entity, `None` is returned.
|
|
|
|
/// If `attribute` doesn't name an attribute, an error is returned.
|
2020-01-14 15:46:21 +00:00
|
|
|
pub fn lookup_value_for_attribute<'sqlite, 'attribute, E>(
|
|
|
|
sqlite: &'sqlite rusqlite::Connection,
|
|
|
|
known: Known,
|
|
|
|
entity: E,
|
|
|
|
attribute: &'attribute Keyword,
|
|
|
|
) -> Result<Option<TypedValue>>
|
|
|
|
where
|
|
|
|
E: Into<Entid>,
|
|
|
|
{
|
2018-02-14 00:51:21 +00:00
|
|
|
let attribute = lookup_attribute(known.schema, attribute)?;
|
|
|
|
lookup_value(sqlite, known, entity.into(), attribute)
|
2017-12-11 19:08:10 +00:00
|
|
|
}
|
|
|
|
|
2020-01-14 15:46:21 +00:00
|
|
|
pub fn lookup_values_for_attribute<'sqlite, 'attribute, E>(
|
|
|
|
sqlite: &'sqlite rusqlite::Connection,
|
|
|
|
known: Known,
|
|
|
|
entity: E,
|
|
|
|
attribute: &'attribute Keyword,
|
|
|
|
) -> Result<Vec<TypedValue>>
|
|
|
|
where
|
|
|
|
E: Into<Entid>,
|
|
|
|
{
|
2018-02-14 00:51:21 +00:00
|
|
|
let attribute = lookup_attribute(known.schema, attribute)?;
|
|
|
|
lookup_values(sqlite, known, entity.into(), attribute)
|
2017-12-11 19:08:10 +00:00
|
|
|
}
|
|
|
|
|
2020-01-14 15:46:21 +00:00
|
|
|
fn run_statement<'sqlite, 'stmt, 'bound>(
|
|
|
|
statement: &'stmt mut rusqlite::Statement<'sqlite>,
|
|
|
|
bindings: &'bound [(String, Rc<rusqlite::types::Value>)],
|
|
|
|
) -> Result<rusqlite::Rows<'stmt>> {
|
2018-01-20 00:44:39 +00:00
|
|
|
let rows = if bindings.is_empty() {
|
2019-07-22 12:54:51 +00:00
|
|
|
statement.query(rusqlite::params![])?
|
2018-01-20 00:44:39 +00:00
|
|
|
} else {
|
2020-01-14 15:46:21 +00:00
|
|
|
let refs: Vec<(&str, &dyn ToSql)> = bindings
|
|
|
|
.iter()
|
|
|
|
.map(|&(ref k, ref v)| (k.as_str(), v.as_ref() as &dyn ToSql))
|
|
|
|
.collect();
|
2018-01-20 00:44:39 +00:00
|
|
|
statement.query_named(&refs)?
|
|
|
|
};
|
|
|
|
Ok(rows)
|
|
|
|
}
|
|
|
|
|
2020-01-14 15:46:21 +00:00
|
|
|
fn run_sql_query<'sqlite, 'sql, 'bound, T, F>(
|
|
|
|
sqlite: &'sqlite rusqlite::Connection,
|
|
|
|
sql: &'sql str,
|
|
|
|
bindings: &'bound [(String, Rc<rusqlite::types::Value>)],
|
|
|
|
mut mapper: F,
|
|
|
|
) -> Result<Vec<T>>
|
|
|
|
where
|
|
|
|
F: FnMut(&rusqlite::Row) -> T,
|
2018-01-20 00:44:39 +00:00
|
|
|
{
|
|
|
|
let mut statement = sqlite.prepare(sql)?;
|
|
|
|
let mut rows = run_statement(&mut statement, &bindings)?;
|
|
|
|
let mut result = vec![];
|
2019-07-22 12:54:51 +00:00
|
|
|
while let Some(row_or_error) = rows.next().unwrap() {
|
|
|
|
result.push(mapper(&row_or_error));
|
2018-01-20 00:44:39 +00:00
|
|
|
}
|
|
|
|
Ok(result)
|
|
|
|
}
|
|
|
|
|
2020-01-14 15:46:21 +00:00
|
|
|
fn algebrize_query_str<'query, T>(
|
|
|
|
known: Known,
|
|
|
|
query: &'query str,
|
|
|
|
inputs: T,
|
|
|
|
) -> Result<AlgebraicQuery>
|
|
|
|
where
|
|
|
|
T: Into<Option<QueryInputs>>,
|
|
|
|
{
|
2018-01-20 00:44:39 +00:00
|
|
|
let parsed = parse_find_string(query)?;
|
2018-02-14 00:51:21 +00:00
|
|
|
algebrize_query(known, parsed, inputs)
|
2018-01-20 00:44:39 +00:00
|
|
|
}
|
|
|
|
|
2020-01-14 15:46:21 +00:00
|
|
|
fn run_algebrized_query<'sqlite>(
|
|
|
|
known: Known,
|
|
|
|
sqlite: &'sqlite rusqlite::Connection,
|
|
|
|
algebrized: AlgebraicQuery,
|
|
|
|
) -> QueryExecutionResult {
|
|
|
|
assert!(
|
|
|
|
algebrized.unbound_variables().is_empty(),
|
|
|
|
"Unbound variables should be checked by now"
|
|
|
|
);
|
2017-03-07 04:18:38 +00:00
|
|
|
if algebrized.is_known_empty() {
|
|
|
|
// We don't need to do any SQL work at all.
|
2018-01-30 22:11:41 +00:00
|
|
|
return Ok(QueryOutput::empty(&algebrized.find_spec));
|
2017-03-07 04:18:38 +00:00
|
|
|
}
|
|
|
|
|
2018-05-04 19:56:00 +00:00
|
|
|
let select = query_to_select(known.schema, algebrized)?;
|
2018-03-06 17:01:20 +00:00
|
|
|
match select {
|
2018-05-04 19:56:00 +00:00
|
|
|
ProjectedSelect::Constant(constant) => {
|
2020-01-14 15:46:21 +00:00
|
|
|
constant.project_without_rows().map_err(|e| e.into())
|
|
|
|
}
|
2018-03-06 17:01:20 +00:00
|
|
|
ProjectedSelect::Query { query, projector } => {
|
|
|
|
let SQLQuery { sql, args } = query.to_sql_query()?;
|
2017-02-24 05:16:19 +00:00
|
|
|
|
2018-03-06 17:01:20 +00:00
|
|
|
let mut statement = sqlite.prepare(sql.as_str())?;
|
|
|
|
let rows = run_statement(&mut statement, &args)?;
|
2017-02-24 05:16:19 +00:00
|
|
|
|
2020-01-14 15:46:21 +00:00
|
|
|
projector
|
|
|
|
.project(known.schema, sqlite, rows)
|
|
|
|
.map_err(|e| e.into())
|
|
|
|
}
|
2018-03-06 17:01:20 +00:00
|
|
|
}
|
2017-02-13 18:30:02 +00:00
|
|
|
}
|
2017-12-11 19:08:10 +00:00
|
|
|
|
|
|
|
/// Take an EDN query string, a reference to an open SQLite connection, a Mentat schema, 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.
|
|
|
|
/// The caller is responsible for ensuring that the SQLite connection has an open transaction if
|
|
|
|
/// isolation is required.
|
2020-01-14 15:46:21 +00:00
|
|
|
pub fn q_once<'sqlite, 'query, T>(
|
|
|
|
sqlite: &'sqlite rusqlite::Connection,
|
|
|
|
known: Known,
|
|
|
|
query: &'query str,
|
|
|
|
inputs: T,
|
|
|
|
) -> QueryExecutionResult
|
|
|
|
where
|
|
|
|
T: Into<Option<QueryInputs>>,
|
2018-02-14 00:51:21 +00:00
|
|
|
{
|
|
|
|
let algebrized = algebrize_query_str(known, query, inputs)?;
|
2018-05-04 19:56:00 +00:00
|
|
|
run_algebrized_query(known, sqlite, algebrized)
|
2018-02-14 00:51:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Just like `q_once`, but doesn't use any cached values.
|
2020-08-06 03:03:58 +00:00
|
|
|
pub fn q_uncached<T>(
|
|
|
|
sqlite: &rusqlite::Connection,
|
|
|
|
schema: &Schema,
|
|
|
|
query: &str,
|
2020-01-14 15:46:21 +00:00
|
|
|
inputs: T,
|
|
|
|
) -> QueryExecutionResult
|
|
|
|
where
|
|
|
|
T: Into<Option<QueryInputs>>,
|
2017-12-11 19:08:10 +00:00
|
|
|
{
|
2018-02-14 00:51:21 +00:00
|
|
|
let known = Known::for_schema(schema);
|
|
|
|
let algebrized = algebrize_query_str(known, query, inputs)?;
|
2017-12-11 19:08:10 +00:00
|
|
|
|
2018-05-04 19:56:00 +00:00
|
|
|
run_algebrized_query(known, sqlite, algebrized)
|
2017-12-11 19:08:10 +00:00
|
|
|
}
|
2018-01-20 00:44:39 +00:00
|
|
|
|
2020-01-14 15:46:21 +00:00
|
|
|
pub fn q_prepare<'sqlite, 'schema, 'cache, 'query, T>(
|
|
|
|
sqlite: &'sqlite rusqlite::Connection,
|
|
|
|
known: Known<'schema, 'cache>,
|
|
|
|
query: &'query str,
|
|
|
|
inputs: T,
|
|
|
|
) -> PreparedResult<'sqlite>
|
|
|
|
where
|
|
|
|
T: Into<Option<QueryInputs>>,
|
2018-01-24 03:02:25 +00:00
|
|
|
{
|
2018-02-14 00:51:21 +00:00
|
|
|
let algebrized = algebrize_query_str(known, query, inputs)?;
|
2018-01-24 03:02:25 +00:00
|
|
|
|
|
|
|
let unbound = algebrized.unbound_variables();
|
|
|
|
if !unbound.is_empty() {
|
|
|
|
// TODO: Allow binding variables at execution time, not just
|
|
|
|
// preparation time.
|
2020-01-14 15:46:21 +00:00
|
|
|
bail!(MentatError::UnboundVariables(
|
|
|
|
unbound.into_iter().map(|v| v.to_string()).collect()
|
|
|
|
));
|
2018-01-24 03:02:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if algebrized.is_known_empty() {
|
|
|
|
// We don't need to do any SQL work at all.
|
|
|
|
return Ok(PreparedQuery::Empty {
|
|
|
|
find_spec: algebrized.find_spec,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-05-04 19:56:00 +00:00
|
|
|
let select = query_to_select(known.schema, algebrized)?;
|
2018-03-06 17:01:20 +00:00
|
|
|
match select {
|
2020-01-14 15:46:21 +00:00
|
|
|
ProjectedSelect::Constant(constant) => Ok(PreparedQuery::Constant { select: constant }),
|
2018-03-06 17:01:20 +00:00
|
|
|
ProjectedSelect::Query { query, projector } => {
|
|
|
|
let SQLQuery { sql, args } = query.to_sql_query()?;
|
|
|
|
let statement = sqlite.prepare(sql.as_str())?;
|
|
|
|
|
|
|
|
Ok(PreparedQuery::Bound {
|
|
|
|
statement,
|
2018-05-04 19:56:00 +00:00
|
|
|
schema: known.schema.clone(),
|
|
|
|
connection: sqlite,
|
2018-03-06 17:01:20 +00:00
|
|
|
args,
|
2020-08-06 03:03:58 +00:00
|
|
|
projector,
|
2018-03-06 17:01:20 +00:00
|
|
|
})
|
2020-01-14 15:46:21 +00:00
|
|
|
}
|
2018-03-06 17:01:20 +00:00
|
|
|
}
|
2018-01-24 03:02:25 +00:00
|
|
|
}
|
|
|
|
|
2020-01-14 15:46:21 +00:00
|
|
|
pub fn q_explain<'sqlite, 'query, T>(
|
|
|
|
sqlite: &'sqlite rusqlite::Connection,
|
|
|
|
known: Known,
|
|
|
|
query: &'query str,
|
|
|
|
inputs: T,
|
|
|
|
) -> Result<QueryExplanation>
|
|
|
|
where
|
|
|
|
T: Into<Option<QueryInputs>>,
|
2018-01-20 00:44:39 +00:00
|
|
|
{
|
2018-02-14 00:51:21 +00:00
|
|
|
let algebrized = algebrize_query_str(known, query, inputs)?;
|
2018-01-20 00:44:39 +00:00
|
|
|
if algebrized.is_known_empty() {
|
2020-01-14 15:46:21 +00:00
|
|
|
return Ok(QueryExplanation::KnownEmpty(
|
|
|
|
algebrized.cc.empty_because.unwrap(),
|
|
|
|
));
|
2018-01-20 00:44:39 +00:00
|
|
|
}
|
2018-05-04 19:56:00 +00:00
|
|
|
match query_to_select(known.schema, algebrized)? {
|
2018-03-06 17:01:20 +00:00
|
|
|
ProjectedSelect::Constant(_constant) => Ok(QueryExplanation::KnownConstant),
|
2020-01-14 15:46:21 +00:00
|
|
|
ProjectedSelect::Query {
|
|
|
|
query,
|
|
|
|
projector: _projector,
|
|
|
|
} => {
|
2018-03-06 17:01:20 +00:00
|
|
|
let query = query.to_sql_query()?;
|
|
|
|
|
|
|
|
let plan_sql = format!("EXPLAIN QUERY PLAN {}", query.sql);
|
|
|
|
|
2020-01-14 15:46:21 +00:00
|
|
|
let steps = run_sql_query(sqlite, &plan_sql, &query.args, |row| QueryPlanStep {
|
|
|
|
select_id: row.get(0).unwrap(),
|
|
|
|
order: row.get(1).unwrap(),
|
|
|
|
from: row.get(2).unwrap(),
|
|
|
|
detail: row.get(3).unwrap(),
|
2018-03-06 17:01:20 +00:00
|
|
|
})?;
|
|
|
|
|
|
|
|
Ok(QueryExplanation::ExecutionPlan { query, steps })
|
2020-01-14 15:46:21 +00:00
|
|
|
}
|
2018-03-06 17:01:20 +00:00
|
|
|
}
|
2018-01-20 00:44:39 +00:00
|
|
|
}
|