Skip to content

Commit c7a29ac

Browse files
committed
DevAPI: Schema.create_table
This patch adds method create_table to the Schema class to create a Table which returns the instance of the Table object if it was successfully created. New classes ColumnDef, ForeignKeyDef and GeneratedColumnDef is added. ColumnTypes class is updated with all the column types. Tests were added for regression.
1 parent d01aed5 commit c7a29ac

File tree

5 files changed

+826
-29
lines changed

5 files changed

+826
-29
lines changed

lib/mysqlx/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@
4242
DeleteStatement, UpdateStatement,
4343
CreateCollectionIndexStatement,
4444
DropCollectionIndexStatement, CreateViewStatement,
45-
AlterViewStatement)
45+
AlterViewStatement, ColumnDef,
46+
GeneratedColumnDef, ForeignKeyDef, Expr)
4647

4748

4849
def _parse_address_list(address_list):
@@ -235,4 +236,5 @@ def get_node_session(*args, **kwargs):
235236
"SelectStatement", "InsertStatement", "DeleteStatement", "UpdateStatement",
236237
"CreateCollectionIndexStatement", "DropCollectionIndexStatement",
237238
"CreateViewStatement", "AlterViewStatement",
239+
"ColumnDef", "GeneratedColumnDef", "ForeignKeyDef", "Expr",
238240
]

lib/mysqlx/crud.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
DeleteStatement, UpdateStatement,
3030
CreateCollectionIndexStatement,
3131
DropCollectionIndexStatement, CreateViewStatement,
32-
AlterViewStatement)
32+
AlterViewStatement, CreateTableStatement)
3333

3434

3535
_COUNT_VIEWS_QUERY = ("SELECT COUNT(*) FROM information_schema.views "
@@ -289,6 +289,16 @@ def alter_view(self, name):
289289
view = View(self, name)
290290
return view.get_alter_statement()
291291

292+
def create_table(self, name, reuse=False):
293+
if not name:
294+
raise ProgrammingError("Table name is invalid")
295+
table = Table(self, name)
296+
if not table.exists_in_database():
297+
return CreateTableStatement(self, name)
298+
elif not reuse:
299+
raise ProgrammingError("Table already exists")
300+
return table
301+
292302

293303
class Collection(DatabaseObject):
294304
"""Represents a collection of documents on a schema.

lib/mysqlx/result.py

Lines changed: 66 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -244,20 +244,72 @@ class ColumnType(object):
244244
MEDIUMINT = 4
245245
INT = 5
246246
BIGINT = 6
247-
FLOAT = 7
248-
DECIMAL = 8
249-
DOUBLE = 9
250-
JSON = 10
251-
STRING = 11
252-
BYTES = 12
253-
TIME = 13
254-
DATE = 14
255-
DATETIME = 15
256-
TIMESTAMP = 16
257-
SET = 17
258-
ENUM = 18
259-
GEOMETRY = 19
260-
XML = 20
247+
REAL = 7
248+
FLOAT = 8
249+
DECIMAL = 9
250+
NUMERIC = 10
251+
DOUBLE = 11
252+
JSON = 12
253+
STRING = 13
254+
BYTES = 14
255+
TIME = 15
256+
DATE = 16
257+
DATETIME = 17
258+
TIMESTAMP = 18
259+
SET = 19
260+
ENUM = 20
261+
GEOMETRY = 21
262+
XML = 22
263+
YEAR = 23
264+
CHAR = 24
265+
VARCHAR = 25
266+
BINARY = 26
267+
VARBINARY = 27
268+
TINYBLOB = 28
269+
BLOB = 29
270+
MEDIUMBLOB = 30
271+
LONGBLOB = 31
272+
TINYTEXT = 32
273+
TEXT = 33
274+
MEDIUMTEXT = 34
275+
LONGTEXT = 35
276+
277+
@classmethod
278+
def to_string(cls, needle):
279+
for key, value in vars(cls).items():
280+
if value == needle:
281+
return key
282+
283+
@classmethod
284+
def from_string(cls, key):
285+
return getattr(cls, key.upper(), None)
286+
287+
@classmethod
288+
def is_char(cls, col_type):
289+
return col_type in (cls.CHAR, cls.VARCHAR,)
290+
291+
@classmethod
292+
def is_binary(cls, col_type):
293+
return col_type in (cls.BINARY, cls.VARBINARY,)
294+
295+
@classmethod
296+
def is_text(cls, col_type):
297+
return col_type in (cls.TEXT, cls.TINYTEXT, cls.MEDIUMTEXT,
298+
cls.LONGTEXT,)
299+
300+
@classmethod
301+
def is_decimals(cls, col_type):
302+
return col_type in (cls.REAL, cls.DOUBLE, cls.FLOAT, cls.DECIMAL,
303+
cls.NUMERIC,)
304+
305+
@classmethod
306+
def is_numeric(cls, col_type):
307+
return col_type in (cls.BIT, cls.TINYINT, cls.SMALLINT, cls.MEDIUMINT,
308+
cls.INT, cls.BIGINT,)
309+
310+
@classmethod
311+
def is_finite_set(cls, col_type):
312+
return col_type in (cls.SET, cls.ENUM,)
261313

262314

263315
class ColumnProtoType(object):

0 commit comments

Comments
 (0)