58

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

 5 years ago
source link: https://www.infoq.cn/article/LZxqJd-ZcNiUKcG1APDz?amp%3Butm_medium=referral
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

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 "_boot",
// this macro is private "used within other macros"
// because it starts with "_".
// this rule only used within `RESTful` context.
_boot{
// the query we want to execute
exec= <<SQL
CREATE TABLEIFNOTEXISTS `users` (
`ID` INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
`name` VARCHAR(30)DEFAULT"@anonymous",
`email` VARCHAR(30)DEFAULT"@anonymous",
`password` VARCHAR(200)DEFAULT"",
`time` INT UNSIGNED
);
SQL
}

// adduser macro/endpoint, just hit `/adduser` with
// a `?user_name=&user_email=` or json `POST` request
// with the same fields.
adduser {
// what request method will this macro be called
// default: ["ANY"]
// this only used within `RESTful` context.
methods = ["POST"]

// 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 = ["GET http://web.hook/api/authorize","GET http://web.hook/api/allowed?roles=admin,root,super_admin"]

// the validation rules
// you can specify separated rules for each request method!
rules {
user_name = ["required"]
user_email = ["required","email"]
user_password = ["required","stringlength: 5,50"]
}

// the query to be executed
exec= <<SQL
{{ template"_boot"}}

/* let's bind a vars to be used within our internal prepared statement */
{{ .BindVar"name".Input.user_name }}
{{ .BindVar"email".Input.user_email }}
{{ .BindVar"emailx".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 "bcrypt" }}',

/* generate a unix timestamp "seconds" */
{{ .UnixTime }}
);

SELECT*FROMusers WHERE id = LAST_INSERT_ID();
SQL
}

// list all databases, and run a transformer function
databases {
exec="SHOW DATABASES"

transformer = <<JS
// there is a global variable called `$result`,
// `$result` holds the result of the sql execution.
(function(){
newResult = []

for( iin$result ) {
newResult.push($result[i].Database)
}

return newResult
})()
JS
}

支持的 SQL 引擎

  • sqlite3
  • mysql
  • postgresql
  • cockroachdb
  • sqlserver

支持的 Util

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

协议

SQLer 遵循 Apache 2.0 协议。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK