gorm 0.1.4

An orm that is simple to use and prevents all mistakes at compile time
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
use super::SqlStatement;
use crate::{
    sql::{
        FieldsConsListItem, Insertable, ParameterBinder, SelectedValues, UniqueConstraint,
        UpdateSet,
    },
    util::{TypedBool, TypedConsListNil, TypedFalse, TypedTrue},
    Table,
};

/// Represents any type of sql insert statement.
pub trait InsertStatement: Sized {
    /// A type identifying the output fields of this insert statement, selected
    /// in its `RETURNING` clause.
    type OutputFields: FieldsConsListItem;

    /// Does this insert statement have a `RETURNING` clause?
    type HasReturningClause: TypedBool;

    /// Does this insert statement have an `ON CONFLICT` clause?
    type HasOnConflictClause: TypedBool;

    /// The insertable which this insert statement inserts.
    type Insertable: Insertable;

    fn get_insertable(&self) -> &Self::Insertable;

    /// Writes the `RETURNING` clause of this insert statement.
    fn write_returning_clause<'s, 'a>(
        &'s self,
        f: &mut String,
        parameter_binder: &mut ParameterBinder<'a>,
    ) -> std::fmt::Result
    where
        's: 'a;

    /// Writes the `ON CONFLICT` clause of this insert statement.
    fn write_on_conflict_clause<'s, 'a>(
        &'s self,
        f: &mut String,
        parameter_binder: &mut ParameterBinder<'a>,
    ) -> std::fmt::Result
    where
        's: 'a;

    /// Writes this insert statement as an sql string.
    fn write_sql_string<'s, 'a>(
        &'s self,
        f: &mut String,
        parameter_binder: &mut ParameterBinder<'a>,
    ) -> std::fmt::Result
    where
        's: 'a,
    {
        use std::fmt::Write;

        write!(
            f,
            "INSERT INTO {}(",
            <<Self::Insertable as Insertable>::Table as Table>::TABLE_NAME,
        )?;
        self.get_insertable().write_value_names(f)?;
        write!(f, ") VALUES(")?;
        self.get_insertable().write_values(f, parameter_binder)?;
        write!(f, ")")?;
        self.write_on_conflict_clause(f, parameter_binder)?;
        self.write_returning_clause(f, parameter_binder)?;

        Ok(())
    }
}

/// Implements the [`SqlStatement`] trait for some type which implements
/// [`InsertStatement`]
macro_rules! impl_sql_statement_for_insert_statement {
    {} => {
        type OutputFields = <Self as InsertStatement>::OutputFields;

        fn write_sql_string<'s, 'a>(
            &'s self,
            f: &mut String,
            parameter_binder: &mut ParameterBinder<'a>,
        ) -> std::fmt::Result
        where
            's: 'a,
        {
            <Self as InsertStatement>::write_sql_string(&self, f, parameter_binder)
        }
    };
}

/// An empty sql insert statement.
///
/// This statement shouldn't be used directly, you should instead use the
/// [`Insertable::insert`] function.
pub struct EmptyInsertStatement<I: Insertable>(I);
impl<I: Insertable> EmptyInsertStatement<I> {
    /// Creates a new sql insert statement which inserts the given insertable.
    pub fn new(insertable: I) -> Self {
        Self(insertable)
    }
}

impl<I: Insertable> InsertStatement for EmptyInsertStatement<I> {
    type HasOnConflictClause = TypedFalse;
    type HasReturningClause = TypedFalse;
    type Insertable = I;
    type OutputFields = TypedConsListNil;

    fn get_insertable(&self) -> &Self::Insertable {
        &self.0
    }

    fn write_returning_clause<'s, 'a>(
        &'s self,
        _f: &mut String,
        _parameter_binder: &mut ParameterBinder<'a>,
    ) -> std::fmt::Result
    where
        's: 'a,
    {
        Ok(())
    }

    fn write_on_conflict_clause<'s, 'a>(
        &'s self,
        _f: &mut String,
        _parameter_binder: &mut ParameterBinder<'a>,
    ) -> std::fmt::Result
    where
        's: 'a,
    {
        Ok(())
    }
}

impl<I: Insertable> SqlStatement for EmptyInsertStatement<I> {
    impl_sql_statement_for_insert_statement! {}
}

/// A wrapper around an sql insert statement which adds a `RETURNING` clause to
/// it.
///
/// This wrapper shouldn't be used directly, you should instead use the
/// [`InsertStatementReturning::returning`] function.
pub struct InsertWithReturningClause<
    S: InsertStatement<HasReturningClause = TypedFalse>,
    R: SelectedValues<<S::Insertable as Insertable>::Table>,
