-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathCrudSource.cs
More file actions
665 lines (621 loc) · 28.2 KB
/
Copy pathCrudSource.cs
File metadata and controls
665 lines (621 loc) · 28.2 KB
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
using Npgsql;
using NpgsqlTypes;
using static NpgsqlRest.NpgsqlRestOptions;
namespace NpgsqlRest.CrudSource;
[Flags]
public enum CrudCommandType
{
Select = 0,
Update = 1,
UpdateReturning = 2,
Insert = 4,
InsertReturning = 8,
InsertOnConflictDoNothing = 16,
InsertOnConflictDoUpdate = 32,
InsertOnConflictDoNothingReturning = 64,
InsertOnConflictDoUpdateReturning = 128,
Delete = 256,
DeleteReturning = 512,
All = Select |
Update |
UpdateReturning |
Insert |
InsertReturning |
InsertOnConflictDoNothing |
InsertOnConflictDoUpdate |
InsertOnConflictDoNothingReturning |
InsertOnConflictDoUpdateReturning |
Delete |
DeleteReturning
}
public class CrudSource(
string? schemaSimilarTo = null,
string? schemaNotSimilarTo = null,
string[]? includeSchemas = null,
string[]? excludeSchemas = null,
string? nameSimilarTo = null,
string? nameNotSimilarTo = null,
string[]? includeNames = null,
string[]? excludeNames = null,
string? query = null,
CrudCommandType crudTypes = CrudCommandType.All,
string returningUrlPattern = "{0}/returning",
string onConflictDoNothingUrlPattern = "{0}/on-conflict-do-nothing",
string onConflictDoNothingReturningUrlPattern = "{0}/on-conflict-do-nothing/returning",
string onConflictDoUpdateUrlPattern = "{0}/on-conflict-do-update",
string onConflictDoUpdateReturningUrlPattern = "{0}/on-conflict-do-update/returning",
Func<Routine, CrudCommandType, bool>? created = null,
CommentsMode? commentsMode = null) : IRoutineSource
{
//
// When not NULL, overrides the main option SchemaSimilarTo. It filters schemas similar to this or null to ignore this parameter.
//
public string? SchemaSimilarTo { get; set; } = schemaSimilarTo;
//
// When not NULL, overrides the main option SchemaNotSimilarTo. It filters schemas not similar to this or null to ignore this parameter.
//
public string? SchemaNotSimilarTo { get; set; } = schemaNotSimilarTo;
//
// When not NULL, overrides the main option IncludeSchemas. List of schema names to be included or null to ignore this parameter.
//
public string[]? IncludeSchemas { get; set; } = includeSchemas;
//
// When not NULL, overrides the main option ExcludeSchemas. List of schema names to be excluded or null to ignore this parameter.
//
public string[]? ExcludeSchemas { get; set; } = excludeSchemas;
//
// When not NULL, overrides the main option NameSimilarTo. It filters names similar to this or null to ignore this parameter.
//
public string? NameSimilarTo { get; set; } = nameSimilarTo;
//
// When not NULL, overrides the main option NameNotSimilarTo. It filters names not similar to this or null to ignore this parameter.
//
public string? NameNotSimilarTo { get; set; } = nameNotSimilarTo;
//
// When not NULL, overrides the main option IncludeNames. List of names to be included or null to ignore this parameter.
//
public string[]? IncludeNames { get; set; } = includeNames;
//
// When not NULL, overrides the main option ExcludeNames. List of names to be excluded or null to ignore this parameter.
//
public string[]? ExcludeNames { get; set; } = excludeNames;
//
// Custom query instead of the default one. See default in CrudSourceQuery.cs
//
public string? Query { get; set; } = query ?? CrudSourceQuery.Query;
//
// Type of CRUD queries and commands to create.
//
public CrudCommandType CrudTypes { get; init; } = crudTypes;
//
// URL pattern for all "returning" endpoints. Parameter is the original URL. Default is "{0}/returning".
//
public string ReturningUrlPattern { get; init; } = returningUrlPattern;
//
// URL pattern for all "do nothing" endpoints. Parameter is the original URL. Default is "{0}/on-conflict-do-nothing".
//
public string OnConflictDoNothingUrlPattern { get; init; } = onConflictDoNothingUrlPattern;
//
// URL pattern for all "do nothing returning " endpoints. Parameter is the original URL. Default is "{0}/on-conflict-do-nothing/returning".
//
public string OnConflictDoNothingReturningUrlPattern { get; init; } = onConflictDoNothingReturningUrlPattern;
//
// URL pattern for all "do update" endpoints. Parameter is the original URL. Default is "{0}/on-conflict-do-update".
//
public string OnConflictDoUpdateUrlPattern { get; init; } = onConflictDoUpdateUrlPattern;
//
// URL pattern for all "do update returning" endpoints. Parameter is the original URL. Default the "{0}/on-conflict-do-update/returning".
//
public string OnConflictDoUpdateReturningUrlPattern { get; init; } = onConflictDoUpdateReturningUrlPattern;
//
// Callback function, when not null it is evaluated when Routine object is created for a certain type. Return true or false to disable or enable endpoints.
//
public Func<Routine, CrudCommandType, bool>? Created { get; init; } = created;
//
// Comments mode (`Ignore`, `ParseAll`, `OnlyWithHttpTag`), when not null overrides the `CommentsMode` from the main options.
//
public CommentsMode? CommentsMode { get; set; } = commentsMode;
//
// When true, composite type columns in the response are serialized as nested JSON objects.
// Not typically used for CRUD sources but required by IRoutineSource interface.
//
public bool NestedJsonForCompositeTypes { get; set; } = false;
private readonly IRoutineSourceParameterFormatter _selectParameterFormatter = new SelectParameterFormatter();
private readonly IRoutineSourceParameterFormatter _updateParameterFormatter = new UpdateParameterFormatter();
private readonly IRoutineSourceParameterFormatter _insertParameterFormatter = new InsertParameterFormatter();
private readonly IRoutineSourceParameterFormatter _deleteParameterFormatter = new DeleteParameterFormatter();
private readonly string NL = Environment.NewLine;
private readonly string[] _selectTags = ["select", "read", "get"];
private readonly string[] _updateTags = ["update", "post"];
private readonly string[] _updateReturningTags = [
"update",
"post",
"update_returning",
"returning"
];
private readonly string[] _deleteTags = ["delete"];
private readonly string[] _deleteReturningTags = [
"delete",
"delete_returning",
"returning"
];
private readonly string[] _insertTags = ["insert", "put", "create"];
private readonly string[] _insertReturningTags = [
"insert",
"put",
"create",
"insert_returning",
"returning"
];
private readonly string[] _insertOnConflictDoNothingTags = [
"insert",
"put",
"create",
"insert_on_conflict_do_nothing",
"on_conflict_do_nothing",
"on_conflict",
];
private readonly string[] _insertOnConflictDoNothingReturningTags = [
"insert",
"put",
"create",
"insert_on_conflict_do_nothing_returning",
"on_conflict_do_nothing",
"on_conflict",
"returning"
];
private readonly string[] _insertOnConflictDoUpdateTags = [
"insert",
"put",
"create",
"insert_on_conflict_do_update",
"on_conflict",
"on_conflict_do_update"
];
private readonly string[] _insertOnConflictDoUpdateReturningTags = [
"insert",
"put",
"create",
"insert_on_conflict_do_update_returning",
"on_conflict_do_update",
"on_conflict",
"returning"
];
private bool Select { get => (CrudTypes & CrudCommandType.Select) == CrudCommandType.Select; }
private bool Update { get => (CrudTypes & CrudCommandType.Update) == CrudCommandType.Update; }
private bool UpdateReturning { get => (CrudTypes & CrudCommandType.UpdateReturning) == CrudCommandType.UpdateReturning; }
private bool Insert { get => (CrudTypes & CrudCommandType.Insert) == CrudCommandType.Insert; }
private bool InsertReturning { get => (CrudTypes & CrudCommandType.InsertReturning) == CrudCommandType.InsertReturning; }
private bool InsertOnConflictDoNothing { get => (CrudTypes & CrudCommandType.InsertOnConflictDoNothing) == CrudCommandType.InsertOnConflictDoNothing; }
private bool InsertOnConflictDoUpdate { get => (CrudTypes & CrudCommandType.InsertOnConflictDoUpdate) == CrudCommandType.InsertOnConflictDoUpdate; }
private bool InsertOnConflictDoNothingReturning { get => (CrudTypes & CrudCommandType.InsertOnConflictDoNothingReturning) == CrudCommandType.InsertOnConflictDoNothingReturning; }
private bool InsertOnConflictDoUpdateReturning { get => (CrudTypes & CrudCommandType.InsertOnConflictDoUpdateReturning) == CrudCommandType.InsertOnConflictDoUpdateReturning; }
private bool Delete { get => (CrudTypes & CrudCommandType.Delete) == CrudCommandType.Delete; }
private bool DeleteReturning { get => (CrudTypes & CrudCommandType.DeleteReturning) == CrudCommandType.DeleteReturning; }
public IEnumerable<(Routine, IRoutineSourceParameterFormatter)> Read(IServiceProvider? serviceProvider, RetryStrategy? retryStrategy)
{
NpgsqlConnection? connection = null;
bool shouldDispose = true;
try
{
Options.CreateAndOpenSourceConnection(serviceProvider, ref connection, ref shouldDispose, nameof(CrudSource));
if (connection is null)
{
yield break;
}
foreach (var (routine, formatter, type) in ReadInternal(Options, connection, retryStrategy))
{
if (Created is not null && !Created(routine, type))
{
continue;
}
yield return (routine, formatter);
}
}
finally
{
if (shouldDispose && connection is not null)
{
connection.Dispose();
}
}
}
private IEnumerable<(Routine routine, IRoutineSourceParameterFormatter formatter, CrudCommandType type)> ReadInternal(
NpgsqlRestOptions options,
NpgsqlConnection connection,
RetryStrategy? retryStrategy)
{
using var command = connection.CreateCommand();
Query ??= CrudSourceQuery.Query;
if (Query.Contains(' ') is false)
{
command.CommandText = string.Concat("select * from ", Query, "($1,$2,$3,$4,$5,$6,$7,$8)");
}
else
{
command.CommandText = Query;
}
command.CommandText = Query;
AddParameter(command, SchemaSimilarTo ?? options.SchemaSimilarTo); // $1
AddParameter(command, SchemaNotSimilarTo ?? options.SchemaNotSimilarTo); // $2
AddParameter(command, IncludeSchemas ?? options.IncludeSchemas, true); // $3
AddParameter(command, ExcludeSchemas ?? options.ExcludeSchemas, true); // $4
AddParameter(command, NameSimilarTo ?? options.NameSimilarTo); // $5
AddParameter(command, NameNotSimilarTo ?? options.NameNotSimilarTo); // $6
AddParameter(command, IncludeNames ?? options.IncludeNames, true); // $7
AddParameter(command, ExcludeNames ?? options.ExcludeNames, true); // $8
command.LogCommand(nameof(CrudCommandType));
using NpgsqlDataReader reader = command.ExecuteReaderWithRetry(retryStrategy);
while (reader.Read())
{
var type = reader.Get<string>(0) switch //"type") switch
{
"BASE TABLE" => RoutineType.Table,
"VIEW" => RoutineType.View,
_ => RoutineType.Other
};
var schema = reader.Get<string>(1);// "schema");
var name = reader.Get<string>(2);//"name");
var comment = reader.Get<string>(11);//"comment");
var columnCount = reader.Get<int>(4);//"column_count");
var columnNames = reader.Get<string[]>(5);//"column_names");
string[] convertedColumnNames = new string[columnNames.Length];
for (int i = 0; i < columnNames.Length; i++)
{
convertedColumnNames[i] = options.NameConverter(columnNames[i]) ?? columnNames[i];
}
string[] jsonColumnNames = new string[convertedColumnNames.Length];
for (int i = 0; i < convertedColumnNames.Length; i++)
{
jsonColumnNames[i] = PgConverters.SerializeString(convertedColumnNames[i]);
}
var columnTypes = reader.Get<string[]>(6);//"column_types");
var primaryKeys = new HashSet<string>(reader.Get<string[]>(10));//"primary_keys"));
var identityColumns = reader.Get<bool[]>(8);//"identity_columns");
var notPrimaryKeys = columnNames.Where(x => !primaryKeys.Contains(x)).ToArray();
var descriptors = columnTypes
.Select((x, i) =>
new TypeDescriptor(x,
hasDefault: true,
isPk: primaryKeys.Contains(columnNames[i]),
isIdentity: identityColumns[i]))
.ToArray();
var updatableColumns = reader.Get<bool[]>(7);//"updatable_columns");
bool hasPks = primaryKeys.Count > 0;
var isInsertable = reader.Get<bool>(3);//"is_insertable");
bool isUpdatable = updatableColumns.Any(x => x) && hasPks;
bool doesUpdates = (Update || UpdateReturning) && isUpdatable;
bool doesInserts = (Insert ||
InsertOnConflictDoNothing ||
InsertOnConflictDoNothingReturning ||
InsertOnConflictDoUpdate ||
InsertOnConflictDoUpdateReturning ||
InsertReturning) && isInsertable;
bool doesReturnings = (UpdateReturning && doesUpdates) ||
((InsertOnConflictDoNothingReturning ||
InsertOnConflictDoUpdateReturning ||
InsertReturning) && doesInserts) || DeleteReturning;
Routine CreateRoutine(
CrudType crudType,
string expression,
string fullDefinition,
string simpleDefinition,
bool isVoid,
string? formatUrlPattern = null,
TypeDescriptor[]? typeDescriptors = null,
string[]? tags = null)
{
TypeDescriptor[] ts = typeDescriptors ?? descriptors;
NpgsqlRestParameter[] parameters = new NpgsqlRestParameter[columnCount];
if (columnCount > 0)
{
for (var i = 0; i < columnCount; i++)
{
var descriptor = ts[i];
//parameters[i] = new NpgsqlRestParameter
//{
// Ordinal = i,
// NpgsqlDbType = descriptor.ActualDbType,
// ConvertedName = convertedColumnNames[i],
// ActualName = columnNames[i],
// TypeDescriptor = descriptor
//};
parameters[i] = new NpgsqlRestParameter(
ordinal: i,
convertedName: convertedColumnNames[i],
actualName: columnNames[i],
typeDescriptor: descriptor);
}
}
return new Routine
{
Type = type,
Schema = schema,
Name = name,
Comment = comment,
IsStrict = false,
CrudType = crudType,
ReturnsRecordType = false,
ReturnsSet = true,
ColumnCount = columnCount,
OriginalColumnNames = columnNames,
ColumnNames = convertedColumnNames,
JsonColumnNames = jsonColumnNames,
ReturnsUnnamedSet = false,
ColumnsTypeDescriptor = ts,
IsVoid = isVoid,
ParamCount = columnCount,
Parameters = parameters,
ParamsHash = [.. parameters.Select(p => p.ConvertedName)],
OriginalParamsHash = [.. parameters.Select(p => p.ActualName)],
Expression = expression,
FullDefinition = fullDefinition,
SimpleDefinition = simpleDefinition,
Tags = tags,
FormatUrlPattern = formatUrlPattern,
EndpointHandler = null,
Metadata = null
};
}
if (Select)
{
var expression = string.Concat(
"select ",
NL, " ",
string.Join(", ", columnNames),
NL,
" from ",
NL, " ",
schema, ".", name,
NL);
var fullDefinition = string.Concat(expression,
"where",
NL, " ",
columnNames.Select(n => $"{n} = ?"));
var simpleDefinition = string.Concat("select ", schema, ".", name);
yield return (
CreateRoutine(
CrudType.Select,
expression,
fullDefinition,
simpleDefinition,
isVoid: false,
tags: _selectTags),
_selectParameterFormatter,
CrudCommandType.Select);
}
string updateExp = default!, updateDef = default!, updateSimple = default!;
if (doesUpdates)
{
updateExp = string.Concat(
"update ", schema, ".", name,
NL,
"{0}",
NL,
"{1}");
updateDef = string.Format(updateExp,
string.Concat("set", NL, " ", string.Join(", ", notPrimaryKeys.Select(n => $"{n} = ?"))),
string.Concat("where", NL, " ", string.Join(" and ", notPrimaryKeys.Select(n => $"{n} = ?"))));
updateSimple = string.Concat("update ", schema, ".", name);
}
if (Update && doesUpdates)
{
yield return (
CreateRoutine(
CrudType.Update,
expression: updateExp,
fullDefinition: updateDef,
simpleDefinition: updateSimple,
isVoid: true,
tags: _updateTags),
_updateParameterFormatter,
CrudCommandType.Update);
}
string returningExp = default!;
if (doesReturnings)
{
returningExp = string.Concat(
NL,
"returning",
NL, " ",
string.Join(", ", columnNames));
}
if (UpdateReturning && doesUpdates)
{
yield return (
CreateRoutine(
CrudType.Update,
expression: string.Concat(updateExp, returningExp),
fullDefinition: string.Concat(updateDef, returningExp),
simpleDefinition: string.Concat(updateSimple, returningExp),
isVoid: false,
formatUrlPattern: ReturningUrlPattern,
tags: _updateReturningTags),
_updateParameterFormatter,
CrudCommandType.UpdateReturning);
}
string deleteExp = default!, deleteDef = default!;
if (Delete || DeleteReturning)
{
deleteExp = string.Concat("delete from ", schema, ".", name, NL, "{0}");
deleteDef = string.Concat("delete from ", schema, ".", name);
}
if (Delete)
{
yield return (
CreateRoutine(
CrudType.Delete,
expression: deleteExp,
fullDefinition: deleteDef,
simpleDefinition: deleteDef,
isVoid: true,
tags: _deleteTags),
_deleteParameterFormatter,
CrudCommandType.Delete);
}
if (DeleteReturning)
{
yield return (
CreateRoutine(
CrudType.Delete,
expression: string.Concat(deleteExp, returningExp),
fullDefinition: string.Concat(deleteDef, returningExp),
simpleDefinition: string.Concat(deleteDef, returningExp),
isVoid: false,
formatUrlPattern: ReturningUrlPattern,
tags: _deleteReturningTags),
_deleteParameterFormatter,
CrudCommandType.DeleteReturning);
}
if (doesInserts is false)
{
yield break;
}
var insertExp = string.Concat("insert into ", schema, ".", name,
NL, "({0})",
"{1}",
NL, "values",
NL, "({2})");
var insertDef = string.Format(insertExp,
string.Join(", ", columnNames),
"",
string.Join(", ", columnNames.Select(_ => "?")));
var insertSimple = string.Concat("insert into ", schema, ".", name);
var hasDefaults = reader.Get<bool[]>(9);//"has_defaults");
var insertTypeDescriptors = columnTypes
.Select((x, i) =>
new TypeDescriptor(x,
hasDefault: hasDefaults[i],
isPk: primaryKeys.Contains(columnNames[i]),
isIdentity: identityColumns[i]))
.ToArray();
if (Insert && doesInserts)
{
yield return (
CreateRoutine(
CrudType.Insert,
expression: insertExp,
fullDefinition: insertDef,
simpleDefinition: insertSimple,
isVoid: true,
typeDescriptors: insertTypeDescriptors,
tags: _insertTags),
_insertParameterFormatter,
CrudCommandType.Insert);
}
if (InsertReturning && doesInserts)
{
yield return (
CreateRoutine(
CrudType.Insert,
expression: string.Concat(insertExp, returningExp),
fullDefinition: string.Concat(insertDef, returningExp),
simpleDefinition: string.Concat(insertSimple, returningExp),
isVoid: false,
formatUrlPattern: ReturningUrlPattern,
tags: _insertReturningTags),
_insertParameterFormatter,
CrudCommandType.InsertReturning);
}
string onConflict = default!;
if (doesInserts && hasPks && (InsertOnConflictDoNothing ||
InsertOnConflictDoNothingReturning ||
InsertOnConflictDoUpdate ||
InsertOnConflictDoUpdateReturning))
{
onConflict = string.Concat(NL, "on conflict (", string.Join(", ", primaryKeys), ") ");
}
if (InsertOnConflictDoNothing && doesInserts && hasPks)
{
yield return (
CreateRoutine(
CrudType.Insert,
expression: string.Concat(insertExp, onConflict, "do nothing"),
fullDefinition: string.Concat(insertDef, onConflict, "do nothing"),
simpleDefinition: string.Concat(insertSimple, onConflict, "do nothing"),
isVoid: true,
formatUrlPattern: OnConflictDoNothingUrlPattern,
typeDescriptors: insertTypeDescriptors,
tags: _insertOnConflictDoNothingTags),
_insertParameterFormatter,
CrudCommandType.InsertOnConflictDoNothing);
}
if (InsertOnConflictDoNothingReturning && doesInserts && hasPks)
{
yield return (
CreateRoutine(
CrudType.Insert,
expression: string.Concat(insertExp, onConflict, "do nothing", returningExp),
fullDefinition: string.Concat(insertDef, onConflict, "do nothing", returningExp),
simpleDefinition: string.Concat(insertSimple, onConflict, "do nothing", returningExp),
isVoid: false,
formatUrlPattern: OnConflictDoNothingReturningUrlPattern,
typeDescriptors: insertTypeDescriptors,
tags: _insertOnConflictDoNothingReturningTags),
_insertParameterFormatter,
CrudCommandType.InsertOnConflictDoNothingReturning);
}
string doUpdate = default!;
if (doesInserts && hasPks && (InsertOnConflictDoUpdate || InsertOnConflictDoUpdateReturning))
{
doUpdate = string.Concat(NL, "do update set ", string.Join(", ", notPrimaryKeys.Select(n => $"{NL} {n} = excluded.{n}")));
}
if (InsertOnConflictDoUpdate && doesInserts && hasPks)
{
yield return (
CreateRoutine(
CrudType.Insert,
expression: string.Concat(insertExp, onConflict, doUpdate),
fullDefinition: string.Concat(insertDef, onConflict, doUpdate),
simpleDefinition: string.Concat(insertSimple, onConflict, "do update"),
isVoid: true,
formatUrlPattern: OnConflictDoUpdateUrlPattern,
typeDescriptors: insertTypeDescriptors,
tags: _insertOnConflictDoUpdateTags),
_insertParameterFormatter,
CrudCommandType.InsertOnConflictDoUpdate);
}
if (InsertOnConflictDoUpdateReturning && doesInserts && hasPks)
{
yield return (
CreateRoutine(
CrudType.Insert,
expression: string.Concat(insertExp, onConflict, doUpdate, returningExp),
fullDefinition: string.Concat(insertDef, onConflict, doUpdate, returningExp),
simpleDefinition: string.Concat(insertSimple, onConflict, "do update", returningExp),
isVoid: false,
formatUrlPattern: OnConflictDoUpdateReturningUrlPattern,
typeDescriptors: insertTypeDescriptors,
tags: _insertOnConflictDoUpdateReturningTags),
_insertParameterFormatter,
CrudCommandType.InsertOnConflictDoUpdateReturning);
}
}
}
private static void AddParameter(NpgsqlCommand command, object? value, bool isArray = false)
{
if (value is null)
{
value = DBNull.Value;
}
else if (isArray && value is string[] array)
{
if (array.Length == 0)
{
value = DBNull.Value;
}
}
else if (!isArray && value is string str)
{
if (string.IsNullOrWhiteSpace(str))
{
value = DBNull.Value;
}
}
command.Parameters.Add(new NpgsqlParameter
{
NpgsqlDbType = isArray ? NpgsqlDbType.Text | NpgsqlDbType.Array : NpgsqlDbType.Text,
Value = value
});
}
}