Esempio n. 1
0
func TestSetFirstResult(t *testing.T) {
	connection := NewTestConnection(t)
	qb := db.NewQueryBuilder(connection)
	qb.SetFirstResult(10)
	test.Fatal(t, qb.GetState(), db.Dirty)
	test.Fatal(t, qb.GetFirstResult(), 10)
}
Esempio n. 2
0
func TestDeleteWithoutAlias(t *testing.T) {
	connection := NewTestConnection(t)
	qb := db.NewQueryBuilder(connection)
	qb.Delete("users")
	test.Fatal(t, db.Delete, qb.GetType())
	test.Fatal(t, "DELETE FROM users", qb.String())
}
Esempio n. 3
0
func TestBuilderSelect(t *testing.T) {
	connection := NewTestConnection(t)
	qb := db.NewQueryBuilder(connection)
	qb.Select("u.id").
		From("users", "u")
	test.Fatal(t, qb.String(), "SELECT u.id FROM users u")
}
Esempio n. 4
0
func TestSelectAllWithoutTableAlias(t *testing.T) {
	connection := NewTestConnection(t)
	qb := db.NewQueryBuilder(connection)
	qb.Select("*").
		From("users")
	test.Fatal(t, qb.String(), "SELECT * FROM users")
}
Esempio n. 5
0
func TestUpdateWithoutAlias(t *testing.T) {
	connection := NewTestConnection(t)
	qb := db.NewQueryBuilder(connection)
	qb.Update("users").
		Set("foo", "?").
		Set("bar", "?")
	test.Fatal(t, qb.String(), "UPDATE users SET foo = ?, bar = ?")
}
Esempio n. 6
0
func TestSelectOrderBy(t *testing.T) {
	connection := NewTestConnection(t)
	qb := db.NewQueryBuilder(connection)
	qb.Select("u.*", "p.*").
		From("users", "u").
		OrderBy("u.name")
	test.Fatal(t, qb.String(), "SELECT u.*, p.* FROM users u ORDER BY u.name ASC")
}
Esempio n. 7
0
func TestUpdateWhere(t *testing.T) {
	connection := NewTestConnection(t)
	qb := db.NewQueryBuilder(connection)
	qb.Update("users", "u").
		Set("u.foo", "?").
		Where("u.foo = ?")
	test.Fatal(t, qb.String(), "UPDATE users u SET u.foo = ? WHERE u.foo = ?")
}
Esempio n. 8
0
func TestDeleteWhere(t *testing.T) {
	connection := NewTestConnection(t)

	qb := db.NewQueryBuilder(connection)
	qb.Delete("users", "u").
		Where("u.foo = ?")
	test.Fatal(t, "DELETE FROM users u WHERE u.foo = ?", qb.String())
}
Esempio n. 9
0
func TestBuilderSelectWithJoin(t *testing.T) {
	connection := NewTestConnection(t)
	qb := db.NewQueryBuilder(connection)
	qb.Select("u.*", "p.*").From("users", "u").
		Join("u", "phones", "p", Eq("p.user_id", "u.id"))
	test.Fatal(t, qb.String(), "SELECT u.*, p.* FROM users u JOIN phones p ON p.user_id = u.id")

}
Esempio n. 10
0
func TestBuilderSelectWithWhere(t *testing.T) {
	connection := NewTestConnection(t)
	qb := db.NewQueryBuilder(connection)
	qb.Select("u.id").
		From("users", "u").
		Where(And(Eq("u.nickname", "?")))
	test.Fatal(t, qb.String(), "SELECT u.id FROM users u WHERE u.nickname = ?")
}
Esempio n. 11
0
func TestSelectGroupBy(t *testing.T) {
	connection := NewTestConnection(t)
	qb := db.NewQueryBuilder(connection)
	qb.Select("u.*", "p.*").
		From("users", "u").
		GroupBy("u.id")
	test.Fatal(t, qb.String(), "SELECT u.*, p.* FROM users u GROUP BY u.id")
}
Esempio n. 12
0
func TestSelectWithOrOrWhereConditions(t *testing.T) {
	connection := NewTestConnection(t)
	qb := db.NewQueryBuilder(connection)
	qb.Select("u.*", "p.*").
		From("users", "u").
		OrWhere("u.username = ?").
		OrWhere("u.name = ?")
	test.Fatal(t, qb.String(), "SELECT u.*, p.* FROM users u WHERE (u.username = ?) OR (u.name = ?)")
}
Esempio n. 13
0
func TestSelectMultipleFrom(t *testing.T) {
	connection := NewTestConnection(t)
	qb := db.NewQueryBuilder(connection)
	qb.Select("u.*").
		AddSelect("p.*").
		From("users", "u").
		From("phonenumbers", "p")
	test.Fatal(t, qb.String(), "SELECT u.*, p.* FROM users u, phonenumbers p")
}
Esempio n. 14
0
func TestGetState(t *testing.T) {
	connection := NewTestConnection(t)
	qb := db.NewQueryBuilder(connection)
	test.Fatal(t, qb.GetState(), db.Clean)
	qb.Select("u.*").From("users", "u")
	test.Fatal(t, qb.GetState(), db.Dirty)
	qb.String()
	test.Fatal(t, qb.GetState(), db.Clean)
}
Esempio n. 15
0
func TestUpdate(t *testing.T) {
	connection := NewTestConnection(t)
	qb := db.NewQueryBuilder(connection)
	qb.Update("users", "u").
		Set("u.foo", "?").
		Set("u.bar", "?")
	test.Fatal(t, qb.GetType(), db.Update)
	test.Fatal(t, qb.String(), "UPDATE users u SET u.foo = ?, u.bar = ?")
}
Esempio n. 16
0
func TestSelectOrHavingOrHaving(t *testing.T) {
	connection := NewTestConnection(t)
	qb := db.NewQueryBuilder(connection)
	qb.Select("u.*", "p.*").
		From("users", "u").
		GroupBy("u.id").
		OrHaving("u.name = ?").
		OrHaving("u.username = ?")
	test.Fatal(t, qb.String(), "SELECT u.*, p.* FROM users u GROUP BY u.id HAVING (u.name = ?) OR (u.username = ?)")
}
Esempio n. 17
0
func TestInsertValuesSetValue(t *testing.T) {
	connection := NewTestConnection(t)

	qb := db.NewQueryBuilder(connection)
	qb.Insert("users").
		SetValue("foo", "?").
		SetValue("bar", "?")
	test.Fatal(t, db.Insert, qb.GetType())
	test.Fatal(t, "INSERT INTO users (foo, bar) VALUES(?, ?)", qb.String())
}
Esempio n. 18
0
func TestComplexSelectWithSomeTableAliases(t *testing.T) {
	// t.Skip("Skipped until the order of joins satisfies the doctrine specification")

	connection := NewTestConnection(t)
	qb := db.NewQueryBuilder(connection)
	qb.Select("u.id").
		From("users", "u").
		From("articles").
		InnerJoin("u", "permissions", "p", "p.user_id = u.id").
		InnerJoin("articles", "comments", "c", "c.article_id = articles.id")
	test.Fatal(t, qb.String(),
		"SELECT u.id FROM users u INNER JOIN permissions p ON p.user_id = u.id, articles INNER JOIN comments c ON c.article_id = articles.id")
}
Esempio n. 19
0
//         qb := db.NewQueryBuilder(connection)
//         qb.Select("id", "name")
//             .From("users")
//             .where("awesome=9001")
//         test.Fatal(t,qb.String(),"SELECT id, name FROM users WHERE awesome=9001")
//     }
func TestComplexSelectWithoutTableAliases(t *testing.T) {
	connection := NewTestConnection(t)
	qb := db.NewQueryBuilder(connection)
	qb.Select("DISTINCT users.id").
		From("users").
		From("articles").
		InnerJoin("articles", "comments", "c", "c.article_id = articles.id").
		InnerJoin("users", "permissions", "p", "p.user_id = users.id").
		Where("users.id = articles.user_id").
		AndWhere("p.read = 1")
	test.FatalWithDiff(t, qb.String(),
		"SELECT DISTINCT users.id FROM users INNER JOIN permissions p ON p.user_id"+
			" = users.id, articles INNER JOIN comments c ON c.article_id = articles.id "+
			"WHERE (users.id = articles.user_id) AND (p.read = 1)")
}
Esempio n. 20
0
/**
 * @group DBAL-172
*/
//      func TestReferenceJoinFromJoin (t *testing.T) {

