db_oracle.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Copyright 2014 beego Author. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package orm
  15. import (
  16. "fmt"
  17. "strings"
  18. )
  19. // oracle operators.
  20. var oracleOperators = map[string]string{
  21. "exact": "= ?",
  22. "gt": "> ?",
  23. "gte": ">= ?",
  24. "lt": "< ?",
  25. "lte": "<= ?",
  26. "//iendswith": "LIKE ?",
  27. }
  28. // oracle column field types.
  29. var oracleTypes = map[string]string{
  30. "pk": "NOT NULL PRIMARY KEY",
  31. "bool": "bool",
  32. "string": "VARCHAR2(%d)",
  33. "string-text": "VARCHAR2(%d)",
  34. "time.Time-date": "DATE",
  35. "time.Time": "TIMESTAMP",
  36. "int8": "INTEGER",
  37. "int16": "INTEGER",
  38. "int32": "INTEGER",
  39. "int64": "INTEGER",
  40. "uint8": "INTEGER",
  41. "uint16": "INTEGER",
  42. "uint32": "INTEGER",
  43. "uint64": "INTEGER",
  44. "float64": "NUMBER",
  45. "float64-decimal": "NUMBER(%d, %d)",
  46. }
  47. // oracle dbBaser
  48. type dbBaseOracle struct {
  49. dbBase
  50. }
  51. var _ dbBaser = new(dbBaseOracle)
  52. // create oracle dbBaser.
  53. func newdbBaseOracle() dbBaser {
  54. b := new(dbBaseOracle)
  55. b.ins = b
  56. return b
  57. }
  58. // OperatorSQL get oracle operator.
  59. func (d *dbBaseOracle) OperatorSQL(operator string) string {
  60. return oracleOperators[operator]
  61. }
  62. // DbTypes get oracle table field types.
  63. func (d *dbBaseOracle) DbTypes() map[string]string {
  64. return oracleTypes
  65. }
  66. //ShowTablesQuery show all the tables in database
  67. func (d *dbBaseOracle) ShowTablesQuery() string {
  68. return "SELECT TABLE_NAME FROM USER_TABLES"
  69. }
  70. // Oracle
  71. func (d *dbBaseOracle) ShowColumnsQuery(table string) string {
  72. return fmt.Sprintf("SELECT COLUMN_NAME FROM ALL_TAB_COLUMNS "+
  73. "WHERE TABLE_NAME ='%s'", strings.ToUpper(table))
  74. }
  75. // check index is exist
  76. func (d *dbBaseOracle) IndexExists(db dbQuerier, table string, name string) bool {
  77. row := db.QueryRow("SELECT COUNT(*) FROM USER_IND_COLUMNS, USER_INDEXES "+
  78. "WHERE USER_IND_COLUMNS.INDEX_NAME = USER_INDEXES.INDEX_NAME "+
  79. "AND USER_IND_COLUMNS.TABLE_NAME = ? AND USER_IND_COLUMNS.INDEX_NAME = ?", strings.ToUpper(table), strings.ToUpper(name))
  80. var cnt int
  81. row.Scan(&cnt)
  82. return cnt > 0
  83. }