Dameng Rust SDK
A comprehensive Rust SDK for Dameng Database (DM8) with ODBC support.
Features
- ✅ Connection Management - Robust connection handling with connection pooling support
- ✅ CRUD Operations - Complete support for Create, Read, Update, Delete operations
- ✅ Transaction Support - Full ACID transaction support with commit and rollback
- ✅ Type-Safe Queries - Type-safe query building with compile-time safety
- ✅ Parameterized Queries - Safe parameterized queries to prevent SQL injection
- ✅ Error Handling - Comprehensive error types with proper error propagation
- ✅ Logging - Built-in logging support using the
logcrate - ✅ Character Encoding - Automatic GBK to UTF-8 conversion for Chinese character support
- ✅ JSON Serialization - Optional JSON serialization/deserialization support
- ✅ TLS Support - Secure database connections with TLS encryption
Installation
Add this to your Cargo.toml:
[]
= "0.1.0"
Or install via cargo:
Quick Start
use *;
use ConnectionOptions;
Usage
Basic Connection
use ;
// Default connection
let mut conn = connect?;
// Connection with custom options
let options = ConnectionOptions ;
let mut conn = with_options?;
Query Operations
Simple SELECT Query
// Execute SELECT query
let mut result = conn.query?;
// Get column names
let columns = result.column_names?;
println!;
// Fetch all rows
let rows = result.fetch_all?;
// Access specific row
if let Some = result.get_row
// Access specific value
if let Some = result.get_value
Parameterized SELECT Query
Use parameterized queries to prevent SQL injection and handle special characters safely:
use InputParameter;
// Create parameters
let odbc_params: = vec!;
// Execute parameterized query with placeholders
let mut result = conn.query_with_param?;
let rows = result.fetch_all?;
for row in rows
Simple INSERT/UPDATE/DELETE
// INSERT
let sql = "INSERT INTO EMPLOYEES (NAME, DEPARTMENT_ID, SALARY) VALUES ('John Doe', 1, 50000)";
let result = conn.execute?;
println!;
// UPDATE
let sql = "UPDATE EMPLOYEES SET SALARY = 55000 WHERE EMPLOYEE_ID = 1";
let result = conn.execute?;
println!;
// DELETE
let sql = "DELETE FROM EMPLOYEES WHERE EMPLOYEE_ID = 100";
let result = conn.execute?;
println!;
Parameterized INSERT/UPDATE/DELETE
Use parameterized queries for dynamic values to prevent SQL injection:
use InputParameter;
// Single parameter
let odbc_params: = vec!;
let result = conn.execute_with_param?;
println!;
// Multiple parameters
let odbc_params: = vec!;
let result = conn.execute_with_param?;
println!;
Transaction Support
// Begin a transaction
conn.begin_transaction?;
try catch
Working with Different Data Types
The SDK supports various Dameng data types:
match value
Database Information
let db_info = conn.database_info?;
println!;
println!;
println!;
println!;
Using DamengClient
For advanced usage, use the DamengClient which provides additional functionality:
use *;
use ConnectionOptions;
use info;
use Env;
Using Query Builder
let builder = conn.query_builder;
// Query builder functionality (implementation depends on version)
Logging
Enable logging by setting the RUST_LOG environment variable:
RUST_LOG=info
use Env;
Configuration
Connection Options
| Option | Type | Default | Description |
|---|---|---|---|
server |
String |
"localhost" |
Database server address |
port |
u16 |
5236 |
Database port |
username |
String |
"SYSDBA" |
Database username |
password |
String |
"" |
Database password |
schema |
String |
"DMHR" |
Database schema |
timeout |
u32 |
30 |
Connection timeout in seconds |
use_tls |
bool |
false |
Enable TLS encryption |
additional_params |
Vec<(String, String)> |
[] |
Additional connection parameters |
Features
default- Includes TLS and JSON supportfull- Includes all features (TLS, JSON, serde, env_logger)tls- TLS encryption supportjson- JSON serialization support
# Use specific features
[]
= { = "0.1.0", = ["full"] }
Examples
Run Demo
The SDK includes a demo application:
Example Projects
See the examples/ directory for more usage examples.
Error Handling
The SDK uses the Result<T> type for error handling:
use Error;
match result
Character Encoding
The SDK automatically handles GBK to UTF-8 conversion for Chinese character support. This is particularly useful when working with Dameng databases containing Chinese data.
Prerequisites
- Rust - Install Rust 1.70 or later from rustup.rs
- Dameng Database - Install and configure Dameng Database (DM8)
- ODBC Driver - Install DM8 ODBC DRIVER
Installing ODBC Driver
- Download DM8 ODBC Driver from the Dameng official website
- Follow the installation instructions for your operating system
- Configure the ODBC Data Source (DSN) if needed
Troubleshooting
Connection Issues
If you encounter connection errors:
- Verify the Dameng database is running
- Check the server address and port
- Ensure the username and password are correct
- Verify the schema exists
- Check ODBC driver installation
Character Encoding Issues
If you see garbled Chinese characters:
- The SDK automatically handles GBK to UTF-8 conversion
- Ensure your database character set is properly configured
Performance Tips
- Use connection pooling for production applications
- Use transactions for multiple related operations
- Optimize your SQL queries with proper indexing
- Use LIMIT clauses to reduce data transfer
API Documentation
Full API documentation is available on docs.rs
Contributing
Contributions are welcome! Please follow these guidelines:
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
Development
# Clone the repository
# Run tests
# Run examples
# Build documentation
License
This project is licensed under the MIT License - see the LICENSE file for details.
Support
- 📧 Email: andrew@out.com
- 📖 Documentation: docs.rs
- 🐛 Issue Tracker: GitHub Issues
- 💬 Discussions: GitHub Discussions
Acknowledgments
- Built with odbc-api
- Character encoding support via encoding_rs
- Error handling with thiserror
Roadmap
- Connection pooling
- Async support
- Stored procedure support
- Batch operations
- Query builder enhancements
- ORM-like features
- Migration tools
Changelog
See CHANGELOG.md for a list of changes in each version.
Made with ❤️ in Rust