Skip to content

Commit

Permalink
alter_data
Browse files Browse the repository at this point in the history
  • Loading branch information
LoveXiaoLiu committed Nov 21, 2019
1 parent 7c8ea44 commit 0a655e7
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 16 deletions.
86 changes: 86 additions & 0 deletions mysqltokenparser/constant.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# coding: utf-8

# sql type
SQL_TYPE_DDL = 'ddl'
SQL_TYPE_DML = 'dml'
SQL_TYPE_TRANSACTION = 'transaction'
SQL_TYPE_REPLICATION = 'replication'
SQL_TYPE_PREPARED = 'prepared'
SQL_TYPE_ADMINISTRATION = 'administration'
SQL_TYPE_UTILITY = 'utility'

# ddl type
DDL_TYPE_CREATEDATABASE = 'createdatabase'
DDL_TYPE_CREATEEVENT = 'createevent'
DDL_TYPE_CREATEINDEX = 'createindex'
DDL_TYPE_CREATELOGFILEGROUP = 'createlogfilegroup'
DDL_TYPE_CREATEPROCEDURE = 'createprocedure'
DDL_TYPE_CREATEFUNCTION = 'createfunction'
DDL_TYPE_CREATESERVER = 'createserver'
DDL_TYPE_CREATETABLE = 'createtable'
DDL_TYPE_CREATETABLESPACEINNODB = 'createtablespaceinnodb'
DDL_TYPE_CREATETABLESPACENDB = 'createtablespacendb'
DDL_TYPE_CREATETRIGGER = 'createtrigger'
DDL_TYPE_CREATEVIEW = 'createview'
DDL_TYPE_ALTERDATABASE = 'alterdatabase'
DDL_TYPE_ALTEREVENT = 'alterevent'
DDL_TYPE_ALTERFUNCTION = 'alterfunction'
DDL_TYPE_ALTERINSTANCE = 'alterinstance'
DDL_TYPE_ALTERLOGFILEGROUP = 'alterlogfilegroup'
DDL_TYPE_ALTERPROCEDURE = 'alterprocedure'
DDL_TYPE_ALTERSERVER = 'alterserver'
DDL_TYPE_ALTERTABLE = 'altertable'
DDL_TYPE_ALTERTABLESPACE = 'altertablespace'
DDL_TYPE_ALTERVIEW = 'alterview'
DDL_TYPE_DROPDATABASE = 'dropdatabase'
DDL_TYPE_DROPEVENT = 'dropevent'
DDL_TYPE_DROPINDEX = 'dropindex'
DDL_TYPE_DROPLOGFILEGROUP = 'droplogfilegroup'
DDL_TYPE_DROPPROCEDURE = 'dropprocedure'
DDL_TYPE_DROPFUNCTION = 'dropfunction'
DDL_TYPE_DROPSERVER = 'dropserver'
DDL_TYPE_DROPTABLE = 'droptable'
DDL_TYPE_DROPTABLESPACE = 'droptablespace'
DDL_TYPE_DROPTRIGGER = 'droptrigger'
DDL_TYPE_DROPVIEW = 'dropview'
DDL_TYPE_RENAMETABLE = 'renametable'
DDL_TYPE_TRUNCATETABLE = 'truncatetable'

# dml type
DML_TYPE_SELECTSTATEMENT = 'select'
DML_TYPE_INSERTSTATEMENT = 'insert'
DML_TYPE_UPDATESTATEMENT = 'update'
DML_TYPE_DELETESTATEMENT = 'delete'
DML_TYPE_REPLACESTATEMENT = 'replace'
DML_TYPE_CALLSTATEMENT = 'call'
DML_TYPE_LOADDATASTATEMENT = 'loaddata'
DML_TYPE_LOADXMLSTATEMENT = 'loadxml'
DML_TYPE_DOSTATEMENT = 'do'
DML_TYPE_HANDLERSTATEMENT = 'handler'

# table option
TABLE_OPTION_ENGINE = 'engine'
TABLE_OPTION_AUTOINCREMENT = 'autoincrement'
TABLE_OPTION_AVERAGE = 'average'
TABLE_OPTION_CHARSET = 'charset'
TABLE_OPTION_CHECKSUM = 'checksum'
TABLE_OPTION_COLLATE = 'collate'
TABLE_OPTION_COMMENT = 'comment'
TABLE_OPTION_COMPRESSION = 'compression'
TABLE_OPTION_CONNECTION = 'connection'
TABLE_OPTION_DATADIRECTORY = 'datadirectory'
TABLE_OPTION_DELAY = 'delay'
TABLE_OPTION_ENCRYPTION = 'encryption'
TABLE_OPTION_INDEXDIRECTORY = 'indexdirectory'
TABLE_OPTION_INSERTMETHOD = 'insertmethod'
TABLE_OPTION_KEYBLOCKSIZE = 'keyblocksize'
TABLE_OPTION_MAXROWS = 'maxrows'
TABLE_OPTION_MINROWS = 'minrows'
TABLE_OPTION_PACKKEYS = 'packkeys'
TABLE_OPTION_PASSWORD = 'password'
TABLE_OPTION_ROWFORMAT = 'rowformat'
TABLE_OPTION_RECALCULATION = 'recalculation'
TABLE_OPTION_PERSISTENT = 'persistent'
TABLE_OPTION_SAMPLEPAGE = 'samplepage'
TABLE_OPTION_TABLESPACE = 'tablespace'
TABLE_OPTION_UNION = 'union'
21 changes: 17 additions & 4 deletions mysqltokenparser/mysqltokenparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@
from MySqlLexer import MySqlLexer
from MySqlParser import MySqlParser
from MySqlParserListener import MySqlParserListener
from sqltypemixins import CreateTableMixin, AlterTableMixin
from sqltypemixins.altertable import AlterTableMixin
from sqltypemixins.createtable import CreateTableMixin
from constant import SQL_TYPE_DDL


