Sequel自动生成Select语句

Sequel 是 Mac 上的一款不错的 mysql 可视化编辑, 它有一个非常好的功能是可以定制自己的插件, 这就是Bundles。 利用这个功能可以生成自己常用的SQL, 提高效率。 查询语句是最常用的, 下面就自己动手写了一个, 具体步骤如下:

1. 在顶部菜单栏点击 Bundles -> Bundles Editor 进入Bundles 编辑器

2. 左侧树形结构, 点击 Input Field 行Show (鼠标放上去才会显示show)

3. 点树形菜单底部的 + 号, 新增一个Bundle 

4. 在新增的Bundle里, 配置Bundle 选项:

  # Bundle Scope: Input Field 

  # Menu Label: General Select SQL

  # Output : Insert as Text

  # 可以配置快捷键

  注意:Scope 的选择的不对,会影响Output的配置项里没有Insert as Text

5. Command内容在代码区。 代码是使用的Shell + Perl 来实现的

6. 保存后就可以通过菜单选择、快捷键来使用了

注意:使用的时候, 焦点必须放在Run Query 区域, 否则会因为SQL没有地方放置而生成失败

解决办法:可以配置Output 为 Show as HTML, 这样会弹出个小框来展示生成的SQL,选择哪一种看个人喜好。

参考链接: https://sequelpro.com/docs/bundles/bundle-editor 

 1 # Check if one table is selected
 2 if [ -z "$SP_SELECTED_TABLE" ]; then
 3     echo "<font color=red>Please select a table.</font>"
 4     exit $SP_BUNDLE_EXIT_SHOW_AS_HTML_TOOLTIP
 5 fi
 6 
 7 if [ -z $SP_PROCESS_ID ]; then
 8     echo "<font color=red>No front most connection window found!</font>"
 9     exit $SP_BUNDLE_EXIT_SHOW_AS_HTML_TOOLTIP
10 fi
11 
12 # send query to Sequel Pro
13 echo "SELECT COLUMN_NAME FROM information_schema.columns WHERE TABLE_NAME ='${SP_SELECTED_TABLE}'" > "$SP_QUERY_FILE"
14 
15 # execute the SQL statement; the result will be available in the file $SP_QUERY_RESULT_FILE
16 open "sequelpro://$SP_PROCESS_ID@passToDoc/ExecuteQuery/csv"
17 
18 
19 # wait for Sequel Pro; status file will be written to disk if query was finished
20 while [ 1 ]
21 do
22     [[ -e "$SP_QUERY_RESULT_STATUS_FILE" ]] && break
23     sleep 0.01
24 done
25 
26 # check for errors
27 if [ `cat $SP_QUERY_RESULT_STATUS_FILE` == 1 ]; then
28     echo "Nothing found to SELECT ${SP_SELECTED_TABLE}"
29     exit $SP_BUNDLE_EXIT_SHOW_AS_HTML_TOOLTIP
30 fi
31 
32 # remove file hand shake files
33 rm -f $SP_QUERY_RESULT_STATUS_FILE
34 rm -f $SP_QUERY_FILE
35 
36 
37 # process the result
38 cat $SP_QUERY_RESULT_FILE | perl -e '
39 
40 # title
41 $firstline = <>;
42 
43 # column
44 chomp(my @file=<>);
45 my $columns = join(", ", @file);
46 $columns =~ s/"/`/g;
47 print "SELECT $columns FROM $ENV{'SP_SELECTED_TABLE'}\n"
48 
49 '

猜你喜欢

转载自www.cnblogs.com/dankewoniu/p/9054835.html