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
/*!
This module defines the main API wrapper.
*/
use log::{debug, warn, LevelFilter};
use rorm_declaration::config::DatabaseDriver;
use rorm_sql::delete::Delete;
use rorm_sql::insert::Insert;
use rorm_sql::join_table::JoinTableData;
use rorm_sql::ordering::OrderByEntry;
use rorm_sql::select::Select;
use rorm_sql::select_column::SelectColumnData;
use rorm_sql::update::Update;
use rorm_sql::value::Value;
use rorm_sql::{conditional, value};
use crate::error::Error;
use crate::executor::{AffectedRows, All, Executor, Nothing, One, QueryStrategy};
use crate::internal;
use crate::query_type::GetLimitClause;
use crate::row::Row;
use crate::transaction::Transaction;
/**
Type alias for [`SelectColumnData`]..
As all databases use currently the same fields, a type alias is sufficient.
*/
pub type ColumnSelector<'a> = SelectColumnData<'a>;
/**
Type alias for [`JoinTableData`].
As all databases use currently the same fields, a type alias is sufficient.
*/
pub type JoinTable<'until_build, 'post_build> = JoinTableData<'until_build, 'post_build>;
/**
Configuration to create a database connection.
`min_connections` and `max_connections` must be greater than 0
and `max_connections` must be greater or equals `min_connections`.
*/
#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct DatabaseConfiguration {
/// The driver and its corresponding settings
pub driver: DatabaseDriver,
/// Minimal connections to initialize upfront.
pub min_connections: u32,
/// Maximum connections that allowed to be created.
pub max_connections: u32,
/// If set to true, logging will be completely disabled.
///
/// In case of None, false will be used.
pub disable_logging: Option<bool>,
/// Set the log level of SQL statements
///
/// In case of None, [`LevelFilter::Debug`] will be used.
pub statement_log_level: Option<LevelFilter>,
/// Log level in case of slow statements (>300 ms)
///
/// In case of None, [`LevelFilter::Warn`] will be used.
pub slow_statement_log_level: Option<LevelFilter>,
}
impl DatabaseConfiguration {
/**
Create a new database configuration with some defaults set.
**Defaults**:
- `min_connections`: 1
- `max_connections`: 10
- `disable_logging`: None
- `statement_log_level`: [`Some`] of [`LevelFilter::Debug`]
- `slow_statement_log_level`: [`Some`] of [`LevelFilter::Warn`]
**Parameter**:
- `driver`: [`DatabaseDriver`]: Configuration of the database driver.
*/
pub fn new(driver: DatabaseDriver) -> Self {
DatabaseConfiguration {
driver,
min_connections: 1,
max_connections: 10,
disable_logging: None,
statement_log_level: Some(LevelFilter::Debug),
slow_statement_log_level: Some(LevelFilter::Warn),
}
}
}
/**
Main API wrapper.
All operations can be started with methods of this struct.
*/
#[derive(Clone)]
pub struct Database(pub(crate) internal::database::Impl);
impl Database {
/**
Connect to the database using `configuration`.
*/
pub async fn connect(configuration: DatabaseConfiguration) -> Result<Self, Error> {
internal::database::connect(configuration).await
}
/**
Execute raw SQL statements on the database.
If possible, the statement is executed as prepared statement.
To bind parameter, use ? as placeholder in SQLite and MySQL
and $1, $2, $n in Postgres.
**Parameter**:
- `query_string`: Reference to a valid SQL query.
- `bind_params`: Optional list of values to bind in the query.
- `transaction`: Optional transaction to execute the query on.
**Returns** a list of rows. If there are no values to retrieve, an empty
list is returned.
*/
pub async fn raw_sql<'a>(
&self,
query_string: &'a str,
bind_params: Option<&[value::Value<'a>]>,
transaction: Option<&mut Transaction>,
) -> Result<Vec<Row>, Error> {
internal::database::raw_sql(self, query_string, bind_params, transaction).await
}
/**
Entry point for a [`Transaction`].
*/
pub async fn start_transaction(&self) -> Result<Transaction, Error> {
internal::database::start_transaction(self).await
}
/// Closes the database connection
pub async fn close(self) {
internal::database::close(self).await
}
}
impl Drop for Database {
fn drop(&mut self) {
if !internal::database::is_closed(self) {
warn!("Database has been dropped without calling close. This might case the last queries to not being flushed properly");
}
}
}
/**
This method is used to retrieve rows that match the provided query.
It is generic over a [`QueryStrategy`] which specifies how and how many rows to query.
**Parameter**:
- `model`: Model to query.
- `columns`: Columns to retrieve values from.
- `joins`: Join tables expressions.
- `conditions`: Optional conditions to apply.
- `limit`: Optional limit / offset to apply to the query.
Depending on the query strategy, this is either [`LimitClause`](rorm_sql::limit_clause::LimitClause)
(for [`All`] and [`Stream`](crate::executor::Stream))
or a simple [`u64`] (for [`One`] and [`Optional`](crate::executor::Optional)).
- `transaction`: Optional transaction to execute the query on.
*/
#[allow(clippy::too_many_arguments)]
pub fn query<'result, 'db: 'result, 'post_query: 'result, Q: QueryStrategy + GetLimitClause>(
executor: impl Executor<'db>,
model: &str,
columns: &[ColumnSelector<'_>],
joins: &[JoinTable<'_, 'post_query>],
conditions: Option<&conditional::Condition<'post_query>>,
order_by_clause: &[OrderByEntry<'_>],
limit: Option<Q::LimitOrOffset>,
) -> Q::Result<'result> {
let columns: Vec<_> = columns
.iter()
.map(|c| {
executor.dialect().select_column(
c.table_name,
c.column_name,
c.select_alias,
c.aggregation,
)
})
.collect();
let joins: Vec<_> = joins
.iter()
.map(|j| {
executor
.dialect()
.join_table(j.join_type, j.table_name, j.join_alias, j.join_condition)
})
.collect();
let mut q = executor
.dialect()
.select(&columns, model, &joins, order_by_clause);
if let Some(condition) = conditions {
q = q.where_clause(condition);
}
if let Some(limit) = Q::get_limit_clause(limit) {
q = q.limit_clause(limit);
}
let (query_string, bind_params) = q.build();
debug!("SQL: {}", query_string);
executor.execute::<Q>(query_string, bind_params)
}
/**
This method is used to insert into a table.
**Parameter**:
- `model`: Table to insert to
- `columns`: Columns to set `values` for.
- `values`: Values to bind to the corresponding columns.
- `transaction`: Optional transaction to execute the query on.
*/
pub async fn insert_returning(
executor: impl Executor<'_>,
model: &str,
columns: &[&str],
values: &[Value<'_>],
returning: &[&str],
) -> Result<Row, Error> {
generic_insert::<One>(executor, model, columns, values, Some(returning)).await
}
/**
This method is used to insert into a table.
**Parameter**:
- `model`: Table to insert to
- `columns`: Columns to set `values` for.
- `values`: Values to bind to the corresponding columns.
- `transaction`: Optional transaction to execute the query on.
*/
pub async fn insert(
executor: impl Executor<'_>,
model: &str,
columns: &[&str],
values: &[Value<'_>],
) -> Result<(), Error> {
generic_insert::<Nothing>(executor, model, columns, values, None).await
}
/// Generic implementation of:
/// - [`Database::insert`]
/// - [`Database::insert_returning`]
pub(crate) fn generic_insert<'result, 'db: 'result, 'post_query: 'result, Q: QueryStrategy>(
executor: impl Executor<'db>,
model: &str,
columns: &[&str],
values: &[Value<'post_query>],
returning: Option<&[&str]>,
) -> Q::Result<'result> {
let values = &[values];
let q = executor.dialect().insert(model, columns, values, returning);
let (query_string, bind_params): (_, Vec<Value<'post_query>>) = q.build();
debug!("SQL: {}", query_string);
executor.execute::<Q>(query_string, bind_params)
}
/**
This method is used to bulk insert rows.
If one insert statement fails, the complete operation will be rolled back.
**Parameter**:
- `model`: Table to insert to
- `columns`: Columns to set `rows` for.
- `rows`: List of values to bind to the corresponding columns.
- `transaction`: Optional transaction to execute the query on.
*/
pub async fn insert_bulk(
executor: impl Executor<'_>,
model: &str,
columns: &[&str],
rows: &[&[Value<'_>]],
) -> Result<(), Error> {
let mut guard = executor.ensure_transaction().await?;
let tr: &mut Transaction = guard.get_transaction();
for chunk in rows.chunks(25) {
let mut insert = tr.dialect().insert(model, columns, chunk, None);
insert = insert.rollback_transaction();
let (insert_query, insert_params) = insert.build();
debug!("SQL: {}", insert_query);
tr.execute::<Nothing>(insert_query, insert_params).await?;
}
guard.commit().await?;
Ok(())
}
/**
This method is used to bulk insert rows.
If one insert statement fails, the complete operation will be rolled back.
**Parameter**:
- `model`: Table to insert to
- `columns`: Columns to set `rows` for.
- `rows`: List of values to bind to the corresponding columns.
- `transaction`: Optional transaction to execute the query on.
*/
pub async fn insert_bulk_returning(
executor: impl Executor<'_>,
model: &str,
columns: &[&str],
rows: &[&[Value<'_>]],
returning: &[&str],
) -> Result<Vec<Row>, Error> {
let mut guard = executor.ensure_transaction().await?;
let tr: &mut Transaction = guard.get_transaction();
let mut inserted = Vec::with_capacity(rows.len());
for chunk in rows.chunks(25) {
let mut insert = tr.dialect().insert(model, columns, chunk, Some(returning));
insert = insert.rollback_transaction();
let (insert_query, insert_params) = insert.build();
debug!("SQL: {}", insert_query);
inserted.extend(tr.execute::<All>(insert_query, insert_params).await?);
}
guard.commit().await?;
Ok(inserted)
}
/**
This method is used to delete rows from a table.
**Parameter**:
- `model`: Name of the model to delete rows from
- `condition`: Optional condition to apply.
- `transaction`: Optional transaction to execute the query on.
**Returns** the rows affected of the delete statement. Note that this also includes
relations, etc.
*/
pub async fn delete<'post_build>(
executor: impl Executor<'_>,
model: &str,
condition: Option<&conditional::Condition<'post_build>>,
) -> Result<u64, Error> {
let mut q = executor.dialect().delete(model);
if condition.is_some() {
q = q.where_clause(condition.unwrap());
}
let (query_string, bind_params) = q.build();
debug!("SQL: {}", query_string);
executor
.execute::<AffectedRows>(query_string, bind_params)
.await
}
/**
This method is used to update rows in a table.
**Parameter**:
- `model`: Name of the model to update rows from
- `updates`: A list of updates. An update is a tuple that consists of a list of columns to
update as well as the value to set to the columns.
- `condition`: Optional condition to apply.
- `transaction`: Optional transaction to execute the query on.
**Returns** the rows affected from the update statement. Note that this also includes
relations, etc.
*/
pub async fn update<'post_build>(
executor: impl Executor<'_>,
model: &str,
updates: &[(&str, Value<'post_build>)],
condition: Option<&conditional::Condition<'post_build>>,
) -> Result<u64, Error> {
let mut stmt = executor.dialect().update(model);
for (column, value) in updates {
stmt = stmt.add_update(column, *value);
}
if let Some(cond) = condition {
stmt = stmt.where_clause(cond);
}
let (query_string, bind_params) = stmt.build()?;
debug!("SQL: {}", query_string);
executor
.execute::<AffectedRows>(query_string, bind_params)
.await
}
#[cfg(test)]
mod test {
use futures::future::BoxFuture;
use crate::{database, Database, Error, Row};
#[allow(unused)]
fn should_compile(db: &'static Database) {
let fut = database::insert_bulk_returning(db, "", &[], &[], &[]);
let fut: BoxFuture<'_, Result<Vec<Row>, Error>> = Box::pin(fut);
drop(fut);
}
}