Prax SQLx Backend
SQLx-based query engine backend for Prax ORM with compile-time checked queries.
Features
- Compile-time query checking - Validate SQL queries at compile time using SQLx macros
- Multi-database support - PostgreSQL, MySQL, and SQLite through a unified API
- Type-safe queries - Strong typing for query parameters and results
- Connection pooling - Built-in connection pool management via SQLx
- Async/await - Full async support with tokio runtime
Installation
Add to your Cargo.toml:
[]
= { = "0.1", = ["postgres"] }
Available Features
postgres- PostgreSQL support (default)mysql- MySQL/MariaDB supportsqlite- SQLite supportall-databases- Enable all database backends
Usage
Basic Setup
use ;
async
Raw Queries
use FilterValue;
// Execute a query and get rows
let rows = engine.raw_query_many.await?;
// Execute a query expecting one result
let row = engine.raw_query_one.await?;
// Execute an INSERT/UPDATE/DELETE
let affected = engine.raw_execute.await?;
// Count rows
let count = engine.count_table.await?;
Compile-Time Checked Queries
Use SQLx's query macros for compile-time SQL verification:
use checked;
// The query! macro validates SQL at compile time
let users = query_as!
.fetch_all
.await?;
Database-Specific Features
PostgreSQL
use ;
// Generate upsert SQL
let sql = upsert.await?;
// Use advisory locks
acquire.await?;
// ... do work ...
release.await?;
// Check PostgreSQL version
let version = version.await?;
MySQL
use ;
// Generate upsert SQL
let sql = upsert_sql;
// Use named locks
get_lock.await?;
release_lock.await?;
// Get last insert ID
let id = last_insert_id.await?;
SQLite
use ;
// Enable foreign keys
enable_foreign_keys.await?;
// Set WAL mode for better concurrency
set_journal_mode.await?;
// Vacuum database
vacuum.await?;
// Check integrity
let results = integrity_check.await?;
Transactions
use SqlxPool;
// Begin a transaction
let tx = engine.pool.begin.await?;
// Execute queries within the transaction
// ...
// Commit
tx.commit.await?;
// Or rollback
// tx.rollback().await?;
Using with Prax QueryEngine
The SqlxEngine implements the QueryEngine trait, so it can be used with Prax's query builder:
use QueryEngine;
// The engine implements QueryEngine trait
let results = engine..await?;
Connection Pool Options
let config = builder
// Pool size
.max_connections
.min_connections
// Timeouts
.connect_timeout
.idle_timeout
.max_lifetime
// SSL (PostgreSQL)
.ssl_mode
// Application name (PostgreSQL)
.application_name
.build;
Error Handling
use SqlxError;
match engine.raw_query_one.await
License
Licensed under either of Apache License, Version 2.0 or MIT license at your option.