Skip to content

Commit

Permalink
RENAME TABLES (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
JelleZijlstra authored Jun 14, 2022
1 parent ba5b6a7 commit 705a77b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
29 changes: 29 additions & 0 deletions sqltree/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,20 @@ class Truncate(Statement):
table: TableName


@dataclass
class TableTo(Node):
table: TableName
to_kw: Keyword = field(compare=False, repr=False)
new_table: TableName


@dataclass
class RenameTables(Statement):
rename_kw: Keyword = field(compare=False, repr=False)
table_kw: Keyword = field(compare=False, repr=False)
tables: Sequence[WithTrailingComma[TableTo]]


@dataclass
class DatabaseClause(Node):
kw: Keyword # FROM or IN
Expand Down Expand Up @@ -1884,6 +1898,20 @@ def _parse_truncate(p: Parser) -> Truncate:
return Truncate((), kw, table_kw, table)


def _parse_table_to(p: Parser) -> TableTo:
old = _parse_table_name(p)
to = _expect_keyword(p, "TO")
new = _parse_table_name(p)
return TableTo(old, to, new)


def _parse_rename(p: Parser) -> RenameTables:
kw = _expect_keyword(p, "RENAME")
table_kw = _expect_keyword(p, "TABLE")
table_to = _parse_comma_separated(p, _parse_table_to)
return RenameTables((), kw, table_kw, table_to)


def _parse_create(p: Parser) -> CreateTable:
kw = _expect_keyword(p, "CREATE")
temporary = _maybe_consume_keyword(p, "TEMPORARY")
Expand Down Expand Up @@ -1927,6 +1955,7 @@ def _parse_create(p: Parser) -> CreateTable:
"FLUSH": _parse_flush,
"TRUNCATE": _parse_truncate,
"CREATE": _parse_create,
"RENAME": _parse_rename,
}


Expand Down
5 changes: 5 additions & 0 deletions tests/test_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -733,3 +733,8 @@ def test_create_table() -> None:
assert format("create table x like y") == "CREATE TABLE x LIKE y\n"
assert format("create table x (like y)") == "CREATE TABLE x (LIKE y)\n"
assert format("create table a.b (like c.d)") == "CREATE TABLE a.b (LIKE c.d)\n"


def test_rename_tables() -> None:
assert format("rename table x to y") == "RENAME TABLE x TO y\n"
assert format("rename table x to y, z to w") == "RENAME TABLE x TO y, z TO w\n"

0 comments on commit 705a77b

Please sign in to comment.