New Features in RBatis ORM v4.6.12: Enhanced Column Selection for CRUD Operations

⚓ Rust    📅 2025-10-12    👤 surdeus    👁️ 1      

surdeus

Overview

We are excited to announce significant enhancements to RBatis's CRUD operations with the introduction of selective column support for both update_by_map and select_by_map methods. This feature addresses GitHub Issue #591 and provides developers with more granular control over database operations while maintaining performance and ease of use.

:rocket: New Features

1. Selective Column Updates

The update_by_map method now supports selective column updates through the "column" key in the condition parameter, allowing you to update only specific fields instead of the entire entity.

Before (updates all fields):

let data = BizActivity::update_by_map(&rb, &activity, value!{ "id": "1" }).await;

After (selective updates):

// Update only specific columns
let data = BizActivity::update_by_map(&rb, &activity, value!{
    "id": "1",
    "column": ["name", "status"]
}).await;

// Update a single column
let data = BizActivity::update_by_map(&rb, &activity, value!{
    "id": "1",
    "column": "name"
}).await;

2. Selective Column Queries

The select_by_map method now supports selecting only specific columns, reducing data transfer and improving query performance.

Before (selects all columns):

let data = BizActivity::select_by_map(&rb, value!{ "status": 1 }).await;

After (selective queries):

// Select only specific columns
let data = BizActivity::select_by_map(&rb, value!{
    "status": 1,
    "column": ["id", "name", "status"]
}).await;

// Select a single column
let data = BizActivity::select_by_map(&rb, value!{
    "status": 1,
    "column": "name"
}).await;

:bullseye: Key Benefits

Performance Optimization

  • Reduced Data Transfer: Select only the columns you need, minimizing network overhead
  • Faster Query Execution: Database engines can optimize queries with specific column selection
  • Lower Memory Usage: Reduced memory footprint in your application

Enhanced Security

  • Principle of Least Privilege: Only access the data fields you actually need
  • Reduced Exposure: Minimize sensitive data exposure in query results

Improved Code Clarity

  • Intentional Operations: Make it clear which fields are being modified or queried
  • Better Maintainability: Self-documenting code that explicitly states the intended columns

:wrench: Technical Implementation

Flexible Column Specification

Both methods support two formats for column specification:

// Single column (String format)
"column": "name"

// Multiple columns (Array format)
"column": ["name", "status", "create_time"]

Automatic Fallback

When no column is specified or the column list is empty, the methods automatically fall back to the default behavior:

  • Update Operations: Updates all fields in the entity
  • Select Operations: Selects all columns (SELECT *)

Backward Compatibility

All existing code continues to work without changes. The new features are purely additive and maintain full backward compatibility.

:memo: Code Examples

Complete Example: Update and Select Operations

use rbatis::rbdc::datetime::DateTime;
use rbs::value;
use rbatis::RBatis;
use rbdc_sqlite::driver::SqliteDriver;
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct BizActivity {
    pub id: Option<String>,
    pub name: Option<String>,
    pub status: Option<i32>,
    pub create_time: Option<DateTime>,
    pub additional_field: Option<String>,
}

crud!(BizActivity{});

#[tokio::main]
async fn main() {
    let rb = RBatis::new();
    rb.init(SqliteDriver {}, "sqlite://target/sqlite.db").unwrap();

    let activity = BizActivity {
        id: Some("1".into()),
        name: Some("Updated Activity".into()),
        status: Some(2),
        create_time: Some(DateTime::now()),
        additional_field: Some("This field won't be updated".into()),
    };

    // Update only name and status columns
    let update_result = BizActivity::update_by_map(&rb, &activity, value!{
        "id": "1",
        "column": ["name", "status"]
    }).await;

    // Select only id, name, and status columns
    let select_result = BizActivity::select_by_map(&rb, value!{
        "status": 2,
        "column": ["id", "name", "status"]
    }).await;

    println!("Update result: {:?}", update_result);
    println!("Select result: {:?}", select_result);
}

Batch Operations with Column Selection

// Batch update with specific columns
let activities = vec![
    BizActivity { /* ... */ },
    BizActivity { /* ... */ },
];

let batch_update = BizActivity::update_batch_by_column(&rb, &activities, "id").await;

// Select specific columns with complex conditions
let complex_select = BizActivity::select_by_map(&rb, value!{
    "status >": 0,
    "create_time >": "2024-01-01",
    "column": ["id", "name", "status"]
}).await;

:artist_palette: Code Quality and Philosophy

  • Simplicity: Clean, concise code with minimal complexity
  • Efficiency: Single-pass processing with no unnecessary allocations
  • Reliability: Robust error handling and edge case management
  • Clarity: Self-documenting code with clear intent

The code has been optimized for elegance and maintainability, featuring:

  • Single-pass condition processing
  • No unwrap() usage - safe error handling throughout
  • Elegant variable naming and structure
  • Efficient memory usage patterns

:test_tube: Testing and Validation

The new features include comprehensive test coverage:

  • :white_check_mark: Single column selection (String format)
  • :white_check_mark: Multiple column selection (Array format)
  • :white_check_mark: Empty column list fallback behavior
  • :white_check_mark: Backward compatibility verification
  • :white_check_mark: SQL generation validation
  • :white_check_mark: Performance benchmarking

All tests pass successfully, ensuring the reliability and stability of the new features.

:counterclockwise_arrows_button: Migration Guide

Existing Code

No changes required! All existing code continues to work as before.

Adopting New Features

Simply add the "column" key to your condition parameters:

// Existing code (still works)
BizActivity::update_by_map(&rb, &data, value!{ "id": "1" }).await;

// Enhanced code (new feature)
BizActivity::update_by_map(&rb, &data, value!{
    "id": "1",
    "column": ["field1", "field2"]
}).await;

:crystal_ball: Future Roadmap

We're committed to continuing this enhancement path:

  • Additional column selection patterns
  • Enhanced query optimization
  • More granular control over batch operations
  • Extended support for complex data types

:folded_hands: Acknowledgments

This version's features were developed with AI + prompt engineering, demonstrating the power of human-AI collaboration in creating high-quality, well-tested, and thoroughly documented software features.

Special thanks to the Rbatis community for feedback and suggestions that helped shape these enhancements.

:books: Additional Resources


Try Rbatis v4.6.12 today and experience enhanced control over your CRUD operations! :rocket:

1 post - 1 participant

Read full topic

🏷️ Rust_feed