Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prepend outputdir on generated files & cleanup #49

Merged
merged 4 commits into from Oct 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 12 additions & 8 deletions cli/commands.go
Expand Up @@ -117,13 +117,14 @@ Creates a default gnorm.toml and the various template files needed to run GNORM.
fmt.Fprintf(env.Stdout, "Can't create gnorm.toml file: %v\n", err)
return codeErr{err, 1}
}
for _, name := range []string{"table", "schema", "enum"} {
if err := createFile(name+".gotmpl", "{{.Name}}"); err != nil {
return codeErr{
errors.WithMessage(err, fmt.Sprintf("Can't create template file %q", name)),
1,
}
}
if err := createFile("templates/table.gotmpl", "Table: {{.Table.Name}}\n{{printf \"%#v\" .}}"); err != nil {
return err
}
if err := createFile("templates/enum.gotmpl", "Enum: {{.Enum.Name}}\n{{printf \"%#v\" .}}"); err != nil {
return err
}
if err := createFile("templates/schema.gotmpl", "Schema: {{.Schema.Name}}\n{{printf \"%#v\" .}}"); err != nil {
return err
}
return nil
},
Expand Down Expand Up @@ -151,7 +152,10 @@ func createFile(name, contents string) error {
}
_, err = f.WriteString(contents)
f.Close()
return err
return codeErr{
errors.WithMessage(err, fmt.Sprintf("Can't create template file %q", name)),
1,
}
}

