def doReformatSQLStatement(text, return_none_if_unsupported): from grt.modules import MysqlSqlFacade ast_list = MysqlSqlFacade.parseAstFromSqlScript(text) if len(ast_list) != 1: raise Exception("Error parsing statement") if type(ast_list[0]) is str: raise Exception("Error parsing statement: %s" % ast_list[0]) ast = ast_list[0] def trim_ast_fix_bq(text, node, add_rollup): s = node[0] v = node[1] c = node[2] # put back backquotes to identifiers, if there's any if s in ("ident", "ident_or_text"): begin = node[3] + node[4] end = node[3] + node[5] if begin > 0 and text[begin - 1] == '`' and text[end] == '`': v = "`%s`" % v.replace("`", "``") l = [] for i, nc in enumerate(c): l.append(trim_ast_fix_bq(text, nc, add_rollup)) if add_rollup and nc[0] == "olap_opt" and nc[1].upper( ) == "WITH" and (i == len(c) - 1 or c[i + 1][1].upper() != "ROLLUP"): l.append(("olap_opt", "ROLLUP", [])) return (s, v, l) formatter = formatter_for_statement_ast(ast) if formatter: # workaround a bug in parser where WITH ROLLUP is turned into WITH add_rollup = "WITH ROLLUP" in text.upper() p = formatter(trim_ast_fix_bq(text, ast, add_rollup)) return p.run() else: if return_none_if_unsupported: return None return text
def doReformatSQLStatement(text, return_none_if_unsupported): from grt.modules import MysqlSqlFacade ast_list = MysqlSqlFacade.parseAstFromSqlScript(text) if len(ast_list) != 1: raise Exception("Error parsing statement") if type(ast_list[0]) is str: raise Exception("Error parsing statement: %s" % ast_list[0]) ast = ast_list[0] def trim_ast_fix_bq(text, node, add_rollup): s = node[0] v = node[1] c = node[2] # put back backquotes to identifiers, if there's any if s in ("ident", "ident_or_text"): begin = node[3] + node[4] end = node[3] + node[5] if begin > 0 and text[begin-1] == '`' and text[end] == '`': v = "`%s`" % v.replace("`", "``") l = [] for i, nc in enumerate(c): l.append(trim_ast_fix_bq(text, nc, add_rollup)) if add_rollup and nc[0] == "olap_opt" and nc[1].upper() == "WITH" and (i == len(c)-1 or c[i+1][1].upper() != "ROLLUP"): l.append(("olap_opt", "ROLLUP", [])) return (s, v, l) formatter = formatter_for_statement_ast(ast) if formatter: # workaround a bug in parser where WITH ROLLUP is turned into WITH add_rollup = "WITH ROLLUP" in text.upper() p = formatter(trim_ast_fix_bq(text, ast, add_rollup)) return p.run() else: if return_none_if_unsupported: return None return text
def doReformatSQLStatement(text, return_none_if_unsupported): from grt.modules import MysqlSqlFacade ast_list = MysqlSqlFacade.parseAstFromSqlScript(text) if len(ast_list) != 1: raise Exception("Error parsing statement") if type(ast_list[0]) is str: raise Exception("Error parsing statement: %s" % ast_list[0]) helper = ASTHelper(text) curpos = 0 new_text = "" ast = ast_list[0] def trim_ast_fix_bq(text, node): s = node[0] v = node[1] c = node[2] # put back backquotes to identifiers, if there's any if s in ("ident", "ident_or_text"): begin = node[3] + node[4] end = node[3] + node[5] if begin > 0 and text[begin-1] == '`' and text[end] == '`': v = "`%s`" % v.replace("`", "``") l = [] for i in c: l.append(trim_ast_fix_bq(text, i)) return (s, v, l) formatter = formatter_for_statement_ast(ast) if formatter: p = formatter(trim_ast_fix_bq(text, ast)) return p.run() else: if return_none_if_unsupported: return None return text
def doReformatSQLStatement(text, return_none_if_unsupported): from grt.modules import MysqlSqlFacade ast_list = MysqlSqlFacade.parseAstFromSqlScript(text) if len(ast_list) != 1: raise Exception("Error parsing statement") if type(ast_list[0]) is str: raise Exception("Error parsing statement: %s" % ast_list[0]) helper = ASTHelper(text) curpos = 0 new_text = "" ast = ast_list[0] def trim_ast_fix_bq(text, node): s = node[0] v = node[1] c = node[2] # put back backquotes to identifiers, if there's any if s in ("ident", "ident_or_text"): begin = node[3] + node[4] end = node[3] + node[5] if begin > 0 and text[begin-1] == '`' and text[end] == '`': v = "`%s`" % v.replace("`", "``") l = [] for i in c: l.append(trim_ast_fix_bq(text, i)) return (s, v, l) formatter = formatter_for_statement_ast(ast) if formatter: p = formatter(trim_ast_fix_bq(text, ast)) return p.run() else: if return_none_if_unsupported: return None return text
def enbeautificate_old(editor): from grt.modules import MysqlSqlFacade text = editor.selectedText selectionOnly = True if not text: selectionOnly = False text = editor.script helper = ASTHelper(text) ok_count = 0 bad_count = 0 curpos = 0 new_text = "" ast_list = MysqlSqlFacade.parseAstFromSqlScript(text) for ast in ast_list: if type(ast) is str: # error print ast mforms.App.get().set_status_text("Cannot format invalid SQL: %s"%ast) return 1 else: if 0: # debug from sql_reformatter import dump_tree import sys dump_tree(sys.stdout, ast) s, v, c, _base, _begin, _end = ast begin, end = helper.get_ast_range(ast) new_text += text[curpos:begin].rstrip(" ") # strip spaces that would come before statement # The token range does not include the quotation char if the token is quoted. # So extend the range by one to avoid adding part of the original token to the output. if end < len(text): possible_quote_char = text[end] else: possible_quote_char = None if possible_quote_char == '\'' or possible_quote_char == '"' or possible_quote_char == '`': curpos = end + 1 else: curpos = end def trim_ast_fix_bq(text, node): s = node[0] v = node[1] c = node[2] # put back backquotes to identifiers, if there's any if s in ("ident", "ident_or_text"): begin = node[3] + node[4] end = node[3] + node[5] if begin > 0 and end < len(text) and text[begin-1] == '`' and text[end] == '`': v = "`%s`" % v.replace("`", "``") l = [] for i in c: l.append(trim_ast_fix_bq(text, i)) return (s, v, l) formatter = formatter_for_statement_ast(ast) if formatter: ok_count += 1 p = formatter(trim_ast_fix_bq(text, ast)) fmted = p.run() else: bad_count += 1 fmted = text[begin:end] new_text += fmted new_text += text[curpos:] if selectionOnly: editor.replaceSelection(new_text) else: editor.replaceContents(new_text) if bad_count > 0: mforms.App.get().set_status_text("Formatted %i statements, %i unsupported statement types skipped."%(ok_count, bad_count)) else: mforms.App.get().set_status_text("Formatted %i statements."%ok_count) return 0
def enbeautificate(editor): from grt.modules import MysqlSqlFacade text = editor.selectedText selectionOnly = True if not text: selectionOnly = False text = editor.script helper = ASTHelper(text) ok_count = 0 bad_count = 0 curpos = 0 new_text = "" ast_list = MysqlSqlFacade.parseAstFromSqlScript(text) for ast in ast_list: if type(ast) is str: # error print ast mforms.App.get().set_status_text("Cannot format invalid SQL: %s"%ast) return 1 else: if 0: # debug from sql_reformatter import dump_tree import sys dump_tree(sys.stdout, ast) s, v, c, _base, _begin, _end = ast begin, end = helper.get_ast_range(ast) new_text += text[curpos:begin].rstrip(" ") # strip spaces that would come before statement # The token range does not include the quotation char if the token is quoted. # So extend the range by one to avoid adding part of the original token to the output. if end < len(text): possible_quote_char = text[end] else: possible_quote_char = None if possible_quote_char == '\'' or possible_quote_char == '"' or possible_quote_char == '`': curpos = end + 1 else: curpos = end def trim_ast(node): s = node[0] v = node[1] c = node[2] l = [] for i in c: l.append(trim_ast(i)) return (s, v, l) formatter = formatter_for_statement_ast(ast) if formatter: ok_count += 1 p = formatter(trim_ast(ast)) fmted = p.run() else: bad_count += 1 fmted = text[begin:end] new_text += fmted new_text += text[curpos:] if selectionOnly: editor.replaceSelection(new_text) else: editor.replaceContents(new_text) if bad_count > 0: mforms.App.get().set_status_text("Formatted %i statements, %i unsupported statement types skipped."%(ok_count, bad_count)) else: mforms.App.get().set_status_text("Formatted %i statements."%ok_count) return 0