pub struct Database { /* private fields */ }Expand description
Main API wrapper.
All operations can be started with methods of this struct.
Implementations§
source§impl Database
impl Database
sourcepub fn get_sql_dialect(&self) -> DBImpl
pub fn get_sql_dialect(&self) -> DBImpl
Access the used driver at runtime.
This can be used to generate SQL statements for the chosen dialect.
sourcepub async fn connect(
configuration: DatabaseConfiguration
) -> Result<Self, Error>
pub async fn connect(
configuration: DatabaseConfiguration
) -> Result<Self, Error>
Connect to the database using configuration.
sourcepub fn query_stream<'db, 'post_query, 'stream>(
&'db self,
model: &str,
columns: &[ColumnSelector<'_>],
joins: &[JoinTable<'_, 'post_query>],
conditions: Option<&Condition<'post_query>>,
order_by_clause: &[OrderByEntry<'_>],
limit: Option<LimitClause>,
transaction: Option<&'stream mut Transaction<'_>>
) -> BoxStream<'stream, Result<Row, Error>>where
'post_query: 'stream,
'db: 'stream,
👎Deprecated: Will be removed to reduce number of methods to maintain
Use query::<Stream> instead!
pub fn query_stream<'db, 'post_query, 'stream>(
&'db self,
model: &str,
columns: &[ColumnSelector<'_>],
joins: &[JoinTable<'_, 'post_query>],
conditions: Option<&Condition<'post_query>>,
order_by_clause: &[OrderByEntry<'_>],
limit: Option<LimitClause>,
transaction: Option<&'stream mut Transaction<'_>>
) -> BoxStream<'stream, Result<Row, Error>>where
'post_query: 'stream,
'db: 'stream,
query::<Stream> instead!This method is used to retrieve a stream of rows that matched the applied conditions.
Parameter:
model: Name of the table.columns: Columns to retrieve values from.joins: Join tables expressions.conditions: Optional conditions to apply.limit: Optional limit / offset to apply to the query.transaction: Optional transaction to execute the query on.
sourcepub async fn query_one(
&self,
model: &str,
columns: &[ColumnSelector<'_>],
joins: &[JoinTable<'_, '_>],
conditions: Option<&Condition<'_>>,
order_by_clause: &[OrderByEntry<'_>],
offset: Option<u64>,
transaction: Option<&mut Transaction<'_>>
) -> Result<Row, Error>
👎Deprecated: Will be removed to reduce number of methods to maintain
Use query::<One> instead!
pub async fn query_one(
&self,
model: &str,
columns: &[ColumnSelector<'_>],
joins: &[JoinTable<'_, '_>],
conditions: Option<&Condition<'_>>,
order_by_clause: &[OrderByEntry<'_>],
offset: Option<u64>,
transaction: Option<&mut Transaction<'_>>
) -> Result<Row, Error>
query::<One> instead!This method is used to retrieve exactly one row from the table. An error is returned if no value could be retrieved.
Parameter:
model: Model to query.columns: Columns to retrieve values from.joins: Join tables expressions.conditions: Optional conditions to apply.offset: Optional offset to apply to the query.transaction: Optional transaction to execute the query on.
sourcepub async fn query_optional(
&self,
model: &str,
columns: &[ColumnSelector<'_>],
joins: &[JoinTable<'_, '_>],
conditions: Option<&Condition<'_>>,
order_by_clause: &[OrderByEntry<'_>],
offset: Option<u64>,
transaction: Option<&mut Transaction<'_>>
) -> Result<Option<Row>, Error>
👎Deprecated: Will be removed to reduce number of methods to maintain
Use query::<Optional> instead!
pub async fn query_optional(
&self,
model: &str,
columns: &[ColumnSelector<'_>],
joins: &[JoinTable<'_, '_>],
conditions: Option<&Condition<'_>>,
order_by_clause: &[OrderByEntry<'_>],
offset: Option<u64>,
transaction: Option<&mut Transaction<'_>>
) -> Result<Option<Row>, Error>
query::<Optional> instead!This method is used to retrieve an optional row from the table.
Parameter:
model: Model to query.columns: Columns to retrieve values from.joins: Join tables expressions.conditions: Optional conditions to apply.offset: Optional offset to apply to the query.transaction: Optional transaction to execute the query on.
sourcepub async fn query_all(
&self,
model: &str,
columns: &[ColumnSelector<'_>],
joins: &[JoinTable<'_, '_>],
conditions: Option<&Condition<'_>>,
order_by_clause: &[OrderByEntry<'_>],
limit: Option<LimitClause>,
transaction: Option<&mut Transaction<'_>>
) -> Result<Vec<Row>, Error>
👎Deprecated: Will be removed to reduce number of methods to maintain
Use query::<All> instead!
pub async fn query_all(
&self,
model: &str,
columns: &[ColumnSelector<'_>],
joins: &[JoinTable<'_, '_>],
conditions: Option<&Condition<'_>>,
order_by_clause: &[OrderByEntry<'_>],
limit: Option<LimitClause>,
transaction: Option<&mut Transaction<'_>>
) -> Result<Vec<Row>, Error>
query::<All> instead!This method is used to retrieve all rows that match the provided 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.transaction: Optional transaction to execute the query on.
sourcepub fn query<'result, 'db: 'result, 'post_query: 'result, Q: QueryStrategy + GetLimitClause>(
&'db self,
model: &str,
columns: &[ColumnSelector<'_>],
joins: &[JoinTable<'_, 'post_query>],
conditions: Option<&Condition<'post_query>>,
order_by_clause: &[OrderByEntry<'_>],
limit: Option<Q::LimitOrOffset>,
transaction: Option<&'db mut Transaction<'_>>
) -> Q::Result<'result>
pub fn query<'result, 'db: 'result, 'post_query: 'result, Q: QueryStrategy + GetLimitClause>(
&'db self,
model: &str,
columns: &[ColumnSelector<'_>],
joins: &[JoinTable<'_, 'post_query>],
conditions: Option<&Condition<'post_query>>,
order_by_clause: &[OrderByEntry<'_>],
limit: Option<Q::LimitOrOffset>,
transaction: Option<&'db mut Transaction<'_>>
) -> Q::Result<'result>
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 (for All and Stream) or a simple u64 (for One and Optional).transaction: Optional transaction to execute the query on.
Examples found in repository?
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
pub fn query_stream<'db, 'post_query, 'stream>(
&'db self,
model: &str,
columns: &[ColumnSelector<'_>],
joins: &[JoinTable<'_, 'post_query>],
conditions: Option<&conditional::Condition<'post_query>>,
order_by_clause: &[OrderByEntry<'_>],
limit: Option<LimitClause>,
transaction: Option<&'stream mut Transaction<'_>>,
) -> BoxStream<'stream, Result<Row, Error>>
where
'post_query: 'stream,
'db: 'stream,
{
Self::query::<Stream>(
self,
model,
columns,
joins,
conditions,
order_by_clause,
limit,
transaction,
)
.boxed()
}
/**
This method is used to retrieve exactly one row from the table.
An error is returned if no value could be retrieved.
**Parameter**:
- `model`: Model to query.
- `columns`: Columns to retrieve values from.
- `joins`: Join tables expressions.
- `conditions`: Optional conditions to apply.
- `offset`: Optional offset to apply to the query.
- `transaction`: Optional transaction to execute the query on.
*/
#[deprecated(
note = "Will be removed to reduce number of methods to maintain\nUse `query::<One>` instead!"
)]
#[allow(clippy::too_many_arguments)]
pub async fn query_one(
&self,
model: &str,
columns: &[ColumnSelector<'_>],
joins: &[JoinTable<'_, '_>],
conditions: Option<&conditional::Condition<'_>>,
order_by_clause: &[OrderByEntry<'_>],
offset: Option<u64>,
transaction: Option<&mut Transaction<'_>>,
) -> Result<Row, Error> {
Self::query::<One>(
self,
model,
columns,
joins,
conditions,
order_by_clause,
offset,
transaction,
)
.await
}
/**
This method is used to retrieve an optional row from the table.
**Parameter**:
- `model`: Model to query.
- `columns`: Columns to retrieve values from.
- `joins`: Join tables expressions.
- `conditions`: Optional conditions to apply.
- `offset`: Optional offset to apply to the query.
- `transaction`: Optional transaction to execute the query on.
*/
#[deprecated(
note = "Will be removed to reduce number of methods to maintain\nUse `query::<Optional>` instead!"
)]
#[allow(clippy::too_many_arguments)]
pub async fn query_optional(
&self,
model: &str,
columns: &[ColumnSelector<'_>],
joins: &[JoinTable<'_, '_>],
conditions: Option<&conditional::Condition<'_>>,
order_by_clause: &[OrderByEntry<'_>],
offset: Option<u64>,
transaction: Option<&mut Transaction<'_>>,
) -> Result<Option<Row>, Error> {
Self::query::<Optional>(
self,
model,
columns,
joins,
conditions,
order_by_clause,
offset,
transaction,
)
.await
}
/**
This method is used to retrieve all rows that match the provided 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.
- `transaction`: Optional transaction to execute the query on.
*/
#[deprecated(
note = "Will be removed to reduce number of methods to maintain\nUse `query::<All>` instead!"
)]
#[allow(clippy::too_many_arguments)]
pub async fn query_all(
&self,
model: &str,
columns: &[ColumnSelector<'_>],
joins: &[JoinTable<'_, '_>],
conditions: Option<&conditional::Condition<'_>>,
order_by_clause: &[OrderByEntry<'_>],
limit: Option<LimitClause>,
transaction: Option<&mut Transaction<'_>>,
) -> Result<Vec<Row>, Error> {
Self::query::<All>(
self,
model,
columns,
joins,
conditions,
order_by_clause,
limit,
transaction,
)
.await
}sourcepub async fn insert_returning(
&self,
model: &str,
columns: &[&str],
values: &[Value<'_>],
transaction: Option<&mut Transaction<'_>>,
returning: &[&str]
) -> Result<Row, Error>
pub async fn insert_returning(
&self,
model: &str,
columns: &[&str],
values: &[Value<'_>],
transaction: Option<&mut Transaction<'_>>,
returning: &[&str]
) -> Result<Row, Error>
This method is used to insert into a table.
Parameter:
model: Table to insert tocolumns: Columns to setvaluesfor.values: Values to bind to the corresponding columns.transaction: Optional transaction to execute the query on.
sourcepub async fn insert(
&self,
model: &str,
columns: &[&str],
values: &[Value<'_>],
transaction: Option<&mut Transaction<'_>>
) -> Result<(), Error>
pub async fn insert(
&self,
model: &str,
columns: &[&str],
values: &[Value<'_>],
transaction: Option<&mut Transaction<'_>>
) -> Result<(), Error>
This method is used to insert into a table.
Parameter:
model: Table to insert tocolumns: Columns to setvaluesfor.values: Values to bind to the corresponding columns.transaction: Optional transaction to execute the query on.
sourcepub async fn insert_bulk(
&self,
model: &str,
columns: &[&str],
rows: &[&[Value<'_>]],
transaction: Option<&mut Transaction<'_>>
) -> Result<(), Error>
pub async fn insert_bulk(
&self,
model: &str,
columns: &[&str],
rows: &[&[Value<'_>]],
transaction: Option<&mut Transaction<'_>>
) -> Result<(), Error>
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 tocolumns: Columns to setrowsfor.rows: List of values to bind to the corresponding columns.transaction: Optional transaction to execute the query on.
sourcepub async fn insert_bulk_returning(
&self,
model: &str,
columns: &[&str],
rows: &[&[Value<'_>]],
transaction: Option<&mut Transaction<'_>>,
returning: &[&str]
) -> Result<Vec<Row>, Error>
pub async fn insert_bulk_returning(
&self,
model: &str,
columns: &[&str],
rows: &[&[Value<'_>]],
transaction: Option<&mut Transaction<'_>>,
returning: &[&str]
) -> Result<Vec<Row>, Error>
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 tocolumns: Columns to setrowsfor.rows: List of values to bind to the corresponding columns.transaction: Optional transaction to execute the query on.
sourcepub async fn delete<'post_build>(
&self,
model: &str,
condition: Option<&Condition<'post_build>>,
transaction: Option<&mut Transaction<'_>>
) -> Result<u64, Error>
pub async fn delete<'post_build>(
&self,
model: &str,
condition: Option<&Condition<'post_build>>,
transaction: Option<&mut Transaction<'_>>
) -> Result<u64, Error>
This method is used to delete rows from a table.
Parameter:
model: Name of the model to delete rows fromcondition: 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.
sourcepub async fn update<'post_build>(
&self,
model: &str,
updates: &[(&str, Value<'post_build>)],
condition: Option<&Condition<'post_build>>,
transaction: Option<&mut Transaction<'_>>
) -> Result<u64, Error>
pub async fn update<'post_build>(
&self,
model: &str,
updates: &[(&str, Value<'post_build>)],
condition: Option<&Condition<'post_build>>,
transaction: Option<&mut Transaction<'_>>
) -> Result<u64, Error>
This method is used to update rows in a table.
Parameter:
model: Name of the model to update rows fromupdates: 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.
sourcepub async fn raw_sql<'a>(
&self,
query_string: &'a str,
bind_params: Option<&[Value<'a>]>,
transaction: Option<&mut Transaction<'_>>
) -> Result<Vec<Row>, Error>
pub async fn raw_sql<'a>(
&self,
query_string: &'a str,
bind_params: Option<&[Value<'a>]>,
transaction: Option<&mut Transaction<'_>>
) -> Result<Vec<Row>, Error>
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.
sourcepub async fn start_transaction(&self) -> Result<Transaction<'_>, Error>
pub async fn start_transaction(&self) -> Result<Transaction<'_>, Error>
Entry point for a Transaction.
Examples found in repository?
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
pub async fn insert_bulk(
&self,
model: &str,
columns: &[&str],
rows: &[&[Value<'_>]],
transaction: Option<&mut Transaction<'_>>,
) -> Result<(), Error> {
return match transaction {
None => {
let mut transaction = self.start_transaction().await?;
with_transaction(self, &mut transaction, model, columns, rows).await?;
transaction.commit().await
}
Some(transaction) => {
with_transaction(self, transaction, model, columns, rows).await?;
Ok(())
}
};
async fn with_transaction(
db: &Database,
tx: &mut Transaction<'_>,
model: &str,
columns: &[&str],
rows: &[&[Value<'_>]],
) -> Result<(), Error> {
for chunk in rows.chunks(25) {
let mut insert = db.db_impl.insert(model, columns, chunk, None);
insert = insert.rollback_transaction();
let (insert_query, insert_params) = insert.build();
debug!("SQL: {}", insert_query);
tx.execute::<Nothing>(insert_query, insert_params).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(
&self,
model: &str,
columns: &[&str],
rows: &[&[Value<'_>]],
transaction: Option<&mut Transaction<'_>>,
returning: &[&str],
) -> Result<Vec<Row>, Error> {
return match transaction {
None => {
let mut transaction = self.start_transaction().await?;
let result =
with_transaction(self, &mut transaction, model, columns, rows, returning).await;
transaction.commit().await?;
result
}
Some(transaction) => {
with_transaction(self, transaction, model, columns, rows, returning).await
}
};
async fn with_transaction(
db: &Database,
tx: &mut Transaction<'_>,
model: &str,
columns: &[&str],
rows: &[&[Value<'_>]],
returning: &[&str],
) -> Result<Vec<Row>, Error> {
let mut inserted = Vec::with_capacity(rows.len());
for chunk in rows.chunks(25) {
let mut insert = db.db_impl.insert(model, columns, chunk, Some(returning));
insert = insert.rollback_transaction();
let (insert_query, insert_params) = insert.build();
debug!("SQL: {}", insert_query);
inserted.extend(tx.execute::<All>(insert_query, insert_params).await?);
}
Ok(inserted)
}
}