mirror of https://github.com/jackc/pgx.git
Simplify Batch.Queue for prepared statements
parent
af1beca9a5
commit
1f010f412d
22
batch.go
22
batch.go
|
@ -45,9 +45,9 @@ func (b *Batch) Conn() *Conn {
|
||||||
return b.conn
|
return b.conn
|
||||||
}
|
}
|
||||||
|
|
||||||
// Queue queues a query to batch b. parameterOIDs are required if there are
|
// Queue queues a query to batch b. query can be an SQL query or the name of a prepared statement. parameterOIDs and
|
||||||
// parameters and query is not the name of a prepared statement.
|
// resultFormatCodes should be nil if query is a prepared statement. Otherwise, parameterOIDs are required if there are
|
||||||
// resultFormatCodes are required if there is a result.
|
// parameters and resultFormatCodes are required if there is a result.
|
||||||
func (b *Batch) Queue(query string, arguments []interface{}, parameterOIDs []pgtype.OID, resultFormatCodes []int16) {
|
func (b *Batch) Queue(query string, arguments []interface{}, parameterOIDs []pgtype.OID, resultFormatCodes []int16) {
|
||||||
b.items = append(b.items, &batchItem{
|
b.items = append(b.items, &batchItem{
|
||||||
query: query,
|
query: query,
|
||||||
|
@ -95,7 +95,21 @@ func (b *Batch) Send(ctx context.Context) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
if ps != nil {
|
if ps != nil {
|
||||||
batch.ExecPrepared(ps.Name, paramValues, paramFormats, bi.resultFormatCodes)
|
resultFormats := bi.resultFormatCodes
|
||||||
|
if resultFormats == nil {
|
||||||
|
resultFormats = make([]int16, len(ps.FieldDescriptions))
|
||||||
|
for i := range resultFormats {
|
||||||
|
if dt, ok := b.conn.ConnInfo.DataTypeForOID(ps.FieldDescriptions[i].DataType); ok {
|
||||||
|
if _, ok := dt.Value.(pgtype.BinaryDecoder); ok {
|
||||||
|
resultFormats[i] = BinaryFormatCode
|
||||||
|
} else {
|
||||||
|
resultFormats[i] = TextFormatCode
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
batch.ExecPrepared(ps.Name, paramValues, paramFormats, resultFormats)
|
||||||
} else {
|
} else {
|
||||||
oids := make([]uint32, len(parameterOIDs))
|
oids := make([]uint32, len(parameterOIDs))
|
||||||
for i := 0; i < len(parameterOIDs); i++ {
|
for i := 0; i < len(parameterOIDs); i++ {
|
||||||
|
|
|
@ -177,7 +177,7 @@ func TestConnBeginBatchWithPreparedStatement(t *testing.T) {
|
||||||
batch.Queue("ps1",
|
batch.Queue("ps1",
|
||||||
[]interface{}{5},
|
[]interface{}{5},
|
||||||
nil,
|
nil,
|
||||||
[]int16{pgx.BinaryFormatCode},
|
nil,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue