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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
//! Database maintenance types
//!
//! This module provides types for database maintenance operations:
//!
//! - [`VacuumOption`]: Options for VACUUM statement
//! - [`OptimizeTableOption`]: Options for OPTIMIZE TABLE statement (MySQL-only)
//! - [`RepairTableOption`]: Options for REPAIR TABLE statement (MySQL-only)
//! - [`CheckTableOption`]: Options for CHECK TABLE statement (MySQL-only)

use crate::types::{DynIden, IntoIden};

/// VACUUM statement options
///
/// This struct represents options for the VACUUM statement.
///
/// # Examples
///
/// ```rust
/// use reinhardt_query::types::maintenance::VacuumOption;
///
/// // Basic VACUUM
/// let opt = VacuumOption::new();
///
/// // VACUUM FULL
/// let opt = VacuumOption::new().full(true);
///
/// // VACUUM FULL ANALYZE
/// let opt = VacuumOption::new().full(true).analyze(true);
/// ```
#[derive(Debug, Clone, Default)]
pub struct VacuumOption {
	pub(crate) full: bool,
	pub(crate) freeze: bool,
	pub(crate) verbose: bool,
	pub(crate) analyze: bool,
}

impl VacuumOption {
	/// Create a new VACUUM option
	///
	/// # Examples
	///
	/// ```rust
	/// use reinhardt_query::types::maintenance::VacuumOption;
	///
	/// let opt = VacuumOption::new();
	/// ```
	pub fn new() -> Self {
		Self::default()
	}

	/// Set FULL option
	///
	/// # Examples
	///
	/// ```rust
	/// use reinhardt_query::types::maintenance::VacuumOption;
	///
	/// let opt = VacuumOption::new().full(true);
	/// ```
	pub fn full(mut self, full: bool) -> Self {
		self.full = full;
		self
	}

	/// Set FREEZE option
	///
	/// # Examples
	///
	/// ```rust
	/// use reinhardt_query::types::maintenance::VacuumOption;
	///
	/// let opt = VacuumOption::new().freeze(true);
	/// ```
	pub fn freeze(mut self, freeze: bool) -> Self {
		self.freeze = freeze;
		self
	}

	/// Set VERBOSE option
	///
	/// # Examples
	///
	/// ```rust
	/// use reinhardt_query::types::maintenance::VacuumOption;
	///
	/// let opt = VacuumOption::new().verbose(true);
	/// ```
	pub fn verbose(mut self, verbose: bool) -> Self {
		self.verbose = verbose;
		self
	}

	/// Set ANALYZE option
	///
	/// # Examples
	///
	/// ```rust
	/// use reinhardt_query::types::maintenance::VacuumOption;
	///
	/// let opt = VacuumOption::new().analyze(true);
	/// ```
	pub fn analyze(mut self, analyze: bool) -> Self {
		self.analyze = analyze;
		self
	}
}

/// Table specification for ANALYZE statement
///
/// This struct represents a table and its optional columns for ANALYZE.
///
/// # Examples
///
/// ```rust
/// use reinhardt_query::types::maintenance::AnalyzeTable;
///
/// // Analyze entire table
/// let tbl = AnalyzeTable::new("users");
///
/// // Analyze specific columns
/// let tbl = AnalyzeTable::new("users")
///     .add_column("email")
///     .add_column("name");
/// ```
#[derive(Debug, Clone)]
pub struct AnalyzeTable {
	// Allow dead_code: table field will be used when Phase B ANALYZE implementation is completed
	#[allow(dead_code)]
	pub(crate) table: DynIden,
	pub(crate) columns: Vec<DynIden>,
}

impl AnalyzeTable {
	/// Create a new ANALYZE table specification
	///
	/// # Examples
	///
	/// ```rust
	/// use reinhardt_query::types::maintenance::AnalyzeTable;
	///
	/// let tbl = AnalyzeTable::new("users");
	/// ```
	pub fn new<T: IntoIden>(table: T) -> Self {
		Self {
			table: table.into_iden(),
			columns: Vec::new(),
		}
	}

