forked from fsprojects/Rezoom.SQL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPostgres.fs
More file actions
219 lines (215 loc) · 8.93 KB
/
Postgres.fs
File metadata and controls
219 lines (215 loc) · 8.93 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
namespace Rezoom.SQL.Compiler.Postgres
open System
open System.Collections.Generic
open System.Globalization
open System.Text.RegularExpressions
open Rezoom.SQL.Compiler
open Rezoom.SQL.Compiler.BackendUtilities
open Rezoom.SQL.Compiler.Translators
open Rezoom.SQL.Mapping
open Rezoom.SQL.Migrations
type private PostgresLiteral() =
inherit DefaultLiteralTranslator()
override __.BlobLiteral(bytes) =
let hexPairs = bytes |> Array.map (fun b -> b.ToString("X2", CultureInfo.InvariantCulture))
@"BYTEA E'\\x" + String.Concat(hexPairs) + "'" |> text
override __.DateTimeLiteral(dt) =
"TIMESTAMPTZ '" + dt.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fffZ") + "'" |> text
override __.DateTimeOffsetLiteral(dt) =
// Can't really store a DateTimeOffset, but we should let people use it since it's the only .NET
// type that unambiguously represents a moment in time.
"TIMESTAMPTZ '" + dt.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fffzzz") + "'" |> text
override __.StringLiteral(str) =
CommandText <| "'" + str.Replace("'", "''") + "'"
type private PostgresExpression(statement : StatementTranslator, indexer) =
inherit DefaultExprTranslator(statement, indexer)
static let eeName = Name(String([| char 102uy; char 117uy; char 99uy; char 107uy|]))
let literal = PostgresLiteral()
override __.Literal = upcast literal
override __.Name(name) =
"\"" + name.Value.ToLowerInvariant().Replace("\"", "\"\"") + "\""
|> text
override __.CollationName(name) = // no ToLower, use as-is
"\"" + name.Value.Replace("\"", "\"\"") + "\""
|> text
override __.TypeName(name, autoIncrement) =
(Seq.singleton << text) <|
match name with
| BooleanTypeName -> "BOOLEAN"
| GuidTypeName -> "UUID"
| IntegerTypeName Integer16 -> "SMALLINT"
| IntegerTypeName Integer32 ->
if autoIncrement then "SERIAL" else "INT"
| IntegerTypeName Integer64 ->
if autoIncrement then "BIGSERIAL" else "BIGINT"
| FloatTypeName Float32 -> "FLOAT4"
| FloatTypeName Float64 -> "FLOAT8"
| StringTypeName(Some len) -> "VARCHAR(" + string len + ")"
| StringTypeName(None) -> "TEXT"
| BinaryTypeName(Some _)
| BinaryTypeName(None) -> "BYTEA"
| DecimalTypeName -> "NUMERIC(38, 19)"
| DateTimeTypeName
| DateTimeOffsetTypeName -> "TIMESTAMPTZ"
override this.ObjectName name =
seq {
if name.ObjectName = eeName then
failAt name.Source Error.tableNameNotSuitableForPG
match name.SchemaName with
// can't schema-qualify temp tables since they are created in a special schema
// with a name generated per-connection
| Some schema when schema <> Name("temp") ->
yield this.Name(schema)
yield text "."
yield this.Name(name.ObjectName)
| _ -> yield this.Name(name.ObjectName)
}
override __.BinaryOperator(op) =
CommandText <|
match op with
| Concatenate -> "||"
| Multiply -> "*"
| Divide -> "/"
| Modulo -> "%"
| Add -> "+"
| Subtract -> "-"
| BitAnd -> "&"
| BitOr -> "|"
| LessThan -> "<"
| LessThanOrEqual -> "<="
| GreaterThan -> ">"
| GreaterThanOrEqual -> ">="
| Equal -> "="
| NotEqual -> "<>"
| And -> "AND"
| Or -> "OR"
| Is -> "IS NOT DISTINCT FROM"
| IsNot -> "IS DISTINCT FROM"
| BitShiftLeft -> "<<"
| BitShiftRight -> ">>"
override __.SimilarityOperator(invert, op) =
CommandText <|
match op with
| Like -> if invert then "NOT LIKE" else "LIKE"
| Match -> if invert then "NOT SIMILAR TO" else "SIMILAR TO"
| Regexp -> if invert then "!~" else "~"
type private PostgresStatement(indexer : IParameterIndexer) as this =
inherit DefaultStatementTranslator(Name("POSTGRES"), indexer)
let expr = PostgresExpression(this :> StatementTranslator, indexer)
override __.Expr = upcast expr
override __.ColumnsNullableByDefault = true
override this.AlterTable(alter) =
let inline alterColumn (col : Name) =
[| text "ALTER COLUMN"
ws
this.Expr.Name(col)
ws
|]
let inline changeType (col : Name) (ty : TypeName) (collation : Name option) changingType =
seq {
yield! alterColumn col
yield text "TYPE"
yield ws
yield! this.Expr.TypeName(ty)
match collation with
| Some collation when ty.SupportsCollation ->
yield ws
yield text "COLLATE"
yield ws
yield this.Expr.CollationName(collation)
| _ -> ()
if changingType then
yield ws
yield text "USING"
yield ws
yield text "CAST("
yield this.Expr.Name(col)
yield ws
yield text "AS"
yield ws
yield! this.Expr.TypeName(ty)
yield text ")"
}
seq {
yield text "ALTER TABLE"
yield ws
yield! this.Expr.ObjectName(alter.Table)
yield ws
match alter.Alteration with
| RenameTo newName ->
yield text "RENAME TO"
yield ws
yield this.Expr.Name(newName)
| AddColumn columnDef ->
yield text "ADD COLUMN"
yield ws
yield! this.ColumnDefinition(alter.Table, columnDef.Value)
| AddConstraint constr ->
yield text "ADD"
yield ws
yield! this.TableConstraint(alter.Table, constr.Value) // includes CONSTRAINT keyword
| AddDefault (col, defaultValue) ->
yield! alterColumn col
yield text "SET DEFAULT"
yield ws
yield! this.FirstClassValue(defaultValue)
| DropColumn name ->
yield text "DROP COLUMN"
yield ws
yield this.Expr.Name(name)
yield ws
yield text "RESTRICT" // this is probably the default but just to be on the safe side
| DropConstraint constr ->
yield text "DROP CONSTRAINT"
yield ws
yield this.Expr.Name(constr)
yield ws
yield text "RESTRICT"
| DropDefault col ->
yield! alterColumn col
yield text "DROP DEFAULT"
| ChangeNullability change ->
yield! alterColumn change.Column
yield text (if change.NewNullable then "DROP NOT NULL" else "SET NOT NULL")
| ChangeType change ->
let schemaColumn = change.ExistingInfo.Column |> Option.get
yield! changeType change.Column change.NewType schemaColumn.Collation true
| ChangeCollation change ->
let schemaColumn = change.ExistingInfo.Column |> Option.get
yield! changeType change.Column schemaColumn.ColumnTypeName (Some change.NewCollation) false
}
override this.PrimaryKeyClause(pk) =
seq {
yield text "PRIMARY KEY"
match pk.Order with
| Ascending -> ()
| Descending ->
fail <| Error.backendDoesNotSupportFeature "Postgres" "Descending PK declared with column definition"
// no need to look at pk.AutoIncrement, because our TypeName will handle it
}
type PostgresBackend() =
static let initialModel =
let main, temp = Name("public"), Name("temp")
{ Schemas =
[ Schema.Empty(main)
Schema.Empty(temp)
] |> List.map (fun s -> s.SchemaName, s) |> Map.ofList
DefaultSchema = main
TemporarySchema = temp
Builtin =
{ Functions = PostgresFunctions.functions
}
BackendCharacteristics =
{ CanDropColumnWithDefaultValue = true
}
}
interface IBackend with
member this.MigrationBackend = <@ fun conn -> new PostgresMigrationBackend(conn) :> IMigrationBackend @>
member this.InitialModel = initialModel
member this.ParameterTransform(columnType) = ParameterTransform.Default(columnType)
member this.ToCommandFragments(indexer, stmts) =
let translator = PostgresStatement(indexer)
translator.TotalStatements(stmts)
|> BackendUtilities.simplifyFragments
|> ResizeArray
:> _ IReadOnlyList