Changed From qmod to Table, added default values
* Added drivername to tplData
This commit is contained in:
parent
c17e48c14a
commit
a957bc3836
13 changed files with 69 additions and 35 deletions
|
@ -70,8 +70,8 @@ func Having(clause string) QueryMod {
|
|||
}
|
||||
}
|
||||
|
||||
func From(table string) QueryMod {
|
||||
func Table(table string) QueryMod {
|
||||
return func(q *boil.Query) {
|
||||
boil.SetFrom(q, table)
|
||||
boil.SetTable(q, table)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -22,7 +22,7 @@ type Query struct {
|
|||
delete bool
|
||||
update map[string]interface{}
|
||||
selectCols []string
|
||||
from string
|
||||
table string
|
||||
innerJoins []join
|
||||
outerJoins []join
|
||||
leftOuterJoins []join
|
||||
|
@ -62,7 +62,7 @@ func buildSelectQuery(q *Query) (*bytes.Buffer, []interface{}) {
|
|||
}
|
||||
|
||||
buf.WriteString(" FROM ")
|
||||
fmt.Fprintf(buf, `"%s"`, q.from)
|
||||
fmt.Fprintf(buf, `"%s"`, q.table)
|
||||
|
||||
return buf, []interface{}{}
|
||||
}
|
||||
|
@ -113,8 +113,8 @@ func SetSelect(q *Query, columns ...string) {
|
|||
q.selectCols = append(q.selectCols, columns...)
|
||||
}
|
||||
|
||||
func SetFrom(q *Query, table string) {
|
||||
q.from = table
|
||||
func SetTable(q *Query, table string) {
|
||||
q.table = table
|
||||
}
|
||||
|
||||
func SetInnerJoin(q *Query, on string, args ...interface{}) {
|
||||
|
|
|
@ -25,7 +25,7 @@ func TestBuildQuery(t *testing.T) {
|
|||
q *Query
|
||||
args []interface{}
|
||||
}{
|
||||
{&Query{from: "t"}, []interface{}{}},
|
||||
{&Query{table: "t"}, []interface{}{}},
|
||||
}
|
||||
|
||||
for i, test := range tests {
|
||||
|
@ -128,15 +128,15 @@ func TestSetHaving(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestSetFrom(t *testing.T) {
|
||||
func TestSetTable(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
q := &Query{}
|
||||
SetFrom(q, "videos a, orders b")
|
||||
SetTable(q, "videos a, orders b")
|
||||
|
||||
expect := "videos a, orders b"
|
||||
if q.from != expect {
|
||||
t.Errorf("Expected %s, got %s", expect, q.from)
|
||||
if q.table != expect {
|
||||
t.Errorf("Expected %s, got %s", expect, q.table)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -80,10 +80,15 @@ func generateSinglesOutput(cmdData *CmdData) error {
|
|||
return errors.New("No single templates located for generation")
|
||||
}
|
||||
|
||||
tplData := &tplData{
|
||||
PkgName: cmdData.PkgName,
|
||||
DriverName: cmdData.DriverName,
|
||||
}
|
||||
|
||||
for _, template := range cmdData.SingleTemplates {
|
||||
var imps imports
|
||||
|
||||
resp, err := generateTemplate(template, &tplData{})
|
||||
resp, err := generateTemplate(template, tplData)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Error generating template %s: %s", template.Name(), err)
|
||||
}
|
||||
|
@ -117,7 +122,12 @@ func generateTestMainOutput(cmdData *CmdData) error {
|
|||
imps.standard = sqlBoilerTestMainImports[cmdData.DriverName].standard
|
||||
imps.thirdparty = sqlBoilerTestMainImports[cmdData.DriverName].thirdparty
|
||||
|
||||
resp, err := generateTemplate(cmdData.TestMainTemplate, &tplData{})
|
||||
tplData := &tplData{
|
||||
PkgName: cmdData.PkgName,
|
||||
DriverName: cmdData.DriverName,
|
||||
}
|
||||
|
||||
resp, err := generateTemplate(cmdData.TestMainTemplate, tplData)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -130,8 +130,9 @@ func (c *CmdData) run(includeTests bool) error {
|
|||
|
||||
for _, table := range c.Tables {
|
||||
data := &tplData{
|
||||
Table: table,
|
||||
PkgName: c.PkgName,
|
||||
Table: table,
|
||||
DriverName: c.DriverName,
|
||||
PkgName: c.PkgName,
|
||||
}
|
||||
|
||||
// Generate the regular templates
|
||||
|
|
|
@ -11,6 +11,6 @@ func {{$tableNamePlural}}(mods ...qs.QueryMod) {{$varNameSingular}}Query {
|
|||
}
|
||||
|
||||
func {{$tableNamePlural}}X(exec boil.Executor, mods ...qs.QueryMod) {{$varNameSingular}}Query {
|
||||
mods = append(mods, qs.From("{{.Table.Name}}"))
|
||||
mods = append(mods, qs.Table("{{.Table.Name}}"))
|
||||
return {{$varNameSingular}}Query{NewQueryX(exec, mods...)}
|
||||
}
|
||||
|
|
|
@ -4,14 +4,22 @@
|
|||
// Delete deletes a single {{$tableNameSingular}} record.
|
||||
// Delete will match against the primary key column to find the record to delete.
|
||||
func (o *{{$tableNameSingular}}) Delete() error {
|
||||
if o == nil {
|
||||
return errors.New("{{.PkgName}}: no {{$tableNameSingular}} provided for deletion")
|
||||
}
|
||||
|
||||
return o.DeleteX(boil.GetDB())
|
||||
}
|
||||
|
||||
func (o *{{$tableNameSingular}}) DeleteX(exec boil.Executor) error {
|
||||
if o == nil {
|
||||
return errors.New("{{.PkgName}}: no {{$tableNameSingular}} provided for deletion")
|
||||
}
|
||||
|
||||
var mods []qs.QueryMod
|
||||
|
||||
mods = append(mods,
|
||||
qs.From("{{.Table.Name}}"),
|
||||
qs.Table("{{.Table.Name}}"),
|
||||
qs.Where("{{wherePrimaryKey .Table.PKey.Columns 1}}", {{paramsPrimaryKey "o." .Table.PKey.Columns true}}),
|
||||
)
|
||||
|
||||
|
@ -27,9 +35,13 @@ func (o *{{$tableNameSingular}}) DeleteX(exec boil.Executor) error {
|
|||
}
|
||||
|
||||
func (o {{$varNameSingular}}Query) DeleteAll() error {
|
||||
boil.SetDelete(o)
|
||||
if o.Query == nil {
|
||||
return errors.New("{{.PkgName}}: no {{$varNameSingular}}Query provided for delete all")
|
||||
}
|
||||
|
||||
_, err := boil.ExecQuery(query)
|
||||
boil.SetDelete(o.Query)
|
||||
|
||||
_, err := boil.ExecQuery(o.Query)
|
||||
if err != nil {
|
||||
return fmt.Errorf("{{.PkgName}}: unable to delete all from {{.Table.Name}}: %s", err)
|
||||
}
|
||||
|
@ -38,17 +50,25 @@ func (o {{$varNameSingular}}Query) DeleteAll() error {
|
|||
}
|
||||
|
||||
func (o {{$varNameSingular}}Slice) DeleteAll() error {
|
||||
return DeleteAllX(boil.GetDB())
|
||||
if o == nil {
|
||||
return errors.New("{{.PkgName}}: no {{$tableNameSingular}} slice provided for delete all")
|
||||
}
|
||||
|
||||
return o.DeleteAllX(boil.GetDB())
|
||||
}
|
||||
|
||||
func (o {{$varNameSingular}}Slice) DeleteAllX(exec boil.Executor) error {
|
||||
if o == nil {
|
||||
return errors.New("{{.PkgName}}: no {{$tableNameSingular}} slice provided for delete all")
|
||||
}
|
||||
|
||||
var mods []qs.QueryMod
|
||||
|
||||
args := o.inPrimaryKeyArgs()
|
||||
in := boil.WherePrimaryKeyIn(len(o), {{primaryKeyStrList .Table.PKey.Columns}})
|
||||
|
||||
mods = append(mods,
|
||||
qs.From("{{.Table.Name}}"),
|
||||
qs.Table("{{.Table.Name}}"),
|
||||
qs.Where(in, args...),
|
||||
)
|
||||
|
||||
|
|
|
@ -3,21 +3,21 @@
|
|||
{{- $dbName := singular .Table.Name -}}
|
||||
{{- $varNameSingular := camelCaseSingular .Table.Name -}}
|
||||
// {{$tableNameSingular}}Find retrieves a single record by ID.
|
||||
func {{$tableNameSingular}}Find({{primaryKeyFuncSig .Table.Columns .Table.PKey.Columns}}, columns ...string) (*{{$tableNameSingular}}, error) {
|
||||
return {{$tableNameSingular}}FindX(boil.GetDB(), {{camelCaseCommaList .Table.PKey.Columns}}, columns...)
|
||||
func {{$tableNameSingular}}Find({{primaryKeyFuncSig .Table.Columns .Table.PKey.Columns}}, selectCols ...string) (*{{$tableNameSingular}}, error) {
|
||||
return {{$tableNameSingular}}FindX(boil.GetDB(), {{camelCaseCommaList .Table.PKey.Columns}}, selectCols...)
|
||||
}
|
||||
|
||||
func {{$tableNameSingular}}FindX(exec boil.Executor, {{primaryKeyFuncSig .Table.Columns .Table.PKey.Columns}}, columns ...string) (*{{$tableNameSingular}}, error) {
|
||||
func {{$tableNameSingular}}FindX(exec boil.Executor, {{primaryKeyFuncSig .Table.Columns .Table.PKey.Columns}}, selectCols ...string) (*{{$tableNameSingular}}, error) {
|
||||
var {{$varNameSingular}} *{{$tableNameSingular}}
|
||||
mods := []qs.QueryMod{
|
||||
qs.Select(columns...),
|
||||
qs.From("{{.Table.Name}}"),
|
||||
qs.Select(selectCols...),
|
||||
qs.Table("{{.Table.Name}}"),
|
||||
qs.Where("{{wherePrimaryKey .Table.PKey.Columns 1}}", {{camelCaseCommaList .Table.PKey.Columns}}),
|
||||
}
|
||||
|
||||
q := NewQueryX(exec, mods...)
|
||||
|
||||
err := boil.ExecQueryOne(q).Scan(boil.GetStructPointers(&{{$varNameSingular}}, columns...)...)
|
||||
err := boil.ExecQueryOne(q).Scan(boil.GetStructPointers(&{{$varNameSingular}}, selectCols...)...)
|
||||
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("{{.PkgName}}: unable to select from {{.Table.Name}}: %s", err)
|
||||
|
|
|
@ -1,12 +1,12 @@
|
|||
{{- $tableNameSingular := titleCaseSingular .Table.Name -}}
|
||||
{{- $varNameSingular := camelCaseSingular .Table.Name -}}
|
||||
type {{$tableNameSingular}}Slice []*{{$tableNameSingular}}
|
||||
type {{$varNameSingular}}Slice []*{{$tableNameSingular}}
|
||||
|
||||
func ({{$varNameSingular}}Query) One() (*{{$tableNameSingular}}, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func ({{$varNameSingular}}Query) All() ({{$tableNameSingular}}Slice, error) {
|
||||
func ({{$varNameSingular}}Query) All() ({{$varNameSingular}}Slice, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
|
|
|
@ -6,7 +6,7 @@ func (o {{$varNameSingular}}Slice) inPrimaryKeyArgs() []interface{} {
|
|||
|
||||
for i := 0; i < len(o); i++ {
|
||||
{{- range $key, $value := .Table.PKey.Columns }}
|
||||
args = append(args, o.{{titleCase $value}})
|
||||
args = append(args, o[i].{{titleCase $value}})
|
||||
{{ end -}}
|
||||
}
|
||||
|
||||
|
|
|
@ -34,8 +34,9 @@ type CmdData struct {
|
|||
|
||||
// tplData is used to pass data to the template
|
||||
type tplData struct {
|
||||
Table dbdrivers.Table
|
||||
PkgName string
|
||||
Table dbdrivers.Table
|
||||
DriverName string
|
||||
PkgName string
|
||||
}
|
||||
|
||||
type importList []string
|
||||
|
|
|
@ -36,6 +36,7 @@ type Table struct {
|
|||
type Column struct {
|
||||
Name string
|
||||
Type string
|
||||
Default string
|
||||
IsNullable bool
|
||||
}
|
||||
|
||||
|
|
|
@ -80,7 +80,7 @@ func (p *PostgresDriver) Columns(tableName string) ([]Column, error) {
|
|||
var columns []Column
|
||||
|
||||
rows, err := p.dbConn.Query(`
|
||||
SELECT column_name, data_type, is_nullable from
|
||||
SELECT column_name, data_type, column_default, is_nullable from
|
||||
information_schema.columns WHERE table_name=$1
|
||||
`, tableName)
|
||||
|
||||
|
@ -90,13 +90,14 @@ func (p *PostgresDriver) Columns(tableName string) ([]Column, error) {
|
|||
|
||||
defer rows.Close()
|
||||
for rows.Next() {
|
||||
var colName, colType, isNullable string
|
||||
if err := rows.Scan(&colName, &colType, &isNullable); err != nil {
|
||||
var colName, colType, colDefault, isNullable string
|
||||
if err := rows.Scan(&colName, &colType, &colDefault, &isNullable); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
column := Column{
|
||||
Name: colName,
|
||||
Type: colType,
|
||||
Default: colDefault,
|
||||
IsNullable: isNullable == "YES",
|
||||
}
|
||||
columns = append(columns, column)
|
||||
|
|
Loading…
Reference in a new issue