Documentation
¶
Overview ¶
Package sqlutil is a package that contains several useful utility functions for interacting with sql databases.
When connecting to a database with a specific dsn ([user]:[password]@/[database]), information such as what databases on the current host are available to the current user, what tables are availble for a given database and how a given table was defined is not easily available. This collection of functions drastically simplifies the retreival of this information, as the following code demonstrates.
// assuming we have a *sql.DB from db, err := sql.Open(...) // we create a SqlUtil sqlUtil := sqlutil.New(db) // We can then retrieve the databases on the host with dbs, err := sqlUtil.ShowDatabases() // We can show the tables for a given database with tables, err := sqlUtil.ShowTables("myDB") // And we can see how a table is defined with columns, err := sqlUtil.DescribeTable("myTable")
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ColumnInfo ¶
type ColumnInfo struct { Field, Type, Null, Key, Default, Extra string }
ColumnInfo describes the six fields of information for a column in a SQL table.
type SqlUtil ¶
type SqlUtil struct {
// contains filtered or unexported fields
}
SqlUtil is useful for extra information about a database, it has wrapper functions to show what databases exist on the host specified by dsn (or at least those that the user specified by the dsn can see), for showing what tables exist on a database and for describing a database table.
func New ¶
New returns a SqlUtil which uses the given db connection to communicate with the desired database host.
func (SqlUtil) DescribeTable ¶
func (s SqlUtil) DescribeTable(tableName string) ([]ColumnInfo, error)
DescribeTable returns the columns in the given table.
func (SqlUtil) ShowDatabases ¶
ShowDatabases shows all the databases that the current user can see on the current database host.