drizzle-mysql 0.2.0

A type-safe SQL query builder 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
use crate::{common::MySQLSchemaType, helpers, values::MySQLValue};
use drizzle_core::{SQL, SQLTable, ToSQL, Token};

use super::ExecutableState;

pub use drizzle_core::builder::{
    AsCteState, SelectFromSet, SelectGroupSet, SelectInitial, SelectJoinSet, SelectLimitSet,
    SelectOffsetSet, SelectOrderSet, SelectSetOpSet, SelectWhereSet,
};

/// Marker for a SELECT after its single HAVING clause.
#[derive(Debug, Clone, Copy, Default)]
pub struct SelectHavingSet;

impl ExecutableState for SelectHavingSet {}
impl drizzle_core::GroupByApplied for SelectHavingSet {}
impl AsCteState for SelectHavingSet {}

/// Marker for a base table that already carries one MySQL index hint.
///
/// MySQL rejects some mixed hint kinds for the same scope. Keeping the chosen
/// kind in the select state prevents an invalid second hint from being added.
#[doc(hidden)]
#[derive(Debug, Clone, Copy, Default)]
pub struct SelectIndexHintSet<Kind>(core::marker::PhantomData<Kind>);

impl<Kind> ExecutableState for SelectIndexHintSet<Kind> {}
impl<Kind> AsCteState for SelectIndexHintSet<Kind> {}
impl<Kind> drizzle_core::JoinAllowed for SelectIndexHintSet<Kind> {}
impl<Kind> drizzle_core::GroupByAllowed for SelectIndexHintSet<Kind> {}

/// Marker for the MySQL `FOR UPDATE` lock strength.
#[doc(hidden)]
#[derive(Debug, Clone, Copy, Default)]
pub struct ForUpdate;

/// Marker for the MySQL `FOR SHARE` lock strength.
#[doc(hidden)]
#[derive(Debug, Clone, Copy, Default)]
pub struct ForShare;

/// Marker for a locking read without a wait modifier.
#[doc(hidden)]
#[derive(Debug, Clone, Copy, Default)]
pub struct Wait;

/// Marker for a locking read with `NOWAIT`.
#[doc(hidden)]
#[derive(Debug, Clone, Copy, Default)]
pub struct NoWait;

/// Marker for a locking read with `SKIP LOCKED`.
#[doc(hidden)]
#[derive(Debug, Clone, Copy, Default)]
pub struct SkipLocked;

/// Terminal MySQL locking-read state.
#[derive(Debug, Clone, Copy, Default)]
pub struct SelectForSet<Strength, Modifier = Wait>(core::marker::PhantomData<(Strength, Modifier)>);

impl<Strength, Modifier> ExecutableState for SelectForSet<Strength, Modifier> {}

#[doc(hidden)]
pub trait SelectWhereAllowed {}
impl SelectWhereAllowed for SelectFromSet {}
impl<Kind> SelectWhereAllowed for SelectIndexHintSet<Kind> {}
impl SelectWhereAllowed for SelectJoinSet {}

#[doc(hidden)]
pub trait SelectLimitAllowed {}
impl SelectLimitAllowed for SelectFromSet {}
impl<Kind> SelectLimitAllowed for SelectIndexHintSet<Kind> {}
impl SelectLimitAllowed for SelectJoinSet {}
impl SelectLimitAllowed for SelectWhereSet {}
impl SelectLimitAllowed for SelectGroupSet {}
impl SelectLimitAllowed for SelectHavingSet {}
impl SelectLimitAllowed for SelectOrderSet {}
impl SelectLimitAllowed for SelectSetOpSet {}

#[doc(hidden)]
pub trait SelectOrderAllowed {}
impl SelectOrderAllowed for SelectFromSet {}
impl<Kind> SelectOrderAllowed for SelectIndexHintSet<Kind> {}
impl SelectOrderAllowed for SelectJoinSet {}
impl SelectOrderAllowed for SelectWhereSet {}
impl SelectOrderAllowed for SelectGroupSet {}
impl SelectOrderAllowed for SelectHavingSet {}

