squeal 0.1.1

A SQL query builder library for Rust
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
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
#![forbid(unsafe_code)]
//! # squeal - Type-Safe SQL Query Builder for PostgreSQL
//!
//! **squeal** is a lightweight, type-safe SQL query builder for Rust that targets PostgreSQL.
//! It provides both fluent builder APIs and direct struct construction for maximum flexibility.
//!
//! ## Philosophy
//!
//! - **Keep it simple** - No macros, no magic, just Rust types
//! - **Type-safe** - Catch errors at compile time, not runtime
//! - **Escape hatches** - Custom operators and raw SQL when you need them
//! - **Zero overhead** - Queries are built at compile time
//!
//! ## Quick Start
//!
//! ```
//! use squeal::*;
//!
//! // Build a SELECT query with the fluent API
//! let mut qb = Q();
//! let query = qb.select(vec!["id", "name", "email"])
//!     .from("users")
//!     .where_(eq("active", "true"))
//!     .order_by(vec![OrderedColumn::Desc("created_at")])
//!     .limit(10)
//!     .build();
//!
//! assert_eq!(
//!     query.sql(),
//!     "SELECT id, name, email FROM users WHERE active = true ORDER BY created_at DESC LIMIT 10"
//! );
//! ```
//!
//! ## Core Concepts
//!
//! ### Fluent Builders
//!
//! All query types have builder functions with short, memorable names:
//! - `Q()` - Build SELECT queries
//! - `I(table)` - Build INSERT statements
//! - `U(table)` - Build UPDATE statements
//! - `D(table)` - Build DELETE statements
//! - `T(table)` - Build CREATE/DROP TABLE DDL
//!
//! ### Terms and Conditions
//!
//! The `Term` enum represents WHERE clause conditions. Use helper functions
//! for common patterns:
//!
//! ```
//! use squeal::*;
//!
//! // Simple equality: active = true
//! let simple = eq("active", "true");
//!
//! // Complex conditions with AND/OR
//! let complex = Term::Condition(
//!     Box::new(eq("status", "'published'")),
//!     Op::And,
//!     Box::new(gt("views", "1000"))
//! );
//!
//! let mut qb = Q();
//! let query = qb.select(vec!["title", "views"])
//!     .from("posts")
//!     .where_(complex)
//!     .build();
//!
//! assert_eq!(
//!     query.sql(),
//!     "SELECT title, views FROM posts WHERE status = 'published' AND views > 1000"
//! );
//! ```
//!
//! ## Advanced Features
//!
//! ### JOINs
//!
//! All standard JOIN types are supported:
//!
//! ```
//! use squeal::*;
//!
//! let mut qb = Q();
//! let query = qb.select(vec!["users.name", "COUNT(orders.id) as order_count"])
//!     .from("users")
//!     .left_join("orders", eq("users.id", "orders.user_id"))
//!     .group_by(vec!["users.id", "users.name"])
//!     .order_by(vec![OrderedColumn::Desc("order_count")])
//!     .build();
//!
//! assert_eq!(
//!     query.sql(),
//!     "SELECT users.name, COUNT(orders.id) as order_count FROM users LEFT JOIN orders ON users.id = orders.user_id GROUP BY users.id, users.name ORDER BY order_count DESC"
//! );
//! ```
//!
//! ### Common Table Expressions (WITH)
//!
//! Build complex queries with CTEs for better readability:
//!
//! ```
//! use squeal::*;
//!
//! // Define a CTE to find high-value customers
//! let mut qb1 = Q();
//! let high_value = qb1.select(vec!["user_id", "SUM(total) as lifetime_value"])
//!     .from("orders")
//!     .group_by(vec!["user_id"])
//!     .having(gt("SUM(total)", "1000"))
//!     .build();
//!
//! // Use the CTE in the main query
//! let mut qb2 = Q();
//! let query = qb2.with("high_value_customers", high_value)
//!     .select(vec!["users.name", "hvc.lifetime_value"])
//!     .from("users")
//!     .inner_join("high_value_customers hvc", eq("users.id", "hvc.user_id"))
//!     .order_by(vec![OrderedColumn::Desc("hvc.lifetime_value")])
//!     .build();
//!
//! assert_eq!(
//!     query.sql(),
//!     "WITH high_value_customers AS (SELECT user_id, SUM(total) as lifetime_value FROM orders GROUP BY user_id HAVING SUM(total) > 1000) SELECT users.name, hvc.lifetime_value FROM users INNER JOIN high_value_customers hvc ON users.id = hvc.user_id ORDER BY hvc.lifetime_value DESC"
//! );
//! ```
//!
//! ### UPSERT (INSERT ... ON CONFLICT)
//!
//! Handle unique constraint violations gracefully:
//!
//! ```
//! use squeal::*;
//!
//! // Insert or update user login count
//! let mut ib = I("users");
//! let upsert = ib.columns(vec!["email", "name", "login_count"])
//!     .values(vec!["'alice@example.com'", "'Alice'", "'1'"])
//!     .on_conflict_do_update(
//!         vec!["email"],
//!         vec![
//!             ("login_count", "users.login_count + 1"),
//!             ("last_login", "NOW()")
//!         ]
//!     )
//!     .returning(Columns::Selected(vec!["id", "login_count"]))
//!     .build();
//!
//! assert_eq!(
//!     upsert.sql(),
//!     "INSERT INTO users (email, name, login_count) VALUES ('alice@example.com', 'Alice', '1') ON CONFLICT (email) DO UPDATE SET login_count = users.login_count + 1, last_login = NOW() RETURNING id, login_count"
//! );
//! ```
//!
//! ### RETURNING Clauses
//!
//! Get auto-generated values and track modifications:
//!
//! ```
//! use squeal::*;
//!
//! // Get the ID of newly inserted row
//! let mut ib = I("posts");
//! let insert = ib.columns(vec!["title", "content"])
//!     .values(vec!["'Hello World'", "'My first post'"])
//!     .returning(Columns::Selected(vec!["id", "created_at"]))
//!     .build();
//!
//! assert_eq!(
//!     insert.sql(),
//!     "INSERT INTO posts (title, content) VALUES ('Hello World', 'My first post') RETURNING id, created_at"
//! );
//!
//! // Track what was deleted
//! let mut db = D("old_logs");
//! let delete = db.where_(lt("created_at", "NOW() - INTERVAL '90 days'"))
//!     .returning(Columns::Selected(vec!["id", "created_at"]))
//!     .build();
//!
//! assert_eq!(
//!     delete.sql(),
//!     "DELETE FROM old_logs WHERE created_at < NOW() - INTERVAL '90 days' RETURNING id, created_at"
//! );
//! ```
//!
//! ## Parameterized Queries
//!
//! Use the `Parameterized` trait for prepared statements:
//!
//! ```
//! use squeal::*;
//!
//! let mut qb = Q();
//! let param = qb.param();
//! let query = qb.select(vec!["*"])
//!     .from("users")
//!     .where_(eq("email", &param))
//!     .build();
//!
//! assert_eq!(query.sql(), "SELECT * FROM users WHERE email = $1");
//! ```
//!
//! ## More Information
//!
//! See the [README](https://github.com/upside-down-research/squeal) for complete documentation
//! and examples.

