Documentation
¶
Overview ¶
Package schema provides access to database schema metadata, for database/sql drivers.
For further information about current driver support status, see https://github.com/jimsmart/schema
Table Metadata ¶
The schema package works alongside database/sql and its underlying driver to provide schema metadata.
tnames, err := schema.TableNames(db)
...
// tnames is []string
for i := range tnames {
fmt.Printf("Table: %s\n", tnames[i])
}
// Output:
// Table: employee_tbl
// Table: department_tbl
// Table: sales_tbl
Both user permissions and current database/schema effect table visibility.
To query column type metadata for a single table, use schema.Table().
tcols, err := schema.Table(db, "employee_tbl")
...
// tcols is []*sql.ColumnInfo
for i := range tcols {
fmt.Printf("Column: %s %s\n", tcols[i].Name(), tcols[i].DatabaseTypeName())
}
// Output:
// Column: employee_id INTEGER
// Column: first_name TEXT
// Column: last_name TEXT
// Column: created_at TIMESTAMP
To query table names and column type metadata for all tables, use schema.Tables().
See also https://golang.org/pkg/database/sql/#ColumnType
Underlying driver support for column type metadata is implementation specific and somewhat variable.
View Metadata ¶
The same metadata can also be queried for views.
Index ¶
- func Table(db *sql.DB, name string) ([]*sql.ColumnType, error)
- func TableNames(db *sql.DB) ([]string, error)
- func Tables(db *sql.DB) (map[string][]*sql.ColumnType, error)
- func View(db *sql.DB, name string) ([]*sql.ColumnType, error)
- func ViewNames(db *sql.DB) ([]string, error)
- func Views(db *sql.DB) (map[string][]*sql.ColumnType, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func TableNames ¶
TableNames returns a list of all table names in the current schema (not including system tables).
func Tables ¶
Tables returns column type metadata for all tables in the current schema (not including system tables). The returned map is keyed by table name.
Types ¶
This section is empty.