Created
November 16, 2010 11:49
-
-
Save nojimage/701728 to your computer and use it in GitHub Desktop.
MySQL Workbench Plugin - Query to PHP Array Format
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
## | |
# MySQL Workbench - Query to PHP Array Format Plugin | |
# | |
# author: nojimage | |
# | |
# original: http://wb.mysql.com/?p=677 verticalquery_grt.py by Alfredo Kojima | |
## | |
# import the wb module | |
from wb import * | |
# import the grt module | |
import grt | |
# import the mforms module for GUI stuff | |
import mforms | |
# define this Python module as a GRT module | |
ModuleInfo = DefineModule(name= "QueryToPHPFormat", author= "nojimage", version="1.0") | |
@ModuleInfo.plugin("wbblog.executeToTextOutputPhp", caption= "Execute Query Into Text Output (PHP)", input= [wbinputs.currentQueryBuffer()], pluginMenu= "SQL/Utilities") | |
@ModuleInfo.export(grt.INT, grt.classes.db_query_QueryBuffer) | |
def executeQueryAsTextPhp(qbuffer): | |
editor= qbuffer.owner | |
sql= qbuffer.selectedText or qbuffer.script | |
resultsets= editor.executeScript(sql) | |
editor.addToOutput("// Query Output:\n", 1) | |
for result in resultsets: | |
column_length = 0 | |
for column in result.columns: | |
column_length = max(column_length, len(column.name)) | |
editor.addToOutput("// > %s\n\n" % result.sql, 0) | |
rows = [] | |
ok= result.goToFirstRow() | |
while ok: | |
rows.append("/******************** %s. row *********************/\n" % (result.currentRow+1)) | |
rows.append("array(\n") | |
for column in result.columns: | |
rows.append("%s => '%s',\n" % (("'" + column.name + "'").ljust(column_length + 2), result.stringFieldValueByName(column.name))) | |
rows.append("),\n") | |
ok= result.nextRow() | |
# much faster to do it at once than add lines one by one | |
editor.addToOutput("".join(rows), 0) | |
editor.addToOutput("\n// %i rows\n" % (result.currentRow+1), 0) | |
return 0 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
6.2 ++ cannot use above code now.
I have done this for someone who need to use it.
https://github.com/z3niths/MYSQL-Workbench-PHP-Array-Plugins