Added comments to all oustanding files

* Deleted unused test files
This commit is contained in:
Patrick O'brien 2016-02-24 19:17:58 +10:00
parent 47c5be1038
commit 35380d6634
4 changed files with 39 additions and 9 deletions

View file

@ -1,15 +1,33 @@
package dbdrivers
// DBDriver is an interface that handles the operation uniqueness of each
// type of database connection. For example, queries to obtain schema data
// will vary depending on what type of database software is in use.
// The goal of the DBDriver is to retrieve all table names in a database
// using GetAllTableNames() if no table names are provided via flags,
// to handle the database connection using Open() and Close(), and to
// build the table information using GetTableInfo() and ParseTableInfo()
type DBDriver interface {
// GetAllTableNames connects to the database and retrieves all "public" table names
GetAllTableNames() ([]string, error)
// GetTableInfo builds an object of []DBTable containing the table information
GetTableInfo(tableName string) ([]DBTable, error)
// ParseTableInfo builds a DBTable out of a column name and column type.
// Its main responsibility is to convert database types to Go types, for example
// "varchar" to "string".
ParseTableInfo(colName, colType string) DBTable
// Open the database connection
Open() error
// Close the database connection
Close()
}
// DBTable holds a column name, for example "column_name", and a column type,
// for example "int64". Column types are Go types, converted by ParseTableInfo.
type DBTable struct {
ColName string
ColType string

View file

@ -4,14 +4,21 @@ import (
"database/sql"
"fmt"
// Import the postgres driver
_ "github.com/lib/pq"
)
// PostgresDriver holds the database connection string and a handle
// to the database connection.
type PostgresDriver struct {
connStr string
dbConn *sql.DB
}
// NewPostgresDriver takes the database connection details as parameters and
// returns a pointer to a PostgresDriver object. Note that it is required to
// call PostgresDriver.Open() and PostgresDriver.Close() to open and close
// the database connection once an object has been obtained.
func NewPostgresDriver(user, pass, dbname, host string, port int) *PostgresDriver {
driver := PostgresDriver{
connStr: fmt.Sprintf("user=%s password=%s dbname=%s host=%s port=%d",
@ -21,6 +28,7 @@ func NewPostgresDriver(user, pass, dbname, host string, port int) *PostgresDrive
return &driver
}
// Open opens the database connection using the connection string
func (d *PostgresDriver) Open() error {
var err error
d.dbConn, err = sql.Open("postgres", d.connStr)
@ -31,10 +39,15 @@ func (d *PostgresDriver) Open() error {
return nil
}
// Close closes the database connection
func (d *PostgresDriver) Close() {
d.dbConn.Close()
}
// GetAllTableNames connects to the postgres database and
// retrieves all table names from the information_schema where the
// table schema is public. It excludes common migration tool tables
// such as gorp_migrations
func (d *PostgresDriver) GetAllTableNames() ([]string, error) {
var tableNames []string
@ -58,6 +71,10 @@ func (d *PostgresDriver) GetAllTableNames() ([]string, error) {
return tableNames, nil
}
// GetTableInfo takes a table name and attempts to retrieve the table information
// from the database information_schema.columns. It retrieves the column names
// and column types and returns those as a []DBTable after ParseTableInfo()
// converts the SQL types to Go types, for example: "varchar" to "string"
func (d *PostgresDriver) GetTableInfo(tableName string) ([]DBTable, error) {
var tableInfo []DBTable
@ -80,6 +97,9 @@ func (d *PostgresDriver) GetTableInfo(tableName string) ([]DBTable, error) {
return tableInfo, nil
}
// ParseTableInfo converts postgres database types to Go types, for example
// "varchar" to "string" and "bigint" to "int64". It returns this parsed data
// as a DBTable object.
func (d *PostgresDriver) ParseTableInfo(colName, colType string) DBTable {
t := DBTable{}

View file

@ -1,9 +0,0 @@
package dbdrivers
import "testing"
func TestFlow(t *testing.T) {
// driver := NewPostgresDriver("test", "pass", "dbname", "localhost", 3456)
// defer driver.Close()
// driver.GetTableInfo()
}

View file

@ -7,6 +7,7 @@ import (
)
func main() {
// Execute SQLBoiler
if err := cmds.SQLBoiler.Execute(); err != nil {
os.Exit(-1)
}