THIS IS A TEST INSTANCE ONLY! REPOSITORIES CAN BE DELETED AT ANY TIME!

Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

97 Zeilen
2.5KB

  1. // Copyright 2017 The Xorm Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package xorm
  5. import (
  6. "errors"
  7. "fmt"
  8. "reflect"
  9. "xorm.io/builder"
  10. "xorm.io/core"
  11. )
  12. // Exist returns true if the record exist otherwise return false
  13. func (session *Session) Exist(bean ...interface{}) (bool, error) {
  14. if session.isAutoClose {
  15. defer session.Close()
  16. }
  17. if session.statement.lastError != nil {
  18. return false, session.statement.lastError
  19. }
  20. var sqlStr string
  21. var args []interface{}
  22. var err error
  23. if session.statement.RawSQL == "" {
  24. if len(bean) == 0 {
  25. tableName := session.statement.TableName()
  26. if len(tableName) <= 0 {
  27. return false, ErrTableNotFound
  28. }
  29. tableName = session.statement.Engine.Quote(tableName)
  30. if session.statement.cond.IsValid() {
  31. condSQL, condArgs, err := builder.ToSQL(session.statement.cond)
  32. if err != nil {
  33. return false, err
  34. }
  35. if session.engine.dialect.DBType() == core.MSSQL {
  36. sqlStr = fmt.Sprintf("SELECT TOP 1 * FROM %s WHERE %s", tableName, condSQL)
  37. } else if session.engine.dialect.DBType() == core.ORACLE {
  38. sqlStr = fmt.Sprintf("SELECT * FROM %s WHERE (%s) AND ROWNUM=1", tableName, condSQL)
  39. } else {
  40. sqlStr = fmt.Sprintf("SELECT * FROM %s WHERE %s LIMIT 1", tableName, condSQL)
  41. }
  42. args = condArgs
  43. } else {
  44. if session.engine.dialect.DBType() == core.MSSQL {
  45. sqlStr = fmt.Sprintf("SELECT TOP 1 * FROM %s", tableName)
  46. } else if session.engine.dialect.DBType() == core.ORACLE {
  47. sqlStr = fmt.Sprintf("SELECT * FROM %s WHERE ROWNUM=1", tableName)
  48. } else {
  49. sqlStr = fmt.Sprintf("SELECT * FROM %s LIMIT 1", tableName)
  50. }
  51. args = []interface{}{}
  52. }
  53. } else {
  54. beanValue := reflect.ValueOf(bean[0])
  55. if beanValue.Kind() != reflect.Ptr {
  56. return false, errors.New("needs a pointer")
  57. }
  58. if beanValue.Elem().Kind() == reflect.Struct {
  59. if err := session.statement.setRefBean(bean[0]); err != nil {
  60. return false, err
  61. }
  62. }
  63. if len(session.statement.TableName()) <= 0 {
  64. return false, ErrTableNotFound
  65. }
  66. session.statement.Limit(1)
  67. sqlStr, args, err = session.statement.genGetSQL(bean[0])
  68. if err != nil {
  69. return false, err
  70. }
  71. }
  72. } else {
  73. sqlStr = session.statement.RawSQL
  74. args = session.statement.RawParams
  75. }
  76. rows, err := session.queryRows(sqlStr, args...)
  77. if err != nil {
  78. return false, err
  79. }
  80. defer rows.Close()
  81. return rows.Next(), nil
  82. }