|
| 1 | +# DuckDB Example |
| 2 | + |
| 3 | +This example demonstrates how to use sqlc with DuckDB. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +DuckDB is an in-process analytical database that supports PostgreSQL-compatible SQL syntax. This integration reuses sqlc's PostgreSQL parser and catalog while providing a DuckDB-specific analyzer that connects to an in-memory DuckDB instance. |
| 8 | + |
| 9 | +## Features |
| 10 | + |
| 11 | +- **PostgreSQL-compatible SQL**: DuckDB uses PostgreSQL-compatible syntax, so you can use familiar SQL constructs |
| 12 | +- **In-memory database**: Perfect for testing and development |
| 13 | +- **Type-safe Go code**: sqlc generates type-safe Go code from your SQL queries |
| 14 | +- **Live database analysis**: The analyzer connects to a DuckDB instance to extract accurate column types |
| 15 | + |
| 16 | +## Configuration |
| 17 | + |
| 18 | +The `sqlc.yaml` file configures sqlc to use the DuckDB engine: |
| 19 | + |
| 20 | +```yaml |
| 21 | +version: "2" |
| 22 | +sql: |
| 23 | + - name: "duckdb_example" |
| 24 | + engine: "duckdb" # Use DuckDB engine |
| 25 | + schema: |
| 26 | + - "schema.sql" |
| 27 | + queries: |
| 28 | + - "query.sql" |
| 29 | + database: |
| 30 | + managed: false |
| 31 | + uri: ":memory:" # Use in-memory database |
| 32 | + analyzer: |
| 33 | + database: true # Enable live database analysis |
| 34 | + gen: |
| 35 | + go: |
| 36 | + package: "db" |
| 37 | + out: "db" |
| 38 | +``` |
| 39 | +
|
| 40 | +## Database URI |
| 41 | +
|
| 42 | +DuckDB supports several URI formats: |
| 43 | +
|
| 44 | +- `:memory:` - In-memory database (default if not specified) |
| 45 | +- `file.db` - File-based database |
| 46 | +- `/path/to/file.db` - Absolute path to database file |
| 47 | + |
| 48 | +## Usage |
| 49 | + |
| 50 | +1. Generate Go code: |
| 51 | + ```bash |
| 52 | + sqlc generate |
| 53 | + ``` |
| 54 | + |
| 55 | +2. Use the generated code in your application: |
| 56 | + ```go |
| 57 | + package main |
| 58 | +
|
| 59 | + import ( |
| 60 | + "context" |
| 61 | + "database/sql" |
| 62 | + "log" |
| 63 | +
|
| 64 | + _ "github.com/marcboeker/go-duckdb" |
| 65 | + "yourmodule/db" |
| 66 | + ) |
| 67 | +
|
| 68 | + func main() { |
| 69 | + // Open DuckDB connection |
| 70 | + conn, err := sql.Open("duckdb", ":memory:") |
| 71 | + if err != nil { |
| 72 | + log.Fatal(err) |
| 73 | + } |
| 74 | + defer conn.Close() |
| 75 | +
|
| 76 | + // Create tables |
| 77 | + schema := ` |
| 78 | + CREATE TABLE users ( |
| 79 | + id INTEGER PRIMARY KEY, |
| 80 | + name VARCHAR NOT NULL, |
| 81 | + email VARCHAR UNIQUE NOT NULL, |
| 82 | + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP |
| 83 | + ); |
| 84 | + ` |
| 85 | + if _, err := conn.Exec(schema); err != nil { |
| 86 | + log.Fatal(err) |
| 87 | + } |
| 88 | +
|
| 89 | + // Use generated queries |
| 90 | + queries := db.New(conn) |
| 91 | + ctx := context.Background() |
| 92 | +
|
| 93 | + // Create a user |
| 94 | + user, err := queries.CreateUser(ctx, db.CreateUserParams{ |
| 95 | + Name: "John Doe", |
| 96 | + Email: "john@example.com", |
| 97 | + }) |
| 98 | + if err != nil { |
| 99 | + log.Fatal(err) |
| 100 | + } |
| 101 | +
|
| 102 | + log.Printf("Created user: %+v\n", user) |
| 103 | +
|
| 104 | + // Get the user |
| 105 | + fetchedUser, err := queries.GetUser(ctx, user.ID) |
| 106 | + if err != nil { |
| 107 | + log.Fatal(err) |
| 108 | + } |
| 109 | +
|
| 110 | + log.Printf("Fetched user: %+v\n", fetchedUser) |
| 111 | + } |
| 112 | + ``` |
| 113 | + |
| 114 | +## Differences from PostgreSQL |
| 115 | + |
| 116 | +While DuckDB supports PostgreSQL-compatible SQL, there are some differences: |
| 117 | + |
| 118 | +1. **Data Types**: DuckDB has its own set of data types, though many are compatible with PostgreSQL |
| 119 | +2. **Functions**: Some PostgreSQL functions may not be available or may behave differently |
| 120 | +3. **Extensions**: DuckDB uses a different extension system than PostgreSQL |
| 121 | + |
| 122 | +## Benefits of DuckDB |
| 123 | + |
| 124 | +1. **Fast analytical queries**: Optimized for OLAP workloads |
| 125 | +2. **Embedded**: No separate server process needed |
| 126 | +3. **Portable**: Single file database |
| 127 | +4. **PostgreSQL-compatible**: Familiar SQL syntax |
| 128 | + |
| 129 | +## Requirements |
| 130 | + |
| 131 | +- Go 1.24.0 or later |
| 132 | +- `github.com/marcboeker/go-duckdb` driver |
| 133 | + |
| 134 | +## Notes |
| 135 | + |
| 136 | +- The DuckDB analyzer uses an in-memory instance to extract query metadata |
| 137 | +- Schema migrations are applied to the analyzer instance automatically |
| 138 | +- Type inference is done by preparing queries against the DuckDB instance |
0 commit comments