Skip to content

Commit 7609ebc

Browse files
committed
.gitignore + README
1 parent 6c5b9a6 commit 7609ebc

File tree

2 files changed

+131
-0
lines changed

2 files changed

+131
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ __pycache__
99
.devenv*
1010
devenv.local.nix
1111

12+
/bin/sqlc

examples/plugin-based-codegen/README.md

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,22 @@ SQLCDEBUG=processplugins=1 sqlc generate
5959

6060
## How It Works
6161

62+
### Architecture Flow
63+
64+
```
65+
┌─────────────────────────────────────────────────────────────────┐
66+
│ sqlc generate │
67+
│ │
68+
│ 1. Read schema.sql & queries.sql │
69+
│ 2. Send SQL to engine plugin (sqlc-engine-*) │
70+
│ └─> Parse SQL, return AST & Catalog │
71+
│ 3. Analyze queries with AST & Catalog │
72+
│ 4. Send queries + catalog to codegen plugin (sqlc-gen-*) │
73+
│ └─> Generate code (Rust, Go, etc.) │
74+
│ 5. Write generated files │
75+
└─────────────────────────────────────────────────────────────────┘
76+
```
77+
6278
### Database Engine Plugin (`sqlc-engine-sqlite3`)
6379

6480
The engine plugin implements the `pkg/engine.Handler` interface:
@@ -98,6 +114,62 @@ func main() {
98114

99115
Communication: **Protobuf over stdin/stdout**
100116

117+
### Parameter Passing: `sql_package` Example
118+
119+
For Go code generation, the `sql_package` parameter is passed to the codegen plugin:
120+
121+
```
122+
┌─────────────────────────────────────────────────────────────────┐
123+
│ sqlc.yaml │
124+
│ ┌───────────────────────────────────────────────────────────┐ │
125+
│ │ gen: │ │
126+
│ │ go: │ │
127+
│ │ sql_package: "database/sql" # or "pgx/v5" │ │
128+
│ └───────────────────────────────────────────────────────────┘ │
129+
│ │ │
130+
│ ▼ │
131+
│ ┌───────────────────────────────────────────────────────────┐ │
132+
│ │ GenerateRequest (protobuf) │ │
133+
│ │ Settings: │ │
134+
│ │ Codegen: │ │
135+
│ │ Options: []byte{ │ │
136+
│ │ "sql_package": "database/sql", # JSON │ │
137+
│ │ "package": "db", │ │
138+
│ │ ... │ │
139+
│ │ } │ │
140+
│ │ Queries: [...] │ │
141+
│ │ Catalog: {...} │ │
142+
│ └───────────────────────────────────────────────────────────┘ │
143+
│ │ │
144+
│ ▼ │
145+
│ ┌───────────────────────────────────────────────────────────┐ │
146+
│ │ Codegen Plugin (sqlc-gen-go or custom) │ │
147+
│ │ func generate(req *plugin.GenerateRequest) { │ │
148+
│ │ var opts Options │ │
149+
│ │ json.Unmarshal(req.PluginOptions, &opts) │ │
150+
│ │ // opts.SqlPackage == "database/sql" │ │
151+
│ │ // Generate code using database/sql APIs │ │
152+
│ │ } │ │
153+
│ └───────────────────────────────────────────────────────────┘ │
154+
└─────────────────────────────────────────────────────────────────┘
155+
```
156+
157+
**Important Notes:**
158+
159+
1. **Standard Go codegen** (`gen.go`) only supports:
160+
- `database/sql` (stdlib)
161+
- `pgx/v4` (PostgreSQL)
162+
- `pgx/v5` (PostgreSQL)
163+
164+
2. **Custom SQL packages** (e.g., `github.com/ydb-platform/ydb-go-sdk/v3`) require:
165+
- A **custom codegen plugin** that reads `sql_package` from `PluginOptions`
166+
- The plugin generates code using the specified package's APIs
167+
168+
3. **Example**: For YDB native SDK, you would create `sqlc-gen-ydb-go` that:
169+
- Reads `sql_package: "github.com/ydb-platform/ydb-go-sdk/v3"` from options
170+
- Generates code using `ydb.Session` instead of `*sql.DB`
171+
- Uses YDB-specific APIs for query execution
172+
101173
## Compatibility
102174

103175
Both plugins import public packages from sqlc:
@@ -177,6 +249,64 @@ pub async fn create_user(pool: &SqlitePool, id: i32, name: String, email: String
177249
}
178250
```
179251

252+
## Example: Go Codegen with Custom `sql_package`
253+
254+
For Go code generation, the standard `gen.go` only supports `database/sql`, `pgx/v4`, and `pgx/v5`. To use other SQL packages (e.g., `github.com/ydb-platform/ydb-go-sdk/v3`), you need a custom codegen plugin.
255+
256+
**Example: `sqlc-gen-ydb-go` plugin**
257+
258+
```go
259+
package main
260+
261+
import (
262+
"encoding/json"
263+
"github.com/sqlc-dev/sqlc/pkg/plugin"
264+
)
265+
266+
type Options struct {
267+
Package string `json:"package"`
268+
SqlPackage string `json:"sql_package"` // e.g., "github.com/ydb-platform/ydb-go-sdk/v3"
269+
Out string `json:"out"`
270+
}
271+
272+
func generate(req *plugin.GenerateRequest) (*plugin.GenerateResponse, error) {
273+
var opts Options
274+
json.Unmarshal(req.PluginOptions, &opts)
275+
276+
// opts.SqlPackage contains the value from sqlc.yaml
277+
// Generate code using the specified package's APIs
278+
if opts.SqlPackage == "github.com/ydb-platform/ydb-go-sdk/v3" {
279+
// Generate YDB-specific code using ydb.Session
280+
} else {
281+
// Generate standard database/sql code
282+
}
283+
284+
return &plugin.GenerateResponse{
285+
Files: []*plugin.File{...},
286+
}, nil
287+
}
288+
```
289+
290+
**Configuration:**
291+
292+
```yaml
293+
plugins:
294+
- name: ydb-go
295+
process:
296+
cmd: sqlc-gen-ydb-go
297+
298+
sql:
299+
- engine: ydb
300+
schema: "schema.sql"
301+
queries: "queries.sql"
302+
codegen:
303+
- plugin: ydb-go
304+
out: db
305+
options:
306+
sql_package: "github.com/ydb-platform/ydb-go-sdk/v3"
307+
package: "db"
308+
```
309+
180310
## See Also
181311
182312
- [Engine Plugins Documentation](../../docs/howto/engine-plugins.md)

0 commit comments

Comments
 (0)