#[doc(hidden)]
pub trait SelectOffsetAllowed {}
impl SelectOffsetAllowed for SelectFromSet {}
impl<Kind> SelectOffsetAllowed for SelectIndexHintSet<Kind> {}
impl SelectOffsetAllowed for SelectJoinSet {}
impl SelectOffsetAllowed for SelectWhereSet {}
impl SelectOffsetAllowed for SelectGroupSet {}
impl SelectOffsetAllowed for SelectHavingSet {}
impl SelectOffsetAllowed for SelectOrderSet {}
impl SelectOffsetAllowed for SelectSetOpSet {}

#[doc(hidden)]
pub trait SetOperationAllowed {}
impl SetOperationAllowed for SelectFromSet {}
impl<Kind> SetOperationAllowed for SelectIndexHintSet<Kind> {}
impl SetOperationAllowed for SelectJoinSet {}
impl SetOperationAllowed for SelectWhereSet {}
impl SetOperationAllowed for SelectGroupSet {}
impl SetOperationAllowed for SelectHavingSet {}
impl SetOperationAllowed for SelectOrderSet {}
impl SetOperationAllowed for SelectLimitSet {}
impl SetOperationAllowed for SelectOffsetSet {}
impl SetOperationAllowed for SelectSetOpSet {}

/// Typed MySQL `SELECT` builder.
pub type SelectBuilder<'a, Schema, State, Table = (), Marker = (), Row = (), Grouped = ()> =
    super::QueryBuilder<'a, Schema, State, Table, Marker, Row, Grouped>;

mod private {
    use super::{
        SelectForSet, SelectFromSet, SelectGroupSet, SelectHavingSet, SelectIndexHintSet,
        SelectJoinSet, SelectLimitSet, SelectOffsetSet, SelectOrderSet, SelectSetOpSet,
        SelectWhereSet,
    };

    pub trait SealedSelect {}

    pub trait Prepare {}
    pub trait Completed: super::ExecutableState {}

    impl Prepare for SelectFromSet {}
    impl<Kind> Prepare for SelectIndexHintSet<Kind> {}
    impl Prepare for SelectJoinSet {}
    impl Prepare for SelectWhereSet {}
    impl Prepare for SelectGroupSet {}
    impl Prepare for SelectHavingSet {}
    impl Prepare for SelectOrderSet {}
    impl Prepare for SelectLimitSet {}
    impl Prepare for SelectOffsetSet {}
    impl Prepare for SelectSetOpSet {}
    impl<Strength, Modifier> Prepare for SelectForSet<Strength, Modifier> {}

    impl Completed for SelectFromSet {}
    impl<Kind> Completed for SelectIndexHintSet<Kind> {}
    impl Completed for SelectJoinSet {}
    impl Completed for SelectWhereSet {}
    impl Completed for SelectGroupSet {}
    impl Completed for SelectHavingSet {}
    impl Completed for SelectOrderSet {}
    impl Completed for SelectLimitSet {}
    impl Completed for SelectOffsetSet {}
    impl Completed for SelectSetOpSet {}
}

impl<'a, S, State, T, M, R, G> SelectBuilder<'a, S, State, T, M, R, G>
where
    State: private::Prepare,
{
    /// Compiles named or anonymous placeholders into MySQL's ordered
    /// positional bind plan after validating scope and grouping.
    #[must_use]
    pub fn prepare<ScopeProof, AggProof>(
        &self,
    ) -> drizzle_core::prepared::PreparedStatement<'a, MySQLValue<'a>>
    where
        M: drizzle_core::row::MarkerScopeValidFor<ScopeProof>
            + drizzle_core::row::MarkerAggValidFor<G, AggProof>,
    {
        self.prepared_statement()
    }
}

impl<'a, S, M> SelectBuilder<'a, S, SelectInitial, (), M> {
    /// Sets the source selected from by this query.
    #[allow(clippy::type_complexity)]
    pub fn from<T>(
        self,
        table: T,
    ) -> SelectBuilder<
        'a,
        S,
        SelectFromSet,
        T,
        drizzle_core::Scoped<M, drizzle_core::Cons<T, drizzle_core::Nil>>,
        <M as drizzle_core::ResolveRow<T>>::Row,
    >
    where
        T: ToSQL<'a, MySQLValue<'a>>,
        M: drizzle_core::ResolveRow<T>,
    {
        SelectBuilder::from_sql(self.sql.append(helpers::from(table)))
    }
}

