Skip to content

Commit

Permalink
Add 'Explain Query' bundle.
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Langtree committed Jun 23, 2011
0 parents commit 61f9803
Showing 1 changed file with 132 additions and 0 deletions.
132 changes: 132 additions & 0 deletions Explain Query/Explain Query.spBundle/command.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>author</key>
<string>Hans-Jörg Bibiko</string>
<key>category</key>
<string>Query Editor</string>
<key>command</key>
<string>
# Since this command will execute SQL statements at the front most
# document window's connection check for a passed process ID to cancel
# this command by displaying a tooltip. Otherwise the loop after
# “open "sequelpro://$SP_PROCESS_ID@passToDoc/ExecuteQuery"”
# won't break.
if [ -z $SP_PROCESS_ID ]; then
echo "&lt;font color=red&gt;No front most connection window found!&lt;/font&gt;"
exit $SP_BUNDLE_EXIT_SHOW_AS_HTML_TOOLTIP
fi
# send query to Sequel Pro
cat &lt;&lt;SQL &gt; "$SP_QUERY_FILE"
EXPLAIN EXTENDED `cat`
SQL
# execute the SQL statement; the result will be available in the file $SP_QUERY_RESULT_FILE
open "sequelpro://$SP_PROCESS_ID@passToDoc/ExecuteQuery"
# wait for Sequel Pro; status file will be written to disk if query was finished
while [ 1 ]
do
[[ -e "$SP_QUERY_RESULT_STATUS_FILE" ]] &amp;&amp; break
sleep 0.01
done
# check for errors
if [ `cat $SP_QUERY_RESULT_STATUS_FILE` == 1 ]; then
echo "Nothing found to EXPLAIN"
exit $SP_BUNDLE_EXIT_SHOW_AS_HTML_TOOLTIP
fi
# remove file hand shake files
rm -f $SP_QUERY_RESULT_STATUS_FILE
rm -f $SP_QUERY_FILE
# process the EXPLAIN result
cat $SP_QUERY_RESULT_FILE | perl -e '
# get the table header
$firstline = &lt;&gt;;
@header = split(/\t/, $firstline);
# read all result lines
@allresults = ();
while($line = &lt;&gt;) {
my @a = split(/\t/, $line);
push(@allresults, \@a);
}
print &lt;&lt;HEAD;
&lt;html&gt;
&lt;head&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;center&gt;
&lt;h3&gt;EXPLAIN&lt;/h3&gt;
&lt;table width=200px&gt;
&lt;tr&gt;&lt;td align=center&gt;
HEAD
# do a syntax highlighting for the passed query
qx/cp $ENV{"SP_BUNDLE_INPUT"} $ENV{"SP_QUERY_FILE"}/;
$task = "sequelpro://".$ENV{'SP_PROCESS_ID'}."@"."SyntaxHighlighting/html";
qx/open "$task"/;
while(!(-e $ENV{'SP_QUERY_RESULT_STATUS_FILE'})) { sleep(0.01); }
open(RF, "&lt;:utf8", $ENV{'SP_QUERY_RESULT_FILE'}) or die $!;
while(&lt;RF&gt;) { print; }
close(RF);
qx/rm -f $ENV{'SP_QUERY_RESULT_FILE'}/;
qx/rm -f $ENV{'SP_QUERY_RESULT_STATUS_FILE'}/;
qx/rm -f $ENV{'SP_QUERY_FILE'}/;
print "&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;\n";
# print the result as vertical table
print "&lt;table&gt;\n";
for($i = 0; $i &lt;= $#header; $i++) {
print "&lt;tr&gt;&lt;td align=right&gt;&lt;b&gt;$header[$i]:&lt;/b&gt;&lt;/td&gt;&lt;td&gt;&amp;nbsp;&lt;/td&gt;";
foreach $line (@allresults) {
$r = $line-&gt;[$i];
$r =~ s/;\s*/&lt;br&gt;/g;
print "&lt;td&gt;$r&lt;/td&gt;";
}
print "&lt;/tr&gt;\n";
}
print &lt;&lt;FOOTER;
&lt;/table&gt;
&lt;/center&gt;
&lt;/body&gt;
&lt;/html&gt;
FOOTER
'</string>
<key>contact</key>
<string>znvy@ovovxb.qr</string>
<key>description</key>
<string>Takes the current query or selection and execute the SQL statement:
EXPLAIN EXTENDED "the_query"
and displays the result as tooltip.
Version 1.0</string>
<key>input</key>
<string>selectedtext</string>
<key>input_fallback</key>
<string>currentquery</string>
<key>keyEquivalent</key>
<string></string>
<key>name</key>
<string>Explain Query</string>
<key>output</key>
<string>showashtmltooltip</string>
<key>scope</key>
<string>inputfield</string>
<key>tooltip</key>
<string>EXPLAIN EXTENDED the current query</string>
<key>uuid</key>
<string>D8E68049-C53E-44EA-ADBA-E636F69F5F0E</string>
</dict>
</plist>

0 comments on commit 61f9803

Please sign in to comment.