Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ vendor/pkg
sqrible.yml
templates/
examples/advanced/generated.go
/go_build_main_go.exe
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
This is a heavily modified version of sqrible - and a work in progress.
Do not use unless you like the following changes and can customize to your needs.

* Automatic detection of nullable fields
* Chooses different data types to be used for nullable fields.
* Added "package" variable to the table yaml which I needed in my templates
* Changed transposition of data types away from pgx - this is a work in progress
* Detection of data types handles user defined types - this is a work in progress


Problematic
-----------

Expand Down
2 changes: 1 addition & 1 deletion src/cmd/sqrible/main.go → main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"errors"
"flag"
"fmt"
"sqrible"
"sqrible/src/sqrible"

"github.com/jackc/pgx"
)
Expand Down
2 changes: 2 additions & 0 deletions src/sqrible/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ type Column struct {
PGDataType string
PGUDTName string
PGOrdinalPosition int32
PGIsUpdatable string
PGIsNullable string

IsPK bool

Expand Down
3 changes: 2 additions & 1 deletion src/sqrible/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"io/ioutil"
"strings"

yaml "gopkg.in/yaml.v2"
"gopkg.in/yaml.v2"
)

func ParseConfig(f string) Config {
Expand All @@ -27,6 +27,7 @@ type TableConfig struct {
Template string `yaml:"template"`
ConfigDetails map[string]TableColumnConfigDetails `yaml:"tablecols"`
GoStruct string
Package string `yaml:"package"`
}

type Config struct {
Expand Down
119 changes: 94 additions & 25 deletions src/sqrible/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ func tableColumns(c *pgx.Conn, name string, cfg Config) ([]*Column, error) {
`SELECT column_name,
data_type,
udt_name,
ordinal_position
ordinal_position,
is_nullable,
is_updatable
FROM information_schema.columns
WHERE table_name=$1
ORDER BY ordinal_position ASC`,
Expand All @@ -106,14 +108,16 @@ func tableColumns(c *pgx.Conn, name string, cfg Config) ([]*Column, error) {
&c.PGDataType,
&c.PGUDTName,
&c.PGOrdinalPosition,
&c.PGIsNullable,
&c.PGIsUpdatable,
)

if err != nil {
return []*Column{}, err
}

c.GoFieldName = asGoFieldName(c.PGColumnName)
c.PgxType = asPgxType(c.PGDataType, c.PGUDTName)
c.PgxType = asPgxType(c.PGDataType, c.PGUDTName, c.PGIsNullable)

c.Config = cfg.columnConfig(name, c.PGColumnName)
c.IsPK = colIsPk(c.PGColumnName, pks)
Expand Down Expand Up @@ -190,32 +194,97 @@ func asGoFieldName(n string) string {
return buf.String()
}

func asPgxType(n string, udt string) string {
m := map[string]string{
"bigint": "pgtype.Int8",
"int8": "pgtype.Int8",
"integer": "pgtype.Int4",
"smallint": "pgtype.Int2",
"character varying": "pgtype.Varchar",
"text": "pgtype.Text",
"date": "pgtype.Date",
"inet": "pgtype.Inet",
"cidr": "pgtype.CIDR",
"bytea": "pgtype.Bytea",
"boolean": "pgtype.Bool",
"bool": "pgtype.Bool",
"real": "pgtype.Float4",
"double precision": "pgtype.Float8",
"timestamp with time zone": "pgtype.Timestamptz",
}

t, found := m[n]
if found {
return t
func asPgxType(n string, udt string, nullable string) string {
// These are the field transpositions IF
// the field is marked nullable

nulls := map[string]string{
"int16": "dat.NullInt64",
"int8": "dat.NullInt64",
"bigint": "dat.NullInt64",
"bigserieal": "dat.NullInt64",
"int4": "dat.NullInt64",
"int": "dat.NullInt64",
"serial": "dat.NullInt64",
"int2": "dat.NullInt64",
"smallint": "dat.NullInt64",
"smallserial": "dat.NullInt64",
"double precision": "dat.NullFloat64",
"real": "dat.NullFloat64",
"bool": "bool",
"boolean": "bool",
"text": "dat.NullString",
"varchar": "dat.NullString",
"citext": "dat.NullString",
"character varying": "dat.NullString",
"timestamp": "dat.NullTime",
"inet": "inet",
"uuid": "uuid.UUID",
"bytea": "pgtype.ByteaArray",
"jsonb": "dat.JSON",
"geography": "pgtype.Point",
"tsvector": "dat.NullString",
}

full := map[string]string{


"int16": "int16",
"int8": "int8",
"bigint": "int8",
"bigserieal": "int8",
"int4": "int",
"int": "int",
"serial": "int",
"int2": "int",
"smallint": "int",
"smallserial": "int",
"double precision": "float64",
"real": "float64",
"bool": "bool",
"boolean": "bool",
"text": "string",
"varchar": "string",
"citext": "string",
"character varying": "string",
"timestamp": "dat.NullTime",
"inet": "inet",
"uuid": "uuid.UUID",
"bytea": "[][]byte",
"jsonb": "dat.JSON",
"geography": "pgtype.Point",
"tsvector": "string",
}

if nullable == "YES" {

t, found := nulls[udt]
if found {
return t
}
if !found {
t, found := nulls[n]
if found {
return t
}
}
} else {

t, found := full[udt]
if found {
return t
}

if !found {
t, found := full[n]
if found {
return t
}
}
}

if strings.ToLower(n) == "array" {
return asPgxType(strings.ToLower(udt[1:]), "") + "Array"
return asPgxType(strings.ToLower(udt[1:]), udt, nullable) + "Array"
}

Quit(fmt.Errorf("Postgres type %s not found in pgx mapping", n))
Expand Down
2 changes: 2 additions & 0 deletions src/sqrible/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ type Table struct {
Columns Columns
GoStructName string
Template string
Package string
}

func NewTable(name string, cols Columns, cfg *TableConfig) *Table {
Expand All @@ -13,6 +14,7 @@ func NewTable(name string, cols Columns, cfg *TableConfig) *Table {
Columns: cols,
GoStructName: cfg.GoStruct,
Template: cfg.Template,
Package: cfg.Package,
}
}

Expand Down