impl<'a, S, T, M, R, G> SelectBuilder<'a, S, SelectFromSet, T, M, R, G>
where
    T: crate::traits::MySQLTable<'a>,
{
    /// Advises MySQL to consider this table's generated index.
    #[must_use]
    pub fn use_index<Indexes>(
        self,
        indexes: Indexes,
    ) -> SelectBuilder<'a, S, SelectIndexHintSet<helpers::UseIndex>, T, M, R, G>
    where
        Indexes: helpers::IndexHintList<'a, T>,
    {
        SelectBuilder::from_sql(self.sql.append(
            helpers::index_hint::<T, Indexes, helpers::UseIndex>(&indexes),
        ))
    }

    /// Advises MySQL to strongly prefer this table's generated index.
    #[must_use]
    pub fn force_index<Indexes>(
        self,
        indexes: Indexes,
    ) -> SelectBuilder<'a, S, SelectIndexHintSet<helpers::ForceIndex>, T, M, R, G>
    where
        Indexes: helpers::IndexHintList<'a, T>,
    {
        SelectBuilder::from_sql(self.sql.append(helpers::index_hint::<
            T,
            Indexes,
            helpers::ForceIndex,
        >(&indexes)))
    }

    /// Advises MySQL not to use this table's generated index.
    #[must_use]
    pub fn ignore_index<Indexes>(
        self,
        indexes: Indexes,
    ) -> SelectBuilder<'a, S, SelectIndexHintSet<helpers::IgnoreIndex>, T, M, R, G>
    where
        Indexes: helpers::IndexHintList<'a, T>,
    {
        SelectBuilder::from_sql(self.sql.append(helpers::index_hint::<
            T,
            Indexes,
            helpers::IgnoreIndex,
        >(&indexes)))
    }
}

macro_rules! join_on_method {
    ($name:ident, $join:expr, $row_trait:ident) => {
        #[doc = concat!("Adds a typed `", stringify!($name), "` join.")]
        #[allow(clippy::type_complexity)]
        pub fn $name<J: helpers::JoinArg<'a, T>>(
            self,
            arg: J,
        ) -> SelectBuilder<
            'a,
            S,
            SelectJoinSet,
            J::JoinedTable,
            <M as drizzle_core::ScopePush<J::JoinedTable>>::Out,
            <M as drizzle_core::$row_trait<R, J::JoinedTable>>::NewRow,
            G,
        >
        where
            M: drizzle_core::$row_trait<R, J::JoinedTable>
                + drizzle_core::ScopePush<J::JoinedTable>,
        {
            SelectBuilder::from_sql(self.sql.append(arg.into_join_sql($join)))
        }
    };
}

