Info
This post is auto-generated from RSS feed The Rust Programming Language Forum - Latest topics. Source: New Features in RBatis ORM v4.6.12: Enhanced Column Selection for CRUD Operations
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.
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;
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;
Both methods support two formats for column specification:
// Single column (String format)
"column": "name"
// Multiple columns (Array format)
"column": ["name", "status", "create_time"]
When no column is specified or the column list is empty, the methods automatically fall back to the default behavior:
SELECT *
)All existing code continues to work without changes. The new features are purely additive and maintain full backward compatibility.
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 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;
The code has been optimized for elegance and maintainability, featuring:
unwrap()
usage - safe error handling throughoutThe new features include comprehensive test coverage:
All tests pass successfully, ensuring the reliability and stability of the new features.
No changes required! All existing code continues to work as before.
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;
We're committed to continuing this enhancement path:
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.
Try Rbatis v4.6.12 today and experience enhanced control over your CRUD operations!
1 post - 1 participant
🏷️ Rust_feed