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 @ModuleInfo.export(grt.STRING, grt.STRING) def reformatSQLStatement(text): return doReformatSQLStatement(text, False) @ModuleInfo.plugin("wb.sqlide.enbeautificate", caption = "Reformat SQL Query", input=[wbinputs.currentQueryBuffer()]) @ModuleInfo.export(grt.INT, grt.classes.db_query_QueryBuffer) def enbeautificate(editor): """Reformat the selected SQL statements or the one under the cursor.""" from grt.modules import MysqlSqlFacade text = editor.selectedText selectionOnly = True if not text: selectionOnly = False text = editor.currentStatement ok_count = 0 bad_count = 0
for var in reversed(vars): begin, end = helper.get_ast_range(var) begin -= offset end -= offset name = query[begin:end] query = query[:begin] + "?" + query[end:] variables.insert(0, name) return query, columns, variables @ModuleInfo.plugin( "wb.sqlide.copyAsPHPQueryAndFetch", caption="Copy as PHP Code (Iterate SELECT Results)", input=[wbinputs.currentQueryBuffer()], pluginMenu="SQL/Utilities", ) @ModuleInfo.export(grt.INT, grt.classes.db_query_QueryBuffer) def copyAsPHPQueryAndFetch(qbuffer): """Copies PHP code to execute the query and iterate its results to the clipboard. The code will substitute @variables with parameter markers (?) and will bind them with matching PHP variables. The results will be bound to PHP variables matching the SELECTed column names or their aliases. """ sql = qbuffer.selectedText or qbuffer.script # try to parse the query and extract the columns it returns res = _parse_column_name_list_from_query(sql) if res: sql, column_names, variable_names = res else:
name = query[begin:end] query = query[:begin] + "?" + query[end:] variables.insert(0, name) duplicates = {} for i, c in enumerate(columns): if duplicates.has_key(c): columns[i] = "%s%i" % (c, duplicates[c]) duplicates[c] += 1 duplicates[c] = duplicates.get(c, 0)+1 return query, columns, variables @ModuleInfo.plugin("wb.sqlide.copyAsPHPQueryAndFetch", caption= "Copy as PHP Code (Iterate SELECT Results)", input= [wbinputs.currentQueryBuffer()], pluginMenu= "SQL/Utilities") @ModuleInfo.export(grt.INT, grt.classes.db_query_QueryBuffer) def copyAsPHPQueryAndFetch(qbuffer): """Copies PHP code to execute the query and iterate its results to the clipboard. The code will substitute @variables with parameter markers (?) and will bind them with matching PHP variables. The results will be bound to PHP variables matching the SELECTed column names or their aliases. """ sql= qbuffer.selectedText or qbuffer.script # try to parse the query and extract the columns it returns res = _parse_column_name_list_from_query(sql) if res: sql, column_names, variable_names = res else: column_names = None variable_names = None
offset = begin vars = find_child_nodes(ast, "variable") for var in reversed(vars): begin, end = helper.get_ast_range(var) begin -= offset end -= offset name = query[begin:end] query = query[:begin] + "?" + query[end:] variables.insert(0, name) return query, columns, variables @ModuleInfo.plugin("wb.sqlide.copyAsPHPQueryAndFetch", caption= "Copy as PHP Code (Iterate SELECT Results)", input= [wbinputs.currentQueryBuffer()], pluginMenu= "SQL/Utilities") @ModuleInfo.export(grt.INT, grt.classes.db_query_QueryBuffer) def copyAsPHPQueryAndFetch(qbuffer): """Copies PHP code to execute the query and iterate its results to the clipboard. The code will substitute @variables with parameter markers (?) and will bind them with matching PHP variables. The results will be bound to PHP variables matching the SELECTed column names or their aliases. """ sql= qbuffer.selectedText or qbuffer.script # try to parse the query and extract the columns it returns res = _parse_column_name_list_from_query(sql) if res: sql, column_names, variable_names = res else: column_names = None variable_names = None
p = formatter(trim_ast_fix_bq(text, ast, add_rollup)) return p.run() else: if return_none_if_unsupported: return None return text @ModuleInfo.export(grt.STRING, grt.STRING) def reformatSQLStatement(text): return doReformatSQLStatement(text, False) @ModuleInfo.plugin("wb.sqlide.enbeautificate", caption="Reformat SQL Query", input=[wbinputs.currentQueryBuffer()]) @ModuleInfo.export(grt.INT, grt.classes.db_query_QueryBuffer) def enbeautificate(editor): """Reformat the selected SQL statements or the one under the cursor.""" from grt.modules import MysqlSqlFacade text = editor.selectedText selectionOnly = True if not text: selectionOnly = False text = editor.currentStatement ok_count = 0 bad_count = 0
# import the wb module from wb import DefineModule, wbinputs # import the grt module import grt # import the mforms module for GUI stuff import mforms from sql_reformatter import formatter_for_statement_ast, ASTHelper # define this Python module as a GRT module ModuleInfo = DefineModule(name= "SQLIDEUtils", author= "Oracle Corp.", version="1.1") @ModuleInfo.plugin("wb.sqlide.executeToTextOutput", caption= "Execute Query Into Text Output", input= [wbinputs.currentQueryBuffer()], pluginMenu= "SQL/Utilities") @ModuleInfo.export(grt.INT, grt.classes.db_query_QueryBuffer) def executeQueryAsText(qbuffer): editor= qbuffer.owner sql= qbuffer.selectedText or qbuffer.script resultsets= editor.executeScript(sql) editor.addToOutput("Query Output:\n", 1) for result in resultsets: editor.addToOutput("> %s\n\n" % result.sql, 0) line= [] column_lengths=[] ncolumns= len(result.columns) for column in result.columns: line.append(column.name + " "*5) column_lengths.append(len(column.name)+5)
# # Created by Tom Kaminski ([email protected]) 2013-10-03 from wb import DefineModule, wbinputs import grt import mforms import re from sql_reformatter import formatter_for_statement_ast, ASTHelper from grt import log_info, log_error, log_warning, log_debug, log_debug2, log_debug3 # Define as a GRT module ModuleInfo = DefineModule(name= "DrupalUtils", author= "Tom Kaminski ([email protected])", version="1.0") ########################################## @ModuleInfo.plugin("wb.sqlide.stripTableBraces", caption = "Strip curly braces from table names", input=[wbinputs.currentQueryBuffer()], pluginMenu="SQL/Utilities") @ModuleInfo.export(grt.INT, grt.classes.db_query_QueryBuffer) def stripTableBraces(buffer): selectionOnly = True text = buffer.selectedText if not text: selectionOnly = False text = buffer.script text = re.sub(r'([ `]){([0-9,a-z,A-Z$_]+)}([ `])',r'\1\2\3',text) if selectionOnly: buffer.replaceSelection(text) else: buffer.replaceContents(text)