Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add 'Explain Query' bundle.

  • Loading branch information...
commit 61f98034d196395f6f4516179491f492a8207f23 0 parents
@mattlangtree mattlangtree authored
Showing with 132 additions and 0 deletions.
  1. +132 −0 Explain Query/Explain Query.spBundle/command.plist
132 Explain Query/Explain Query.spBundle/command.plist
@@ -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>
Please sign in to comment.
Something went wrong with that request. Please try again.