VVVYGD 最近的时间轴更新
VVVYGD

VVVYGD

V2EX 第 555191 号会员,加入于 2021-09-05 11:35:43 +08:00
VVVYGD 最近回复了
配享太庙
@f0rb 是的,这里用了一种类 SQL 的方式去转成 mongo 的查询,也可以实现其他的的 nosql 查询,虽然我也不太明确还有哪些语法,哈哈。
虽然有点倒反天罡,但是还是比较行的,讲解了动静态链 FFI 原理。用了 rust 之后,只觉得 C++在语法主义层面比 rust 好,不过开发工具 c++没有 rust 好,所以一直坚持用 rust 实现各种 api 业务。 快速的项目用 python 组合 Nextjs+ AI 写真的爽
@f0rb 因为这个是转换成 Expr 的,在 rust 里是可以做前置检查的。
@f0rb 其实就是类似 SQL 解析,这里 https://github.com/ddx2x/crossgate/blob/master/condition/src/cond.rs
https://github.com/ddx2x/crossgate/blob/master/condition/src/cond.y 将语法树解析为 Expr ,然后将解析的 Expr 转换成 mongo 的谓词条件 https://github.com/ddx2x/crossgate/blob/master/src/store/mongo/filter.rs ,当然其实这个过程直接转换成 mongo SDK 的对象 doc. 例如原本 sql:select username ,amount from users where local="abc" and(xx=1 or
b =2 and c=3) 我这边的使用写法是 new_condition().withTable("users").fields(["username","amount"]).wheres(#b" local="abc" && (xx=1 || b=2 && c=3)")
@f0rb 例如 :a=1&&b=2||c=1&&b=2&&abc='abc21' 转换成的就是
MongoFilter(Document({"$or": Array([Document({"$and": Array([Document({"a": Document({"$eq": Int64(1)})}), Document({"b": Document({"$eq": Int64(2)})})])}), Document({"$and": Array([Document({"c": Document({"$eq": Int64(1)})}), Document({"$and": Array([Document({"b": Document({"$eq": Int64(2)})}), Document({"abc": Document({"$eq": String("abc21")})})])})])})])})


参考 https://github.com/ddx2x/crossgate/blob/master/src/store/condition.rs 测试样例
差不多吧, a > 1 会转换成 yacc 语法解析成 field(a, gt,1). ->. mongo 的实现者转换成 真实的 {$gt:{a:1}}
@f0rb 汗,按个回车就回复了,我这里抽象 Condition<Filter> ,然后把 ab=123 & abc='123' 这种语法转成 mongo db 的 filter, 实现了大部份的逻辑,我小团队业务中用还是挺好用的,你可以看看。
关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   5181 人在线   最高记录 6679   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 18ms · UTC 01:21 · PVG 09:21 · LAX 18:21 · JFK 21:21
Developed with CodeLauncher
♥ Do have faith in what you're doing.