first attempt at a working dialect
This commit is contained in:
parent
2f2cc37ccc
commit
4061744821
@ -10,19 +10,33 @@ import (
|
|||||||
|
|
||||||
_ "github.com/godror/godror"
|
_ "github.com/godror/godror"
|
||||||
"github.com/jinzhu/gorm"
|
"github.com/jinzhu/gorm"
|
||||||
|
_ "github.com/mattn/go-oci8"
|
||||||
|
_ "gopkg.in/rana/ora.v4"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
//const dialectName = "godror"
|
||||||
|
// const dialectName = "ora"
|
||||||
|
const dialectName = "oci8"
|
||||||
|
|
||||||
type oracle struct {
|
type oracle struct {
|
||||||
db gorm.SQLCommon
|
db gorm.SQLCommon
|
||||||
gorm.DefaultForeignKeyNamer
|
gorm.DefaultForeignKeyNamer
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
gorm.RegisterDialect("godror", &oracle{})
|
gorm.RegisterDialect(dialectName, &oracle{})
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *oracle) fieldCanAutoIncrement(field *gorm.StructField) bool {
|
||||||
|
if value, ok := field.TagSettingsGet("AUTO_INCREMENT"); ok {
|
||||||
|
return strings.ToLower(value) != "false"
|
||||||
|
}
|
||||||
|
return field.IsPrimaryKey
|
||||||
}
|
}
|
||||||
|
|
||||||
func (oracle) GetName() string {
|
func (oracle) GetName() string {
|
||||||
return "goracle"
|
return dialectName
|
||||||
}
|
}
|
||||||
|
|
||||||
func (oracle) BindVar(i int) string {
|
func (oracle) BindVar(i int) string {
|
||||||
@ -30,12 +44,13 @@ func (oracle) BindVar(i int) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (oracle) Quote(key string) string {
|
func (oracle) Quote(key string) string {
|
||||||
return fmt.Sprintf("%s", key)
|
return key
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s oracle) CurrentDatabase() (name string) {
|
func (s oracle) CurrentDatabase() string {
|
||||||
|
var name string
|
||||||
s.db.QueryRow("SELECT ORA_DATABASE_NAME as \"Current Database\" FROM DUAL").Scan(&name)
|
s.db.QueryRow("SELECT ORA_DATABASE_NAME as \"Current Database\" FROM DUAL").Scan(&name)
|
||||||
return
|
return name
|
||||||
}
|
}
|
||||||
|
|
||||||
func (oracle) DefaultValueStr() string {
|
func (oracle) DefaultValueStr() string {
|
||||||
@ -44,40 +59,53 @@ func (oracle) DefaultValueStr() string {
|
|||||||
|
|
||||||
func (s oracle) HasColumn(tableName string, columnName string) bool {
|
func (s oracle) HasColumn(tableName string, columnName string) bool {
|
||||||
var count int
|
var count int
|
||||||
currentDatabase, tableName := currentDatabaseAndTable(&s, tableName)
|
_, tableName = currentDatabaseAndTable(&s, tableName)
|
||||||
fmt.Printf(currentDatabase)
|
tableName = strings.ToUpper(tableName)
|
||||||
s.db.QueryRow("SELECT count(*) FROM ALL_TAB_COLUMNS WHERE TABLE_NAME = ? AND COLUMN_NAME = ?", tableName, columnName).Scan(&count)
|
columnName = strings.ToUpper(columnName)
|
||||||
return count > 0
|
if err := s.db.QueryRow("SELECT count(*) FROM ALL_TAB_COLUMNS WHERE TABLE_NAME = :1 AND COLUMN_NAME = :2", tableName, columnName).Scan(&count); err == nil {
|
||||||
|
return count > 0
|
||||||
|
} else {
|
||||||
|
fmt.Println(err)
|
||||||
|
}
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s oracle) HasForeignKey(tableName string, foreignKeyName string) bool {
|
func (s oracle) HasForeignKey(tableName string, foreignKeyName string) bool {
|
||||||
var count int
|
var count int
|
||||||
currentDatabase, tableName := currentDatabaseAndTable(&s, tableName)
|
_, tableName = currentDatabaseAndTable(&s, tableName)
|
||||||
fmt.Printf(currentDatabase)
|
tableName = strings.ToUpper(tableName)
|
||||||
s.db.QueryRow(`SELECT count(*)
|
if err := s.db.QueryRow(`SELECT count(*)
|
||||||
FROM all_cons_columns a
|
FROM all_cons_columns a
|
||||||
JOIN all_constraints c ON a.owner = c.owner
|
JOIN all_constraints c ON a.owner = c.owner
|
||||||
AND a.constraint_name = c.constraint_name
|
AND a.constraint_name = c.constraint_name
|
||||||
AND a.constraint_name = ?
|
AND a.constraint_name = :1
|
||||||
JOIN all_constraints c_pk ON c.r_owner = c_pk.owner
|
JOIN all_constraints c_pk ON c.r_owner = c_pk.owner
|
||||||
AND c.r_constraint_name = c_pk.constraint_name
|
AND c.r_constraint_name = c_pk.constraint_name
|
||||||
WHERE c.constraint_type = 'R'
|
WHERE c.constraint_type = 'R'
|
||||||
AND a.table_name = ?;`, foreignKeyName, tableName).Scan(&count)
|
AND a.table_name = :2 ;`, foreignKeyName, tableName).Scan(&count); err == nil {
|
||||||
return count > 0
|
return count > 0
|
||||||
|
}
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s oracle) HasIndex(tableName string, indexName string) bool {
|
func (s oracle) HasIndex(tableName string, indexName string) bool {
|
||||||
var count int
|
var count int
|
||||||
s.db.QueryRow("SELECT count(*) FROM ALL_INDEXES WHERE INDEX_NAME = ? AND TABLE_NAME = ?", indexName, tableName).Scan(&count)
|
tableName = strings.ToUpper(tableName)
|
||||||
return count > 0
|
indexName = strings.ToUpper(indexName)
|
||||||
|
if err := s.db.QueryRow("SELECT count(*) FROM ALL_INDEXES WHERE INDEX_NAME = :1 AND TABLE_NAME = :2", indexName, tableName).Scan(&count); err == nil {
|
||||||
|
return count > 0
|
||||||
|
}
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s oracle) HasTable(tableName string) bool {
|
func (s oracle) HasTable(tableName string) bool {
|
||||||
var count int
|
var count int
|
||||||
currentDatabase, tableName := currentDatabaseAndTable(&s, tableName)
|
_, tableName = currentDatabaseAndTable(&s, tableName)
|
||||||
fmt.Printf(currentDatabase)
|
tableName = strings.ToUpper(tableName)
|
||||||
s.db.QueryRow("SELECT DISTINCT OBJECT_NAME FROM USER_OBJECTS WHERE OBJECT_TYPE = 'TABLE' AND OBJECT_NAME = ?", tableName).Scan(&count)
|
if err := s.db.QueryRow("select count(*) from user_tables where table_name = :1", tableName).Scan(&count); err == nil {
|
||||||
return count > 0
|
return count > 0
|
||||||
|
}
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (oracle) LastInsertIDReturningSuffix(tableName, columnName string) string {
|
func (oracle) LastInsertIDReturningSuffix(tableName, columnName string) string {
|
||||||
@ -99,7 +127,7 @@ func (s oracle) RemoveIndex(tableName string, indexName string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (oracle) SelectFromDummyTable() string {
|
func (oracle) SelectFromDummyTable() string {
|
||||||
return "SELECT * FROM DUAL"
|
return "FROM DUAL"
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *oracle) SetDB(db gorm.SQLCommon) {
|
func (s *oracle) SetDB(db gorm.SQLCommon) {
|
||||||
@ -122,17 +150,24 @@ func (s *oracle) DataTypeOf(field *gorm.StructField) string {
|
|||||||
case reflect.Bool, reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Uint, reflect.Uint8,
|
case reflect.Bool, reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Uint, reflect.Uint8,
|
||||||
reflect.Uint16, reflect.Uintptr, reflect.Int64, reflect.Uint32, reflect.Uint64,
|
reflect.Uint16, reflect.Uintptr, reflect.Int64, reflect.Uint32, reflect.Uint64,
|
||||||
reflect.Float32, reflect.Float64:
|
reflect.Float32, reflect.Float64:
|
||||||
sqlType = "NUMBER"
|
if s.fieldCanAutoIncrement(field) {
|
||||||
|
sqlType = "NUMBER GENERATED BY DEFAULT AS IDENTITY"
|
||||||
|
} else {
|
||||||
|
sqlType = "NUMBER"
|
||||||
|
}
|
||||||
case reflect.String:
|
case reflect.String:
|
||||||
if _, ok := field.TagSettingsGet("SIZE"); !ok {
|
if _, ok := field.TagSettingsGet("SIZE"); !ok {
|
||||||
size = 0 // if SIZE haven't been set, use `text` as the default type, as there are no performance different
|
size = 0 // if SIZE haven't been set, use `text` as the default type, as there are no performance different
|
||||||
}
|
}
|
||||||
|
switch {
|
||||||
if size > 0 && size < 4000 {
|
case size > 0 && size < 4000:
|
||||||
sqlType = fmt.Sprintf("VARCHAR2(%d)", size)
|
sqlType = fmt.Sprintf("VARCHAR2(%d)", size)
|
||||||
} else {
|
case size == 0:
|
||||||
|
sqlType = "VARCHAR2 (4000)"
|
||||||
|
default:
|
||||||
sqlType = "CLOB"
|
sqlType = "CLOB"
|
||||||
}
|
}
|
||||||
|
|
||||||
case reflect.Struct:
|
case reflect.Struct:
|
||||||
if _, ok := dataValue.Interface().(time.Time); ok {
|
if _, ok := dataValue.Interface().(time.Time); ok {
|
||||||
sqlType = "TIMESTAMP WITH TIME ZONE"
|
sqlType = "TIMESTAMP WITH TIME ZONE"
|
||||||
@ -143,15 +178,7 @@ func (s *oracle) DataTypeOf(field *gorm.StructField) string {
|
|||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
if gorm.IsByteArrayOrSlice(dataValue) {
|
if gorm.IsByteArrayOrSlice(dataValue) {
|
||||||
sqlType = "VARCHAR2"
|
sqlType = "VARCHAR2 (4000)"
|
||||||
|
|
||||||
/*if isUUID(dataValue) {
|
|
||||||
sqlType = "VARCHAR2"
|
|
||||||
}
|
|
||||||
|
|
||||||
if isJSON(dataValue) {
|
|
||||||
sqlType = "CLOB"
|
|
||||||
}*/
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -163,6 +190,22 @@ func (s *oracle) DataTypeOf(field *gorm.StructField) string {
|
|||||||
if strings.TrimSpace(additionalType) == "" {
|
if strings.TrimSpace(additionalType) == "" {
|
||||||
return sqlType
|
return sqlType
|
||||||
}
|
}
|
||||||
|
if strings.EqualFold(sqlType, "json") {
|
||||||
|
sqlType = "VARCHAR2 (4000)"
|
||||||
|
}
|
||||||
|
|
||||||
|
// For oracle, we have to redo the order of the Default type from tag setting
|
||||||
|
notNull, _ := field.TagSettingsGet("NOT NULL")
|
||||||
|
unique, _ := field.TagSettingsGet("UNIQUE")
|
||||||
|
additionalType = notNull + " " + unique
|
||||||
|
if value, ok := field.TagSettingsGet("DEFAULT"); ok {
|
||||||
|
additionalType = fmt.Sprintf("%s %s %s", "DEFAULT", value, additionalType)
|
||||||
|
// additionalType = additionalType + " DEFAULT " + value
|
||||||
|
}
|
||||||
|
|
||||||
|
if value, ok := field.TagSettingsGet("COMMENT"); ok {
|
||||||
|
additionalType = additionalType + " COMMENT " + value
|
||||||
|
}
|
||||||
return fmt.Sprintf("%v %v", sqlType, additionalType)
|
return fmt.Sprintf("%v %v", sqlType, additionalType)
|
||||||
}
|
}
|
||||||
func (s oracle) LimitAndOffsetSQL(limit, offset interface{}) (sql string, err error) {
|
func (s oracle) LimitAndOffsetSQL(limit, offset interface{}) (sql string, err error) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user