2014-04-11 13:06:11 +00:00
|
|
|
package xorm
|
2013-08-08 05:24:38 +00:00
|
|
|
|
2013-09-28 15:14:42 +00:00
|
|
|
import (
|
2013-12-18 03:31:32 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2014-04-11 13:06:11 +00:00
|
|
|
"github.com/go-xorm/core"
|
2014-01-07 09:33:27 +00:00
|
|
|
)
|
2013-09-28 15:14:42 +00:00
|
|
|
|
2014-04-11 13:06:11 +00:00
|
|
|
// func init() {
|
|
|
|
// RegisterDialect("postgres", &postgres{})
|
|
|
|
// }
|
2014-09-06 15:25:46 +00:00
|
|
|
var (
|
|
|
|
postgresReservedWords = map[string]bool{}
|
|
|
|
)
|
2013-10-12 15:16:51 +00:00
|
|
|
|
2014-01-07 09:33:27 +00:00
|
|
|
type postgres struct {
|
2014-04-11 13:06:11 +00:00
|
|
|
core.Base
|
2013-12-17 09:30:05 +00:00
|
|
|
}
|
|
|
|
|
2014-04-18 10:39:07 +00:00
|
|
|
func (db *postgres) Init(d *core.DB, uri *core.Uri, drivername, dataSourceName string) error {
|
|
|
|
return db.Base.Init(d, db, uri, drivername, dataSourceName)
|
2013-08-08 05:24:38 +00:00
|
|
|
}
|
|
|
|
|
2014-04-11 13:06:11 +00:00
|
|
|
func (db *postgres) SqlType(c *core.Column) string {
|
2013-12-18 03:31:32 +00:00
|
|
|
var res string
|
|
|
|
switch t := c.SQLType.Name; t {
|
2014-04-11 13:06:11 +00:00
|
|
|
case core.TinyInt:
|
|
|
|
res = core.SmallInt
|
2014-01-25 02:07:11 +00:00
|
|
|
return res
|
2014-04-11 13:06:11 +00:00
|
|
|
case core.MediumInt, core.Int, core.Integer:
|
2013-12-24 10:18:48 +00:00
|
|
|
if c.IsAutoIncrement {
|
2014-04-11 13:06:11 +00:00
|
|
|
return core.Serial
|
2013-12-24 10:18:48 +00:00
|
|
|
}
|
2014-04-11 13:06:11 +00:00
|
|
|
return core.Integer
|
|
|
|
case core.Serial, core.BigSerial:
|
2013-12-18 03:31:32 +00:00
|
|
|
c.IsAutoIncrement = true
|
|
|
|
c.Nullable = false
|
|
|
|
res = t
|
2014-04-11 13:06:11 +00:00
|
|
|
case core.Binary, core.VarBinary:
|
|
|
|
return core.Bytea
|
|
|
|
case core.DateTime:
|
|
|
|
res = core.TimeStamp
|
|
|
|
case core.TimeStampz:
|
2013-12-18 03:31:32 +00:00
|
|
|
return "timestamp with time zone"
|
2014-04-11 13:06:11 +00:00
|
|
|
case core.Float:
|
|
|
|
res = core.Real
|
2014-06-26 06:35:40 +00:00
|
|
|
case core.TinyText, core.MediumText, core.LongText:
|
2014-04-11 13:06:11 +00:00
|
|
|
res = core.Text
|
2014-06-26 06:35:40 +00:00
|
|
|
case core.Uuid:
|
|
|
|
res = core.Uuid
|
2014-04-11 13:06:11 +00:00
|
|
|
case core.Blob, core.TinyBlob, core.MediumBlob, core.LongBlob:
|
|
|
|
return core.Bytea
|
|
|
|
case core.Double:
|
2013-12-18 03:31:32 +00:00
|
|
|
return "DOUBLE PRECISION"
|
|
|
|
default:
|
|
|
|
if c.IsAutoIncrement {
|
2014-04-11 13:06:11 +00:00
|
|
|
return core.Serial
|
2013-12-18 03:31:32 +00:00
|
|
|
}
|
|
|
|
res = t
|
|
|
|
}
|
|
|
|
|
|
|
|
var hasLen1 bool = (c.Length > 0)
|
|
|
|
var hasLen2 bool = (c.Length2 > 0)
|
2014-05-29 08:53:23 +00:00
|
|
|
if hasLen2 {
|
2013-12-18 03:31:32 +00:00
|
|
|
res += "(" + strconv.Itoa(c.Length) + "," + strconv.Itoa(c.Length2) + ")"
|
2014-05-29 08:53:23 +00:00
|
|
|
} else if hasLen1 {
|
|
|
|
res += "(" + strconv.Itoa(c.Length) + ")"
|
2013-12-18 03:31:32 +00:00
|
|
|
}
|
|
|
|
return res
|
2013-08-08 05:24:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (db *postgres) SupportInsertMany() bool {
|
2013-12-18 03:31:32 +00:00
|
|
|
return true
|
2013-08-08 05:24:38 +00:00
|
|
|
}
|
|
|
|
|
2014-09-06 15:25:46 +00:00
|
|
|
func (db *postgres) IsReserved(name string) bool {
|
|
|
|
_, ok := postgresReservedWords[name]
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *postgres) Quote(name string) string {
|
|
|
|
return "\"" + name + "\""
|
|
|
|
}
|
|
|
|
|
2013-08-08 05:24:38 +00:00
|
|
|
func (db *postgres) QuoteStr() string {
|
2013-12-18 03:31:32 +00:00
|
|
|
return "\""
|
2013-08-08 05:24:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (db *postgres) AutoIncrStr() string {
|
2013-12-18 03:31:32 +00:00
|
|
|
return ""
|
2013-08-08 05:24:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (db *postgres) SupportEngine() bool {
|
2013-12-18 03:31:32 +00:00
|
|
|
return false
|
2013-08-08 05:24:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (db *postgres) SupportCharset() bool {
|
2013-12-18 03:31:32 +00:00
|
|
|
return false
|
2013-08-08 05:24:38 +00:00
|
|
|
}
|
2013-09-26 07:19:39 +00:00
|
|
|
|
|
|
|
func (db *postgres) IndexOnTable() bool {
|
2013-12-18 03:31:32 +00:00
|
|
|
return false
|
2013-09-26 07:19:39 +00:00
|
|
|
}
|
2013-09-28 15:14:42 +00:00
|
|
|
|
|
|
|
func (db *postgres) IndexCheckSql(tableName, idxName string) (string, []interface{}) {
|
2013-12-18 03:31:32 +00:00
|
|
|
args := []interface{}{tableName, idxName}
|
|
|
|
return `SELECT indexname FROM pg_indexes ` +
|
|
|
|
`WHERE tablename = ? AND indexname = ?`, args
|
2013-09-28 15:14:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (db *postgres) TableCheckSql(tableName string) (string, []interface{}) {
|
2013-12-18 03:31:32 +00:00
|
|
|
args := []interface{}{tableName}
|
|
|
|
return `SELECT tablename FROM pg_tables WHERE tablename = ?`, args
|
2013-09-28 15:14:42 +00:00
|
|
|
}
|
|
|
|
|
2014-04-23 06:01:04 +00:00
|
|
|
/*func (db *postgres) ColumnCheckSql(tableName, colName string) (string, []interface{}) {
|
2013-12-18 03:31:32 +00:00
|
|
|
args := []interface{}{tableName, colName}
|
|
|
|
return "SELECT column_name FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = ?" +
|
|
|
|
" AND column_name = ?", args
|
2014-04-23 06:01:04 +00:00
|
|
|
}*/
|
2013-10-12 15:16:51 +00:00
|
|
|
|
2014-06-11 06:01:14 +00:00
|
|
|
func (db *postgres) ModifyColumnSql(tableName string, col *core.Column) string {
|
|
|
|
return fmt.Sprintf("alter table %s ALTER COLUMN %s TYPE %s",
|
|
|
|
tableName, col.Name, db.SqlType(col))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *postgres) DropIndexSql(tableName string, index *core.Index) string {
|
|
|
|
quote := db.Quote
|
|
|
|
//var unique string
|
|
|
|
var idxName string = index.Name
|
|
|
|
if !strings.HasPrefix(idxName, "UQE_") &&
|
|
|
|
!strings.HasPrefix(idxName, "IDX_") {
|
|
|
|
if index.Type == core.UniqueType {
|
|
|
|
idxName = fmt.Sprintf("UQE_%v_%v", tableName, index.Name)
|
|
|
|
} else {
|
|
|
|
idxName = fmt.Sprintf("IDX_%v_%v", tableName, index.Name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("DROP INDEX %v", quote(idxName))
|
|
|
|
}
|
|
|
|
|
2014-04-29 07:20:18 +00:00
|
|
|
func (db *postgres) IsColumnExist(tableName string, col *core.Column) (bool, error) {
|
|
|
|
args := []interface{}{tableName, col.Name}
|
|
|
|
query := "SELECT column_name FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = $1" +
|
|
|
|
" AND column_name = $2"
|
|
|
|
rows, err := db.DB().Query(query, args...)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
2014-06-11 06:01:14 +00:00
|
|
|
return rows.Next(), nil
|
2014-04-29 07:20:18 +00:00
|
|
|
}
|
|
|
|
|
2014-04-11 13:06:11 +00:00
|
|
|
func (db *postgres) GetColumns(tableName string) ([]string, map[string]*core.Column, error) {
|
2014-06-26 06:35:40 +00:00
|
|
|
args := []interface{}{tableName}
|
|
|
|
s := `SELECT column_name, column_default, is_nullable, data_type, character_maximum_length, numeric_precision, numeric_precision_radix ,
|
2014-06-23 13:46:08 +00:00
|
|
|
CASE WHEN p.contype = 'p' THEN true ELSE false END AS primarykey,
|
|
|
|
CASE WHEN p.contype = 'u' THEN true ELSE false END AS uniquekey
|
|
|
|
FROM pg_attribute f
|
|
|
|
JOIN pg_class c ON c.oid = f.attrelid JOIN pg_type t ON t.oid = f.atttypid
|
|
|
|
LEFT JOIN pg_attrdef d ON d.adrelid = c.oid AND d.adnum = f.attnum
|
|
|
|
LEFT JOIN pg_namespace n ON n.oid = c.relnamespace
|
|
|
|
LEFT JOIN pg_constraint p ON p.conrelid = c.oid AND f.attnum = ANY (p.conkey)
|
|
|
|
LEFT JOIN pg_class AS g ON p.confrelid = g.oid
|
|
|
|
LEFT JOIN INFORMATION_SCHEMA.COLUMNS s ON s.column_name=f.attname AND c.relname=s.table_name
|
|
|
|
WHERE c.relkind = 'r'::char AND c.relname = $1 AND f.attnum > 0 ORDER BY f.attnum;`
|
|
|
|
|
2014-06-26 06:35:40 +00:00
|
|
|
rows, err := db.DB().Query(s, args...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
cols := make(map[string]*core.Column)
|
|
|
|
colSeq := make([]string, 0)
|
|
|
|
|
|
|
|
for rows.Next() {
|
|
|
|
col := new(core.Column)
|
|
|
|
col.Indexes = make(map[string]bool)
|
|
|
|
|
|
|
|
var colName, isNullable, dataType string
|
|
|
|
var maxLenStr, colDefault, numPrecision, numRadix *string
|
|
|
|
var isPK, isUnique bool
|
|
|
|
err = rows.Scan(&colName, &colDefault, &isNullable, &dataType, &maxLenStr, &numPrecision, &numRadix, &isPK, &isUnique)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2014-08-28 14:48:01 +00:00
|
|
|
|
|
|
|
//fmt.Println(args, colName, isNullable, dataType, maxLenStr, colDefault, numPrecision, numRadix, isPK, isUnique)
|
2014-06-26 06:35:40 +00:00
|
|
|
var maxLen int
|
|
|
|
if maxLenStr != nil {
|
|
|
|
maxLen, err = strconv.Atoi(*maxLenStr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
col.Name = strings.Trim(colName, `" `)
|
|
|
|
|
|
|
|
if colDefault != nil || isPK {
|
|
|
|
if isPK {
|
|
|
|
col.IsPrimaryKey = true
|
|
|
|
} else {
|
|
|
|
col.Default = *colDefault
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-28 15:04:28 +00:00
|
|
|
if colDefault != nil && strings.HasPrefix(*colDefault, "nextval(") {
|
|
|
|
col.IsAutoIncrement = true
|
|
|
|
}
|
|
|
|
|
2014-06-26 06:35:40 +00:00
|
|
|
col.Nullable = (isNullable == "YES")
|
|
|
|
|
|
|
|
switch dataType {
|
|
|
|
case "character varying", "character":
|
|
|
|
col.SQLType = core.SQLType{core.Varchar, 0, 0}
|
|
|
|
case "timestamp without time zone":
|
|
|
|
col.SQLType = core.SQLType{core.DateTime, 0, 0}
|
|
|
|
case "timestamp with time zone":
|
|
|
|
col.SQLType = core.SQLType{core.TimeStampz, 0, 0}
|
|
|
|
case "double precision":
|
|
|
|
col.SQLType = core.SQLType{core.Double, 0, 0}
|
|
|
|
case "boolean":
|
|
|
|
col.SQLType = core.SQLType{core.Bool, 0, 0}
|
|
|
|
case "time without time zone":
|
|
|
|
col.SQLType = core.SQLType{core.Time, 0, 0}
|
|
|
|
default:
|
|
|
|
col.SQLType = core.SQLType{strings.ToUpper(dataType), 0, 0}
|
|
|
|
}
|
|
|
|
if _, ok := core.SqlTypes[col.SQLType.Name]; !ok {
|
|
|
|
return nil, nil, errors.New(fmt.Sprintf("unkonw colType %v", dataType))
|
|
|
|
}
|
|
|
|
|
|
|
|
col.Length = maxLen
|
|
|
|
|
2014-08-28 14:48:01 +00:00
|
|
|
if col.SQLType.IsText() || col.SQLType.IsTime() {
|
2014-06-26 06:35:40 +00:00
|
|
|
if col.Default != "" {
|
|
|
|
col.Default = "'" + col.Default + "'"
|
|
|
|
} else {
|
|
|
|
if col.DefaultIsEmpty {
|
|
|
|
col.Default = "''"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cols[col.Name] = col
|
|
|
|
colSeq = append(colSeq, col.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
return colSeq, cols, nil
|
2013-10-12 15:16:51 +00:00
|
|
|
}
|
|
|
|
|
2014-04-11 13:06:11 +00:00
|
|
|
func (db *postgres) GetTables() ([]*core.Table, error) {
|
2013-12-18 03:31:32 +00:00
|
|
|
args := []interface{}{}
|
|
|
|
s := "SELECT tablename FROM pg_tables where schemaname = 'public'"
|
2014-04-18 10:39:07 +00:00
|
|
|
|
|
|
|
rows, err := db.DB().Query(s, args...)
|
2013-12-18 03:31:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-04-18 10:39:07 +00:00
|
|
|
defer rows.Close()
|
2013-12-18 03:31:32 +00:00
|
|
|
|
2014-04-11 13:06:11 +00:00
|
|
|
tables := make([]*core.Table, 0)
|
2014-01-07 09:33:27 +00:00
|
|
|
for rows.Next() {
|
2014-04-11 13:06:11 +00:00
|
|
|
table := core.NewEmptyTable()
|
2014-01-07 09:33:27 +00:00
|
|
|
var name string
|
|
|
|
err = rows.Scan(&name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2013-12-18 03:31:32 +00:00
|
|
|
}
|
2014-01-07 09:33:27 +00:00
|
|
|
table.Name = name
|
2013-12-18 03:31:32 +00:00
|
|
|
tables = append(tables, table)
|
|
|
|
}
|
|
|
|
return tables, nil
|
2013-10-12 15:16:51 +00:00
|
|
|
}
|
|
|
|
|
2014-04-11 13:06:11 +00:00
|
|
|
func (db *postgres) GetIndexes(tableName string) (map[string]*core.Index, error) {
|
2013-12-18 03:31:32 +00:00
|
|
|
args := []interface{}{tableName}
|
2014-01-07 09:33:27 +00:00
|
|
|
s := "SELECT indexname, indexdef FROM pg_indexes WHERE schemaname = 'public' and tablename = $1"
|
2013-12-18 03:31:32 +00:00
|
|
|
|
2014-04-18 10:39:07 +00:00
|
|
|
rows, err := db.DB().Query(s, args...)
|
2013-12-18 03:31:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-04-18 10:39:07 +00:00
|
|
|
defer rows.Close()
|
2013-12-18 03:31:32 +00:00
|
|
|
|
2014-04-11 13:06:11 +00:00
|
|
|
indexes := make(map[string]*core.Index, 0)
|
2014-01-07 09:33:27 +00:00
|
|
|
for rows.Next() {
|
2013-12-18 03:31:32 +00:00
|
|
|
var indexType int
|
2014-01-07 09:33:27 +00:00
|
|
|
var indexName, indexdef string
|
2013-12-18 03:31:32 +00:00
|
|
|
var colNames []string
|
2014-01-07 09:33:27 +00:00
|
|
|
err = rows.Scan(&indexName, &indexdef)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
indexName = strings.Trim(indexName, `" `)
|
2014-06-11 06:01:14 +00:00
|
|
|
if strings.HasSuffix(indexName, "_pkey") {
|
|
|
|
continue
|
|
|
|
}
|
2014-01-07 09:33:27 +00:00
|
|
|
if strings.HasPrefix(indexdef, "CREATE UNIQUE INDEX") {
|
2014-04-11 13:06:11 +00:00
|
|
|
indexType = core.UniqueType
|
2014-01-07 09:33:27 +00:00
|
|
|
} else {
|
2014-04-11 13:06:11 +00:00
|
|
|
indexType = core.IndexType
|
2013-12-18 03:31:32 +00:00
|
|
|
}
|
2014-01-07 09:33:27 +00:00
|
|
|
cs := strings.Split(indexdef, "(")
|
|
|
|
colNames = strings.Split(cs[1][0:len(cs[1])-1], ",")
|
|
|
|
|
2013-12-18 03:31:32 +00:00
|
|
|
if strings.HasPrefix(indexName, "IDX_"+tableName) || strings.HasPrefix(indexName, "UQE_"+tableName) {
|
|
|
|
newIdxName := indexName[5+len(tableName) : len(indexName)]
|
|
|
|
if newIdxName != "" {
|
|
|
|
indexName = newIdxName
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-11 13:06:11 +00:00
|
|
|
index := &core.Index{Name: indexName, Type: indexType, Cols: make([]string, 0)}
|
2013-12-18 03:31:32 +00:00
|
|
|
for _, colName := range colNames {
|
|
|
|
index.Cols = append(index.Cols, strings.Trim(colName, `" `))
|
|
|
|
}
|
|
|
|
indexes[index.Name] = index
|
|
|
|
}
|
|
|
|
return indexes, nil
|
2013-10-12 15:16:51 +00:00
|
|
|
}
|
2014-01-07 09:33:27 +00:00
|
|
|
|
2014-04-11 13:06:11 +00:00
|
|
|
func (db *postgres) Filters() []core.Filter {
|
2014-04-11 15:33:56 +00:00
|
|
|
return []core.Filter{&core.IdFilter{}, &core.QuoteFilter{}, &core.SeqFilter{"$", 1}}
|
2014-01-07 09:33:27 +00:00
|
|
|
}
|