impl<'a, S, State, T, M, R, G> SelectBuilder<'a, S, State, T, M, R, G>
where
    State: drizzle_core::JoinAllowed,
{
    join_on_method!(join, drizzle_core::Join::new(), AfterJoin);
    join_on_method!(inner_join, drizzle_core::Join::new().inner(), AfterJoin);
    join_on_method!(left_join, drizzle_core::Join::new().left(), AfterLeftJoin);
    join_on_method!(
        left_outer_join,
        drizzle_core::Join::new().left().outer(),
        AfterLeftJoin
    );
    join_on_method!(
        right_join,
        drizzle_core::Join::new().right(),
        AfterRightJoin
    );
    join_on_method!(
        right_outer_join,
        drizzle_core::Join::new().right().outer(),
        AfterRightJoin
    );

    /// Adds a cross join.
    ///
    /// A bare source renders `CROSS JOIN`. For backwards compatibility,
    /// `(source, predicate)` renders the equivalent `INNER JOIN ... ON ...`.
    #[allow(clippy::type_complexity)]
    pub fn cross_join<Arg: helpers::CrossJoinArg<'a, T>>(
        self,
        arg: Arg,
    ) -> SelectBuilder<
        'a,
        S,
        SelectJoinSet,
        Arg::JoinedTable,
        <M as drizzle_core::ScopePush<Arg::JoinedTable>>::Out,
        <M as drizzle_core::AfterJoin<R, Arg::JoinedTable>>::NewRow,
        G,
    >
    where
        M: drizzle_core::AfterJoin<R, Arg::JoinedTable> + drizzle_core::ScopePush<Arg::JoinedTable>,
    {
        SelectBuilder::from_sql(self.sql.append(arg.into_cross_join_sql()))
    }

    /// Adds an INNER JOIN LATERAL clause.
    #[allow(clippy::type_complexity)]
    pub fn inner_join_lateral<Arg>(
        self,
        arg: Arg,
    ) -> SelectBuilder<
        'a,
        S,
        SelectJoinSet,
        Arg::JoinedTable,
        <M as drizzle_core::ScopePush<Arg::JoinedTable>>::Out,
        <M as drizzle_core::AfterJoin<R, Arg::JoinedTable>>::NewRow,
        G,
    >
    where
        Arg: drizzle_core::LateralArg<'a, MySQLValue<'a>>,
        M: drizzle_core::AfterJoin<R, Arg::JoinedTable> + drizzle_core::ScopePush<Arg::JoinedTable>,
    {
        SelectBuilder::from_sql(
            self.sql
                .append(arg.into_lateral_sql(drizzle_core::Join::new().inner())),
        )
    }

    /// Adds a LEFT JOIN LATERAL clause.
    #[allow(clippy::type_complexity)]
    pub fn left_join_lateral<Arg, SelectionProof>(
        self,
        arg: Arg,
    ) -> SelectBuilder<
        'a,
        S,
        SelectJoinSet,
        Arg::JoinedTable,
        <M as drizzle_core::ScopePush<Arg::JoinedTable>>::Out,
        <M as drizzle_core::AfterLeftJoin<R, Arg::JoinedTable>>::NewRow,
        G,
    >
    where
        Arg: drizzle_core::LateralArg<'a, MySQLValue<'a>>,
        M: drizzle_core::AfterLeftJoin<R, Arg::JoinedTable>
            + drizzle_core::ScopePush<Arg::JoinedTable>
            + drizzle_core::LeftLateralSelection<SelectionProof>,
    {
        SelectBuilder::from_sql(
            self.sql
                .append(arg.into_lateral_sql(drizzle_core::Join::new().left())),
        )
    }

    /// Adds a CROSS JOIN LATERAL clause without an ON condition.
    #[allow(clippy::type_complexity)]
    pub fn cross_join_lateral<Source>(
        self,
        source: Source,
    ) -> SelectBuilder<
        'a,
        S,
        SelectJoinSet,
        Source::JoinedTable,
        <M as drizzle_core::ScopePush<Source::JoinedTable>>::Out,
        <M as drizzle_core::AfterJoin<R, Source::JoinedTable>>::NewRow,
        G,
    >
    where
        Source: drizzle_core::LateralSource<'a, MySQLValue<'a>>,
        M: drizzle_core::AfterJoin<R, Source::JoinedTable>
            + drizzle_core::ScopePush<Source::JoinedTable>,
    {
        SelectBuilder::from_sql(self.sql.append(source.into_cross_lateral_sql()))
    }
}

