add schema on postgres dialect

This commit is contained in:
Lunny Xiao 2018-02-08 15:57:08 +08:00
parent 430fbe866a
commit d0808c2be2
No known key found for this signature in database
GPG Key ID: C3B7C91B632F738A
2 changed files with 13 additions and 23 deletions

View File

@ -873,25 +873,19 @@ func (db *postgres) IndexOnTable() bool {
} }
func (db *postgres) IndexCheckSql(tableName, idxName string) (string, []interface{}) { func (db *postgres) IndexCheckSql(tableName, idxName string) (string, []interface{}) {
args := []interface{}{tableName, idxName} args := []interface{}{db.schema, tableName, idxName}
return `SELECT indexname FROM pg_indexes ` + return `SELECT indexname FROM pg_indexes ` +
`WHERE tablename = ? AND indexname = ?`, args `WHERE schemaname = ? AND tablename = ? AND indexname = ?`, args
} }
func (db *postgres) TableCheckSql(tableName string) (string, []interface{}) { func (db *postgres) TableCheckSql(tableName string) (string, []interface{}) {
args := []interface{}{tableName} args := []interface{}{db.schema, tableName}
return `SELECT tablename FROM pg_tables WHERE tablename = ?`, args return `SELECT tablename FROM pg_tables WHERE schemaname = ? AND tablename = ?`, args
} }
/*func (db *postgres) ColumnCheckSql(tableName, colName string) (string, []interface{}) {
args := []interface{}{tableName, colName}
return "SELECT column_name FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = ?" +
" AND column_name = ?", args
}*/
func (db *postgres) ModifyColumnSql(tableName string, col *core.Column) string { func (db *postgres) ModifyColumnSql(tableName string, col *core.Column) string {
return fmt.Sprintf("alter table %s ALTER COLUMN %s TYPE %s", return fmt.Sprintf("alter table %s.%s ALTER COLUMN %s TYPE %s",
tableName, col.Name, db.SqlType(col)) db.schema, tableName, col.Name, db.SqlType(col))
} }
func (db *postgres) DropIndexSql(tableName string, index *core.Index) string { func (db *postgres) DropIndexSql(tableName string, index *core.Index) string {
@ -911,9 +905,9 @@ func (db *postgres) DropIndexSql(tableName string, index *core.Index) string {
} }
func (db *postgres) IsColumnExist(tableName, colName string) (bool, error) { func (db *postgres) IsColumnExist(tableName, colName string) (bool, error) {
args := []interface{}{tableName, colName} args := []interface{}{db.schema, tableName, colName}
query := "SELECT column_name FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = $1" + query := "SELECT column_name FROM INFORMATION_SCHEMA.COLUMNS WHERE table_schema = $1 AND table_name = $2" +
" AND column_name = $2" " AND column_name = $3"
db.LogSQL(query, args) db.LogSQL(query, args)
rows, err := db.DB().Query(query, args...) rows, err := db.DB().Query(query, args...)

View File

@ -7,11 +7,7 @@ import (
"github.com/go-xorm/core" "github.com/go-xorm/core"
) )
func TestPostgresDialect(t *testing.T) { func TestParsePostgres(t *testing.T) {
TestParse(t)
}
func TestParse(t *testing.T) {
tests := []struct { tests := []struct {
in string in string
expected string expected string
@ -20,10 +16,10 @@ func TestParse(t *testing.T) {
{"postgres://auser:password@localhost:5432/db?sslmode=disable", "db", true}, {"postgres://auser:password@localhost:5432/db?sslmode=disable", "db", true},
{"postgresql://auser:password@localhost:5432/db?sslmode=disable", "db", true}, {"postgresql://auser:password@localhost:5432/db?sslmode=disable", "db", true},
{"postg://auser:password@localhost:5432/db?sslmode=disable", "db", false}, {"postg://auser:password@localhost:5432/db?sslmode=disable", "db", false},
{"postgres://auser:pass with space@localhost:5432/db?sslmode=disable", "db", true}, //{"postgres://auser:pass with space@localhost:5432/db?sslmode=disable", "db", true},
{"postgres:// auser : password@localhost:5432/db?sslmode=disable", "db", true}, //{"postgres:// auser : password@localhost:5432/db?sslmode=disable", "db", true},
{"postgres://%20auser%20:pass%20with%20space@localhost:5432/db?sslmode=disable", "db", true}, {"postgres://%20auser%20:pass%20with%20space@localhost:5432/db?sslmode=disable", "db", true},
{"postgres://auser:パスワード@localhost:5432/データベース?sslmode=disable", "データベース", true}, //{"postgres://auser:パスワード@localhost:5432/データベース?sslmode=disable", "データベース", true},
{"dbname=db sslmode=disable", "db", true}, {"dbname=db sslmode=disable", "db", true},
{"user=auser password=password dbname=db sslmode=disable", "db", true}, {"user=auser password=password dbname=db sslmode=disable", "db", true},
{"", "db", false}, {"", "db", false},