Example #1
0
func TestExcept(t *testing.T) {
	relationOne := nodes.Relation("table_one")
	relationTwo := nodes.Relation("table_two")
	relationThree := nodes.Relation("table_three")
	one := nodes.SelectStatement(relationOne)
	two := nodes.SelectStatement(relationTwo)
	three := nodes.SelectStatement(relationThree)
	one.Combinator = nodes.Except(one, two)
	two.Combinator = nodes.Except(two, three)
	expected := `(SELECT FROM "table_one" EXCEPT (SELECT FROM "table_two" EXCEPT SELECT FROM "table_three"))`
	if got, _ := sql.Accept(one); expected != got {
		t.Errorf("TestUnion was expected to return %s, got %s", expected, got)
	}
}
Example #2
0
func TestSelectManager(t *testing.T) {
	relation := nodes.Relation("table")
	mgr := managers.Selection(relation)

	// The following struct members should exist.
	_ = mgr.Tree
	_ = mgr.Context

	// The following receiver methods should exist.
	_ = mgr.Project(1)
	_ = mgr.Where(1)
	_ = mgr.Offset(1)
	_ = mgr.Limit(1)
	_ = mgr.InnerJoin(1)
	_ = mgr.OuterJoin(1)
	_ = mgr.On(1)
	_ = mgr.Order(1)
	_ = mgr.Group(1)
	_ = mgr.Having(1)
	_ = mgr.Union(managers.Selection(relation))
	_ = mgr.Intersect(managers.Selection(relation))
	_ = mgr.Except(managers.Selection(relation))
	_ = mgr.SetAdapter(1)
	_, _ = mgr.ToSql()
}
Example #3
0
func TestUnaliasedRelation(t *testing.T) {
	relation := nodes.Relation("table")
	expected := `"table"`
	if got, _ := sql.Accept(relation); expected != got {
		t.Errorf("TestUnaliasedRelation was expected to return %s, got %s", expected, got)
	}
}
Example #4
0
func TestDeleteStatement(t *testing.T) {
	relation := nodes.Relation("table")
	stmt := nodes.DeleteStatement(relation)
	expected := `DELETE FROM "table" `
	if got, _ := sql.Accept(stmt); expected != got {
		t.Errorf("TestDeleteStatement was expected to return %s, got %s", expected, got)
	}
}
Example #5
0
func TestInsertStatement(t *testing.T) {
	relation := nodes.Relation("table")
	stmt := nodes.InsertStatement(relation)
	expected := `INSERT INTO "table" `
	if got, _ := sql.Accept(stmt); expected != got {
		t.Errorf("TestInsertStatement was expected to return %s, got %s", expected, got)
	}
}
Example #6
0
func TestSelectCore(t *testing.T) {
	relation := nodes.Relation("table")
	core := nodes.SelectCore(relation)
	expected := `SELECT FROM "table"`
	if got, _ := sql.Accept(core); expected != got {
		t.Errorf("TestSelectCore was expected to return %s, got %s", expected, got)
	}
}
Example #7
0
func TestJoinSource(t *testing.T) {
	relation := nodes.Relation("table")
	source := nodes.JoinSource(relation)
	expected := `"table"`
	if got, _ := sql.Accept(source); expected != got {
		t.Errorf("TestJoinSource was expected to return %s, got %s", expected, got)
	}
}
Example #8
0
func TestVisitForeignKey(t *testing.T) {
	fkey := nodes.ForeignKey([]interface{}{nodes.UnqualifiedColumn("column")})
	fkey.Options = append(fkey.Options, nodes.Relation("table"))
	expected := `ADD FOREIGN KEY("column") REFERENCES "table"`
	if got, _ := sql.Accept(fkey); expected != got {
		t.Errorf("TestVisitForeignKey was expected to return %s, got %s", expected, got)
	}
}
Example #9
0
func TestExtensiveJoinSource(t *testing.T) {
	relation := nodes.Relation("table")
	source := nodes.JoinSource(relation)
	source.Right = append(source.Right, []interface{}{1, 2, 3}...)
	expected := `"table" 1 2 3`
	if got, _ := sql.Accept(source); expected != got {
		t.Errorf("TestExtensiveJoinSource was expected to return %s, got %s", expected, got)
	}
}
Example #10
0
func TestUnaliasedAttribute(t *testing.T) {
	relation := nodes.Relation("table")
	column := nodes.Column("column")
	attribute := nodes.Attribute(column, relation)
	expected := `"table"."column"`
	if got, _ := sql.Accept(attribute); expected != got {
		t.Errorf("TestUnaliasedAttribute was expected to return %s, got %s", expected, got)
	}
}
Example #11
0
// Table returns an Accessor from the managers package for
// generating SQL to interact with existing tables.
func Table(name string) managers.Accessor {
	relation := nodes.Relation(name)
	return func(name interface{}) *nodes.AttributeNode {
		if _, ok := name.(string); ok {
			return nodes.Attribute(nodes.Column(name), relation)
		}

		return nodes.Attribute(name, relation)
	}
}
Example #12
0
func TestSelectCoreExtensive(t *testing.T) {
	relation := nodes.Relation("table")
	core := nodes.SelectCore(relation)
	core.Projections = append(core.Projections, 1, 2)
	core.Wheres = append(core.Wheres, 3, 4)
	core.Source.Right = append(core.Source.Right, nodes.InnerJoin(5, nil))
	expected := `SELECT 1, 2 FROM "table" INNER JOIN 5 WHERE 3 AND 4`
	if got, _ := sql.Accept(core); expected != got {
		t.Errorf("TestSelectCoreExtensive was expected to return %s, got %s", expected, got)
	}
}
Example #13
0
func TestDeleteManager(t *testing.T) {
	relation := nodes.Relation("table")
	mgr := managers.Deletion(relation)

	// The following struct members should exist.
	_ = mgr.Tree

	// The following receiver methods should exist.
	_ = mgr.Delete(1)
	_ = mgr.SetAdapter(1)
	_, _ = mgr.ToSql()
}
Example #14
0
func TestAlterManager(t *testing.T) {
	relation := nodes.Relation("table")
	mgr := managers.Alteration(relation)

	// The following struct members should exist.
	_ = mgr.Tree

	// The following receiver methods should exist.
	_ = mgr.AddColumn(1, sql.String)
	_ = mgr.AddConstraint([]interface{}{1}, sql.Unique, 1, 2, 3)
	_ = mgr.SetAdapter(1)
	_, _ = mgr.ToSql()
}
Example #15
0
func TestInsertManager(t *testing.T) {
	relation := nodes.Relation("table")
	mgr := managers.Insertion(relation)

	// The following struct members should exist.
	_ = mgr.Tree

	// The following receiver methods should exist.
	_ = mgr.Insert(1)
	_ = mgr.Into(1)
	_ = mgr.Returning(1)
	_ = mgr.SetAdapter(1)
	_, _ = mgr.ToSql()
}
Example #16
0
func (self *ToSqlVisitor) VisitForeignKey(o *nodes.ForeignKeyNode, visitor VisitorInterface) string {
	if 0 >= len(o.Options) {
		panic("Missing column REFERENCE name for FOREIGN KEY constraint.")
	}

	str := "ADD "
	// For FOREIGN KEY, index name is optional, REFERENCES is not.
	//
	// No index name ex.
	//
	//  CreateTable("orders").
	//    AddColumn("user_id").
	//    AddConstraint("user_id", FOREIGN_KEY, "users")
	//
	// With option index name ex.
	//
	//  CreateTable("orders").
	//    AddColumn("user_id").
	//    AddConstraint("user_id", FOREIGN_KEY, "users_fkey", "users")
	if 1 < len(o.Options) {
		expr := o.Options[0]
		if _, ok := expr.(string); ok {
			expr = nodes.IndexName(expr)
		}

		// Remove this item from the array, avoiding any potential memory leak.
		// https://code.google.com/p/go-wiki/wiki/SliceTricks
		length := len(o.Options) - 1
		copy(o.Options[0:], o.Options[1:])
		o.Options[length] = nil
		o.Options = o.Options[:length]

		str = fmt.Sprintf("%vCONSTRAINT %v%v", str, visitor.Visit(expr, visitor), SPACE)
	}

	str = fmt.Sprintf("%vFOREIGN KEY(%v)", str, visitor.FormatConstraintColumns(o.Columns, visitor))

	// If option is not here, user didn't do it right, but don't dereference and panic.
	if 0 < len(o.Options) {
		relation := o.Options[0]
		if _, ok := relation.(string); ok {
			relation = nodes.Relation(relation.(string))
		}

		str = fmt.Sprintf("%v%vREFERENCES %v", str, SPACE, visitor.Visit(relation, visitor))
	}

	return str
}
Example #17
0
func TestUpdateManager(t *testing.T) {
	relation := nodes.Relation("table")
	mgr := managers.Modification(relation)

	// The following struct members should exist.
	_ = mgr.Tree

	// The following receiver methods should exist.
	_ = mgr.Set(1)
	_ = mgr.To(1)
	_ = mgr.Where(1)
	_ = mgr.Limit(1)
	_ = mgr.SetAdapter(1)
	_, _ = mgr.ToSql()
}
Example #18
0
func TestAttribute(t *testing.T) {
	attr := nodes.Attribute("column", nodes.Relation("table"))

	// The following struct members should exist.
	_ = attr.Name
	_ = attr.Relation

	// The following receiver methods should exist.
	_ = attr.And(1)
	_ = attr.Or(1)
	_ = attr.Eq(1)
	_ = attr.Neq(1)
	_ = attr.Gt(1)
	_ = attr.Gte(1)
	_ = attr.Lt(1)
	_ = attr.Lte(1)
	_ = attr.Like(1)
	_ = attr.Unlike(1)
	_ = attr.Asc()
	_ = attr.Desc()
}
Example #19
0
// CreateTable returns an AlterManager from the managers package
// for generating SQL to create new tables.
func CreateTable(name string) *managers.CreateManager {
	relation := nodes.Relation(name)
	return managers.Creation(relation)
}
Example #20
0
// CreateTable returns an AlterManager from the managers package
// for generating SQL to alter existing tables.
func AlterTable(name string) *managers.AlterManager {
	relation := nodes.Relation(name)
	return managers.Alteration(relation)
}