Class pgj

java.lang.Object
  extended bypgj
All Implemented Interfaces:
java.lang.Runnable

public class pgj
extends java.lang.Object
implements java.lang.Runnable


Nested Class Summary
static class pgj.AuthReply
           
static class pgj.BackendKeyData
           
static class pgj.BindComplete
           
static class pgj.ChunkReadThread
          Reads and parses input stream, storing to queue and printing.
static class pgj.CloseComplete
           
static class pgj.CommandComplete
           
static class pgj.CopyData
           
static class pgj.CopyDone
           
static class pgj.DataRow
           
static class pgj.EmitQueryResponse
           
static class pgj.ErrorResponse
           
static class pgj.FunctionCallResponse
           
static class pgj.NoData
           
static class pgj.NotificationResponse
           
static class pgj.ParameterDescription
           
static class pgj.ParameterStatus
           
static class pgj.ParseComplete
           
static class pgj.PortalSuspended
           
static interface pgj.ProtocolChunk
          tagging interface.
static class pgj.ReadyForQuery
           
static class pgj.RowDescription
           
 
Field Summary
static byte describePortal
           
static byte describeStatement
           
 
Constructor Summary
pgj()
           
 
Method Summary
 void addChunk(pgj.ProtocolChunk it)
          add a chunk to the queue.
 int availableChunks()
          how many ProtocolChunks do we have from the server?
 void bind(java.lang.String portal, java.lang.String statementName, short[] paramFormats, byte[][] parameters, short[] resultFormats)
           
 void bindOrFunc(java.lang.String portal, java.lang.String statementName, short[] paramFormats, byte[][] parameters, short[] resultFormats, byte op)
           
 void cancelRequest(int pid, int key)
          send CancelRequest messsage
 void close(byte mode, java.lang.String name)
           
 void closeConnection()
          send the terminate message and close the secket
 void copyData(byte[] data)
          sends data.
 java.io.OutputStream copyDataStart(int len)
          sends a copy data header.
 void copyDone()
          sends CopyDone message
 void copyFail(java.lang.String message)
          sends CopyFail message
 void describe(byte mode, java.lang.String name)
           
 void execute(java.lang.String name, int rowlimit)
          execute a bound portal
 void flush()
          sends Flush message
 void functionCall(java.lang.String portal, java.lang.String statementName, short[] paramFormats, byte[][] parameters, short[] resultFormats)
           
 pgj.ProtocolChunk getChunk(long timeoutMillis)
          get the first chunk off the queue.
static int intFromByteArray(byte[] a, int p)
           
static void intToByteArray(byte[] a, int pos, int i)
           
static void main(java.lang.String[] args)
           
 void openConnection(java.lang.String server, int port, java.lang.String[] params)
           
 void parse(java.lang.String statementName, java.lang.String query, int[] paramTypes)
           
 void passwordMessage(java.lang.String message)
          sends PasswordMessage message
 void query(java.lang.String message)
          simple Query
 void run()
           
 void sendString(java.lang.String message, byte op)
          send a string for various simple operations
static short shortFromByteArray(byte[] a, int p)
           
static void shortToByteArray(byte[] a, int pos, int i)
           
 void sslRequest()
          send SSLRequest message
 void sync()
          send sync message
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

describeStatement

public static final byte describeStatement
See Also:
Constant Field Values

describePortal

public static final byte describePortal
See Also:
Constant Field Values
Constructor Detail

pgj

public pgj()
Method Detail

openConnection

public void openConnection(java.lang.String server,
                           int port,
                           java.lang.String[] params)
                    throws java.lang.Exception
Parameters:
server - something like "localhost" or "db.a.com"
port - 5432 is common
params - "name","value","name","value",...
Throws:
java.lang.Exception

closeConnection

public void closeConnection()
                     throws java.lang.Exception
send the terminate message and close the secket

Throws:
java.lang.Exception

parse

public void parse(java.lang.String statementName,
                  java.lang.String query,
                  int[] paramTypes)
           throws java.lang.Exception
Parameters:
statementName - null for unnamed statement, otherwise a name for preparing a statement
query - sql query text
paramTypes - oid type of parameters
Throws:
java.lang.Exception

bind

public void bind(java.lang.String portal,
                 java.lang.String statementName,
                 short[] paramFormats,
                 byte[][] parameters,
                 short[] resultFormats)
          throws java.lang.Exception
Parameters:
portal - null for unnamed portal, a name otherwise
statementName - null for unnamed portal, a name previously parse()d otherwise
paramFormats - 0 = text, 1 = binary, {0} = all text, {1} = all binary, null = all text
parameters - data in appropriate format
resultFormats - 0 = text, 1 = binary, {0} = all text, {1} = all binary, null = all text
Throws:
java.lang.Exception

functionCall