pub mod queries;

pub use queries::create_table::{CreateTable, T, TableBuilder};
pub use queries::delete::{D, Delete, DeleteBuilder};
pub use queries::drop_table::DropTable;
pub use queries::insert::{I, Insert, InsertBuilder, InsertSource, OnConflict};
pub use queries::select::{Columns, Select, SelectExpression};
pub use queries::update::{U, Update, UpdateBuilder};

/// The Sql trait is implemented by all objects that can be used in a query.
/// It provides a single method, sql(), that returns a String.
///
/// This is not intended to be implemented by the user.
pub trait Sql {
    /// Returns the fragment which will be assembled in the given query.
    fn sql(&self) -> String;
}

/// The Build trait is used by the XBuilder structs to build the X struct.
/// This is a means of providing a nice factory/fluent interface.
pub trait Build {
    /// Builds the final struct from the builder
    fn build(&self) -> Self;
}

/// The Parameterized trait provides PostgreSQL parameter placeholder generation.
/// Implemented by all query builder structs to provide consistent param() API.
pub trait Parameterized {
    /// Returns the next PostgreSQL parameter placeholder ($1, $2, $3, etc.)
    /// Each builder maintains its own isolated counter.
    fn param(&mut self) -> String;
}
#[derive(Clone)]
pub enum Distinct<'a> {
    All,
    On(Vec<&'a str>),
}

/// The Op enum is used to specify the operator in a condition.
/// It is used in the Term struct.
///
/// The Op::O variant is an escape hatch to allow you to use any operator you want.
#[derive(Clone)]
pub enum Op<'a> {
    /// Logical AND operator
    And,
    /// Logical OR operator
    Or,
    /// Equality operator (=)
    Equals,
    /// Not equals operator (!=)
    NotEquals,
    /// Greater than operator (>)
    GreaterThan,
    /// Less than operator (<)
    LessThan,
    /// Greater than or equal operator (>=)
    GreaterOrEqual,
    /// Less than or equal operator (<=)
    LessOrEqual,
    /// LIKE operator for pattern matching
    Like,
    /// IN operator for set membership
    In,
    /// EXISTS operator for subquery existence testing
    Exists,
    /// NOT EXISTS operator for subquery non-existence testing
    NotExists,
    /// ANY operator for comparing against any value in a subquery
    Any,
    /// ALL operator for comparing against all values in a subquery
    All,
    /// Custom operator escape hatch
    O(&'a str),
}

impl<'a> Sql for Op<'a> {
    fn sql(&self) -> String {
        match &self {
            Op::And => "AND",
            Op::Or => "OR",
            Op::Equals => "=",
            Op::NotEquals => "!=",
            Op::GreaterThan => ">",
            Op::LessThan => "<",
            Op::GreaterOrEqual => ">=",
            Op::LessOrEqual => "<=",
            Op::Like => "LIKE",
            Op::In => "IN",
            Op::Exists => "EXISTS",
            Op::NotExists => "NOT EXISTS",
            Op::Any => "ANY",
            Op::All => "ALL",
            Op::O(s) => s,
        }
        .to_string()
    }
}

