Remove QueryArgs
An external SQL builder like github.com/jackc/pgsql should be used instead.
This commit is contained in:
+9
-1
@@ -5,6 +5,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
@@ -504,6 +505,13 @@ func benchmarkWriteNRowsViaInsert(b *testing.B, n int) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type queryArgs []interface{}
|
||||||
|
|
||||||
|
func (qa *queryArgs) Append(v interface{}) string {
|
||||||
|
*qa = append(*qa, v)
|
||||||
|
return "$" + strconv.Itoa(len(*qa))
|
||||||
|
}
|
||||||
|
|
||||||
// note this function is only used for benchmarks -- it doesn't escape tableName
|
// note this function is only used for benchmarks -- it doesn't escape tableName
|
||||||
// or columnNames
|
// or columnNames
|
||||||
func multiInsert(conn *pgx.Conn, tableName string, columnNames []string, rowSrc pgx.CopyFromSource) (int, error) {
|
func multiInsert(conn *pgx.Conn, tableName string, columnNames []string, rowSrc pgx.CopyFromSource) (int, error) {
|
||||||
@@ -512,7 +520,7 @@ func multiInsert(conn *pgx.Conn, tableName string, columnNames []string, rowSrc
|
|||||||
rowCount := 0
|
rowCount := 0
|
||||||
|
|
||||||
sqlBuf := &bytes.Buffer{}
|
sqlBuf := &bytes.Buffer{}
|
||||||
args := make(pgx.QueryArgs, 0)
|
args := make(queryArgs, 0)
|
||||||
|
|
||||||
resetQuery := func() {
|
resetQuery := func() {
|
||||||
sqlBuf.Reset()
|
sqlBuf.Reset()
|
||||||
|
|||||||
@@ -1,29 +0,0 @@
|
|||||||
package pgx
|
|
||||||
|
|
||||||
import (
|
|
||||||
"strconv"
|
|
||||||
)
|
|
||||||
|
|
||||||
// QueryArgs is a container for arguments to an SQL query. It is helpful when
|
|
||||||
// building SQL statements where the number of arguments is variable.
|
|
||||||
type QueryArgs []interface{}
|
|
||||||
|
|
||||||
var placeholders []string
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
placeholders = make([]string, 64)
|
|
||||||
|
|
||||||
for i := 1; i < 64; i++ {
|
|
||||||
placeholders[i] = "$" + strconv.Itoa(i)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Append adds a value to qa and returns the placeholder value for the
|
|
||||||
// argument. e.g. $1, $2, etc.
|
|
||||||
func (qa *QueryArgs) Append(v interface{}) string {
|
|
||||||
*qa = append(*qa, v)
|
|
||||||
if len(*qa) < len(placeholders) {
|
|
||||||
return placeholders[len(*qa)]
|
|
||||||
}
|
|
||||||
return "$" + strconv.Itoa(len(*qa))
|
|
||||||
}
|
|
||||||
-36
@@ -1,36 +0,0 @@
|
|||||||
package pgx_test
|
|
||||||
|
|
||||||
import (
|
|
||||||
"strconv"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/jackc/pgx/v4"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestQueryArgs(t *testing.T) {
|
|
||||||
var qa pgx.QueryArgs
|
|
||||||
|
|
||||||
for i := 1; i < 512; i++ {
|
|
||||||
expectedPlaceholder := "$" + strconv.Itoa(i)
|
|
||||||
placeholder := qa.Append(i)
|
|
||||||
if placeholder != expectedPlaceholder {
|
|
||||||
t.Errorf(`Expected qa.Append to return "%s", but it returned "%s"`, expectedPlaceholder, placeholder)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func BenchmarkQueryArgs(b *testing.B) {
|
|
||||||
for i := 0; i < b.N; i++ {
|
|
||||||
qa := pgx.QueryArgs(make([]interface{}, 0, 16))
|
|
||||||
qa.Append("foo1")
|
|
||||||
qa.Append("foo2")
|
|
||||||
qa.Append("foo3")
|
|
||||||
qa.Append("foo4")
|
|
||||||
qa.Append("foo5")
|
|
||||||
qa.Append("foo6")
|
|
||||||
qa.Append("foo7")
|
|
||||||
qa.Append("foo8")
|
|
||||||
qa.Append("foo9")
|
|
||||||
qa.Append("foo10")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user