sqlutil

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jul 29, 2014 License: MIT Imports: 2 Imported by: 1

README

sqlutil

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.

Usage

// 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")

Hope you find these useful!

Examples

There are a bunch of examples under the examples/ directory (they are each under their own package so you can go build/install them to play around with).

Godoc: http://godoc.org/github.com/ttacon/go-utils/db/sqlutil

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

func New(dbConn *sql.DB) SqlUtil

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

func (s SqlUtil) ShowDatabases() ([]string, error)

ShowDatabases shows all the databases that the current user can see on the current database host.

func (SqlUtil) ShowTables

func (s SqlUtil) ShowTables(database string) ([]string, error)

ShowTables returns a slice of the names of all the tables in the given database. The given database can be the empty string, in which case the tables for the currently selected database are returned.

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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