/// The Term enum is used to specify a condition in a query (WHERE clause).
/// It is used in the Query struct.
///
/// A Term can be an atom, a condition, parentheses or null. Observant minds might notice that
/// this is a fragment of a grammar and simply a reified syntax tree.
///
/// # Examples
///
/// Atom:
/// ```
/// use squeal::*;
/// let result = Term::Atom("a").sql();
/// assert_eq!(result, "a");
/// ```
///
/// A number of different conditions and complex combinations:
/// ```
/// use squeal::*;
/// let result = Term::Condition(
///    Box::new(Term::Atom("a")),
///   Op::O("<>"),
/// Box::new(Term::Atom("b")),
/// ).sql();
/// assert_eq!(result, "a <> b");
/// ```
/// An example setting up `a = b AND (c = d OR e <> f)`:
///
/// ```
/// use squeal::*;
/// let result = Term::Condition(
///   Box::new(Term::Atom("a")),
/// Op::Equals,
/// Box::new(Term::Condition(
///   Box::new(Term::Atom("b")),
/// Op::And,
/// Box::new(Term::Parens(Box::new(Term::Condition(
///  Box::new(Term::Atom("c")),
/// Op::Equals,
/// Box::new(Term::Condition(
/// Box::new(Term::Atom("d")),
/// Op::Or,
/// Box::new(Term::Atom("e")),
/// ))))))))).sql();
/// assert_eq!(result, "a = b AND (c = d OR e)");
/// ```
///
///
///
#[derive(Clone)]
pub enum Term<'a> {
    /// An atom is a single identifier.
    Atom(&'a str),
    /// A condition is a combination of two terms and an operator.
    Condition(Box<Term<'a>>, Op<'a>, Box<Term<'a>>),
    /// A parenthesized term.
    Parens(Box<Term<'a>>),
    /// A null term.
    Null,
    /// A subquery that can be used in WHERE clauses
    Subquery(Box<Query<'a>>),
    Not(Box<Term<'a>>),
    Cast(Box<Term<'a>>, &'a str),
    PgCast(Box<Term<'a>>, &'a str),
    Case(CaseExpression<'a>),
    Coalesce(Vec<Term<'a>>),
    NullIf(Box<Term<'a>>, Box<Term<'a>>),
    Concat(Vec<Term<'a>>),
    Substring(Box<Term<'a>>, Option<Box<Term<'a>>>, Option<Box<Term<'a>>>),
    Upper(Box<Term<'a>>),
    Lower(Box<Term<'a>>),
    Now,
    CurrentDate,
    Interval(&'a str),
    DateAdd(Box<Term<'a>>, Box<Term<'a>>),
    DateSub(Box<Term<'a>>, Box<Term<'a>>),
}

impl<'a> Sql for CaseExpression<'a> {
    fn sql(&self) -> String {
        let mut s = "CASE".to_string();
        for wt in &self.when_thens {
            s.push_str(&format!(" WHEN {} THEN {}", wt.when.sql(), wt.then.sql()));
        }
        if let Some(et) = &self.else_term {
            s.push_str(&format!(" ELSE {}", et.sql()));
        }
        s.push_str(" END");
        s
    }
}

#[derive(Clone)]
pub struct WhenThen<'a> {
    pub when: Term<'a>,
    pub then: Term<'a>,
}

#[derive(Clone)]
pub struct CaseExpression<'a> {
    pub when_thens: Vec<WhenThen<'a>>,
    pub else_term: Option<Box<Term<'a>>>,
}

impl<'a> Sql for Term<'a> {
    fn sql(&self) -> String {
        match &self {
            Term::Atom(s) => s.to_string(),
            Term::Condition(t1, op, t2) => format!("{} {} {}", t1.sql(), op.sql(), t2.sql()),
            Term::Null => "".to_string(),
            Term::Parens(t) => format!("({})", t.sql()),
            Term::Subquery(q) => format!("({})", q.sql()),
            Term::Not(t) => format!("NOT {}", t.sql()),
            Term::Cast(t, ty) => format!("CAST({} AS {})", t.sql(), ty),
            Term::PgCast(t, ty) => format!("{}::{}", t.sql(), ty),
            Term::Case(c) => c.sql(),
            Term::Coalesce(terms) => {
                let terms_sql: Vec<String> = terms.iter().map(|t| t.sql()).collect();
                format!("COALESCE({})", terms_sql.join(", "))
            }
            Term::NullIf(t1, t2) => format!("NULLIF({}, {})", t1.sql(), t2.sql()),
            Term::Concat(terms) => {
                let terms_sql: Vec<String> = terms.iter().map(|t| t.sql()).collect();
                format!("CONCAT({})", terms_sql.join(", "))
            }
            Term::Substring(t, from, for_) => {
                let mut s = format!("SUBSTRING({}", t.sql());
                if let Some(f) = from {
                    s.push_str(&format!(" FROM {}", f.sql()));
                }
                if let Some(f) = for_ {
                    s.push_str(&format!(" FOR {}", f.sql()));
                }
                s.push(')');
                s
            }
            Term::Upper(t) => format!("UPPER({})", t.sql()),
            Term::Lower(t) => format!("LOWER({})", t.sql()),
            Term::Now => "NOW()".to_string(),
            Term::CurrentDate => "CURRENT_DATE".to_string(),
            Term::Interval(s) => format!("INTERVAL '{}'", s),
            Term::DateAdd(t1, t2) => format!("{} + {}", t1.sql(), t2.sql()),
            Term::DateSub(t1, t2) => format!("{} - {}", t1.sql(), t2.sql()),
        }
    }
}

// Helper functions for building WHERE clauses ergonomically

/// Creates an equality condition (=)
pub fn eq<'a>(left: &'a str, right: &'a str) -> Term<'a> {
    Term::Condition(
        Box::new(Term::Atom(left)),
        Op::Equals,
        Box::new(Term::Atom(right)),
    )
}

/// Creates a not-equals condition (!=)
pub fn ne<'a>(left: &'a str, right: &'a str) -> Term<'a> {
    Term::Condition(
        Box::new(Term::Atom(left)),
        Op::NotEquals,
        Box::new(Term::Atom(right)),
    )
}

/// Creates a greater-than condition (>)
pub fn gt<'a>(left: &'a str, right: &'a str) -> Term<'a> {
    Term::Condition(
        Box::new(Term::Atom(left)),
        Op::GreaterThan,
        Box::new(Term::Atom(right)),
    )
}

/// Creates a less-than condition (<)
pub fn lt<'a>(left: &'a str, right: &'a str) -> Term<'a> {
    Term::Condition(
        Box::new(Term::Atom(left)),
        Op::LessThan,
        Box::new(Term::Atom(right)),
    )
}

/// Creates a greater-than-or-equal condition (>=)
pub fn gte<'a>(left: &'a str, right: &'a str) -> Term<'a> {
    Term::Condition(
        Box::new(Term::Atom(left)),
        Op::GreaterOrEqual,
        Box::new(Term::Atom(right)),
    )
}

/// Creates a less-than-or-equal condition (<=)
pub fn lte<'a>(left: &'a str, right: &'a str) -> Term<'a> {
    Term::Condition(
        Box::new(Term::Atom(left)),
        Op::LessOrEqual,
        Box::new(Term::Atom(right)),
    )
}

/// Creates a LIKE condition
pub fn like<'a>(left: &'a str, right: &'a str) -> Term<'a> {
    Term::Condition(
        Box::new(Term::Atom(left)),
        Op::Like,
        Box::new(Term::Atom(right)),
    )
}

/// Combines two terms with AND
pub fn and<'a>(left: Term<'a>, right: Term<'a>) -> Term<'a> {
    Term::Condition(Box::new(left), Op::And, Box::new(right))
}

/// Combines two terms with OR
pub fn or<'a>(left: Term<'a>, right: Term<'a>) -> Term<'a> {
    Term::Condition(Box::new(left), Op::Or, Box::new(right))
}

/// Negates a term with NOT
pub fn not<'a>(term: Term<'a>) -> Term<'a> {
    Term::Not(Box::new(term))
}

/// Creates a CAST expression
pub fn cast<'a>(term: Term<'a>, type_name: &'a str) -> Term<'a> {
    Term::Cast(Box::new(term), type_name)
}

/// Creates a PostgreSQL-style CAST expression (::)
pub fn pg_cast<'a>(term: Term<'a>, type_name: &'a str) -> Term<'a> {
    Term::PgCast(Box::new(term), type_name)
}

/// Creates a CASE expression
pub fn case<'a>(when_thens: Vec<WhenThen<'a>>, else_term: Option<Term<'a>>) -> Term<'a> {
    Term::Case(CaseExpression {
        when_thens,
        else_term: else_term.map(Box::new),
    })
}

/// Creates a COALESCE expression
pub fn coalesce<'a>(terms: Vec<Term<'a>>) -> Term<'a> {
    Term::Coalesce(terms)
}

/// Creates a NULLIF expression
pub fn nullif<'a>(left: Term<'a>, right: Term<'a>) -> Term<'a> {
    Term::NullIf(Box::new(left), Box::new(right))
}

/// Creates a CONCAT expression
pub fn concat<'a>(terms: Vec<Term<'a>>) -> Term<'a> {
    Term::Concat(terms)
}

/// Creates a SUBSTRING expression
pub fn substring<'a>(term: Term<'a>, from: Option<Term<'a>>, for_: Option<Term<'a>>) -> Term<'a> {
    Term::Substring(Box::new(term), from.map(Box::new), for_.map(Box::new))
}

/// Creates a UPPER expression
pub fn upper<'a>(term: Term<'a>) -> Term<'a> {
    Term::Upper(Box::new(term))
}

