-
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
1 parent
ade8e86
commit 2ea1513
Showing
5 changed files
with
113 additions
and
4 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 |
---|---|---|
@@ -1,2 +1,5 @@ | ||
# coding: utf-8 | ||
|
||
from altertable import * | ||
from createtable import * | ||
from select 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
# coding: utf-8 | ||
from mysqltokenparser.utils import iterchild | ||
from mysqltokenparser.MySqlParser import MySqlParser | ||
from mysqltokenparser.constant import * | ||
|
||
|
||
class SelectMixin: | ||
""" | ||
select type: | ||
simpleSelect parenthesisSelect unionSelect unionParenthesisSelect | ||
""" | ||
def enterSimpleSelect(self, ctx): | ||
data = {} | ||
self.ret['data'] = { | ||
'type': DML_TYPE_SELECTSTATEMENT, | ||
'data': { | ||
'type': SELECT_TYPE_SIMPLESELECT, | ||
'data': data | ||
} | ||
} | ||
|
||
children = ctx.children | ||
for child in children: | ||
if isinstance(child, MySqlParser.QuerySpecificationContext): | ||
data.update(self._enterQuerySpecification(child)) | ||
|
||
@iterchild | ||
def _enterQuerySpecification(self, child, ret): | ||
if isinstance(child, MySqlParser.SelectElementsContext): | ||
ret.update(self._enterSelectElements(child)) | ||
|
||
if isinstance(child, MySqlParser.FromClauseContext): | ||
ret['from_clause'] = self._enterFromClause(child) | ||
|
||
@iterchild | ||
def _enterFromClause(self, child, ret): | ||
if isinstance(child, MySqlParser.TableSourcesContext): | ||
ret.update(self._enterTableSources(child)) | ||
|
||
if isinstance(child, MySqlParser.LogicalExpressionContext): | ||
ret.update(self._enterLogicalExpression(child)) | ||
|
||
@iterchild | ||
def _enterLogicalExpression(self, child, ret): | ||
pass | ||
|
||
@iterchild | ||
def _enterTableSources(self, child, ret): | ||
select_table = ret.setdefault('from_tables', []) | ||
if isinstance(child, MySqlParser.TableSourceBaseContext): | ||
select_table.append(self._enterTableSourceBase(child)) | ||
|
||
@iterchild | ||
def _enterTableSourceBase(self, child, ret): | ||
if isinstance(child, MySqlParser.AtomTableItemContext): | ||
ret.update(self._enterAtomTableItem(child)) | ||
|
||
@iterchild | ||
def _enterAtomTableItem(self, child, ret): | ||
if isinstance(child, MySqlParser.TableNameContext): | ||
ret[TABLE_NAME] = self._get_last_name(child) | ||
if isinstance(child, MySqlParser.UidContext): | ||
ret['as'] = self._get_last_name(child) | ||
|
||
@iterchild | ||
def _enterSelectElements(self, child, ret): | ||
select_element = ret.setdefault('select_element', []) | ||
if isinstance(child, MySqlParser.SelectColumnElementContext): | ||
select_element.append(self._enterSelectColumnElement(child)) | ||
|
||
@iterchild | ||
def _enterSelectColumnElement(self, child, ret): | ||
if isinstance(child, MySqlParser.FullColumnNameContext): | ||
ret[COLUMN_NAME] = self._get_last_name(child) | ||
if isinstance(child, MySqlParser.UidContext): | ||
ret['as'] = self._get_last_name(child) |
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,18 @@ | ||
#!/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""" | ||
select id as qq, name as ww, age as ee, class as rr | ||
from student as s, teacher as t | ||
where name='cs' and age=26; | ||
""" | ||
|
||
tokens = mtp.mysql_token_parser(sql) | ||
print(tokens) | ||
assert isinstance(tokens, dict) |