+Version 0.0.4 (2016-09-04)
+
+ Add MySQL-specific remove_directives() and process_statement().
+
Version 0.0.3 (2016-09-04)
Rename the project: mysql2py -> sqlconvert.
import sys
from sqlconvert.print_tokens import print_tokens
-from sqlconvert.process_mysql import requote_names
+from sqlconvert.process_mysql import process_statement
from sqlconvert.process_tokens import find_error, StatementGrouper
print("----------")
if find_error(statement):
print("ERRORS IN QUERY")
- requote_names(statement)
+ process_statement(statement)
print_tokens(statement)
print()
statement._pprint_tree()
import sys
from sqlconvert.print_tokens import print_tokens
-from sqlconvert.process_mysql import requote_names
+from sqlconvert.process_mysql import process_statement
from sqlconvert.process_tokens import find_error, StatementGrouper
print("----------")
if find_error(statement):
print("ERRORS IN QUERY")
- requote_names(statement)
+ process_statement(statement)
print_tokens(statement)
print()
statement._pprint_tree()
import sys
from sqlparse import parse
from sqlconvert.print_tokens import print_tokens
-from sqlconvert.process_mysql import requote_names
+from sqlconvert.process_mysql import process_statement
from sqlconvert.process_tokens import find_error
print("----------")
if find_error(parsed):
print("ERRORS IN QUERY")
- requote_names(parsed)
+ process_statement(parsed)
print_tokens(parsed)
print()
parsed._pprint_tree()
import sys
from sqlparse import parse
from sqlconvert.print_tokens import print_tokens
-from sqlconvert.process_mysql import requote_names
+from sqlconvert.process_mysql import process_statement
from sqlconvert.process_tokens import find_error
print("----------")
if find_error(parsed):
print("ERRORS IN QUERY")
- requote_names(parsed)
+ process_statement(parsed)
print_tokens(parsed)
print()
parsed._pprint_tree()
from sqlparse.compat import text_type
from sqlconvert.print_tokens import print_tokens
-from sqlconvert.process_mysql import requote_names
+from sqlconvert.process_mysql import process_statement
from sqlconvert.process_tokens import StatementGrouper
from m_lib.defenc import default_encoding
grouper.process_line(line)
if grouper.statements:
for statement in grouper.get_statements():
- requote_names(statement)
+ process_statement(statement)
print_tokens(statement, outfile=outfile,
encoding=output_encoding)
tokens = grouper.close()
-__version__ = '0.0.3'
+__version__ = '0.0.4'
-from sqlparse.tokens import Name
+from sqlparse.sql import Comment
+from sqlparse import tokens as T
def requote_names(token_list):
"""Remove backticks, quote non-lowercase identifiers"""
for token in token_list.flatten():
- if token.ttype is Name:
+ if token.ttype is T.Name:
value = token.value
if (value[0] == "`") and (value[-1] == "`"):
value = value[1:-1]
token.normalized = token.value = value
else:
token.normalized = token.value = '"%s"' % value
+
+
+def remove_directives(statement):
+ """Remove /*! directives */ from the first-level"""
+ new_tokens = []
+ for token in statement.tokens:
+ if isinstance(token, Comment):
+ subtokens = token.tokens
+ if subtokens:
+ comment = subtokens[0]
+ if comment.ttype is T.Comment.Multiline and \
+ comment.value.startswith('/*!'):
+ continue
+ new_tokens.append(token)
+ statement.tokens = new_tokens
+
+
+def process_statement(statement):
+ requote_names(statement)
+ remove_directives(statement)
from sqlparse import parse
from sqlconvert.print_tokens import tlist2str
-from sqlconvert.process_mysql import requote_names
+from sqlconvert.process_mysql import requote_names, remove_directives, \
+ process_statement
from tests import main
class TestTokens(unittest.TestCase):
- def test_requote(self):
- parsed = parse("select * from `T`")[0]
- requote_names(parsed)
- query = tlist2str(parsed)
- self.assertEqual(query, 'SELECT * FROM "T"')
-
def test_encoding(self):
parsed = parse("insert into test (1, 'тест')", 'utf-8')[0]
query = tlist2str(parsed).encode('utf-8')
query = tlist2str(parsed)
self.assertEqual(query, u"INSERT INTO test (1, 'тест')")
+ def test_requote(self):
+ parsed = parse("select * from `T`")[0]
+ requote_names(parsed)
+ query = tlist2str(parsed)
+ self.assertEqual(query, 'SELECT * FROM "T"')
+
+ def test_directives(self):
+ parsed = parse("select /*! test */ * from /* test */ `T`")[0]
+ remove_directives(parsed)
+ query = tlist2str(parsed)
+ self.assertEqual(query, 'SELECT * FROM /* test */ `T`')
+
+ def test_process(self):
+ parsed = parse("select /*! test */ * from /* test */ `T`")[0]
+ process_statement(parsed)
+ query = tlist2str(parsed)
+ self.assertEqual(query, 'SELECT * FROM /* test */ "T"')
+
if __name__ == "__main__":
main()