Skip to content

Commit

Permalink
Merge pull request kayak#627 from klen/master
Browse files Browse the repository at this point in the history
fix: mysql create/drop tables
  • Loading branch information
x8lucas8x authored Aug 27, 2021
2 parents 3d5f478 + 92e6a04 commit 46c90d1
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
16 changes: 16 additions & 0 deletions pypika/dialects.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@ def _builder(cls, **kwargs: Any) -> "MySQLQueryBuilder":
def load(cls, fp: str) -> "MySQLLoadQueryBuilder":
return MySQLLoadQueryBuilder().load(fp)

@classmethod
def create_table(cls, table: Union[str, Table]) -> "MySQLCreateQueryBuilder":
return MySQLCreateQueryBuilder().create_table(table)

@classmethod
def drop_table(cls, table: Union[str, Table]) -> "MySQLDropQueryBuilder":
return MySQLDropQueryBuilder().drop_table(table)


class MySQLQueryBuilder(QueryBuilder):
QUOTE_CHAR = "`"
Expand Down Expand Up @@ -212,6 +220,14 @@ def __str__(self) -> str:
return self.get_sql()


class MySQLCreateQueryBuilder(CreateQueryBuilder):
QUOTE_CHAR = "`"


class MySQLDropQueryBuilder(DropQueryBuilder):
QUOTE_CHAR = "`"


class VerticaQuery(Query):
"""
Defines a query class for use with Vertica.
Expand Down
20 changes: 19 additions & 1 deletion pypika/tests/dialects/test_mysql.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import unittest

from pypika import MySQLQuery, QueryException, Table
from pypika import MySQLQuery, QueryException, Table, Column


class SelectTests(unittest.TestCase):
Expand Down Expand Up @@ -71,3 +71,21 @@ def test_load_from_file(self):
"LOAD DATA LOCAL INFILE '/path/to/file' INTO TABLE `abc` FIELDS TERMINATED BY ','",
str(q2),
)


class TableTests(unittest.TestCase):
table_abc = Table("abc")

def test_create_table(self):
q = MySQLQuery.create_table(self.table_abc).columns(Column("id", "INT"))
self.assertEqual(
'CREATE TABLE `abc` (`id` INT)',
str(q),
)

def test_drop_table(self):
q = MySQLQuery.drop_table(self.table_abc)
self.assertEqual(
'DROP TABLE `abc`',
str(q),
)

0 comments on commit 46c90d1

Please sign in to comment.