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 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642
// Copyright (c) 2016 Anatoly Ikorsky
//
// Licensed under the Apache License, Version 2.0
// <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0> or the MIT
// license <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. All files in the project carrying such notice may not be copied,
// modified, or distributed except according to those terms.
//! Tokio based asynchronous MySql client library for The Rust Programming Language.
//!
//! # Installation
//!
//! The library is hosted on [crates.io](https://crates.io/crates/mysql_async/).
//!
//! ```toml
//! [dependencies]
//! mysql_async = "<desired version>"
//! ```
//!
//! # Crate Features
//!
//! Default feature set is wide – it includes all default [`mysql_common` features][myslqcommonfeatures]
//! as well as `native-tls`-based TLS support.
//!
//! ## List Of Features
//!
//! * `minimal` – enables only necessary features (at the moment the only necessary feature
//! is `flate2` backend). Enables:
//!
//! - `flate2/zlib"
//!
//! **Example:**
//!
//! ```toml
//! [dependencies]
//! mysql_async = { version = "*", default-features = false, features = ["minimal"]}
//! ```
//!
//! **Note:* it is possible to use another `flate2` backend by directly choosing it:
//!
//! ```toml
//! [dependencies]
//! mysql_async = { version = "*", default-features = false }
//! flate2 = { version = "*", default-features = false, features = ["rust_backend"] }
//! ```
//!
//! * `default` – enables the following set of crate's and dependencies' features:
//!
//! - `native-tls-tls`
//! - `flate2/zlib"
//! - `mysql_common/bigdecimal03`
//! - `mysql_common/rust_decimal`
//! - `mysql_common/time03`
//! - `mysql_common/uuid`
//! - `mysql_common/frunk`
//!
//! * `default-rustls` – same as default but with `rustls-tls` instead of `native-tls-tls`.
//!
//! **Example:**
//!
//! ```toml
//! [dependencies]
//! mysql_async = { version = "*", default-features = false, features = ["default-rustls"] }
//! ```
//!
//! * `native-tls-tls` – enables `native-tls`-based TLS support _(conflicts with `rustls-tls`)_
//!
//! **Example:**
//!
//! ```toml
//! [dependencies]
//! mysql_async = { version = "*", default-features = false, features = ["native-tls-tls"] }
//!
//! * `rustls-tls` – enables `native-tls`-based TLS support _(conflicts with `native-tls-tls`)_
//!
//! **Example:**
//!
//! ```toml
//! [dependencies]
//! mysql_async = { version = "*", default-features = false, features = ["rustls-tls"] }
//!
//! * `tracing` – enables instrumentation via `tracing` package.
//!
//! Primary operations (`query`, `prepare`, `exec`) are instrumented at `INFO` level.
//! Remaining operations, incl. `get_conn`, are instrumented at `DEBUG` level.
//! Also at `DEBUG`, the SQL queries and parameters are added to the `query`, `prepare`
//! and `exec` spans. Also some internal queries are instrumented at `TRACE` level.
//!
//! **Example:**
//!
//! ```toml
//! [dependencies]
//! mysql_async = { version = "*", features = ["tracing"] }
//! ```
//!
//! [myslqcommonfeatures]: https://github.com/blackbeam/rust_mysql_common#crate-features
//!
//! # TLS/SSL Support
//!
//! SSL support comes in two flavors:
//!
//! 1. Based on native-tls – this is the default option, that usually works without pitfalls
//! (see the `native-tls-tls` crate feature).
//!
//! 2. Based on rustls – TLS backend written in Rust (see the `rustls-tls` crate feature).
//!
//! Please also note a few things about rustls:
//! - it will fail if you'll try to connect to the server by its IP address,
//! hostname is required;
//! - it, most likely, won't work on windows, at least with default server certs,
//! generated by the MySql installer.
//!
//! # Connection URL parameters
//!
//! There is a set of url-parameters supported by the driver (see documentation on [`Opts`]).
//!
//! # Example
//!
//! ```rust
//! # use mysql_async::{Result, test_misc::get_opts};
//! use mysql_async::prelude::*;
//! # use std::env;
//!
//! #[derive(Debug, PartialEq, Eq, Clone)]
//! struct Payment {
//! customer_id: i32,
//! amount: i32,
//! account_name: Option<String>,
//! }
//!
//! #[tokio::main]
//! async fn main() -> Result<()> {
//! let payments = vec![
//! Payment { customer_id: 1, amount: 2, account_name: None },
//! Payment { customer_id: 3, amount: 4, account_name: Some("foo".into()) },
//! Payment { customer_id: 5, amount: 6, account_name: None },
//! Payment { customer_id: 7, amount: 8, account_name: None },
//! Payment { customer_id: 9, amount: 10, account_name: Some("bar".into()) },
//! ];
//!
//! let database_url = /* ... */
//! # get_opts();
//!
//! let pool = mysql_async::Pool::new(database_url);
//! let mut conn = pool.get_conn().await?;
//!
//! // Create a temporary table
//! r"CREATE TEMPORARY TABLE payment (
//! customer_id int not null,
//! amount int not null,
//! account_name text
//! )".ignore(&mut conn).await?;
//!
//! // Save payments
//! r"INSERT INTO payment (customer_id, amount, account_name)
//! VALUES (:customer_id, :amount, :account_name)"
//! .with(payments.iter().map(|payment| params! {
//! "customer_id" => payment.customer_id,
//! "amount" => payment.amount,
//! "account_name" => payment.account_name.as_ref(),
//! }))
//! .batch(&mut conn)
//! .await?;
//!
//! // Load payments from the database. Type inference will work here.
//! let loaded_payments = "SELECT customer_id, amount, account_name FROM payment"
//! .with(())
//! .map(&mut conn, |(customer_id, amount, account_name)| Payment { customer_id, amount, account_name })
//! .await?;
//!
//! // Dropped connection will go to the pool
//! drop(conn);
//!
//! // The Pool must be disconnected explicitly because
//! // it's an asynchronous operation.
//! pool.disconnect().await?;
//!
//! assert_eq!(loaded_payments, payments);
//!
//! // the async fn returns Result, so
//! Ok(())
//! }
//! ```
//!
//! # Pool
//!
//! The [`Pool`] structure is an asynchronous connection pool.
//!
//! Please note:
//!
//! * [`Pool`] is a smart pointer – each clone will point to the same pool instance.
//! * [`Pool`] is `Send + Sync + 'static` – feel free to pass it around.
//! * use [`Pool::disconnect`] to gracefuly close the pool.
//! * [`Pool::new`] is lazy and won't assert server availability.
//!
//! # Transaction
//!
//! [`Conn::start_transaction`] is a wrapper, that starts with `START TRANSACTION`
//! and ends with `COMMIT` or `ROLLBACK`.
//!
//! Dropped transaction will be implicitly rolled back if it wasn't explicitly
//! committed or rolled back. Note that this behaviour will be triggered by a pool
//! (on conn drop) or by the next query, i.e. may be delayed.
//!
//! API won't allow you to run nested transactions because some statements causes
//! an implicit commit (`START TRANSACTION` is one of them), so this behavior
//! is chosen as less error prone.
//!
//! # `Value`
//!
//! This enumeration represents the raw value of a MySql cell. Library offers conversion between
//! `Value` and different rust types via `FromValue` trait described below.
//!
//! ## `FromValue` trait
//!
//! This trait is reexported from **mysql_common** create. Please refer to its
//! [crate docs](https://docs.rs/mysql_common) for the list of supported conversions.
//!
//! Trait offers conversion in two flavours:
//!
//! * `from_value(Value) -> T` - convenient, but panicking conversion.
//!
//! Note, that for any variant of `Value` there exist a type, that fully covers its domain,
//! i.e. for any variant of `Value` there exist `T: FromValue` such that `from_value` will never
//! panic. This means, that if your database schema is known, than it's possible to write your
//! application using only `from_value` with no fear of runtime panic.
//!
//! Also note, that some convertions may fail even though the type seem sufficient,
//! e.g. in case of invalid dates (see [sql mode](https://dev.mysql.com/doc/refman/8.0/en/sql-mode.html)).
//!
//! * `from_value_opt(Value) -> Option<T>` - non-panicking, but less convenient conversion.
//!
//! This function is useful to probe conversion in cases, where source database schema
//! is unknown.
//!
//! # MySql query protocols
//!
//! ## Text protocol
//!
//! MySql text protocol is implemented in the set of `Queryable::query*` methods
//! and in the [`prelude::Query`] trait if query is [`prelude::AsQuery`].
//! It's useful when your query doesn't have parameters.
//!
//! **Note:** All values of a text protocol result set will be encoded as strings by the server,
//! so `from_value` conversion may lead to additional parsing costs.
//!
//! ## Binary protocol and prepared statements.
//!
//! MySql binary protocol is implemented in the set of `exec*` methods,
//! defined on the [`prelude::Queryable`] trait and in the [`prelude::Query`]
//! trait if query is [`QueryWithParams`]. Prepared statements is the only way to
//! pass rust value to the MySql server. MySql uses `?` symbol as a parameter placeholder.
//!
//! **Note:** it's only possible to use parameters where a single MySql value
//! is expected, i.e. you can't execute something like `SELECT ... WHERE id IN ?`
//! with a vector as a parameter. You'll need to build a query that looks like
//! `SELECT ... WHERE id IN (?, ?, ...)` and to pass each vector element as
//! a parameter.
//!
//! # Named parameters
//!
//! MySql itself doesn't have named parameters support, so it's implemented on the client side.
//! One should use `:name` as a placeholder syntax for a named parameter. Named parameters uses
//! the following naming convention:
//!
//! * parameter name must start with either `_` or `a..z`
//! * parameter name may continue with `_`, `a..z` and `0..9`
//!
//! **Note:** this rules mean that, say, the statment `SELECT :fooBar` will be translated
//! to `SELECT ?Bar` so please be careful.
//!
//! Named parameters may be repeated within the statement, e.g `SELECT :foo, :foo` will require
//! a single named parameter `foo` that will be repeated on the corresponding positions during
//! statement execution.
//!
//! One should use the `params!` macro to build parameters for execution.
//!
//! **Note:** Positional and named parameters can't be mixed within the single statement.
//!
//! # Statements
//!
//! In MySql each prepared statement belongs to a particular connection and can't be executed
//! on another connection. Trying to do so will lead to an error. The driver won't tie statement
//! to its connection in any way, but one can look on to the connection id, contained
//! in the [`Statement`] structure.
//!
//! # LOCAL INFILE Handlers
//!
//! **Warning:** You should be aware of [Security Considerations for LOAD DATA LOCAL][1].
//!
//! There are two flavors of LOCAL INFILE handlers – _global_ and _local_.
//!
//! I case of a LOCAL INFILE request from the server the driver will try to find a handler for it:
//!
//! 1. It'll try to use _local_ handler installed on the connection, if any;
//! 2. It'll try to use _global_ handler, specified via [`OptsBuilder::local_infile_handler`],
//! if any;
//! 3. It will emit [`LocalInfileError::NoHandler`] if no handlers found.
//!
//! The purpose of a handler (_local_ or _global_) is to return [`InfileData`].
//!
//! ## _Global_ LOCAL INFILE handler
//!
//! See [`prelude::GlobalHandler`].
//!
//! Simply speaking the _global_ handler is an async function that takes a file name (as `&[u8]`)
//! and returns `Result<InfileData>`.
//!
//! You can set it up using [`OptsBuilder::local_infile_handler`]. Server will use it if there is no
//! _local_ handler installed for the connection. This handler might be called multiple times.
//!
//! Examles:
//!
//! 1. [`WhiteListFsHandler`] is a _global_ handler.
//! 2. Every `T: Fn(&[u8]) -> BoxFuture<'static, Result<InfileData, LocalInfileError>>`
//! is a _global_ handler.
//!
//! ## _Local_ LOCAL INFILE handler.
//!
//! Simply speaking the _local_ handler is a future, that returns `Result<InfileData>`.
//!
//! This is a one-time handler – it's consumed after use. You can set it up using
//! [`Conn::set_infile_handler`]. This handler have priority over _global_ handler.
//!
//! Worth noting:
//!
//! 1. `impl Drop for Conn` will clear _local_ handler, i.e. handler will be removed when
//! connection is returned to a `Pool`.
//! 2. [`Conn::reset`] will clear _local_ handler.
//!
//! Example:
//!
//! ```rust
//! # use mysql_async::{prelude::*, test_misc::get_opts, OptsBuilder, Result, Error};
//! # use futures_util::future::FutureExt;
//! # use futures_util::stream::{self, StreamExt};
//! # use bytes::Bytes;
//! # use std::env;
//! # #[tokio::main]
//! # async fn main() -> Result<()> {
//! #
//! # let database_url = get_opts();
//! let pool = mysql_async::Pool::new(database_url);
//!
//! let mut conn = pool.get_conn().await?;
//! "CREATE TEMPORARY TABLE tmp (id INT, val TEXT)".ignore(&mut conn).await?;
//!
//! // We are going to call `LOAD DATA LOCAL` so let's setup a one-time handler.
//! conn.set_infile_handler(async move {
//! // We need to return a stream of `io::Result<Bytes>`
//! Ok(stream::iter([Bytes::from("1,a\r\n"), Bytes::from("2,b\r\n3,c")]).map(Ok).boxed())
//! });
//!
//! let result = r#"LOAD DATA LOCAL INFILE 'whatever'
//! INTO TABLE `tmp`
//! FIELDS TERMINATED BY ',' ENCLOSED BY '\"'
//! LINES TERMINATED BY '\r\n'"#.ignore(&mut conn).await;
//!
//! match result {
//! Ok(()) => (),
//! Err(Error::Server(ref err)) if err.code == 1148 => {
//! // The used command is not allowed with this MySQL version
//! return Ok(());
//! },
//! Err(Error::Server(ref err)) if err.code == 3948 => {
//! // Loading local data is disabled;
//! // this must be enabled on both the client and the server
//! return Ok(());
//! }
//! e @ Err(_) => e.unwrap(),
//! }
//!
//! // Now let's verify the result
//! let result: Vec<(u32, String)> = conn.query("SELECT * FROM tmp ORDER BY id ASC").await?;
//! assert_eq!(
//! result,
//! vec![(1, "a".into()), (2, "b".into()), (3, "c".into())]
//! );
//!
//! drop(conn);
//! pool.disconnect().await?;
//! # Ok(())
//! # }
//! ```
//!
//! [1]: https://dev.mysql.com/doc/refman/8.0/en/load-data-local-security.html
//!
//! # Testing
//!
//! Tests uses followin environment variables:
//! * `DATABASE_URL` – defaults to `mysql://root:password@127.0.0.1:3307/mysql`
//! * `COMPRESS` – set to `1` or `true` to enable compression for tests
//! * `SSL` – set to `1` or `true` to enable TLS for tests
//!
//! You can run a test server using doker. Please note that params related
//! to max allowed packet, local-infile and binary logging are required
//! to properly run tests (please refer to `azure-pipelines.yml`):
//!
//! ```sh
//! docker run -d --name container \
//! -v `pwd`:/root \
//! -p 3307:3306 \
//! -e MYSQL_ROOT_PASSWORD=password \
//! mysql:8.0 \
//! --max-allowed-packet=36700160 \
//! --local-infile \
//! --log-bin=mysql-bin \
//! --log-slave-updates \
//! --gtid_mode=ON \
//! --enforce_gtid_consistency=ON \
//! --server-id=1
//! ```
//!
#![recursion_limit = "1024"]
#![cfg_attr(feature = "nightly", feature(test))]
#[cfg(feature = "nightly")]
extern crate test;
pub use mysql_common::{constants as consts, params};
use std::sync::Arc;
mod buffer_pool;
#[macro_use]
mod tracing_utils;
#[macro_use]
mod macros;
mod conn;
mod connection_like;
/// Errors used in this crate
mod error;
mod io;
mod local_infile_handler;
mod opts;
mod query;
mod queryable;
type BoxFuture<'a, T> = futures_core::future::BoxFuture<'a, Result<T>>;
static BUFFER_POOL: once_cell::sync::Lazy<Arc<crate::buffer_pool::BufferPool>> =
once_cell::sync::Lazy::new(|| Default::default());
#[doc(inline)]
pub use self::conn::{binlog_stream::BinlogStream, Conn};
#[doc(inline)]
pub use self::conn::pool::Pool;
#[doc(inline)]
pub use self::error::{
DriverError, Error, IoError, LocalInfileError, ParseError, Result, ServerError, UrlError,
};
#[doc(inline)]
pub use self::query::QueryWithParams;
#[doc(inline)]
pub use self::queryable::transaction::IsolationLevel;
#[doc(inline)]
#[cfg(any(feature = "rustls", feature = "native-tls"))]
pub use self::opts::ClientIdentity;
#[doc(inline)]
pub use self::opts::{
Opts, OptsBuilder, PoolConstraints, PoolOpts, SslOpts, DEFAULT_INACTIVE_CONNECTION_TTL,
DEFAULT_POOL_CONSTRAINTS, DEFAULT_STMT_CACHE_SIZE, DEFAULT_TTL_CHECK_INTERVAL,
};
#[doc(inline)]
pub use self::local_infile_handler::{builtin::WhiteListFsHandler, InfileData};
#[doc(inline)]
pub use mysql_common::packets::{
binlog_request::BinlogRequest,
session_state_change::{
Gtids, Schema, SessionStateChange, SystemVariable, TransactionCharacteristics,
TransactionState, Unsupported,
},
BinlogDumpFlags, Column, Interval, OkPacket, SessionStateInfo, Sid,
};
pub mod binlog {
#[doc(inline)]
pub use mysql_common::binlog::consts::*;
#[doc(inline)]
pub use mysql_common::binlog::{events, jsonb, jsondiff, row, value};
}
#[doc(inline)]
pub use mysql_common::proto::codec::Compression;
#[doc(inline)]
pub use mysql_common::row::Row;
#[doc(inline)]
pub use mysql_common::params::Params;
#[doc(inline)]
pub use mysql_common::value::Value;
#[doc(inline)]
pub use mysql_common::row::convert::{from_row, from_row_opt, FromRowError};
#[doc(inline)]
pub use mysql_common::value::convert::{from_value, from_value_opt, FromValueError};
#[doc(inline)]
pub use mysql_common::value::json::{Deserialized, Serialized};
#[doc(inline)]
pub use self::queryable::query_result::{result_set_stream::ResultSetStream, QueryResult};
#[doc(inline)]
pub use self::queryable::transaction::{Transaction, TxOpts};
#[doc(inline)]
pub use self::queryable::{BinaryProtocol, TextProtocol};
#[doc(inline)]
pub use self::queryable::stmt::Statement;
/// Futures used in this crate
pub mod futures {
pub use crate::conn::pool::futures::{DisconnectPool, GetConn};
}
/// Traits used in this crate
pub mod prelude {
#[doc(inline)]
pub use crate::local_infile_handler::GlobalHandler;
#[doc(inline)]
pub use crate::query::AsQuery;
#[doc(inline)]
pub use crate::query::{BatchQuery, Query, WithParams};
#[doc(inline)]
pub use crate::queryable::Queryable;
#[doc(inline)]
pub use mysql_common::row::convert::FromRow;
#[doc(inline)]
pub use mysql_common::value::convert::{ConvIr, FromValue, ToValue};
/// Everything that is a statement.
///
/// ```no_run
/// # use std::{borrow::Cow, sync::Arc};
/// # use mysql_async::{Statement, prelude::StatementLike};
/// fn type_is_a_stmt<T: StatementLike>() {}
///
/// type_is_a_stmt::<Cow<'_, str>>();
/// type_is_a_stmt::<&'_ str>();
/// type_is_a_stmt::<String>();
/// type_is_a_stmt::<Box<str>>();
/// type_is_a_stmt::<Arc<str>>();
/// type_is_a_stmt::<Statement>();
///
/// fn ref_to_a_clonable_stmt_is_also_a_stmt<T: StatementLike + Clone>() {
/// type_is_a_stmt::<&T>();
/// }
/// ```
pub trait StatementLike: crate::queryable::stmt::StatementLike {}
impl<T: crate::queryable::stmt::StatementLike> StatementLike for T {}
/// Everything that is a connection.
///
/// Note that you could obtain a `'static` connection by giving away `Conn` or `Pool`.
pub trait ToConnection<'a, 't: 'a>: crate::connection_like::ToConnection<'a, 't> {}
// explicitly implemented because of rusdoc
impl<'a> ToConnection<'a, 'static> for &'a crate::Pool {}
impl<'a> ToConnection<'static, 'static> for crate::Pool {}
impl ToConnection<'static, 'static> for crate::Conn {}
impl<'a> ToConnection<'a, 'static> for &'a mut crate::Conn {}
impl<'a, 't> ToConnection<'a, 't> for &'a mut crate::Transaction<'t> {}
/// Trait for protocol markers [`crate::TextProtocol`] and [`crate::BinaryProtocol`].
pub trait Protocol: crate::queryable::Protocol {}
impl Protocol for crate::BinaryProtocol {}
impl Protocol for crate::TextProtocol {}
pub use mysql_common::params;
}
#[doc(hidden)]
pub mod test_misc {
use lazy_static::lazy_static;
use std::env;
use crate::opts::{Opts, OptsBuilder, SslOpts};
#[allow(dead_code)]
#[allow(unreachable_code)]
fn error_should_implement_send_and_sync() {
fn _dummy<T: Send + Sync + Unpin>(_: T) {}
_dummy(panic!());
}
lazy_static! {
pub static ref DATABASE_URL: String = {
if let Ok(url) = env::var("DATABASE_URL") {
let opts = Opts::from_url(&url).expect("DATABASE_URL invalid");
if opts
.db_name()
.expect("a database name is required")
.is_empty()
{
panic!("database name is empty");
}
url
} else {
"mysql://root:password@localhost:3307/mysql".into()
}
};
}
pub fn get_opts() -> OptsBuilder {
let mut builder = OptsBuilder::from_opts(Opts::from_url(&**DATABASE_URL).unwrap());
if test_ssl() {
let ssl_opts = SslOpts::default()
.with_danger_skip_domain_validation(true)
.with_danger_accept_invalid_certs(true);
builder = builder.prefer_socket(false).ssl_opts(ssl_opts);
}
if test_compression() {
builder = builder.compression(crate::Compression::default());
}
builder
}
pub fn test_compression() -> bool {
["true", "1"].contains(&&*env::var("COMPRESS").unwrap_or_default())
}
pub fn test_ssl() -> bool {
["true", "1"].contains(&&*env::var("SSL").unwrap_or_default())
}
}