gorm/dialect/postgres.go

63 lines
1.3 KiB
Go
Raw Normal View History

2013-11-14 13:35:17 +04:00
package dialect
2013-11-14 14:59:11 +04:00
import (
"database/sql"
"fmt"
"time"
)
type postgres struct {
}
2013-11-16 16:47:25 +04:00
func (s *postgres) BinVar(i int) string {
return fmt.Sprintf("$%v", i)
2013-11-14 14:59:11 +04:00
}
func (s *postgres) SupportLastInsertId() bool {
return false
}
func (d *postgres) SqlTag(column interface{}, size int) string {
switch column.(type) {
case time.Time:
return "timestamp with time zone"
case bool, sql.NullBool:
return "boolean"
case int, int8, int16, int32, uint, uint8, uint16, uint32:
return "integer"
case int64, uint64, sql.NullInt64:
return "bigint"
case float32, float64, sql.NullFloat64:
2013-12-03 04:23:26 +04:00
return "numeric"
2013-11-14 14:59:11 +04:00
case []byte:
return "bytea"
case string, sql.NullString:
if size > 0 && size < 65532 {
return fmt.Sprintf("varchar(%d)", size)
} else {
return "text"
}
default:
panic("Invalid sql type for postgres")
}
}
func (s *postgres) PrimaryKeyTag(column interface{}, size int) string {
switch column.(type) {
case int, int8, int16, int32, uint, uint8, uint16, uint32:
2013-12-06 04:19:40 +04:00
return "serial PRIMARY KEY"
2013-11-14 14:59:11 +04:00
case int64, uint64:
2013-12-06 04:19:40 +04:00
return "bigserial PRIMARY KEY"
2013-11-14 14:59:11 +04:00
default:
panic("Invalid primary key type")
}
}
func (s *postgres) ReturningStr(key string) (str string) {
return fmt.Sprintf("RETURNING \"%v\"", key)
}
func (s *postgres) Quote(key string) (str string) {
return fmt.Sprintf("\"%s\"", key)
}