Пример #1
0
void PgStatement::PrepareQuery(const ArrayRecord& raParameters)
  {
  wxASSERT_MSG(pgr == NULL, wxT("PgStatement::PrepareQuery() should only get called if pgr is NULL"));

  // Prepare parameter types
  wxString sParamTypes;
  if(raParameters.size() != 0)
    {
    sParamTypes = wxT(" (");
    for(ArrayRecord::const_iterator it = raParameters.begin(); it != raParameters.end(); it++)
      {
      if(it != raParameters.begin())
        sParamTypes += wxT(", ");

      // Translate wxVariant type names into Postgres type names
      wxString sType = it->GetType();
      wxString sPgTypeName = Postgres::GetPgTypeFromVariantType(sType);
      sParamTypes += sPgTypeName;
      }
    sParamTypes += wxT(")");
    }
  else
    sParamTypes = wxT("");

  wxString sPrepareStatement = wxString::Format
    (
    wxT("PREPARE %s%s AS %s"),
    sQueryHandle.c_str(),
    sParamTypes.c_str(),
    sQuery.c_str()
    );

  PGresult * pgrPrepare = PQexec(pg->conn, sPrepareStatement.mbc_str());
  ExecStatusType exs = PQresultStatus(pgrPrepare);
  if(exs != PGRES_COMMAND_OK)
    ThrowPgError();
  if(pgrPrepare != NULL)
    {
    PQclear(pgrPrepare);
    pgr = NULL;
    }
  }
Пример #2
0
PgStatement::PgParameters::PgParameters
  (
  const ArrayRecord& raParameters
  ) :
  paramValues(NULL),
  paramLengths(NULL),
  paramFormats(NULL)
  {
  nParamCount = raParameters.size();
  if(nParamCount > 0)
    {
    paramTypes = new Oid [nParamCount];
    paramValues = new char * [nParamCount];
    paramLengths = new int[nParamCount];
    paramFormats = new int[nParamCount];
    for(int i = 0; i < nParamCount; i++)
      {
      wxString sType = raParameters.at(i).GetType();
      Oid oid = Postgres::GetPgOidFromVariantType(sType);
      paramTypes[i] = oid;
      if(sType == TYPENAME_LONG)
        {
        // Binary data must be in big endian byte order
        unsigned long ul = (unsigned long) raParameters.at(i).GetLong();
        unsigned long ulBigEndian = htonl(ul);
        long * pl = (long *) malloc(sizeof(long));
        paramValues[i] = (char *) pl;
        *pl = ulBigEndian;
        paramLengths[i] = sizeof(long);
        paramFormats[i] = 1;
        }
      else if(sType == TYPENAME_BOOLEAN)
        {
        bool * pb = (bool *) malloc(sizeof(bool));
        paramValues[i] = (char *) pb;
        *pb = raParameters.at(i).GetBool();
        paramLengths[i] = sizeof(bool);
        paramFormats[i] = 1;
        }
      else if(sType == TYPENAME_DOUBLE)
        {
        // FIXME: Don't know the network conversion for double
        // so we can't pass binary data
        double d = raParameters.at(i).GetDouble();
        char * sz = (char *) malloc(20 * sizeof(char));
        sprintf(sz, "%.10f", d);
        paramValues[i] = sz;
        paramLengths[i] = 0;
        paramFormats[i] = 0; // Treat as text for now
        }
      else if(sType == TYPENAME_STRING)
        {
        wxString s = raParameters.at(i).GetString();

        char * sz = (char *) malloc((s.Length() + 1) * sizeof(char));
        strcpy(sz, s.mbc_str());
        paramValues[i] = sz;
        paramLengths[i] = 0;
        paramFormats[i] = 0;
        }
      else
        throw wx::Exception(wxT("PgStatement::PgParameters::PgParameters() Unknown type '%s'"), sType);
      }
    }
  }