From 763f783d9f4853b1a1ccf28fbc27af4bb9074a9a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Knuchel?= Date: Tue, 15 Nov 2022 13:29:48 +0100 Subject: [PATCH] imporve sql parser --- .../SqlMiner/Parsers/CreateTable.elm | 2 +- .../SqlMiner/Parsers/CreateTableTest.elm | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/frontend/src/DataSources/SqlMiner/Parsers/CreateTable.elm b/frontend/src/DataSources/SqlMiner/Parsers/CreateTable.elm index 4dafe9e47..a3c96d05a 100644 --- a/frontend/src/DataSources/SqlMiner/Parsers/CreateTable.elm +++ b/frontend/src/DataSources/SqlMiner/Parsers/CreateTable.elm @@ -200,7 +200,7 @@ parseCreateTableColumnForeignKey constraint = parseCreateTablePrimaryKey : RawSql -> Result ParseError ParsedPrimaryKey parseCreateTablePrimaryKey sql = - case sql |> Regex.matches "^PRIMARY KEY \\((?[^)]+)\\)(?:\\s+USING [^ ]+)?$" of + case sql |> Regex.matches "^PRIMARY\\s+KEY(?:\\s+CLUSTERED)?\\s*\\((?[^)]+)\\)(?:\\s*USING [^ ]+)?(?:\\s*WITH \\([^)]+\\))?(?:\\s*ON [^ ]+)?$" of (Just columns) :: [] -> columns |> String.split "," diff --git a/frontend/tests/DataSources/SqlMiner/Parsers/CreateTableTest.elm b/frontend/tests/DataSources/SqlMiner/Parsers/CreateTableTest.elm index 8fdda60ed..d3891f06c 100644 --- a/frontend/tests/DataSources/SqlMiner/Parsers/CreateTableTest.elm +++ b/frontend/tests/DataSources/SqlMiner/Parsers/CreateTableTest.elm @@ -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" )