impl<'a, S, State, T, M, R, G> SelectBuilder<'a, S, State, T, M, R, G>
where
    State: SelectWhereAllowed,
{
    /// Filters rows before grouping and projection.
    pub fn r#where<E>(self, condition: E) -> SelectBuilder<'a, S, SelectWhereSet, T, M, R, G>
    where
        E: drizzle_core::expr::Expr<'a, MySQLValue<'a>>,
        E::SQLType: drizzle_core::types::BooleanLike,
    {
        SelectBuilder::from_sql(self.sql.append(helpers::r#where(condition)))
    }
}

impl<'a, S, State, T, M, R, G> SelectBuilder<'a, S, State, T, M, R, G>
where
    State: drizzle_core::GroupByAllowed,
{
    /// Groups rows by the supplied expressions.
    pub fn group_by<Gr>(
        self,
        columns: Gr,
    ) -> SelectBuilder<'a, S, SelectGroupSet, T, M, R, Gr::Columns>
    where
        Gr: drizzle_core::IntoGroupBy<'a, MySQLValue<'a>>,
    {
        SelectBuilder::from_sql(self.sql.append(helpers::group_by_expr(columns)))
    }
}

impl<'a, S, State, T, M, R, G> SelectBuilder<'a, S, State, T, M, R, G>
where
    State: drizzle_core::HavingAllowed,
{
    /// Filters grouped rows.
    pub fn having<E>(self, condition: E) -> SelectBuilder<'a, S, SelectHavingSet, T, M, R, G>
    where
        E: drizzle_core::expr::Expr<'a, MySQLValue<'a>>,
        E::SQLType: drizzle_core::types::BooleanLike,
    {
        SelectBuilder::from_sql(self.sql.append(helpers::having(condition)))
    }
}

impl<'a, S, State, T, M, R, G> SelectBuilder<'a, S, State, T, M, R, G>
where
    State: SelectOrderAllowed,
{
    /// Orders the selected rows.
    pub fn order_by<O>(self, order: O) -> SelectBuilder<'a, S, SelectOrderSet, T, M, R, G>
    where
        O: ToSQL<'a, MySQLValue<'a>>,
    {
        SelectBuilder::from_sql(self.sql.append(helpers::order_by(order)))
    }
}

impl<'a, S, T, M, R, G> SelectBuilder<'a, S, SelectSetOpSet, T, M, R, G> {
    /// Orders a compound query by an output expression or alias.
    pub fn order_by<O, Proof>(self, order: O) -> SelectBuilder<'a, S, SelectOrderSet, T, M, R, G>
    where
        O: helpers::SetOrderBy<'a, M, T, Proof>,
    {
        let order = helpers::SetOrderBy::into_set_order_sql(order);
        SelectBuilder::from_sql(
            self.sql
                .append(SQL::from_iter([Token::ORDER, Token::BY]).append(order)),
        )
    }
}

impl<'a, S, State, T, M, R, G> SelectBuilder<'a, S, State, T, M, R, G>
where
    State: SelectLimitAllowed,
{
    /// Limits the number of returned rows.
    #[track_caller]
    pub fn limit<P>(self, limit: P) -> SelectBuilder<'a, S, SelectLimitSet, T, M, R, G>
    where
        P: drizzle_core::PaginationArg<'a, MySQLValue<'a>>,
    {
        SelectBuilder::from_sql(self.sql.append(helpers::limit(limit)))
    }
}

impl<'a, S, State, T, M, R, G> SelectBuilder<'a, S, State, T, M, R, G>
where
    State: SelectOffsetAllowed,
{
    /// Skips rows without an explicit limit.
    ///
    /// MySQL has no bare `OFFSET`, so this renders `LIMIT 9223372036854775807`
    /// before the offset (see [`drizzle_core::helpers::MYSQL_UNBOUNDED_LIMIT`]).
    #[track_caller]
    pub fn offset<P>(self, offset: P) -> SelectBuilder<'a, S, SelectOffsetSet, T, M, R, G>
    where
        P: drizzle_core::PaginationArg<'a, MySQLValue<'a>>,
    {
        SelectBuilder::from_sql(self.sql.append(helpers::standalone_offset(offset)))
    }
}

impl<'a, S, T, M, R, G> SelectBuilder<'a, S, SelectLimitSet, T, M, R, G> {
    /// Skips rows after an explicit limit.
    #[track_caller]
    pub fn offset<P>(self, offset: P) -> SelectBuilder<'a, S, SelectOffsetSet, T, M, R, G>
    where
        P: drizzle_core::PaginationArg<'a, MySQLValue<'a>>,
    {
        SelectBuilder::from_sql(self.sql.append(drizzle_core::helpers::offset(offset)))
    }
}

/// Select states on which MySQL permits a terminal locking clause.
#[doc(hidden)]
pub trait LockingReadAllowed {}

impl LockingReadAllowed for SelectFromSet {}
impl<Kind> LockingReadAllowed for SelectIndexHintSet<Kind> {}
impl LockingReadAllowed for SelectJoinSet {}
impl LockingReadAllowed for SelectWhereSet {}
impl LockingReadAllowed for SelectGroupSet {}
impl LockingReadAllowed for SelectHavingSet {}
impl LockingReadAllowed for SelectOrderSet {}
impl LockingReadAllowed for SelectLimitSet {}
impl LockingReadAllowed for SelectOffsetSet {}

impl<'a, S, State, T, M, R, G> SelectBuilder<'a, S, State, T, M, R, G>
where
    State: LockingReadAllowed,
{
    /// Locks matching rows for update.
    #[must_use]
    pub fn for_update(self) -> SelectBuilder<'a, S, SelectForSet<ForUpdate>, T, M, R, G> {
        SelectBuilder::from_sql(
            self.sql
                .push(drizzle_core::Token::FOR)
                .push(drizzle_core::Token::UPDATE),
        )
    }

    /// Acquires shared locks on matching rows.
    #[must_use]
    pub fn for_share(self) -> SelectBuilder<'a, S, SelectForSet<ForShare>, T, M, R, G> {
        SelectBuilder::from_sql(
            self.sql
                .push(drizzle_core::Token::FOR)
                .push(drizzle_core::Token::SHARE),
        )
    }
}

