Rename everything.

This commit is contained in:
Aaron 2016-03-22 21:25:57 -07:00
parent 48a9ba8d29
commit 1394489a63
11 changed files with 99 additions and 97 deletions

View file

@ -44,11 +44,11 @@ func (d *PostgresDriver) Close() {
d.dbConn.Close()
}
// GetAllTables connects to the postgres database and
// AllTables 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) GetAllTables() ([]string, error) {
func (d *PostgresDriver) AllTables() ([]string, error) {
var tableNames []string
rows, err := d.dbConn.Query(`select table_name from
@ -71,12 +71,12 @@ func (d *PostgresDriver) GetAllTables() ([]string, error) {
return tableNames, nil
}
// GetTableInfo takes a table name and attempts to retrieve the table information
// Columns 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 []DBColumn after ParseTableInfo()
// and column types and returns those as a []Column after TranslateColumn()
// converts the SQL types to Go types, for example: "varchar" to "string"
func (d *PostgresDriver) GetTableInfo(tableName string) ([]DBColumn, error) {
var table []DBColumn
func (d *PostgresDriver) Columns(tableName string) ([]Column, error) {
var table []Column
rows, err := d.dbConn.Query(`
SELECT c.column_name, c.data_type, c.is_nullable,
@ -104,56 +104,55 @@ func (d *PostgresDriver) GetTableInfo(tableName string) ([]DBColumn, error) {
if err := rows.Scan(&colName, &colType, &isNullable, &isPrimary); err != nil {
return nil, err
}
t := d.ParseTableInfo(colName, colType, isNullable == "YES", isPrimary == "PRIMARY KEY")
t := d.TranslateColumn(Column{
Name: colName,
Type: colType,
IsNullable: isNullable == "YES",
IsPrimaryKey: isPrimary == "PRIMARY KEY",
})
table = append(table, t)
}
return table, nil
}
// ParseTableInfo converts postgres database types to Go types, for example
// TranslateColumn converts postgres database types to Go types, for example
// "varchar" to "string" and "bigint" to "int64". It returns this parsed data
// as a DBColumn object.
func (d *PostgresDriver) ParseTableInfo(colName, colType string, isNullable bool, isPrimary bool) DBColumn {
var t DBColumn
t.Name = colName
t.IsPrimaryKey = isPrimary
t.IsNullable = isNullable
if isNullable {
switch colType {
// as a Column object.
func (d *PostgresDriver) TranslateColumn(c Column) Column {
if c.IsNullable {
switch c.Type {
case "bigint", "bigserial", "integer", "smallint", "smallserial", "serial":
t.Type = "null.Int"
c.Type = "null.Int"
case "bit", "bit varying", "character", "character varying", "cidr", "inet", "json", "macaddr", "text", "uuid", "xml":
t.Type = "null.String"
c.Type = "null.String"
case "boolean":
t.Type = "null.Bool"
c.Type = "null.Bool"
case "date", "interval", "time", "timestamp without time zone", "timestamp with time zone":
t.Type = "null.Time"
c.Type = "null.Time"
case "double precision", "money", "numeric", "real":
t.Type = "null.Float"
c.Type = "null.Float"
default:
t.Type = "null.String"
c.Type = "null.String"
}
} else {
switch colType {
switch c.Type {
case "bigint", "bigserial", "integer", "smallint", "smallserial", "serial":
t.Type = "int64"
c.Type = "int64"
case "bit", "bit varying", "character", "character varying", "cidr", "inet", "json", "macaddr", "text", "uuid", "xml":
t.Type = "string"
c.Type = "string"
case "bytea":
t.Type = "[]byte"
c.Type = "[]byte"
case "boolean":
t.Type = "bool"
c.Type = "bool"
case "date", "interval", "time", "timestamp without time zone", "timestamp with time zone":
t.Type = "time.Time"
c.Type = "time.Time"
case "double precision", "money", "numeric", "real":
t.Type = "float64"
c.Type = "float64"
default:
t.Type = "string"
c.Type = "string"
}
}
return t
return c
}