-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
caoshuai03
committed
Nov 25, 2019
1 parent
115f60c
commit e88ba16
Showing
6 changed files
with
96 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,4 @@ | |
from select import * | ||
from insert import * | ||
from delete import * | ||
from update import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# coding: utf-8 | ||
import antlr4 | ||
from mysqltokenparser.utils import iterchild | ||
from mysqltokenparser.MySqlParser import MySqlParser | ||
from mysqltokenparser.constant import * | ||
|
||
|
||
class UpdateMixin(object): | ||
""" | ||
delete type: | ||
singleDeleteStatement multipleDeleteStatement | ||
""" | ||
def enterUpdateStatement(self, ctx): | ||
data = {} | ||
self.ret['data'] = { | ||
'type': DML_TYPE_UPDATESTATEMENT, | ||
'data': data | ||
} | ||
|
||
children = ctx.children | ||
for child in children: | ||
if isinstance(child, MySqlParser.MultipleUpdateStatementContext): | ||
data.update(self._enterMultipleUpdateStatement(child)) | ||
|
||
if isinstance(child, MySqlParser.SingleUpdateStatementContext): | ||
data.update(self._enterSingleUpdateStatement(child)) | ||
|
||
@iterchild | ||
def _enterMultipleUpdateStatement(self, child, ret): | ||
pass | ||
|
||
@iterchild | ||
def _enterSingleUpdateStatement(self, child, ret): | ||
if isinstance(child, MySqlParser.TableNameContext): | ||
ret[TABLE_NAME] = self._get_last_name(child) | ||
|
||
if isinstance(child, MySqlParser.PredicateExpressionContext): | ||
ret.update(self._enterPredicateExpression(child)) | ||
|
||
if isinstance(child, MySqlParser.UpdatedElementContext): | ||
update_elements = ret.setdefault('update_elements', []) | ||
update_elements.append(self._enterUpdatedElement(child).get('update_element')) | ||
|
||
@iterchild | ||
def _enterUpdatedElement(self, child, ret): | ||
update_element = ret.setdefault('update_element', []) | ||
if isinstance(child, MySqlParser.FullColumnNameContext): | ||
update_element.append(self._get_last_name(child)) | ||
if isinstance(child, MySqlParser.PredicateExpressionContext): | ||
update_element.extend(self._enterPredicateExpression(child).get('where_expression')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#!/usr/bin/env python | ||
# coding:utf-8 | ||
|
||
import pytest | ||
from helper import mysqltokenparser as mtp | ||
from helper import constant as _c | ||
|
||
|
||
def test_simpleselect(): | ||
sql = u""" | ||
UPDATE table_name SET field1='new-value1', field2='new-value2' WHERE id=23 and name="css"; | ||
""" | ||
|
||
tokens = mtp.mysql_token_parser(sql) | ||
print(tokens) | ||
assert isinstance(tokens, dict) |