Skip to content

Commit 46c90d1

Browse files
authored
Merge pull request #627 from klen/master
fix: mysql create/drop tables
2 parents 3d5f478 + 92e6a04 commit 46c90d1

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

pypika/dialects.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,14 @@ def _builder(cls, **kwargs: Any) -> "MySQLQueryBuilder":
7373
def load(cls, fp: str) -> "MySQLLoadQueryBuilder":
7474
return MySQLLoadQueryBuilder().load(fp)
7575

76+
@classmethod
77+
def create_table(cls, table: Union[str, Table]) -> "MySQLCreateQueryBuilder":
78+
return MySQLCreateQueryBuilder().create_table(table)
79+
80+
@classmethod
81+
def drop_table(cls, table: Union[str, Table]) -> "MySQLDropQueryBuilder":
82+
return MySQLDropQueryBuilder().drop_table(table)
83+
7684

7785
class MySQLQueryBuilder(QueryBuilder):
7886
QUOTE_CHAR = "`"
@@ -212,6 +220,14 @@ def __str__(self) -> str:
212220
return self.get_sql()
213221

214222

223+
class MySQLCreateQueryBuilder(CreateQueryBuilder):
224+
QUOTE_CHAR = "`"
225+
226+
227+
class MySQLDropQueryBuilder(DropQueryBuilder):
228+
QUOTE_CHAR = "`"
229+
230+
215231
class VerticaQuery(Query):
216232
"""
217233
Defines a query class for use with Vertica.

pypika/tests/dialects/test_mysql.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import unittest
22

3-
from pypika import MySQLQuery, QueryException, Table
3+
from pypika import MySQLQuery, QueryException, Table, Column
44

55

66
class SelectTests(unittest.TestCase):
@@ -71,3 +71,21 @@ def test_load_from_file(self):
7171
"LOAD DATA LOCAL INFILE '/path/to/file' INTO TABLE `abc` FIELDS TERMINATED BY ','",
7272
str(q2),
7373
)
74+
75+
76+
class TableTests(unittest.TestCase):
77+
table_abc = Table("abc")
78+
79+
def test_create_table(self):
80+
q = MySQLQuery.create_table(self.table_abc).columns(Column("id", "INT"))
81+
self.assertEqual(
82+
'CREATE TABLE `abc` (`id` INT)',
83+
str(q),
84+
)
85+
86+
def test_drop_table(self):
87+
q = MySQLQuery.drop_table(self.table_abc)
88+
self.assertEqual(
89+
'DROP TABLE `abc`',
90+
str(q),
91+
)

0 commit comments

Comments
 (0)