You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+46Lines changed: 46 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -17,6 +17,7 @@
17
17
-[Use `sql.Named` in a builder](#use-sqlnamed-in-a-builder)
18
18
-[Argument modifiers](#argument-modifiers)
19
19
-[Freestyle builder](#freestyle-builder)
20
+
-[Clone builders](#clone-builders)
20
21
-[Using special syntax to build SQL](#using-special-syntax-to-build-sql)
21
22
-[Interpolate `args` in the `sql`](#interpolate-args-in-the-sql)
22
23
-[License](#license)
@@ -381,6 +382,51 @@ fmt.Println(args)
381
382
// [1 2]
382
383
```
383
384
385
+
### Clone builders
386
+
387
+
The `Clone` methods make any builder reusable as a template. You can create a partially initialized builder once (even as a global), then call `Clone()` to get an independent copy to customize per request. This avoids repeated setup while keeping shared templates immutable and safe for concurrent use.
sb:= baseUserSelect.Clone() // start from the same template
422
+
sb.Where(sb.Equal("id", id))
423
+
sb.Limit(1)
424
+
return sb.Build()
425
+
}
426
+
```
427
+
428
+
The same template pattern applies to other builders. For example, keep a base `UpdateBuilder` with the table and common `SET` clauses, or a base `CTEBuilder` defining reusable CTEs, then `Clone()` and add query-specific `WHERE`/`ORDER BY`/`LIMIT`/`RETURNING` as needed.
429
+
384
430
### Using special syntax to build SQL
385
431
386
432
The `sqlbuilder` package incorporates special syntax for representing uncompiled SQL internally. To leverage this syntax for developing customized tools, the `Build` function can be utilized to compile it with the necessary arguments.
a.Equal("CREATE TABLE IF NOT EXISTS demo.user (id BIGINT(20) NOT NULL AUTO_INCREMENT PRIMARY KEY COMMENT \"user id\") DEFAULT CHARACTER SET utf8mb4", sql1)
119
+
a.Equal(0, len(args1))
120
+
121
+
a.Equal("CREATE TABLE IF NOT EXISTS demo.user (id BIGINT(20) NOT NULL AUTO_INCREMENT PRIMARY KEY COMMENT \"user id\", name VARCHAR(255) NOT NULL COMMENT \"user name\") DEFAULT CHARACTER SET utf8mb4", sql2)
0 commit comments