SQG(SQL到代码生成器)现在支持Java流和列表类型。
SQG (SQL to Code Generator) v0.10: Java Streams and List Type Support

原始链接: https://sqg.dev/blog/java-streams-and-list-types/

## SQG v0.10.0:增强的 SQL 代码生成 SQG 是一款工具,可以从带有注释的 SQL 查询中生成类型安全的数据库访问代码(TypeScript 和 Java),并在构建时针对您的实际数据库进行验证。最新版本 v0.10.0 为 Java 代码生成带来了关键改进。 现在,Java 方法可以返回 `Stream` 以及 `List`,从而实现延迟评估和高效处理大型结果集,避免不必要的内存使用。此外,数组列(如 `TEXT[]`)现在可以正确映射到 `List`、`List` 等。 DuckDB appender 也收到更新,现在可以正确支持列表/数组列类型,以实现高效的批量插入。 使用 `npm install -g @sqg/cli@latest` 升级到 v0.10.0,并在 GitHub 上探索完整的源代码和游乐场。

## SQG:SQL到代码生成器更新 SQG的新版本(v0.10)已发布。SQG是一个工具,可以将SQL查询生成Java代码(特别是Java Streams和List类型)。Hacker News上的公告引发了关于最佳实践的讨论——一位评论者建议使用自定义类型代替通用的“Stream”,以明确指示资源关闭要求。 对话中也提到了相关的项目。一位用户分享了他们使用“pgroutiner”的经验,这是一个类似的从PostgreSQL生成C#代码的工具,这促使他们开发了一种更灵活的REST API方法,称为“npgsqlrest”。另一位用户询问了实际用例,质疑直接查询数据库是否更简单。最后,将SQG与现有的工具(如SQLDelight和SQLC)进行了比较,并指出SQG具有相似的语法。
相关文章

原文

SQG is a type-safe SQL code generator — you write .sql files with annotated queries, and it generates strongly-typed database access code for TypeScript and Java by introspecting your queries against real database engines at build time.

Here’s what’s new in v0.10.0.

Generated Java code now includes methods that return Stream<T> in addition to List<T>. This gives you lazy evaluation, easier composition with the standard library, and avoids materializing large result sets into memory when you don’t need to.

try (Stream<User> users = queries.getAllUsersStream()) {

users.forEach(user -> process(user));

The stream holds a reference to the underlying ResultSet, so it needs to be closed after use — hence the try-with-resources.

Array columns like TEXT[] or INTEGER[] are now handled correctly in generated Java code. Previously these types could produce incorrect mappings — they now resolve to proper List<String>, List<Integer>, etc.

The DuckDB appender now supports list/array column types. If your table has a column like tags VARCHAR[], the generated appender method accepts the corresponding list type and writes it correctly using DuckDB’s bulk insert API.


Upgrade with npm install -g @sqg/[email protected] or update your project’s dependency. Full source on GitHub. Try it out in the playground.

联系我们 contact @ memedata.com