Powerful & expressive ORM/query-builder/static checker for raw queries/Fully Automated migration tooling , designed to offer an intuitive API, strict type-checking, novel features, & full specification support. It provides a fresh perspective in data management. Currently supports SurrealDB engine. RDMSs(PG, MYSQL etc) and others coming soon
85
stars
2
forks
source link
45 Introducing Enhanced SQL Query Building in Rust #46
This new feature significantly improves the way Surrealdb queries are constructed and executed in Rust applications. This update introduces a procedural macro query! that simplifies the process of writing SQL queries, ensuring type safety and reducing the need for repetitive boilerplate code.
Simplified Query Syntax: Write SQL queries directly in Rust with automatic handling of placeholders and bindings.
Type Safety: The macro ensures that the types of Rust variables match the expected SQL types, reducing runtime errors.
Automatic Placeholder Binding: Bind Rust variables to SQL query placeholders seamlessly, without manual string concatenation.
Compile-time SQL Validation: Queries are checked for syntax errors at compile time, catching mistakes early in the development process.
Support for Multiple Queries: Execute multiple SQL statements in one go, with proper handling of each query's unique bindings.
Usage Examples
Basic Query: Execute a simple SQL query with no placeholders or bindings.
let db = Surreal::new::<Mem>(()).await.unwrap();
db.use_ns("test").use_db("test").await.unwrap();
query!(db, "SELECT * FROM users").await;
query!(db, "SELECT * FROM users", {}).await;
With Placeholders: Include Rust variables as parameters in your SQL query.
let username = "Oyelowo";
query!(db, "SELECT name, score FROM users WHERE score > $score AND name = $name", {
score : 99,
name : username
})
.await;
Multiple Queries: Run several SQL statements with their respective bindings.
query!(
db,
[
"SELECT * FROM users WHERE score = $score",
"CREATE user:oyelowo SET name = $name, company = 'Codebreather', skills = $skills"
],
{
score: 1,
name: "Oyelowo",
skills: vec!["Rust", "Python", "Typescript", "Java", "Go", "Ruby", "R"]
}
)
.await;
Benefits
Developer Efficiency: Spend less time writing and debugging SQL queries, and more time on business logic.
Code Clarity: Maintain cleaner code with SURQL queries that are easy to read and understand.
Error Reduction: Reduce the risk of SQL injection and runtime errors through compile-time checks and safe bindings.
Conclusion
This enhancement is a step forward in making surrealdb interactions in Rust as developer-friendly as possible.
Overview
This new feature significantly improves the way
Surrealdb
queries are constructed and executed in Rust applications. This update introduces a procedural macroquery!
that simplifies the process of writing SQL queries, ensuring type safety and reducing the need for repetitive boilerplate code.Demo
https://www.youtube.com/watch?v=eaW3DKrT1Zw
Features
Usage Examples
Basic Query: Execute a simple SQL query with no placeholders or bindings.
With Placeholders: Include Rust variables as parameters in your SQL query.
Multiple Queries: Run several SQL statements with their respective bindings.
Benefits
Conclusion
This enhancement is a step forward in making surrealdb interactions in Rust as developer-friendly as possible.