Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
MySQL Workbench Plugin - Query to PHP Array Format
##
# 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
@nojimage

This comment has been minimized.

Copy link
Owner Author

@nojimage nojimage commented Nov 16, 2010

軽くテストした限りではそこそこ動いたけど、Workbench自体がハングアップしてくれるテーブルもあったりしました。ので、ご利用は用法用量を守って正しくお使いください。

@alphabraga

This comment has been minimized.

Copy link

@alphabraga alphabraga commented Oct 11, 2014

Save my day!

@purefan

This comment has been minimized.

Copy link

@purefan purefan commented Nov 21, 2014

Trying this in MySQL Workbench 6.2 on Mac and I get this error:
Error during "Execute Query Into Text Output (PHP)"
error calling Python module function QueryToPHPFormat.executeQueryAsTextPhp

@z3niths

This comment has been minimized.

Copy link

@z3niths z3niths commented Jul 25, 2017

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment