Back to index

lightning-sunbird  0.9+nobinonly
Public Member Functions | Public Attributes | Protected Member Functions | Protected Attributes | Private Attributes | Friends
mozSqlConnectionPgsql Class Reference

#include <mozSqlConnectionPgsql.h>

Inheritance diagram for mozSqlConnectionPgsql:
Inheritance graph
[legend]
Collaboration diagram for mozSqlConnectionPgsql:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 mozSqlConnectionPgsql ()
virtual ~mozSqlConnectionPgsql ()
NS_DECL_ISUPPORTS NS_IMETHOD Init (const nsAString &aHost, PRInt32 aPort, const nsAString &aDatabase, const nsAString &aUsername, const nsAString &aPassword)
NS_IMETHOD GetPrimaryKeys (const nsAString &aSchema, const nsAString &aTable, mozISqlResult **_retval)
void init (in AString aHost, in long aPort, in AString aDatabase, in AString aUsername, in AString aPassword)
 Set up the connection.
mozISqlResult executeQuery (in AString aQuery)
 Execute an SQL query synchronously and return the query result.
long executeUpdate (in AString aUpdate)
 Execute an SQL update synchronously and return the number of updated rows.
mozISqlRequest asyncExecuteQuery (in AString aQuery, in nsISupports aContext, in mozISqlRequestObserver aObserver)
 Execute an SQL query asynchronously and return a request.
mozISqlRequest asyncExecuteUpdate (in AString aQuery, in nsISupports aContext, in mozISqlRequestObserver aObserver)
 Execute an SQL update asynchronously and return a request.
void beginTransaction ()
 Begin a transaction.
void commitTransaction ()
 Commit the current transaction.
void rollbackTransaction ()
 Rollback (cancel) the current transaction.
mozISqlResult getPrimaryKeys (in AString aSchema, in AString aTable)
 Return the primary keys of a given table.
NS_IMETHOD Run ()=0
 Defines an entry point for a newly created thread.
void run ()

Public Attributes

NS_DECL_ISUPPORTS
NS_DECL_MOZISQLCONNECTION
friend class 
mozSqlRequest
readonly attribute AString serverVersion
 A string holding the name and/or version info of the database.
readonly attribute AString errorMessage
 The most recent error message.
readonly attribute long lastID
 The ID of the most recently added record.

Protected Member Functions

nsresult Setup ()
virtual nsresult RealExec (const nsAString &aQuery, mozISqlResult **aResult, PRInt32 *aAffectedRows)
virtual nsresult CancelExec ()
virtual nsresult GetIDName (nsAString &aIDName)
nsresult CancelRequest (mozISqlRequest *aRequest)

Protected Attributes

nsString mServerVersion
nsString mErrorMessage
PRInt32 mLastID
PRLockmLock
PRCondVarmCondVar
PRLockmExecLock
nsCOMPtr< nsIThreadmThread
nsCOMArray< mozISqlRequestmRequests
nsCOMPtr< mozISqlRequestmCurrentRequest
PRBool mShutdown
PRBool mWaiting

Private Attributes

PGconn * mConnection
PRInt32 mVersion

Friends

class mozSqlResult

Detailed Description

Definition at line 50 of file mozSqlConnectionPgsql.h.


Constructor & Destructor Documentation

Definition at line 41 of file mozSqlConnectionPgsql.cpp.

Definition at line 46 of file mozSqlConnectionPgsql.cpp.

{
  if (mConnection)
    PQfinish(mConnection);
}

Member Function Documentation

mozISqlRequest mozISqlConnection::asyncExecuteQuery ( in AString  aQuery,
in nsISupports  aContext,
in mozISqlRequestObserver  aObserver 
) [inherited]

Execute an SQL query asynchronously and return a request.

An observer may be used to track when the query has completed.

Parameters:
aQuerythe SQL string of the query to execute
aContextextra argument that will be passed to the observer
aObserverobserver that will be notified when the query is done
Returns:
a request object
mozISqlRequest mozISqlConnection::asyncExecuteUpdate ( in AString  aQuery,
in nsISupports  aContext,
in mozISqlRequestObserver  aObserver 
) [inherited]

Execute an SQL update asynchronously and return a request.

An observer may be used to track when the query has completed.

Parameters:
aQuerythe SQL string of the update to execute
aContextextra argument that will be passed to the observer
aObserverobserver that will be notified when the update is done
Returns:
a request object

Begin a transaction.

Updates made during the transaction will not be made permanent until it is committed using commitTransaction.