//         qb := db.NewQueryBuilder(connection)
//         qb.Select("COUNT(DISTINCT news.id)")
//             .From("cb_newspages", "news")
//             .InnerJoin("news", "nodeversion", "nv", "nv.refId = news.id AND nv.refEntityname=\"News\'')
//             .InnerJoin("invalid", "nodetranslation", "nt", "nv.nodetranslation = nt.id")
//             .InnerJoin("nt", "node", "n", "nt.node = n.id")
//             .where("nt.lang = :lang AND n.deleted != 1")
//         this.setExpectedException("Doctrine\DBAL\Query\QueryException", "The given alias "invalid" is not part of any FROM or JOIN clause table. The currently registered aliases are: news, nv.")
//         test.Fatal(t,qb.String(),'', qb.getSQL())
//     }
//     /**
//      * @group DBAL-172
//      */
//      func TestSelectFromMasterWithWhereOnJoinedTables (t *testing.T) {

//         qb := db.NewQueryBuilder(connection)
//         qb.Select("COUNT(DISTINCT news.id)")
//             .From("newspages", "news")
//             .InnerJoin("news", "nodeversion", "nv", "nv.refId = news.id AND nv.refEntityname="Entity\\News"")
//             .InnerJoin("nv", "nodetranslation", "nt", "nv.nodetranslation = nt.id")
//             .InnerJoin("nt", "node", "n", "nt.node = n.id")
//             .where("nt.lang = ?")
//             .andWhere("n.deleted = 0")
//         test.Fatal(t,qb.String(),"SELECT COUNT(DISTINCT news.id) FROM newspages news INNER JOIN nodeversion nv ON nv.refId = news.id AND nv.refEntityname="Entity\\News" INNER JOIN nodetranslation nt ON nv.nodetranslation = nt.id INNER JOIN node n ON nt.node = n.id WHERE (nt.lang = ?) AND (n.deleted = 0)", qb.getSQL())
//     }
//     /**
//      * @group DBAL-442
//      */
//      func TestSelectWithMultipleFromAndJoins (t *testing.T) {

