예제 #1
0
// For ordered, the default should be 0 (if you just need to fill out the args)
// Note ONLY stable_id can be char, all other pk's must be IDType (see code)
Vector *BaseAdaptor_listDbIDs(BaseAdaptor *ba, char *table, char *pk, int ordered) {
  int ok = 1;
  char colName[1024];

  if (pk == NULL) {
    sprintf(colName, "%s_id", table);
  } else {
    strcpy(colName, pk);
  }

  char qStr[1024];
  sprintf(qStr,"SELECT `%s` FROM `%s`", colName, table );

  if ( BaseAdaptor_isMultiSpecies(BaseAdaptor *ba)
      // For now just the multi species because I don't have adaptors in the Class hierarchy
      // && $self->isa('Bio::EnsEMBL::DBSQL::BaseFeatureAdaptor')
      // && !$self->isa('Bio::EnsEMBL::DBSQL::UnmappedObjectAdaptor') 
     ) {
    char tmpStr[1024];
    
    sprintf(tmpStr, "JOIN seq_region USING (seq_region_id) "
                    "JOIN coord_system cs USING (coord_system_id) "
                    "WHERE cs.species_id = "IDFMTSTR, BaseAdaptor_getSpeciesId(ba));

    sprintf(qStr, "%s %s", qStr, tmpStr);
  }

  if (ordered) {
    sprintf(qStr, "%s ORDER BY seq_region_id, seq_region_start", qStr);
  }

  StatementHandle *sth = ba->prepare(ba,qStr,strlen(qStr));

  sth->execute(sth);

  Vector *out = Vector_new();

  if (strcmp(pk, "stable_id")) {
    ResultRow *row;
    while ((row = sth->fetchRow(sth))) {
      char *stableId = row->getStringCopyAt(row, 0);
  
      Vector_addElement(out, stableId);
    }
  } else  {
    IDType *idP;
    ResultRow *row;
    while ((row = sth->fetchRow(sth))) {
      IDType id = row->getLongLongAt(row, 0);
  
      if ((idP = calloc(1,sizeof(IDType))) == NULL) {
        fprintf(stderr, "Failed allocating space for a id\n");      
        ok = 0;
      } else {
        *idP = id;
        Vector_addElement(out, idP);
      }
    }
  }

  if (!ok) {
    Vector_free(out);
    out = NULL;
  }

  return out;
}