forked from mirror/gorm
Merge pull request #127 from vgarvardt/master
Multicolumn index and unique index
This commit is contained in:
commit
a77ee01cec
18
README.md
18
README.md
|
@ -923,6 +923,22 @@ for rows.Next() {
|
||||||
db.Table("users").Select("users.name, emails.email").Joins("left join emails on emails.user_id = users.id").Scan(&results)
|
db.Table("users").Select("users.name, emails.email").Joins("left join emails on emails.user_id = users.id").Scan(&results)
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Indices
|
||||||
|
|
||||||
|
```go
|
||||||
|
// single column index
|
||||||
|
db.Model(User{}).AddIndex("idx_user_name", "name")
|
||||||
|
|
||||||
|
// multiple column index
|
||||||
|
db.Model(User{}).AddIndex("idx_user_name_age", "name", "age")
|
||||||
|
|
||||||
|
// single column unique index
|
||||||
|
db.Model(User{}).AddUniqueIndex("idx_user_name", "name")
|
||||||
|
|
||||||
|
// multiple column unique index
|
||||||
|
db.Model(User{}).AddUniqueIndex("idx_user_name_age", "name", "age")
|
||||||
|
```
|
||||||
|
|
||||||
## Run Raw SQL
|
## Run Raw SQL
|
||||||
|
|
||||||
```go
|
```go
|
||||||
|
@ -1032,7 +1048,7 @@ db.Where("email = ?", "x@example.org").Attrs(User{RegisteredIp: "111.111.111.111
|
||||||
share or not? transaction?
|
share or not? transaction?
|
||||||
* Github Pages
|
* Github Pages
|
||||||
* Includes
|
* Includes
|
||||||
* AlertColumn, DropColumn, AddIndex, RemoveIndex
|
* AlertColumn, DropColumn
|
||||||
|
|
||||||
# Author
|
# Author
|
||||||
|
|
||||||
|
|
9
main.go
9
main.go
|
@ -314,8 +314,13 @@ func (s *DB) DropColumn(column string) *DB {
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *DB) AddIndex(column string, indexName ...string) *DB {
|
func (s *DB) AddIndex(indexName string, column ...string) *DB {
|
||||||
s.clone().NewScope(s.Value).addIndex(column, indexName...)
|
s.clone().NewScope(s.Value).addIndex(false, indexName, column...)
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *DB) AddUniqueIndex(indexName string, column ...string) *DB {
|
||||||
|
s.clone().NewScope(s.Value).addIndex(true, indexName, column...)
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
35
main_test.go
35
main_test.go
|
@ -83,6 +83,16 @@ type User struct {
|
||||||
IgnoreStringSlice []string `sql:"-"`
|
IgnoreStringSlice []string `sql:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type UserCompany struct {
|
||||||
|
Id int64
|
||||||
|
UserId int64
|
||||||
|
CompanyId int64
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t UserCompany) TableName() string {
|
||||||
|
return "user_companies"
|
||||||
|
}
|
||||||
|
|
||||||
type CreditCard struct {
|
type CreditCard struct {
|
||||||
Id int8
|
Id int8
|
||||||
Number string
|
Number string
|
||||||
|
@ -179,6 +189,7 @@ func init() {
|
||||||
db.Exec("drop table roles")
|
db.Exec("drop table roles")
|
||||||
db.Exec("drop table companies")
|
db.Exec("drop table companies")
|
||||||
db.Exec("drop table animals")
|
db.Exec("drop table animals")
|
||||||
|
db.Exec("drop table user_companies")
|
||||||
|
|
||||||
if err = db.CreateTable(&Animal{}).Error; err != nil {
|
if err = db.CreateTable(&Animal{}).Error; err != nil {
|
||||||
panic(fmt.Sprintf("No error should happen when create table, but got %+v", err))
|
panic(fmt.Sprintf("No error should happen when create table, but got %+v", err))
|
||||||
|
@ -212,6 +223,10 @@ func init() {
|
||||||
panic(fmt.Sprintf("No error should happen when create table, but got %+v", err))
|
panic(fmt.Sprintf("No error should happen when create table, but got %+v", err))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err = db.AutoMigrate(UserCompany{}).Error; err != nil {
|
||||||
|
panic(fmt.Sprintf("No error should happen when create table, but got %+v", err))
|
||||||
|
}
|
||||||
|
|
||||||
var shortForm = "2006-01-02 15:04:05"
|
var shortForm = "2006-01-02 15:04:05"
|
||||||
t1, _ = time.Parse(shortForm, "2000-10-27 12:02:40")
|
t1, _ = time.Parse(shortForm, "2000-10-27 12:02:40")
|
||||||
t2, _ = time.Parse(shortForm, "2002-01-01 00:00:00")
|
t2, _ = time.Parse(shortForm, "2002-01-01 00:00:00")
|
||||||
|
@ -2003,3 +2018,23 @@ func TestSelectWithEscapedFieldName(t *testing.T) {
|
||||||
t.Errorf("Expected one name, but got: %d", len(names))
|
t.Errorf("Expected one name, but got: %d", len(names))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestIndices(t *testing.T) {
|
||||||
|
if err := db.Model(&UserCompany{}).AddIndex("idx_user_company_user", "user_id").Error; err != nil {
|
||||||
|
t.Errorf("Got error when tried to create index: %+v", err)
|
||||||
|
}
|
||||||
|
if err := db.Model(&UserCompany{}).RemoveIndex("idx_user_company_user").Error; err != nil {
|
||||||
|
t.Errorf("Got error when tried to remove index: %+v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := db.Model(&UserCompany{}).AddIndex("idx_user_company_user_company", "user_id", "company_id").Error; err != nil {
|
||||||
|
t.Errorf("Got error when tried to create index: %+v", err)
|
||||||
|
}
|
||||||
|
if err := db.Model(&UserCompany{}).RemoveIndex("idx_user_company_user_company").Error; err != nil {
|
||||||
|
t.Errorf("Got error when tried to remove index: %+v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := db.Model(&UserCompany{}).AddUniqueIndex("idx_user_company_user_company", "user_id", "company_id").Error; err != nil {
|
||||||
|
t.Errorf("Got error when tried to create index: %+v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -431,15 +431,18 @@ func (scope *Scope) dropColumn(column string) {
|
||||||
scope.Raw(fmt.Sprintf("ALTER TABLE %v DROP COLUMN %v", scope.Quote(scope.TableName()), scope.Quote(column))).Exec()
|
scope.Raw(fmt.Sprintf("ALTER TABLE %v DROP COLUMN %v", scope.Quote(scope.TableName()), scope.Quote(column))).Exec()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (scope *Scope) addIndex(column string, names ...string) {
|
func (scope *Scope) addIndex(unique bool, indexName string, column ...string) {
|
||||||
var indexName string
|
var columns []string
|
||||||
if len(names) > 0 {
|
for _, name := range column {
|
||||||
indexName = names[0]
|
columns = append(columns, scope.Quote(name))
|
||||||
} else {
|
|
||||||
indexName = fmt.Sprintf("index_%v_on_%v", scope.TableName(), column)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
scope.Raw(fmt.Sprintf("CREATE INDEX %v ON %v(%v);", indexName, scope.Quote(scope.TableName()), scope.Quote(column))).Exec()
|
sqlCreate := "CREATE INDEX"
|
||||||
|
if unique {
|
||||||
|
sqlCreate = "CREATE UNIQUE INDEX"
|
||||||
|
}
|
||||||
|
|
||||||
|
scope.Raw(fmt.Sprintf("%s %v ON %v(%v);", sqlCreate, indexName, scope.Quote(scope.TableName()), strings.Join(columns, ", "))).Exec()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (scope *Scope) removeIndex(indexName string) {
|
func (scope *Scope) removeIndex(indexName string) {
|
||||||
|
|
Loading…
Reference in New Issue