Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Custom Dialects

SqliteDialect and PostgresDialect are used with afastorm’s native SQLx pools when their features are enabled. A custom Dialect can be used with Orm::connect_with_dialect, which connects through SQLx Any; the target database must be supported by SQLx Any.

use afastorm::{Dialect, FieldType, Orm, Result, SqliteDialect};

#[derive(Clone, Copy)]
struct CustomSqliteDialect;

impl Dialect for CustomSqliteDialect {
    fn name(&self) -> &'static str {
        "sqlite"
    }

    fn quote_identifier(&self, identifier: &str) -> Result<String> {
        SqliteDialect.quote_identifier(identifier)
    }

    fn placeholder(&self, index: usize) -> String {
        SqliteDialect.placeholder(index)
    }

    fn column_type(&self, field_type: FieldType) -> &'static str {
        SqliteDialect.column_type(field_type)
    }

    fn auto_increment_primary_key(&self) -> &'static str {
        SqliteDialect.auto_increment_primary_key()
    }

    fn boolean_default(&self, value: bool) -> &'static str {
        SqliteDialect.boolean_default(value)
    }

    fn current_value(&self, field_type: FieldType) -> &'static str {
        SqliteDialect.current_value(field_type)
    }
}

let orm = Orm::connect_with_dialect("sqlite::memory:", CustomSqliteDialect, 1).await?;

In this example, the dialect delegates SQLite syntax and uses SQLx Any; it is intended to show the integration point, not to add a new database backend.

The trait controls identifier quoting, placeholders, column types, auto-increment primary keys, boolean defaults, and current-value expressions. The default field column mapping uses column_type; PostgreSQL-specific per-field array/range schema metadata is handled by the built-in PostgreSQL dialect.

Any has driver and type-encoding limitations. Confirm support for the URL scheme, SQL syntax, RETURNING, transaction behavior, and every model type with the target SQLx driver. Implement current_value with a type-compatible expression if models use auto_create, auto_update, or soft deletion. Custom dialect support does not add custom model field codecs; see Models.