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.
// Fetch names of all tables tnames, err := schema.TableNames(db) ... // tnames is [][2]string for i := range tnames { fmt.Println("Table:", tnames[i][1]) } // Output: // Table: employee_tbl // Table: department_tbl // Table: sales_tbl
Both user permissions and current database/schema effect table visibility.
Use schema.Table() to query column type metadata for a single table:
// Fetch column metadata for given table tcols, err := schema.Table(db, "", "employee_tbl") ... // tcols is []*sql.ColumnType for i := range tcols { fmt.Println("Column:", 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
Note: underlying support for column type metadata is driver implementation specific and somewhat variable.
View Metadata ¶
The same metadata can also be queried for views also:
// Fetch names of all views vnames, err := schema.ViewNames(db) ... // Fetch column metadata for given view vcols, err := schema.View(db, "", "monthly_sales_view") ... // Fetch column metadata for all views views, err := schema.Views(db) ...
Primary Key Metadata ¶
To obtain a list of columns making up the primary key for a given table:
// Fetch primary key for given table pks, err := schema.PrimaryKey(db, "", "employee_tbl") ... // pks is []string for i := range pks { fmt.Println("Primary Key:", pks[i]) } // Output: // Primary Key: employee_id
Index ¶
- func PrimaryKey(db *sql.DB, schema, table string) ([]string, error)
- func Table(db *sql.DB, schema, table string) ([]*sql.ColumnType, error)
- func TableNames(db *sql.DB) ([][2]string, error)
- func Tables(db *sql.DB) (map[[2]string][]*sql.ColumnType, error)
- func View(db *sql.DB, schema, view string) ([]*sql.ColumnType, error)
- func ViewNames(db *sql.DB) ([][2]string, error)
- func Views(db *sql.DB) (map[[2]string][]*sql.ColumnType, error)
- type UnknownDriverError
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func PrimaryKey ¶ added in v0.0.7
PrimaryKey returns a list of column names making up the primary key for the given table in the given schema.
func Table ¶
Table returns the column type metadata for the given table in the given schema.
Setting schema to an empty string results in the current schema being used.
func TableNames ¶
TableNames returns a list of all table names.
Each name consists of a [2]string tuple: schema name, table name.
func Tables ¶
Tables returns column type metadata for all tables in the current schema.
The returned map is keyed by table name tuples.
func View ¶
View returns the column type metadata for the given view in the given schema.
Setting schema to an empty string results in the current schema being used.
Types ¶
type UnknownDriverError ¶ added in v0.0.4
type UnknownDriverError struct {
Driver string
}
UnknownDriverError is returned when there is no matching database driver type name in the driverDialect table.
Errors of this kind are caused by using an unsupported database driver/dialect, or if/when a database driver developer renames the type underlying calls to db.Driver().
func (UnknownDriverError) Error ¶ added in v0.0.4
func (e UnknownDriverError) Error() string
Error returns a formatted string description.