reinhardt-query 0.1.0

SQL query builder for Reinhardt framework
Documentation
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
#![warn(missing_docs)]

//! # reinhardt-query
//!
//! A type-safe SQL query builder for the Reinhardt framework.
//!
//! This crate provides a fluent API for constructing SQL queries that target
//! PostgreSQL, MySQL, and SQLite databases. It generates parameterized queries
//! with proper identifier escaping and value placeholders for each backend.
//!
//! ## Features
//!
//! ### DML (Data Manipulation Language)
//! - **Type-safe query construction** - Build SELECT, INSERT, UPDATE, DELETE statements
//! - **DCL (Data Control Language) support** - Build GRANT and REVOKE statements
//! - **Expression system** - Rich expression API with arithmetic, comparison, and logical operators
//! - **Advanced SQL features** - JOINs, GROUP BY, HAVING, DISTINCT, UNION, CTEs, Window functions
//!
//! ### DDL (Data Definition Language)
//! - **Schema management** - CREATE/ALTER/DROP SCHEMA (PostgreSQL, CockroachDB)
//! - **Sequence operations** - CREATE/ALTER/DROP SEQUENCE (PostgreSQL, CockroachDB)
//! - **Database operations** - CREATE/ALTER/DROP DATABASE (all backends)
//! - **Functions & Procedures** - CREATE/ALTER/DROP FUNCTION/PROCEDURE (PostgreSQL, MySQL, CockroachDB)
//! - **Custom types** - CREATE/ALTER/DROP TYPE (PostgreSQL, CockroachDB)
//! - **Materialized views** - CREATE/ALTER/DROP/REFRESH MATERIALIZED VIEW (PostgreSQL, CockroachDB)
//! - **Events** - CREATE/ALTER/DROP EVENT (MySQL)
//! - **Comments** - COMMENT ON for all database objects (PostgreSQL, CockroachDB)
//! - **Maintenance** - VACUUM, ANALYZE, OPTIMIZE/REPAIR/CHECK TABLE
//!
//! ### Multi-Backend Support
//! - **PostgreSQL** - Full DDL and DML support with advanced features
//! - **MySQL** - DML, Functions, Procedures, Events, and table maintenance
//! - **SQLite** - DML and basic DDL operations
//! - **CockroachDB** - Full PostgreSQL compatibility with distributed database features
//! - **Parameterized queries** - Automatic placeholder generation (`$1` for PostgreSQL, `?` for MySQL/SQLite)
//!
//! ## Architecture
//!
//! The crate is organized into several modules:
//!
//! - [`value`]: Core value types for representing SQL values
//! - [`types`]: Identifier, column reference, table reference, and operator types
//! - [`expr`]: Expression building with the [`ExprTrait`] system
//! - [`query`]: Query builders ([`SelectStatement`],
//!   [`InsertStatement`], [`UpdateStatement`],
//!   [`DeleteStatement`])
//! - [`dcl`]: DCL (Data Control Language) builders ([`GrantStatement`],
//!   [`RevokeStatement`], [`Privilege`], [`ObjectType`], [`Grantee`])
//! - [`backend`]: Database backend implementations
//!   ([`PostgresQueryBuilder`],
//!   [`MySqlQueryBuilder`],
//!   [`SqliteQueryBuilder`],
//!   [`CockroachDBQueryBuilder`])
//!
//! ## Quick Start
//!
//! ```rust
//! use reinhardt_query::prelude::*;
//!
//! // Build a SELECT query
//! let mut stmt = Query::select();
//! stmt.column("name")
//!     .column("email")
//!     .from("users")
//!     .and_where(Expr::col("active").eq(true))
//!     .order_by("name", Order::Asc)
//!     .limit(10);
//!
//! // Generate SQL for PostgreSQL
//! let builder = PostgresQueryBuilder::new();
//! let (sql, values) = builder.build_select(&stmt);
//! assert_eq!(
//!     sql,
//!     r#"SELECT "name", "email" FROM "users" WHERE "active" = $1 ORDER BY "name" ASC LIMIT $2"#
//! );
//! assert_eq!(values.len(), 2);
//! ```
//!
//! ## Backend Differences
//!
//! ### DML Features
//! | Feature | PostgreSQL | MySQL | SQLite | CockroachDB |
//! |---------|-----------|-------|--------|-------------|
//! | Identifier quoting | `"name"` | `` `name` `` | `"name"` | `"name"` |
//! | Placeholders | `$1, $2, ...` | `?, ?, ...` | `?, ?, ...` | `$1, $2, ...` |
//! | NULLS FIRST/LAST | ✅ Native | ❌ | ✅ Native | ✅ Native |
//! | DISTINCT ON | ✅ | ❌ | ❌ | ✅ |
//! | Window functions | ✅ Full | ✅ Full | ✅ Full | ✅ Full |
//! | CTEs (WITH) | ✅ | ✅ | ✅ | ✅ |
//!
//! ### DDL Features
//! | Feature | PostgreSQL | MySQL | SQLite | CockroachDB |
//! |---------|-----------|-------|--------|-------------|
//! | CREATE/ALTER/DROP SCHEMA | ✅ | ❌ | ❌ | ✅ |
//! | CREATE/ALTER/DROP SEQUENCE | ✅ | ❌ | ❌ | ✅ |
//! | CREATE/ALTER/DROP DATABASE | ✅ | ✅ | ✅ | ✅ |
//! | CREATE/ALTER/DROP FUNCTION | ✅ | ✅ | ❌ | ✅ |
//! | CREATE/ALTER/DROP PROCEDURE | ✅ | ✅ | ❌ | ✅ |
//! | CREATE/ALTER/DROP TYPE | ✅ | ❌ | ❌ | ✅ |
//! | CREATE/ALTER/DROP EVENT | ❌ | ✅ | ❌ | ❌ |
//! | MATERIALIZED VIEW | ✅ | ❌ | ❌ | ✅ |
//! | COMMENT ON | ✅ | ❌ | ❌ | ✅ |
//! | VACUUM/ANALYZE | ✅ | ❌ | ✅ | ✅ |
//! | OPTIMIZE/REPAIR/CHECK | ❌ | ✅ | ❌ | ❌ |
//!
//! ### DCL Features
//! | Feature | PostgreSQL | MySQL | SQLite | CockroachDB |
//! |---------|-----------|-------|--------|-------------|
//! | GRANT/REVOKE | ✅ | ✅ | ❌ | ✅ |
//! | CREATE/DROP/ALTER ROLE | ✅ | ✅ | ❌ | ✅ |
//! | CREATE/DROP/ALTER USER | ✅ | ✅ | ❌ | ✅ |
//! | RENAME USER | ❌ | ✅ | ❌ | ❌ |
//! | SET ROLE | ✅ | ✅ | ❌ | ✅ |
//! | RESET ROLE | ✅ | ❌ | ❌ | ✅ |
//! | SET DEFAULT ROLE | ❌ | ✅ | ❌ | ❌ |
//!
//! ## Expression Examples
//!
//! ```rust
//! use reinhardt_query::prelude::*;
//!
//! // Arithmetic expressions
//! let expr = Expr::col("price").mul(Expr::col("quantity"));
//!
//! // Comparison with chaining
//! let cond = Expr::col("age").gte(18i32).and(Expr::col("active").eq(true));
//!
//! // CASE WHEN expressions
//! let case_expr = Expr::case()
//!     .when(Expr::col("score").gte(90i32), "A")
//!     .when(Expr::col("score").gte(80i32), "B")
//!     .else_result("C");
//!
//! // LIKE pattern matching
//! let like_expr = Expr::col("email").like("%@example.com");
//! ```
//!
//! ## DDL Examples
//!
//! ```rust,ignore
//! use reinhardt_query::prelude::*;
//!
//! // Create a schema (PostgreSQL, CockroachDB)
//! let mut stmt = Query::create_schema();
//! stmt.name("app_schema").if_not_exists();
//!
//! // Create a sequence (PostgreSQL, CockroachDB)
//! let mut stmt = Query::create_sequence();
//! stmt.name("user_id_seq").start_with(1000).increment_by(1);
//!
//! // Create a function (PostgreSQL, MySQL, CockroachDB)
//! use reinhardt_query::types::function::FunctionLanguage;
//! let mut stmt = Query::create_function();
//! stmt.name("add_numbers")
//!     .add_parameter("a", "INTEGER")
//!     .add_parameter("b", "INTEGER")
//!     .returns("INTEGER")
//!     .language(FunctionLanguage::Sql)
//!     .body("SELECT $1 + $2");
//!
//! // Create a procedure (PostgreSQL, MySQL, CockroachDB)
//! let mut stmt = Query::create_procedure();
//! stmt.name("log_event")
//!     .add_parameter("message", "text")
//!     .language(FunctionLanguage::Sql)
//!     .body("INSERT INTO event_log (message) VALUES ($1)");
//!
//! // Create a custom ENUM type (PostgreSQL, CockroachDB)
//! let mut stmt = Query::create_type();
//! stmt.name("status")
//!     .as_enum(vec!["pending".to_string(), "active".to_string(), "completed".to_string()]);
//!
//! // Create a COMPOSITE type (PostgreSQL, CockroachDB)
//! let mut stmt = Query::create_type();
//! stmt.name("address")
//!     .as_composite(vec![
//!         ("street".to_string(), "text".to_string()),
//!         ("city".to_string(), "text".to_string()),
//!     ]);
//!
//! // Create a materialized view (PostgreSQL, CockroachDB)
//! let select = Query::select()
//!     .column(Expr::col("id"))
//!     .column(Expr::col("name"))
//!     .from("users")
//!     .and_where(Expr::col("active").eq(true));
//!
//! let mut stmt = Query::create_materialized_view();
//! stmt.name("active_users").as_select(select);
//!
//! // Add a comment (PostgreSQL, CockroachDB)
//! let mut stmt = Query::comment();
//! stmt.target(CommentTarget::Table("users".into_iden()))
//!     .comment("Stores user account information");
//! ```
//!
//! ## DCL Examples
//!
//! ### Privilege Management
//!
//! ```rust
//! use reinhardt_query::prelude::*;
//!
//! // GRANT privileges
//! let grant_stmt = Query::grant()
//!     .privilege(Privilege::Select)
//!     .privilege(Privilege::Insert)
//!     .on_table("users")
//!     .to("app_user")
//!     .with_grant_option(true);
//!
//! let builder = PostgresQueryBuilder::new();
//! let (sql, values) = builder.build_grant(&grant_stmt);
//! // sql = r#"GRANT SELECT, INSERT ON TABLE "users" TO "app_user" WITH GRANT OPTION"#
//!
//! // REVOKE privileges
//! let revoke_stmt = Query::revoke()
//!     .privilege(Privilege::Insert)
//!     .from_table("users")
//!     .from("app_user")
//!     .cascade(true);
//!
//! let (sql, values) = builder.build_revoke(&revoke_stmt);
//! // sql = r#"REVOKE INSERT ON TABLE "users" FROM "app_user" CASCADE"#
//! ```
//!
//! ### Role Management
//!
//! ```rust
//! use reinhardt_query::prelude::*;
//!
//! // PostgreSQL: CREATE ROLE with attributes
//! let create_role = Query::create_role()
//!     .role("app_admin")
//!     .attribute(RoleAttribute::Login)
//!     .attribute(RoleAttribute::CreateDb)
//!     .attribute(RoleAttribute::Password("secure_password".to_string()));
//!
//! let builder = PostgresQueryBuilder::new();
//! let (sql, _) = builder.build_create_role(&create_role);
//! // sql = r#"CREATE ROLE "app_admin" WITH LOGIN CREATEDB PASSWORD $1"#
//!
//! // ALTER ROLE
//! let alter_role = Query::alter_role()
//!     .role("app_admin")
//!     .attribute(RoleAttribute::CreateRole);
//!
//! let (sql, _) = builder.build_alter_role(&alter_role);
//! // sql = r#"ALTER ROLE "app_admin" WITH CREATEROLE"#
//!
//! // DROP ROLE
//! let drop_role = Query::drop_role()
//!     .role("app_admin")
//!     .if_exists(true);
//!
//! let (sql, _) = builder.build_drop_role(&drop_role);
//! // sql = r#"DROP ROLE IF EXISTS "app_admin""#
//! ```
//!
//! ### User Management
//!
//! ```rust
//! use reinhardt_query::prelude::*;
//!
//! // MySQL: CREATE USER with options
//! let create_user = Query::create_user()
//!     .user("webapp@localhost")
//!     .if_not_exists(true)
//!     .option(UserOption::Password("webapp_pass".to_string()))
//!     .option(UserOption::AccountUnlock);
//!
//! let builder = MySqlQueryBuilder::new();
//! let (sql, _) = builder.build_create_user(&create_user);
//! // sql = r#"CREATE USER IF NOT EXISTS `webapp@localhost` IDENTIFIED BY ? ACCOUNT UNLOCK"#
//!
//! // MySQL: RENAME USER
//! let rename = Query::rename_user()
//!     .rename("old_user", "new_user");
//!
//! let (sql, _) = builder.build_rename_user(&rename);
//! // sql = r#"RENAME USER `old_user` TO `new_user`"#
//! ```
//!
//! ### Session Management
//!
//! ```rust
//! use reinhardt_query::prelude::*;
//!
//! // PostgreSQL: SET ROLE
//! let set_role = Query::set_role()
//!     .role(RoleTarget::Named("admin".to_string()));
//!
//! let builder = PostgresQueryBuilder::new();
//! let (sql, _) = builder.build_set_role(&set_role);
//! // sql = r#"SET ROLE "admin""#
//!
//! // PostgreSQL: RESET ROLE
//! let reset_role = Query::reset_role();
//!
//! let (sql, _) = builder.build_reset_role(&reset_role);
//! // sql = r#"RESET ROLE"#
//!
//! // MySQL: SET DEFAULT ROLE
//! let set_default = Query::set_default_role()
//!     .roles(DefaultRoleSpec::All)
//!     .user("webapp");
//!
//! let builder = MySqlQueryBuilder::new();
//! let (sql, _) = builder.build_set_default_role(&set_default);
//! // sql = r#"SET DEFAULT ROLE ALL TO `webapp`"#
//! ```
//!
//! ## Feature Flags
//!
//! All features are disabled by default.
//!
//! - `derive`: Enable `#[derive(Iden)]` procedural macro
//! - `thread-safe`: Use `Arc` instead of `Rc` for `DynIden` (enables thread-safe identifiers)
//! - `with-chrono`: Enable chrono date/time types in `Value`
//! - `with-uuid`: Enable UUID type in `Value`
//! - `with-json`: Enable JSON type in `Value`
//! - `with-rust_decimal`: Enable Decimal type in `Value`
//! - `with-bigdecimal`: Enable BigDecimal type in `Value`
//! - `full`: Enable all optional features

