{"id":13529443,"url":"https://github.com/x-ream/sqli","last_synced_at":"2025-05-15T00:08:37.117Z","repository":{"id":37588329,"uuid":"288747572","full_name":"x-ream/sqli","owner":"x-ream","description":"orm sql query builder,  API: QB, QB.X, QrB","archived":false,"fork":false,"pushed_at":"2024-05-29T18:44:00.000Z","size":2707,"stargazers_count":1899,"open_issues_count":19,"forks_count":812,"subscribers_count":327,"default_branch":"master","last_synced_at":"2025-04-11T10:16:30.421Z","etag":null,"topics":["clickhouse","impala","jdbc","mysql","orm","presto","query-builder","tdengine","xream"],"latest_commit_sha":null,"homepage":"http://sqli.xream.io","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/x-ream.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-08-19T14:02:22.000Z","updated_at":"2025-04-01T13:37:44.000Z","dependencies_parsed_at":"2023-02-05T01:16:29.150Z","dependency_job_id":"7866719c-c68c-4a4b-8871-1e636a5ba5e9","html_url":"https://github.com/x-ream/sqli","commit_stats":null,"previous_names":[],"tags_count":17,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/x-ream%2Fsqli","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/x-ream%2Fsqli/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/x-ream%2Fsqli/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/x-ream%2Fsqli/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/x-ream","download_url":"https://codeload.github.com/x-ream/sqli/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254249202,"owners_count":22039029,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["clickhouse","impala","jdbc","mysql","orm","presto","query-builder","tdengine","xream"],"created_at":"2024-08-01T07:00:36.354Z","updated_at":"2025-05-15T00:08:37.061Z","avatar_url":"https://github.com/x-ream.png","language":"Java","readme":"# sqli 简单的SQL拼写\n   [http://sqli.xream.io](http://sqli.xream.io) \n   \n[![license](https://img.shields.io/github/license/x-ream/sqli.svg)](https://www.apache.org/licenses/LICENSE-2.0.html)\n[![maven](https://img.shields.io/maven-central/v/io.xream.sqli/sqli-parent.svg)](https://search.maven.org/search?q=io.xream)\n[![Codacy Badge](https://app.codacy.com/project/badge/Grade/8e414bcc7a6944529c5a35b27b2d5e37)](https://www.codacy.com/gh/x-ream/sqli?utm_source=github.com\u0026amp;utm_medium=referral\u0026amp;utm_content=x-ream/sqli\u0026amp;utm_campaign=Badge_Grade)\n[![Gitter](https://badges.gitter.im/x-ream/x-ream.svg)](https://gitter.im/x-ream/community)\n    \n   [WIKI](https://github.com/x-ream/sqli/wiki)\n    \n    sqli/sqli-builder\n    sqli/sqli-core\n    sqli/sqli-dialect\n    sqli/sqli-repo\n        \n## sqli-repo \n\n### 使用方法\n    sqli仅仅是SQL的编程接口,需要整合到已有的框架或项目中,\n    在io.xream.x7项目里实现了和Spring-Boot/Spring-JdbcTemplate的整合\n    \n    @EnableX7Repostory  // code at x7/x7-spring-boot-starter\n    public class App{\n        main() \n        ....\n    }\n```xml        \n    \u003cdependency\u003e\n         \u003cgroupId\u003eio.xream.x7\u003c/groupId\u003e\n         \u003cartifactId\u003ex7-spring-boot-starter\u003c/artifactId\u003e\n         ....\n    \u003c/dependency\u003e\n```     \n    更多代码片段:\n\n    @Repository\n    public interface FooRepository extends BaseRepository\u003cFoo\u003e {}\n    @Repository\n    public interface BarRepository extends RepositoryX {}\n\n    @X.Mapping(\"t_foo\")//默认是foo\n    public class Foo {\n        @X.Key //不指定主键的情况下，不支持根据get(id),remove(id)\n        private Long id;\n        @X.Mapping(\"full_name\") //默认是fullName\n        private String fullName;\n    }\n\n    @Service\n    public class FooServiceImpl implements FooService {\n    \n        @Autowired\n        private FooRepository fooRepository;\n        @Autowired\n        private FooFindRepository fooFindRepository;\n        \n        // 临时表, 原生SQL, 则直接注入, 不支持代理\n        @Autowired\n        private TemporaryRepository temporaryRepository;\n        @Autowired\n        private NativeRepository nativeRepository;\n\n\n###    BaseRepository API\n\n            1. in(property, inList) //in查询, 例如: 页面上需要的主表ID或记录已经查出后，补充查询其他表的文本说明数据时使用\n            2. find(q) //标准拼接查询，返回对象形式记录，返回分页对象\n            3. list(q) //标准拼接查询，返回对象形式记录，不返回分页对象\n            4. get(Id) //根据主键查询记录\n            5. getOne(q) //数据库只有一条记录时，就返回那条记录\n            6. creaet(Object) //插入一条, 不支持返回自增键, 框架自带ID生成器\n            7. createOrReplace(Object) //插入或替换一条\n            8. createBatch(List\u003cObject\u003e) //批量插入\n            9. refresh( qr) //根据主键更新\n            10. refreshUnSafe( qr)//不根据主键更新\n            11. remove(Id)//根据主键删除\n            12. removeRefreshCreate(RemoveRefreshCreate\u003cT\u003e) //编辑页面列表时写数据库\n\n###     RepositoryX API\n            13. find(xq) //标准拼接查询，返回Map形式记录，返回分页对象\n            14. list(xq) //标准拼接查询，返回Map形式记录，不返回分页对象\n            15. listPlainValue(Class\u003cK\u003e, qx)//返回没有key的单列数据列表 (结果优化1)\n            16. findToHandle(xq, RowHandler\u003cMap\u003cString,Object\u003e\u003e) //流处理API\n\n###     QueryBuilder拼接API\n        QB // 返回q, 查出对象形式记录\n        QB.X //xq, 查出Map形式记录，支持连表查询\n        QrB //构建要更新的字段和条件\n        \n        代码片段:\n            {\n                QB qb = QB.of(Order.class); \n                qb.eq(\"userId\",obj.getUserId()).eq(\"status\",\"PAID\");\n                Q q = qb.build();\n                orderRepository.find(q);\n            }\n        \n            {\n                QB.X qbx =  QB.x();\n                qbx.resultKey(\"o.id\");\n                qbx.eq(\"o.status\",\"PAID\");\n                qbx.and(sub -\u003e sub.gt(\"o.createAt\",obj.getStartTime()).lt(\"o.createAt\",obj.getEndTime()));\n                qbx.or(sub -\u003e sub.eq(\"o.test\",obj.getTest()).or().eq(\"i.test\",obj.getTest()));\n                qbx.from(\"FROM order o INNER JOIN orderItem i ON i.orderId = o.id\");\n                qbx.paged(obj);\n                Q.X xq = qbx.build();\n                orderRepository.find(xq);\n            }\n            \n            {\n                orderRepository.refresh(\n                    QrB.of(Order.class).refresh(\"status\",\"PAYING\").eq(\"id\",1).eq(\"status\",\"UN_PAID\").build()\n                );\n            }\n        \n        条件构建API  (QB | QB.X)\n            1. or(sub) // or(sql)\n            2. or() // OR\n            3. eq // = (eq, 以及其它的API, 值为null，不会被拼接到SQL)\n            4. ne // !=\n            5. gt // \u003e\n            6. gte // \u003e=\n            7. lt // \u003c\n            8. lte // \u003c=\n            9. like //like %xxx%, if likeLeftRight =\u003e xxx, likeLeft =\u003e xxx%, then like =\u003e %xxx%\n            10. likeLeft // like xxx%\n            11. notLike // not like %xxx%\n            12. in // in\n            13. nin // not in\n            14. isNull // is null\n            15. nonNull // is not null\n            16. x // 简单的手写sql片段， 例如 x(\"foo.amount = bar.price * bar.qty\") , x(\"item.quantity = 0\")\n            17. sub(sql, sub) //\n            18. and(sub)\n\n        MAP查询结果构建API  (QB.X)\n            19. distinct //去重\n            20. reduce //归并计算\n                    // .reduce(ReduceType.SUM, \"dogTest.petId\") \n                    // .reduce(ReduceType.SUM, \"dogTest.petId\", Having.of(Op.GT, 1))\n                    //含Having接口 (仅仅在reduc查询后,有限支持Having)\n            21. groupBy //分组\n            22. select //指定返回列\n            23. selectWithFunc //返回列函数支持\n                    // .selectWithFunc(ResultKeyAlia.of(\"o\",\"at\"),\"FFF(o.createAt, ?)\", 100000) \n            24. resultWithDottedKey //连表查询返回非JSON格式数据,map的key包含\".\"  (结果优化2)\n           \n        连表构建API  (QB.X)\n            25. from(joinSql) //简单的连表SQL，不支持LEFT JOIN  ON 多条件; 多条件，请用API[28]\n            26. fromBuilder.of(Order.class,\"o\") //连表里的主表, API: .fromX(FromX fromX)\n            27. fromBuilder.JOIN(LEFT).of(OrderItem.class,\"i\")\n                                              .on(\"i.orderId = o.id\", \n            28                  on -\u003e on.gt(...)) //LEFT JOIN等, 更多条件\n            29. fromBuilder.sub(....,\"i\").JOIN(\"ANY INNER JOIN\").on(....) //fluent构建连表sql\n        \n        分页及排序API  (QB | QB.X)\n            30. sort(\"o.id\", Direction.DESC)\n            31. paged(pb -\u003e pb.ignoreTotalRows().page(1).rows(10).last(10000)) //设置last(long),会忽略page(int); \n                                           \n        更新构建API  ( qr)\n            32. refresh\n            \n        框架优化\n            froms/fromBuilder\n                如果条件和返回都不包括sourceScript里的连表，框架会优化移除连接（但目标连接表有用时，中间表不会\n                被移除）。\n                关闭优化: qb.withoutOptimization()\n            in\n                每500个条件会切割出一次in查询\n            \n        不支持项\n            union // 过于复杂\n\n            \n## 二级缓存 \n\n    在x7项目里实现了springBoot的Enable\n        \n    @EnableX7L2Caching\n    public class App{\n        main()\n\n    二级缓存是基于redis.multiGet的高速缓存实现。\n\n    二级缓存建议返回记录条数不超过20条。调用带二级缓存的API，返回记录条数超过了\n    20条，请关闭二级缓存。\n    如果需要开启二级缓存，所有对数据库的写操作项目都需要开启二级缓存。\n    \n    支持二级缓存的BaseRepository的API：\n            1. in(property, inList)\n            2. find(q)\n            3. list(q)\n            4. get(Id)\n            5. getOne(q)\n        \n    不支持二级缓存的BaseRepository, RepositoryX的API:\n            1. findX(xq)\n            2. listX(xq)\n            3. listPlainValue(xq)\n        \n    以上设计意味着，如果in和list查询返回记录条数超过20条, 二级缓存\n    会失去高速响应的效果，请务必关闭二级缓存. \n    如果需要返回很多条记录，需要自定义返回列, 请使用:\n        findX(xq)\n        listX(xq)\n        listPlainValue(xq)\n        \n    用户级的过滤\n    {\n        CacheFilter.filter(userId);\n        this.orderRepository.create(order); // refresh and remove\n    }\n    \n    {\n        CacheFilter.filter(userId);\n        this.orderRepository.find(q);\n    }               \n    ","funding_links":[],"categories":["ORM","数据库开发"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fx-ream%2Fsqli","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fx-ream%2Fsqli","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fx-ream%2Fsqli/lists"}