forked from go-gorm/gorm
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Utilize go1.8 context support in database/sql
Fixes go-gorm#1231 The related go1.8 release notes: https://golang.org/doc/go1.8#database_sql
- Loading branch information
1 parent
0a51f6c
commit fb51756
Showing
29 changed files
with
580 additions
and
181 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// +build go1.8 | ||
|
||
package gorm | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
) | ||
|
||
func (s commonDialect) HasIndex(tableName string, indexName string) bool { | ||
var count int | ||
s.db.QueryRowContext(context.Background(), queryHasIndex, s.CurrentDatabase(), tableName, indexName).Scan(&count) | ||
return count > 0 | ||
} | ||
|
||
func (s commonDialect) RemoveIndex(tableName string, indexName string) error { | ||
_, err := s.db.ExecContext(context.Background(), fmt.Sprintf(queryRemoveIndex, indexName)) | ||
return err | ||
} | ||
|
||
func (s commonDialect) HasTable(tableName string) bool { | ||
var count int | ||
s.db.QueryRowContext(context.Background(), queryHasTable, s.CurrentDatabase(), tableName).Scan(&count) | ||
return count > 0 | ||
} | ||
|
||
func (s commonDialect) HasColumn(tableName string, columnName string) bool { | ||
var count int | ||
s.db.QueryRowContext(context.Background(), queryHasColumn, s.CurrentDatabase(), tableName, columnName).Scan(&count) | ||
return count > 0 | ||
} | ||
|
||
func (s commonDialect) CurrentDatabase() (name string) { | ||
s.db.QueryRowContext(context.Background(), queryCurrentDatabase).Scan(&name) | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// +build !go1.8 | ||
|
||
package gorm | ||
|
||
import "fmt" | ||
|
||
func (s commonDialect) HasIndex(tableName string, indexName string) bool { | ||
var count int | ||
s.db.QueryRow(queryHasIndex, s.CurrentDatabase(), tableName, indexName).Scan(&count) | ||
return count > 0 | ||
} | ||
|
||
func (s commonDialect) RemoveIndex(tableName string, indexName string) error { | ||
_, err := s.db.Exec(fmt.Sprintf(queryRemoveIndex, indexName)) | ||
return err | ||
} | ||
|
||
func (s commonDialect) HasTable(tableName string) bool { | ||
var count int | ||
s.db.QueryRow(queryHasTable, s.CurrentDatabase(), tableName).Scan(&count) | ||
return count > 0 | ||
} | ||
|
||
func (s commonDialect) HasColumn(tableName string, columnName string) bool { | ||
var count int | ||
s.db.QueryRow(queryHasColumn, s.CurrentDatabase(), tableName, columnName).Scan(&count) | ||
return count > 0 | ||
} | ||
|
||
func (s commonDialect) CurrentDatabase() (name string) { | ||
s.db.QueryRow(queryCurrentDatabase).Scan(&name) | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// +build go1.8 | ||
|
||
package gorm | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
) | ||
|
||
func (s mysql) RemoveIndex(tableName string, indexName string) error { | ||
_, err := s.db.ExecContext(context.Background(), fmt.Sprintf(queryMySQLRemoveIndex, indexName, s.Quote(tableName))) | ||
return err | ||
} | ||
|
||
func (s mysql) HasForeignKey(tableName string, foreignKeyName string) bool { | ||
var count int | ||
s.db.QueryRowContext(context.Background(), queryMySQLHasForeignKey, s.CurrentDatabase(), tableName, foreignKeyName).Scan(&count) | ||
return count > 0 | ||
} | ||
|
||
func (s mysql) CurrentDatabase() (name string) { | ||
s.db.QueryRowContext(context.Background(), queryMySQLCurrentDatabase).Scan(&name) | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// +build !go1.8 | ||
|
||
package gorm | ||
|
||
import "fmt" | ||
|
||
func (s mysql) RemoveIndex(tableName string, indexName string) error { | ||
_, err := s.db.Exec(fmt.Sprintf(queryMySQLRemoveIndex, indexName, s.Quote(tableName))) | ||
return err | ||
} | ||
|
||
func (s mysql) HasForeignKey(tableName string, foreignKeyName string) bool { | ||
var count int | ||
s.db.QueryRow(queryMySQLHasForeignKey, s.CurrentDatabase(), tableName, foreignKeyName).Scan(&count) | ||
return count > 0 | ||
} | ||
|
||
func (s mysql) CurrentDatabase() (name string) { | ||
s.db.QueryRow(queryMySQLCurrentDatabase).Scan(&name) | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// +build go1.8 | ||
|
||
package gorm | ||
|
||
import "context" | ||
|
||
func (s postgres) HasIndex(tableName string, indexName string) bool { | ||
var count int | ||
s.db.QueryRowContext(context.Background(), queryPostgresHasIndex, tableName, indexName).Scan(&count) | ||
return count > 0 | ||
} | ||
|
||
func (s postgres) HasForeignKey(tableName string, foreignKeyName string) bool { | ||
var count int | ||
s.db.QueryRowContext(context.Background(), queryPostgresHasForeignKey, tableName, foreignKeyName).Scan(&count) | ||
return count > 0 | ||
} | ||
|
||
func (s postgres) HasTable(tableName string) bool { | ||
var count int | ||
s.db.QueryRowContext(context.Background(), queryPostgresHasTable, tableName).Scan(&count) | ||
return count > 0 | ||
} | ||
|
||
func (s postgres) HasColumn(tableName string, columnName string) bool { | ||
var count int | ||
s.db.QueryRowContext(context.Background(), queryPostgresHasColumn, tableName, columnName).Scan(&count) | ||
return count > 0 | ||
} | ||
|
||
func (s postgres) CurrentDatabase() (name string) { | ||
s.db.QueryRowContext(context.Background(), queryPostgresCurrentDatabase).Scan(&name) | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// +build !go1.8 | ||
|
||
package gorm | ||
|
||
func (s postgres) HasIndex(tableName string, indexName string) bool { | ||
var count int | ||
s.db.QueryRow(queryPostgresHasIndex, tableName, indexName).Scan(&count) | ||
return count > 0 | ||
} | ||
|
||
func (s postgres) HasForeignKey(tableName string, foreignKeyName string) bool { | ||
var count int | ||
s.db.QueryRow(queryPostgresHasForeignKey, tableName, foreignKeyName).Scan(&count) | ||
return count > 0 | ||
} | ||
|
||
func (s postgres) HasTable(tableName string) bool { | ||
var count int | ||
s.db.QueryRow(queryPostgresHasTable, tableName).Scan(&count) | ||
return count > 0 | ||
} | ||
|
||
func (s postgres) HasColumn(tableName string, columnName string) bool { | ||
var count int | ||
s.db.QueryRow(queryPostgresHasColumn, tableName, columnName).Scan(&count) | ||
return count > 0 | ||
} | ||
|
||
func (s postgres) CurrentDatabase() (name string) { | ||
s.db.QueryRow(queryPostgresCurrentDatabase).Scan(&name) | ||
return | ||
} |
Oops, something went wrong.