Skip to content

Commit f47a963

Browse files
committed
on_action support
1 parent bee2f00 commit f47a963

5 files changed

Lines changed: 24 additions & 3 deletions

File tree

sql_convertor/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@
1313
__all__ = ["ParserFactory", "OutPutFactory"]
1414

1515

16-
__version__ = '0.0.7'
16+
__version__ = '0.0.8'

sql_convertor/source/sql/helper.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,6 @@ def parse_table_def(self, s, loc, tok):
107107

108108
def parse_char_set(self, s, loc, tok):
109109
return {"char_set": tok[0]}
110+
111+
def on_action(self, s, loc, tok):
112+
return {"default": tok[1]["field_name"]}

sql_convertor/source/sql/parser.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ def generate_parser(self):
3737
default = Literal("DEFAULT") + (string_literal | number | Literal("NULL") | keyword)
3838
comment = Literal("COMMENT") + string_literal
3939
auto_inc = Literal("AUTO_INCREMENT")
40+
on_action = Literal("ON UPDATE") + keyword
4041

4142
enclosing_int.setParseAction(self.helper.parse_field_length)
4243
enclosing_func.setParseAction(self.helper.parse_field_length_func)
@@ -47,8 +48,9 @@ def generate_parser(self):
4748
auto_inc.setParseAction(self.helper.parse_auto_inc)
4849
default.setParseAction(self.helper.parse_default_value)
4950
comment.setParseAction(self.helper.parse_comment)
51+
on_action.setParseAction(self.helper.on_action)
5052
# `c_add_dt` timestamp NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
51-
col_stm = keyword + type_ + Optional(enclosing_int | enclosing_func) + Optional(nullable) + Optional(auto_inc) + Optional(char_set) + Optional(default) + Optional(comment)
53+
col_stm = keyword + type_ + Optional(enclosing_int | enclosing_func) + Optional(nullable) + Optional(auto_inc) + Optional(char_set) + Optional(default) + Optional(on_action) + Optional(comment)
5254
col_stm.setParseAction(self.helper.parse_col)
5355

5456
# key

sql_convertor/tests/base.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
`c_is_delete` tinyint(1) NOT NULL DEFAULT '0' COMMENT '是否删除',
1313
`c_statement` VARCHAR(1000) CHARACTER SET UTF8MB4 COLLATE UTF8MB4_BIN DEFAULT '' COMMENT '声明字段',
1414
`c_record_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP COMMENT '记录时间',
15+
`c_test_update_dt` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间2',
1516
PRIMARY KEY (`c_id`),
1617
KEY `ix_company` (`c_company_id`) USING BTREE
1718
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='记录表';
@@ -99,7 +100,14 @@
99100
null=True,
100101
default=datetime.datetime.now,
101102
column_name="c_record_time")
103+
""",
102104
"""
105+
test_update_dt = peewee.TimestampField(
106+
verbose_name="修改时间2",
107+
null=True,
108+
default=datetime.datetime.now,
109+
column_name="c_test_update_dt")
110+
""",
103111
],
104112
[
105113
"""
@@ -138,7 +146,8 @@ def to_dict(self):
138146
"add_dt": self.add_dt,
139147
"is_delete": self.is_delete,
140148
"statement": self.statement,
141-
"record_time": self.record_time
149+
"record_time": self.record_time,
150+
"test_update_dt": self.test_update_dt
142151
}
143152
""",
144153
"""

sql_convertor/tests/test_sql_parser.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,10 @@ def test_c_record_time(self):
8686
'comment': '记录时间', 'default': 'CURRENT_TIMESTAMP', 'field_name': 'C_RECORD_TIME',
8787
'field_type': 'TIMESTAMP', 'null': True
8888
}
89+
90+
def test_test_update_dt(self):
91+
assert "C_TEST_UPDATE_DT" in self.field_name_row_map
92+
assert self.field_name_row_map["C_TEST_UPDATE_DT"] == {
93+
'comment': '修改时间2', 'default': 'CURRENT_TIMESTAMP', 'field_name': 'C_TEST_UPDATE_DT',
94+
'field_type': 'TIMESTAMP', 'null': True
95+
}

0 commit comments

Comments
 (0)