refactor query
This commit is contained in:
parent
a0f42c421a
commit
e2f9dffe4a
|
@ -382,10 +382,7 @@ func (statement *Statement) GenExistSQL(bean ...interface{}) (string, []interfac
|
||||||
return statement.GenRawSQL(), statement.RawParams, nil
|
return statement.GenRawSQL(), statement.RawParams, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
var sqlStr string
|
|
||||||
var args []interface{}
|
|
||||||
var joinStr string
|
var joinStr string
|
||||||
var err error
|
|
||||||
var b interface{}
|
var b interface{}
|
||||||
if len(bean) > 0 {
|
if len(bean) > 0 {
|
||||||
b = bean[0]
|
b = bean[0]
|
||||||
|
@ -404,45 +401,61 @@ func (statement *Statement) GenExistSQL(bean ...interface{}) (string, []interfac
|
||||||
if len(tableName) <= 0 {
|
if len(tableName) <= 0 {
|
||||||
return "", nil, ErrTableNotFound
|
return "", nil, ErrTableNotFound
|
||||||
}
|
}
|
||||||
if statement.RefTable == nil {
|
if statement.RefTable != nil {
|
||||||
|
return statement.Limit(1).GenGetSQL(b)
|
||||||
|
}
|
||||||
|
|
||||||
tableName = statement.quote(tableName)
|
tableName = statement.quote(tableName)
|
||||||
if len(statement.JoinStr) > 0 {
|
if len(statement.JoinStr) > 0 {
|
||||||
joinStr = statement.JoinStr
|
joinStr = " " + statement.JoinStr + " "
|
||||||
}
|
}
|
||||||
|
|
||||||
|
buf := builder.NewWriter()
|
||||||
|
if statement.dialect.URI().DBType == schemas.MSSQL {
|
||||||
|
if _, err := fmt.Fprintf(buf, "SELECT TOP 1 * FROM %s%s", tableName, joinStr); err != nil {
|
||||||
|
return "", nil, err
|
||||||
|
}
|
||||||
if statement.Conds().IsValid() {
|
if statement.Conds().IsValid() {
|
||||||
condSQL, condArgs, err := statement.GenCondSQL(statement.Conds())
|
if _, err := fmt.Fprintf(buf, " WHERE "); err != nil {
|
||||||
if err != nil {
|
|
||||||
return "", nil, err
|
return "", nil, err
|
||||||
}
|
}
|
||||||
|
if err := statement.Conds().WriteTo(buf); err != nil {
|
||||||
if statement.dialect.URI().DBType == schemas.MSSQL {
|
return "", nil, err
|
||||||
sqlStr = fmt.Sprintf("SELECT TOP 1 * FROM %s %s WHERE %s", tableName, joinStr, condSQL)
|
}
|
||||||
|
}
|
||||||
} else if statement.dialect.URI().DBType == schemas.ORACLE {
|
} else if statement.dialect.URI().DBType == schemas.ORACLE {
|
||||||
sqlStr = fmt.Sprintf("SELECT * FROM %s WHERE (%s) %s AND ROWNUM=1", tableName, joinStr, condSQL)
|
if _, err := fmt.Fprintf(buf, "SELECT * FROM %s%s WHERE ", tableName, joinStr); err != nil {
|
||||||
} else {
|
return "", nil, err
|
||||||
sqlStr = fmt.Sprintf("SELECT 1 FROM %s %s WHERE %s LIMIT 1", tableName, joinStr, condSQL)
|
|
||||||
}
|
}
|
||||||
args = condArgs
|
if statement.Conds().IsValid() {
|
||||||
} else {
|
if err := statement.Conds().WriteTo(buf); err != nil {
|
||||||
if statement.dialect.URI().DBType == schemas.MSSQL {
|
return "", nil, err
|
||||||
sqlStr = fmt.Sprintf("SELECT TOP 1 * FROM %s %s", tableName, joinStr)
|
|
||||||
} else if statement.dialect.URI().DBType == schemas.ORACLE {
|
|
||||||
sqlStr = fmt.Sprintf("SELECT * FROM %s %s WHERE ROWNUM=1", tableName, joinStr)
|
|
||||||
} else {
|
|
||||||
sqlStr = fmt.Sprintf("SELECT 1 FROM %s %s LIMIT 1", tableName, joinStr)
|
|
||||||
}
|
}
|
||||||
args = []interface{}{}
|
if _, err := fmt.Fprintf(buf, " AND "); err != nil {
|
||||||
|
return "", nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if _, err := fmt.Fprintf(buf, "ROWNUM=1"); err != nil {
|
||||||
|
return "", nil, err
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
statement.Limit(1)
|
if _, err := fmt.Fprintf(buf, "SELECT 1 FROM %s%s", tableName, joinStr); err != nil {
|
||||||
sqlStr, args, err = statement.GenGetSQL(b)
|
return "", nil, err
|
||||||
if err != nil {
|
}
|
||||||
|
if statement.Conds().IsValid() {
|
||||||
|
if _, err := fmt.Fprintf(buf, " WHERE "); err != nil {
|
||||||
|
return "", nil, err
|
||||||
|
}
|
||||||
|
if err := statement.Conds().WriteTo(buf); err != nil {
|
||||||
|
return "", nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if _, err := fmt.Fprintf(buf, "LIMIT 1"); err != nil {
|
||||||
return "", nil, err
|
return "", nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return sqlStr, args, nil
|
return buf.String(), buf.Args(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GenFindSQL generates Find SQL
|
// GenFindSQL generates Find SQL
|
||||||
|
|
Loading…
Reference in New Issue