Skip to main content

DatabaseItemBinder

Trait DatabaseItemBinder 

Source
pub trait DatabaseItemBinder<O, DB: Database> {
    // Required method
    fn bind(&self, item: &O, query_builder: Separated<'_, '_, DB, &str>);
}
Available on crate feature rdbc only.
Expand description

Trait for binding item data to database query parameters.

This trait is generic over the database type, allowing it to work with PostgreSQL, MySQL, SQLite, and other databases supported by SQLx. This provides a unified interface for database-specific item writers.

§Type Parameters

  • O - The item type to bind
  • DB - The SQLx database type (e.g., Postgres, MySql, Sqlite)

§Examples

§PostgreSQL Implementation

use spring_batch_rs::item::rdbc::DatabaseItemBinder;
use sqlx::{query_builder::Separated, Postgres};
use serde::Serialize;

#[derive(Clone, Serialize)]
struct User {
    id: i32,
    name: String,
}

struct UserBinder;
impl DatabaseItemBinder<User, Postgres> for UserBinder {
    fn bind(&self, item: &User, mut query_builder: Separated<Postgres, &str>) {
        let _ = (item, query_builder); // Placeholder to avoid unused warnings
        // In real usage: query_builder.push_bind(item.id);
        // In real usage: query_builder.push_bind(&item.name);
    }
}

§MySQL Implementation

use spring_batch_rs::item::rdbc::DatabaseItemBinder;
use sqlx::{query_builder::Separated, MySql};
use serde::Serialize;

#[derive(Clone, Serialize)]
struct Product {
    id: i32,
    name: String,
    price: f64,
}

struct ProductBinder;
impl DatabaseItemBinder<Product, MySql> for ProductBinder {
    fn bind(&self, item: &Product, mut query_builder: Separated<MySql, &str>) {
        let _ = (item, query_builder); // Placeholder to avoid unused warnings
        // In real usage: query_builder.push_bind(item.id);
        // In real usage: query_builder.push_bind(&item.name);
        // In real usage: query_builder.push_bind(item.price);
    }
}

§SQLite Implementation

use spring_batch_rs::item::rdbc::DatabaseItemBinder;
use sqlx::{query_builder::Separated, Sqlite};
use serde::Serialize;

#[derive(Clone, Serialize)]
struct Task {
    id: i32,
    title: String,
    completed: bool,
}

struct TaskBinder;
impl DatabaseItemBinder<Task, Sqlite> for TaskBinder {
    fn bind(&self, item: &Task, mut query_builder: Separated<Sqlite, &str>) {
        let _ = (item, query_builder); // Placeholder to avoid unused warnings
        // In real usage: query_builder.push_bind(item.id);
        // In real usage: query_builder.push_bind(&item.title);
        // In real usage: query_builder.push_bind(item.completed);
    }
}

Required Methods§

Source

fn bind(&self, item: &O, query_builder: Separated<'_, '_, DB, &str>)

Binds the properties of an item to a separated query builder.

§Arguments
  • item - The item whose properties should be bound.
  • query_builder - The separated query builder to bind parameters to.

Implementors§