Пример #1
0
void CVTypeDumperImpl::visitArray(TypeLeafKind Leaf, ArrayRecord &AT) {
    printTypeIndex("ElementType", AT.getElementType());
    printTypeIndex("IndexType", AT.getIndexType());
    W.printNumber("SizeOf", AT.getSize());
    W.printString("Name", AT.getName());
    Name = AT.getName();
}
Пример #2
0
bool Postgres::DatabaseExists(const wxString& sDatabaseName)
  {
  if(!IsConnected())
    throw wx::Exception(wxT("Postgres::DatabaseExists() Database not connected."));

  // The query uses field 'datdba' as it's lightweight (int4)
  wxString sqryDatabaseExists = wxT("         \
SELECT datdba                                 \
FROM pg_database                              \
WHERE datname = $1;                           \
");

  // Unfortunately, SQL has a stupid rule about case folding.
  // PG holds table names internally as lower case.
  wxString sDbLower = sDatabaseName.Lower();

  ArrayRecord arParams;
  arParams.push_back(wxVariant(sDbLower));
  ArrayRecordArray ara;
  stc.CacheExecute
    (
    wxT("Postgres::DatabaseExists"),
    sqryDatabaseExists,
    arParams,
    ara
    );

  return (ara.size() > 0)? true : false;
  }
Пример #3
0
Error TypeDumpVisitor::visitKnownRecord(CVType &CVR, ArrayRecord &AT) {
  printTypeIndex("ElementType", AT.getElementType());
  printTypeIndex("IndexType", AT.getIndexType());
  W->printNumber("SizeOf", AT.getSize());
  W->printString("Name", AT.getName());
  return Error::success();
}
Пример #4
0
TypeIndex TypeTableBuilder::writeKnownType(const ArrayRecord &Record) {
  TypeRecordBuilder Builder(Record.getKind());

  Builder.writeTypeIndex(Record.getElementType());
  Builder.writeTypeIndex(Record.getIndexType());
  Builder.writeEncodedUnsignedInteger(Record.getSize());
  Builder.writeNullTerminatedString(Record.getName());

  return writeRecord(Builder);
}
Пример #5
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;
    }
  }
Пример #6
0
bool Postgres::TableExists(const wxString& sTableName)
  {
  if(!IsConnected())
    throw wx::Exception(wxT("Postgres::TableExists() Database not connected."));

  wxString sqryTableExists = wxT("            \
SELECT                                        \
  cl.relname                                  \
FROM                                          \
  pg_class cl                                 \
INNER JOIN                                    \
  pg_namespace ns                             \
ON                                            \
  cl.relnamespace = ns.oid                    \
WHERE                                         \
  ns.nspname = 'public'                       \
AND                                           \
  cl.reltype <> 0                             \
AND                                           \
  cl.relkind = 'r'                            \
AND                                           \
  cl.relname = $1;                            \
");

  ArrayRecord arParams;
  arParams.push_back(wxVariant(sTableName));
  ArrayRecordArray ara;
  stc.CacheExecute
    (
    wxT("Postgres::TableExists"),
    sqryTableExists,
    arParams,
    ara
    );

  return (ara.size() != 0)? true : false;;
  }
Пример #7
0
wxArrayString Postgres::GetTables()
  {
  if(!IsConnected())
    throw wx::Exception(wxT("Postgres::GetTables() Database not connected."));

  wxString sqryTables = wxT("                 \
SELECT                                        \
  relname                                     \
FROM                                          \
  pg_class cl                                 \
INNER JOIN                                    \
  pg_namespace ns                             \
ON                                            \
  cl.relnamespace = ns.oid                    \
WHERE                                         \
  ns.nspname = 'public'                       \
AND                                           \
  cl.reltype <> 0                             \
AND                                           \
  cl.relkind = 'r';                           \
");

  ArrayRecordArray ara;
  stc.CacheExecute
    (
    wxT("Postgres::GetTables"),
    sqryTables,
    ArrayRecord(),
    ara
    );

  ArrayRecord ar;
  for(ArrayRecordArray::iterator it = ara.begin(); it != ara.end(); it++)
    ar.push_back(it->at(0));

  return ar.GetArrayString();
  }
Пример #8
0
Error TypeNameComputer::visitKnownRecord(CVType &CVR, ArrayRecord &AT) {
  Name = AT.getName();
  return Error::success();
}
Пример #9
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);
      }
    }
  }