-
Notifications
You must be signed in to change notification settings - Fork 128
/
SqLiteBuilder.cs
91 lines (82 loc) · 3.93 KB
/
SqLiteBuilder.cs
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
namespace Dapper.FastCrud.SqlBuilders
{
using System;
using System.Globalization;
using System.Linq;
using Dapper.FastCrud.EntityDescriptors;
using Dapper.FastCrud.Mappings;
internal class SqLiteBuilder:GenericStatementSqlBuilder
{
public SqLiteBuilder(EntityDescriptor entityDescriptor, EntityMapping entityMapping)
: base(entityDescriptor, entityMapping, SqlDialect.SqLite)
{
if (this.KeyProperties.Length > 1)
{
throw new NotSupportedException($"Entity <{entityMapping.EntityType.Name}> has more than one primary keys. This is not supported by SqLite.");
}
}
/// <summary>
/// Constructs a full insert statement
/// </summary>
protected override string ConstructFullInsertStatementInternal()
{
var sql = this.ResolveWithCultureInvariantFormatter(
$"INSERT INTO {this.GetTableName()} ({this.ConstructColumnEnumerationForInsert()}) VALUES ({this.ConstructParamEnumerationForInsert()}); ");
if (this.RefreshOnInsertProperties.Length > 0)
{
// we have to bring some column values back
if (this.KeyProperties.Length == 0)
{
throw new NotSupportedException($"Entity '{this.EntityMapping.EntityType.Name}' has database generated fields but no primary key to retrieve them with after insertion.");
}
// we have an identity column, so we can fetch the rest of them
if (this.InsertKeyDatabaseGeneratedProperties.Length == 1 && this.RefreshOnInsertProperties.Length == 1)
{
// just one, this is going to be easy
sql += this.ResolveWithCultureInvariantFormatter($"SELECT last_insert_rowid() as {this.GetDelimitedIdentifier(this.InsertKeyDatabaseGeneratedProperties[0].PropertyName)};");
}
else
{
// There are no primary keys generated by the database
if (this.InsertKeyDatabaseGeneratedProperties.Length == 0)
{
sql += $"SELECT {this.ConstructRefreshOnInsertColumnSelection()} FROM {this.GetTableName()} WHERE" + this.ConstructKeysWhereClause();
}
else
{
sql += this.ResolveWithCultureInvariantFormatter($"SELECT {this.ConstructRefreshOnInsertColumnSelection()} FROM {this.GetTableName()} WHERE {this.GetColumnName(this.InsertKeyDatabaseGeneratedProperties[0], null, false)} = last_insert_rowid();");
}
}
}
return sql;
}
protected override string ConstructFullSelectStatementInternal(
string selectClause,
string fromClause,
FormattableString whereClause = null,
FormattableString orderClause = null,
long? skipRowsCount = null,
long? limitRowsCount = null,
bool forceTableColumnResolution = false)
{
var sql = this.ResolveWithCultureInvariantFormatter($"SELECT {selectClause} FROM {fromClause}");
if (whereClause != null)
{
sql += " WHERE " + this.ResolveWithSqlFormatter(whereClause, forceTableColumnResolution);
}
if (orderClause != null)
{
sql += " ORDER BY " + this.ResolveWithSqlFormatter(orderClause, forceTableColumnResolution);
}
if (limitRowsCount.HasValue || skipRowsCount.HasValue)
{
sql += this.ResolveWithCultureInvariantFormatter($" LIMIT {limitRowsCount ?? -1}");
}
if (skipRowsCount.HasValue)
{
sql += this.ResolveWithCultureInvariantFormatter($" OFFSET {skipRowsCount}");
}
return sql;
}
}
}