Tuesday, April 26, 2011

Writing plugins/modules for MySQL Workbench

The example below copies first five rows of the current resultset to the editor below the selected text (goes to the top if no text is selected).


from wb import *
import grt

ModuleInfo = DefineModule(name= "MyModule", author= "My Name", version="1.0")

def generate_script(editor):
from string import strip
rs = editor.activeResultset
buffer = "\n"
if rs:
rows = min(5, rs.rowCount)
cols = len(rs.columns)
rs.goToFirstRow()
buffer += "-- Ans. "
for j in xrange(cols):
if j < cols - 1:
buffer += rs.columns[j].name + ", "
else:
buffer += rs.columns[j].name + "\n"
for i in xrange(rows):
buffer += '-- '
for j in xrange(cols):
if j < cols - 1:
buffer += strip(rs.stringFieldValue(j)) + ", "
else:
buffer += strip(rs.stringFieldValue(j)) + "\n"
rs.nextRow()
buffer += "\n"
return buffer


# because of a bug in the wbinputs.currentSQLEditor() input specifier from the wb module
# in Workbench 5.2.26, we include our own version of it here
def currentSQLEditor():
arg= grt.classes.app_PluginObjectInput()
arg.name= "activeSQLEditor"
arg.objectStructName= "db.query.Editor"
return arg


@ModuleInfo.plugin("my.plugin.fill_random_query", caption= "Copy Results to Editor", input=[currentSQLEditor()], pluginMenu="SQL/Utilities")
@ModuleInfo.export(grt.INT, grt.classes.db_query_Editor)
def fill_random_query(editor):
active_buffer = editor.activeQueryBuffer
script = active_buffer.script

try:
buffer = generate_script(editor)
except Exception, exc:
buffer = "\nError: %s\n" % exc

pos = active_buffer.selectionEnd
new_script = script[:pos] + buffer + script[pos:]
active_buffer.replaceContents(new_script)


return 0

Thursday, April 21, 2011

Eclipse Projects - Many types in one

How to make an Eclipse project be both PyDev and Java project?


File: .project


<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>Project-Name</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.python.pydev.PyDevBuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.python.pydev.pythonNature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>


File: .classpath


<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="java-src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="output" path="classes"/>
</classpath>


File: .pydevproject


<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?eclipse-pydev version="1.0"?>
<pydev_project>
<pydev_property name="org.python.pydev.PYTHON_PROJECT_INTERPRETER">Default</pydev_property>
<pydev_property name="org.python.pydev.PYTHON_PROJECT_VERSION">python 2.7</pydev_property>
<pydev_pathproperty name="org.python.pydev.PROJECT_SOURCE_PATH">
<path>/Project-Name/src</path>
</pydev_pathproperty>
</pydev_project>