impl<'a, S, Strength, T, M, R, G> SelectBuilder<'a, S, SelectForSet<Strength, Wait>, T, M, R, G> {
    /// Fails immediately instead of waiting for a conflicting row lock.
    #[must_use]
    pub fn nowait(self) -> SelectBuilder<'a, S, SelectForSet<Strength, NoWait>, T, M, R, G> {
        SelectBuilder::from_sql(self.sql.push(drizzle_core::Token::NOWAIT))
    }

    /// Skips rows currently held by another transaction.
    #[must_use]
    pub fn skip_locked(
        self,
    ) -> SelectBuilder<'a, S, SelectForSet<Strength, SkipLocked>, T, M, R, G> {
        SelectBuilder::from_sql(
            self.sql
                .push(drizzle_core::Token::SKIP)
                .push(drizzle_core::Token::LOCKED),
        )
    }
}

impl<'a, S, State, T, M, R, G> SelectBuilder<'a, S, State, T, M, R, G>
where
    State: AsCteState + ExecutableState,
    T: SQLTable<'a, MySQLSchemaType, MySQLValue<'a>>,
{
    /// Converts this query into a named common table expression.
    #[must_use]
    pub fn into_cte<Tag: drizzle_core::Tag + 'static>(
        self,
    ) -> super::CTEView<'a, <T as SQLTable<'a, MySQLSchemaType, MySQLValue<'a>>>::Aliased<Tag>, Self>
    {
        super::CTEView::new(
            <T as SQLTable<'a, MySQLSchemaType, MySQLValue<'a>>>::alias::<Tag>(),
            Tag::NAME,
            self,
        )
    }
}

impl<'a, S, State, T, M, R, G> SelectBuilder<'a, S, State, T, M, R, G>
where
    State: ExecutableState,
{
    /// Names this completed projection for use as a derived table.
    ///
    /// # Panics
    ///
    /// Panics when the projection contains duplicate output names. Name a
    /// computed expression with [`drizzle_core::expr::NamedExt::named`] to
    /// make each output unique.
    #[must_use]
    pub fn alias<Tag, ScopeProof, AggProof>(
        self,
        _tag: Tag,
    ) -> drizzle_core::Derived<
        'a,
        MySQLValue<'a>,
        Tag,
        <M as drizzle_core::DerivedSelection<'a, MySQLValue<'a>, MySQLSchemaType, T>>::Projection,
        Self,
    >
    where
        Tag: drizzle_core::Tag,
        M: drizzle_core::DerivedSelection<'a, MySQLValue<'a>, MySQLSchemaType, T>
            + drizzle_core::row::MarkerScopeValidFor<ScopeProof>
            + drizzle_core::row::MarkerAggValidFor<G, AggProof>,
        <M as drizzle_core::DerivedSelection<'a, MySQLValue<'a>, MySQLSchemaType, T>>::Projection:
            drizzle_core::DerivedProjection<Tag>,
    {
        // SAFETY: The executable-state, scope, aggregate, and projection
        // bounds above prove that this query matches the derived projection.
        unsafe { drizzle_core::Derived::new_unchecked(self) }
    }
}

