Skip to content

Instantly share code, notes, and snippets.

@nojimage
Created November 16, 2010 11:49
Show Gist options
  • Save nojimage/701728 to your computer and use it in GitHub Desktop.
Save nojimage/701728 to your computer and use it in GitHub Desktop.
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
Copy link
Author

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

@alphabraga
Copy link

Save my day!

@purefan
Copy link

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
Copy link

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