/// Creates a LOWER expression
pub fn lower<'a>(term: Term<'a>) -> Term<'a> {
    Term::Lower(Box::new(term))
}

/// Creates a NOW() expression
pub fn now<'a>() -> Term<'a> {
    Term::Now
}

/// Creates a CURRENT_DATE expression
pub fn current_date<'a>() -> Term<'a> {
    Term::CurrentDate
}

/// Creates an INTERVAL expression
pub fn interval<'a>(s: &'a str) -> Term<'a> {
    Term::Interval(s)
}

/// Creates a date addition expression
pub fn date_add<'a>(left: Term<'a>, right: Term<'a>) -> Term<'a> {
    Term::DateAdd(Box::new(left), Box::new(right))
}

/// Creates a date subtraction expression
pub fn date_sub<'a>(left: Term<'a>, right: Term<'a>) -> Term<'a> {
    Term::DateSub(Box::new(left), Box::new(right))
}

/// Wraps a term in parentheses
pub fn parens<'a>(term: Term<'a>) -> Term<'a> {
    Term::Parens(Box::new(term))
}

// Convenience functions for common SQL patterns

/// Creates an IN clause
/// Example: in_("status", vec!["'active'", "'pending'"]) => "status IN ('active', 'pending')"
pub fn in_<'a>(column: &'a str, values: Vec<&'a str>) -> Term<'a> {
    let values_str = values.join(", ");
    Term::Atom(Box::leak(
        format!("{} IN ({})", column, values_str).into_boxed_str(),
    ))
}

/// Creates a BETWEEN clause
/// Example: between("age", "18", "65") => "age BETWEEN 18 AND 65"
pub fn between<'a>(column: &'a str, low: &'a str, high: &'a str) -> Term<'a> {
    Term::Atom(Box::leak(
        format!("{} BETWEEN {} AND {}", column, low, high).into_boxed_str(),
    ))
}

/// Creates an IS NULL condition
/// Example: is_null("deleted_at") => "deleted_at IS NULL"
pub fn is_null<'a>(column: &'a str) -> Term<'a> {
    Term::Atom(Box::leak(format!("{} IS NULL", column).into_boxed_str()))
}

/// Creates an IS NOT NULL condition
/// Example: is_not_null("created_at") => "created_at IS NOT NULL"
pub fn is_not_null<'a>(column: &'a str) -> Term<'a> {
    Term::Atom(Box::leak(
        format!("{} IS NOT NULL", column).into_boxed_str(),
    ))
}

// Nested query helpers

/// Creates an EXISTS condition with a subquery
/// Example: exists(subquery) => "EXISTS (SELECT ...)"
pub fn exists<'a>(subquery: Query<'a>) -> Term<'a> {
    let sql = format!("EXISTS ({})", subquery.sql());
    Term::Atom(Box::leak(sql.into_boxed_str()))
}

/// Creates a NOT EXISTS condition with a subquery
/// Example: not_exists(subquery) => "NOT EXISTS (SELECT ...)"
pub fn not_exists<'a>(subquery: Query<'a>) -> Term<'a> {
    let sql = format!("NOT EXISTS ({})", subquery.sql());
    Term::Atom(Box::leak(sql.into_boxed_str()))
}

/// Creates an IN condition with a subquery
/// Example: in_subquery("user_id", subquery) => "user_id IN (SELECT ...)"
pub fn in_subquery<'a>(column: &'a str, subquery: Query<'a>) -> Term<'a> {
    Term::Condition(
        Box::new(Term::Atom(column)),
        Op::In,
        Box::new(Term::Subquery(Box::new(subquery))),
    )
}

/// Creates a comparison with ANY (subquery)
/// Example: any("price", Op::GreaterThan, subquery) => "price > ANY (SELECT ...)"
pub fn any<'a>(column: &'a str, op: Op<'a>, subquery: Query<'a>) -> Term<'a> {
    let sql = format!("{} {} ANY ({})", column, op.sql(), subquery.sql());
    Term::Atom(Box::leak(sql.into_boxed_str()))
}

/// Creates a comparison with ALL (subquery)
/// Example: all("price", Op::LessThan, subquery) => "price < ALL (SELECT ...)"
pub fn all<'a>(column: &'a str, op: Op<'a>, subquery: Query<'a>) -> Term<'a> {
    let sql = format!("{} {} ALL ({})", column, op.sql(), subquery.sql());
    Term::Atom(Box::leak(sql.into_boxed_str()))
}

// PostgreSQL parameter helpers

/// Returns a PostgreSQL parameter placeholder
/// Example: p(1) => "$1", p(2) => "$2"
pub fn p(n: usize) -> String {
    format!("${}", n)
}

/// PostgreSQL parameter counter for auto-sequencing
/// Automatically generates $1, $2, $3, etc. as you call seq()
///
/// # Example
/// ```
/// use squeal::*;
/// let mut pg = PgParams::new();
/// let p1 = pg.seq();  // "$1"
/// let p2 = pg.seq();  // "$2"
/// let mut qb = Q();
/// let query = qb
///     .select(vec!["*"])
///     .from("users")
///     .where_(and(
///         eq("id", &p1),
///         eq("status", &p2)
///     ))
///     .build();
/// assert_eq!(query.sql(), "SELECT * FROM users WHERE id = $1 AND status = $2");
/// ```
pub struct PgParams {
    count: usize,
}

impl PgParams {
    /// Creates a new parameter counter starting at 0
    pub fn new() -> Self {
        PgParams { count: 0 }
    }

