static List* get_columnnames( List* candidates ) { int proc; int ret; StringInfoData query; /* string for Query */ StringInfoData cols; /* string for Columns */ MemoryContext outerContext; ListCell *cell; IndexCandidate* idxcd; elog( DEBUG3, "IND ADV: get_column_names: ENTER" ); initStringInfo( &query ); initStringInfo( &cols ); foreach( cell, candidates ) /* foreach cell in candidates */ { int i; /*elog (INFO, "Ingresando a foreach");*/ idxcd = (IndexCandidate*)lfirst( cell ); if (idxcd == NULL) { elog( INFO, "idxcd IS NULL" ); continue; /* Or is that fatal enough to break instead? */ } if (!idxcd->idxused) continue; /* pfree() the memory allocated for the previous candidate. FIXME: Avoid * meddling with the internals of a StringInfo, and try to use an API. */ if( cols.len > 0 ) { initStringInfo(&cols); } /*IF col.len>0*/ if( query.len > 0 ) { initStringInfo(&query); } /*IF col.len>0*/ elog(INFO,"reloid:%d", idxcd->reloid); appendStringInfo( &query, "select a.attname from pg_class c,pg_attribute a where c.oid=%d AND a.attrelid = c.oid AND (", idxcd->reloid); /*elog(INFO,"QUERY:%s", query.data);*/ /*elog(INFO,"ncols:%d", idxcd->ncols);*/ for (i = 0; i < idxcd->ncols; ++i) { /*elog(INFO,"i:%d", i);*/ /*elog(INFO,"var attno i:%d", idxcd->varattno[i]);*/ /*elog(INFO,"cols:%s", cols.data);*/ appendStringInfo( &cols, "%s a.attnum=%d", (i>0 ? " OR" : ""), idxcd->varattno[i]); /*elog(INFO,"cols:%s", cols.data);*/ /*elog(INFO,"i:%d", i);*/ elog(INFO,"varattno i:%d", idxcd->varattno[i]); }/* foreach col in varattno*/ /*elog(INFO,"PASA EL FOR");*/ appendStringInfo( &cols, "%s", ")"); /* FIXME: Mention the column names explicitly after the table name. */ appendStringInfo( &query, "%s;", cols.data); elog(INFO,"QUERY:%s", query.data); /*elog(INFO,"LONGITUD:%d", query.len);*/ if( query.len > 0 ) /* if we generated any SQL */ { outerContext = CurrentMemoryContext; if( SPI_connect() == SPI_OK_CONNECT ) { /*elog(INFO,"CONECTADO:%d", query.len);*/ ret=SPI_exec(query.data, 0); proc=SPI_processed; TupleDesc tupdesc=SPI_tuptable->tupdesc; SPITupleTable *tuptable=SPI_tuptable; char buf[8192]; if( ret>0 ) { /*elog(INFO,"EJECUTA:%d", query.len);*/ if( SPI_tuptable != NULL) { //TupleDesc tupdesc; //SPITupleTable *tuptable = SPI_tuptable; //tupdesc = tuptable->tupdesc; elog(INFO,"procantesciclo:%d", proc); int i,j; for(j=0;jvals[j]; if (tuple!=NULL) { for (i=1,buf[0]=0;i<=tupdesc->natts;i++) { char *data; /* cada columna de cada fila*/ data=SPI_getvalue(tuple,tupdesc,i); elog(INFO,"data:%s", data); idxcd->varattnombres[i]=MemoryContextStrdup(outerContext, data); elog(INFO,"valorgc:%s", idxcd->varattnombres[i]); elog(INFO,"indice:%d", cont); cont++; snprintf(buf + strlen (buf), sizeof(buf) - strlen(buf), " %s%s", SPI_getvalue(tuple, tupdesc, i),(i == tupdesc->natts) ? " " : " |"); } /* (i=0,buf[0]=0;inatts;i++)*/ elog (INFO, "EXECQ: %s", buf); } /* if (tuple!=null)*/ else elog( WARNING, "IND ADV: tuple is null." ); } /* (j=0;j 0 )*/ elog (INFO, "if( query.len > 0"); /*if( query.len > 0 )*/ } /* foreach cell in candidates */ elog (INFO, "/* foreach cell in candidates */"); foreach( cell, candidates ) /* foreach cell in candidates */ { MemoryContext oldContext = MemoryContextSwitchTo( outerContext ); MemoryContextSwitchTo( oldContext ); /*elog (INFO, "Ingresando a foreach");*/ idxcd = (IndexCandidate*)lfirst( cell ); if (idxcd == NULL) { elog( INFO, "idxcd IS NULL" ); continue; /* Or is that fatal enough to break instead? */ } if (!idxcd->idxused) continue; int i; for (i = 0; i < idxcd->ncols; ++i) { /*elog(INFO,"cols:%s", cols.data);*/ elog(INFO,"i:%d", i); elog(INFO,"varattnombres i:%s", idxcd->varattnombres[i]); elog(INFO,"varattno i:%d", idxcd->varattno[i]); }/* foreach col in varattno*/ } /* TODO: Propose to -hackers to introduce API to free a StringInfoData . */ if ( query.len > 0 ) pfree( query.data ); elog( DEBUG3, "IND ADV: select: EXIT" ); elog (INFO, "retornando get_columnnames"); return candidates; }