macro_rules! set_operation {
    ($name:ident, $token:expr, $all:expr) => {
        #[doc = concat!("Combines this query with another using `", stringify!($name), "`.")]
        pub fn $name(
            self,
            other: impl IntoSelectQuery<'a, S, R>,
        ) -> SelectBuilder<'a, S, SelectSetOpSet, T, M, R, G> {
            SelectBuilder::from_sql(helpers::set_op(
                self.sql,
                $token,
                $all,
                other.into_select_query().into_select_sql(),
            ))
        }
    };
}

impl<'a, S, State, T, M, R, G> SelectBuilder<'a, S, State, T, M, R, G>
where
    State: SetOperationAllowed,
{
    set_operation!(union, drizzle_core::Token::UNION, false);
    set_operation!(union_all, drizzle_core::Token::UNION, true);
    set_operation!(intersect, drizzle_core::Token::INTERSECT, false);
    set_operation!(intersect_all, drizzle_core::Token::INTERSECT, true);
    set_operation!(except, drizzle_core::Token::EXCEPT, false);
    set_operation!(except_all, drizzle_core::Token::EXCEPT, true);
}

/// A completed SELECT with the inferred row shape `R`.
///
/// This trait is sealed so INSERT ... SELECT and set operations cannot accept
/// arbitrary SQL or DML builders.
#[doc(hidden)]
pub trait CompletedSelect<'a, S, R>: private::SealedSelect {
    type Marker;
    type Grouped;

    fn into_select_sql(self) -> drizzle_core::SQL<'a, MySQLValue<'a>>;
}

/// Safe extension seam for driver wrappers around a completed MySQL select.
///
/// Implementations must unwrap to the sealed [`CompletedSelect`] type; they cannot
/// manufacture an arbitrary SQL fragment or row marker.
#[doc(hidden)]
pub trait IntoSelectQuery<'a, S, R> {
    type Marker;
    type Grouped;
    type Select: CompletedSelect<'a, S, R, Marker = Self::Marker, Grouped = Self::Grouped>;

    fn into_select_query(self) -> Self::Select;
}

impl<'a, S, State, T, M, R, G> private::SealedSelect for SelectBuilder<'a, S, State, T, M, R, G> where
    State: private::Completed
{
}

impl<'a, S, State, T, M, R, G> CompletedSelect<'a, S, R> for SelectBuilder<'a, S, State, T, M, R, G>
where
    State: private::Completed,
{
    type Marker = M;
    type Grouped = G;

    fn into_select_sql(self) -> drizzle_core::SQL<'a, MySQLValue<'a>> {
        self.sql
    }
}

impl<'a, S, State, T, M, R, G> IntoSelectQuery<'a, S, R> for SelectBuilder<'a, S, State, T, M, R, G>
where
    State: private::Completed,
{
    type Marker = M;
    type Grouped = G;
    type Select = Self;

    fn into_select_query(self) -> Self::Select {
        self
    }
}

impl<'a, S, State, T, M, R, G> drizzle_core::expr::Expr<'a, MySQLValue<'a>>
    for SelectBuilder<'a, S, State, T, M, R, G>
where
    State: private::Completed,
    M: drizzle_core::expr::SubqueryType<'a, MySQLValue<'a>>,
{
    type SQLType = <M as drizzle_core::expr::SubqueryType<'a, MySQLValue<'a>>>::SQLType;
    type Nullable = drizzle_core::expr::Null;
    type Aggregate = drizzle_core::expr::Scalar;
}