MCPcopy
hub / github.com/sqldef/sqldef / domains

Method domains

database/postgres/database.go:479–598  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

477}
478
479func (d *PostgresDatabase) domains() ([]string, error) {
480 rows, err := d.db.Query(`
481 SELECT n.nspname AS domain_schema,
482 t.typname AS domain_name,
483 pg_catalog.format_type(t.typbasetype, t.typtypmod) AS data_type,
484 t.typdefault AS default_value,
485 t.typnotnull AS not_null,
486 c.collname AS collation,
487 obj_description(t.oid, 'pg_type') AS domain_comment
488 FROM pg_catalog.pg_type t
489 INNER JOIN pg_catalog.pg_namespace n ON t.typnamespace = n.oid
490 LEFT JOIN pg_catalog.pg_collation c ON t.typcollation = c.oid AND t.typcollation <> 0
491 WHERE t.typtype = 'd'
492 AND n.nspname NOT IN ('pg_catalog', 'information_schema', 'pg_toast', 'sys')
493 AND NOT EXISTS (SELECT 1 FROM pg_depend d WHERE d.objid = t.oid AND d.classid = (SELECT oid FROM pg_catalog.pg_class WHERE relname = 'pg_type') AND d.deptype = 'e')
494 ORDER BY n.nspname, t.typname;
495 `)
496 if err != nil {
497 return nil, err
498 }
499 defer rows.Close()
500
501 type domainInfo struct {
502 schema, name, dataType string
503 defaultValue, collation sql.NullString
504 notNull bool
505 comment sql.NullString
506 }
507
508 var domains []domainInfo
509 for rows.Next() {
510 var di domainInfo
511 if err := rows.Scan(&di.schema, &di.name, &di.dataType, &di.defaultValue, &di.notNull, &di.collation, &di.comment); err != nil {
512 return nil, err
513 }
514 if d.config.TargetSchema != nil && !slices.Contains(d.config.TargetSchema, di.schema) {
515 continue
516 }
517 domains = append(domains, di)
518 }
519 if err := rows.Err(); err != nil {
520 return nil, err
521 }
522
523 // Now fetch constraints for domains, applying the same TargetSchema filter
524 constraintRows, err := d.db.Query(`
525 SELECT n.nspname AS domain_schema,
526 t.typname AS domain_name,
527 con.conname AS constraint_name,
528 pg_catalog.pg_get_constraintdef(con.oid, true) AS constraint_def
529 FROM pg_catalog.pg_constraint con
530 INNER JOIN pg_catalog.pg_type t ON con.contypid = t.oid
531 INNER JOIN pg_catalog.pg_namespace n ON t.typnamespace = n.oid
532 WHERE t.typtype = 'd'
533 AND n.nspname NOT IN ('pg_catalog', 'information_schema', 'pg_toast', 'sys')
534 ORDER BY n.nspname, t.typname, con.conname;
535 `)
536 if err != nil {

Callers 1

ExportDDLsMethod · 0.95

Calls 6

QueryMethod · 0.80
NextMethod · 0.80
ScanMethod · 0.80
CloseMethod · 0.65
StringMethod · 0.45

Tested by

no test coverage detected