    /// Returns the next parameter placeholder ($1, $2, $3, etc.)
    pub fn seq(&mut self) -> String {
        self.count += 1;
        format!("${}", self.count)
    }
}

impl Default for PgParams {
    fn default() -> Self {
        Self::new()
    }
}

/// The Having struct is used to specify the having clause in a query.
/// It is used in the Query struct.
///
/// It is constructed with a Term, similar to a Where clause.
#[derive(Clone)]
pub struct Having<'a> {
    /// The condition for the HAVING clause
    pub term: Term<'a>,
}

impl<'a> Having<'a> {
    /// Creates a new Having clause with the given term
    ///
    /// # Example
    /// ```
    /// use squeal::*;
    /// let having = Having::new(gt("count", "5"));
    /// assert_eq!(having.sql(), "count > 5");
    /// ```
    pub fn new(t: Term<'a>) -> Having<'a> {
        Having { term: t }
    }
}

impl<'a> Sql for Having<'a> {
    fn sql(&self) -> String {
        self.term.sql().to_string()
    }
}

/// The OrderedColumn enum is used to specify the order by clause in a query.
/// It is used in the OrderBy struct.
/// It is used to specify the columns, and optionally, whether they are ascending or descending.
#[derive(Clone)]
pub enum OrderedColumn<'a> {
    /// Ascending order
    Asc(&'a str),
    /// Descending order
    Desc(&'a str),
}

/// The OrderBy struct is used to specify the order by clause in a query.
/// It is used in the Query struct.
/// It is used to specify the columns, and optionally, whether they are ascending or descending.
/// Each column can be ascending or descending
#[derive(Clone)]
pub struct OrderBy<'a> {
    /// List of columns with their sort order
    pub columns: Vec<OrderedColumn<'a>>,
}

impl<'a> Sql for OrderBy<'a> {
    fn sql(&self) -> String {
        let mut result = "ORDER BY ".to_string();
        let mut first = true;
        for c in &self.columns {
            if !first {
                result.push_str(", ");
            }
            first = false;
            match c {
                OrderedColumn::Asc(s) => result.push_str(&format!("{} ASC", s)),
                OrderedColumn::Desc(s) => result.push_str(&format!("{} DESC", s)),
            }
        }
        result
    }
}

/// The FromSource enum represents the source of data in a FROM clause.
/// It can be either a simple table name or a subquery with an alias.
///
/// # Examples
///
/// Simple table:
/// ```
/// use squeal::*;
/// let from = FromSource::Table("users");
/// assert_eq!(from.sql(), "users");
/// ```
///
/// Subquery with alias:
/// ```
/// use squeal::*;
/// let subquery = Query {
///     with_clause: None,
///     select: Some(Select::new(Columns::Star, None)),
///     from: Some(FromSource::Table("users")),
///     joins: vec![],
///     where_clause: None,
///     group_by: None,
///     having: None,
///     order_by: None,
///     limit: None,
///     offset: None,
///     for_update: false,
/// };
/// let from = FromSource::Subquery(Box::new(subquery), "u");
/// assert_eq!(from.sql(), "(SELECT * FROM users) AS u");
/// ```
#[derive(Clone)]
pub enum FromSource<'a> {
    /// A simple table name
    Table(&'a str),
    /// A subquery with an alias
    Subquery(Box<Query<'a>>, &'a str),
}

impl<'a> Sql for FromSource<'a> {
    fn sql(&self) -> String {
        match self {
            FromSource::Table(table) => table.to_string(),
            FromSource::Subquery(query, alias) => format!("({}) AS {}", query.sql(), alias),
        }
    }
}

/// Join type for SQL JOIN clauses
#[derive(Clone)]
pub enum JoinType {
    /// INNER JOIN
    Inner,
    /// LEFT JOIN (LEFT OUTER JOIN)
    Left,
    /// RIGHT JOIN (RIGHT OUTER JOIN)
    Right,
    /// FULL JOIN (FULL OUTER JOIN)
    Full,
    /// CROSS JOIN
    Cross,
}

impl Sql for JoinType {
    fn sql(&self) -> String {
        match self {
            JoinType::Inner => "INNER JOIN",
            JoinType::Left => "LEFT JOIN",
            JoinType::Right => "RIGHT JOIN",
            JoinType::Full => "FULL JOIN",
            JoinType::Cross => "CROSS JOIN",
        }
        .to_string()
    }
}

/// Represents a JOIN clause in a SQL query
#[derive(Clone)]
pub struct Join<'a> {
    /// The type of join (INNER, LEFT, RIGHT, FULL, CROSS)
    pub join_type: JoinType,
    /// The table or subquery to join
    pub source: FromSource<'a>,
    /// The join condition (ON clause), None for CROSS JOIN
    pub on: Option<Term<'a>>,
}

impl<'a> Sql for Join<'a> {
    fn sql(&self) -> String {
        let mut result = format!("{} {}", self.join_type.sql(), self.source.sql());
        if let Some(condition) = &self.on {
            result.push_str(&format!(" ON {}", condition.sql()));
        }
        result
    }
}

/// Represents a Common Table Expression (CTE) in a WITH clause
#[derive(Clone)]
pub struct Cte<'a> {
    /// The name of the CTE
    pub name: &'a str,
    /// The query that defines the CTE
    pub query: Box<Query<'a>>,
}

impl<'a> Sql for Cte<'a> {
    fn sql(&self) -> String {
        format!("{} AS ({})", self.name, self.query.sql())
    }
}

/// The Query struct is the top-level object that represents a query.
/// The user is expected to construct the Query object and then call the sql() method to get the
/// SQL string.
///
#[derive(Clone)]
pub struct Query<'a> {
    /// WITH clause (Common Table Expressions)
    pub with_clause: Option<Vec<Cte<'a>>>,
    /// The select clause.
    pub select: Option<Select<'a>>,
    /// The table name for the select clause.
    pub from: Option<FromSource<'a>>,
    /// JOIN clauses
    pub joins: Vec<Join<'a>>,
    /// The conditions for the where clause, if it exists.
    pub where_clause: Option<Term<'a>>,
    /// The columns to group by, if any.
    pub group_by: Option<Vec<&'a str>>,
    /// The having clause conditions, if any.
    pub having: Option<Having<'a>>,
    /// The order by clause, if any.
    pub order_by: Option<OrderBy<'a>>,
    /// The maximum number of rows to return.
    pub limit: Option<u64>,
    /// The number of rows to skip.
    pub offset: Option<u64>,
    /// Whether to lock rows with FOR UPDATE.
    pub for_update: bool,
}

