Struct mysql::conn::Stmt
[−]
[src]
pub struct Stmt<'a> { // some fields omitted }
Mysql prepared statement.
Methods
impl<'a> Stmt<'a>
fn params_ref(&self) -> Option<&[Column]>
Returns a slice of a Column
s which represents
Stmt
's params if any.
fn columns_ref(&self) -> Option<&[Column]>
Returns a slice of a Column
s which represents
Stmt
's columns if any.
fn column_index<T: AsRef<str>>(&self, name: T) -> Option<usize>
Returns index of a Stmt
's column by name.
fn execute<'s, T: ToRow>(&'s mut self, params: T) -> MyResult<QueryResult<'s>>
Executes prepared statement with an arguments passed as a
ToRow
implementor.
let mut stmt0 = pool.prepare("SELECT 42").unwrap(); let mut stmt1 = pool.prepare("SELECT ?").unwrap(); let mut stmt12 = pool.prepare("SELECT ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?").unwrap(); // It is better to pass tuple as a params when executing statements of arity <= 11 for row in stmt0.execute(()).unwrap() { assert_eq!(from_value::<u8>(row.unwrap().pop().unwrap()), 42); } // just do not forget about trailing comma in case of arity = 1 for row in stmt1.execute((42,)).unwrap() { assert_eq!(from_value::<u8>(row.unwrap().pop().unwrap()), 42); } // If you do not want to lose ownership of param, when you should pass it by referense let word = "hello".to_string(); for _ in 0..2 { for row in stmt1.execute((&word,)).unwrap() { assert_eq!(from_value::<String>(row.unwrap().pop().unwrap()),"hello"); } } // If you want to execute statement of arity > 11, then you can pass &[&ToValue] // as params. let params: &[&ToValue] = &[&1, &2, &3, &4, &5, &6, &7, &8, &9, &10, &11, &12]; for row in stmt12.execute(params).unwrap() { let row: Vec<u8> = row.unwrap().into_iter().map(from_value::<u8>).collect(); assert_eq!(row, vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]); } // but be aware of implicit copying, so if you have huge params and do not care // about ownership, then better to use plain Vec<Value>. let mut params: Vec<Value> = Vec::with_capacity(12); for i in 1..13 { params.push(repeat('A').take(i * 1000).collect::<String>().into_value()); } for row in stmt12.execute(params).unwrap() { let row: Vec<String> = row.unwrap().into_iter().map(from_value::<String>).collect(); for i in 1..13 { assert_eq!(row[i-1], repeat('A').take(i * 1000).collect::<String>()); } }