2013-07-30 12:32:38 +00:00
|
|
|
package orm
|
|
|
|
|
2013-08-10 16:15:26 +00:00
|
|
|
import (
|
2013-08-11 14:27:45 +00:00
|
|
|
"fmt"
|
2013-08-10 16:15:26 +00:00
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
|
|
|
var postgresOperators = map[string]string{
|
|
|
|
"exact": "= ?",
|
|
|
|
"iexact": "= UPPER(?)",
|
|
|
|
"contains": "LIKE ?",
|
|
|
|
"icontains": "LIKE UPPER(?)",
|
|
|
|
"gt": "> ?",
|
|
|
|
"gte": ">= ?",
|
|
|
|
"lt": "< ?",
|
|
|
|
"lte": "<= ?",
|
|
|
|
"startswith": "LIKE ?",
|
|
|
|
"endswith": "LIKE ?",
|
|
|
|
"istartswith": "LIKE UPPER(?)",
|
|
|
|
"iendswith": "LIKE UPPER(?)",
|
|
|
|
}
|
|
|
|
|
2013-07-30 12:32:38 +00:00
|
|
|
type dbBasePostgres struct {
|
|
|
|
dbBase
|
|
|
|
}
|
|
|
|
|
2013-08-10 16:15:26 +00:00
|
|
|
var _ dbBaser = new(dbBasePostgres)
|
|
|
|
|
|
|
|
func (d *dbBasePostgres) OperatorSql(operator string) string {
|
|
|
|
return postgresOperators[operator]
|
|
|
|
}
|
|
|
|
|
2013-08-13 09:16:12 +00:00
|
|
|
func (d *dbBasePostgres) GenerateOperatorLeftCol(fi *fieldInfo, operator string, leftCol *string) {
|
2013-08-11 14:27:45 +00:00
|
|
|
switch operator {
|
|
|
|
case "contains", "startswith", "endswith":
|
|
|
|
*leftCol = fmt.Sprintf("%s::text", *leftCol)
|
|
|
|
case "iexact", "icontains", "istartswith", "iendswith":
|
|
|
|
*leftCol = fmt.Sprintf("UPPER(%s::text)", *leftCol)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *dbBasePostgres) SupportUpdateJoin() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *dbBasePostgres) MaxLimit() uint64 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2013-08-10 16:15:26 +00:00
|
|
|
func (d *dbBasePostgres) TableQuote() string {
|
|
|
|
return `"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *dbBasePostgres) ReplaceMarks(query *string) {
|
|
|
|
q := *query
|
|
|
|
num := 0
|
|
|
|
for _, c := range q {
|
|
|
|
if c == '?' {
|
|
|
|
num += 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if num == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
data := make([]byte, 0, len(q)+num)
|
|
|
|
num = 1
|
|
|
|
for i := 0; i < len(q); i++ {
|
|
|
|
c := q[i]
|
|
|
|
if c == '?' {
|
|
|
|
data = append(data, '$')
|
|
|
|
data = append(data, []byte(strconv.Itoa(num))...)
|
|
|
|
num += 1
|
|
|
|
} else {
|
|
|
|
data = append(data, c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*query = string(data)
|
|
|
|
}
|
|
|
|
|
2013-08-11 14:27:45 +00:00
|
|
|
func (d *dbBasePostgres) HasReturningID(mi *modelInfo, query *string) (has bool) {
|
|
|
|
if mi.fields.pk.auto {
|
|
|
|
if query != nil {
|
|
|
|
*query = fmt.Sprintf(`%s RETURNING "%s"`, *query, mi.fields.pk.column)
|
|
|
|
}
|
|
|
|
has = true
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2013-08-10 16:15:26 +00:00
|
|
|
|
2013-07-30 12:32:38 +00:00
|
|
|
func newdbBasePostgres() dbBaser {
|
|
|
|
b := new(dbBasePostgres)
|
|
|
|
b.ins = b
|
|
|
|
return b
|
|
|
|
}
|