> {
    statement: S,
    returning: R,
}

impl<
    S: InsertStatement<HasReturningClause = TypedFalse>,
    R: SelectedValues<<S::Insertable as Insertable>::Table>,
> InsertStatement for InsertWithReturningClause<S, R>
{
    type HasOnConflictClause = S::HasOnConflictClause;
    type HasReturningClause = TypedTrue;
    type Insertable = S::Insertable;
    type OutputFields = R::Fields;

    fn get_insertable(&self) -> &Self::Insertable {
        self.statement.get_insertable()
    }

    fn write_returning_clause<'s, 'a>(
        &'s self,
        f: &mut String,
        parameter_binder: &mut ParameterBinder<'a>,
    ) -> std::fmt::Result
    where
        's: 'a,
    {
        use std::fmt::Write;

        write!(f, " RETURNING ")?;
        self.returning.write_sql_string(f, parameter_binder)
    }

    fn write_on_conflict_clause<'s, 'a>(
        &'s self,
        f: &mut String,
        parameter_binder: &mut ParameterBinder<'a>,
    ) -> std::fmt::Result
    where
        's: 'a,
    {
        self.statement.write_on_conflict_clause(f, parameter_binder)
    }
}

impl<
    S: InsertStatement<HasReturningClause = TypedFalse>,
    R: SelectedValues<<S::Insertable as Insertable>::Table>,
> SqlStatement for InsertWithReturningClause<S, R>
{
    impl_sql_statement_for_insert_statement! {}
}

/// A trait which allows returning some values from the records inserted by some
/// insert statement.
pub trait InsertStatementReturning: InsertStatement<HasReturningClause = TypedFalse> {
    /// Selects some values to be returned from the records inserted by this
    /// insert statement. To provide a list of values to be returned, use the
    /// [`returning!`] macro.
    ///
    /// [`returning!`]: crate::returning
    fn returning<R: SelectedValues<<Self::Insertable as Insertable>::Table>>(
        self,
        returning: R,
    ) -> InsertWithReturningClause<Self, R> {
        InsertWithReturningClause {
            statement: self,
            returning,
        }
    }
}

impl<T: InsertStatement<HasReturningClause = TypedFalse>> InsertStatementReturning for T {}

/// A wrapper around an sql insert statement which allows adding an `ON
/// CONFLICT` clause to it, by specifying the updates that should be performed
/// on a conflicting row in case of conflict, using the
/// [`InsertWithOnConflictClauseBuilder::do_update`] function.
///
/// This wrapper shouldn't be used directly, you should instead use the
/// [`InsertStatementOnConflict::on_conflict`] function.
pub struct InsertWithOnConflictClauseBuilder<
    S: InsertStatement<HasOnConflictClause = TypedFalse>,
    C: UniqueConstraint<Table = <S::Insertable as Insertable>::Table>,
> {
    statement: S,
    constraint: C,
}

impl<
    S: InsertStatement<HasOnConflictClause = TypedFalse>,
    C: UniqueConstraint<Table = <S::Insertable as Insertable>::Table>,
> InsertWithOnConflictClauseBuilder<S, C>
{
    /// Adds an update set to be applied to a conflicting row in case
    /// of a conflict when inserting this value. To provide an update set to be
    /// applied in case of a conflict, use the [`update_set!`] macro.
    ///
    /// [`update_set!`]: gorm_macros::update_set
    pub fn do_update<U: UpdateSet<UpdateTable = <S::Insertable as Insertable>::Table>>(
        self,
        update_set: U,
    ) -> InsertWithOnConflictClause<S, C, U> {
        InsertWithOnConflictClause {
            statement: self.statement,
            _constraint: self.constraint,
            update_set,
        }
    }
}

/// A trait which allows updating an existing row when a conflict occurs while
/// trying to insert values into some table.
pub trait InsertStatementOnConflict: InsertStatement<HasOnConflictClause = TypedFalse> {
    /// Returns a builder for an on conflict clause, using a unique constraint
    /// to detect the conflict.
    ///
    /// The returned builder allows specifying what updates should be performed
    /// on the conflicting row in case of a conflict, by using the
    /// [`InsertWithOnConflictClauseBuilder::do_update`] function.
    fn on_conflict<C: UniqueConstraint<Table = <Self::Insertable as Insertable>::Table>>(
        self,
        constraint: C,
    ) -> InsertWithOnConflictClauseBuilder<Self, C> {
        InsertWithOnConflictClauseBuilder {
            statement: self,
            constraint,
        }
    }

