Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,19 @@ The fantastic ORM library for Golang, aims to be developer friendly.

* GORM Guides [https://gorm.io](https://gorm.io)

## Oracle Support
Oracle support is made optional.

Since oracle drivers initialize at start, a build flag has been added to enable oracle sdk.
Example:
```shell
go build -tags oracle ./...
```
or running main during development
```shell
go run -tags oracle main.go
```

## Contributing

[You can help to deliver a better GORM, check out things you can do](https://gorm.io/contribute.html)
Expand Down
2 changes: 1 addition & 1 deletion association_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"sort"
"testing"

"github.com/jinzhu/gorm"
"github.com/Vernacular-ai/gorm"
)

func TestBelongsTo(t *testing.T) {
Expand Down
6 changes: 6 additions & 0 deletions callback_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,12 @@ func afterCreateCallback(scope *Scope) {
scope.Dialect().SetDB(scope.db.db)
primaryField := scope.PrimaryField()
val := primaryField.Field.Interface()

// In case the primary key implements valuer, using the value from that valuer to resolve a row ID
if primary, ok := val.(primary); ok {
val = primary.ID
}

// Row ID cannot be 0. Obvious issue that has occurred upstream.
if arg, ok := val.(uint); ok && arg != 0{
scope.Err(primaryField.Set(scope.Dialect().ResolveRowID(scope.TableName(), arg)))
Expand Down
2 changes: 1 addition & 1 deletion callbacks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package gorm_test
import (
"errors"

"github.com/jinzhu/gorm"
"github.com/Vernacular-ai/gorm"

"reflect"
"testing"
Expand Down
2 changes: 1 addition & 1 deletion customize_column_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"testing"
"time"

"github.com/jinzhu/gorm"
"github.com/Vernacular-ai/gorm"
)

type CustomizeColumn struct {
Expand Down
4 changes: 4 additions & 0 deletions dialect.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ type Dialect interface {
HasForeignKey(tableName string, foreignKeyName string) bool
// RemoveIndex remove index
RemoveIndex(tableName string, indexName string) error
// Remove constraint from a column in the DB
RemoveConstraint(tableName string, constraintName string) error
// HasTable check has table or not
HasTable(tableName string) bool
// HasColumn check has column or not
Expand Down Expand Up @@ -66,6 +68,8 @@ type Dialect interface {

// Determing the tag setting based on the dialect being used
GetTagSetting(field *StructField, key string) (string, bool)
// Determine the limit of byte size for a BLOB
GetByteLimit() int
}

var dialectsMap = map[string]Dialect{}
Expand Down
9 changes: 9 additions & 0 deletions dialect_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@ func (s commonDialect) RemoveIndex(tableName string, indexName string) error {
return err
}

func (s commonDialect) RemoveConstraint(tableName string, constraintName string) error {
_, err := s.db.Exec(fmt.Sprintf("ALTER TABLE %v DROP CONSTRAINT %v", tableName, constraintName))
return err
}

func (s commonDialect) HasForeignKey(tableName string, foreignKeyName string) bool {
return false
}
Expand Down Expand Up @@ -201,3 +206,7 @@ func (commonDialect) ColumnEquality(fieldDBName, columnName string) bool {
func (s commonDialect) GetTagSetting(field *StructField, key string) (val string, ok bool) {
return field.TagSettingsGet(key)
}

func (s commonDialect) GetByteLimit() int {
return -1
}
13 changes: 12 additions & 1 deletion dialect_mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,19 @@ func (s *mysql) DataTypeOf(field *StructField) string {
}
default:
if IsByteArrayOrSlice(dataValue) {
if size > 0 && size < 65532 {
if isJSON(dataValue) {
// Adding a constraint to see ensure that the value is a well formed JSON
sqlType = "json"
} else if size > 0 && size < 65532 {
sqlType = fmt.Sprintf("varbinary(%d)", size)
} else {
sqlType = "longblob"
}
}
}
} else if isUUID(dataValue) {
// In case the user has specified uuid as the type explicitly
sqlType = "varchar(36)"
}

if sqlType == "" {
Expand All @@ -134,6 +140,11 @@ func (s mysql) RemoveIndex(tableName string, indexName string) error {
return err
}

func (s mysql) RemoveConstraint(tableName string, constraintName string) error {
_, err := s.db.Exec(fmt.Sprintf("ALTER TABLE %v DROP INDEX %v", tableName, constraintName))
return err
}

func (s mysql) ModifyColumn(tableName string, columnName string, typ string) error {
_, err := s.db.Exec(fmt.Sprintf("ALTER TABLE %v MODIFY COLUMN %v %v", tableName, columnName, typ))
return err
Expand Down
9 changes: 8 additions & 1 deletion dialect_oci8.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// +build oracle

package gorm

import (
Expand Down Expand Up @@ -152,7 +154,7 @@ func (o *oci8) ResolveRowID(tableName string, rowID uint) uint{
query := fmt.Sprintf(`SELECT id FROM %s WHERE rowid = :2`, o.Quote(tableName))
var err error
if err = o.db.QueryRow(query, strRowID).Scan(&id); err == nil{
if res, err := strconv.ParseUint(id, 10, 32); err == nil{
if res, err := strconv.ParseUint(id, 10, 64); err == nil{
resolvedId := uint(res)
return resolvedId
}
Expand Down Expand Up @@ -208,3 +210,8 @@ func (*oci8) ColumnEquality(fieldDBName, columnName string) bool {
func (o *oci8) GetTagSetting(field *StructField, key string) (val string, ok bool) {
return field.TagSettingsGetFirst(strings.ToUpper(o.GetName())+" "+key, key)
}

func (o *oci8) GetByteLimit() int {
return 30000
}

2 changes: 1 addition & 1 deletion dialects/mssql/mssql.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

// Importing mssql driver package only in dialect file, otherwide not needed
_ "github.com/denisenkom/go-mssqldb"
"github.com/jinzhu/gorm"
"github.com/Vernacular-ai/gorm"
)

func setIdentityInsert(scope *gorm.Scope) {
Expand Down
1 change: 1 addition & 0 deletions dialects/oci8/oci8.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package oci8
2 changes: 2 additions & 0 deletions dialects/oci8/oracle.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// +build oracle

package oci8

import _ "github.com/mattn/go-oci8"
2 changes: 1 addition & 1 deletion errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"errors"
"testing"

"github.com/jinzhu/gorm"
"github.com/Vernacular-ai/gorm"
)

func TestErrorsCanBeUsedOutsideGorm(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion field_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"fmt"
"testing"

"github.com/jinzhu/gorm"
"github.com/Vernacular-ai/gorm"
)

type CalculateField struct {
Expand Down
10 changes: 4 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
module github.com/jinzhu/gorm
module github.com/Vernacular-ai/gorm

go 1.12

require (
github.com/denisenkom/go-mssqldb v0.0.0-20190515213511-eb9f6a1743f3
github.com/erikstmartin/go-testdb v0.0.0-20160219214506-8d10e4a1bae5
github.com/denisenkom/go-mssqldb v0.0.0-20190806190131-db2462fef53b
github.com/go-sql-driver/mysql v1.4.1
github.com/jinzhu/inflection v1.0.0
github.com/jinzhu/now v1.0.1
github.com/lib/pq v1.1.1
github.com/lib/pq v1.2.0
github.com/mattn/go-oci8 v0.0.0-20190524004114-abcafa411cc8
github.com/mattn/go-sqlite3 v1.10.0
github.com/mattn/go-sqlite3 v1.11.0
)
18 changes: 6 additions & 12 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,11 @@ github.com/apache/thrift v0.12.0/go.mod h1:cp2SuWMxlEZw2r+iP2GNCdIi4C1qmUzdZFSVb
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/denisenkom/go-mssqldb v0.0.0-20190515213511-eb9f6a1743f3 h1:tkum0XDgfR0jcVVXuTsYv/erY2NnEDqwRojbxR1rBYA=
github.com/denisenkom/go-mssqldb v0.0.0-20190515213511-eb9f6a1743f3/go.mod h1:zAg7JM8CkOJ43xKXIj7eRO9kmWm/TW578qo+oDO6tuM=
github.com/denisenkom/go-mssqldb v0.0.0-20190806190131-db2462fef53b h1:C8eKel5CV265bq4KKOvyMKyNK4NPPr1mImhqm8xcK7k=
github.com/denisenkom/go-mssqldb v0.0.0-20190806190131-db2462fef53b/go.mod h1:uU0N10vx1abI4qeVe79CxepBP6PPREVTgMS5Gx6/mOk=
github.com/eapache/go-resiliency v1.1.0/go.mod h1:kFI+JgMyC7bLPUVY133qvEBtVayf5mFgVsvEsIPBvNs=
github.com/eapache/go-xerial-snappy v0.0.0-20180814174437-776d5712da21/go.mod h1:+020luEh2TKB4/GOp8oxxtq0Daoen/Cii55CzbTV6DU=
github.com/eapache/queue v1.1.0/go.mod h1:6eCeP0CKFpHLu8blIFXhExK/dRa7WDZfr6jVFPTqq+I=
github.com/erikstmartin/go-testdb v0.0.0-20160219214506-8d10e4a1bae5 h1:Yzb9+7DPaBjB8zlTR87/ElzFsnQfuHnVUVqpZZIcV5Y=
github.com/erikstmartin/go-testdb v0.0.0-20160219214506-8d10e4a1bae5/go.mod h1:a2zkGnVExMxdzMo3M0Hi/3sEU+cWnZpSni0O6/Yb/P0=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE=
Expand All @@ -32,7 +30,6 @@ github.com/golang/mock v1.2.0/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfb
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
github.com/google/go-cmp v0.2.0 h1:+dTQ8DZQJz0Mb/HjFlkptS1FeQ4cWSnN941F8aEG4SQ=
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs=
github.com/google/pprof v0.0.0-20181206194817-3ea8567a2e57/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=
Expand All @@ -43,19 +40,17 @@ github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/jinzhu/now v1.0.1 h1:HjfetcXq097iXP0uoPCdnM4Efp5/9MsM0/M+XOTeR3M=
github.com/jinzhu/now v1.0.1/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc=
github.com/lib/pq v1.1.1 h1:sJZmqHoEaY7f+NPP8pgLB/WxulyR3fewgCM2qaSlBb4=
github.com/lib/pq v1.1.1/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/lib/pq v1.2.0 h1:LXpIM/LZ5xGFhOpXAQUIMM1HdyqzVYM13zNdjCEEcA0=
github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/mattn/go-oci8 v0.0.0-20190524004114-abcafa411cc8 h1:f6VmRhxS0zvzsi+8snVfi9gSYhjMnb5Rmy4vnQJ107Q=
github.com/mattn/go-oci8 v0.0.0-20190524004114-abcafa411cc8/go.mod h1:/M9VLO+lUPmxvoOK2PfWRZ8mTtB4q1Hy9lEGijv9Nr8=
github.com/mattn/go-sqlite3 v1.10.0 h1:jbhqpg7tQe4SupckyijYiy0mJJ/pRyHvXf7JdWK860o=
github.com/mattn/go-sqlite3 v1.10.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
github.com/mattn/go-sqlite3 v1.11.0 h1:LDdKkqtYlom37fkvqs8rMPFKAMe8+SgjbwZ6ex1/A/Q=
github.com/mattn/go-sqlite3 v1.11.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
Expand Down Expand Up @@ -114,7 +109,6 @@ golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3
golang.org/x/tools v0.0.0-20190312170243-e65039ee4138/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
google.golang.org/api v0.3.1/go.mod h1:6wY9I6uQWHQ8EM57III9mq/AjF+i8G65rmVagqKMtkk=
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
google.golang.org/appengine v1.4.0 h1:/wp5JvzpHIxhs/dumFmF7BXTf3Z+dd4uXta4kVyO508=
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc=
google.golang.org/genproto v0.0.0-20190307195333-5fe7a883aa19/go.mod h1:VzzqZJRnGkLBvHegQrXjBqPurQTc5/KpmUdxsrq26oE=
Expand Down
2 changes: 1 addition & 1 deletion join_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"testing"
"time"

"github.com/jinzhu/gorm"
"github.com/Vernacular-ai/gorm"
)

type Person struct {
Expand Down
15 changes: 11 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ const (
// db, err := gorm.Open("mysql", "user:password@/dbname?charset=utf8&parseTime=True&loc=Local")
// }
// GORM has wrapped some drivers, for easier to remember driver's import path, so you could import the mysql driver with
// import _ "github.com/jinzhu/gorm/dialects/mysql"
// // import _ "github.com/jinzhu/gorm/dialects/postgres"
// // import _ "github.com/jinzhu/gorm/dialects/sqlite"
// // import _ "github.com/jinzhu/gorm/dialects/mssql"
// import _ "github.com/Vernacular-ai/gorm/dialects/mysql"
// // import _ "github.com/Vernacular-ai/gorm/dialects/postgres"
// // import _ "github.com/Vernacular-ai/gorm/dialects/sqlite"
// // import _ "github.com/Vernacular-ai/gorm/dialects/mssql"
func Open(dialect string, args ...interface{}) (db *DB, err error) {
if len(args) == 0 {
err = errors.New("invalid database source")
Expand Down Expand Up @@ -694,6 +694,13 @@ func (s *DB) RemoveIndex(indexName string) *DB {
return scope.db
}

// RemoveIndex remove constraint with name
func (s *DB) RemoveConstraint(constraintName string) *DB {
scope := s.NewScope(s.Value)
scope.removeConstraint(constraintName)
return scope.db
}

// AddForeignKey Add foreign key to the given scope, e.g:
// db.Model(&User{}).AddForeignKey("city_id", "cities(id)", "RESTRICT", "RESTRICT")
func (s *DB) AddForeignKey(field string, dest string, onDelete string, onUpdate string) *DB {
Expand Down
10 changes: 5 additions & 5 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ import (
"time"

"github.com/erikstmartin/go-testdb"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/mssql"
_ "github.com/jinzhu/gorm/dialects/mysql"
"github.com/jinzhu/gorm/dialects/postgres"
_ "github.com/jinzhu/gorm/dialects/sqlite"
"github.com/Vernacular-ai/gorm"
_ "github.com/Vernacular-ai/gorm/dialects/mssql"
_ "github.com/Vernacular-ai/gorm/dialects/mysql"
"github.com/Vernacular-ai/gorm/dialects/postgres"
_ "github.com/Vernacular-ai/gorm/dialects/sqlite"
"github.com/jinzhu/now"
)

Expand Down
2 changes: 1 addition & 1 deletion migration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"testing"
"time"

"github.com/jinzhu/gorm"
"github.com/Vernacular-ai/gorm"
)

type User struct {
Expand Down
37 changes: 36 additions & 1 deletion model.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package gorm

import "time"
import (
"database/sql/driver"
"time"
)

// Model base model definition, including fields `ID`, `CreatedAt`, `UpdatedAt`, `DeletedAt`, which could be embedded in your models
// type User struct {
Expand All @@ -12,3 +15,35 @@ type Model struct {
UpdatedAt time.Time
DeletedAt *time.Time `sql:"index"`
}

type primary struct {
ID uint
}

func (x *primary) Scan(src interface{}) error {
if val, ok := src.(uint); ok {
x.ID = val
} else if val, ok := src.(float64); ok {
// This happens due to oracle driver not knowing that uint is required by the calling code.
x.ID = uint(val)
} else if val, ok := src.(int64); ok {
x.ID = uint(val)
}
// Note: This else results in errors while inserting into the table in oracle. Hence not using such a clause
//else {
// return errors.New(fmt.Sprintf("Unable to convert %v to uint", src))
//}

return nil
}

func (x *primary) Value() (driver.Value, error) {
return x.ID, nil
}

type ORM struct {
ID primary `gorm:"primary_key"`
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt *time.Time `sql:"index"`
}
2 changes: 1 addition & 1 deletion naming_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package gorm_test
import (
"testing"

"github.com/jinzhu/gorm"
"github.com/Vernacular-ai/gorm"
)

func TestTheNamingStrategy(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion preload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"reflect"
"testing"

"github.com/jinzhu/gorm"
"github.com/Vernacular-ai/gorm"
)

func getPreloadUser(name string) *User {
Expand Down
Loading