Struct rorm_db::database::Database

source ·
pub struct Database { /* private fields */ }
Expand description

Main API wrapper.

All operations can be started with methods of this struct.

Implementations§

Access the used driver at runtime.

This can be used to generate SQL statements for the chosen dialect.

Connect to the database using configuration.

👎Deprecated: Will be removed to reduce number of methods to maintain Use 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.
👎Deprecated: Will be removed to reduce number of methods to maintain Use 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.
👎Deprecated: Will be removed to reduce number of methods to maintain Use 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.
👎Deprecated: Will be removed to reduce number of methods to maintain Use 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.

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?
src/database.rs (lines 151-160)
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
    }

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.

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.

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.

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.

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.

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.

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.

Entry point for a Transaction.

Examples found in repository?
src/database.rs (line 429)
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)
        }
    }

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Execute a query Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Should always be Self
The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.