pub struct Transaction<'db> { /* private fields */ }
Expand description

Transactions can be used to provide a safe way to execute multiple SQL operations after another with a way to go back to the start without something changed in the database.

Can be obtained using crate::Database::start_transaction.

Implementations§

This function commits the transaction.

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

Use this function to abort the transaction.

Trait Implementations§

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 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.