use crate::{catalogs, config, open, options, runtime, sys};
use dope_runtime::topology;
use std::{mem, time};
#[derive(Debug)]
pub enum Error {
Catalog(crate::Error),
ParameterItemsZero,
ParameterItems {
actual: usize,
},
ParameterBytesZero,
ScanRowsZero,
RowBytesZero,
RowBytes {
actual: usize,
},
CollectRows {
actual: usize,
maximum: usize,
},
CollectBytes {
actual: usize,
required: usize,
},
StatementBytes {
actual: usize,
maximum: usize,
},
MainCacheBytes {
actual: usize,
},
MainCacheInteger {
actual: usize,
},
TemporaryCacheBytes {
actual: usize,
},
TemporaryCacheInteger {
actual: usize,
},
Contention {
operation: Operation,
interval: time::Duration,
window: time::Duration,
},
Savepoints {
actual: usize,
maximum: usize,
},
Runtime(runtime::Error),
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum Operation {
Open,
Call,
Cleanup,
}
pub struct Target<'sql> {
runtime: runtime::Runtime,
location: Location,
options: options::OpenOptions,
commands: sys::Commands<'sql>,
}
const _: () = {
assert!(!mem::needs_drop::<Error>());
assert!(mem::size_of::<Error>() <= 5 * mem::size_of::<usize>());
};
pub(crate) enum Location {
File(sys::Filename),
Memory,
}
#[derive(Clone, Copy)]
pub(crate) enum Kind {
File,
Memory,
}
impl Location {
pub(crate) const fn kind(&self) -> Kind {
match self {
Self::File(_) => Kind::File,
Self::Memory => Kind::Memory,
}
}
}
impl<'sql> Target<'sql> {
pub(crate) fn new(
runtime: runtime::Runtime,
location: Location,
options: options::OpenOptions,
) -> Self {
Self {
runtime,
location,
options,
commands: sys::Commands::EMPTY,
}
}
pub fn commands(mut self, sql: &'sql [&'sql str]) -> Result<Self, crate::Error> {
self.commands = sys::Commands::try_new(sql)?;
Ok(self)
}
pub fn plan<I: catalogs::Catalog>(
self,
limits: crate::Limits,
memory: crate::Memory,
) -> Result<topology::Plan<runtime::Layout<'sql, I>, open::Plan<I>>, Error> {
catalogs::validate::<I>().map_err(Error::Catalog)?;
let config = config::Config::from_parts(limits, memory)?;
let runtime = self.runtime;
let layout = runtime
.layout(config, memory, self)
.map_err(Error::Runtime)?;
Ok(topology::Plan::new(layout, open::Plan::new()))
}
pub(crate) const fn sql_bytes(&self) -> usize {
self.commands.max_bytes()
}
pub(crate) fn into_parts(self) -> (Location, options::OpenOptions, sys::Commands<'sql>) {
(self.location, self.options, self.commands)
}
}