type codeErr struct {
Expand Down
21 changes: 19 additions & 2 deletions cli/gnorm.toml
Expand Up @@ -41,6 +41,23 @@ ExcludeTables = []
# PostRun = ["goimports", "-w", "$GNORMFILE"]
PostRun = []

# OutputDir is the directory relative to the project root (where the
# gnorm.toml file is located) in which all the generated files are written
# to.
#
# This defaults to the current working directory i.e the directory in which
# gnorm.toml is found.
OutputDir = "gnorm"

# StaticDir is the directory relative to the project root (where the
# gnorm.toml file is located) in which all static files , which are
# intended to be copied to the OutputDir are found.
#
# The directory structure is preserved when copying the files to the
# OutputDir
StaticDir = "static"


# TablePaths is a map of output paths to template paths that tells Gnorm how to
# render and output its table info and where to save that output. Each template
# will be rendered with each table in turn and written out to the given output
Expand All @@ -53,7 +70,7 @@ PostRun = []
# "tables.gotmpl" would render tables.gotmpl template with data from the the
# "public.users" table to ./public/users/users.go.
[TablePaths]
"{{.Schema}}/tables/{{.Table}}.go" = "table.gotmpl"
"{{.Schema}}/tables/{{.Table}}.go" = "templates/table.gotmpl"

# SchemaPaths iis a map of output paths to template paths that tells Gnorm how
# to render and output its schema info. Each template will be rendered with
Expand All @@ -67,7 +84,7 @@ PostRun = []
# schemas.gotmpl template with the "public" schema and output to
# ./schemas/public/public.go
[SchemaPaths]
"{{.Schema}}.go" = "schema.gotmpl"
"{{.Schema}}.go" = "templates/schema.gotmpl"

# EnumPaths is a is a map of output paths to template paths that tells Gnorm how
# to render and output its enum info. Each template will be rendered with each
Expand Down
21 changes: 19 additions & 2 deletions cli/sample.go
Expand Up @@ -67,6 +67,23 @@ ExcludeTables = []
# PostRun = ["goimports", "-w", "$GNORMFILE"]
PostRun = []

# OutputDir is the directory relative to the project root (where the
# gnorm.toml file is located) in which all the generated files are written
# to.
#
# This defaults to the current working directory i.e the directory in which
# gnorm.toml is found.
OutputDir = "gnorm"

# StaticDir is the directory relative to the project root (where the
# gnorm.toml file is located) in which all static files , which are
# intended to be copied to the OutputDir are found.
#
# The directory structure is preserved when copying the files to the
# OutputDir
StaticDir = "static"


# TablePaths is a map of output paths to template paths that tells Gnorm how to
# render and output its table info and where to save that output. Each template
# will be rendered with each table in turn and written out to the given output
Expand All @@ -79,7 +96,7 @@ PostRun = []
# "tables.gotmpl" would render tables.gotmpl template with data from the the
# "public.users" table to ./public/users/users.go.
[TablePaths]
"{{.Schema}}/tables/{{.Table}}.go" = "table.gotmpl"
"{{.Schema}}/tables/{{.Table}}.go" = "templates/table.gotmpl"

# SchemaPaths iis a map of output paths to template paths that tells Gnorm how
# to render and output its schema info. Each template will be rendered with
Expand All @@ -93,7 +110,7 @@ PostRun = []
# schemas.gotmpl template with the "public" schema and output to
# ./schemas/public/public.go
[SchemaPaths]
"{{.Schema}}.go" = "schema.gotmpl"
"{{.Schema}}.go" = "templates/schema.gotmpl"

# EnumPaths is a is a map of output paths to template paths that tells Gnorm how
# to render and output its enum info. Each template will be rendered with each
Expand Down
121 changes: 121 additions & 0 deletions database/drivers/postgres/_static/db.go
@@ -0,0 +1,121 @@
package gnorm // import "gnorm.org/gnorm/database/drivers/postgres/gnorm"

// Note that this file is *NOT* generated. :)

import (
"database/sql"
"strconv"
"strings"
)

// DB is the common interface for database operations.
// This should work with database/sql.DB and database/sql.Tx.
type DB interface {
Exec(string, ...interface{}) (sql.Result, error)
Query(string, ...interface{}) (*sql.Rows, error)
QueryRow(string, ...interface{}) *sql.Row
}

// WhereClause has a String function should return a properly formatted where
// clause (not including the WHERE) for positional arguments starting at idx.
type WhereClause interface {
String(idx *int) string
Values() []interface{}
}

type comparison string

const (
compEqual comparison = " = "
compGreater comparison = " > "
compLess comparison = " < "
compGTE comparison = " >= "
compLTE comparison = " <= "
compNE comparison = " <> "
)

type inClause struct {
field string
values []interface{}
}

func (in inClause) String(idx *int) string {
ret := in.field + " in ("
for x := range in.values {
if x != 0 {
ret += ", "
}
ret += "$" + strconv.Itoa(*idx)
(*idx)++
}
ret += ")"
return ret
}

func (in inClause) Values() []interface{} {
return in.values
}

type whereClause struct {
field string
comp comparison
value interface{}
}

func (w whereClause) String(idx *int) string {
ret := w.field + string(w.comp) + "$" + strconv.Itoa(*idx)
(*idx)++
return ret
}

func (w whereClause) Values() []interface{} {
return []interface{}{w.value}
}

// AndClause returns a WhereClause that serializes to the AND
// of all the given where clauses.
func AndClause(wheres ...WhereClause) WhereClause {
return andClause(wheres)
}

type andClause []WhereClause

func (a andClause) String(idx *int) string {
wheres := make([]string, len(a))
for x := 0; x < len(a); x++ {
wheres[x] = a[x].String(idx)
}
return strings.Join(wheres, " AND ")
}

func (a andClause) Values() []interface{} {
vals := make([]interface{}, 0, len(a))
for x := 0; x < len(a); x++ {
vals = append(vals, a[x].Values()...)
}
return vals
}

// OrClause returns a WhereClause that serializes to the OR
// of all the given where clauses.
func OrClause(wheres ...WhereClause) WhereClause {
return orClause(wheres)
}

type orClause []WhereClause

func (o orClause) String(idx *int) string {
wheres := make([]string, len(o))
for x := 0; x < len(wheres); x++ {
wheres[x] = o[x].String(idx)
}
return strings.Join(wheres, " OR ")
}

func (o orClause) Values() []interface{} {
vals := make([]interface{}, len(o))
for x := 0; x < len(o); x++ {
vals = append(vals, o[x].Values()...)
}
return vals
}