Avoid allocating strings in common message types
This commit is contained in:
@@ -199,25 +199,7 @@ func connect(ctx context.Context, config *Config, fallbackConfig *FallbackConfig
|
|||||||
// handled by ReceiveMessage
|
// handled by ReceiveMessage
|
||||||
case *pgproto3.ErrorResponse:
|
case *pgproto3.ErrorResponse:
|
||||||
pgConn.conn.Close()
|
pgConn.conn.Close()
|
||||||
return nil, &PgError{
|
return nil, errorResponseToPgError(msg)
|
||||||
Severity: msg.Severity,
|
|
||||||
Code: msg.Code,
|
|
||||||
Message: msg.Message,
|
|
||||||
Detail: msg.Detail,
|
|
||||||
Hint: msg.Hint,
|
|
||||||
Position: msg.Position,
|
|
||||||
InternalPosition: msg.InternalPosition,
|
|
||||||
InternalQuery: msg.InternalQuery,
|
|
||||||
Where: msg.Where,
|
|
||||||
SchemaName: msg.SchemaName,
|
|
||||||
TableName: msg.TableName,
|
|
||||||
ColumnName: msg.ColumnName,
|
|
||||||
DataTypeName: msg.DataTypeName,
|
|
||||||
ConstraintName: msg.ConstraintName,
|
|
||||||
File: msg.File,
|
|
||||||
Line: msg.Line,
|
|
||||||
Routine: msg.Routine,
|
|
||||||
}
|
|
||||||
default:
|
default:
|
||||||
pgConn.conn.Close()
|
pgConn.conn.Close()
|
||||||
return nil, errors.New("unexpected message")
|
return nil, errors.New("unexpected message")
|
||||||
@@ -348,7 +330,7 @@ func (pgConn *PgConn) ParameterStatus(key string) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// CommandTag is the result of an Exec function
|
// CommandTag is the result of an Exec function
|
||||||
type CommandTag string
|
type CommandTag []byte
|
||||||
|
|
||||||
// RowsAffected returns the number of rows affected. If the CommandTag was not
|
// RowsAffected returns the number of rows affected. If the CommandTag was not
|
||||||
// for a row affecting command (e.g. "CREATE TABLE") then it returns 0.
|
// for a row affecting command (e.g. "CREATE TABLE") then it returns 0.
|
||||||
@@ -362,6 +344,10 @@ func (ct CommandTag) RowsAffected() int64 {
|
|||||||
return n
|
return n
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (ct CommandTag) String() string {
|
||||||
|
return string(ct)
|
||||||
|
}
|
||||||
|
|
||||||
// SendExec enqueues the execution of sql via the PostgreSQL simple query protocol. sql may contain multiple queries.
|
// SendExec enqueues the execution of sql via the PostgreSQL simple query protocol. sql may contain multiple queries.
|
||||||
// Execution is implicitly wrapped in a transactions unless a transaction is already in progress or sql contains
|
// Execution is implicitly wrapped in a transactions unless a transaction is already in progress or sql contains
|
||||||
// transaction control statements. It is only sent to the PostgreSQL server when Flush is called.
|
// transaction control statements. It is only sent to the PostgreSQL server when Flush is called.
|
||||||
@@ -511,6 +497,7 @@ func (pgConn *PgConn) SendExecParams(sql string, paramValues [][]byte, paramOIDs
|
|||||||
}
|
}
|
||||||
|
|
||||||
pgConn.batchBuf = appendParse(pgConn.batchBuf, "", sql, paramOIDs)
|
pgConn.batchBuf = appendParse(pgConn.batchBuf, "", sql, paramOIDs)
|
||||||
|
pgConn.batchBuf = appendDescribe(pgConn.batchBuf, 'S', "")
|
||||||
pgConn.batchBuf = appendBind(pgConn.batchBuf, "", "", paramFormats, paramValues, resultFormats)
|
pgConn.batchBuf = appendBind(pgConn.batchBuf, "", "", paramFormats, paramValues, resultFormats)
|
||||||
pgConn.batchBuf = appendExecute(pgConn.batchBuf, "", 0)
|
pgConn.batchBuf = appendExecute(pgConn.batchBuf, "", 0)
|
||||||
pgConn.batchBuf = appendSync(pgConn.batchBuf)
|
pgConn.batchBuf = appendSync(pgConn.batchBuf)
|
||||||
@@ -530,6 +517,7 @@ func (pgConn *PgConn) SendExecParams(sql string, paramValues [][]byte, paramOIDs
|
|||||||
//
|
//
|
||||||
// Query is only sent to the PostgreSQL server when Flush is called.
|
// Query is only sent to the PostgreSQL server when Flush is called.
|
||||||
func (pgConn *PgConn) SendExecPrepared(stmtName string, paramValues [][]byte, paramFormats []int16, resultFormats []int16) {
|
func (pgConn *PgConn) SendExecPrepared(stmtName string, paramValues [][]byte, paramFormats []int16, resultFormats []int16) {
|
||||||
|
pgConn.batchBuf = appendDescribe(pgConn.batchBuf, 'S', stmtName)
|
||||||
pgConn.batchBuf = appendBind(pgConn.batchBuf, "", stmtName, paramFormats, paramValues, resultFormats)
|
pgConn.batchBuf = appendBind(pgConn.batchBuf, "", stmtName, paramFormats, paramValues, resultFormats)
|
||||||
pgConn.batchBuf = appendExecute(pgConn.batchBuf, "", 0)
|
pgConn.batchBuf = appendExecute(pgConn.batchBuf, "", 0)
|
||||||
pgConn.batchBuf = appendSync(pgConn.batchBuf)
|
pgConn.batchBuf = appendSync(pgConn.batchBuf)
|
||||||
@@ -616,6 +604,12 @@ func (rr *PgResultReader) NextRow() bool {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FieldDescriptions returns the field descriptions for the current result set. The returned slice is only valid until
|
||||||
|
// the PgResultReader is closed.
|
||||||
|
func (rr *PgResultReader) FieldDescriptions() []pgproto3.FieldDescription {
|
||||||
|
return rr.fieldDescriptions
|
||||||
|
}
|
||||||
|
|
||||||
// Values returns the current row data. NextRow must have been previously been called. The returned [][]byte is only
|
// Values returns the current row data. NextRow must have been previously been called. The returned [][]byte is only
|
||||||
// valid until the next NextRow call or the PgResultReader is closed. However, the underlying byte data is safe to
|
// valid until the next NextRow call or the PgResultReader is closed. However, the underlying byte data is safe to
|
||||||
// retain a reference to and mutate.
|
// retain a reference to and mutate.
|
||||||
@@ -914,23 +908,23 @@ func (pgConn *PgConn) Prepare(ctx context.Context, name, sql string, paramOIDs [
|
|||||||
|
|
||||||
func errorResponseToPgError(msg *pgproto3.ErrorResponse) *PgError {
|
func errorResponseToPgError(msg *pgproto3.ErrorResponse) *PgError {
|
||||||
return &PgError{
|
return &PgError{
|
||||||
Severity: msg.Severity,
|
Severity: string(msg.Severity),
|
||||||
Code: msg.Code,
|
Code: string(msg.Code),
|
||||||
Message: msg.Message,
|
Message: string(msg.Message),
|
||||||
Detail: msg.Detail,
|
Detail: string(msg.Detail),
|
||||||
Hint: msg.Hint,
|
Hint: string(msg.Hint),
|
||||||
Position: msg.Position,
|
Position: msg.Position,
|
||||||
InternalPosition: msg.InternalPosition,
|
InternalPosition: msg.InternalPosition,
|
||||||
InternalQuery: msg.InternalQuery,
|
InternalQuery: string(msg.InternalQuery),
|
||||||
Where: msg.Where,
|
Where: string(msg.Where),
|
||||||
SchemaName: msg.SchemaName,
|
SchemaName: string(msg.SchemaName),
|
||||||
TableName: msg.TableName,
|
TableName: string(msg.TableName),
|
||||||
ColumnName: msg.ColumnName,
|
ColumnName: string(msg.ColumnName),
|
||||||
DataTypeName: msg.DataTypeName,
|
DataTypeName: string(msg.DataTypeName),
|
||||||
ConstraintName: msg.ConstraintName,
|
ConstraintName: string(msg.ConstraintName),
|
||||||
File: msg.File,
|
File: string(msg.File),
|
||||||
Line: msg.Line,
|
Line: msg.Line,
|
||||||
Routine: msg.Routine,
|
Routine: string(msg.Routine),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user