SQLer:无需编程语言即可将SQL查询转换为RESTful API的工具

SQLer是一个微型http服务器,用Go语言编写,将旧的CGI概念应用于SQL查询。SQLer允许编写端点并分配一个SQL查询,以便任何人点击它时能执行查询。此外SQLer还允许自定义验证规则,可验证请求正文或查询参数。SQLer使用nginx样式配置语言(HCL)。

SQLer功能

  • 无需依赖,可独立使用;
  • 支持多种数据可类型,包括:SQL Server, MYSQL, SQLITE, PostgreSQL, Cockroachdb等;
  • 内置RESTful服务器;
  • 内置RESP Redis协议,可以使用任何redis客户端连接到SQLer;
  • 内置Javascript解释器,可轻松转换结果;
  • 内置验证器;
  • 自动使用预备语句;
  • 使用(HCL)配置语言;
  • 可基于unix glob模式加载多个配置文件;
  • 每条SQL查询可被命名为宏;
  • 在每个宏内可使用 Go text/template
  • 每个宏都有自己的Context(查询参数+正文参数)作为.Input(map [string] interface{}),而.Utils是辅助函数列表,目前它只包含SQLEscape;
  • 可自定义授权程序,授权程序只是一个简单的webhook,sqler使用这个webhook验证是否应该完成某请求。

下载

配置概况

// create a macro/endpoint called \u0026quot;_boot\u0026quot;,// this macro is private \u0026quot;used within other macros\u0026quot; // because it starts with \u0026quot;_\u0026quot;.// this rule only used within `RESTful` context._boot {    // the query we want to execute    exec = \u0026lt;\u0026lt;SQL        CREATE TABLE IF NOT EXISTS `users` (            `ID` INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,            `name` VARCHAR(30) DEFAULT \u0026quot;@anonymous\u0026quot;,            `email` VARCHAR(30) DEFAULT \u0026quot;@anonymous\u0026quot;,            `password` VARCHAR(200) DEFAULT \u0026quot;\u0026quot;,            `time` INT UNSIGNED        );    SQL}// adduser macro/endpoint, just hit `/adduser` with// a `?user_name=\u0026amp;user_email=` or json `POST` request// with the same fields.adduser {    // what request method will this macro be called    // default: [\u0026quot;ANY\u0026quot;]    // this only used within `RESTful` context.    methods = [\u0026quot;POST\u0026quot;]    // authorizers,    // sqler will attempt to send the incoming authorization header    // to the provided endpoint(s) as `Authorization`,    // each endpoint MUST return `200 OK` so sqler can continue, other wise,    // sqler will break the request and return back the client with the error occurred.    // each authorizer has a method and a url.    // this only used within `RESTful` context.    authorizers = [\u0026quot;GET http://web.hook/api/authorize\u0026quot;, \u0026quot;GET http://web.hook/api/allowed?roles=admin,root,super_admin\u0026quot;]    // the validation rules    // you can specify separated rules for each request method!    rules {        user_name = [\u0026quot;required\u0026quot;]        user_email =  [\u0026quot;required\u0026quot;, \u0026quot;email\u0026quot;]        user_password = [\u0026quot;required\u0026quot;, \u0026quot;stringlength: 5,50\u0026quot;]    }    // the query to be executed    exec = \u0026lt;\u0026lt;SQL       {{ template \u0026quot;_boot\u0026quot; }}        /* let's bind a vars to be used within our internal prepared statement */        {{ .BindVar \u0026quot;name\u0026quot; .Input.user_name }}        {{ .BindVar \u0026quot;email\u0026quot; .Input.user_email }}        {{ .BindVar \u0026quot;emailx\u0026quot; .Input.user_email }}        INSERT INTO users(name, email, password, time) VALUES(            /* we added it above */            :name,            /* we added it above */            :email,            /* it will be secured anyway because it is encoded */            '{{ .Input.user_password | .Hash \u0026quot;bcrypt\u0026quot; }}',            /* generate a unix timestamp \u0026quot;seconds\u0026quot; */            {{ .UnixTime }}        );        SELECT * FROM users WHERE id = LAST_INSERT_ID();    SQL}// list all databases, and run a transformer functiondatabases {    exec = \u0026quot;SHOW DATABASES\u0026quot;    transformer = \u0026lt;\u0026lt;JS        // there is a global variable called `$result`,        // `$result` holds the result of the sql execution.        (function(){            newResult = []            for ( i in $result ) {                newResult.push($result[i].Database)            }            return newResult        })()    JS}

支持的SQL引擎

  • sqlite3
  • mysql
  • postgresql
  • cockroachdb
  • sqlserver

支持的 Util

  • .Hash \u0026lt;method\u0026gt; - 使用指定的方法[md5,sha1,sha256,sha512,bcrypt]散列指定的输入, {{ \u0026quot;data\u0026quot; | .Hash \u0026quot;md5\u0026quot; }}
  • ·.UnixTime - 以秒为单位返回unit时间, {{ .UnixTime }}
  • .UnixNanoTime - 以纳秒为单位返回unix时间,{{ .UnixNanoTime }}
  • .Uniqid - 返回唯一ID,{{ .Uniqid }}

协议

SQLer遵循 Apache 2.0协议。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

2名名名名名名名名名名名名名名名名名名名

谢谢啊011702

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值