Janken SQL Hub - Database Query Management Library
A high-performance, modular Rust library for parameterizable SQL query management that prevents SQL injection through prepared statements and supports multiple database backends (currently SQLite, PostgreSQL planned).
๐ฏ Overview
Janken SQL Hub enables developers to define SQL queries with parameters in a database-agnostic way, automatically generating prepared statements for different database backends while preventing SQL injection attacks.
Core Capabilities
- โ
Parameterizable SQL Templates -
@param_namesyntax in queries, types defined separately - โ
Dynamic Identifiers -
#[identifier]syntax for parameterizable table/column names and other SQL identifiers - โ List Parameter Support - :[list_param] syntax for IN clauses with item type validation
- โ Web API Integration - Server-side query adapter mapping JSON requests to prepared statements
- โ SQL Injection Protection - Automatic prepared statement generation
- โ Type Safety & Validation - Parameter type validation with constraints (range, pattern, enum, table name validation)
- โ Parameter Constraints - Range limits, regex patterns, enumerated values, and table name validation
๐ Quick Start
Janken SQL Hub enables developers to define SQL queries with parameters in a database-agnostic way, automatically generating prepared statements for different database backends while preventing SQL injection attacks.
โจ Key Features
Parameter Syntax
-- Basic parameter syntax - @params default to string type if no args specified
SELECT * FROM users WHERE id=@user_id AND name=@user_name
-- Dynamic identifier parameters - #[xxx] syntax for table names, column names, etc. (always table_name type)
SELECT * FROM #[table_name] WHERE id=@user_id
SELECT #[column_name] FROM users ORDER BY #[column_name]
-- List parameters for IN clauses - always list type with item type validation
SELECT * FROM users WHERE id IN :[user_ids] AND status IN :[statuses]
-- Parameters in quoted strings (treated as literals)
SELECT * FROM users WHERE name='@literal_text'
Architecture Design Principles
Janken SQL Hub serves as a server-side query adapter, bridging the gap between web API endpoints and database operations:
- QueryDef: Pre-defined, validated SQL queries stored on the server
- query_run(): Web request handler that maps JSON parameters to prepared statements
- Security First: Query templates prevent SQL injection while retaining SQL's efficiency
- No ORM Abstraction: Direct SQL usage avoids inefficient query builders and ORMs
// Web API Workflow:
// 1. Client sends JSON payload: {"user_id": 123}
// 2. Server uses query_name (not SQL) to identify predefined query
// 3. Parameters are validated and injected into prepared statement
// 4. Result returned as JSON
let params = json!;
let result = conn.query_run?;
๐ Usage Guide
1. Define Queries (JSON Configuration)
Each query definition contains:
"query": Required - The SQL statement with@parameter(#[table_name]) placeholders"args": Optional - only needed to override default types or add constraints"returns": Optional - Array of column names for SELECT queries (determines JSON response structure)
2. Load Queries
use ;
// Load from JSON file
let queries = from_file?;
// Or load from JSON object
let json = json!;
let queries = from_json?;
3. Execute Queries
// Setup SQLite connection
let sqlite_conn = SQLite;
let mut conn = SQLite;
// Get user by ID (returns QueryResult with JSON data and SQL execution details)
let params = json!;
let query_result = conn.query_run?;
// Access JSON results: query_result.data
// Access executed SQL statements: query_result.sql_statements (for debugging)
// Create new user
let params = json!;
let query_result = conn.query_run?;
// Query from dynamic table
let params = json!;
let query_result = conn.query_run?;
// Insert into dynamic table
let params = json!;
let query_result = conn.query_run?;
4. Parameter Types and Constraints Supported
Automatic Type Assignment:
@paramparameters: Default to "string" type (can be overridden)#[table_name]parameters: Automatically assigned "table_name" type:[list_param]parameters: Automatically assigned "list" type
// User-specified parameter types (all case-insensitive)
"integer", "string", "float", "boolean", "blob"
// Automatically assigned parameter types (cannot be overridden)
"table_name" // Assigned to parameters using #table syntax
"list" // Assigned to parameters using :[list] syntax
// Constraint types
"range": // For numeric types (integer/float) and blob sizes
"pattern": "regex" // For string types (e.g., email validation)
"enum": // For any type (allowed values). Table names support enum only.
"itemtype": "type" // For list types: specifies the type of each item in the list
// Examples in args object
"id": // Basic integer (overridden from default string)
"balance": // Float with range
"status": // String enum
"email": // String with regex
"user_ids": // List of integers for IN clauses
"names": // Explicit string type (same as default)
"source": // Table name enum (table_name type auto-assigned)
โก Performance Characteristics
- Regex Compilation: One-time lazy static initialization
- Parameter Parsing: O(n) where n = SQL length
- Query Execution: Database-dependent (SQLite ~2-3x slower prepared vs raw, PostgreSQL similar)
- Memory Usage: Minimal (regex + parameter vectors)
- Zero-Copy: Parameter values passed by reference where possible
๐งช Quality Assurance
- Test Coverage: 100% coverage
- Zero Warnings:
cargo clippy -- -D warningsclean - Memory Safety: Rust ownership system guarantees
- Type Safety: Compile-time parameter validation
- SQL Injection: Automatic prepared statements prevent attacks
๐ Roadmap
Planned Enhancements
- PostgreSQL native connection implementation
Database Backend Priorities
- โ SQLite (complete)
- PostgreSQL (planned implementation)
๐ฆ Installation & Links
Install from Crates.io:
Links:
Built with โค๏ธ in Rust for type-safe, performant database query management.