nsresult mozSqlConnectionPgsql::CancelExec ( ) [protected, virtual]

Implements mozSqlConnection.

Definition at line 261 of file mozSqlConnectionPgsql.cpp.

{
  if (!PQrequestCancel(mConnection)) {
    CopyUTF8toUTF16(PQerrorMessage(mConnection), mErrorMessage);
    return NS_ERROR_FAILURE;
  }

  return NS_OK;
}

Here is the call graph for this function:

nsresult mozSqlConnection::CancelRequest ( mozISqlRequest aRequest) [protected, inherited]

Definition at line 263 of file mozSqlConnection.cpp.

{
  PR_Lock(mLock);
  if (mCurrentRequest == aRequest)
    CancelExec();
  else {
    if (mRequests.RemoveObject(aRequest))
      ((mozSqlRequest*)aRequest)->mStatus = mozISqlRequest::STATUS_CANCELLED;
  }
  PR_Unlock(mLock);

  return NS_OK;
}

Here is the call graph for this function:

Commit the current transaction.

mozISqlResult mozISqlConnection::executeQuery ( in AString  aQuery) [inherited]

Execute an SQL query synchronously and return the query result.

Parameters:
aQuerythe SQL string of the query to execute
Returns:
the result of the query
long mozISqlConnection::executeUpdate ( in AString  aUpdate) [inherited]

Execute an SQL update synchronously and return the number of updated rows.

Parameters:
aUpdatethe update to execute
Returns:
the result of the query
nsresult mozSqlConnectionPgsql::GetIDName ( nsAString &  aIDName) [protected, virtual]

Implements mozSqlConnection.

Definition at line 272 of file mozSqlConnectionPgsql.cpp.

{
  aIDName.AssignLiteral("OID");
  return NS_OK;
}
NS_IMETHODIMP mozSqlConnectionPgsql::GetPrimaryKeys ( const nsAString &  aSchema,
const nsAString &  aTable,
mozISqlResult **  _retval 
)

Definition at line 85 of file mozSqlConnectionPgsql.cpp.

{
  nsAutoString select;
  nsAutoString from;
  nsAutoString where;
  if (mVersion >= SERVER_VERSION(7,3,0)) {
    select.AssignLiteral("SELECT n.nspname AS TABLE_SCHEM, ");
    from.AssignLiteral(" FROM pg_catalog.pg_namespace n, pg_catalog.pg_class ct, pg_catalog.pg_class ci, pg_catalog.pg_attribute a, pg_catalog.pg_index i");
    where.AssignLiteral(" AND ct.relnamespace = n.oid ");
    if (!aSchema.IsEmpty()) {
      where.Append(NS_LITERAL_STRING(" AND n.nspname = '") + aSchema);
      where.Append(PRUnichar('\''));
    }
  }
  else {
    select.AssignLiteral("SELECT NULL AS TABLE_SCHEM, ");
    from.AssignLiteral(" FROM pg_class ct, pg_class ci, pg_attribute a, pg_index i ");
  }

  if (!aTable.IsEmpty()) {
    where.Append(NS_LITERAL_STRING(" AND ct.relname = '") + aTable);
    where.Append(PRUnichar('\''));
  }

  NS_NAMED_LITERAL_STRING(select2, " ct.relname AS TABLE_NAME, a.attname AS COLUMN_NAME, a.attnum AS KEY_SEQ, ci.relname AS PK_NAME ");
  NS_NAMED_LITERAL_STRING(where2, " WHERE ct.oid=i.indrelid AND ci.oid=i.indexrelid AND a.attrelid=ci.oid AND i.indisprimary ");
  NS_NAMED_LITERAL_STRING(order2, " ORDER BY table_name, pk_name, key_seq");

  return RealExec(select + select2 + from + where2 + where + order2, _retval, nsnull);
}

Here is the call graph for this function:

mozISqlResult mozISqlConnection::getPrimaryKeys ( in AString  aSchema,
in AString  aTable 
) [inherited]

Return the primary keys of a given table.

Parameters:
aSchemathe schema
aTablethe table name of the keys to retrieve
Returns:
the result which holds the keys
NS_IMETHODIMP mozSqlConnectionPgsql::Init ( const nsAString &  aHost,
PRInt32  aPort,
const nsAString &  aDatabase,
const nsAString &  aUsername,
const nsAString &  aPassword 
)

Definition at line 61 of file mozSqlConnectionPgsql.cpp.

