PSQLConnection

Connection object.

class PSQLConnection (
SocketT = StdSocket
alias logTrace = nop_logger
alias logError = nop_logger
) {}

Constructors

this
this(BackendParams bp)

Allocate reuired memory, build socket and authorize the connection.

Members

Aliases

InterceptorT
alias InterceptorT = bool delegate(Message msg, ref bool err, ref string errMsg) 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.

putSimpleQuery
alias putSimpleQuery = putQueryMessage
Undocumented in source.
sync
alias sync = putSyncMessage
Undocumented in source.

Functions

cancelRequest
void cancelRequest()

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

cwrite
int cwrite(string s)
Undocumented in source. Be warned that the author may not have intended to support it.
discard
void discard()

discard write buffer content

ensureOpen
void ensureOpen()
Undocumented in source. Be warned that the author may not have intended to support it.
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.

initialize
void initialize(BackendParams params)
Undocumented in source. Be warned that the author may not have intended to support it.
parseNoticeMessage
void parseNoticeMessage(ubyte[] data, Notice n)
Undocumented in source. Be warned that the author may not have intended to support it.
pollMessages
void pollMessages(InterceptorT interceptor, bool finishOnError)

Read messages from the socket in loop until: 1). if finishOnError is set and ErrorResponse is received, function throws 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. It may set bool 'err' flag to true and append to 'errMsg' string, if delayed throw is required.

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

Put Bind message into write buffer.

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

putBindMessage overload for parameterless portals

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)

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."

putMd5PasswordMessage
void putMd5PasswordMessage(string pw, string user, ubyte[4] salt)
Undocumented in source. Be warned that the author may not have intended to support it.
putParseMessage
void putParseMessage(string prepared, string query, PR ptypes)
Undocumented in source. Be warned that the author may not have intended to support it.
putPasswordMessage
void putPasswordMessage(string pw)
Undocumented in source. Be warned that the author may not have intended to support it.
putQueryMessage
void putQueryMessage(string query)

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

putStartupMessage
void putStartupMessage(BackendParams params)
Undocumented in source. Be warned that the author may not have intended to support it.
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.

putTerminateMessage
void putTerminateMessage()
Undocumented in source. Be warned that the author may not have intended to support it.
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 marshalling 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

wrappedMarsh
int wrappedMarsh(MarshT m)

extends writeBuffer if marshalling functor m is lacking space (returns -2)

write
int write(T val)

write numeric type T to write buffer

write
int write(string s)
Undocumented in source. Be warned that the author may not have intended to support it.

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

bufHead
int bufHead;
Undocumented in source.
m_backendParams
BackendParams m_backendParams;

backend params this connection was created with

m_cancellationKey
int m_cancellationKey;
Undocumented in source.
m_processId
int m_processId;
Undocumented in source.
m_socket
SocketT m_socket;
Undocumented in source.
noticeCallback
void delegate(typeof(this) con, Notice n) nothrow noticeCallback;

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

notificationCallback
bool delegate(typeof(this) con, Notification n) nothrow 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

open
bool open;
Undocumented in source.
portalCounter
int portalCounter;
Undocumented in source.
preparedCounter
int preparedCounter;

counters to generate unique prepared statement and portal ids

readBatch
ubyte[] readBatch;
Undocumented in source.
readBatchSize
int readBatchSize;
Undocumented in source.
readyForQueryExpected
int readyForQueryExpected;
Undocumented in source.
tstatus
TransactionStatus tstatus;
Undocumented in source.
writeBuffer
ubyte[] writeBuffer;
Undocumented in source.

Parameters

SocketT

socket class type to use.

logTrace

alias of a logging function with std.stdio.writef signature. Very verbose byte-stream information will be printed through it.

logError

same as logTrace but for errors.

Meta