Files
gf/g/database/gdb/db_pgsql.go

54 lines
1.2 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package gdb
import (
"database/sql"
"fmt"
"regexp"
"../../os/glog"
)
// postgresql的适配
// @todo 需要完善replace和save的操作覆盖
// 数据库链接对象
type pgsqlLink struct {
dbLink
}
// 创建SQL操作对象内部采用了lazy link处理
func (l *pgsqlLink) Open (c *ConfigNode) (*sql.DB, error) {
var dbsource string
if c.Linkinfo != "" {
dbsource = c.Linkinfo
} else {
dbsource = fmt.Sprintf("user=%s password=%s host=%s port=%s dbname=%s", c.User, c.Pass, c.Host, c.Port, c.Name)
}
db, err := sql.Open("postgres", dbsource)
if err != nil {
glog.Fatal(err)
}
return db, err
}
// 获得关键字操作符 - 左
func (l *pgsqlLink) getQuoteCharLeft () string {
return "\""
}
// 获得关键字操作符 - 右
func (l *pgsqlLink) getQuoteCharRight () string {
return "\""
}
// 在执行sql之前对sql进行进一步处理
func (l *pgsqlLink) handleSqlBeforeExec(q *string) *string {
reg := regexp.MustCompile("\\?")
index := 0
str := reg.ReplaceAllStringFunc(*q, func (s string) string {
index ++
return fmt.Sprintf("$%d", index)
})
return &str
}