|
4 | 4 | "github.com/sqlc-dev/sqlc/internal/compiler" |
5 | 5 | "github.com/sqlc-dev/sqlc/internal/config" |
6 | 6 | "github.com/sqlc-dev/sqlc/internal/config/convert" |
| 7 | + "github.com/sqlc-dev/sqlc/internal/core" |
7 | 8 | "github.com/sqlc-dev/sqlc/internal/info" |
8 | 9 | "github.com/sqlc-dev/sqlc/internal/plugin" |
9 | 10 | "github.com/sqlc-dev/sqlc/internal/sql/catalog" |
@@ -224,10 +225,90 @@ func pluginQueryParam(p compiler.Parameter) *plugin.Parameter { |
224 | 225 | } |
225 | 226 |
|
226 | 227 | func codeGenRequest(r *compiler.Result, settings config.CombinedSettings) *plugin.GenerateRequest { |
| 228 | + // Engines on the xqlc core (ClickHouse) project the codegen catalog |
| 229 | + // from the core catalog rather than the in-memory sql/catalog, which is |
| 230 | + // nil on that path. |
| 231 | + var cat *plugin.Catalog |
| 232 | + if r.CoreCatalog != nil { |
| 233 | + cat = pluginCatalogFromCore(r.CoreCatalog) |
| 234 | + } else { |
| 235 | + cat = pluginCatalog(r.Catalog) |
| 236 | + } |
227 | 237 | return &plugin.GenerateRequest{ |
228 | 238 | Settings: pluginSettings(r, settings), |
229 | | - Catalog: pluginCatalog(r.Catalog), |
| 239 | + Catalog: cat, |
230 | 240 | Queries: pluginQueries(r), |
231 | 241 | SqlcVersion: info.Version, |
232 | 242 | } |
233 | 243 | } |
| 244 | + |
| 245 | +// pluginCatalogFromCore projects a core.Catalog (the xqlc SQLite-backed |
| 246 | +// catalog) into the plugin.Catalog that codegen consumes to emit models |
| 247 | +// and enums. It reads the namespace / class / attribute / type tables |
| 248 | +// directly. Projection is best-effort: an unexpected query error against |
| 249 | +// the in-memory catalog yields a partial catalog rather than aborting |
| 250 | +// generation. |
| 251 | +func pluginCatalogFromCore(cc *core.Catalog) *plugin.Catalog { |
| 252 | + db := cc.DB() |
| 253 | + var schemas []*plugin.Schema |
| 254 | + |
| 255 | + type row struct { |
| 256 | + oid int64 |
| 257 | + name string |
| 258 | + } |
| 259 | + readRows := func(query string, args ...any) []row { |
| 260 | + rows, err := db.Query(query, args...) |
| 261 | + if err != nil { |
| 262 | + return nil |
| 263 | + } |
| 264 | + defer rows.Close() |
| 265 | + var out []row |
| 266 | + for rows.Next() { |
| 267 | + var r row |
| 268 | + if err := rows.Scan(&r.oid, &r.name); err != nil { |
| 269 | + return out |
| 270 | + } |
| 271 | + out = append(out, r) |
| 272 | + } |
| 273 | + return out |
| 274 | + } |
| 275 | + |
| 276 | + for _, ns := range readRows(`SELECT oid, name FROM sql_namespace ORDER BY oid`) { |
| 277 | + var tables []*plugin.Table |
| 278 | + for _, cl := range readRows( |
| 279 | + `SELECT oid, name FROM sql_class WHERE namespace_oid = ? AND kind = 'r' ORDER BY oid`, ns.oid, |
| 280 | + ) { |
| 281 | + rel := &plugin.Identifier{Schema: ns.name, Name: cl.name} |
| 282 | + var columns []*plugin.Column |
| 283 | + crows, err := db.Query( |
| 284 | + `SELECT a.name, t.name, a.not_null |
| 285 | + FROM sql_attribute a JOIN sql_type t ON t.oid = a.type_oid |
| 286 | + WHERE a.class_oid = ? ORDER BY a.num`, cl.oid, |
| 287 | + ) |
| 288 | + if err != nil { |
| 289 | + continue |
| 290 | + } |
| 291 | + for crows.Next() { |
| 292 | + var name, typeName string |
| 293 | + var notNull int |
| 294 | + if err := crows.Scan(&name, &typeName, ¬Null); err != nil { |
| 295 | + break |
| 296 | + } |
| 297 | + columns = append(columns, &plugin.Column{ |
| 298 | + Name: name, |
| 299 | + Type: &plugin.Identifier{Name: typeName}, |
| 300 | + NotNull: notNull != 0, |
| 301 | + Table: rel, |
| 302 | + }) |
| 303 | + } |
| 304 | + crows.Close() |
| 305 | + tables = append(tables, &plugin.Table{Rel: rel, Columns: columns}) |
| 306 | + } |
| 307 | + schemas = append(schemas, &plugin.Schema{Name: ns.name, Tables: tables}) |
| 308 | + } |
| 309 | + |
| 310 | + return &plugin.Catalog{ |
| 311 | + DefaultSchema: "public", |
| 312 | + Schemas: schemas, |
| 313 | + } |
| 314 | +} |
0 commit comments