	/// Add a column to analyze
	///
	/// # Examples
	///
	/// ```rust
	/// use reinhardt_query::types::maintenance::AnalyzeTable;
	///
	/// let tbl = AnalyzeTable::new("users")
	///     .add_column("email");
	/// ```
	pub fn add_column<C: IntoIden>(mut self, column: C) -> Self {
		self.columns.push(column.into_iden());
		self
	}
}

/// OPTIMIZE TABLE statement options (MySQL-only)
///
/// This struct represents options for the OPTIMIZE TABLE statement.
/// OPTIMIZE TABLE reorganizes the physical storage of table data and associated index data.
///
/// # Examples
///
/// ```rust
/// use reinhardt_query::types::maintenance::OptimizeTableOption;
///
/// // Basic OPTIMIZE TABLE
/// let opt = OptimizeTableOption::new();
///
/// // OPTIMIZE NO_WRITE_TO_BINLOG TABLE
/// let opt = OptimizeTableOption::new().no_write_to_binlog(true);
///
/// // OPTIMIZE LOCAL TABLE
/// let opt = OptimizeTableOption::new().local(true);
/// ```
#[derive(Debug, Clone, Default)]
pub struct OptimizeTableOption {
	pub(crate) no_write_to_binlog: bool,
	pub(crate) local: bool,
}

impl OptimizeTableOption {
	/// Create a new OPTIMIZE TABLE option
	///
	/// # Examples
	///
	/// ```rust
	/// use reinhardt_query::types::maintenance::OptimizeTableOption;
	///
	/// let opt = OptimizeTableOption::new();
	/// ```
	pub fn new() -> Self {
		Self::default()
	}

	/// Set NO_WRITE_TO_BINLOG option
	///
	/// Suppresses binary logging for this operation (same as LOCAL).
	///
	/// # Examples
	///
	/// ```rust
	/// use reinhardt_query::types::maintenance::OptimizeTableOption;
	///
	/// let opt = OptimizeTableOption::new().no_write_to_binlog(true);
	/// ```
	pub fn no_write_to_binlog(mut self, no_write_to_binlog: bool) -> Self {
		self.no_write_to_binlog = no_write_to_binlog;
		self
	}

	/// Set LOCAL option
	///
	/// Suppresses binary logging for this operation (same as NO_WRITE_TO_BINLOG).
	///
	/// # Examples
	///
	/// ```rust
	/// use reinhardt_query::types::maintenance::OptimizeTableOption;
	///
	/// let opt = OptimizeTableOption::new().local(true);
	/// ```
	pub fn local(mut self, local: bool) -> Self {
		self.local = local;
		self
	}
}

/// REPAIR TABLE statement options (MySQL-only)
///
/// This struct represents options for the REPAIR TABLE statement.
/// REPAIR TABLE repairs a possibly corrupted table.
///
/// # Examples
///
/// ```rust
/// use reinhardt_query::types::maintenance::RepairTableOption;
///
/// // Basic REPAIR TABLE
/// let opt = RepairTableOption::new();
///
/// // REPAIR TABLE with QUICK option
/// let opt = RepairTableOption::new().quick(true);
///
/// // REPAIR TABLE with EXTENDED option
/// let opt = RepairTableOption::new().extended(true);
/// ```
#[derive(Debug, Clone, Default)]
pub struct RepairTableOption {
	pub(crate) no_write_to_binlog: bool,
	pub(crate) local: bool,
	pub(crate) quick: bool,
	pub(crate) extended: bool,
	pub(crate) use_frm: bool,
}

impl RepairTableOption {
	/// Create a new REPAIR TABLE option
	///
	/// # Examples
	///
	/// ```rust
	/// use reinhardt_query::types::maintenance::RepairTableOption;
	///
	/// let opt = RepairTableOption::new();
	/// ```
	pub fn new() -> Self {
		Self::default()
	}

