|
@@ -13,17 +13,28 @@
|
|
|
//!
|
|
|
//! Querying supports a small subset of SQL expressed as type composition.
|
|
|
//!
|
|
|
-//! A simple example using a SQLite table as an (unindexed) key/value store
|
|
|
+//! A simple example using an SQLite table as an (indexed) key/value store
|
|
|
//! might look something like this:
|
|
|
-//! ```
|
|
|
-//! use microrm::Entity;
|
|
|
+//! ```rust
|
|
|
+//! use microrm::{Entity,make_index};
|
|
|
//! #[derive(Debug,Entity,serde::Serialize,serde::Deserialize)]
|
|
|
//! pub struct KVStore {
|
|
|
//! pub key: String,
|
|
|
//! pub value: String
|
|
|
//! }
|
|
|
//!
|
|
|
-//! let schema = microrm::model::SchemaModel::new().add::<KVStore>();
|
|
|
+//! // the !KVStoreIndex here means a type representing a unique index named KVStoreIndex
|
|
|
+//! make_index!(!KVStoreIndex, KVStoreColumns::Key);
|
|
|
+//!
|
|
|
+//! let schema = microrm::model::SchemaModel::new()
|
|
|
+//! .add::<KVStore>()
|
|
|
+//! .index::<KVStoreIndex>();
|
|
|
+//!
|
|
|
+//! // dump the schema in case you want to inspect it manually
|
|
|
+//! for create_sql in schema.create() {
|
|
|
+//! println!("{};", create_sql);
|
|
|
+//! }
|
|
|
+//!
|
|
|
//! let db = microrm::DB::new_in_memory(schema).unwrap();
|
|
|
//! let qi = db.query_interface();
|
|
|
//!
|
|
@@ -32,6 +43,7 @@
|
|
|
//! value: "a_value".to_string()
|
|
|
//! });
|
|
|
//!
|
|
|
+//! // because KVStoreIndex indexes key, this is a logarithmic lookup
|
|
|
//! let qr = qi.get_one_by(KVStoreColumns::Key, "a_key");
|
|
|
//!
|
|
|
//! assert_eq!(qr.is_some(), true);
|
|
@@ -39,7 +51,12 @@
|
|
|
//! assert_eq!(qr.as_ref().unwrap().value, "a_value");
|
|
|
//! ```
|
|
|
//!
|
|
|
-//! A more interesting
|
|
|
+//! The schema output from the loop is (details subject to change based on internals):
|
|
|
+//! ```sql
|
|
|
+//! CREATE TABLE IF NOT EXISTS "kv_store" (id integer primary key,"key" text,"value" text);
|
|
|
+//! CREATE UNIQUE INDEX "kv_store_index" ON "kv_store" ("key");
|
|
|
+//! ```
|
|
|
+
|
|
|
|
|
|
mod meta;
|
|
|
pub mod model;
|
|
@@ -48,7 +65,7 @@ pub mod query;
|
|
|
use meta::Metaschema;
|
|
|
use model::Entity;
|
|
|
|
|
|
-pub use microrm_macros::{Entity, Modelable};
|
|
|
+pub use microrm_macros::{Entity, Modelable, make_index};
|
|
|
pub use query::{QueryInterface, WithID};
|
|
|
|
|
|
// no need to show the re-exports in the documentation
|
|
@@ -265,3 +282,44 @@ mod test {
|
|
|
|
|
|
microrm_macros::make_index_internal!(S2ParentIndex, S2Columns::ParentId);
|
|
|
}
|
|
|
+
|
|
|
+#[cfg(test)]
|
|
|
+mod test2 {
|
|
|
+ use crate::{Entity,make_index};
|
|
|
+ #[derive(Debug,Entity,serde::Serialize,serde::Deserialize)]
|
|
|
+ #[microrm_internal]
|
|
|
+ pub struct KVStore {
|
|
|
+ pub key: String,
|
|
|
+ pub value: String
|
|
|
+ }
|
|
|
+
|
|
|
+ // the !KVStoreIndex here means a type representing a unique index named KVStoreIndex
|
|
|
+ microrm_macros::make_index_internal!(!KVStoreIndex, KVStoreColumns::Key);
|
|
|
+
|
|
|
+ #[test]
|
|
|
+ fn dump_test() {
|
|
|
+ let schema = crate::model::SchemaModel::new()
|
|
|
+ .add::<KVStore>()
|
|
|
+ .index::<KVStoreIndex>();
|
|
|
+
|
|
|
+ // dump the schema in case you want to inspect it manually
|
|
|
+ for create_sql in schema.create() {
|
|
|
+ println!("{};", create_sql);
|
|
|
+ }
|
|
|
+
|
|
|
+ let db = crate::DB::new_in_memory(schema).unwrap();
|
|
|
+ let qi = db.query_interface();
|
|
|
+
|
|
|
+ qi.add(&KVStore {
|
|
|
+ key: "a_key".to_string(),
|
|
|
+ value: "a_value".to_string()
|
|
|
+ });
|
|
|
+
|
|
|
+ // because KVStoreIndex indexes key, this is a logarithmic lookup
|
|
|
+ let qr = qi.get_one_by(KVStoreColumns::Key, "a_key");
|
|
|
+
|
|
|
+ assert_eq!(qr.is_some(), true);
|
|
|
+ assert_eq!(qr.as_ref().unwrap().key, "a_key");
|
|
|
+ assert_eq!(qr.as_ref().unwrap().value, "a_value");
|
|
|
+ }
|
|
|
+}
|