/// The QueryBuilder struct is a fluent interface for building a Query.
/// It is not intended to be used directly, but rather through the Q() function.
/// See the integration_test.rs for an example of usage.
pub struct QueryBuilder<'a> {
    /// WITH clause (Common Table Expressions)
    pub with_clause: Option<Vec<Cte<'a>>>,
    /// The select clause
    pub select: Option<Select<'a>>,
    /// The table to select from
    pub from: Option<FromSource<'a>>,
    /// JOIN clauses
    pub joins: Vec<Join<'a>>,
    /// The WHERE clause conditions
    pub where_clause: Option<Term<'a>>,
    /// The columns to GROUP BY
    pub group_by: Option<Vec<&'a str>>,
    /// The HAVING clause conditions
    pub having: Option<Having<'a>>,
    /// The ORDER BY clause
    pub order_by: Option<OrderBy<'a>>,
    /// The LIMIT value
    pub limit: Option<u64>,
    /// The OFFSET value
    pub offset: Option<u64>,
    /// Whether to use FOR UPDATE
    pub for_update: bool,
    /// PostgreSQL parameter counter
    pub params: PgParams,
}

/// The Q function is a fluent interface for building a Query.
/// The user is expected to construct the Query object and then call the sql() method to get the SQL string.
/// The goal is any valid construction of a QueryBuilder is a valid Query and will, at least, syntactically, be valid SQL.
#[allow(non_snake_case)]
pub fn Q<'a>() -> QueryBuilder<'a> {
    QueryBuilder {
        with_clause: None,
        select: None,
        from: None,
        joins: Vec::new(),
        where_clause: None,
        group_by: None,
        having: None,
        order_by: None,
        limit: None,
        offset: None,
        for_update: false,
        params: PgParams::new(),
    }
}