	/// Set NO_WRITE_TO_BINLOG option
	///
	/// Suppresses binary logging for this operation.
	///
	/// # Examples
	///
	/// ```rust
	/// use reinhardt_query::types::maintenance::RepairTableOption;
	///
	/// let opt = RepairTableOption::new().no_write_to_binlog(true);
	/// ```
	pub fn no_write_to_binlog(mut self, no_write_to_binlog: bool) -> Self {
		self.no_write_to_binlog = no_write_to_binlog;
		self
	}

	/// Set LOCAL option
	///
	/// Suppresses binary logging for this operation (same as NO_WRITE_TO_BINLOG).
	///
	/// # Examples
	///
	/// ```rust
	/// use reinhardt_query::types::maintenance::RepairTableOption;
	///
	/// let opt = RepairTableOption::new().local(true);
	/// ```
	pub fn local(mut self, local: bool) -> Self {
		self.local = local;
		self
	}

	/// Set QUICK option
	///
	/// Tries to repair only the index file, not the data file.
	///
	/// # Examples
	///
	/// ```rust
	/// use reinhardt_query::types::maintenance::RepairTableOption;
	///
	/// let opt = RepairTableOption::new().quick(true);
	/// ```
	pub fn quick(mut self, quick: bool) -> Self {
		self.quick = quick;
		self
	}

	/// Set EXTENDED option
	///
	/// Creates the index row by row instead of creating one index at a time with sorting.
	///
	/// # Examples
	///
	/// ```rust
	/// use reinhardt_query::types::maintenance::RepairTableOption;
	///
	/// let opt = RepairTableOption::new().extended(true);
	/// ```
	pub fn extended(mut self, extended: bool) -> Self {
		self.extended = extended;
		self
	}

	/// Set USE_FRM option
	///
	/// Uses the table definition from the .frm file to recreate the index file.
	///
	/// # Examples
	///
	/// ```rust
	/// use reinhardt_query::types::maintenance::RepairTableOption;
	///
	/// let opt = RepairTableOption::new().use_frm(true);
	/// ```
	pub fn use_frm(mut self, use_frm: bool) -> Self {
		self.use_frm = use_frm;
		self
	}
}

/// CHECK TABLE statement options (MySQL-only)
///
/// This enum represents the check option for the CHECK TABLE statement.
/// CHECK TABLE checks a table or tables for errors.
///
/// # Examples
///
/// ```rust
/// use reinhardt_query::types::maintenance::CheckTableOption;
///
/// // Default check (MEDIUM)
/// let opt = CheckTableOption::default();
///
/// // Quick check
/// let opt = CheckTableOption::Quick;
///
/// // Extended check
/// let opt = CheckTableOption::Extended;
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum CheckTableOption {
	/// Check for version compatibility
	ForUpgrade,
	/// Quick check, skip scanning rows for incorrect links
	Quick,
	/// Fast check, check only tables that haven't been closed properly
	Fast,
	/// Medium check (default), scan rows to verify deleted links are valid
	#[default]
	Medium,
	/// Extended check, do a full key lookup for all keys
	Extended,
	/// Check only tables that have been changed since last check or not closed properly
	Changed,
}

#[cfg(test)]
mod tests {
	use super::*;
	use rstest::*;

	// VacuumOption tests
	#[rstest]
	fn test_vacuum_option_default() {
		let opt = VacuumOption::new();
		assert!(!opt.full);
		assert!(!opt.freeze);
		assert!(!opt.verbose);
		assert!(!opt.analyze);
	}

	#[rstest]
	fn test_vacuum_option_full() {
		let opt = VacuumOption::new().full(true);
		assert!(opt.full);
		assert!(!opt.freeze);
		assert!(!opt.verbose);
		assert!(!opt.analyze);
	}

	#[rstest]
	fn test_vacuum_option_freeze() {
		let opt = VacuumOption::new().freeze(true);
		assert!(!opt.full);
		assert!(opt.freeze);
		assert!(!opt.verbose);
		assert!(!opt.analyze);
	}

	#[rstest]
	fn test_vacuum_option_verbose() {
		let opt = VacuumOption::new().verbose(true);
		assert!(!opt.full);
		assert!(!opt.freeze);
		assert!(opt.verbose);
		assert!(!opt.analyze);
	}

