-
Notifications
You must be signed in to change notification settings - Fork 133
Expand file tree
/
Copy pathCreateTable.elm
More file actions
75 lines (63 loc) · 2.49 KB
/
CreateTable.elm
File metadata and controls
75 lines (63 loc) · 2.49 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
module DataSources.NewSqlParser.Parsers.CreateTable exposing (columnParser, columnsParser, createTableParser)
import DataSources.NewSqlParser.Dsl exposing (ColumnConstraint(..), ParsedColumn, ParsedTable)
import DataSources.NewSqlParser.Parsers.Basic exposing (checkParser, columnNameParser, columnTypeParser, constraintParser, defaultValueParser, notNullParser, primaryKeyParser, tableRefParser)
import Libs.Maybe as M
import Libs.Parser exposing (symbolInsensitive)
import Parser exposing ((|.), (|=), Parser, Trailing(..), oneOf, sequence, spaces, succeed)
-- https://www.postgresql.org/docs/current/sql-createtable.html
-- https://dev.mysql.com/doc/refman/8.0/en/create-table.html
-- https://docs.microsoft.com/fr-fr/sql/t-sql/statements/create-table-transact-sql
-- https://docs.oracle.com/en/database/oracle/oracle-database/21/sqlrf/CREATE-TABLE.html
-- https://www.sqlite.org/lang_createtable.html
createTableParser : Parser ParsedTable
createTableParser =
succeed (\( schemaName, tableName ) columns -> ParsedTable schemaName tableName columns [])
|. symbolInsensitive "CREATE TABLE"
|. spaces
|. oneOf
[ symbolInsensitive "IF NOT EXISTS"
, succeed ""
]
|. spaces
|= tableRefParser
|. spaces
|= columnsParser
columnsParser : Parser (List ParsedColumn)
columnsParser =
sequence
{ start = "("
, separator = ","
, end = ")"
, spaces = spaces
, item = columnParser
, trailing = Forbidden
}
columnParser : Parser ParsedColumn
columnParser =
succeed
(\name kind nullable default primaryKey check constraint ->
let
( pk, fk ) =
case constraint of
Just ( constraintName, ColumnPrimaryKey ) ->
( primaryKey |> M.orElse (Just constraintName), Nothing )
Just ( constraintName, ColumnForeignKey ref ) ->
( primaryKey, Just ( constraintName, ref ) )
Nothing ->
( primaryKey, Nothing )
in
ParsedColumn name kind nullable default pk fk check
)
|= columnNameParser
|. spaces
|= columnTypeParser
|. spaces
|= notNullParser
|. spaces
|= defaultValueParser
|. spaces
|= primaryKeyParser
|. spaces
|= checkParser
|. spaces
|= constraintParser