impl<'a> QueryBuilder<'a> {
    /// Builds the final Query from this builder
    ///
    /// # Example
    /// ```
    /// use squeal::*;
    /// let mut qb = Q();
    /// let query = qb.select(vec!["*"]).from("users").build();
    /// assert_eq!(query.sql(), "SELECT * FROM users");
    /// ```
    pub fn build(&self) -> Query<'a> {
        Query {
            with_clause: self.with_clause.clone(),
            select: self.select.clone(),
            from: self.from.clone(),
            joins: self.joins.clone(),
            where_clause: self.where_clause.clone(),
            group_by: self.group_by.clone(),
            having: self.having.clone(),
            order_by: self.order_by.clone(),
            limit: self.limit,
            offset: self.offset,
            for_update: self.for_update,
        }
    }

    /// Adds a WITH clause (Common Table Expression)
    ///
    /// # Example
    /// ```
    /// use squeal::*;
    /// let cte_query = Query {
    ///     with_clause: None,
    ///     select: Some(Select::new(Columns::Selected(vec!["id", "name"]), None)),
    ///     from: Some(FromSource::Table("users")),
    ///     joins: vec![],
    ///     where_clause: Some(eq("active", "true")),
    ///     group_by: None,
    ///     having: None,
    ///     order_by: None,
    ///     limit: None,
    ///     offset: None,
    ///     for_update: false,
    /// };
    /// let mut qb = Q();
    /// let query = qb.with("active_users", cte_query)
    ///     .select(vec!["*"])
    ///     .from("active_users")
    ///     .build();
    /// assert_eq!(query.sql(), "WITH active_users AS (SELECT id, name FROM users WHERE active = true) SELECT * FROM active_users");
    /// ```
    pub fn with(&'a mut self, name: &'a str, query: Query<'a>) -> &'a mut QueryBuilder<'a> {
        let cte = Cte {
            name,
            query: Box::new(query),
        };
        match &mut self.with_clause {
            None => self.with_clause = Some(vec![cte]),
            Some(ctes) => ctes.push(cte),
        }
        self
    }

    /// Sets the columns to SELECT
    ///
    /// # Example
    /// ```
    /// use squeal::*;
    /// let mut qb = Q();
    /// let query = qb.select(vec!["id", "name"]).from("users").build();
    /// assert_eq!(query.sql(), "SELECT id, name FROM users");
    /// ```
    pub fn select(&'a mut self, cols: Vec<&'a str>) -> &'a mut QueryBuilder<'a> {
        self.select = Some(Select::new(Columns::Selected(cols), None));
        self
    }

    /// Sets the SELECT clause with expressions (columns and/or subqueries)
    ///
    /// # Example
    /// ```
    /// use squeal::*;
    /// let subquery = Query {
    ///     with_clause: None,
    ///     select: Some(Select::new(Columns::Selected(vec!["COUNT(*)"]), None)),
    ///     from: Some(FromSource::Table("orders")),
    ///     joins: vec![],
    ///     where_clause: None,
    ///     group_by: None,
    ///     having: None,
    ///     order_by: None,
    ///     limit: None,
    ///     offset: None,
    ///     for_update: false,
    /// };
    /// let mut qb = Q();
    /// let query = qb.select_expressions(vec![
    ///     SelectExpression::Column("id"),
    ///     SelectExpression::Subquery(Box::new(subquery), Some("order_count"))
    /// ]).from("users").build();
    /// assert_eq!(query.sql(), "SELECT id, (SELECT COUNT(*) FROM orders) AS order_count FROM users");
    /// ```
    pub fn select_expressions(
        &'a mut self,
        exprs: Vec<SelectExpression<'a>>,
    ) -> &'a mut QueryBuilder<'a> {
        self.select = Some(Select::new(Columns::Expressions(exprs), None));
        self
    }
    /// Sets the SELECT clause to be DISTINCT
    pub fn distinct(&'a mut self) -> &'a mut QueryBuilder<'a> {
        if let Some(s) = &mut self.select {
            s.distinct = Some(Distinct::All);
        }
        self
    }

    /// Sets the SELECT clause to be DISTINCT ON the given columns
    pub fn distinct_on(&'a mut self, cols: Vec<&'a str>) -> &'a mut QueryBuilder<'a> {
        if let Some(s) = &mut self.select {
            s.distinct = Some(Distinct::On(cols));
        }
        self
    }
    /// Sets the table to SELECT FROM
    ///
    /// # Example
    /// ```
    /// use squeal::*;
    /// let mut qb = Q();
    /// let query = qb.select(vec!["*"]).from("products").build();
    /// assert_eq!(query.sql(), "SELECT * FROM products");
    /// ```
    pub fn from(&'a mut self, table: &'a str) -> &'a mut QueryBuilder<'a> {
        self.from = Some(FromSource::Table(table));
        self
    }

    /// Sets a subquery as the FROM source
    ///
    /// # Example
    /// ```
    /// use squeal::*;
    /// let subquery = Query {
    ///     with_clause: None,
    ///     select: Some(Select::new(Columns::Star, None)),
    ///     from: Some(FromSource::Table("users")),
    ///     joins: vec![],
    ///     where_clause: None,
    ///     group_by: None,
    ///     having: None,
    ///     order_by: None,
    ///     limit: None,
    ///     offset: None,
    ///     for_update: false,
    /// };
    /// let mut qb = Q();
    /// let query = qb.select(vec!["*"]).from_subquery(subquery, "u").build();
    /// assert_eq!(query.sql(), "SELECT * FROM (SELECT * FROM users) AS u");
    /// ```
    pub fn from_subquery(
        &'a mut self,
        subquery: Query<'a>,
        alias: &'a str,
    ) -> &'a mut QueryBuilder<'a> {
        self.from = Some(FromSource::Subquery(Box::new(subquery), alias));
        self
    }

    /// Adds an INNER JOIN clause
    ///
    /// # Example
    /// ```
    /// use squeal::*;
    /// let mut qb = Q();
    /// let query = qb.select(vec!["users.name", "orders.total"])
    ///     .from("users")
    ///     .inner_join("orders", eq("users.id", "orders.user_id"))
    ///     .build();
    /// assert_eq!(query.sql(), "SELECT users.name, orders.total FROM users INNER JOIN orders ON users.id = orders.user_id");
    /// ```
    pub fn inner_join(&'a mut self, table: &'a str, on: Term<'a>) -> &'a mut QueryBuilder<'a> {
        self.joins.push(Join {
            join_type: JoinType::Inner,
            source: FromSource::Table(table),
            on: Some(on),
        });
        self
    }

    /// Adds a LEFT JOIN clause
    ///
    /// # Example
    /// ```
    /// use squeal::*;
    /// let mut qb = Q();
    /// let query = qb.select(vec!["users.name", "orders.total"])
    ///     .from("users")
    ///     .left_join("orders", eq("users.id", "orders.user_id"))
    ///     .build();
    /// assert_eq!(query.sql(), "SELECT users.name, orders.total FROM users LEFT JOIN orders ON users.id = orders.user_id");
    /// ```
    pub fn left_join(&'a mut self, table: &'a str, on: Term<'a>) -> &'a mut QueryBuilder<'a> {
        self.joins.push(Join {
            join_type: JoinType::Left,
            source: FromSource::Table(table),
            on: Some(on),
        });
        self
    }

    /// Adds a RIGHT JOIN clause
    ///
    /// # Example
    /// ```
    /// use squeal::*;
    /// let mut qb = Q();
    /// let query = qb.select(vec!["users.name", "orders.total"])
    ///     .from("users")
    ///     .right_join("orders", eq("users.id", "orders.user_id"))
    ///     .build();
    /// assert_eq!(query.sql(), "SELECT users.name, orders.total FROM users RIGHT JOIN orders ON users.id = orders.user_id");
    /// ```
    pub fn right_join(&'a mut self, table: &'a str, on: Term<'a>) -> &'a mut QueryBuilder<'a> {
        self.joins.push(Join {
            join_type: JoinType::Right,
            source: FromSource::Table(table),
            on: Some(on),
        });
        self
    }

    /// Adds a FULL JOIN clause
    ///
    /// # Example
    /// ```
    /// use squeal::*;
    /// let mut qb = Q();
    /// let query = qb.select(vec!["users.name", "orders.total"])
    ///     .from("users")
    ///     .full_join("orders", eq("users.id", "orders.user_id"))
    ///     .build();
    /// assert_eq!(query.sql(), "SELECT users.name, orders.total FROM users FULL JOIN orders ON users.id = orders.user_id");
    /// ```
    pub fn full_join(&'a mut self, table: &'a str, on: Term<'a>) -> &'a mut QueryBuilder<'a> {
        self.joins.push(Join {
            join_type: JoinType::Full,
            source: FromSource::Table(table),
            on: Some(on),
        });
        self
    }

    /// Adds a CROSS JOIN clause (no ON condition required)
    ///
    /// # Example
    /// ```
    /// use squeal::*;
    /// let mut qb = Q();
    /// let query = qb.select(vec!["users.name", "colors.name"])
    ///     .from("users")
    ///     .cross_join("colors")
    ///     .build();
    /// assert_eq!(query.sql(), "SELECT users.name, colors.name FROM users CROSS JOIN colors");
    /// ```
    pub fn cross_join(&'a mut self, table: &'a str) -> &'a mut QueryBuilder<'a> {
        self.joins.push(Join {
            join_type: JoinType::Cross,
            source: FromSource::Table(table),
            on: None,
        });
        self
    }

    /// Adds a JOIN clause with a subquery as the source
    ///
    /// # Example
    /// ```
    /// use squeal::*;
    /// let subquery = Query {
    ///     with_clause: None,
    ///     select: Some(Select::new(Columns::Selected(vec!["user_id", "COUNT(*) as order_count"]), None)),
    ///     from: Some(FromSource::Table("orders")),
    ///     joins: vec![],
    ///     where_clause: None,
    ///     group_by: Some(vec!["user_id"]),
    ///     having: None,
    ///     order_by: None,
    ///     limit: None,
    ///     offset: None,
    ///     for_update: false,
    /// };
    /// let mut qb = Q();
    /// let query = qb.select(vec!["users.name", "oc.order_count"])
    ///     .from("users")
    ///     .join_subquery(JoinType::Left, subquery, "oc", eq("users.id", "oc.user_id"))
    ///     .build();
    /// assert_eq!(query.sql(), "SELECT users.name, oc.order_count FROM users LEFT JOIN (SELECT user_id, COUNT(*) as order_count FROM orders GROUP BY user_id) AS oc ON users.id = oc.user_id");
    /// ```
    pub fn join_subquery(
        &'a mut self,
        join_type: JoinType,
        subquery: Query<'a>,
        alias: &'a str,
        on: Term<'a>,
    ) -> &'a mut QueryBuilder<'a> {
        self.joins.push(Join {
            join_type,
            source: FromSource::Subquery(Box::new(subquery), alias),
            on: Some(on),
        });
        self
    }

    /// Sets the WHERE clause
    ///
    /// # Example
    /// ```
    /// use squeal::*;
    /// let mut qb = Q();
    /// let query = qb.select(vec!["*"]).from("users").where_(eq("id", "1")).build();
    /// assert_eq!(query.sql(), "SELECT * FROM users WHERE id = 1");
    /// ```
    pub fn where_(&'a mut self, term: Term<'a>) -> &'a mut QueryBuilder<'a> {
        self.where_clause = Some(term);
        self
    }

    /// Sets WHERE clause only if the Option contains Some value
    /// Useful for conditional/dynamic query building
    pub fn where_opt(&'a mut self, term: Option<Term<'a>>) -> &'a mut QueryBuilder<'a> {
        if let Some(t) = term {
            self.where_clause = Some(t);
        }
        self
    }

    /// Adds a condition to the WHERE clause with AND
    /// If no WHERE clause exists yet, this becomes the first condition
    /// Otherwise, it ANDs the new condition with the existing one
    pub fn and_where(&'a mut self, term: Term<'a>) -> &'a mut QueryBuilder<'a> {
        match &self.where_clause {
            None => self.where_clause = Some(term),
            Some(existing) => {
                self.where_clause = Some(Term::Condition(
                    Box::new(existing.clone()),
                    Op::And,
                    Box::new(term),
                ));
            }
        }
        self
    }

    /// Sets the GROUP BY clause
    ///
    /// # Example
    /// ```
    /// use squeal::*;
    /// let mut qb = Q();
    /// let query = qb.select(vec!["category", "count(*)"]).from("products").group_by(vec!["category"]).build();
    /// assert_eq!(query.sql(), "SELECT category, count(*) FROM products GROUP BY category");
    /// ```
    pub fn group_by(&'a mut self, cols: Vec<&'a str>) -> &'a mut QueryBuilder<'a> {
        self.group_by = Some(cols);
        self
    }
    /// Sets the HAVING clause
    ///
    /// # Example
    /// ```
    /// use squeal::*;
    /// let mut qb = Q();
    /// let query = qb.select(vec!["category", "count(*)"]).from("products").group_by(vec!["category"]).having(gt("count(*)", "5")).build();
    /// assert_eq!(query.sql(), "SELECT category, count(*) FROM products GROUP BY category HAVING count(*) > 5");
    /// ```
    pub fn having(&'a mut self, term: Term<'a>) -> &'a mut QueryBuilder<'a> {
        self.having = Some(Having::new(term));
        self
    }
    /// Sets the ORDER BY clause
    ///
    /// # Example
    /// ```
    /// use squeal::*;
    /// let mut qb = Q();
    /// let query = qb.select(vec!["*"]).from("users").order_by(vec![OrderedColumn::Desc("created_at")]).build();
    /// assert_eq!(query.sql(), "SELECT * FROM users ORDER BY created_at DESC");
    /// ```
    pub fn order_by(&'a mut self, cols: Vec<OrderedColumn<'a>>) -> &'a mut QueryBuilder<'a> {
        self.order_by = Some(OrderBy { columns: cols });
        self
    }
    /// Sets the LIMIT clause
    ///
    /// # Example
    /// ```
    /// use squeal::*;
    /// let mut qb = Q();
    /// let query = qb.select(vec!["*"]).from("users").limit(10).build();
    /// assert_eq!(query.sql(), "SELECT * FROM users LIMIT 10");
    /// ```
    pub fn limit(&'a mut self, limit: u64) -> &'a mut QueryBuilder<'a> {
        self.limit = Some(limit);
        self
    }
    /// Sets the OFFSET clause
    ///
    /// # Example
    /// ```
    /// use squeal::*;
    /// let mut qb = Q();
    /// let query = qb.select(vec!["*"]).from("users").offset(20).build();
    /// assert_eq!(query.sql(), "SELECT * FROM users OFFSET 20");
    /// ```
    pub fn offset(&'a mut self, offset: u64) -> &'a mut QueryBuilder<'a> {
        self.offset = Some(offset);
        self
    }
    /// Adds FOR UPDATE to lock selected rows
    ///
    /// # Example
    /// ```
    /// use squeal::*;
    /// let mut qb = Q();
    /// let query = qb.select(vec!["*"]).from("users").for_update().build();
    /// assert_eq!(query.sql(), "SELECT * FROM users FOR UPDATE");
    /// ```
    pub fn for_update(&'a mut self) -> &'a mut QueryBuilder<'a> {
        self.for_update = true;
        self
    }
}

