mirror of https://github.com/jackc/pgx.git
Add QueryResultFormats option
parent
93aa913677
commit
2a55a4048a
19
query.go
19
query.go
|
@ -287,12 +287,27 @@ func (c *Conn) getRows(sql string, args []interface{}) *connRows {
|
||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type QueryResultFormats []int16
|
||||||
|
|
||||||
// Query executes sql with args. If there is an error the returned Rows will be returned in an error state. So it is
|
// Query executes sql with args. If there is an error the returned Rows will be returned in an error state. So it is
|
||||||
// allowed to ignore the error returned from Query and handle it in Rows.
|
// allowed to ignore the error returned from Query and handle it in Rows.
|
||||||
func (c *Conn) Query(ctx context.Context, sql string, args ...interface{}) (Rows, error) {
|
func (c *Conn) Query(ctx context.Context, sql string, args ...interface{}) (Rows, error) {
|
||||||
c.lastStmtSent = false
|
c.lastStmtSent = false
|
||||||
// rows = c.getRows(sql, args)
|
// rows = c.getRows(sql, args)
|
||||||
|
|
||||||
|
var resultFormats QueryResultFormats
|
||||||
|
|
||||||
|
optionLoop:
|
||||||
|
for len(args) > 0 {
|
||||||
|
switch arg := args[0].(type) {
|
||||||
|
case QueryResultFormats:
|
||||||
|
resultFormats = arg
|
||||||
|
args = args[1:]
|
||||||
|
default:
|
||||||
|
break optionLoop
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
rows := &connRows{
|
rows := &connRows{
|
||||||
conn: c,
|
conn: c,
|
||||||
startTime: time.Now(),
|
startTime: time.Now(),
|
||||||
|
@ -404,7 +419,8 @@ func (c *Conn) Query(ctx context.Context, sql string, args ...interface{}) (Rows
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
resultFormats := make([]int16, len(ps.FieldDescriptions))
|
if resultFormats == nil {
|
||||||
|
resultFormats = make([]int16, len(ps.FieldDescriptions))
|
||||||
for i := range resultFormats {
|
for i := range resultFormats {
|
||||||
if dt, ok := c.ConnInfo.DataTypeForOID(ps.FieldDescriptions[i].DataType); ok {
|
if dt, ok := c.ConnInfo.DataTypeForOID(ps.FieldDescriptions[i].DataType); ok {
|
||||||
if _, ok := dt.Value.(pgtype.BinaryDecoder); ok {
|
if _, ok := dt.Value.(pgtype.BinaryDecoder); ok {
|
||||||
|
@ -414,6 +430,7 @@ func (c *Conn) Query(ctx context.Context, sql string, args ...interface{}) (Rows
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
c.lastStmtSent = true
|
c.lastStmtSent = true
|
||||||
rows.resultReader = c.pgConn.ExecPrepared(ctx, ps.Name, paramValues, paramFormats, resultFormats)
|
rows.resultReader = c.pgConn.ExecPrepared(ctx, ps.Name, paramValues, paramFormats, resultFormats)
|
||||||
|
|
|
@ -254,15 +254,30 @@ func (c *Conn) QueryContext(ctx context.Context, query string, argsV []driver.Na
|
||||||
|
|
||||||
restrictBinaryToDatabaseSqlTypes(ps)
|
restrictBinaryToDatabaseSqlTypes(ps)
|
||||||
return c.queryPreparedContext(ctx, psname, argsV)
|
return c.queryPreparedContext(ctx, psname, argsV)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// func (c *Conn) execParams(ctx context.Context, sql string, argsV []driver.NamedValue) (*pgconn.ResultReader, error) {
|
||||||
|
// if !c.conn.IsAlive() {
|
||||||
|
// return nil, driver.ErrBadConn
|
||||||
|
// }
|
||||||
|
|
||||||
|
// paramValues := make([][]byte, len(argsV))
|
||||||
|
// for i := 0;i< len(paramValues); i++ {
|
||||||
|
// v := argsV[i].Value
|
||||||
|
// paramValues
|
||||||
|
// }
|
||||||
|
|
||||||
|
// return c.conn.PgConn().ExecParams(ctx, sql,paramValues, nil, nil, nil)
|
||||||
|
// }
|
||||||
|
|
||||||
func (c *Conn) queryPreparedContext(ctx context.Context, name string, argsV []driver.NamedValue) (driver.Rows, error) {
|
func (c *Conn) queryPreparedContext(ctx context.Context, name string, argsV []driver.NamedValue) (driver.Rows, error) {
|
||||||
if !c.conn.IsAlive() {
|
if !c.conn.IsAlive() {
|
||||||
return nil, driver.ErrBadConn
|
return nil, driver.ErrBadConn
|
||||||
}
|
}
|
||||||
|
|
||||||
args := namedValueToInterface(argsV)
|
// TODO - don't always use text
|
||||||
|
args := []interface{}{pgx.QueryResultFormats{0}}
|
||||||
|
args = append(args, namedValueToInterface(argsV)...)
|
||||||
|
|
||||||
rows, err := c.conn.Query(ctx, name, args...)
|
rows, err := c.conn.Query(ctx, name, args...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
Loading…
Reference in New Issue