github.com/astaxie/beego@v1.12.3/orm/db_oracle.go (about)

     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  
    15  package orm
    16  
    17  import (
    18  	"fmt"
    19  	"strings"
    20  )
    21  
    22  // oracle operators.
    23  var oracleOperators = map[string]string{
    24  	"exact":       "= ?",
    25  	"gt":          "> ?",
    26  	"gte":         ">= ?",
    27  	"lt":          "< ?",
    28  	"lte":         "<= ?",
    29  	"//iendswith": "LIKE ?",
    30  }
    31  
    32  // oracle column field types.
    33  var oracleTypes = map[string]string{
    34  	"pk":              "NOT NULL PRIMARY KEY",
    35  	"bool":            "bool",
    36  	"string":          "VARCHAR2(%d)",
    37  	"string-char":     "CHAR(%d)",
    38  	"string-text":     "VARCHAR2(%d)",
    39  	"time.Time-date":  "DATE",
    40  	"time.Time":       "TIMESTAMP",
    41  	"int8":            "INTEGER",
    42  	"int16":           "INTEGER",
    43  	"int32":           "INTEGER",
    44  	"int64":           "INTEGER",
    45  	"uint8":           "INTEGER",
    46  	"uint16":          "INTEGER",
    47  	"uint32":          "INTEGER",
    48  	"uint64":          "INTEGER",
    49  	"float64":         "NUMBER",
    50  	"float64-decimal": "NUMBER(%d, %d)",
    51  }
    52  
    53  // oracle dbBaser
    54  type dbBaseOracle struct {
    55  	dbBase
    56  }
    57  
    58  var _ dbBaser = new(dbBaseOracle)
    59  
    60  // create oracle dbBaser.
    61  func newdbBaseOracle() dbBaser {
    62  	b := new(dbBaseOracle)
    63  	b.ins = b
    64  	return b
    65  }
    66  
    67  // OperatorSQL get oracle operator.
    68  func (d *dbBaseOracle) OperatorSQL(operator string) string {
    69  	return oracleOperators[operator]
    70  }
    71  
    72  // DbTypes get oracle table field types.
    73  func (d *dbBaseOracle) DbTypes() map[string]string {
    74  	return oracleTypes
    75  }
    76  
    77  //ShowTablesQuery show all the tables in database
    78  func (d *dbBaseOracle) ShowTablesQuery() string {
    79  	return "SELECT TABLE_NAME FROM USER_TABLES"
    80  }
    81  
    82  // Oracle
    83  func (d *dbBaseOracle) ShowColumnsQuery(table string) string {
    84  	return fmt.Sprintf("SELECT COLUMN_NAME FROM ALL_TAB_COLUMNS "+
    85  		"WHERE TABLE_NAME ='%s'", strings.ToUpper(table))
    86  }
    87  
    88  // check index is exist
    89  func (d *dbBaseOracle) IndexExists(db dbQuerier, table string, name string) bool {
    90  	row := db.QueryRow("SELECT COUNT(*) FROM USER_IND_COLUMNS, USER_INDEXES "+
    91  		"WHERE USER_IND_COLUMNS.INDEX_NAME = USER_INDEXES.INDEX_NAME "+
    92  		"AND  USER_IND_COLUMNS.TABLE_NAME = ? AND USER_IND_COLUMNS.INDEX_NAME = ?", strings.ToUpper(table), strings.ToUpper(name))
    93  
    94  	var cnt int
    95  	row.Scan(&cnt)
    96  	return cnt > 0
    97  }
    98  
    99  // execute insert sql with given struct and given values.
   100  // insert the given values, not the field values in struct.
   101  func (d *dbBaseOracle) InsertValue(q dbQuerier, mi *modelInfo, isMulti bool, names []string, values []interface{}) (int64, error) {
   102  	Q := d.ins.TableQuote()
   103  
   104  	marks := make([]string, len(names))
   105  	for i := range marks {
   106  		marks[i] = ":" + names[i]
   107  	}
   108  
   109  	sep := fmt.Sprintf("%s, %s", Q, Q)
   110  	qmarks := strings.Join(marks, ", ")
   111  	columns := strings.Join(names, sep)
   112  
   113  	multi := len(values) / len(names)
   114  
   115  	if isMulti {
   116  		qmarks = strings.Repeat(qmarks+"), (", multi-1) + qmarks
   117  	}
   118  
   119  	query := fmt.Sprintf("INSERT INTO %s%s%s (%s%s%s) VALUES (%s)", Q, mi.table, Q, Q, columns, Q, qmarks)
   120  
   121  	d.ins.ReplaceMarks(&query)
   122  
   123  	if isMulti || !d.ins.HasReturningID(mi, &query) {
   124  		res, err := q.Exec(query, values...)
   125  		if err == nil {
   126  			if isMulti {
   127  				return res.RowsAffected()
   128  			}
   129  			return res.LastInsertId()
   130  		}
   131  		return 0, err
   132  	}
   133  	row := q.QueryRow(query, values...)
   134  	var id int64
   135  	err := row.Scan(&id)
   136  	return id, err
   137  }