clean up example and add .exec fun
This commit is contained in:
parent
ea93419094
commit
942d5377c4
@ -1,18 +1,40 @@
|
||||
extern crate inline_sql;
|
||||
use inline_sql::{defs, sql};
|
||||
use postgres::{Client, NoTls};
|
||||
use std::env;
|
||||
|
||||
defs!();
|
||||
|
||||
fn main() {
|
||||
fn main() -> Result<(), postgres::Error> {
|
||||
let mut client = Client::connect("host=localhost, user=postgres", NoTls).unwrap();
|
||||
let x = 10;
|
||||
|
||||
client.exec(sql! {
|
||||
CREATE TABLE IF NOT EXISTS INPUTS (
|
||||
arg varchar(420)
|
||||
)
|
||||
})?;
|
||||
|
||||
let args = env::args().collect::<Vec<String>>();
|
||||
|
||||
if args.len() != 2 {
|
||||
eprintln!("Please provide exactly one argument you would like to insert into the DB.");
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let inject = &args[1];
|
||||
|
||||
client.exec(sql! {
|
||||
INSERT INTO INPUTS(arg) VALUES({inject})
|
||||
})?;
|
||||
|
||||
let rows = client.fetch(sql!{
|
||||
SELECT 1 as [foo:i32], generate_series(5, {x}) as [bar:i32]
|
||||
}).unwrap();
|
||||
SELECT [arg: String] FROM INPUTS
|
||||
})?;
|
||||
|
||||
println!("Previously inserted values:");
|
||||
for row in rows {
|
||||
println!("foo={}, bar={}", row.foo, row.bar);
|
||||
println!("{}", row.arg);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@ -34,6 +34,15 @@ pub fn defs(_: TokenStream) -> TokenStream {
|
||||
Ok(res)
|
||||
}
|
||||
}
|
||||
trait Exec {
|
||||
fn exec<'a, O: From<::postgres::row::Row>>(&mut self, query: Query<'a, O>) -> Result<(), ::postgres::error::Error>;
|
||||
}
|
||||
impl Exec for ::postgres::Client {
|
||||
fn exec<'a, O: From<::postgres::row::Row>>(&mut self, query: Query<'a, O>) -> Result<(), ::postgres::error::Error> {
|
||||
self.query(query.code, query.vals)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}.into()
|
||||
}
|
||||
|
||||
@ -41,8 +50,6 @@ pub fn defs(_: TokenStream) -> TokenStream {
|
||||
pub fn sql(tokens: TokenStream) -> TokenStream {
|
||||
let dims = dim::Dimensions::from_tokens(tokens.clone());
|
||||
let printer = print::Printer::render(dims.clone(), tokens.clone());
|
||||
println!("{dims:?}");
|
||||
println!("{}", printer.code());
|
||||
|
||||
let values: TokenStream2 = printer
|
||||
.captured_values()
|
||||
@ -82,6 +89,7 @@ pub fn sql(tokens: TokenStream) -> TokenStream {
|
||||
let query = printer.code();
|
||||
|
||||
quote!{{
|
||||
#[derive(Debug)]
|
||||
struct r#struct {
|
||||
#struct_content_def
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user