4 个版本 (2 个重大更新)

0.202107.0 2021年7月7日
0.202106.4 2021年6月10日
0.202106.3 2021年6月8日
0.202105.8 2021年5月23日

解析器实现 中排名 #1497

MIT 和可能 LGPL-3.0-or-later

595KB
4K SLoC

partiql-rs

用 Rust 编写的 PartiQL 实现

文档(进行中)

[tests.hello]
script = '''
cat<<EOS | pq -q "SELECT NAME, LOGNAME" -t json
{
  "SHELL": "/bin/bash",
  "NAME": "my machine name",
  "PWD": "/home/fuyutarow/partiql-rs",
  "LOGNAME": "fuyutarow",
  "HOME": "/home/fuyutarow",
  "LANG": "C.UTF-8",
  "USER": "fuyutarow",
  "HOSTTYPE": "x86_64",
  "_": "/usr/bin/env"
}
EOS
'''
tobe = '''
[
  {
    "NAME": "my machine name",
    "LOGNAME": "fuyutarow"
  }
]
'''

家族

内容 语言
pq 命令行界面 (brew, scoop)
partiql-rs Rust (cargo) https://crates.io/crates/partiql-rs
partiql-js JavaScript (npm) https://npmjs.net.cn/package/partiql-js
partiql-py Python (pip) https://pypi.ac.cn/project/partiql

常数表

功能

动机

什么是 PartiQL?

用法

格式化打印

选项 描述
-c, --compact 紧凑格式输出,而不是格式化输出,仅当输出为 JSON 时
-S, --sort-keys 输出对象键的排序。当 --to 选项为 json 时,当前有效
curl -s "https://api.github.com/repos/fuyutarow/partiql-rs/commits?per_page=1" | pq

转换文件格式

选项 描述
-f, --from 目标配置文件 [可能的值:csv, json, toml, yaml, xml]
-t, --to 目标配置文件 [可能的值:csv, json, toml, yaml, xml]

使用 -t 选项将 Json, Yaml, Toml 和 XML 转换为彼此。

cat pokemon.json | pq -t yaml
cat pokemon.json | pq -t yaml | pq -t toml

与现有命令 yj 的比较

格式 pq yj
JSON
TOML ⚠️*1
YAML
XML
CSV

*1 下述格式的 TOML 无法用 yj 序列化,但可以通过替换相应字段用 pq 序列化。

{
  "name": "partiql-pokemon",
  "dependencies": {
    "react": "^16.13.1",
    "react-dom": "^16.13.1"
  },
  "license": "MIT"
}
选项 描述
-q 查询
查询 描述
SELECT <字段路径>
SELECT <字段路径> AS <别名路径>

计算 BMI

  1. 下载文件然后在本地计算 BMI。
curl -s https://raw.githubusercontent.com/fuyutarow/pokemon.json/master/en/pokemon.json | pq -q "SELECT name, weight/height/height AS bmi ORDER BY bmi DESC LIMIT 20"
  1. 在终端中,向服务器发送查询以在远程计算 BMI。
curl https://partiql-pokemon.vercel.app/api/pokemon/ja -G --data-urlencode "q= SELECT name, weight/height/height AS bmi ORDER BY bmi DESC LIMIT 20"
  1. 在网页浏览器中,向服务器发送查询以在远程计算 BMI。

[“https://partiql-pokemon.vercel.app/api/pokemon/en?q= SELECT name, weight/height/height AS bmi ORDER BY bmi DESC LIMIT 20”] (https://partiql-pokemon.vercel.app/api/pokemon/ja?q= SELECT name, weight/height/height AS bmi ORDER BY bmi DESC LIMIT 20)

partiql-pokemon.vercel.app/api/pokemon/en?q= SELECT name, weight/height/height AS bmi ORDER BY bmi DESC LIMIT 20

[partiql-pokemon.vercel.app/api/pokemon/en?q= SELECT name, weight/height/height AS bmi ORDER BY bmi DESC LIMIT 20] (https://partiql-pokemon.vercel.app/api/pokemon/ja?q= SELECT name, weight/height/height AS bmi ORDER BY bmi DESC LIMIT 20)

https://partiql-pokemon.vercel.app/api/pokemon/ja?q= SELECT name, weight/height/height AS bmi ORDER BY bmi DESC LIMIT 20

安装

brew install fuyutarow/tap/pq
pq -h
scoop install pq
pq -h

转换数据

env | jo | pq "SELECT NAME AS name, USER AS user"

ip 命令仅在 Linux 和 WSL 中可用,不在 Mac 中。

ip -j -p | pq "$(cat<<EOS
SELECT
  address,
  info.family AS inet,
  info.local
FROM addr_info AS info
WHERE inet LIKE 'inet%'
EOS
)"
  • [x] SELECT
  • [x] FROM
  • [x] LEFT JOIN
  • [x] WHERE
  • [x] LIKE
  • [x] ORDER BY
  • [x] LIMIT

更多示例

测试

使用 tests-make 测试 CLI pq

brew install fuyutarow/tap/tests-make
tests-make tests-make/index.toml

makers test:pq
内容 测试 命令
pq 测试 makers 测试:pq
partiql-rs 测试 makers 测试:lib
partiql-js 测试 makers 测试:js
partiql-py 测试 makres 测试:py
全部 makers 测试

代码覆盖率

cargo install cargo-kcov
cargo kcov

makers cov

准备

makers install-dev

构建

makers build
makers build:pq ;: for pq commnad

LICENCE

附录

可提取字段的工具比较

jq 方法

curl -s "https://api.github.com/repos/fuyutarow/partiql-rs/commits?per_page=1" | jq  ".[].commit.author"

gron 方法

curl -s "https://api.github.com/repos/fuyutarow/partiql-rs/commits?per_page=1" | gron | grep "commit.author" | gron -u

nusehll 方法

curl -s "https://api.github.com/repos/fuyutarow/partiql-rs/commits?per_page=1" | from json | get commit.author | to json

pq 方法

curl -s "https://api.github.com/repos/fuyutarow/partiql-rs/commits?per_page=1" | pq -q "SELECT commit.author"

utils

  • makers

  1. https://github.com/sclevine/yj ↩︎

  2. https://github.com/stedolan/jq ↩︎

  3. https://github.com/tomnomnom/gron ↩︎

  4. https://github.com/nushell/nushell ↩︎

  5. https://github.com/fuyutarow/partiql-rs ↩︎

  6. https://github.com/sagiegurari/cargo-make … 运行 cargo install cargo-make 使用 makers 命令。 ↩︎

依赖

~17–35MB
~551K SLoC