|
@@ -7,9 +7,16 @@ pub mod query;
|
|
|
use meta::Metaschema;
|
|
|
use model::Entity;
|
|
|
|
|
|
-pub use microrm_macros::{Entity, Modelable, make_index};
|
|
|
+pub use microrm_macros::{make_index, Entity, Modelable};
|
|
|
pub use query::{QueryInterface, WithID};
|
|
|
|
|
|
+#[macro_export]
|
|
|
+macro_rules! value_list {
|
|
|
+ ( $( $element:expr ),* ) => {
|
|
|
+ [ $( ($element) as &dyn $crate::model::Modelable ),* ]
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
// no need to show the re-exports in the documentation
|
|
|
#[doc(hidden)]
|
|
|
pub mod re_export {
|
|
@@ -131,9 +138,8 @@ impl DB {
|
|
|
|
|
|
if !has_metaschema && mode != CreateMode::MustExist {
|
|
|
return self.create_schema();
|
|
|
- }
|
|
|
- else if !has_metaschema && mode == CreateMode::MustExist {
|
|
|
- return Err(DBError::NoSchema)
|
|
|
+ } else if !has_metaschema && mode == CreateMode::MustExist {
|
|
|
+ return Err(DBError::NoSchema);
|
|
|
}
|
|
|
|
|
|
let qi = query::QueryInterface::new(self);
|
|
@@ -185,7 +191,7 @@ impl DB {
|
|
|
}
|
|
|
|
|
|
/// Add support for multi-threading to a `DB`.
|
|
|
-///
|
|
|
+///
|
|
|
/// This is a thread-local cache that carefully maintains the property that no
|
|
|
/// element of the cache will ever be accessed in any way from another thread. The only
|
|
|
/// way to maintain this property is to leak all data, so this is best used
|
|
@@ -205,15 +211,21 @@ pub struct DBPool<'a> {
|
|
|
|
|
|
impl<'a> DBPool<'a> {
|
|
|
pub fn new(db: &'a DB) -> Self {
|
|
|
- Self { db: db, qi: std::sync::RwLock::new(Vec::new()) }
|
|
|
+ Self {
|
|
|
+ db: db,
|
|
|
+ qi: std::sync::RwLock::new(Vec::new()),
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/// Get a query interface from this DB pool for the current thread
|
|
|
pub fn query_interface(&self) -> &query::QueryInterface<'a> {
|
|
|
let guard = self.qi.read().expect("Couldn't acquire read lock");
|
|
|
let current_id = std::thread::current().id();
|
|
|
- if let Some(res) = guard.iter().find_map(|x| if x.0 == current_id { Some(x.1) } else { None }) {
|
|
|
- return res
|
|
|
+ if let Some(res) = guard
|
|
|
+ .iter()
|
|
|
+ .find_map(|x| if x.0 == current_id { Some(x.1) } else { None })
|
|
|
+ {
|
|
|
+ return res;
|
|
|
}
|
|
|
|
|
|
drop(guard);
|
|
@@ -232,12 +244,12 @@ unsafe impl<'a> Sync for DBPool<'a> {}
|
|
|
|
|
|
#[cfg(test)]
|
|
|
mod pool_test {
|
|
|
- trait IsSend: Send { }
|
|
|
- impl IsSend for super::DB { }
|
|
|
- impl<'a> IsSend for super::DBPool<'a> { }
|
|
|
+ trait IsSend: Send {}
|
|
|
+ impl IsSend for super::DB {}
|
|
|
+ impl<'a> IsSend for super::DBPool<'a> {}
|
|
|
// we make sure that DBPool is send / sync safe
|
|
|
- trait IsSendAndSync : Send + Sync { }
|
|
|
- impl<'a> IsSendAndSync for super::DBPool<'a> { }
|
|
|
+ trait IsSendAndSync: Send + Sync {}
|
|
|
+ impl<'a> IsSendAndSync for super::DBPool<'a> {}
|
|
|
}
|
|
|
|
|
|
#[cfg(test)]
|
|
@@ -284,11 +296,11 @@ mod test {
|
|
|
|
|
|
#[cfg(test)]
|
|
|
mod test2 {
|
|
|
- #[derive(Debug,crate::Entity,serde::Serialize,serde::Deserialize)]
|
|
|
+ #[derive(Debug, crate::Entity, serde::Serialize, serde::Deserialize)]
|
|
|
#[microrm_internal]
|
|
|
pub struct KVStore {
|
|
|
pub key: String,
|
|
|
- pub value: String
|
|
|
+ pub value: String,
|
|
|
}
|
|
|
|
|
|
// the !KVStoreIndex here means a type representing a unique index named KVStoreIndex
|
|
@@ -310,7 +322,7 @@ mod test2 {
|
|
|
|
|
|
qi.add(&KVStore {
|
|
|
key: "a_key".to_string(),
|
|
|
- value: "a_value".to_string()
|
|
|
+ value: "a_value".to_string(),
|
|
|
});
|
|
|
|
|
|
// because KVStoreIndex indexes key, this is a logarithmic lookup
|