Skip to content

Commit

Permalink
fix: Add support for ALTER database.TABLE syntax in the parser #376
Browse files Browse the repository at this point in the history
  • Loading branch information
dineug committed Sep 28, 2024
1 parent 7713c51 commit 51522c1
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 3 deletions.
34 changes: 31 additions & 3 deletions packages/schema-sql-parser/src/parser/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,18 @@ export const isAlterTableAddPrimaryKey = (tokens: Token[]) => {
isAdd(pos + 3) &&
isConstraint(pos + 4) &&
isPrimary(pos + 6) &&
isKey(pos + 7));
isKey(pos + 7)) ||
(isAlter(pos) &&
isTable(pos + 1) &&
isAdd(pos + 5) &&
isPrimary(pos + 6) &&
isKey(pos + 7)) ||
(isAlter(pos) &&
isTable(pos + 1) &&
isAdd(pos + 5) &&
isConstraint(pos + 6) &&
isPrimary(pos + 8) &&
isKey(pos + 9));
};

export const isAlterTableAddForeignKey = (tokens: Token[]) => {
Expand All @@ -151,7 +162,18 @@ export const isAlterTableAddForeignKey = (tokens: Token[]) => {
isAdd(pos + 3) &&
isConstraint(pos + 4) &&
isForeign(pos + 6) &&
isKey(pos + 7));
isKey(pos + 7)) ||
(isAlter(pos) &&
isTable(pos + 1) &&
isAdd(pos + 5) &&
isForeign(pos + 6) &&
isKey(pos + 7)) ||
(isAlter(pos) &&
isTable(pos + 1) &&
isAdd(pos + 5) &&
isConstraint(pos + 6) &&
isForeign(pos + 8) &&
isKey(pos + 9));
};

export const isAlterTableAddUnique = (tokens: Token[]) => {
Expand All @@ -166,7 +188,13 @@ export const isAlterTableAddUnique = (tokens: Token[]) => {
isTable(pos + 1) &&
isAdd(pos + 3) &&
isConstraint(pos + 4) &&
isUnique(pos + 6));
isUnique(pos + 6)) ||
(isAlter(pos) && isTable(pos + 1) && isAdd(pos + 5) && isUnique(pos + 6)) ||
(isAlter(pos) &&
isTable(pos + 1) &&
isAdd(pos + 5) &&
isConstraint(pos + 6) &&
isUnique(pos + 8));
};

const DataTypes: ReadonlyArray<string> = Array.from(
Expand Down
80 changes: 80 additions & 0 deletions packages/schema-sql-parser/src/schema_sql_test_case.md
Original file line number Diff line number Diff line change
Expand Up @@ -827,6 +827,30 @@ ALTER TABLE Persons ADD CONSTRAINT PK_Person PRIMARY KEY (ID,LastName)
}
```

### Alter database.Table Add PRIMARY KEY

```sql
ALTER TABLE "public".Persons ADD PRIMARY KEY (ID)
ALTER TABLE "public".Persons ADD CONSTRAINT PK_Person PRIMARY KEY (ID,LastName)
```

```json
{
"statements": [
{
"type": "alter.table.add.primaryKey",
"name": "Persons",
"columnNames": ["ID"]
},
{
"type": "alter.table.add.primaryKey",
"name": "Persons",
"columnNames": ["ID", "LastName"]
}
]
}
```

### Alter Table Add FOREIGN KEY

```sql
Expand Down Expand Up @@ -859,6 +883,38 @@ FOREIGN KEY (PersonID) REFERENCES Persons(PersonID)
}
```

### Alter database.Table Add FOREIGN KEY

```sql
ALTER TABLE "public".Orders
ADD FOREIGN KEY (PersonID) REFERENCES "public".Persons(PersonID)

ALTER TABLE "public".Orders
ADD CONSTRAINT FK_PersonOrder
FOREIGN KEY (PersonID) REFERENCES "public".Persons(PersonID)
```

```json
{
"statements": [
{
"type": "alter.table.add.foreignKey",
"name": "Orders",
"columnNames": ["PersonID"],
"refTableName": "Persons",
"refColumnNames": ["PersonID"]
},
{
"type": "alter.table.add.foreignKey",
"name": "Orders",
"columnNames": ["PersonID"],
"refTableName": "Persons",
"refColumnNames": ["PersonID"]
}
]
}
```

### Alter Table Add UNIQUE

```sql
Expand All @@ -882,3 +938,27 @@ ALTER TABLE Persons ADD CONSTRAINT UC_Person UNIQUE (ID,LastName)
]
}
```

### Alter database.Table Add UNIQUE

```sql
ALTER TABLE "public".Persons ADD UNIQUE (ID)
ALTER TABLE "public".Persons ADD CONSTRAINT UC_Person UNIQUE (ID,LastName)
```

```json
{
"statements": [
{
"type": "alter.table.add.unique",
"name": "Persons",
"columnNames": ["ID"]
},
{
"type": "alter.table.add.unique",
"name": "Persons",
"columnNames": ["ID", "LastName"]
}
]
}
```

0 comments on commit 51522c1

Please sign in to comment.