LJ Archive
/** PGConnection encapsulates a connection to the backend.
 */.  It wraps around PGconn struct, which libpq uses for the same
purpose.  */

public class PGConnection extends Object
{

public  static final int CON_OK = 0;
public  static final int CON_BAD = 1;

private int PGconnRep;     // will be cast to (PGconn *) in C
private int getlineResult; // -1 = EOF, 0 = done, 1 = more
private boolean copyDone;  // Set at end of getline,
            // reset at endcopy

public PGConnection(String host, String port, String options,
          String tty, String dbName) throws PostgresException
 {
    this.connectDB(host, port, options, tty, dbName);

    if (this.status() != CON_OK)
      {
   String errString = this.errorMessage();
   this.finish();
   this.PGconnRep = 0;
   throw (new PostgresException(errString));
      }
}

protected void finalize()
  {
    if (PGconnRep != 0)
      this.finish();
  }

public synchronized PGResult exec(String query) throws PostgresException
  {
    int resultPtr = nativeExec(query);
    if (resultPtr != 0)
      return new PGResult(resultPtr);
    else
      {
   String errString = this.errorMessage();
   throw new PostgresException(errString);
      }
  }

public synchronized String getline()
  {
    if (!this.copyDone)
      {
   StringBuffer result = new StringBuffer(nativeGetline());
   while (this.getlineResult == 1)
     {
       result.append(nativeGetline());
     }
   if (this.getlineResult == -1)       // EOF
     this.copyDone = true;
   return new String(result);
      }
    else
      return null;
  }

 /**Native methods that are _only_ called from
    other methods of PGConnection*/

private synchronized native void connectDB(String host,
                  String port,
                  String options,
                  String tty, String dbName);
private synchronized native void finish();             // cleans up connection
private synchronized native int nativeExec(String query); // used by exec
private synchronized native  String nativeGetline();      // Used by getline
  /**Native methods that are called from other objects*/
public  synchronized native String db() ;
public  synchronized native String host();
public  synchronized native String options();
public  synchronized native String port();
public  synchronized native String tty();
public  synchronized native  void reset();
public  synchronized native  int status();
public  synchronized native  String errorMessage();
public  synchronized native  void untrace();
public  synchronized native  void trace(String filename);
public  synchronized native  int endcopy();
public  synchronized native  void putline(String string);

  static
  {
    System.loadLibrary("Jgres");
  }

}
LJ Archive