-
-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathPostgresDialect.swift
118 lines (94 loc) · 2.93 KB
/
PostgresDialect.swift
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
import SQLKit
public struct PostgresDialect: SQLDialect {
public init() {}
public var name: String {
"postgresql"
}
public var identifierQuote: any SQLExpression {
SQLRaw(#"""#)
}
public var literalStringQuote: any SQLExpression {
SQLRaw("'")
}
public var supportsAutoIncrement: Bool {
true
}
public var autoIncrementClause: any SQLExpression {
SQLRaw("GENERATED BY DEFAULT AS IDENTITY")
}
public var autoIncrementFunction: (any SQLExpression)? {
nil
}
public func bindPlaceholder(at position: Int) -> any SQLExpression {
SQLRaw("$\(position)")
}
public func literalBoolean(_ value: Bool) -> any SQLExpression {
SQLRaw("\(value)")
}
public var literalDefault: any SQLExpression {
SQLRaw("DEFAULT")
}
public var supportsIfExists: Bool {
true
}
public var enumSyntax: SQLEnumSyntax {
.typeName
}
public var supportsDropBehavior: Bool {
true
}
public var supportsReturning: Bool {
true
}
public var triggerSyntax: SQLTriggerSyntax {
.init(
create: [.supportsForEach, .postgreSQLChecks, .supportsCondition, .conditionRequiresParentheses, .supportsConstraints],
drop: [.supportsCascade, .supportsTableName]
)
}
public var alterTableSyntax: SQLAlterTableSyntax {
.init(
alterColumnDefinitionClause: SQLRaw("ALTER COLUMN"),
alterColumnDefinitionTypeKeyword: SQLRaw("SET DATA TYPE")
)
}
public func customDataType(for dataType: SQLDataType) -> (any SQLExpression)? {
if case let .custom(expr) = dataType, (expr as? SQLRaw)?.sql == "TIMESTAMP" {
return SQLRaw("TIMESTAMPTZ")
} else if case .blob = dataType {
return SQLRaw("BYTEA")
} else {
return nil
}
}
public var upsertSyntax: SQLUpsertSyntax {
.standard
}
public var unionFeatures: SQLUnionFeatures {
[
.union, .unionAll,
.intersect, .intersectAll,
.except, .exceptAll,
.explicitDistinct,
.parenthesizedSubqueries
]
}
public var sharedSelectLockExpression: (any SQLExpression)? {
SQLRaw("FOR SHARE")
}
public var exclusiveSelectLockExpression: (any SQLExpression)? {
SQLRaw("FOR UPDATE")
}
public func nestedSubpathExpression(in column: any SQLExpression, for path: [String]) -> (any SQLExpression)? {
guard !path.isEmpty else { return nil }
let descender = SQLList(
[column] + path.dropLast().map(SQLLiteral.string(_:)),
separator: SQLRaw("->")
)
let accessor = SQLList(
[descender, SQLLiteral.string(path.last!)],
separator: SQLRaw("->>")
)
return SQLGroupExpression(accessor)
}
}