4 phút đọc
huandu/go-sqlbuilder: A flexible and powerful SQL string builder library plus a zero-config ORM. – Repository huandu/go-sqlbuilder. Mô tả: A flexible and powerful SQL string builder library plus a zero-config ORM.. 1730 stars, 145 forks, ngôn ngữ chính Go, cập nhật 2026-10-21.
1. Repository làm gì?
The sqlbuilder package offers a comprehensive suite of SQL string concatenation utilities. It is designed to facilitate the construction of SQL statements compatible with Go's standard library sql.DB and sql.Stmt interfaces, focusing on optimizing the performance of SQL statement creation and minimizing memory usage.
The primary objective of this package's design was to craft a SQL construction library that operates independently of specific database drivers and business logic. It is tailored to accommodate the diverse needs of enterprise environments, including the use of custom database drivers, adherence to specialized operational standards, integration into heterogeneous systems, and handling of non-standard SQL in intricate scenarios. Following its open-source release, the package has undergone extensive testing within a large-scale enterprise context, successfully managing the workload of hundreds of millions of orders daily and nearly ten million transactions daily, thus highlighting its robust performance and scalability.
This package is not restricted to any particular database driver and does not automatically establish connections with any database systems. It does not presuppose the execution of the generated SQL, making it versatile for a broad spectrum of application scenarios that involve the construction of SQL-like statements. It is equally well-suited for further development aimed at creating more business-specific database interaction packages, ORMs, and similar tools.
Install this package by executing the following command:
We can rapidly construct SQL statements using this package.
In common scenarios, it is necessary to escape all user inputs. To achieve this, initialize a builder at the outset.
2. Dữ liệu và cấu trúc đáng chú ý
This package includes the following pre-defined builders. API documentation and usage examples are available in the godoc online documentation.
A unique method, SQL(sql string), is implemented across all statement builders, enabling the insertion of any arbitrary SQL segment into a builder during SQL construction. This feature is particularly beneficial for crafting SQL statements that incorporate non-standard syntax required by OLTP or OLAP systems.
Below are several utility methods designed to address special cases.
For detailed instructions on utilizing these builders, consult the examples provided on GoDoc.
WHERE clause is the most important part of a SQL. We can use Where method to add one or more conditions to a builder.
To simplify the construction of WHERE clauses, a utility type named Cond is provided for condition building. All builders that support WHERE clauses possess an anonymous Cond field, enabling the invocation of Cond methods on these builders.
There are many methods for building conditions.
3. Khả năng ứng dụng và giới hạn
There are also some methods to combine conditions.
Share WHERE clause among builders
Due to the importance of the WHERE statement in SQL, we often need to continuously append conditions and even share some common WHERE conditions among different builders. Therefore, we abstract the WHERE statement into a WhereClause struct, which can be used to create reusable WHERE conditions.
The following example illustrates how to transfer a WHERE clause from a SelectBuilder to an UpdateBuilder.
UpdateBuilder.From emits a FROM clause for PostgreSQL, SQLite, and SQLServer flavors (it is ignored by other flavors). When a CTE includes tables created with CTETable, those table names are emitted before any explicit From(…) tables.
Refer to the WhereClause examples to learn its usage.
The ORDER BY clause is commonly used to sort query results. This package provides convenient methods to build ORDER BY clauses with proper ordering directions.
For scenarios where you need to order by multiple columns with different directions (ASC/DESC), use OrderByAsc and OrderByDesc methods. These methods can be chained to add multiple columns with their specific ordering.
The older OrderBy method combined with Asc/Desc is still available but deprecated, as it only supports a single ordering direction for all columns. The new OrderByAsc and OrderByDesc methods provide more flexibility and clarity when working with multiple columns.
Số liệu repository có thể thay đổi; nên mở liên kết nguồn để kiểm tra README, giấy phép và trạng thái phát hành.
Nguồn tham khảo: Xem bài gốc