Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(parser): Improve sql parser #136

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion frontend/src/DataSources/SqlMiner/Parsers/CreateTable.elm
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ parseCreateTableColumnForeignKey constraint =

parseCreateTablePrimaryKey : RawSql -> Result ParseError ParsedPrimaryKey
parseCreateTablePrimaryKey sql =
case sql |> Regex.matches "^PRIMARY KEY \\((?<columns>[^)]+)\\)(?:\\s+USING [^ ]+)?$" of
case sql |> Regex.matches "^PRIMARY\\s+KEY(?:\\s+CLUSTERED)?\\s*\\((?<columns>[^)]+)\\)(?:\\s*USING [^ ]+)?(?:\\s*WITH \\([^)]+\\))?(?:\\s*ON [^ ]+)?$" of
(Just columns) :: [] ->
columns
|> String.split ","
Expand Down
18 changes: 18 additions & 0 deletions frontend/tests/DataSources/SqlMiner/Parsers/CreateTableTest.elm
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,24 @@ suite =
, testStatement ( parseCreateTable, "with multiple constraints" )
"CREATE TABLE t1 (id int constraint t1_pk primary key constraint t1_t2_fk references t2);"
{ parsedTable | schema = Nothing, table = "t1", columns = Nel { parsedColumn | name = "id", kind = "int", primaryKey = Just "t1_pk", foreignKey = Just ( Just "t1_t2_fk", { schema = Nothing, table = "t2", column = Nothing } ) } [] }
, testStatement ( parseCreateTable, "test" )
"""CREATE TABLE [dbo].[comments](
[id] [bigint] IDENTITY(1,1) NOT NULL,
[created_at] [datetimeoffset](7) NULL,
[updated_at] [datetimeoffset](7) NULL,
[deleted_at] [datetimeoffset](7) NULL,
[user_id] [nvarchar](max) NULL,
[order_id] [bigint] NOT NULL,
[machine_id] [bigint] NOT NULL,
[repair_id] [bigint] NOT NULL,
[inspection_id] [bigint] NOT NULL,
[issue_id] [bigint] NOT NULL,
[note_id] [bigint] NOT NULL,
[demand_id] [bigint] NOT NULL,
[text] [varchar](max) NOT NULL,
PRIMARY KEY CLUSTERED ([id] ASC) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY];"""
parsedTable
]
, describe "parseCreateTableColumn"
[ testSql ( parseCreateTableColumn, "basic" )
Expand Down