    /// Adds an `ON CONFLICT DO NOTHING` clause to this insert statement, which
    /// means that if any conflict occurs while inserting this record,
    /// nothing will happen and no error will be returned.
    ///
    /// Please note that adding this `ON CONFLICT DO NOTHING` clause also means that in case of a
    /// conflict, this statement won't return any rows.
    fn on_conflict_do_nothing(self) -> InsertWithOnConflictDoNothingClause<Self> {
        InsertWithOnConflictDoNothingClause { statement: self }
    }
}

impl<T: InsertStatement<HasOnConflictClause = TypedFalse>> InsertStatementOnConflict for T {}

/// A wrapper around an sql insert statement which adds an `ON CONFLICT` clause
/// to it.
///
/// This wrapper shouldn't be used directly, you should instead use the
/// [`InsertStatementOnConflict::on_conflict`], and then the
/// [`InsertWithOnConflictClauseBuilder::do_update`] functions.
pub struct InsertWithOnConflictClause<
    S: InsertStatement<HasOnConflictClause = TypedFalse>,
    C: UniqueConstraint<Table = <S::Insertable as Insertable>::Table>,
    U: UpdateSet<UpdateTable = <S::Insertable as Insertable>::Table>,
> {
    statement: S,
    _constraint: C,
    update_set: U,
}

impl<
    S: InsertStatement<HasOnConflictClause = TypedFalse>,
    C: UniqueConstraint<Table = <S::Insertable as Insertable>::Table>,
    U: UpdateSet<UpdateTable = <S::Insertable as Insertable>::Table>,
> InsertStatement for InsertWithOnConflictClause<S, C, U>
{
    type HasOnConflictClause = TypedTrue;
    type HasReturningClause = S::HasReturningClause;
    type Insertable = S::Insertable;
    type OutputFields = S::OutputFields;

    fn get_insertable(&self) -> &Self::Insertable {
        self.statement.get_insertable()
    }

    fn write_returning_clause<'s, 'a>(
        &'s self,
        f: &mut String,
        parameter_binder: &mut ParameterBinder<'a>,
    ) -> std::fmt::Result
    where
        's: 'a,
    {
        self.statement.write_returning_clause(f, parameter_binder)
    }

    fn write_on_conflict_clause<'s, 'a>(
        &'s self,
        f: &mut String,
        parameter_binder: &mut ParameterBinder<'a>,
    ) -> std::fmt::Result
    where
        's: 'a,
    {
        use std::fmt::Write;

        write!(
            f,
            " ON CONFLICT({}) DO UPDATE SET ",
            C::FIELDS_COMMA_SEPERATED
        )?;
        self.update_set.write_sql_string(f, parameter_binder)
    }
}

impl<
    S: InsertStatement<HasOnConflictClause = TypedFalse>,
    C: UniqueConstraint<Table = <S::Insertable as Insertable>::Table>,
    U: UpdateSet<UpdateTable = <S::Insertable as Insertable>::Table>,
> SqlStatement for InsertWithOnConflictClause<S, C, U>
{
    impl_sql_statement_for_insert_statement! {}
}

/// A wrapper around an sql insert statement which adds an `ON CONFLICT DO
/// NOTHING` clause to it.
///
/// This wrapper shouldn't be used directly, you should instead use the
/// [`InsertStatementOnConflict::on_conflict_do_nothing`] function.
pub struct InsertWithOnConflictDoNothingClause<S: InsertStatement<HasOnConflictClause = TypedFalse>>
{
    statement: S,
}

impl<S: InsertStatement<HasOnConflictClause = TypedFalse>> InsertStatement
    for InsertWithOnConflictDoNothingClause<S>
{
    type HasOnConflictClause = TypedTrue;
    type HasReturningClause = S::HasReturningClause;
    type Insertable = S::Insertable;
    type OutputFields = S::OutputFields;

    fn get_insertable(&self) -> &Self::Insertable {
        self.statement.get_insertable()
    }

    fn write_returning_clause<'s, 'a>(
        &'s self,
        f: &mut String,
        parameter_binder: &mut ParameterBinder<'a>,
    ) -> std::fmt::Result
    where
        's: 'a,
    {
        self.statement.write_returning_clause(f, parameter_binder)
    }

    fn write_on_conflict_clause<'s, 'a>(
        &'s self,
        f: &mut String,
        _parameter_binder: &mut ParameterBinder<'a>,
    ) -> std::fmt::Result
    where
        's: 'a,
    {
        use std::fmt::Write;

        write!(f, " ON CONFLICT DO NOTHING")
    }
}

impl<S: InsertStatement<HasOnConflictClause = TypedFalse>> SqlStatement
    for InsertWithOnConflictDoNothingClause<S>
{
    impl_sql_statement_for_insert_statement! {}
}