schema

package module
v0.0.0-...-fd50541 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2019 License: BSD-3-Clause Imports: 4 Imported by: 0

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
  • A supported database driver.
  • Standard library.
  • Ginkgo and Gomega if you wish to run the tests.
  • Tests also require various database drivers to be installed and configured.

Example

See GoDocs for usage examples.

Documentation

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

Testing

Note that a moderate amount of database setup and configuration is required for successful execution of the tests.

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

License

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

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

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

This section is empty.

Jump to

Keyboard shortcuts

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