00001
00002
00003
00004
00005
00006
00007 #include <stdio.h>
00008 #include <stdlib.h>
00009 #include <libpq-fe.h>
00010 #include <qsqlquery.h>
00011 #include <qcstring.h>
00012 #include "../global.h"
00013 #include "libpq/libpq-fs.h"
00014
00015
00016 void exit_nicely(PGconn *conn);
00017
00018
00019 bool check(PGconn *conn, QString table, QString linkno, QString var, QString oid) {
00020 PGresult *res;
00021
00022 res=PQexec(conn, "select no from "+table+" where no="+linkno);
00023 if (!res || PQresultStatus(res) != PGRES_TUPLES_OK)
00024 {
00025 fprintf(stderr, "res: command failed\n");
00026 PQclear(res);
00027 exit_nicely(conn);
00028 }
00029 int cnt=PQntuples(res);
00030 PQclear(res);
00031 if(cnt>0) {
00032 res=PQexec(conn, "update link set "+var+"=tabid('"+table+"') where oid="+oid);
00033 if (!res || PQresultStatus(res) != PGRES_COMMAND_OK)
00034 {
00035 fprintf(stderr, "res: command failed\n");
00036 PQclear(res);
00037 exit_nicely(conn);
00038 }
00039 PQclear(res);
00040 return false;
00041 }
00042 return true;
00043 }
00044
00045
00046 void
00047 exit_nicely(PGconn *conn)
00048 {
00049 PQfinish(conn);
00050 exit(1);
00051 }
00052
00053 main()
00054 {
00055 char *pghost,
00056 *pgport,
00057 *pgoptions,
00058 *pgtty;
00059 char *dbName;
00060 int nFields;
00061 int i,
00062 j;
00063
00064 PGconn *conn;
00065 PGresult *recs;
00066
00067
00068 pghost = NULL;
00069 pgport = NULL;
00070 pgoptions = NULL;
00071
00072 pgtty = NULL;
00073 dbName = "kaspadev";
00074
00075 conn = PQsetdb(pghost, pgport, pgoptions, pgtty, dbName);
00076
00077 if (PQstatus(conn) == CONNECTION_BAD)
00078 {
00079 fprintf(stderr, "Connection to database '%s' failed.\n", dbName);
00080 fprintf(stderr, "%s", PQerrorMessage(conn));
00081 exit_nicely(conn);
00082 }
00083
00084 recs = PQexec(conn, "select obj_1, obj_2, oid from link");
00085 if (!recs || PQresultStatus(recs) != PGRES_TUPLES_OK)
00086 {
00087 fprintf(stderr, "recs: command failed\n");
00088 PQclear(recs);
00089 exit_nicely(conn);
00090 }
00091
00092 for (i=0; i<PQntuples(recs); i++)
00093 {
00094 if(QString(PQgetvalue(recs, i, 0)).isEmpty() or QString(PQgetvalue(recs, i, 1)).isEmpty())
00095 continue;
00096 fprintf(stderr, "Processing link %i\n", i);
00097
00098 check(conn, "author", PQgetvalue(recs, i, 0), "tab_1", PQgetvalue(recs, i, 2)) &&
00099 check(conn, "note", PQgetvalue(recs, i, 0), "tab_1", PQgetvalue(recs, i, 2)) &&
00100 check(conn, "part", PQgetvalue(recs, i, 0), "tab_1", PQgetvalue(recs, i, 2)) &&
00101 check(conn, "publication", PQgetvalue(recs, i, 0), "tab_1", PQgetvalue(recs, i, 2)) &&
00102 check(conn, "partdata", PQgetvalue(recs, i, 0), "tab_1", PQgetvalue(recs, i, 2));
00103
00104 check(conn, "author", PQgetvalue(recs, i, 1), "tab_2", PQgetvalue(recs, i, 2)) &&
00105 check(conn, "note", PQgetvalue(recs, i, 1), "tab_2", PQgetvalue(recs, i, 2)) &&
00106 check(conn, "part", PQgetvalue(recs, i, 1), "tab_2", PQgetvalue(recs, i, 2)) &&
00107 check(conn, "publication", PQgetvalue(recs, i, 1), "tab_2", PQgetvalue(recs, i, 2)) &&
00108 check(conn, "partdata", PQgetvalue(recs, i, 1), "tab_2", PQgetvalue(recs, i, 2));
00109 }
00110
00111 PQfinish(conn);
00112
00113 return 0;
00114 }