Exemplo n.º 1
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)")
}
Exemplo n.º 2
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")
}