Merge branch 'master' of github.com:nullbio/sqlboiler

This commit is contained in:
Patrick O'brien 2016-08-08 17:50:14 +10:00
commit 79ad706f78
16 changed files with 101 additions and 88 deletions

View file

@ -9,9 +9,9 @@ func TestColumnNames(t *testing.T) {
t.Parallel()
cols := []Column{
Column{Name: "one"},
Column{Name: "two"},
Column{Name: "three"},
{Name: "one"},
{Name: "two"},
{Name: "three"},
}
out := strings.Join(ColumnNames(cols), " ")
@ -22,8 +22,8 @@ func TestColumnNames(t *testing.T) {
func TestColumnDBTypes(t *testing.T) {
cols := []Column{
Column{Name: "test_one", DBType: "integer"},
Column{Name: "test_two", DBType: "interval"},
{Name: "test_one", DBType: "integer"},
{Name: "test_two", DBType: "interval"},
}
res := ColumnDBTypes(cols)

View file

@ -12,8 +12,8 @@ func (t testInterface) TableNames() ([]string, error) {
}
var testCols = []Column{
Column{Name: "col1", Type: "character varying"},
Column{Name: "col2", Type: "character varying", Nullable: true},
{Name: "col1", Type: "character varying"},
{Name: "col2", Type: "character varying", Nullable: true},
}
func (t testInterface) Columns(tableName string) ([]Column, error) {
@ -129,18 +129,18 @@ func TestSetForeignKeyConstraints(t *testing.T) {
t.Parallel()
tables := []Table{
Table{
{
Name: "one",
Columns: []Column{
Column{Name: "id1", Type: "string", Nullable: false, Unique: false},
Column{Name: "id2", Type: "string", Nullable: true, Unique: true},
{Name: "id1", Type: "string", Nullable: false, Unique: false},
{Name: "id2", Type: "string", Nullable: true, Unique: true},
},
},
Table{
{
Name: "other",
Columns: []Column{
Column{Name: "one_id_1", Type: "string", Nullable: false, Unique: false},
Column{Name: "one_id_2", Type: "string", Nullable: true, Unique: true},
{Name: "one_id_1", Type: "string", Nullable: false, Unique: false},
{Name: "one_id_2", Type: "string", Nullable: true, Unique: true},
},
FKeys: []ForeignKey{
{Column: "one_id_1", ForeignTable: "one", ForeignColumn: "id1"},
@ -184,16 +184,16 @@ func TestSetRelationships(t *testing.T) {
t.Parallel()
tables := []Table{
Table{
{
Name: "one",
Columns: []Column{
Column{Name: "id", Type: "string"},
{Name: "id", Type: "string"},
},
},
Table{
{
Name: "other",
Columns: []Column{
Column{Name: "other_id", Type: "string"},
{Name: "other_id", Type: "string"},
},
FKeys: []ForeignKey{{Column: "other_id", ForeignTable: "one", ForeignColumn: "id", Nullable: true}},
},

View file

@ -69,27 +69,27 @@ func TestAutoIncPrimaryKey(t *testing.T) {
Ok: true,
Expect: Column{Name: "one", Type: "int32", Nullable: false, Default: `nextval('abc'::regclass)`},
Pkey: &PrimaryKey{Name: "pkey", Columns: []string{"one"}},
Columns: []Column{Column{Name: "one", Type: "int32", Nullable: false, Default: `nextval('abc'::regclass)`}},
Columns: []Column{{Name: "one", Type: "int32", Nullable: false, Default: `nextval('abc'::regclass)`}},
},
"missingcase": {
Ok: false,
Pkey: &PrimaryKey{Name: "pkey", Columns: []string{"two"}},
Columns: []Column{Column{Name: "one", Type: "int32", Nullable: false, Default: `nextval('abc'::regclass)`}},
Columns: []Column{{Name: "one", Type: "int32", Nullable: false, Default: `nextval('abc'::regclass)`}},
},
"wrongtype": {
Ok: false,
Pkey: &PrimaryKey{Name: "pkey", Columns: []string{"one"}},
Columns: []Column{Column{Name: "one", Type: "string", Nullable: false, Default: `nextval('abc'::regclass)`}},
Columns: []Column{{Name: "one", Type: "string", Nullable: false, Default: `nextval('abc'::regclass)`}},
},
"nodefault": {
Ok: false,
Pkey: &PrimaryKey{Name: "pkey", Columns: []string{"one"}},
Columns: []Column{Column{Name: "one", Type: "string", Nullable: false, Default: ``}},
Columns: []Column{{Name: "one", Type: "string", Nullable: false, Default: ``}},
},
"nullable": {
Ok: false,
Pkey: &PrimaryKey{Name: "pkey", Columns: []string{"one"}},
Columns: []Column{Column{Name: "one", Type: "string", Nullable: true, Default: `nextval('abc'::regclass)`}},
Columns: []Column{{Name: "one", Type: "string", Nullable: true, Default: `nextval('abc'::regclass)`}},
},
}

View file

@ -9,9 +9,9 @@ func TestToManyRelationships(t *testing.T) {
t.Parallel()
tables := []Table{
Table{Name: "users", Columns: []Column{{Name: "id"}}},
Table{Name: "contests", Columns: []Column{{Name: "id"}}},
Table{
{Name: "users", Columns: []Column{{Name: "id"}}},
{Name: "contests", Columns: []Column{{Name: "id"}}},
{
Name: "videos",
Columns: []Column{
{Name: "id"},
@ -23,7 +23,7 @@ func TestToManyRelationships(t *testing.T) {
{Name: "videos_contest_id_fk", Column: "contest_id", ForeignTable: "contests", ForeignColumn: "id"},
},
},
Table{
{
Name: "notifications",
Columns: []Column{
{Name: "user_id"},
@ -34,7 +34,7 @@ func TestToManyRelationships(t *testing.T) {
{Name: "notifications_source_id_fk", Column: "source_id", ForeignTable: "users", ForeignColumn: "id"},
},
},
Table{
{
Name: "users_video_tags",
IsJoinTable: true,
Columns: []Column{
@ -51,7 +51,7 @@ func TestToManyRelationships(t *testing.T) {
relationships := ToManyRelationships("users", tables)
expected := []ToManyRelationship{
ToManyRelationship{
{
Column: "id",
Nullable: false,
Unique: false,
@ -63,7 +63,7 @@ func TestToManyRelationships(t *testing.T) {
ToJoinTable: false,
},
ToManyRelationship{
{
Column: "id",
Nullable: false,
Unique: false,
@ -75,7 +75,7 @@ func TestToManyRelationships(t *testing.T) {
ToJoinTable: false,
},
ToManyRelationship{
{
Column: "id",
Nullable: false,
Unique: false,
@ -87,7 +87,7 @@ func TestToManyRelationships(t *testing.T) {
ToJoinTable: false,
},
ToManyRelationship{
{
Column: "id",
Nullable: false,
Unique: false,
@ -125,9 +125,9 @@ func TestToManyRelationshipsNull(t *testing.T) {
t.Parallel()
tables := []Table{
Table{Name: "users", Columns: []Column{{Name: "id", Nullable: true, Unique: true}}},
Table{Name: "contests", Columns: []Column{{Name: "id", Nullable: true, Unique: true}}},
Table{
{Name: "users", Columns: []Column{{Name: "id", Nullable: true, Unique: true}}},
{Name: "contests", Columns: []Column{{Name: "id", Nullable: true, Unique: true}}},
{
Name: "videos",
Columns: []Column{
{Name: "id", Nullable: true, Unique: true},
@ -139,7 +139,7 @@ func TestToManyRelationshipsNull(t *testing.T) {
{Name: "videos_contest_id_fk", Column: "contest_id", ForeignTable: "contests", ForeignColumn: "id", Nullable: true, Unique: true},
},
},
Table{
{
Name: "notifications",
Columns: []Column{
{Name: "user_id", Nullable: true, Unique: true},
@ -150,7 +150,7 @@ func TestToManyRelationshipsNull(t *testing.T) {
{Name: "notifications_source_id_fk", Column: "source_id", ForeignTable: "users", ForeignColumn: "id", Nullable: true, Unique: true},
},
},
Table{
{
Name: "users_video_tags",
IsJoinTable: true,
Columns: []Column{
@ -170,7 +170,7 @@ func TestToManyRelationshipsNull(t *testing.T) {
}
expected := []ToManyRelationship{
ToManyRelationship{
{
Column: "id",
Nullable: true,
Unique: true,
@ -182,7 +182,7 @@ func TestToManyRelationshipsNull(t *testing.T) {
ToJoinTable: false,
},
ToManyRelationship{
{
Column: "id",
Nullable: true,
Unique: true,
@ -194,7 +194,7 @@ func TestToManyRelationshipsNull(t *testing.T) {
ToJoinTable: false,
},
ToManyRelationship{
{
Column: "id",
Nullable: true,
Unique: true,
@ -206,7 +206,7 @@ func TestToManyRelationshipsNull(t *testing.T) {
ToJoinTable: false,
},
ToManyRelationship{
{
Column: "id",
Nullable: true,
Unique: true,

View file

@ -37,7 +37,7 @@ func TestGetColumn(t *testing.T) {
table := Table{
Columns: []Column{
Column{Name: "one"},
{Name: "one"},
},
}
@ -53,7 +53,7 @@ func TestGetColumnMissing(t *testing.T) {
table := Table{
Columns: []Column{
Column{Name: "one"},
{Name: "one"},
},
}

1
boil/_fixtures/07.sql Normal file
View file

@ -0,0 +1 @@
SELECT * FROM "a" GROUP BY id,name HAVING id <> 1,length(name, 'utf8') > 5;

View file

@ -224,7 +224,7 @@ func Update(columns map[string]interface{}) string {
return strings.Join(names, ", ")
}
// SetParamNames takes a slice of columns and returns a comma seperated
// SetParamNames takes a slice of columns and returns a comma separated
// list of parameter names for a template statement SET clause.
// eg: "col1"=$1, "col2"=$2, "col3"=$3
func SetParamNames(columns []string) string {

View file

@ -36,7 +36,7 @@ type Query struct {
type where struct {
clause string
orSeperator bool
orSeparator bool
args []interface{}
}
@ -180,9 +180,9 @@ func SetWhere(q *Query, clause string, args ...interface{}) {
q.where = append([]where(nil), where{clause: clause, args: args})
}
// SetLastWhereAsOr sets the or seperator for the last element in the where slice
// SetLastWhereAsOr sets the or separator for the last element in the where slice
func SetLastWhereAsOr(q *Query) {
q.where[len(q.where)-1].orSeperator = true
q.where[len(q.where)-1].orSeparator = true
}
// ApplyGroupBy on the query.

View file

@ -73,6 +73,14 @@ func buildSelectQuery(q *Query) (*bytes.Buffer, []interface{}) {
where, args := whereClause(q)
buf.WriteString(where)
if len(q.groupBy) != 0 {
fmt.Fprintf(buf, " GROUP BY %s", strings.Join(q.groupBy, ","))
}
if len(q.having) != 0 {
fmt.Fprintf(buf, " HAVING %s", strings.Join(q.having, ","))
}
if len(q.orderBy) != 0 {
buf.WriteString(" ORDER BY ")
buf.WriteString(strings.Join(q.orderBy, `,`))
@ -172,7 +180,7 @@ func whereClause(q *Query) (string, []interface{}) {
if i >= len(q.where)-1 {
continue
}
if q.where[i].orSeperator {
if q.where[i].orSeparator {
buf.WriteString(" OR ")
} else {
buf.WriteString(" AND ")

View file

@ -39,6 +39,11 @@ func TestBuildQuery(t *testing.T) {
from: []string{"happiness as a"},
joins: []join{{clause: "rainbows r on a.id = r.happy_id"}},
}, nil},
{&Query{
from: []string{"a"},
groupBy: []string{"id", "name"},
having: []string{"id <> 1", "length(name, 'utf8') > 5"},
}, nil},
}
for i, test := range tests {

View file

@ -12,8 +12,8 @@ func TestSetLastWhereAsOr(t *testing.T) {
AppendWhere(q, "")
if q.where[0].orSeperator {
t.Errorf("Do not want or seperator")
if q.where[0].orSeparator {
t.Errorf("Do not want or separator")
}
SetLastWhereAsOr(q)
@ -21,8 +21,8 @@ func TestSetLastWhereAsOr(t *testing.T) {
if len(q.where) != 1 {
t.Errorf("Want len 1")
}
if !q.where[0].orSeperator {
t.Errorf("Want or seperator")
if !q.where[0].orSeparator {
t.Errorf("Want or separator")
}
AppendWhere(q, "")
@ -31,10 +31,10 @@ func TestSetLastWhereAsOr(t *testing.T) {
if len(q.where) != 2 {
t.Errorf("Want len 2")
}
if q.where[0].orSeperator != true {
if q.where[0].orSeparator != true {
t.Errorf("Expected true")
}
if q.where[1].orSeperator != true {
if q.where[1].orSeparator != true {
t.Errorf("Expected true")
}
}

View file

@ -164,7 +164,7 @@ func TestBind_InnerJoinSelect(t *testing.T) {
testResults := []*struct {
Happy struct {
ID int
} `boil:",bind"`
} `boil:"h,bind"`
Fun struct {
ID int
} `boil:",bind"`

View file

@ -155,7 +155,7 @@ var defaultTemplateImports = imports{
}
var defaultSingletonTemplateImports = map[string]imports{
"boil_helpers": imports{
"boil_helpers": {
standard: importList{},
thirdParty: importList{
`"github.com/nullbio/sqlboiler/boil"`,
@ -179,7 +179,7 @@ var defaultTestTemplateImports = imports{
}
var defaultSingletonTestTemplateImports = map[string]imports{
"boil_main_helpers": imports{
"boil_main_helpers": {
standard: importList{
`"database/sql"`,
`"os"`,
@ -189,7 +189,7 @@ var defaultSingletonTestTemplateImports = map[string]imports{
`"github.com/spf13/viper"`,
},
},
"boil_helpers": imports{
"boil_helpers": {
standard: importList{
`"crypto/md5"`,
`"fmt"`,
@ -205,7 +205,7 @@ var defaultSingletonTestTemplateImports = map[string]imports{
}
var defaultTestMainImports = map[string]imports{
"postgres": imports{
"postgres": {
standard: importList{
`"testing"`,
`"os"`,
@ -233,52 +233,52 @@ var defaultTestMainImports = map[string]imports{
// database requires one of the following special types. Check
// TranslateColumnType to see the type assignments.
var importsBasedOnType = map[string]imports{
"null.Float32": imports{
"null.Float32": {
thirdParty: importList{`"gopkg.in/nullbio/null.v4"`},
},
"null.Float64": imports{
"null.Float64": {
thirdParty: importList{`"gopkg.in/nullbio/null.v4"`},
},
"null.Int": imports{
"null.Int": {
thirdParty: importList{`"gopkg.in/nullbio/null.v4"`},
},
"null.Int8": imports{
"null.Int8": {
thirdParty: importList{`"gopkg.in/nullbio/null.v4"`},
},
"null.Int16": imports{
"null.Int16": {
thirdParty: importList{`"gopkg.in/nullbio/null.v4"`},
},
"null.Int32": imports{
"null.Int32": {
thirdParty: importList{`"gopkg.in/nullbio/null.v4"`},
},
"null.Int64": imports{
"null.Int64": {
thirdParty: importList{`"gopkg.in/nullbio/null.v4"`},
},
"null.Uint": imports{
"null.Uint": {
thirdParty: importList{`"gopkg.in/nullbio/null.v4"`},
},
"null.Uint8": imports{
"null.Uint8": {
thirdParty: importList{`"gopkg.in/nullbio/null.v4"`},
},
"null.Uint16": imports{
"null.Uint16": {
thirdParty: importList{`"gopkg.in/nullbio/null.v4"`},
},
"null.Uint32": imports{
"null.Uint32": {
thirdParty: importList{`"gopkg.in/nullbio/null.v4"`},
},
"null.Uint64": imports{
"null.Uint64": {
thirdParty: importList{`"gopkg.in/nullbio/null.v4"`},
},
"null.String": imports{
"null.String": {
thirdParty: importList{`"gopkg.in/nullbio/null.v4"`},
},
"null.Bool": imports{
"null.Bool": {
thirdParty: importList{`"gopkg.in/nullbio/null.v4"`},
},
"null.Time": imports{
"null.Time": {
thirdParty: importList{`"gopkg.in/nullbio/null.v4"`},
},
"time.Time": imports{
"time.Time": {
standard: importList{`"time"`},
},
}

View file

@ -78,16 +78,16 @@ func TestCombineTypeImports(t *testing.T) {
}
cols := []bdb.Column{
bdb.Column{
{
Type: "null.Time",
},
bdb.Column{
{
Type: "null.Time",
},
bdb.Column{
{
Type: "time.Time",
},
bdb.Column{
{
Type: "null.Float",
},
}

View file

@ -8,7 +8,6 @@
{{- else -}}
{{- $rel := textsFromRelationship $dot.Tables $table . -}}
func Test{{$rel.LocalTable.NameGo}}ToMany{{$rel.Function.Name}}(t *testing.T) {
t.Skip("this test is broke")
var err error
tx := MustTx(boil.Begin())
defer tx.Rollback()

View file

@ -16,18 +16,18 @@ func (fakeDB) TableNames() ([]string, error) {
}
func (fakeDB) Columns(tableName string) ([]bdb.Column, error) {
return map[string][]bdb.Column{
"users": []bdb.Column{{Name: "id", Type: "int32"}},
"contests": []bdb.Column{{Name: "id", Type: "int32", Nullable: true}},
"videos": []bdb.Column{
"users": {{Name: "id", Type: "int32"}},
"contests": {{Name: "id", Type: "int32", Nullable: true}},
"videos": {
{Name: "id", Type: "int32"},
{Name: "user_id", Type: "int32", Nullable: true, Unique: true},
{Name: "contest_id", Type: "int32"},
},
"notifications": []bdb.Column{
"notifications": {
{Name: "user_id", Type: "int32"},
{Name: "source_id", Type: "int32", Nullable: true},
},
"users_videos_tags": []bdb.Column{
"users_videos_tags": {
{Name: "user_id", Type: "int32"},
{Name: "video_id", Type: "int32"},
},
@ -35,15 +35,15 @@ func (fakeDB) Columns(tableName string) ([]bdb.Column, error) {
}
func (fakeDB) ForeignKeyInfo(tableName string) ([]bdb.ForeignKey, error) {
return map[string][]bdb.ForeignKey{
"videos": []bdb.ForeignKey{
"videos": {
{Name: "videos_user_id_fk", Column: "user_id", ForeignTable: "users", ForeignColumn: "id"},
{Name: "videos_contest_id_fk", Column: "contest_id", ForeignTable: "contests", ForeignColumn: "id"},
},
"notifications": []bdb.ForeignKey{
"notifications": {
{Name: "notifications_user_id_fk", Column: "user_id", ForeignTable: "users", ForeignColumn: "id"},
{Name: "notifications_source_id_fk", Column: "source_id", ForeignTable: "users", ForeignColumn: "id"},
},
"users_videos_tags": []bdb.ForeignKey{
"users_videos_tags": {
{Name: "user_id_fk", Column: "user_id", ForeignTable: "users", ForeignColumn: "id"},
{Name: "video_id_fk", Column: "video_id", ForeignTable: "videos", ForeignColumn: "id"},
},
@ -57,7 +57,7 @@ func (fakeDB) TranslateColumnType(c bdb.Column) bdb.Column {
}
func (fakeDB) PrimaryKeyInfo(tableName string) (*bdb.PrimaryKey, error) {
return map[string]*bdb.PrimaryKey{
"users_videos_tags": &bdb.PrimaryKey{
"users_videos_tags": {
Name: "user_video_id_pkey",
Columns: []string{"user_id", "video_id"},
},