+void pqloadtable(char *tablename, PQQueryHandler init, PQQueryHandler data, PQQueryHandler fini, void *tag)
+{
+ pqtableloaderinfo_s *tli;
+
+ tli=(pqtableloaderinfo_s *)nsmalloc(POOL_PQSQL, sizeof(pqtableloaderinfo_s));
+ tli->tablename=getsstring(tablename, 100);
+ tli->init=init;
+ tli->data=data;
+ tli->fini=fini;
+ tli->tag=tag;
+ pqasyncquery(pqstartloadtable, tli, "SELECT COUNT(*) FROM %s", tli->tablename->content);
+}
+
+void pqstartloadtable(PGconn *dbconn, void *arg)
+{
+ PGresult *res;
+ unsigned long i, count, tablecrc;
+ pqtableloaderinfo_s *tli = arg;
+
+ res = PQgetResult(dbconn);
+
+ if (PQresultStatus(res) != PGRES_TUPLES_OK && PQresultStatus(res) != PGRES_COMMAND_OK) {
+ Error("pqsql", ERR_ERROR, "Error getting row count for %s.", tli->tablename->content);
+ return;
+ }
+
+ if (PQnfields(res) != 1) {
+ Error("pqsql", ERR_ERROR, "Count query format error for %s.", tli->tablename->content);
+ return;
+ }
+
+ tablecrc=crc32(tli->tablename->content);
+ count=strtoul(PQgetvalue(res, 0, 0), NULL, 10);
+ PQclear(res);
+
+ Error("pqsql", ERR_INFO, "Found %lu entries in table %s, scheduling load.", count, tli->tablename->content);
+
+ pqasyncquery(tli->init, tli->tag, "BEGIN");
+ pqasyncquery(NULL, NULL, "DECLARE table%lx%lx CURSOR FOR SELECT * FROM %s", tablecrc, count, tli->tablename->content);
+
+ for (i=0;(count - i) > 1000; i+=1000)
+ pqasyncquery(tli->data, tli->tag, "FETCH 1000 FROM table%lx%lx", tablecrc, count);
+
+ pqasyncquery(tli->data, tli->tag, "FETCH ALL FROM table%lx%lx", tablecrc, count);
+
+ pqasyncquery(NULL, NULL, "CLOSE table%lx%lx", tablecrc, count);
+ pqasyncquery(tli->fini, tli->tag, "COMMIT");
+
+ freesstring(tli->tablename);
+ nsfree(POOL_PQSQL, tli);
+}
+