/*
TODO: make it possible to easely prepare a query
without generating runtime cost
Make every other function prefixed with slow_
to make clear that those function calls are slow
insert should be save to a prepare statement in the table
::new function will be async
implement foreign keys
implement joins
Example Client:
use std::time::{ Instant, Duration };
use std::thread::sleep;
use fire_postgres::{ Database, Table, UniqueId, Result };
use fire_postgres::{ TableTempl, whr };
prepare_find_one!( FindById, "id" = :UniqueId );
pub struct Tests {
table: Table<Test>
find_by_id: FindById
}
impl Tests {
pub async fn new( db: Database ) -> Self {
let table = db.table("test").create().await;
let find_by_id = FindById::new( &table ).await;
Self { table, find_by_id }
}
pub async fn insert( &self, entry: &Test ) -> Result<()> {
self.table.insert_one( entry ).await
}
pub async fn by_id( &self, id: &UniqueId ) -> Result<Option<Test>> {
self.find_by_id.find_by_id( id ).await
}
}
#[derive(Debug, Clone, TableTempl)]
pub struct Test {
#[index(primary)]
id: UniqueId,
num: u32
}
#[tokio::main]
async fn main() -> Result<()> {
let start_full = Instant::now();
let db = Database::new("speedtest", "postgres", "P0St1?b!y1?1bHb").await;
let tests = Tests::new( db ).await;
let mut entries = Vec::with_capacity( 100_000 );
for i in 0..100_000 {
entries.push( Test {
id: UniqueId::new(),
num: i
} );
}
println!("created entries");
return Ok(());
// insert
let start_insert = Instant::now();
for entry in &entries {
tests.insert( entry ).await?
}
let insert_duration = start_insert.elapsed().div_f64( 100_000f64 );
println!("inserted");
sleep( Duration::from_secs( 2 ) );
// select
let start_select = Instant::now();
for entry in &entries {
match tests.by_id( &entry.id ).await? {
Some(_) => continue,
_ => panic!("test select failed")
}
}
let select_duration = start_select.elapsed().div_f64( 100_000f64 );
println!("select");
sleep( Duration::from_secs( 2 ) );
// calculate values
println!( "fulltime {}s", start_full.elapsed().as_secs() );
println!( "1 insert: {}s {}ns", insert_duration.as_secs(), insert_duration.as_nanos() );
println!( "1 select: {}s {}ns", select_duration.as_secs(), select_duration.as_nanos() );
Ok(())
}
*/