{
  if (mConnection)
    return NS_OK;

  if (aPort == -1)
    aPort = 5432;
  char port[11];
  char options[] = "";
  char tty[] = "";
  PR_snprintf(port, 11, "%d", aPort);

  mConnection = PQsetdbLogin(NS_ConvertUCS2toUTF8(aHost).get(),
                             port, options, tty,
                             NS_ConvertUCS2toUTF8(aDatabase).get(),
                             NS_ConvertUCS2toUTF8(aUsername).get(),
                             NS_ConvertUCS2toUTF8(aPassword).get());

  return Setup();
}

Here is the call graph for this function:

void mozISqlConnection::init ( in AString  aHost,
in long  aPort,
in AString  aDatabase,
in AString  aUsername,
in AString  aPassword 
) [inherited]

Set up the connection.

This is called by the SQL service. There is no need to call this method directly.

Parameters:
aHostthe host name.
aPortthe port at which the host is listening.
aDatabasethe real database name to connect to.
aUsernamethe username to connect as.
aPasswordthe password to use in authentification phase.
nsresult mozSqlConnectionPgsql::RealExec ( const nsAString &  aQuery,
mozISqlResult **  aResult,
PRInt32 aAffectedRows 
) [protected, virtual]

Implements mozSqlConnection.

Definition at line 177 of file mozSqlConnectionPgsql.cpp.