class MyListener(AlterTableMixin, CreateTableMixin, MySqlParserListener):
def __init__(self, ret):
self.ret = ret

def enterDdlStatement(self, ctx):
self.ret['type'] = 'ddl'
self.ret['type'] = SQL_TYPE_DDL

@staticmethod
def _get_last_name(ctx):
Expand Down Expand Up @@ -69,5 +71,16 @@ def mysql_token_parser(sql):


if __name__ == "__main__":
print mysql_token_parser(u"""ALTER TABLE t_a_gun2_6_dw_pfm_emp_cm ADD INDEX
idx_eob_date(empid_org_bus (200),pfm_date);""")
print mysql_token_parser(u"""CREATE TABLE tab_name (
id int NOT NULL AUTO_INCREMENT COMMENT '主键',
uid int NOT NULL COMMENT '唯一流水id',
name varchar(20) NOT NULL DEFAULT '' COMMENT '名称',
amount int NOT NULL DEFAULT 0 COMMENT '数量',
create_date date NOT NULL DEFAULT '1000-01-01' COMMENT '创建日期',
create_time datetime DEFAULT '1000-01-01 00:00:00' COMMENT '创建时间',
update_time timestamp default current_timestamp on update current_timestamp COMMENT '更新时间(会自动更新,不需要刻意程序更新)',
PRIMARY KEY (id),
UNIQUE KEY uniq_uid (uid, ccccc),
KEY idx_name (name, wwwww),
KEY idx_cscscsc (data, nunm, ssw)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='测试表';""")
2 changes: 0 additions & 2 deletions mysqltokenparser/sqltypemixins/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
# coding: utf-8

from .createtable import *
from .altertable import *
3 changes: 2 additions & 1 deletion mysqltokenparser/sqltypemixins/altertable.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@

from mysqltokenparser.utils import iterchild
from mysqltokenparser.MySqlParser import MySqlParser
from mysqltokenparser.constant import DDL_TYPE_ALTERTABLE


class AlterTableMixin:
def enterAlterTable(self, ctx):
data = {}
self.ret['data'] = {
'type': 'altertable',
'type': DDL_TYPE_ALTERTABLE,
'data': data
}
alter_data = data.setdefault('alter_data', [])
Expand Down
21 changes: 16 additions & 5 deletions mysqltokenparser/sqltypemixins/createtable.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,37 @@
# coding: utf-8
from mysqltokenparser.utils import iterchild
from mysqltokenparser.MySqlParser import MySqlParser
from mysqltokenparser.constant import *


class CreateTableMixin:
"""
create table type: copyCreateTable queryCreateTable columnCreateTable
just support columnCreateTable.
"""
def enterColumnCreateTable(self, ctx):
data = {}
self.ret['data'] = {
'type': 'createtable',
'type': DDL_TYPE_CREATETABLE,
'data': data
}

children = ctx.children
for child in children:
if isinstance(child, MySqlParser.TableNameContext):
data['tablename'] = self._get_last_name(child)
data['table_name'] = self._get_last_name(child)

if isinstance(child, MySqlParser.CreateDefinitionsContext):
data['createdefinitions'] = self._enterCreateDefinitions(child)
data['create_definitions'] = self._enterCreateDefinitions(child)

if isinstance(child, MySqlParser.TableOptionEngineContext):
data.update(self._enterTableOptionEngine(child))
data[TABLE_OPTION_ENGINE] = self._enterTableOptionEngine(
child).get(TABLE_OPTION_ENGINE)

if isinstance(child, MySqlParser.TableOptionCharsetContext):
data.update(self._enterTableOptionCharset(child))
data[TABLE_OPTION_CHARSET] = self._enterTableOptionCharset(
child).get(TABLE_OPTION_CHARSET)

@iterchild
def _enterTableOptionCharset(self, child, ret):
Expand Down
9 changes: 5 additions & 4 deletions tests/test_createtable.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,11 @@ def test_createtable(response):
"""

tokens = mtp.mysql_token_parser(sql)
print(tokens)
assert isinstance(tokens, dict)

hope_tablename = 'tab_name'
assert hope_tablename == tokens['data']['data']['tablename']
assert hope_tablename == tokens['data']['data']['table_name']

hope_engine = 'InnoDB'
assert hope_engine == tokens['data']['data']['engine']
Expand All @@ -52,11 +53,11 @@ def test_createtable(response):
assert hope_charset == tokens['data']['data']['charset']

hope_column_len = 7
assert hope_column_len == len(tokens['data']['data']['createdefinitions']['columns'])
assert hope_column_len == len(tokens['data']['data']['create_definitions']['columns'])

hope_common_index_len = 2
assert hope_common_index_len == len(tokens['data']['data']['createdefinitions']['indexs']['common_key'])
assert hope_common_index_len == len(tokens['data']['data']['create_definitions']['indexs']['common_key'])

hope_columnname = ["id", "uid", "name", "amount", "create_date", "create_time", "update_time"]
for i in tokens['data']['data']['createdefinitions']['columns']:
for i in tokens['data']['data']['create_definitions']['columns']:
assert i['columnname'] in hope_columnname

0 comments on commit 0a655e7

Please sign in to comment.