	#[rstest]
	fn test_vacuum_option_analyze() {
		let opt = VacuumOption::new().analyze(true);
		assert!(!opt.full);
		assert!(!opt.freeze);
		assert!(!opt.verbose);
		assert!(opt.analyze);
	}

	#[rstest]
	fn test_vacuum_option_combined() {
		let opt = VacuumOption::new()
			.full(true)
			.freeze(true)
			.verbose(true)
			.analyze(true);
		assert!(opt.full);
		assert!(opt.freeze);
		assert!(opt.verbose);
		assert!(opt.analyze);
	}

	// AnalyzeTable tests
	#[rstest]
	fn test_analyze_table_basic() {
		let tbl = AnalyzeTable::new("users");
		assert_eq!(tbl.table.to_string(), "users");
		assert!(tbl.columns.is_empty());
	}

	#[rstest]
	fn test_analyze_table_with_column() {
		let tbl = AnalyzeTable::new("users").add_column("email");
		assert_eq!(tbl.table.to_string(), "users");
		assert_eq!(tbl.columns.len(), 1);
		assert_eq!(tbl.columns[0].to_string(), "email");
	}

	#[rstest]
	fn test_analyze_table_with_multiple_columns() {
		let tbl = AnalyzeTable::new("users")
			.add_column("email")
			.add_column("name")
			.add_column("age");
		assert_eq!(tbl.table.to_string(), "users");
		assert_eq!(tbl.columns.len(), 3);
		assert_eq!(tbl.columns[0].to_string(), "email");
		assert_eq!(tbl.columns[1].to_string(), "name");
		assert_eq!(tbl.columns[2].to_string(), "age");
	}

	// OptimizeTableOption tests
	#[rstest]
	fn test_optimize_table_option_default() {
		let opt = OptimizeTableOption::new();
		assert!(!opt.no_write_to_binlog);
		assert!(!opt.local);
	}

	#[rstest]
	fn test_optimize_table_option_no_write_to_binlog() {
		let opt = OptimizeTableOption::new().no_write_to_binlog(true);
		assert!(opt.no_write_to_binlog);
		assert!(!opt.local);
	}

	#[rstest]
	fn test_optimize_table_option_local() {
		let opt = OptimizeTableOption::new().local(true);
		assert!(!opt.no_write_to_binlog);
		assert!(opt.local);
	}

	// RepairTableOption tests
	#[rstest]
	fn test_repair_table_option_default() {
		let opt = RepairTableOption::new();
		assert!(!opt.no_write_to_binlog);
		assert!(!opt.local);
		assert!(!opt.quick);
		assert!(!opt.extended);
		assert!(!opt.use_frm);
	}

	#[rstest]
	fn test_repair_table_option_quick() {
		let opt = RepairTableOption::new().quick(true);
		assert!(opt.quick);
		assert!(!opt.extended);
	}

	#[rstest]
	fn test_repair_table_option_extended() {
		let opt = RepairTableOption::new().extended(true);
		assert!(!opt.quick);
		assert!(opt.extended);
	}

	#[rstest]
	fn test_repair_table_option_use_frm() {
		let opt = RepairTableOption::new().use_frm(true);
		assert!(opt.use_frm);
	}

	// CheckTableOption tests
	#[rstest]
	fn test_check_table_option_default() {
		let opt = CheckTableOption::default();
		assert!(matches!(opt, CheckTableOption::Medium));
	}

	#[rstest]
	fn test_check_table_option_variants() {
		assert!(matches!(
			CheckTableOption::ForUpgrade,
			CheckTableOption::ForUpgrade
		));
		assert!(matches!(CheckTableOption::Quick, CheckTableOption::Quick));
		assert!(matches!(CheckTableOption::Fast, CheckTableOption::Fast));
		assert!(matches!(CheckTableOption::Medium, CheckTableOption::Medium));
		assert!(matches!(
			CheckTableOption::Extended,
			CheckTableOption::Extended
		));
		assert!(matches!(
			CheckTableOption::Changed,
			CheckTableOption::Changed
		));
	}
}