github.com/RevenueMonster/sqlike@v1.0.6/sql/expr/extra.go (about)

     1  package expr
     2  
     3  import "github.com/RevenueMonster/sqlike/sqlike/primitive"
     4  
     5  type selectStmt interface {
     6  	// Where(fields ...interface{}) selectStmt
     7  }
     8  
     9  // Union :
    10  func Union(stmt1 selectStmt, stmt2 selectStmt, others ...selectStmt) (grp primitive.Group) {
    11  	grp = union(Raw(" UNION "), append([]selectStmt{stmt1, stmt2}, others...))
    12  	return
    13  }
    14  
    15  // // UnionAll :
    16  // func UnionAll(stmt1 *sql.SelectStmt, stmt2 *sql.SelectStmt, others ...*sql.SelectStmt) (grp primitive.Group) {
    17  // 	grp = union(Raw("UNION ALL"), append([]*sql.SelectStmt{stmt1, stmt2}, others...))
    18  // 	return
    19  // }
    20  
    21  // Exists :
    22  func Exists(subquery interface{}) (grp primitive.Group) {
    23  	grp.Values = append(grp.Values, Raw("EXISTS ("))
    24  	grp.Values = append(grp.Values, subquery)
    25  	grp.Values = append(grp.Values, Raw(")"))
    26  	return
    27  }
    28  
    29  // NotExists :
    30  func NotExists(subquery interface{}) (grp primitive.Group) {
    31  	grp.Values = append(grp.Values, Raw("NOT EXISTS ("))
    32  	grp.Values = append(grp.Values, subquery)
    33  	grp.Values = append(grp.Values, Raw(")"))
    34  	return
    35  }
    36  
    37  // Case :
    38  func Case() *primitive.Case {
    39  	return new(primitive.Case)
    40  }
    41  
    42  func union(link primitive.Raw, stmts []selectStmt) (grp primitive.Group) {
    43  	for i, stmt := range stmts {
    44  		if i > 0 {
    45  			grp.Values = append(grp.Values, link)
    46  		}
    47  		grp.Values = append(grp.Values, Raw("("))
    48  		grp.Values = append(grp.Values, stmt)
    49  		grp.Values = append(grp.Values, Raw(")"))
    50  	}
    51  	return
    52  }