tableInd = findTableByName(tblinfo, numTables,
indinfo[i].indrelname);
- indkey = atoi(indinfo[i].indkey) - 1;
- if (indkey == ObjectIdAttributeNumber - 1)
- attname = "oid";
- else
- attname = tblinfo[tableInd].attnames[indkey];
+
if (strcmp(indinfo[i].indproc,"0") == 0) {
funcname = NULL;
} else {
"where pg_proc.oid = '%s'::oid",
indinfo[i].indproc);
res = PQexec(g_conn, q);
+ if ( !res || PQresultStatus(res) != PGRES_TUPLES_OK )
+ {
+ fprintf(stderr,"dumpIndices(): SELECT (funcname) failed\n");
+ exit_nicely(g_conn);
+ }
funcname = strdup(PQgetvalue(res, 0,
PQfnumber(res,"proname")));
PQclear(res);
}
+
+ /* convert opclass oid(s) into names */
+ for (nclass = 0; nclass < INDEX_MAX_KEYS; nclass++)
+ {
+ indclass = atoi(indinfo[i].indclass[nclass]);
+ if ( indclass == 0 )
+ break;
+ sprintf(q,
+ "SELECT opcname from pg_opclass "
+ "where pg_opclass.oid = '%u'::oid",
+ indclass);
+ res = PQexec(g_conn, q);
+ if ( !res || PQresultStatus(res) != PGRES_TUPLES_OK )
+ {
+ fprintf(stderr,"dumpIndices(): SELECT (classname) failed\n");
+ exit_nicely(g_conn);
+ }
+ classname[nclass] = strdup(PQgetvalue(res, 0,
+ PQfnumber(res,"opcname")));
+ PQclear(res);
+ }
+
+ if ( funcname && nclass != 1 )
+ {
+ fprintf(stderr,"dumpIndices(): Must be exactly one OpClass "
+ "for functional index %s\n", indinfo[i].indexrelname);
+ exit_nicely(g_conn);
+ }
+
+ /* convert attribute numbers into attribute list */
+ for (k = 0, attlist[0] = 0; k < INDEX_MAX_KEYS; k++)
+ {
+ char * attname;
+
+ indkey = atoi(indinfo[i].indkey[k]);
+ if ( indkey == 0 )
+ break;
+ indkey--;
+ if (indkey == ObjectIdAttributeNumber - 1)
+ attname = "oid";
+ else
+ attname = tblinfo[tableInd].attnames[indkey];
+ if ( funcname )
+ sprintf (attlist + strlen(attlist), "%s%s",
+ ( k == 0 ) ? "" : ", ", attname);
+ else
+ {
+ if ( k >= nclass )
+ {
+ fprintf(stderr,"dumpIndices(): OpClass not found for "
+ "attribute %s of index %s\n",
+ attname, indinfo[i].indexrelname);
+ exit_nicely(g_conn);
+ }
+ sprintf (attlist + strlen(attlist), "%s%s %s",
+ ( k == 0 ) ? "" : ", ", attname, classname[k]);
+ free (classname[k]);
+ }
+ }
if (!tablename || (!strcmp(indinfo[i].indrelname,tablename))) {
indinfo[i].indrelname,
indinfo[i].indamname);
if (funcname) {
- sprintf(q, "%s %s(%s) %s);\n",
- q,funcname, attname, indinfo[i].indclassname);
+ sprintf(q, "%s %s (%s) %s );\n",
+ q, funcname, attlist, classname[0]);
free(funcname);
+ free(classname[0]);
} else
- sprintf(q, "%s %s %s);\n",
- q,attname,indinfo[i].indclassname);
+ sprintf(q, "%s %s );\n",
+ q, attlist);
fputs(q,fout);
}