sequel ace 无法格式化sql(使用ruby替代php脚本解决问题)

问题描述

由于Format Sql默认使用php脚本,会提示错误:

在这里插入图片描述
原因是目前的mac系统(macos 12.5)中缺少php程序。第一时间想到是用本人电脑里已有的python来处理格式化,没去安装php。

配置python脚本(仍有问题)

修改 Bundles 》Format 》Format SQL 配置ruby脚本

#!/opt/homebrew/bin/python3
# encoding=utf-8

import urllib.request
import json
import sys

sql = sys.stdin.read()
params = {'sql': sql, 'reindent': 1,
          'keyword_case': 'upper', 'identifier_case': 'lower'}

req = urllib.request.Request(
    'https://sqlformat.org/api/v1/format', data=urllib.parse.urlencode(params).encode('utf-8'))
response = urllib.request.urlopen(req)

data = json.loads(response.read())
print(data['result'])

新的问题出现了:无权限调用python执行

官方issue中也提出了这个bug,还是open状态。

根据作者的建议,目前ruby还是支持在sandbox中调用的,于是改了脚本测试下,果然成功了。

ruby将来也可能会被系统移除,继续关注官方issue吧。

配置ruby脚本(已解决)

修改 Bundles 》Format 》Format SQL 配置ruby脚本

#!/usr/bin/ruby
# encoding=utf-8

require "json"
require "open-uri"  
require 'net/https'

sql = $stdin.read()

params = {'sql': sql, 'reindent': 1,
          'keyword_case': 'upper', 'identifier_case': 'lower'}

url = 'https://sqlformat.org/api/v1/format'  

uri = URI(url)
http = Net::HTTP.new(uri.host, uri.port)
    if uri.scheme == "https"
        http.use_ssl = true
        http.verify_mode = OpenSSL::SSL::VERIFY_NONE 
    end
req = Net::HTTP::Post.new(uri.path)
req.set_form_data(params)
res = http.request(req)
json_body = JSON.parse res.body
puts json_body['result']

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值