public void functionCall(java.lang.String portal,
                         java.lang.String statementName,
                         short[] paramFormats,
                         byte[][] parameters,
                         short[] resultFormats)
                  throws java.lang.Exception
Parameters:
portal - null for unnamed portal, a name otherwise
statementName - null for unnamed portal, a name previously parse()d otherwise
paramFormats - 0 = text, 1 = binary, {0} = all text, {1} = all binary, null = all text
parameters - data in appropriate format
resultFormats - 0 = text, 1 = binary, {0} = all text, {1} = all binary, null = all text
Throws:
java.lang.Exception

bindOrFunc

public void bindOrFunc(java.lang.String portal,
                       java.lang.String statementName,
                       short[] paramFormats,
                       byte[][] parameters,
                       short[] resultFormats,
                       byte op)
                throws java.lang.Exception
Parameters:
portal - null for unnamed portal, a name otherwise
statementName - null for unnamed portal, a name previously parse()d otherwise
paramFormats - 0 = text, 1 = binary, {0} = all text, {1} = all binary, null = all text
parameters - data in appropriate format
resultFormats - 0 = text, 1 = binary, {0} = all text, {1} = all binary, null = all text
op - 'B' or 'F'
Throws:
java.lang.Exception

describe

public void describe(byte mode,
                     java.lang.String name)
              throws java.lang.Exception
Parameters:
mode - describeStatement or describePortal
name - null for unnamed statement or portal, otherwise a name parse()d or bind()ed
Throws:
java.lang.Exception

execute

public void execute(java.lang.String name,
                    int rowlimit)
             throws java.lang.Exception
execute a bound portal

Parameters:
name - null for unnamed portal, otherwise a name previously bind()ed
rowlimit - 0 for no limit, otherwise the maximum number of rows to return
Throws:
java.lang.Exception

close

public void close(byte mode,
                  java.lang.String name)
           throws java.lang.Exception
Throws:
java.lang.Exception

sync

public void sync()
          throws java.lang.Exception
send sync message

Throws:
java.lang.Exception

cancelRequest

public void cancelRequest(int pid,
                          int key)
                   throws java.lang.Exception
send CancelRequest messsage

Parameters:
pid - data received at connect time
key - data received at connect time
Throws:
java.lang.Exception

sslRequest

public void sslRequest()
                throws java.lang.Exception
send SSLRequest message

Throws:
java.lang.Exception

copyData

public void copyData(byte[] data)
              throws java.lang.Exception
sends data. no copy is performed.

Parameters:
data - sent in a CopyData message to db server
Throws:
java.lang.Exception

copyDataStart

public java.io.OutputStream copyDataStart(int len)
                                   throws java.lang.Exception
sends a copy data header. Write the rest on the OutputStream returned.

Parameters:
len - length of data to copy
Returns:
a stream to write the data to. do not close() the stream. do flush() the stream.
Throws:
java.lang.Exception

copyDone

public void copyDone()
              throws java.lang.Exception
sends CopyDone message

Throws:
java.lang.Exception

copyFail

public void copyFail(java.lang.String message)
              throws java.lang.Exception
sends CopyFail message

Parameters:
message - description of error
Throws:
java.lang.Exception

passwordMessage

public void passwordMessage(java.lang.String message)
                     throws java.lang.Exception
sends PasswordMessage message

Parameters:
message - password
Throws:
java.lang.Exception

query

public void query(java.lang.String message)
           throws java.lang.Exception
simple Query

Parameters:
message - sql text
Throws:
java.lang.Exception

sendString

public void sendString(java.lang.String message,
                       byte op)
                throws java.lang.Exception
send a string for various simple operations

Parameters:
message - string to send
op - f,p,Q,(future protocol expansion?)
Throws:
java.lang.Exception

flush

public void flush()
           throws java.lang.Exception
sends Flush message

Throws:
java.lang.Exception

availableChunks

public int availableChunks()
how many ProtocolChunks do we have from the server?

Returns:
number of stored chunks

addChunk

public void addChunk(pgj.ProtocolChunk it)
add a chunk to the queue. probably just called by the thread reading from the socked, but could be used to inject other traffic.

Parameters:
it - a ProtocolChunk

getChunk

public pgj.ProtocolChunk getChunk(long timeoutMillis)
get the first chunk off the queue. Blocks for up to timeoutMillis if there is none

Parameters:
timeoutMillis - max millis to wait()
Returns:
first available ProtocolChunk from the queue

run

public void run()
Specified by:
run in interface java.lang.Runnable

main

public static void main(java.lang.String[] args)

intToByteArray

public static void intToByteArray(byte[] a,
                                  int pos,
                                  int i)

shortToByteArray

public static void shortToByteArray(byte[] a,
                                    int pos,
                                    int i)

intFromByteArray

public static int intFromByteArray(byte[] a,
                                   int p)

shortFromByteArray

public static short shortFromByteArray(byte[] a,
                                       int p)