Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

discuss: issue #3525 on pgsql, char ? in sql arguments #3528

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions contrib/drivers/pgsql/pgsql_do_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ func (d *Driver) DoFilter(
var index int
// Convert placeholder char '?' to string "$x".
newSql, err = gregex.ReplaceStringFunc(`\?`, sql, func(s string) string {
if _, ok := args[index].(gdb.NoReplacement); ok {
args = append(args[:index], args[index+1:]...)
return s
}
index++
return fmt.Sprintf(`$%d`, index)
})
Expand Down
13 changes: 7 additions & 6 deletions database/gdb/gdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,12 +356,13 @@ type Counter struct {
}

type (
Raw string // Raw is a raw sql that will not be treated as argument but as a direct sql part.
Value = *gvar.Var // Value is the field value type.
Record map[string]Value // Record is the row record of the table.
Result []Record // Result is the row record array.
Map = map[string]interface{} // Map is alias of map[string]interface{}, which is the most common usage map type.
List = []Map // List is type of map array.
Raw string // Raw is a raw sql that will not be treated as argument but as a direct sql part.
Value = *gvar.Var // Value is the field value type.
Record map[string]Value // Record is the row record of the table.
Result []Record // Result is the row record array.
Map = map[string]interface{} // Map is alias of map[string]interface{}, which is the most common usage map type.
List = []Map // List is type of map array.
NoReplacement bool
)

type CatchSQLManager struct {
Expand Down
5 changes: 5 additions & 0 deletions database/gdb/gdb_core.go
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,11 @@ func (c *Core) DoUpdate(ctx context.Context, link Link, table string, data inter

default:
if s, ok := v.(Raw); ok {
placeholders := strings.Index(string(s), "?")
if placeholders != -1 {
// 插入一个空的
args = append([]any{(NoReplacement)(true)}, args...)
}
fields = append(fields, c.QuoteWord(k)+"="+gconv.String(s))
} else {
fields = append(fields, c.QuoteWord(k)+"=?")
Expand Down