sqlb-0.3.4 has been yanked.
sqlb is a simple and expressive SQLBuilder for Rust for sqlx focused on PostgreSQL (for now).
- Simple - Focused on providing an expressive, composable, and reasonably typed scheme to build and execute (via sqlx for now) parameterized SQL statements. The goal is NOT to abstract SQL but to make it expressive and composable using Rust programmatic constructs.
- Expressive - From arbitrary typed data in and out (list of names/values) to struct and mapping rules.
- Focused
sqlb
goal is to have a highly ergonomic API at a minimum performance cost. However, using sqlx directly for high bach commands or more advanced usecases is an encouraged approach.- Prepared Statement ONLY!
WARNING: During the
0.y.z
period API changes will result in.y
increments.
NOTE: SQL Builders are typically not used directly by application business logic, but rather to be wrapped in some Application Model Access Layer (e.g., DAOs or MCs - Model Controller - patterns). Even when using ORMs, it is often a good code design to wrap those access via some model access layers.
NOTE: sqlb has the feature
runtime-tokio-rustls
enabled by the sqlx crate. Do not enable a conflicting runtime feature when adding sqlx to your project.
Goals for first 0.x.x releases:
- sqlx - Only plan to be on top of sqlx.
- PostgreSQL - Focus only on PostgreSQL.
- Macros - Adding macros to keep things DRY (but they are optional. All can be implemented via trait objects)
- limitations - Currently, to make types work with
sqlb
they must implementsqlb::SqlxBindable
trait. The aim is to implementSqlxBindable
for allsqlx
types and allowing app code to implementSqlxBindable
for their specific types. If there are any external types that should be supported but are not currently, please feel free to log a ticket.
Early API Example (just conceptual for now)
// `sqlx::FromRow` allows to do sqlx_exec::fetch_as...
// `sqlb::Fields` allows to have:
// - `toto.fields()` (name, value)[] (only direct or NOT Not values)
// - `Todo::field_names()` here would return `["id", "title"]`
// -- Get the field names
let field_names = field_names;
// ["id", "title", "description"]
// -- Create new row
let todo_c = TodoForCreate ;
// will update all fields specified in TodoForCreate
let sb = insert.table.data;
let sb = sb.returning;
let = sb..await?;
// -- Select
let sb = select.table.columns.order_by;
let todos: = sb.fetch_as_all.await?;
// -- Update
let todo_u - TodoForUpdate ;
let sb = update.table.data.and_where_eq;
let row_affected = sb.exec.await?;
// will not update .title because of the use of `.not_none_fields()`.
Latest Major Changes
!
breaking change, +
addition, -
fix.
0.3.1
!
BREAKING CHANGE -HasFields.fields
has been rename toHasFields.not_none_fields()
.!
BREAKING CHANGE -HasFields.not_none_fields()
andHasFields.all_fields()
consume theself
(to avoid uncessary clone).+
-HasFields.all_fields()
- returns all fields (even the one where value are None).+
-HasFields::field_names(): &'static [&'static]
- list of field names (i.e., column names).+
- AddedSqlxBindable
for theOption<T>
(not a blanket impl at this point).0.3.0
been deprecated since did not have the...fields(self)
behavior.
0.2.0
- Changing the generic order to match
sqlx
. From.fetch_one::<(i64, String), _>
to.fetch_one::<_, (i64, String)>
- Changing the generic order to match
0.0.7
sqlb::insert().table("todo")
(in 0.0.7) rather thansqlb::insert("toto")
(<=0.0.6) (for all SqlBuilders)
For sqlb Dev
Start a PostgreSQL
# In terminal 1 - start postges
# In terminal 2 - (optional) launch psql on the Postgres instance above
# In terminal 3 -
# or watch a particular test target