{
  if (! mConnection)
    return NS_ERROR_NOT_INITIALIZED;

  PGresult* r;
  r = PQexec(mConnection, NS_ConvertUCS2toUTF8(aQuery).get());
  PRInt32 stat = PQresultStatus(r);

  if (PQstatus(mConnection) == CONNECTION_BAD) {
    PQreset(mConnection);
    nsresult rv = Setup();
    if (NS_FAILED(rv))
      return rv;
    r = PQexec(mConnection, NS_ConvertUCS2toUTF8(aQuery).get());
    stat = PQresultStatus(r);
  }

  if (stat == PGRES_TUPLES_OK) {
    if (!aResult)
      return NS_ERROR_NULL_POINTER;

    static char select1[] = "select t.oid, case when t.typbasetype = 0 then t.typname else (select t2.typname from pg_type t2 where t2.oid=t.typbasetype) end as typname from pg_type t where t.oid in (";
    static char select2[] = "select oid, typname from pg_type where oid in (";
    char* select;
    if (mVersion >= SERVER_VERSION(7,3,0))
      select = select1;
    else
      select = select2;
    PRInt32 columnCount = PQnfields(r);
    char* query = (char*)malloc(strlen(select) + columnCount * 11 + 2);
    strcpy(query, select);
    for (PRInt32 i = 0; i < columnCount; i++) {
      PRInt32 oid = PQftype(r, i);
      char oidStr[11];
      if (i)
        sprintf(oidStr, ",%d", oid);
      else
        sprintf(oidStr, "%d", oid);
      strcat(query, oidStr);
    }
    strcat(query, ")");
    PGresult* types = PQexec(mConnection, query);
    free(query);
    stat = PQresultStatus(types);
    if (stat != PGRES_TUPLES_OK) {
      CopyUTF8toUTF16(PQresultErrorMessage(types), mErrorMessage);
      return NS_ERROR_FAILURE;
    }

    if (*aResult) {
      ((mozSqlResultPgsql*)*aResult)->SetResult(r, types);
      nsresult rv = ((mozSqlResult*)*aResult)->Rebuild();
      if (NS_FAILED(rv))
        return rv;
      NS_ADDREF(*aResult);
    }
    else {
      mozSqlResult* result = new mozSqlResultPgsql(this, aQuery);
      if (! result)
        return NS_ERROR_OUT_OF_MEMORY;
      ((mozSqlResultPgsql*)result)->SetResult(r, types);
      nsresult rv = result->Init();
      if (NS_FAILED(rv))
        return rv;
      NS_ADDREF(*aResult = result);
    }
  }
  else if (stat == PGRES_COMMAND_OK) {
    if (!aAffectedRows)
      return NS_ERROR_NULL_POINTER;
    PR_sscanf(PQcmdTuples(r), "%d", aAffectedRows);
    mLastID = PQoidValue(r);
  }
  else {
    CopyUTF8toUTF16(PQresultErrorMessage(r), mErrorMessage);
    return NS_ERROR_FAILURE;
  }

  return NS_OK;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Rollback (cancel) the current transaction.

void nsIRunnable::run ( ) [inherited]
NS_IMETHOD nsIRunnable::Run ( ) [pure virtual, inherited]

Definition at line 117 of file mozSqlConnectionPgsql.cpp.

{
  if (PQstatus(mConnection) == CONNECTION_BAD) {
    CopyUTF8toUTF16(PQerrorMessage(mConnection), mErrorMessage);
    mConnection = nsnull;
    return NS_ERROR_FAILURE;
  }

  PQsetClientEncoding(mConnection, "UNICODE");

/*
  PGresult* result = PQexec(mConnection, "SET DATESTYLE TO US");
  PRInt32 stat = PQresultStatus(result);
  if (stat != PGRES_COMMAND_OK) {
    CopyUTF8toUTF16(PQresultErrorMessage(result), mErrorMessage);
    PQfinish(mConnection);
    mConnection = nsnull;
    return NS_ERROR_FAILURE;
  }
*/

  PGresult* result = PQexec(mConnection, "select version()");
  PRInt32 stat = PQresultStatus(result);
  if (stat != PGRES_TUPLES_OK) {
    CopyUTF8toUTF16(PQresultErrorMessage(result), mErrorMessage);
    return NS_ERROR_FAILURE;
  }
  char* version = PQgetvalue(result, 0, 0);
  NS_ConvertUTF8toUCS2 buffer(version);
  nsAString::const_iterator start, end, iter;
  buffer.BeginReading(iter);
  buffer.EndReading(end);
  while (iter != end && !nsCRT::IsAsciiSpace(*iter))
    ++iter;
  while (iter != end && nsCRT::IsAsciiSpace(*iter))
    ++iter;
  start = iter;
  while (iter != end && !nsCRT::IsAsciiSpace(*iter))
    ++iter;
  mServerVersion = Substring(start,iter);

  PRInt32 numbers[3] = {0,0,0};
  mServerVersion.BeginReading(iter);
  mServerVersion.EndReading(end);
  for (PRInt32 i = 0; i < 3; i++) {
    start = iter;
    while (iter != end && *iter != PRUnichar('.'))
      ++iter;
    nsAutoString v(Substring(start,iter));
    PRInt32 err;
    numbers[i] = v.ToInteger(&err);
    while (iter != end && *iter == PRUnichar('.'))
      ++iter;
  }
  mVersion = SERVER_VERSION(numbers[0], numbers[1], numbers[2]);

  return NS_OK;
}

Here is the call graph for this function:

Here is the caller graph for this function:


Friends And Related Function Documentation

friend class mozSqlResult [friend, inherited]

Definition at line 64 of file mozSqlConnection.h.


Member Data Documentation

readonly attribute AString mozISqlConnection::errorMessage [inherited]

The most recent error message.

Definition at line 63 of file mozISqlConnection.idl.

The ID of the most recently added record.

Definition at line 68 of file mozISqlConnection.idl.

PRCondVar* mozSqlConnection::mCondVar [protected, inherited]

Definition at line 79 of file mozSqlConnection.h.

Definition at line 78 of file mozSqlConnectionPgsql.h.

Definition at line 83 of file mozSqlConnection.h.

nsString mozSqlConnection::mErrorMessage [protected, inherited]

Definition at line 75 of file mozSqlConnection.h.

PRLock* mozSqlConnection::mExecLock [protected, inherited]

Definition at line 80 of file mozSqlConnection.h.

PRInt32 mozSqlConnection::mLastID [protected, inherited]

Definition at line 76 of file mozSqlConnection.h.

PRLock* mozSqlConnection::mLock [protected, inherited]

Definition at line 78 of file mozSqlConnection.h.

NS_DECL_ISUPPORTS NS_DECL_MOZISQLCONNECTION friend class mozSqlConnection::mozSqlRequest [inherited]

Definition at line 63 of file mozSqlConnection.h.

Definition at line 82 of file mozSqlConnection.h.

Definition at line 74 of file mozSqlConnection.h.

PRBool mozSqlConnection::mShutdown [protected, inherited]

Definition at line 84 of file mozSqlConnection.h.

nsCOMPtr<nsIThread> mozSqlConnection::mThread [protected, inherited]

Definition at line 81 of file mozSqlConnection.h.

Definition at line 79 of file mozSqlConnectionPgsql.h.

PRBool mozSqlConnection::mWaiting [protected, inherited]

Definition at line 85 of file mozSqlConnection.h.

readonly attribute AString mozISqlConnection::serverVersion [inherited]

A string holding the name and/or version info of the database.

Definition at line 58 of file mozISqlConnection.idl.


The documentation for this class was generated from the following files: