tiberius
tiberius copied to clipboard
Owned variants on FromSql trait are missing for String and Vec<u8>
I would like the ability to pull in row data directly into an owned struct, currently not possible with row.get as there is only support for &str and &[u8] in the FromSql trait.
Consider the following code as example. The commented lines are not possible with the current main branch.
use chrono::NaiveDateTime;
use std::error::Error;
use tiberius::{Client, Config, SqlBrowser, Uuid};
use tokio::net::TcpStream;
use tokio_util::compat::TokioAsyncWriteCompatExt;
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
let config = Config::from_ado_string("Server=localhost;Database=testdb")?;
let tcp = TcpStream::connect_named(&config).await?;
tcp.set_nodelay(true)?;
let mut db = Client::connect(config, tcp.compat_write()).await?;
let result = db.simple_query("select * from [User]").await?.into_first_result().await?;
let _users: Vec<User> = result
.iter()
.map(|row| User {
user_key: row.get(0).unwrap(),
user_name: row.get(1).unwrap(), //String stuct member - FromSql trait only exists for &str currently
name: row.get(2), //String stuct member - FromSql trait only exists for &str currently
email: row.get(3), //String stuct member - FromSql trait only exists for &str currently
active: row.get(4).unwrap(), //
password: row.get(5), //Vec<u8> stuct member - FromSql trait only exists for &[u8] currently
created_on: row.get(6).unwrap(),
updated_on: row.get(7).unwrap(),
})
.collect();
Ok(())
}
pub struct User {
pub user_key: Uuid,
pub user_name: String,
pub name: Option<String>,
pub email: Option<String>,
pub active: bool,
pub password: Option<Vec<u8>>,
pub created_on: NaiveDateTime,
pub updated_on: NaiveDateTime,
}