2017-08-22 17:09:52 +05:30
|
|
|
// Copyright 2017 The Xorm Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package xorm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
|
2019-06-23 20:52:43 +05:30
|
|
|
"xorm.io/builder"
|
|
|
|
"xorm.io/core"
|
2017-08-22 17:09:52 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
// Exist returns true if the record exist otherwise return false
|
|
|
|
func (session *Session) Exist(bean ...interface{}) (bool, error) {
|
|
|
|
if session.isAutoClose {
|
|
|
|
defer session.Close()
|
|
|
|
}
|
|
|
|
|
2019-06-23 20:52:43 +05:30
|
|
|
if session.statement.lastError != nil {
|
|
|
|
return false, session.statement.lastError
|
|
|
|
}
|
|
|
|
|
2017-08-22 17:09:52 +05:30
|
|
|
var sqlStr string
|
|
|
|
var args []interface{}
|
|
|
|
var err error
|
|
|
|
|
|
|
|
if session.statement.RawSQL == "" {
|
|
|
|
if len(bean) == 0 {
|
|
|
|
tableName := session.statement.TableName()
|
|
|
|
if len(tableName) <= 0 {
|
|
|
|
return false, ErrTableNotFound
|
|
|
|
}
|
|
|
|
|
2019-06-23 20:52:43 +05:30
|
|
|
tableName = session.statement.Engine.Quote(tableName)
|
|
|
|
|
2017-08-22 17:09:52 +05:30
|
|
|
if session.statement.cond.IsValid() {
|
|
|
|
condSQL, condArgs, err := builder.ToSQL(session.statement.cond)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2018-01-27 20:50:59 +05:30
|
|
|
if session.engine.dialect.DBType() == core.MSSQL {
|
2019-06-23 20:52:43 +05:30
|
|
|
sqlStr = fmt.Sprintf("SELECT TOP 1 * FROM %s WHERE %s", tableName, condSQL)
|
|
|
|
} else if session.engine.dialect.DBType() == core.ORACLE {
|
|
|
|
sqlStr = fmt.Sprintf("SELECT * FROM %s WHERE (%s) AND ROWNUM=1", tableName, condSQL)
|
2018-01-27 20:50:59 +05:30
|
|
|
} else {
|
|
|
|
sqlStr = fmt.Sprintf("SELECT * FROM %s WHERE %s LIMIT 1", tableName, condSQL)
|
|
|
|
}
|
2017-08-22 17:09:52 +05:30
|
|
|
args = condArgs
|
|
|
|
} else {
|
2018-01-27 20:50:59 +05:30
|
|
|
if session.engine.dialect.DBType() == core.MSSQL {
|
2019-06-23 20:52:43 +05:30
|
|
|
sqlStr = fmt.Sprintf("SELECT TOP 1 * FROM %s", tableName)
|
|
|
|
} else if session.engine.dialect.DBType() == core.ORACLE {
|
|
|
|
sqlStr = fmt.Sprintf("SELECT * FROM %s WHERE ROWNUM=1", tableName)
|
2018-01-27 20:50:59 +05:30
|
|
|
} else {
|
|
|
|
sqlStr = fmt.Sprintf("SELECT * FROM %s LIMIT 1", tableName)
|
|
|
|
}
|
2017-08-22 17:09:52 +05:30
|
|
|
args = []interface{}{}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
beanValue := reflect.ValueOf(bean[0])
|
|
|
|
if beanValue.Kind() != reflect.Ptr {
|
|
|
|
return false, errors.New("needs a pointer")
|
|
|
|
}
|
|
|
|
|
|
|
|
if beanValue.Elem().Kind() == reflect.Struct {
|
2018-07-20 07:40:17 +05:30
|
|
|
if err := session.statement.setRefBean(bean[0]); err != nil {
|
2017-08-22 17:09:52 +05:30
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(session.statement.TableName()) <= 0 {
|
|
|
|
return false, ErrTableNotFound
|
|
|
|
}
|
|
|
|
session.statement.Limit(1)
|
|
|
|
sqlStr, args, err = session.statement.genGetSQL(bean[0])
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
sqlStr = session.statement.RawSQL
|
|
|
|
args = session.statement.RawParams
|
|
|
|
}
|
|
|
|
|
2017-09-13 10:48:22 +05:30
|
|
|
rows, err := session.queryRows(sqlStr, args...)
|
2017-08-22 17:09:52 +05:30
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2017-09-13 10:48:22 +05:30
|
|
|
defer rows.Close()
|
2017-08-22 17:09:52 +05:30
|
|
|
|
2017-09-13 10:48:22 +05:30
|
|
|
return rows.Next(), nil
|
2017-08-22 17:09:52 +05:30
|
|
|
}
|