impl<'a> Parameterized for QueryBuilder<'a> {
    fn param(&mut self) -> String {
        self.params.seq()
    }
}

impl<'a> Sql for Query<'a> {
    fn sql(&self) -> String {
        let mut result = String::new();

        if let Some(ctes) = &self.with_clause {
            result.push_str("WITH ");
            let mut first = true;
            for cte in ctes {
                if !first {
                    result.push_str(", ");
                }
                first = false;
                result.push_str(&cte.sql());
            }
            result.push(' ');
        }

        if let Some(select) = &self.select {
            result.push_str(&format!("SELECT {}", select.sql()));
        }
        if let Some(from) = &self.from {
            result.push_str(&format!(" FROM {}", from.sql()));
        }
        for join in &self.joins {
            result.push_str(&format!(" {}", join.sql()));
        }
        if let Some(conditions) = &self.where_clause {
            result.push_str(&format!(" WHERE {}", conditions.sql()));
        }
        if let Some(group_by) = &self.group_by {
            result.push_str(&format!(" GROUP BY {}", group_by.join(", ")));
        }
        if let Some(having) = &self.having {
            result.push_str(&format!(" HAVING {}", having.sql()));
        }
        if let Some(order_by) = &self.order_by {
            result.push_str(&format!(" {}", order_by.sql()));
        }
        if let Some(limit) = &self.limit {
            result.push_str(&format!(" LIMIT {}", limit));
        }
        if let Some(offset) = &self.offset {
            result.push_str(&format!(" OFFSET {}", offset));
        }
        if self.for_update {
            result.push_str(" FOR UPDATE");
        }
        result
    }
}