1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
//! # typed_sqlx_client
//!
//! A type-safe, extensible Rust library for managing multiple SQL databases and tables with sqlx,
//! providing automatic CRUD operations and flexible query capabilities.
//!
//! ## 🎯 Core Design Philosophy
//!
//! This library is specifically designed for **multi-database, multi-table scenarios** where:
//! - Different tables require different operation sets and business logic
//! - Each table can implement custom traits independently
//! - No monolithic "SQL client" that tries to handle all table operations
//! - Type safety prevents accidental cross-database/cross-table operations
//!
//! **Key Principle**: Each table type gets its own trait implementations, allowing for:
//! - 🎛️ **Granular control**: Only implement the operations each table actually needs
//! - 🔒 **Type safety**: Compile-time prevention of mixing databases/tables
//! - 🚀 **Performance**: No runtime overhead for unused operations
//! - 🧩 **Modularity**: Easy to add/remove/modify table-specific behaviors
//!
//! ## Overview
//!
//! This library evolved from v0.1.1 with significant improvements:
//! - **Removed** the old `CrudOps` trait
//! - **Added** the `#[derive(CrudOpsRef)]` macro for automatic CRUD implementation
//! - **Enhanced** SELECT queries with type-safe `execute_select_as_only<T>()` method
//! - **Direct implementation** of `SelectOnlyQuery` on `SqlTable` (no macro required)
//!
//! ## Key Features
//!
//! - **🔒 Type-safe wrappers**: Generic `SqlDB<P, DB>` and `SqlTable<P, DB, Table>` for compile-time safety
//! - **🗄️ Multi-database support**: MySQL, PostgreSQL, and SQLite with unified API
//! - **⚡ Auto-generated CRUD**: `#[derive(CrudOpsRef)]` macro creates all CRUD operations instantly
//! - **🔍 Dual query modes**:
//! - `execute_select_only()`: Dynamic JSON results for unknown schemas
//! - `execute_select_as_only<T>()`: Type-safe deserialization to structs/tuples
//! - **🌐 Framework ready**: Perfect for actix-web, warp, and other async web frameworks
//! - **🏷️ Multiple database support**: Handle multiple databases with compile-time distinction
//!
//! ## Migration from v0.1.1
//!
//! **Breaking Changes:**
//! - `CrudOps` trait has been **removed**
//! - Replace with `#[derive(CrudOpsRef)]` macro on your struct
//! - `SelectOnlyQuery` is now implemented directly on `SqlTable`
//!
//! **New Features:**
//! - Type-safe SELECT with `execute_select_as_only<T>()`
//! - Enhanced field mapping with `#[crud(rename = "...")]`
//! - Improved primary key detection and handling
//!
//! ## Quick Start Example
//!
//! ```rust
//! use typed_sqlx_client::{CrudOpsRef, SqlDB, SelectOnlyQuery};
//! use sqlx::{PgPool, FromRow};
//! use uuid::Uuid;
//!
//! // Define your database entity with automatic CRUD
//! #[derive(FromRow, CrudOpsRef, Debug)]
//! #[crud(table = "users", db = "postgres")]
//! struct User {
//! #[crud(primary_key)]
//! id: Option<Uuid>,
//! #[crud(rename = "user_name")] // Maps to database column 'user_name'
//! name: String,
//! email: String,
//! }
//!
//! // Create a database marker for type safety
//! struct MainDatabase;
//!
//! #[tokio::main]
//! async fn main() -> Result<(), sqlx::Error> {
//! // Setup typed connection pool
//! let pool = PgPool::connect("postgres://...").await?;
//! let sql_pool = SqlDB::from_pool::<MainDatabase>(pool);
//! let user_table = sql_pool.get_table::<User>();
//!
//! // CRUD operations (auto-generated by derive macro)
//! let user = User {
//! id: Some(Uuid::new_v4()),
//! name: "Alice".to_string(),
//! email: "alice@example.com".to_string()
//! };
//! user_table.insert(&user).await?;
//!
//! let found_user = user_table.get_by_id(&user.id.unwrap()).await?;
//!
//! // Type-safe SELECT queries
//! let users: Vec<User> = user_table
//! .execute_select_as_only::<User>("SELECT * FROM users WHERE email LIKE '%@example.com'")
//! .await?;
//!
//! // For aggregations or custom queries
//! let counts: Vec<(i64,)> = user_table
//! .execute_select_as_only::<(i64,)>("SELECT COUNT(*) FROM users")
//! .await?;
//!
//! // Dynamic JSON queries for unknown schemas
//! let json_rows = user_table
//! .execute_select_only("SELECT name, email FROM users")
//! .await?;
//!
//! Ok(())
//! }
//! ```
//!
//! ## Multi-Database Usage
//!
//! Handle multiple databases with type safety:
//!
//! ```rust
//! struct MainDB;
//! struct AnalyticsDB;
//!
//! let main_pool = SqlDB::from_pool::<MainDB>(main_pg_pool);
//! let analytics_pool = SqlDB::from_pool::<AnalyticsDB>(analytics_pg_pool);
//!
//! let users = main_pool.get_table::<User>();
//! let events = analytics_pool.get_table::<Event>();
//! // Compile-time prevents mixing up databases!
//! ```
//!
//! ## Supported Databases
//!
//! | Database | Derive Support | Query Support | Status |
//! |------------|---------------|---------------|---------|
//! | MySQL | ✅ `db = "mysql"` | ✅ Both modes | Stable |
//! | PostgreSQL | ✅ `db = "postgres"` | ✅ Both modes | Stable |
//! | SQLite | ✅ `db = "sqlite"` | ✅ Both modes | Stable |
pub use *;
pub use *;
// Re-export the CrudOpsRef derive macro
pub use CrudOpsRef;