PSQLConnection

Connection object.

class PSQLConnection (
SocketT = StdSocket
alias logTrace = nop_logger
alias logError = nop_logger
) {
SocketT m_socket;
ubyte[] writeBuffer;
int bufHead;
bool open;
int readBatchSize;
ubyte[] readBatch;
int readyForQueryExpected;
int unflushedRfq;
TransactionStatus tstatus;
int portalCounter;
int m_processId;
int m_cancellationKey;
}

Constructors

this
this(const BackendParams bp, Duration connectTimeout = seconds(10), size_t writeBufSize = 2 * 4096)

Allocate reuired memory, build socket and authorize the connection.

Members

Aliases

InterceptorT
alias InterceptorT = bool delegate(Message msg) @safe nothrow

When this callback returns true, pollMessages will exit it's loop. Interceptor should set err to true if it has encountered some kind of error and wants it to be rethrown as PsqlClientException at the end of pollMessages call. errMsg should be appended with error description.

Functions

cancelRequest
void cancelRequest()

Open new socket and connect it to the same backend as this connection is bound to, send CancelRequest and close the temporary socket.

discard
void discard()

discard write buffer content

flush
void flush()

Flush writeBuffer into the socket. Blocks/yields (according to socket implementation).

getNewPortalName
string getNewPortalName()

Generate next connection-unique portal name.

getNewPreparedName
string getNewPreparedName()

Generate next connection-unique prepared statement name.

pollMessages
void pollMessages(scope InterceptorT interceptor, bool finishOnError = false)

Read messages from the socket in loop until: 1). if finishOnError is set and ErrorResponse is received, function throws PsqlErrorResponseException immediately. 2). ReadyForQuery message is received. 3). interceptor delegate returns true. 4). NotificationResponse received and notificationCallback returned 'true'. Interceptor delegate is used to customize the logic. If the message is not ReadyForQuery, ErrorResponse or Notice\Notification, it is passed to interceptor.

pollMessages
void pollMessages(scope InterceptorT interceptor, out bool error, out Notice errorNotice, bool finishOnError = false)

Same as above, but throws only on serious protocol or socket-level errors.

putBindMessage
void putBindMessage(string portal, string prepared, scope FR formatCodes, scope PR parameters, scope RR resultFormatCodes)

Put Bind message into write buffer.

putBindMessage
void putBindMessage(string portal, string prepared, scope RR resultFormatCodes)

putBindMessage overload for parameterless portals

putBindMessage
void putBindMessage(string portal, string prepared, scope const(const(ubyte)[])[] rawChunks)

putBindMessage overload for already serialized parameters

putCloseMessage
void putCloseMessage(StmtOrPortal closeWhat, string name)

put Close message into write buffer. closeWhat is 'S' for prepared statement and 'P' for portal.

putDescribeMessage
void putDescribeMessage(StmtOrPortal descWhat, string name)

put Close message into write buffer. closeWhat is 'S' for prepared statement and 'P' for portal.

putExecuteMessage
void putExecuteMessage(string portal = "", int maxRows = 0)

non-zero maxRows will generate PortalSuspended messages, wich are currently not handled by dpeq commands

putFlushMessage
void putFlushMessage()

Quote: "The Flush message does not cause any specific output to be generated, but forces the backend to deliver any data pending in its output buffers. A Flush must be sent after any extended-query command except Sync, if the frontend wishes to examine the results of that command before issuing more commands. Without Flush, messages returned by the backend will be combined into the minimum possible number of packets to minimize network overhead."

putQueryMessage
void putQueryMessage(in string query)
void putQueryMessage(in string[] queryChunks)

put Query message (simple query protocol) into the write buffer

putSyncMessage
void putSyncMessage()

Put Sync message into write buffer. Usually you should call this after every portal execute message. Every postSimpleQuery or PSQLConnection.sync MUST be accompanied by getQueryResults call.

read
void read(ubyte[] buf)

Read from socket to buffer buf exactly buf.length bytes. Blocks and throws.

readOneMessage
Message readOneMessage()

read exactly one message from the socket

reserveLen
auto reserveLen()

Reserve space in write buffer for length prefix and return struct that automatically fills it from current buffer offset position.

saveBuffer
auto saveBuffer()

Save write buffer cursor in order to be able to restore it in case of errors. Use it to prevent sending junk to backend when something goes wrong during serialization or message creation.

terminate
void terminate()

Notify backend and close socket.

windupResponseStack
void windupResponseStack()

reads and discards messages from socket until all expected ReadyForQuery messages are received

wrappedSerialize
int wrappedSerialize(scope SerialT m)

extends writeBuffer if serializer 'm' is lacking space (returns -2)

write
int write(T val)

write numeric type T to write buffer

Properties

backendParams
const(BackendParams) backendParams [@property getter]

Backend parameters this connection was constructed from

cancellationKey
int cancellationKey [@property getter]

Cancellation secret. Used in CancelRequest message.

expectedRFQCount
int expectedRFQCount [@property getter]

Number of ReadyForQuery messages that are yet to be received from the backend. May be useful for checking wether getQueryResults would block forever.

isOpen
bool isOpen [@property getter]

Connection is open when it is authorized and socket was alive last time it was checked.

processId
int processId [@property getter]

Backend process ID. Used in CancelRequest message.

socket
SocketT socket [@property getter]

Socket getter.

transactionStatus
TransactionStatus transactionStatus [@property getter]

Transaction status, reported by the last received ReadyForQuery message. For a new connection TransactionStatus.IDLE is returned.

Variables

m_backendParams
BackendParams m_backendParams;

backend params this connection was created with

noticeCallback
void delegate(typeof(this) con, Notice n) nothrow @safe noticeCallback;

NoticeResponse messages will be parsed and passed to this callback during 'pollMessages' call.

notificationCallback
bool delegate(typeof(this) con, Notification n) nothrow @safe notificationCallback;

NotificationResponse messages will be parsed and passed to this callback during 'pollMessages' call. https://www.postgresql.org/docs/9.5/static/sql-notify.html

preparedCounter
int preparedCounter;

counters to generate unique prepared statement and portal ids

Meta