Skip to content

Commit

Permalink
test: fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
tr1v3r committed Aug 7, 2021
1 parent 83b88b8 commit a69dafa
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 22 deletions.
19 changes: 9 additions & 10 deletions do.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,13 @@ var (
return stmt
}

// withSELECT add SELECT clause
withSELECT stmtOpt = func(stmt *gorm.Statement) *gorm.Statement {
if _, ok := stmt.Clauses["SELECT"]; !ok {
stmt.AddClause(clause.Select{})
}
return stmt
}
// // withSELECT add SELECT clause
// withSELECT stmtOpt = func(stmt *gorm.Statement) *gorm.Statement {
// if _, ok := stmt.Clauses["SELECT"]; !ok {
// stmt.AddClause(clause.Select{Distinct: stmt.Distinct})
// }
// return stmt
// }
)

// build FOR TEST. call statement.Build to combine all clauses in one statement
Expand All @@ -110,8 +110,8 @@ func (d *DO) build(opts ...stmtOpt) *gorm.Statement {
stmt = opt(stmt)
}

if len(stmt.Selects) > 0 {
stmt = withSELECT(stmt)
if _, ok := stmt.Clauses["SELECT"]; !ok && len(stmt.Selects) > 0 {
stmt.AddClause(clause.Select{Distinct: stmt.Distinct, Expression: clause.Expr{SQL: strings.Join(stmt.Selects, ",")}})
}

findClauses := func() []string {
Expand Down Expand Up @@ -151,7 +151,6 @@ func (d *DO) Select(columns ...field.Expr) Dao {
if len(columns) == 0 {
return NewDO(d.db.Clauses(clause.Select{}))
}
// return NewDO(d.db.Clauses(clause.Select{Expression: clause.CommaExpression{Exprs: toExpression(columns...)}}))
return NewDO(d.db.Select(buildExpr(d.db.Statement, columns...)))
}

Expand Down
24 changes: 12 additions & 12 deletions do_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,27 +152,27 @@ func TestDO_methods(t *testing.T) {
},
{
Expr: u.Select(u.ID, u.Name),
Result: "SELECT `id`, `name`",
Result: "SELECT `id`,`name`",
},
{
Expr: u.Distinct(u.Name),
Result: "SELECT `name`",
Result: "SELECT DISTINCT `name`",
},
{
Expr: teacher.Distinct(teacher.ID, teacher.Name),
Result: "SELECT `teacher`.`id`, `teacher`.`name`",
Result: "SELECT DISTINCT `teacher`.`id`,`teacher`.`name`",
},
{
Expr: teacher.Select(teacher.ID, teacher.Name).Distinct(),
Result: "SELECT `teacher`.`id`, `teacher`.`name`",
Result: "SELECT DISTINCT `teacher`.`id`,`teacher`.`name`",
},
{
Expr: teacher.Select(teacher.Name.As("n")).Distinct(),
Result: "SELECT `teacher`.`name` AS `n`",
Result: "SELECT DISTINCT `teacher`.`name` AS `n`",
},
{
Expr: teacher.Select(teacher.ID.As("i"), teacher.Name.As("n")).Distinct(),
Result: "SELECT `teacher`.`id` AS `i`, `teacher`.`name` AS `n`",
Result: "SELECT DISTINCT `teacher`.`id` AS `i`,`teacher`.`name` AS `n`",
},
{
Expr: u.Where(u.ID.Eq(10)),
Expand Down Expand Up @@ -240,7 +240,7 @@ func TestDO_methods(t *testing.T) {
{
Expr: u.Select(u.ID, u.Name).Where(u.Age.Gt(18), u.Score.Gte(100)),
ExpectedVars: []interface{}{18, 100.0},
Result: "SELECT `id`, `name` WHERE `age` > ? AND `score` >= ?",
Result: "SELECT `id`,`name` WHERE `age` > ? AND `score` >= ?",
},
// ======================== subquery ========================
{
Expand All @@ -256,7 +256,7 @@ func TestDO_methods(t *testing.T) {
{
Expr: u.Select(u.ID, u.Name).Where(Lte(u.Score, u.Select(u.Score.Avg()).Where(u.Age.Gte(18)))),
ExpectedVars: []interface{}{18},
Result: "SELECT `id`, `name` WHERE `score` <= (SELECT AVG(`score`) FROM `users_info` WHERE `age` >= ?)",
Result: "SELECT `id`,`name` WHERE `score` <= (SELECT AVG(`score`) FROM `users_info` WHERE `age` >= ?)",
},
{
Expr: u.Select(u.ID).Where(In(u.Score, u.Select(u.Score).Where(u.Age.Gte(18)))),
Expand All @@ -266,7 +266,7 @@ func TestDO_methods(t *testing.T) {
{
Expr: u.Select(u.ID).Where(In(u.ID, u.Age, u.Select(u.ID, u.Age).Where(u.Score.Eq(100)))),
ExpectedVars: []interface{}{100.0},
Result: "SELECT `id` WHERE (`id`, `age`) IN (SELECT `id`, `age` FROM `users_info` WHERE `score` = ?)",
Result: "SELECT `id` WHERE (`id`, `age`) IN (SELECT `id`,`age` FROM `users_info` WHERE `score` = ?)",
},
{
Expr: u.Select(u.Age.Avg().As("avgage")).Group(u.Name).Having(Gt(u.Age.Avg(), u.Select(u.Age.Avg()).Where(u.Name.Like("name%")))),
Expand All @@ -279,7 +279,7 @@ func TestDO_methods(t *testing.T) {
Expr: Table(u.Select(u.ID, u.Name).Where(u.Age.Gt(18))).Select(),
Opts: []stmtOpt{withFROM},
ExpectedVars: []interface{}{18},
Result: "SELECT * FROM (SELECT `id`, `name` FROM `users_info` WHERE `age` > ?)",
Result: "SELECT * FROM (SELECT `id`,`name` FROM `users_info` WHERE `age` > ?)",
},
{
Expr: Table(u.Select(u.ID).Where(u.Age.Gt(18)), u.Select(u.ID).Where(u.Score.Gte(100))).Select(),
Expand Down Expand Up @@ -307,7 +307,7 @@ func TestDO_methods(t *testing.T) {
},
{
Expr: student.LeftJoin(teacher, student.Instructor.EqCol(teacher.ID)).Where(teacher.ID.Gt(0)).Select(student.Name, teacher.Name),
Result: "SELECT `student`.`name`, `teacher`.`name` FROM `student` LEFT JOIN `teacher` ON `student`.`instructor` = `teacher`.`id` WHERE `teacher`.`id` > ?",
Result: "SELECT `student`.`name`,`teacher`.`name` FROM `student` LEFT JOIN `teacher` ON `student`.`instructor` = `teacher`.`id` WHERE `teacher`.`id` > ?",
ExpectedVars: []interface{}{int64(0)},
},
{
Expand All @@ -317,7 +317,7 @@ func TestDO_methods(t *testing.T) {
},
{
Expr: student.Join(teacher, student.Instructor.EqCol(teacher.ID)).LeftJoin(teacher, student.ID.Gt(100)).Select(student.ID, student.Name, teacher.Name.As("teacher_name")),
Result: "SELECT `student`.`id`, `student`.`name`, `teacher`.`name` AS `teacher_name` FROM `student` INNER JOIN `teacher` ON `student`.`instructor` = `teacher`.`id` LEFT JOIN `teacher` ON `student`.`id` > ?",
Result: "SELECT `student`.`id`,`student`.`name`,`teacher`.`name` AS `teacher_name` FROM `student` INNER JOIN `teacher` ON `student`.`instructor` = `teacher`.`id` LEFT JOIN `teacher` ON `student`.`id` > ?",
ExpectedVars: []interface{}{int64(100)},
},
}
Expand Down

0 comments on commit a69dafa

Please sign in to comment.