//         qb := db.NewQueryBuilder(connection)
//         qb.Select("DISTINCT u.id")
//             .From("users", "u")
//             .From("articles", "a")
//             .InnerJoin("u", "permissions", "p", "p.user_id = u.id")
//             .InnerJoin("a", "comments", "c", "c.article_id = a.id")
//             .where("u.id = a.user_id")
//             .andWhere("p.read = 1")
//         test.Fatal(t,qb.String(),"SELECT DISTINCT u.id FROM users u INNER JOIN permissions p ON p.user_id = u.id, articles a INNER JOIN comments c ON c.article_id = a.id WHERE (u.id = a.user_id) AND (p.read = 1)", qb.getSQL())
//     }
//     /**
//      * @group DBAL-774
//      */
func TestSelectWithJoinsWithMultipleOnConditionsParseOrder(t *testing.T) {
	t.Skip("join alias should also match alias in other joins if not found in from")
	connection := NewTestConnection(t)
	qb := db.NewQueryBuilder(connection)
	qb.Select("a.id").
		From("table_a", "a").
		Join("a", "table_b", "b", "a.fk_b = b.id").
		Join("b", "table_c", "c", "c.fk_b = b.id AND b.language = ?").
		Join("a", "table_d", "d", "a.fk_d = d.id").
		Join("c", "table_e", "e", "e.fk_c = c.id AND e.fk_d = d.id")
	test.FatalWithDiff(t, qb.String(),
		"SELECT a.id "+
			"FROM table_a a "+
			"INNER JOIN table_b b ON a.fk_b = b.id "+
			"INNER JOIN table_d d ON a.fk_d = d.id "+
			"INNER JOIN table_c c ON c.fk_b = b.id AND b.language = ? "+
			"INNER JOIN table_e e ON e.fk_c = c.id AND e.fk_d = d.id")
}
Esempio n. 21
0
func TestEmptyAddSelect(t *testing.T) {
	connection := NewTestConnection(t)

	qb := db.NewQueryBuilder(connection)
	test.Fatal(t, qb.GetType(), db.Select)
}
Esempio n. 22
0
func TestGetConnection(t *testing.T) {
	connection := NewTestConnection(t)

	qb := db.NewQueryBuilder(connection)
	test.Fatal(t, connection, qb.GetConnection())
}
Esempio n. 23
0
func TestEmptyUpdate(t *testing.T) {
	connection := NewTestConnection(t)
	qb := db.NewQueryBuilder(connection).Update("")
	test.Fatal(t, qb.GetType(), db.Update)
}