Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit fa67a24

Browse files
committed
Bring in Peter's changes...finally :(
1 parent e9cd0f2 commit fa67a24

File tree

10 files changed

+1324
-247
lines changed

10 files changed

+1324
-247
lines changed

src/interfaces/jdbc/postgresql/Connection.java

+9-247
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
*/
2929
public class Connection implements java.sql.Connection
3030
{
31-
private PG_Stream pg_stream;
31+
protected PG_Stream pg_stream;
3232

3333
private String PG_HOST;
3434
private int PG_PORT;
@@ -591,256 +591,18 @@ public String getUserName() throws SQLException
591591
{
592592
return PG_USER;
593593
}
594-
}
595-
596-
// ***********************************************************************
597-
598-
// This class handles all the Streamed I/O for a postgresql connection
599-
class PG_Stream
600-
{
601-
private Socket connection;
602-
private InputStream pg_input;
603-
private OutputStream pg_output;
604-
605-
/**
606-
* Constructor: Connect to the PostgreSQL back end and return
607-
* a stream connection.
608-
*
609-
* @param host the hostname to connect to
610-
* @param port the port number that the postmaster is sitting on
611-
* @exception IOException if an IOException occurs below it.
612-
*/
613-
public PG_Stream(String host, int port) throws IOException
614-
{
615-
connection = new Socket(host, port);
616-
pg_input = connection.getInputStream();
617-
pg_output = connection.getOutputStream();
618-
}
619-
620-
/**
621-
* Sends a single character to the back end
622-
*
623-
* @param val the character to be sent
624-
* @exception IOException if an I/O error occurs
625-
*/
626-
public void SendChar(int val) throws IOException
627-
{
628-
pg_output.write(val);
629-
}
630-
631-
/**
632-
* Sends an integer to the back end
633-
*
634-
* @param val the integer to be sent
635-
* @param siz the length of the integer in bytes (size of structure)
636-
* @exception IOException if an I/O error occurs
637-
*/
638-
public void SendInteger(int val, int siz) throws IOException
639-
{
640-
byte[] buf = new byte[siz];
641-
642-
while (siz-- > 0)
643-
{
644-
buf[siz] = (byte)(val & 0xff);
645-
val >>= 8;
646-
}
647-
Send(buf);
648-
}
649-
650-
/**
651-
* Send an array of bytes to the backend
652-
*
653-
* @param buf The array of bytes to be sent
654-
* @exception IOException if an I/O error occurs
655-
*/
656-
public void Send(byte buf[]) throws IOException
657-
{
658-
pg_output.write(buf);
659-
}
660-
661-
/**
662-
* Send an exact array of bytes to the backend - if the length
663-
* has not been reached, send nulls until it has.
664-
*
665-
* @param buf the array of bytes to be sent
666-
* @param siz the number of bytes to be sent
667-
* @exception IOException if an I/O error occurs
668-
*/
669-
public void Send(byte buf[], int siz) throws IOException
670-
{
671-
int i;
672-
673-
pg_output.write(buf, 0, (buf.length < siz ? buf.length : siz));
674-
if (buf.length < siz)
675-
{
676-
for (i = buf.length ; i < siz ; ++i)
677-
{
678-
pg_output.write(0);
679-
}
680-
}
681-
}
682-
683-
/**
684-
* Receives a single character from the backend
685-
*
686-
* @return the character received
687-
* @exception SQLException if an I/O Error returns
688-
*/
689-
public int ReceiveChar() throws SQLException
690-
{
691-
int c = 0;
692-
693-
try
694-
{
695-
c = pg_input.read();
696-
if (c < 0) throw new IOException("EOF");
697-
} catch (IOException e) {
698-
throw new SQLException("Error reading from backend: " + e.toString());
699-
}
700-
return c;
701-
}
702-
703-
/**
704-
* Receives an integer from the backend
705-
*
706-
* @param siz length of the integer in bytes
707-
* @return the integer received from the backend
708-
* @exception SQLException if an I/O error occurs
709-
*/
710-
public int ReceiveInteger(int siz) throws SQLException
711-
{
712-
int n = 0;
713-
714-
try
715-
{
716-
for (int i = 0 ; i < siz ; i++)
717-
{
718-
int b = pg_input.read();
719-
720-
if (b < 0)
721-
throw new IOException("EOF");
722-
n = n | (b >> (8 * i)) ;
723-
}
724-
} catch (IOException e) {
725-
throw new SQLException("Error reading from backend: " + e.toString());
726-
}
727-
return n;
728-
}
729-
730-
/**
731-
* Receives a null-terminated string from the backend. Maximum of
732-
* maxsiz bytes - if we don't see a null, then we assume something
733-
* has gone wrong.
734-
*
735-
* @param maxsiz maximum length of string
736-
* @return string from back end
737-
* @exception SQLException if an I/O error occurs
738-
*/
739-
public String ReceiveString(int maxsiz) throws SQLException
740-
{
741-
byte[] rst = new byte[maxsiz];
742-
int s = 0;
743-
744-
try
745-
{
746-
while (s < maxsiz)
747-
{
748-
int c = pg_input.read();
749-
if (c < 0)
750-
throw new IOException("EOF");
751-
else if (c == 0)
752-
break;
753-
else
754-
rst[s++] = (byte)c;
755-
}
756-
if (s >= maxsiz)
757-
throw new IOException("Too Much Data");
758-
} catch (IOException e) {
759-
throw new SQLException("Error reading from backend: " + e.toString());
760-
}
761-
String v = new String(rst, 0, s);
762-
return v;
763-
}
764594

765595
/**
766-
* Read a tuple from the back end. A tuple is a two dimensional
767-
* array of bytes
768-
*
769-
* @param nf the number of fields expected
770-
* @param bin true if the tuple is a binary tuple
771-
* @return null if the current response has no more tuples, otherwise
772-
* an array of strings
773-
* @exception SQLException if a data I/O error occurs
774-
*/
775-
public byte[][] ReceiveTuple(int nf, boolean bin) throws SQLException
776-
{
777-
int i, bim = (nf + 7)/8;
778-
byte[] bitmask = Receive(bim);
779-
byte[][] answer = new byte[nf][0];
780-
781-
int whichbit = 0x80;
782-
int whichbyte = 0;
783-
784-
for (i = 0 ; i < nf ; ++i)
785-
{
786-
boolean isNull = ((bitmask[whichbyte] & whichbit) == 0);
787-
whichbit >>= 1;
788-
if (whichbit == 0)
789-
{
790-
++whichbyte;
791-
whichbit = 0x80;
792-
}
793-
if (isNull)
794-
answer[i] = null;
795-
else
796-
{
797-
int len = ReceiveInteger(4);
798-
if (!bin)
799-
len -= 4;
800-
if (len < 0)
801-
len = 0;
802-
answer[i] = Receive(len);
803-
}
804-
}
805-
return answer;
806-
}
807-
808-
/**
809-
* Reads in a given number of bytes from the backend
810-
*
811-
* @param siz number of bytes to read
812-
* @return array of bytes received
813-
* @exception SQLException if a data I/O error occurs
814-
*/
815-
private byte[] Receive(int siz) throws SQLException
816-
{
817-
byte[] answer = new byte[siz];
818-
int s = 0;
819-
820-
try
821-
{
822-
while (s < siz)
823-
{
824-
int w = pg_input.read(answer, s, siz - s);
825-
if (w < 0)
826-
throw new IOException("EOF");
827-
s += w;
828-
}
829-
} catch (IOException e) {
830-
throw new SQLException("Error reading from backend: " + e.toString());
831-
}
832-
return answer;
833-
}
834-
835-
/**
836-
* Closes the connection
596+
* This method is not part of the Connection interface. Its is an extension
597+
* that allows access to the PostgreSQL Large Object API
837598
*
838-
* @exception IOException if a IO Error occurs
599+
* @return PGlobj class that implements the API
839600
*/
840-
public void close() throws IOException
601+
public PGlobj getLargeObjectAPI() throws SQLException
841602
{
842-
pg_output.close();
843-
pg_input.close();
844-
connection.close();
603+
return new PGlobj(this);
845604
}
846605
}
606+
607+
// ***********************************************************************
608+

0 commit comments

Comments
 (0)