// Core modules
pub mod types;
pub mod value;

// Expression module
pub mod expr;

// Query builders
pub mod query;

// DCL (Data Control Language)
pub mod dcl;

// Backend implementations
pub mod backend;

/// NoSQL command builders (Redis, etc.).
#[cfg(feature = "nosql-redis")]
pub mod nosql;

/// Prelude module for convenient imports.
///
/// Import everything from this module to get started quickly:
///
/// ```rust
/// use reinhardt_query::prelude::*;
/// ```
pub mod prelude {
	// Backend builders
	pub use crate::backend::{
		CockroachDBQueryBuilder, MySqlQueryBuilder, PostgresQueryBuilder, QueryBuilder, SqlWriter,
		SqliteQueryBuilder,
	};
	// DCL statements
	pub use crate::dcl::{
		AlterRoleStatement, AlterUserStatement, CreateRoleStatement, CreateUserStatement,
		DefaultRoleSpec, DropRoleStatement, DropUserStatement, GrantRoleStatement, GrantStatement,
		Grantee, ObjectType, Privilege, RenameUserStatement, ResetRoleStatement,
		RevokeRoleStatement, RevokeStatement, RoleAttribute, RoleSpecification, RoleTarget,
		SetDefaultRoleStatement, SetRoleStatement, UserOption,
	};
	// Expression system
	pub use crate::expr::{
		CaseExprBuilder, CaseStatement, Cond, Condition, ConditionExpression, ConditionHolder,
		ConditionType, Expr, ExprTrait, Func, IntoCondition, Keyword, SimpleExpr,
	};
	// DML query builders
	pub use crate::query::{
		DeleteStatement, ForeignKey, ForeignKeyCreateStatement, InsertStatement, OnConflict, Query,
		QueryBuilderTrait, QueryStatementBuilder, QueryStatementWriter, SelectStatement,
		UpdateStatement,
	};
	// DDL query builders
	pub use crate::query::{
		AlterIndexStatement, AlterTableStatement, CreateIndexStatement, CreateTableStatement,
		CreateViewStatement, DropIndexStatement, DropTableStatement, DropViewStatement,
		TruncateTableStatement,
	};
	// Function/Procedure/Type DDL
	pub use crate::query::{
		AlterFunctionStatement, AlterProcedureStatement, AlterTypeStatement,
		CreateFunctionStatement, CreateProcedureStatement, CreateTypeStatement,
		DropFunctionStatement, DropProcedureStatement, DropTypeStatement,
	};
	// Type system
	pub use crate::types::{
		Alias, ColumnRef, DynIden, Iden, IdenStatic, IntoColumnRef, IntoIden, IntoTableRef, Order,
		TableRef,
	};
	// DDL types
	pub use crate::types::{BinOper, JoinType};
	pub use crate::types::{ColumnDef, ColumnType, ForeignKeyAction, IndexDef, TableConstraint};
	// Value system
	pub use crate::value::{ArrayType, IntoValue, Value, ValueTuple, Values};
	// Iden derive macro (feature-gated)
	#[cfg(feature = "derive")]
	pub use reinhardt_query_macros::Iden;
}

// Re-export commonly used types at crate root
pub use prelude::*;