schema

package module
v0.0.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 8, 2019 License: BSD-3-Clause Imports: 3 Imported by: 79

README

schema

BSD3 Build Status codecov Go Report Card Used By Godoc

schema is a Go package providing access to database schema metadata, for database/sql drivers.

TODO more docs

Currently supporting the following database engines / SQL dialects:

  • Microsoft SQL Server
  • MySQL
  • Oracle
  • Postgres
  • SQLite

For a list of supported drivers, and their capabilities with regards to sql.ColumnType support, see drivercaps

Installation

$ go get github.com/jimsmart/schema
import "github.com/jimsmart/schema"
Dependencies

Example

See GoDocs for usage examples.

Documentation

GoDocs https://godoc.org/github.com/jimsmart/schema

Testing

Database services for testing against are hosted in Docker.

To bring up the database services: execute docker-compose up inside the project folder, and wait until all of the Docker services have completed their startup (i.e. there is no further output in the terminal), then open a second terminal. (In future one may choose to use docker-compose up -d instead)

To run the tests execute go test inside the project folder.

For a full coverage report, try:

$ go test -coverprofile=coverage.out && go tool cover -html=coverage.out

To shutdown the Docker services, execute docker-compose down -v inside the project folder.

License

Package schema is copyright 2018-2019 by Jim Smart and released under the BSD 3-Clause License

History

  • v0.0.4: Improved error handling for unknown DB driver types. Test environment now uses Docker.
  • v0.0.3: Minor code cleanups.
  • v0.0.2: Added identifier escaping for methods that query sql.ColumnType.
  • v0.0.1: Started using Go modules.
  • 2019-11-04: Fix for renamed driver struct in github.com/mattn/go-oci8 (Oracle)
  • 2019-11-04: Fix for renamed driver struct in github.com/denisenkom/go-mssqldb (MSSQL)

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.ColumnType
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

Constants

This section is empty.

Variables

This section is empty.

Functions

func Table

func Table(db *sql.DB, name string) ([]*sql.ColumnType, error)

Table returns the column type metadata for the given table name.

func TableNames

func TableNames(db *sql.DB) ([]string, error)

TableNames returns a list of all table names in the current schema (not including system tables).

func Tables

func Tables(db *sql.DB) (map[string][]*sql.ColumnType, error)

Tables returns column type metadata for all tables in the current schema (not including system tables). The returned map is keyed by table name.

func View

func View(db *sql.DB, name string) ([]*sql.ColumnType, error)

View returns the column type metadata for the given view name.

func ViewNames

func ViewNames(db *sql.DB) ([]string, error)

ViewNames returns a list of all view names in the current schema (not including system views).

func Views

func Views(db *sql.DB) (map[string][]*sql.ColumnType, error)

Views returns column type metadata for all views in the current schema (not including system views). The returned map is keyed by view name.

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL