2013-08-08 05:24:38 +00:00
|
|
|
package xorm
|
|
|
|
|
2013-09-28 15:14:42 +00:00
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
2013-08-08 05:24:38 +00:00
|
|
|
|
|
|
|
type postgres struct {
|
2013-09-28 15:14:42 +00:00
|
|
|
dbname string
|
|
|
|
}
|
|
|
|
|
2013-09-30 06:45:34 +00:00
|
|
|
type values map[string]string
|
2013-09-28 15:14:42 +00:00
|
|
|
|
2013-09-30 06:45:34 +00:00
|
|
|
func (vs values) Set(k, v string) {
|
2013-09-28 15:14:42 +00:00
|
|
|
vs[k] = v
|
|
|
|
}
|
|
|
|
|
2013-09-30 06:45:34 +00:00
|
|
|
func (vs values) Get(k string) (v string) {
|
2013-09-28 15:14:42 +00:00
|
|
|
return vs[k]
|
|
|
|
}
|
|
|
|
|
|
|
|
type Error error
|
|
|
|
|
|
|
|
func errorf(s string, args ...interface{}) {
|
|
|
|
panic(Error(fmt.Errorf("pq: %s", fmt.Sprintf(s, args...))))
|
|
|
|
}
|
|
|
|
|
2013-09-30 06:45:34 +00:00
|
|
|
func parseOpts(name string, o values) {
|
2013-09-28 15:14:42 +00:00
|
|
|
if len(name) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
name = strings.TrimSpace(name)
|
|
|
|
|
|
|
|
ps := strings.Split(name, " ")
|
|
|
|
for _, p := range ps {
|
|
|
|
kv := strings.Split(p, "=")
|
|
|
|
if len(kv) < 2 {
|
|
|
|
errorf("invalid option: %q", p)
|
|
|
|
}
|
|
|
|
o.Set(kv[0], kv[1])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *postgres) Init(uri string) error {
|
2013-09-30 06:45:34 +00:00
|
|
|
o := make(values)
|
2013-09-28 15:14:42 +00:00
|
|
|
parseOpts(uri, o)
|
|
|
|
|
|
|
|
db.dbname = o.Get("dbname")
|
|
|
|
if db.dbname == "" {
|
|
|
|
return errors.New("dbname is empty")
|
|
|
|
}
|
|
|
|
return nil
|
2013-08-08 05:24:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (db *postgres) SqlType(c *Column) string {
|
|
|
|
var res string
|
2013-08-08 16:03:33 +00:00
|
|
|
switch t := c.SQLType.Name; t {
|
2013-08-08 05:24:38 +00:00
|
|
|
case TinyInt:
|
2013-08-08 16:03:33 +00:00
|
|
|
res = SmallInt
|
2013-08-08 05:24:38 +00:00
|
|
|
case MediumInt, Int, Integer:
|
2013-08-08 16:03:33 +00:00
|
|
|
return Integer
|
2013-08-08 05:24:38 +00:00
|
|
|
case Serial, BigSerial:
|
|
|
|
c.IsAutoIncrement = true
|
2013-08-08 16:03:33 +00:00
|
|
|
c.Nullable = false
|
|
|
|
res = t
|
2013-08-08 05:24:38 +00:00
|
|
|
case Binary, VarBinary:
|
2013-08-08 16:03:33 +00:00
|
|
|
return Bytea
|
2013-08-08 05:24:38 +00:00
|
|
|
case DateTime:
|
2013-08-08 16:03:33 +00:00
|
|
|
res = TimeStamp
|
2013-08-08 05:24:38 +00:00
|
|
|
case Float:
|
2013-08-08 16:03:33 +00:00
|
|
|
res = Real
|
2013-08-08 05:24:38 +00:00
|
|
|
case TinyText, MediumText, LongText:
|
2013-08-08 16:03:33 +00:00
|
|
|
res = Text
|
2013-08-08 05:24:38 +00:00
|
|
|
case Blob, TinyBlob, MediumBlob, LongBlob:
|
2013-08-08 16:03:33 +00:00
|
|
|
return Bytea
|
2013-08-08 05:24:38 +00:00
|
|
|
case Double:
|
|
|
|
return "DOUBLE PRECISION"
|
|
|
|
default:
|
|
|
|
if c.IsAutoIncrement {
|
2013-08-08 16:03:33 +00:00
|
|
|
return Serial
|
2013-08-08 05:24:38 +00:00
|
|
|
}
|
2013-08-08 16:03:33 +00:00
|
|
|
res = t
|
2013-08-08 05:24:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var hasLen1 bool = (c.Length > 0)
|
|
|
|
var hasLen2 bool = (c.Length2 > 0)
|
|
|
|
if hasLen1 {
|
|
|
|
res += "(" + strconv.Itoa(c.Length) + ")"
|
|
|
|
} else if hasLen2 {
|
|
|
|
res += "(" + strconv.Itoa(c.Length) + "," + strconv.Itoa(c.Length2) + ")"
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *postgres) SupportInsertMany() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *postgres) QuoteStr() string {
|
|
|
|
return "\""
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *postgres) AutoIncrStr() string {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *postgres) SupportEngine() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *postgres) SupportCharset() bool {
|
|
|
|
return false
|
|
|
|
}
|
2013-09-26 07:19:39 +00:00
|
|
|
|
|
|
|
func (db *postgres) IndexOnTable() bool {
|
|
|
|
return false
|
|
|
|
}
|
2013-09-28 15:14:42 +00:00
|
|
|
|
|
|
|
func (db *postgres) IndexCheckSql(tableName, idxName string) (string, []interface{}) {
|
|
|
|
args := []interface{}{tableName, idxName}
|
|
|
|
return `SELECT indexname FROM pg_indexes ` +
|
|
|
|
`WHERE tablename = ? AND indexname = ?`, args
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *postgres) TableCheckSql(tableName string) (string, []interface{}) {
|
|
|
|
args := []interface{}{tableName}
|
|
|
|
return `SELECT tablename FROM pg_tables WHERE 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
|
|
|
|
}
|