tiberius/client.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
mod auth;
mod config;
mod connection;
mod tls;
#[cfg(any(
feature = "rustls",
feature = "native-tls",
feature = "vendored-openssl"
))]
mod tls_stream;
pub use auth::*;
pub use config::*;
pub(crate) use connection::*;
use crate::tds::stream::ReceivedToken;
use crate::{
result::ExecuteResult,
tds::{
codec::{self, IteratorJoin},
stream::{QueryStream, TokenStream},
},
BulkLoadRequest, ColumnFlag, SqlReadBytes, ToSql,
};
use codec::{BatchRequest, ColumnData, PacketHeader, RpcParam, RpcProcId, TokenRpcRequest};
use enumflags2::BitFlags;
use futures::{AsyncRead, AsyncWrite};
use futures_util::TryStreamExt;
use std::{borrow::Cow, fmt::Debug};
/// `Client` is the main entry point to the SQL Server, providing query
/// execution capabilities.
///
/// A `Client` is created using the [`Config`], defining the needed
/// connection options and capabilities.
///
/// # Example
///
/// ```no_run
/// # use tiberius::{Config, AuthMethod};
/// use tokio_util::compat::TokioAsyncWriteCompatExt;
///
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let mut config = Config::new();
///
/// config.host("0.0.0.0");
/// config.port(1433);
/// config.authentication(AuthMethod::sql_server("SA", "<Mys3cureP4ssW0rD>"));
///
/// let tcp = tokio::net::TcpStream::connect(config.get_addr()).await?;
/// tcp.set_nodelay(true)?;
/// // Client is ready to use.
/// let client = tiberius::Client::connect(config, tcp.compat_write()).await?;
/// # Ok(())
/// # }
/// ```
///
/// [`Config`]: struct.Config.html
#[derive(Debug)]
pub struct Client<S: AsyncRead + AsyncWrite + Unpin + Send> {
pub(crate) connection: Connection<S>,
}
impl<S: AsyncRead + AsyncWrite + Unpin + Send> Client<S> {
/// Uses an instance of [`Config`] to specify the connection
/// options required to connect to the database using an established
/// tcp connection
///
/// [`Config`]: struct.Config.html
pub async fn connect(config: Config, tcp_stream: S) -> crate::Result<Client<S>> {
Ok(Client {
connection: Connection::connect(config, tcp_stream).await?,
})
}
/// Executes SQL statements in the SQL Server, returning the number rows
/// affected. Useful for `INSERT`, `UPDATE` and `DELETE` statements. The
/// `query` can define the parameter placement by annotating them with
/// `@PN`, where N is the index of the parameter, starting from `1`. If
/// executing multiple queries at a time, delimit them with `;` and refer to
/// [`ExecuteResult`] how to get results for the separate queries.
///
/// For mapping of Rust types when writing, see the documentation for
/// [`ToSql`]. For reading data from the database, see the documentation for
/// [`FromSql`].
///
/// This API is not quite suitable for dynamic query parameters. In these
/// cases using a [`Query`] object might be easier.
///
/// # Example
///
/// ```no_run
/// # use tiberius::Config;
/// # use tokio_util::compat::TokioAsyncWriteCompatExt;
/// # use std::env;
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// # let c_str = env::var("TIBERIUS_TEST_CONNECTION_STRING").unwrap_or(
/// # "server=tcp:localhost,1433;integratedSecurity=true;TrustServerCertificate=true".to_owned(),
/// # );
/// # let config = Config::from_ado_string(&c_str)?;
/// # let tcp = tokio::net::TcpStream::connect(config.get_addr()).await?;
/// # tcp.set_nodelay(true)?;
/// # let mut client = tiberius::Client::connect(config, tcp.compat_write()).await?;
/// let results = client
/// .execute(
/// "INSERT INTO ##Test (id) VALUES (@P1), (@P2), (@P3)",
/// &[&1i32, &2i32, &3i32],
/// )
/// .await?;
/// # Ok(())
/// # }
/// ```
///
/// [`ExecuteResult`]: struct.ExecuteResult.html
/// [`ToSql`]: trait.ToSql.html
/// [`FromSql`]: trait.FromSql.html
/// [`Query`]: struct.Query.html
pub async fn execute<'a>(
&mut self,
query: impl Into<Cow<'a, str>>,
params: &[&dyn ToSql],
) -> crate::Result<ExecuteResult> {
self.connection.flush_stream().await?;
let rpc_params = Self::rpc_params(query);
let params = params.iter().map(|s| s.to_sql());
self.rpc_perform_query(RpcProcId::ExecuteSQL, rpc_params, params)
.await?;
ExecuteResult::new(&mut self.connection).await
}
/// Executes SQL statements in the SQL Server, returning resulting rows.
/// Useful for `SELECT` statements. The `query` can define the parameter
/// placement by annotating them with `@PN`, where N is the index of the
/// parameter, starting from `1`. If executing multiple queries at a time,
/// delimit them with `;` and refer to [`QueryStream`] on proper stream
/// handling.
///
/// For mapping of Rust types when writing, see the documentation for
/// [`ToSql`]. For reading data from the database, see the documentation for
/// [`FromSql`].
///
/// This API can be cumbersome for dynamic query parameters. In these cases,
/// if fighting too much with the compiler, using a [`Query`] object might be
/// easier.
///
/// # Example
///
/// ```
/// # use tiberius::Config;
/// # use tokio_util::compat::TokioAsyncWriteCompatExt;
/// # use std::env;
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// # let c_str = env::var("TIBERIUS_TEST_CONNECTION_STRING").unwrap_or(
/// # "server=tcp:localhost,1433;integratedSecurity=true;TrustServerCertificate=true".to_owned(),
/// # );
/// # let config = Config::from_ado_string(&c_str)?;
/// # let tcp = tokio::net::TcpStream::connect(config.get_addr()).await?;
/// # tcp.set_nodelay(true)?;
/// # let mut client = tiberius::Client::connect(config, tcp.compat_write()).await?;
/// let stream = client
/// .query(
/// "SELECT @P1, @P2, @P3",
/// &[&1i32, &2i32, &3i32],
/// )
/// .await?;
/// # Ok(())
/// # }
/// ```
///
/// [`QueryStream`]: struct.QueryStream.html
/// [`Query`]: struct.Query.html
/// [`ToSql`]: trait.ToSql.html
/// [`FromSql`]: trait.FromSql.html
pub async fn query<'a, 'b>(
&'a mut self,
query: impl Into<Cow<'b, str>>,
params: &'b [&'b dyn ToSql],
) -> crate::Result<QueryStream<'a>>
where
'a: 'b,
{
self.connection.flush_stream().await?;
let rpc_params = Self::rpc_params(query);
let params = params.iter().map(|p| p.to_sql());
self.rpc_perform_query(RpcProcId::ExecuteSQL, rpc_params, params)
.await?;
let ts = TokenStream::new(&mut self.connection);
let mut result = QueryStream::new(ts.try_unfold());
result.forward_to_metadata().await?;
Ok(result)
}
/// Execute multiple queries, delimited with `;` and return multiple result
/// sets; one for each query.
///
/// # Example
///
/// ```
/// # use tiberius::Config;
/// # use tokio_util::compat::TokioAsyncWriteCompatExt;
/// # use std::env;
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// # let c_str = env::var("TIBERIUS_TEST_CONNECTION_STRING").unwrap_or(
/// # "server=tcp:localhost,1433;integratedSecurity=true;TrustServerCertificate=true".to_owned(),
/// # );
/// # let config = Config::from_ado_string(&c_str)?;
/// # let tcp = tokio::net::TcpStream::connect(config.get_addr()).await?;
/// # tcp.set_nodelay(true)?;
/// # let mut client = tiberius::Client::connect(config, tcp.compat_write()).await?;
/// let row = client.simple_query("SELECT 1 AS col").await?.into_row().await?.unwrap();
/// assert_eq!(Some(1i32), row.get("col"));
/// # Ok(())
/// # }
/// ```
///
/// # Warning
///
/// Do not use this with any user specified input. Please resort to prepared
/// statements using the [`query`] method.
///
/// [`query`]: #method.query
pub async fn simple_query<'a, 'b>(
&'a mut self,
query: impl Into<Cow<'b, str>>,
) -> crate::Result<QueryStream<'a>>
where
'a: 'b,
{
self.connection.flush_stream().await?;
let req = BatchRequest::new(query, self.connection.context().transaction_descriptor());
let id = self.connection.context_mut().next_packet_id();
self.connection.send(PacketHeader::batch(id), req).await?;
let ts = TokenStream::new(&mut self.connection);
let mut result = QueryStream::new(ts.try_unfold());
result.forward_to_metadata().await?;
Ok(result)
}
/// Execute a `BULK INSERT` statement, efficiantly storing a large number of
/// rows to a specified table. Note: make sure the input row follows the same
/// schema as the table, otherwise calling `send()` will return an error.
///
/// # Example
///
/// ```
/// # use tiberius::{Config, IntoRow};
/// # use tokio_util::compat::TokioAsyncWriteCompatExt;
/// # use std::env;
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// # let c_str = env::var("TIBERIUS_TEST_CONNECTION_STRING").unwrap_or(
/// # "server=tcp:localhost,1433;integratedSecurity=true;TrustServerCertificate=true".to_owned(),
/// # );
/// # let config = Config::from_ado_string(&c_str)?;
/// # let tcp = tokio::net::TcpStream::connect(config.get_addr()).await?;
/// # tcp.set_nodelay(true)?;
/// # let mut client = tiberius::Client::connect(config, tcp.compat_write()).await?;
/// let create_table = r#"
/// CREATE TABLE ##bulk_test (
/// id INT IDENTITY PRIMARY KEY,
/// val INT NOT NULL
/// )
/// "#;
///
/// client.simple_query(create_table).await?;
///
/// // Start the bulk insert with the client.
/// let mut req = client.bulk_insert("##bulk_test").await?;
///
/// for i in [0i32, 1i32, 2i32] {
/// let row = (i).into_row();
///
/// // The request will handle flushing to the wire in an optimal way,
/// // balancing between memory usage and IO performance.
/// req.send(row).await?;
/// }
///
/// // The request must be finalized.
/// let res = req.finalize().await?;
/// assert_eq!(3, res.total());
/// # Ok(())
/// # }
/// ```
pub async fn bulk_insert<'a>(
&'a mut self,
table: &'a str,
) -> crate::Result<BulkLoadRequest<'a, S>> {
// Start the bulk request
self.connection.flush_stream().await?;
// retrieve column metadata from server
let query = format!("SELECT TOP 0 * FROM {}", table);
let req = BatchRequest::new(query, self.connection.context().transaction_descriptor());
let id = self.connection.context_mut().next_packet_id();
self.connection.send(PacketHeader::batch(id), req).await?;
let token_stream = TokenStream::new(&mut self.connection).try_unfold();
let columns = token_stream
.try_fold(None, |mut columns, token| async move {
if let ReceivedToken::NewResultset(metadata) = token {
columns = Some(metadata.columns.clone());
};
Ok(columns)
})
.await?;
// now start bulk upload
let columns: Vec<_> = columns
.ok_or_else(|| {
crate::Error::Protocol("expecting column metadata from query but not found".into())
})?
.into_iter()
.filter(|column| column.base.flags.contains(ColumnFlag::Updateable))
.collect();
self.connection.flush_stream().await?;
let col_data = columns.iter().map(|c| format!("{}", c)).join(", ");
let query = format!("INSERT BULK {} ({})", table, col_data);
let req = BatchRequest::new(query, self.connection.context().transaction_descriptor());
let id = self.connection.context_mut().next_packet_id();
self.connection.send(PacketHeader::batch(id), req).await?;
let ts = TokenStream::new(&mut self.connection);
ts.flush_done().await?;
BulkLoadRequest::new(&mut self.connection, columns)
}
/// Closes this database connection explicitly.
pub async fn close(self) -> crate::Result<()> {
self.connection.close().await
}
pub(crate) fn rpc_params<'a>(query: impl Into<Cow<'a, str>>) -> Vec<RpcParam<'a>> {
vec![
RpcParam {
name: Cow::Borrowed("stmt"),
flags: BitFlags::empty(),
value: ColumnData::String(Some(query.into())),
},
RpcParam {
name: Cow::Borrowed("params"),
flags: BitFlags::empty(),
value: ColumnData::I32(Some(0)),
},
]
}
pub(crate) async fn rpc_perform_query<'a, 'b>(
&'a mut self,
proc_id: RpcProcId,
mut rpc_params: Vec<RpcParam<'b>>,
params: impl Iterator<Item = ColumnData<'b>>,
) -> crate::Result<()>
where
'a: 'b,
{
let mut param_str = String::new();
for (i, param) in params.enumerate() {
if i > 0 {
param_str.push(',')
}
param_str.push_str(&format!("@P{} ", i + 1));
param_str.push_str(¶m.type_name());
rpc_params.push(RpcParam {
name: Cow::Owned(format!("@P{}", i + 1)),
flags: BitFlags::empty(),
value: param,
});
}
if let Some(params) = rpc_params.iter_mut().find(|x| x.name == "params") {
params.value = ColumnData::String(Some(param_str.into()));
}
let req = TokenRpcRequest::new(
proc_id,
rpc_params,
self.connection.context().transaction_descriptor(),
);
let id = self.connection.context_mut().next_packet_id();
self.connection.send(PacketHeader::rpc(id), req).await?;
Ok(())
}
}