xorm/oracle_dialect.go

245 lines
5.9 KiB
Go
Raw Normal View History

package xorm
2014-01-07 09:19:33 +00:00
import (
"errors"
"fmt"
"strconv"
"strings"
"github.com/go-xorm/core"
2014-01-07 09:33:27 +00:00
)
2014-01-07 09:19:33 +00:00
// func init() {
// RegisterDialect("oracle", &oracle{})
// }
2014-01-07 09:19:33 +00:00
2014-01-07 09:33:27 +00:00
type oracle struct {
core.Base
2014-01-07 09:19:33 +00:00
}
2014-04-18 10:39:07 +00:00
func (db *oracle) Init(d *core.DB, uri *core.Uri, drivername, dataSourceName string) error {
return db.Base.Init(d, db, uri, drivername, dataSourceName)
2014-01-07 09:19:33 +00:00
}
func (db *oracle) SqlType(c *core.Column) string {
2014-01-07 09:19:33 +00:00
var res string
switch t := c.SQLType.Name; t {
case core.Bit, core.TinyInt, core.SmallInt, core.MediumInt, core.Int, core.Integer, core.BigInt, core.Bool, core.Serial, core.BigSerial:
2014-01-07 09:19:33 +00:00
return "NUMBER"
case core.Binary, core.VarBinary, core.Blob, core.TinyBlob, core.MediumBlob, core.LongBlob, core.Bytea:
return core.Blob
case core.Time, core.DateTime, core.TimeStamp:
res = core.TimeStamp
case core.TimeStampz:
2014-01-07 09:19:33 +00:00
res = "TIMESTAMP WITH TIME ZONE"
case core.Float, core.Double, core.Numeric, core.Decimal:
2014-01-07 09:19:33 +00:00
res = "NUMBER"
case core.Text, core.MediumText, core.LongText:
2014-01-07 09:19:33 +00:00
res = "CLOB"
case core.Char, core.Varchar, core.TinyText:
2014-01-07 09:19:33 +00:00
return "VARCHAR2"
default:
res = t
}
var hasLen1 bool = (c.Length > 0)
var hasLen2 bool = (c.Length2 > 0)
2014-05-29 08:53:23 +00:00
if hasLen2 {
2014-01-07 09:19:33 +00:00
res += "(" + strconv.Itoa(c.Length) + "," + strconv.Itoa(c.Length2) + ")"
2014-05-29 08:53:23 +00:00
} else if hasLen1 {
res += "(" + strconv.Itoa(c.Length) + ")"
2014-01-07 09:19:33 +00:00
}
return res
}
func (db *oracle) SupportInsertMany() bool {
return true
}
func (db *oracle) QuoteStr() string {
return "\""
}
func (db *oracle) AutoIncrStr() string {
return ""
}
func (db *oracle) SupportEngine() bool {
return false
}
func (db *oracle) SupportCharset() bool {
return false
}
func (db *oracle) IndexOnTable() bool {
return false
}
func (db *oracle) IndexCheckSql(tableName, idxName string) (string, []interface{}) {
args := []interface{}{strings.ToUpper(tableName), strings.ToUpper(idxName)}
return `SELECT INDEX_NAME FROM USER_INDEXES ` +
`WHERE TABLE_NAME = ? AND INDEX_NAME = ?`, args
}
func (db *oracle) TableCheckSql(tableName string) (string, []interface{}) {
args := []interface{}{strings.ToUpper(tableName)}
return `SELECT table_name FROM user_tables WHERE table_name = ?`, args
}
2014-04-23 06:01:04 +00:00
/*func (db *oracle) ColumnCheckSql(tableName, colName string) (string, []interface{}) {
2014-01-07 09:19:33 +00:00
args := []interface{}{strings.ToUpper(tableName), strings.ToUpper(colName)}
return "SELECT column_name FROM USER_TAB_COLUMNS WHERE table_name = ?" +
" AND column_name = ?", args
2014-04-23 06:01:04 +00:00
}*/
func (db *oracle) IsColumnExist(tableName string, col *core.Column) (bool, error) {
args := []interface{}{strings.ToUpper(tableName), strings.ToUpper(col.Name)}
query := "SELECT column_name FROM USER_TAB_COLUMNS WHERE table_name = ?" +
" AND column_name = ?"
rows, err := db.DB().Query(query, args...)
if err != nil {
return false, err
}
defer rows.Close()
if rows.Next() {
return true, nil
}
2014-05-14 12:26:42 +00:00
return false, nil
2014-01-07 09:19:33 +00:00
}
func (db *oracle) GetColumns(tableName string) ([]string, map[string]*core.Column, error) {
2014-01-07 09:19:33 +00:00
args := []interface{}{strings.ToUpper(tableName)}
s := "SELECT column_name,data_default,data_type,data_length,data_precision,data_scale," +
"nullable FROM USER_TAB_COLUMNS WHERE table_name = :1"
2014-04-18 10:39:07 +00:00
rows, err := db.DB().Query(s, args...)
2014-01-07 09:19:33 +00:00
if err != nil {
return nil, nil, err
}
2014-01-07 09:33:27 +00:00
defer rows.Close()
cols := make(map[string]*core.Column)
2014-01-07 09:19:33 +00:00
colSeq := make([]string, 0)
2014-01-07 09:33:27 +00:00
for rows.Next() {
col := new(core.Column)
2014-01-07 09:19:33 +00:00
col.Indexes = make(map[string]bool)
2014-01-07 09:33:27 +00:00
var colName, colDefault, nullable, dataType, dataPrecision, dataScale string
var dataLen int
err = rows.Scan(&colName, &colDefault, &dataType, &dataLen, &dataPrecision,
&dataScale, &nullable)
if err != nil {
return nil, nil, err
}
col.Name = strings.Trim(colName, `" `)
col.Default = colDefault
if nullable == "Y" {
col.Nullable = true
} else {
col.Nullable = false
}
switch dataType {
case "VARCHAR2":
col.SQLType = core.SQLType{core.Varchar, 0, 0}
2014-01-07 09:33:27 +00:00
case "TIMESTAMP WITH TIME ZONE":
col.SQLType = core.SQLType{core.TimeStampz, 0, 0}
2014-01-07 09:33:27 +00:00
default:
col.SQLType = core.SQLType{strings.ToUpper(dataType), 0, 0}
2014-01-07 09:19:33 +00:00
}
if _, ok := core.SqlTypes[col.SQLType.Name]; !ok {
2014-01-07 09:33:27 +00:00
return nil, nil, errors.New(fmt.Sprintf("unkonw colType %v", dataType))
}
col.Length = dataLen
2014-01-07 09:19:33 +00:00
if col.SQLType.IsText() {
if col.Default != "" {
col.Default = "'" + col.Default + "'"
} else {
if col.DefaultIsEmpty {
col.Default = "''"
}
2014-01-07 09:19:33 +00:00
}
}
cols[col.Name] = col
colSeq = append(colSeq, col.Name)
}
return colSeq, cols, nil
}
func (db *oracle) GetTables() ([]*core.Table, error) {
2014-01-07 09:19:33 +00:00
args := []interface{}{}
s := "SELECT table_name FROM user_tables"
2014-04-18 10:39:07 +00:00
rows, err := db.DB().Query(s, args...)
2014-01-07 09:19:33 +00:00
if err != nil {
return nil, err
}
2014-04-18 10:39:07 +00:00
defer rows.Close()
2014-01-07 09:19:33 +00:00
tables := make([]*core.Table, 0)
2014-01-07 09:33:27 +00:00
for rows.Next() {
table := core.NewEmptyTable()
2014-01-07 09:33:27 +00:00
err = rows.Scan(&table.Name)
if err != nil {
return nil, err
2014-01-07 09:19:33 +00:00
}
2014-01-07 09:33:27 +00:00
2014-01-07 09:19:33 +00:00
tables = append(tables, table)
}
return tables, nil
}
func (db *oracle) GetIndexes(tableName string) (map[string]*core.Index, error) {
2014-01-07 09:19:33 +00:00
args := []interface{}{tableName}
2014-01-07 09:33:27 +00:00
s := "SELECT t.column_name,i.uniqueness,i.index_name FROM user_ind_columns t,user_indexes i " +
2014-01-07 09:19:33 +00:00
"WHERE t.index_name = i.index_name and t.table_name = i.table_name and t.table_name =:1"
2014-04-18 10:39:07 +00:00
rows, err := db.DB().Query(s, args...)
2014-01-07 09:19:33 +00:00
if err != nil {
return nil, err
}
2014-01-07 09:33:27 +00:00
defer rows.Close()
2014-01-07 09:19:33 +00:00
indexes := make(map[string]*core.Index, 0)
2014-01-07 09:33:27 +00:00
for rows.Next() {
2014-01-07 09:19:33 +00:00
var indexType int
2014-01-07 09:33:27 +00:00
var indexName, colName, uniqueness string
err = rows.Scan(&colName, &uniqueness, &indexName)
if err != nil {
return nil, err
}
indexName = strings.Trim(indexName, `" `)
if uniqueness == "UNIQUE" {
indexType = core.UniqueType
2014-01-07 09:33:27 +00:00
} else {
indexType = core.IndexType
2014-01-07 09:19:33 +00:00
}
var index *core.Index
2014-01-07 09:19:33 +00:00
var ok bool
if index, ok = indexes[indexName]; !ok {
index = new(core.Index)
2014-01-07 09:19:33 +00:00
index.Type = indexType
index.Name = indexName
indexes[indexName] = index
}
index.AddColumn(colName)
}
return indexes, nil
}
2014-01-07 09:33:27 +00:00
func (db *oracle) Filters() []core.Filter {
2014-04-21 09:49:34 +00:00
return []core.Filter{&core.QuoteFilter{}, &core.SeqFilter{":", 1}, &core.IdFilter{}}
2014-01-07 09:33:27 +00:00
}