Advertisement
Guest User

Untitled

a guest
May 8th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 17.29 KB | None | 0 0
  1. #include "wet.h"
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <libpq-fe.h>
  5.  
  6.  
  7. // ===================== HELPER FUNCTIONS =============================
  8. int cmpDates (char* date1, char* date2) {
  9.     //returns -1 if date1 < date2, +1 if date1>date2, 0 if date1==date2.
  10.     char yr1[4], yr2[4];
  11.     char mon1[2], mon2[2];
  12.     int i;
  13.     for (i=0; i<4;i++ ) {
  14.         yr1[i] = date1[i];
  15.         yr2[i] = date2[i];
  16.     }
  17.     if (atoi(yr1) < atoi(yr2))
  18.         return -1;
  19.     else if (atoi(yr1) > atoi(yr2))
  20.         return 1;
  21.    
  22.     //else if years are equal
  23.     for (i=0; i<2;i++ ) {
  24.         mon1[i] = date1[i+5];
  25.         mon2[i] = date2[i+5];
  26.     }
  27.     if (atoi(mon1) < atoi(mon2))
  28.         return -1;
  29.     else if (atoi(mon1) > atoi(mon2))
  30.         return 1;
  31.     //else if months are equal
  32.     for (i=0; i<2;i++ ) {
  33.         mon1[i] = date1[i+8];
  34.         mon2[i] = date2[i+8];
  35.     }
  36.     if (atoi(mon1) < atoi(mon2))
  37.         return -1;
  38.     else if (atoi(mon1) > atoi(mon2))
  39.         return 1;
  40.     else
  41.         return 0;
  42. }
  43.  
  44.  
  45. void  checkQuery(PGresult* res)
  46. {
  47.     if(!res || PQresultStatus(res) != PGRES_TUPLES_OK){
  48.         fprintf(stdout, "Error executing query: %s\n",
  49.             PQresultErrorMessage(res));
  50.         PQclear(res);
  51.         exit(1);
  52.     }
  53. }
  54.  
  55. int dropView (PGconn* conn, char* cmd, char* viewName, int viewsToDrop) {
  56.         if (0 > viewsToDrop--)  {
  57.             PQfinish(conn);
  58.         }
  59.         sprintf(cmd,viewName);
  60.         PQexec(conn,cmd);
  61.         return viewsToDrop;
  62. }
  63.  
  64. void  checkCmd(PGresult* res)   {
  65.  
  66.     if(!res || PQresultStatus(res) != PGRES_COMMAND_OK) {
  67.         fprintf(stdout, "Error executing query: %s\n",
  68.             PQresultErrorMessage(res));
  69.         PQclear(res);
  70.         return;
  71.     }
  72. }
  73.  
  74. void  checkCmdDropViews(PGconn* conn, PGresult* res, int viewsToDrop, int drop) {
  75.  
  76.     if(!res || PQresultStatus(res) != PGRES_COMMAND_OK || drop == 1) {
  77.         char cmd[50];
  78.         if (!drop)
  79.                 fprintf(stdout, "Error executing query: %s\n",
  80.                     PQresultErrorMessage(res));
  81.         PQclear(res);
  82.         if (!drop)
  83.             fprintf(stdout, "Dropping temp views. Drop flag = %d. \n", drop);
  84.        
  85.         viewsToDrop = dropView (conn, cmd, "DROP VIEW loser_versions", viewsToDrop);
  86.         viewsToDrop = dropView (conn, cmd, "DROP VIEW tempview1", viewsToDrop);
  87.         viewsToDrop = dropView (conn, cmd, "DROP VIEW before_first", viewsToDrop);
  88.         viewsToDrop = dropView (conn, cmd, "DROP VIEW first_update", viewsToDrop);
  89.         viewsToDrop = dropView (conn, cmd, "DROP VIEW hostiles", viewsToDrop);
  90.         viewsToDrop = dropView (conn, cmd, "DROP VIEW hos_count", viewsToDrop);
  91. //      viewsToDrop = dropView (conn, cmd, "DROP VIEW hostileNoriginal", viewsToDrop);
  92. //      viewsToDrop = dropView (conn, cmd, "DROP VIEW pre_report", viewsToDrop);
  93.         viewsToDrop = dropView (conn, cmd, "DROP VIEW usersusers", viewsToDrop);
  94.         viewsToDrop = dropView (conn, cmd, "DROP VIEW lazy", viewsToDrop);
  95.         viewsToDrop = dropView (conn, cmd, "DROP VIEW almost_there", viewsToDrop);
  96.         if (!drop)
  97.                 exit (1);
  98.        
  99.     }
  100. }
  101.  
  102. void  checkConn(PGconn* conn)   {
  103.    
  104.     if (!conn || PQstatus(conn) == CONNECTION_BAD) {
  105.         fprintf(stdout, "Connection to server failed: %s\n",
  106.             PQerrorMessage(conn));
  107.         PQfinish(conn);
  108.         exit(1);
  109.     }
  110. }
  111. // ============================ MAIN FUNCTIONS ================================
  112.    
  113. void * addUser(int id, char * name)
  114. {
  115.     char cmd[200];
  116.     char connect_param[80];
  117.     PGresult *res;
  118.     PGconn *conn;
  119.     sprintf(connect_param,
  120.         "host=pgsql.cs.technion.ac.il dbname=%s user=%s password=%s",
  121.         USERNAME, USERNAME, PASSWORD);
  122.     conn = PQconnectdb(connect_param);
  123.     checkConn(conn);
  124.     sprintf(cmd, "SELECT * FROM users   WHERE id=%d", id);
  125.     res = PQexec(conn, cmd);
  126.     checkQuery(res);
  127.     if (PQntuples(res)!=0){
  128.         printf (ILL_PARAMS);
  129.         return NULL;
  130.     }
  131.     cmd[0] = '\0';
  132.     sprintf(cmd, "INSERT INTO users VALUES %d %s", id, name);
  133.     res = PQexec(conn, cmd);
  134.     checkCmd(res);
  135.     PQfinish(conn);
  136.     return NULL;
  137. }
  138.  
  139. void * addEditor(int id)
  140. {
  141.     char cmd[200];
  142.     char connect_param[80];
  143.     PGresult *res;
  144.     PGconn *conn;
  145.     sprintf(connect_param,  "host=pgsql.cs.technion.ac.il dbname=%s user=%s password=%s",       USERNAME, USERNAME, PASSWORD);
  146.     conn = PQconnectdb(connect_param);
  147.     checkConn(conn);
  148.     sprintf(cmd, "SELECT * FROM users WHERE id=%d", id);
  149.     res = PQexec(conn, cmd);
  150.     checkQuery(res);
  151.     if (PQntuples(res)==0){
  152.         printf (ILL_PARAMS);
  153.         return NULL;
  154.     }
  155.    
  156.     sprintf(cmd, "SELECT * FROM editor  WHERE id=%d", id);
  157.     res = PQexec(conn, cmd);
  158.     checkQuery(res);
  159.     if (PQntuples(res)!=0){
  160.         printf (ILL_PARAMS);
  161.         return NULL;
  162.     }
  163.    
  164.     sprintf(cmd, "INSERT INTO editor VALUES %d", id);
  165.     res = PQexec(conn, cmd);
  166.     checkCmd(res);
  167.     PQfinish(conn);
  168.     return NULL;
  169. }
  170.  
  171. void * addAdmin(int id)
  172. {
  173.     char cmd[200];
  174.     char connect_param[80];
  175.     PGresult *res;
  176.     PGconn *conn;
  177.     sprintf(connect_param,
  178.         "host=pgsql.cs.technion.ac.il dbname=%s user=%s password=%s",
  179.         USERNAME, USERNAME, PASSWORD);
  180.     conn = PQconnectdb(connect_param);
  181.     checkConn(conn);
  182.     sprintf(cmd, "SELECT * FROM users   WHERE id=%d", id);
  183.     res = PQexec(conn, cmd);
  184.     checkQuery(res);
  185.     if (PQntuples(res)==0){
  186.         printf (ILL_PARAMS);
  187.         return NULL;
  188.     }
  189.    
  190.     sprintf(cmd, "SELECT * FROM admin WHERE id=%d", id);
  191.     res = PQexec(conn, cmd);
  192.     checkQuery(res);
  193.     if (PQntuples(res)!=0){
  194.         printf (ILL_PARAMS);
  195.         return NULL;
  196.     }
  197.    
  198.     sprintf(cmd, "SELECT * FROM editor  WHERE id=%d", id);
  199.     res = PQexec(conn, cmd);
  200.     checkQuery(res);
  201.     if (PQntuples(res)==0){
  202.            
  203.             sprintf(cmd, "INSERT INTO editor VALUES %d", id);
  204.             res = PQexec(conn, cmd);
  205.             checkCmd(res);
  206.     }
  207.    
  208.     sprintf(cmd, "INSERT INTO admin VALUES %d", id);
  209.     res = PQexec(conn, cmd);
  210.     checkCmd(res);
  211.     PQfinish(conn);
  212.     return NULL;
  213. }
  214. void * addLink(char * fromTitle, char * toTitle)
  215. {
  216.     char cmd[200];
  217.     PGresult *res1, *res2;
  218.     PGconn *conn;
  219.     char connect_param[80];
  220.         sprintf(connect_param,
  221.         "host=pgsql.cs.technion.ac.il dbname=%s user=%s password=%s",
  222.         USERNAME, USERNAME, PASSWORD);
  223.     conn = PQconnectdb(connect_param);
  224.     checkConn(conn);
  225.     sprintf(cmd, "SELECT * FROM article WHERE title=%s", fromTitle);
  226.     res1 = PQexec(conn, cmd);
  227.     checkQuery(res1);
  228.    
  229.     sprintf(cmd, "SELECT * FROM article WHERE title=%s", toTitle);
  230.     res2 = PQexec(conn, cmd);
  231.     checkQuery(res2);
  232.     if ((PQntuples(res1)==0) || (PQntuples(res2)==0)) {
  233.         printf(ILL_PARAMS);
  234.         return NULL;
  235.     }
  236.    
  237.     sprintf(cmd, "INSERT INTO link VALUES '%s' '%s'", fromTitle, toTitle);
  238.     res1 = PQexec(conn, cmd);
  239.     checkCmd(res1);
  240.     PQfinish(conn);
  241.     return NULL;
  242. }
  243.  
  244. void * addViewed(int id, char * title, char * date)
  245. {
  246.     char cmd[200];
  247.     char connect_param[80];
  248.     PGresult *res1, *res2;
  249.     PGconn *conn;
  250.     sprintf(connect_param,
  251.         "host=pgsql.cs.technion.ac.il dbname=%s user=%s password=%s",
  252.         USERNAME, USERNAME, PASSWORD);
  253.     conn = PQconnectdb(connect_param);
  254.     checkConn(conn);
  255.     sprintf(cmd, "SELECT * FROM users       WHERE id=%d", id);
  256.     res1 = PQexec(conn, cmd);
  257.     checkQuery(res1);
  258.    
  259.     sprintf(cmd, "SELECT * FROM version     WHERE title=%s", title);
  260.     res2 = PQexec(conn, cmd);
  261.     checkQuery(res2);
  262.     if ((PQntuples(res1)==0) || (PQntuples(res2)==0)) {
  263.         printf(ILL_PARAMS);
  264.         return NULL;
  265.     }
  266.    
  267.     sprintf(cmd, "INSERT INTO viewed VALUES %d %s DATE(%s)", id, title, date);
  268.     res1 = PQexec(conn, cmd);
  269.     checkCmd(res1);
  270.     PQfinish(conn);
  271.     return NULL;
  272. }
  273.  
  274. void * addVersion(int id, char * date, char * title, char * content)
  275. {
  276.     char cmd[200];
  277.     char connect_param[80];
  278.     PGresult *res;
  279.     PGconn *conn;
  280.     sprintf(connect_param,
  281.         "host=pgsql.cs.technion.ac.il dbname=%s user=%s password=%s",
  282.         USERNAME, USERNAME, PASSWORD);
  283.     conn = PQconnectdb(connect_param);
  284.     checkConn(conn);
  285.     sprintf(cmd, "SELECT * FROM editor WHERE id=%d", id);
  286.     res = PQexec(conn, cmd);
  287.     checkQuery(res);
  288.     if (PQntuples(res)==0) {
  289.         printf(ILL_PARAMS);
  290.         return NULL;
  291.     }
  292.    
  293.     sprintf(cmd, "SELECT * FROM article WHERE title='%s'", title);
  294.     res = PQexec(conn, cmd);
  295.     checkQuery(res);
  296.     if (PQntuples(res)==0) {
  297.         sprintf(cmd, "INSERT INTO article VALUES ('%s') ", title);
  298.         res = PQexec(conn, cmd);
  299.         checkCmd(res);
  300.     }
  301.     sprintf(cmd, "INSERT INTO version VALUES (%d,'%s','%s','%s')", id, date, title, content);
  302.     res = PQexec(conn, cmd);
  303.     checkCmd(res);
  304.     PQfinish(conn);
  305.     return NULL;
  306. }
  307.  
  308. void * addAccepted(int eid, int aid, char * title, char * vdate, char * adate)
  309. {
  310.     char cmd[200];
  311.     char connect_param[80];
  312.     PGresult *res1, *res2;
  313.     PGconn *conn;
  314.     sprintf(connect_param,
  315.         "host=pgsql.cs.technion.ac.il dbname=%s user=%s password=%s",
  316.         USERNAME, USERNAME, PASSWORD);
  317.     conn = PQconnectdb(connect_param);
  318.     checkConn(conn);
  319.     sprintf(cmd, "SELECT * FROM admin WHERE id=%d", aid);
  320.     res1 = PQexec(conn, cmd);
  321.     checkQuery(res1);
  322.    
  323.     sprintf(cmd, "SELECT * FROM editor  WHERE id=%d", eid);
  324.     res2 = PQexec(conn, cmd);
  325.     if(!res2 || PQresultStatus(res2) != PGRES_TUPLES_OK){
  326.         fprintf(stdout, "Error executing query: %s\n",
  327.         PQresultErrorMessage(res2));
  328.         PQclear(res2);
  329.         return NULL;
  330.     }
  331.     if ((PQntuples(res1)==0) || (PQntuples(res2)==0)) {
  332.         printf(ILL_PARAMS);
  333.         return NULL;
  334.     }
  335.    
  336.     sprintf(cmd, "SELECT * FROM version WHERE title=%s AND date= %s", title, vdate);
  337.     res1 = PQexec(conn, cmd);
  338.     checkQuery(res1);
  339.     if ((PQntuples(res1)==0) || (cmpDates(adate,vdate)==-1)) {
  340.         printf(ILL_PARAMS);
  341.         return NULL;
  342.     }
  343.    
  344.     sprintf(cmd, "INSERT INTO accepted VALUES %d, %d, %s, DATE(%s), DATE(%s)",eid, aid, title,vdate,adate);
  345.     res1 = PQexec(conn, cmd);
  346.     checkCmd(res1);
  347.     PQfinish(conn);
  348.     return NULL;
  349. }
  350.  
  351. void * removeUser(int id, int opt, int newId)
  352. {
  353.     char cmd[200];
  354.     char connect_param[80];
  355.     PGresult *res,*res1,*res2;
  356.     PGconn *conn;
  357.     sprintf(connect_param,
  358.         "host=pgsql.cs.technion.ac.il dbname=%s user=%s password=%s",
  359.         USERNAME, USERNAME, PASSWORD);
  360.     conn = PQconnectdb(connect_param);
  361.     checkConn(conn);
  362.     sprintf(cmd, "SELECT * FROM users   WHERE id=%d", id);
  363.     res = PQexec(conn, cmd);
  364.     checkQuery(res);
  365.     if (PQntuples(res)==0){
  366.         printf (ILL_PARAMS);
  367.         return NULL;
  368.     }
  369.    
  370.     sprintf(cmd, "SELECT * FROM viewed  WHERE id=%d", id);
  371.     res1 = PQexec(conn, cmd);
  372.     checkQuery(res1);
  373.    
  374.     sprintf(cmd, "SELECT * FROM accepted WHERE aid=%d", id);
  375.     res2 = PQexec(conn, cmd);
  376.     checkQuery(res2);
  377.     if ( (PQntuples(res1)!=0) && (PQntuples(res2)!=0) && (opt==REJECT)) {
  378.         printf(ILL_PARAMS);
  379.         return NULL;
  380.     }
  381.     switch (opt)    {
  382.         case DELETE:
  383.            
  384.             sprintf(cmd, "DELETE FROM viewed WHERE id=%d", id);
  385.             res = PQexec(conn, cmd);
  386.             checkCmd(res);
  387.            
  388.             sprintf(cmd, "DELETE FROM accepted WHERE aid=%d", id);
  389.             res = PQexec(conn, cmd);
  390.             checkCmd(res);
  391.            
  392.             sprintf(cmd, "DELETE FROM accepted WHERE eid=%d",id);
  393.             res = PQexec(conn, cmd);
  394.             checkCmd(res);
  395.            
  396.             sprintf(cmd,"DELETE FROM viewed WHERE title, date IN (SELECT title, date FROM viewed WHERE date < ALL(SELECT adate FROM accepted WHERE accepted.title=viewed.title)) T");
  397.             res = PQexec(conn,cmd);
  398.             checkCmd(res);
  399.            
  400.             break;
  401.    
  402.         case CHANGE:
  403.            
  404.             sprintf(cmd, "SELECT * FROM editor WHERE id=%d", id);
  405.             res1 = PQexec(conn, cmd);
  406.             checkQuery (res1);
  407.             if (PQntuples(res1) >0) {
  408.                
  409.                 sprintf(cmd, "SELECT * FROM editor WHERE id=%d", newId);
  410.                 res2 = PQexec(conn, cmd);
  411.                 checkQuery (res2);
  412.                 if (PQntuples(res2)==0)  {
  413.                     printf(ILL_PARAMS);
  414.                     return NULL;
  415.                 }
  416.             }
  417.            
  418.             sprintf(cmd, "SELECT * FROM accepted WHERE aid=%d", id);
  419.             res1 = PQexec(conn, cmd);
  420.             checkQuery (res1);
  421.             if (PQntuples(res1) >0) {
  422.                
  423.                 sprintf(cmd, "SELECT * FROM admin WHERE id=%d", newId);
  424.                 res2 = PQexec(conn, cmd);
  425.                 checkQuery (res2);
  426.                 if (PQntuples(res2)==0)  {
  427.                     printf(ILL_PARAMS);
  428.                     return NULL;
  429.                 }
  430.             }
  431.            
  432.             sprintf(cmd, "UPDATE version SET id=%d WHERE id=%d", newId, id);
  433.             res = PQexec(conn, cmd);
  434.             checkCmd (res);
  435.            
  436.             sprintf(cmd, "UPDATE accepted SET eid=%d WHERE eid=%d", newId, id);
  437.             res = PQexec(conn, cmd);
  438.             checkCmd (res);
  439.            
  440.             sprintf(cmd, "UPDATE accepted SET aid=%d WHERE aid=%d", newId, id);
  441.             res = PQexec(conn, cmd);
  442.             checkCmd (res);
  443.         break;
  444.         default: printf("error! default case reached in removeUSer()!.\n");
  445.             exit(1);   
  446.             break;
  447.     }
  448.     sprintf(cmd, "DELETE FROM users WHERE id=%d", id);
  449.     res = PQexec(conn, cmd);
  450.     checkCmd(res);
  451.    
  452.    
  453.     sprintf(cmd, "DELETE FROM editor WHERE id=%d", id);
  454.     res = PQexec(conn, cmd);
  455.     checkCmd(res);
  456.    
  457.     sprintf(cmd, "DELETE FROM admin WHERE id=%d", id);
  458.     res = PQexec(conn, cmd);
  459.     checkCmd(res);
  460. }
  461. void * popularVersions(char * fromDate, char * toDate, int distinct, int aboveAvg)
  462. {
  463.     return NULL;
  464. }
  465. void * hostileEditors()
  466. {
  467.     char cmd[400];
  468.     char connect_param[200];
  469.     PGresult *res,*res1,*res2;
  470.     int i,nTuples;
  471.     PGconn *conn;
  472.     int viewsToDrop = 1;
  473.  
  474.     //printf("in hostileeditors().\n");
  475.    
  476.     sprintf(connect_param,
  477.         "host=pgsql.cs.technion.ac.il dbname=%s user=%s password=%s",
  478.         USERNAME, USERNAME, PASSWORD);
  479.     conn = PQconnectdb(connect_param);
  480.     checkConn(conn);
  481.     sprintf(cmd, "CREATE VIEW loser_versions AS SELECT id, title, date FROM version WHERE title IN (SELECT title FROM version WHERE     id IN(          SELECT id FROM editor           WHERE id NOT IN (SELECT eid FROM accepted)))");
  482.     res = PQexec(conn, cmd);
  483.     checkCmdDropViews(conn, res, viewsToDrop++, 0);
  484.    
  485.     sprintf(cmd,"CREATE VIEW TempView1 AS       SELECT N.id AS O_Editor, V.id AS Editor, V.title AS title, N.date AS o_date, V.date AS e_date       FROM loser_versions N ,Version V WHERE (N.title = V.title) AND (V.date - N.date BETWEEN 0 AND 7) AND (V.id <> N.id)");
  486.     PQclear(res);
  487.     res = PQexec(conn,cmd);
  488.     checkCmdDropViews(conn, res, viewsToDrop++, 0);
  489.  
  490.     sprintf(cmd,"CREATE VIEW before_first AS    SELECT o_editor, title, min(e_date) AS update_date, o_date AS orig_date FROM TempView1 WHERE o_editor IN (SELECT id FROM editor WHERE id NOT IN (SELECT eid FROM accepted)) GROUP BY o_Editor,title,o_date");
  491.     PQclear(res);
  492.     res = PQexec(conn,cmd);
  493.     checkCmdDropViews(conn, res, viewsToDrop++, 0);
  494.  
  495.     sprintf(cmd,"CREATE VIEW first_update AS    SELECT  BF.o_editor, BF.orig_date, BF.title, V.id AS editor ,BF.update_date FROM before_first BF , version V WHERE V.date=BF.update_date AND V.title=BF.title   ");
  496.     PQclear(res);
  497.     res = PQexec(conn,cmd);
  498.     checkCmdDropViews(conn, res, viewsToDrop++, 0);
  499.  
  500.     sprintf(cmd,"CREATE VIEW hostiles AS        SELECT DISTINCT T1.Editor AS hostile_id, F.o_editor as loser_id FROM TempView1 T1, first_update F       WHERE (T1.editor = F.editor) AND (T1.o_date = F.orig_date)");
  501.     PQclear(res);
  502.     res = PQexec(conn,cmd);
  503.     checkCmdDropViews(conn, res, viewsToDrop++, 0);
  504.  
  505.     sprintf(cmd,"CREATE VIEW hos_count AS       SELECT * FROM   hostiles WHERE loser_id IN  (SELECT loser_id FROM hostiles GROUP BY loser_id HAVING count(hostile_id)=1)");
  506.     PQclear(res);
  507.     res = PQexec(conn,cmd);
  508.     checkCmdDropViews(conn, res, viewsToDrop++, 0);
  509. /*
  510.     sprintf(cmd,"CREATE VIEW hostileNoriginal AS        SELECT hos_count.editor, o_editor FROM      hos_count LEFT OUTER JOIN hostiles ON (hos_count.editor = hostiles.editor)");
  511.     PQclear(res);
  512.     res = PQexec(conn,cmd);
  513.     checkCmdDropViews(conn, res, viewsToDrop++, 0);
  514.  
  515.     sprintf(cmd,"CREATE VIEW pre_report AS      SELECT loser_versions.id AS loser_id, o_editor AS hostile_id FROM       loser_versions LEFT OUTER JOIN hostileNoriginal HN ON (loser_versions.id = HN.editor)       ORDER BY loser_versions.id");
  516.     PQclear(res);
  517.     res = PQexec(conn,cmd);
  518.     checkCmdDropViews(conn, res, viewsToDrop++, 0);
  519. */
  520.     sprintf(cmd,"CREATE VIEW usersusers AS      SELECT U1.id AS u1_id, U1.name AS u1_name, U2.id AS u2_id, U2.name AS u2_name FROM users U1, users U2");
  521.     PQclear(res);
  522.     res = PQexec(conn,cmd);
  523.     checkCmdDropViews(conn, res, viewsToDrop++, 0);
  524.  
  525.     sprintf(cmd,"CREATE VIEW lazy AS        SELECT id ,name FROM users WHERE id NOT IN (SELECT eid FROM accepted) AND id IN (SELECT id FROM editor)");
  526.     PQclear(res);
  527.     res = PQexec(conn,cmd);
  528.     checkCmdDropViews(conn, res, viewsToDrop++, 0);
  529.  
  530.     sprintf(cmd,"CREATE VIEW almost_there AS SELECT u1_id,u1_name,u2_id,u2_name FROM hos_count LEFT OUTER JOIN usersusers ON (u1_id=hos_count.loser_id) AND (u2_id=hos_count.hostile_id)");
  531.     PQclear(res);
  532.     res = PQexec(conn,cmd);
  533.     checkCmdDropViews(conn, res, viewsToDrop++, 0);
  534.  
  535.     sprintf(cmd,"SELECT lazy.id,lazy.name,u2_id,u2_name FROM lazy LEFT OUTER JOIN almost_there ON (lazy.id=almost_there.u1_id)");
  536.     PQclear(res);
  537.     res = PQexec(conn,cmd);
  538.     checkQuery(res);
  539.     nTuples=PQntuples(res);
  540.     printf(HOSTILE_EDITOR_HEAD);
  541.     for (i=0;i<nTuples;i++) {
  542.         char value1[11];
  543.         char value2[16];
  544.         char value3[11];
  545.         char value4[16];
  546.         strcpy(value1, PQgetvalue(res, i,0));
  547.         strcpy(value2, PQgetvalue(res, i,1));
  548.         if (!PQgetisnull(res, i, 2))
  549.             strcpy(value3, PQgetvalue(res, i,2));
  550.         else
  551.             strcpy(value3, "null");
  552.         if (!PQgetisnull(res, i, 3))
  553.             strcpy(value4, PQgetvalue(res, i,3));
  554.         else
  555.             strcpy(value4, "null");
  556.         printf(HOSTILE_EDITOR_REC,value1,value2,value3,value4);
  557.     }
  558.  
  559.     /*
  560.     sprintf(cmd,"DROP VIEW loser_versions");
  561.     res = PQexec(conn,cmd);
  562.     //checkCmdDropViews(res, viewsToDrop--);
  563.  
  564.     sprintf(cmd,"DROP VIEW TempView1");
  565.     res = PQexec(conn,cmd);
  566.     //checkCmdDropViews(conn, res, viewsToDrop++, 0);
  567.  
  568.     sprintf(cmd,"DROP VIEW first_update");
  569.     res = PQexec(conn,cmd);
  570.     //checkCmdDropViews(conn, res, viewsToDrop++, 0);
  571.  
  572.     sprintf(cmd,"DROP VIEW hostiles");
  573.     res = PQexec(conn,cmd);
  574.     //checkCmdDropViews(conn, res, viewsToDrop++, 0);
  575.  
  576.     sprintf(cmd,"DROP VIEW hos_count");
  577.     res = PQexec(conn,cmd);
  578.     //checkCmdDropViews(conn, res, viewsToDrop++, 0);
  579.  
  580.     sprintf(cmd,"DROP VIEW hostileNoriginal");
  581.     res = PQexec(conn,cmd);
  582.     //checkCmdDropViews(conn, res, viewsToDrop++, 0);
  583.  
  584.     sprintf(cmd,"DROP VIEW pre_report");
  585.     res = PQexec(conn,cmd);
  586.  
  587.  
  588.     //checkCmdDropViews(conn, res, viewsToDrop++, 0);
  589.     */
  590.    
  591.     checkCmdDropViews(conn, res, viewsToDrop++, 1);     //Drop views, flag is up
  592.  
  593.     PQclear(res);
  594.     //PQfinish(conn); -- done in the above function.
  595.     //printf("not in hostileeditors().\n");
  596.     return NULL;
  597. }
  598.  
  599. int main()
  600. {
  601.     startParsing();
  602. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement