Advertisement
Guest User

Untitled

a guest
Mar 26th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.69 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5. #include <libpq-fe.h>
  6.  
  7. #define X 500
  8. #define Y 500
  9. #define Z 500
  10.  
  11. int outLen = 0, inLen = 0, globalGuard = 0, alter=0;
  12.  
  13. void doSQL(PGconn *conn, char *command)
  14. {
  15.   PGresult *result;
  16.  
  17.   printf("%s\n", command);
  18.  
  19.   result = PQexec(conn, command);
  20.   printf("status is     : %s\n", PQresStatus(PQresultStatus(result)));
  21.   printf("#rows affected: %s\n", PQcmdTuples(result));
  22.   printf("result message: %s\n", PQresultErrorMessage(result));
  23.  
  24.   switch(PQresultStatus(result)) {
  25.   case PGRES_TUPLES_OK:
  26.     {
  27.       int n = 0, m = 0;
  28.       int nrows   = PQntuples(result);
  29.       int nfields = PQnfields(result);
  30.       printf("number of rows returned   = %d\n", nrows);
  31.       printf("number of fields returned = %d\n", nfields);
  32.       for(m = 0; m < nrows; m++) {
  33.     for(n = 0; n < nfields; n++)
  34.       printf(" %s = %s", PQfname(result, n),PQgetvalue(result,m,n));
  35.     printf("\n");
  36.       }
  37.     }
  38.   }
  39.   PQclear(result);
  40. }
  41.  
  42. /*void create_table (PGconn *conn)
  43. {
  44.     if(PQstatus(conn) == CONNECTION_OK) {
  45.     printf("connection made\n");
  46.  
  47.     doSQL(conn, "DROP TABLE number");
  48.     doSQL(conn, "CREATE TABLE number(value INTEGER PRIMARY KEY, name VARCHAR)");
  49.     doSQL(conn, "INSERT INTO number values(42, 'The Answer')");
  50.     doSQL(conn, "INSERT INTO number values(29, 'My Age')");
  51.     doSQL(conn, "INSERT INTO number values(30, 'Anniversary')");
  52.     doSQL(conn, "INSERT INTO number values(66, 'Clickety-Click')");
  53.     doSQL(conn, "SELECT * FROM number");
  54.     doSQL(conn, "UPDATE number SET name = 'Zaphod' WHERE value = 42");
  55.     doSQL(conn, "DELETE FROM number WHERE value = 29");
  56.     doSQL(conn, "SELECT * FROM number");
  57.   }
  58.   else
  59.     printf("connection failed: %s\n", PQerrorMessage(conn));
  60.  
  61.  
  62. }*/
  63.  
  64. void open_file(char ***table,char *data)
  65. {
  66.       FILE *file = fopen(data, "r");
  67.  
  68.   int i, j, lenGuard;
  69.   char *c = (char*) malloc(Y * sizeof(char)),
  70.        **helpTable = (char**) malloc(X * sizeof(char*)),
  71.        *pch;
  72.  
  73.   for (i = 0; i < Z; i++)
  74.   {
  75.     if (fgets(c, X, file) == NULL) break;
  76.  
  77.     table[i] = (char**) malloc(X * sizeof(char*));
  78.     helpTable[i] = (char*) malloc((strlen(c) + 1) * sizeof(char));
  79.  
  80.     strcpy(helpTable[i], c);
  81.     pch = strtok(helpTable[i], ";");
  82.  
  83.     for (j = 0; pch != NULL; j++)
  84.     {
  85.       lenGuard = j;
  86.       table[i][j] = (char*) malloc((strlen(pch) + 1) * sizeof(char));
  87.       strcpy(table[i][j], pch);
  88.       pch = strtok(NULL, ";");
  89.       if (i == 0) inLen = lenGuard;
  90.       else if (lenGuard > inLen) globalGuard = 1;
  91.      
  92.     }
  93.  
  94.     if (lenGuard < inLen) globalGuard = 1;
  95.   }
  96.  
  97.   outLen = i;
  98.  
  99.   fclose(file);
  100.  
  101.   free(c);
  102. }
  103. //////////////////////////////
  104. void alter_f(char ***table, int help_tab[])
  105. {
  106.    
  107.     for (int i=1;i<outLen;i++){
  108.     for (int j=1;j<inLen;j++){
  109.   if (strlen(table[i][j])>20){
  110.      help_tab[alter]=j;
  111.       alter++;
  112.       }  
  113.        }
  114.     }
  115.  
  116.  
  117. }
  118. void altertabF(char ***table, int alttab[], char *alterTabS,char *name)
  119. {
  120.     if(alttab[0]!=0){
  121.           strcpy(alterTabS, "ALTER TABLE ");
  122.           strcat(alterTabS, name);
  123.           strcat(alterTabS, " ALTER COLUMN ");
  124.           strcat(alterTabS, table[0][alttab[0]]);
  125.           strcat(alterTabS, " TYPE VARCHAR(");
  126.           strcat(alterTabS, "40");
  127.           strcat(alterTabS, ")");
  128.         alttab[0]=0;
  129.         //p++;
  130.        
  131.         }
  132. }
  133. void generateCommands(char **commands, char ***table, char *data, int *alttab)
  134. {
  135.   int i, j;
  136.   char *name = data;
  137.  
  138.   for (i = strlen(data) - 4; i <= strlen(data); i++) name[i] = 0;
  139.  
  140.   commands[0] = (char*) malloc((X + 1) * sizeof(char));
  141.   strcpy(commands[0], "DROP TABLE ");
  142.   strcat(commands[0], name);
  143.  
  144.   commands[outLen + 1] = (char*) malloc((X + 1) * sizeof(char));
  145.   strcpy(commands[outLen + 1], "SELECT * FROM ");
  146.   strcat(commands[outLen + 1], name);
  147.  
  148.   for (i = 0; i < 1; i++)
  149.   {
  150.     commands[i + 1] = (char*) malloc((X + 1) * sizeof(char));
  151.  
  152.     for (j = 0; j < inLen; j++)
  153.     {
  154.       if (i == 0)
  155.       {
  156.         if (j == 0)
  157.         {
  158.           strcpy(commands[i + 1], "CREATE TABLE ");
  159.           strcat(commands[i + 1], name);
  160.           strcat(commands[i + 1], "(");
  161.           strcat(commands[i + 1], table[i][j]);
  162.           strcat(commands[i + 1], " VARCHAR(20) UNIQUE, ");
  163.         }
  164.         else if (j == inLen - 1)
  165.         {
  166.           strncat(commands[i + 1], table[i][j], strlen(table[i][j]));
  167.           strcat(commands[i + 1], " VARCHAR(20))");
  168.         }
  169.         else
  170.         {
  171.           strcat(commands[i + 1], table[i][j]);
  172.           strcat(commands[i + 1], " VARCHAR(20), ");
  173.         }
  174.        
  175.       }
  176.     }
  177.   }
  178.  
  179.   for (i = 1; i < outLen; i++)
  180.   {
  181.      commands[i + 1] = (char*) malloc((X + 1) * sizeof(char));
  182.      for (j = 0; j < inLen; j++){
  183.         if (j == 0)
  184.         {
  185.  
  186.           strcpy(commands[i + 1], "INSERT INTO ");
  187.           strcat(commands[i + 1], name);
  188.           strcat(commands[i + 1], " values(");
  189.           strcat(commands[i + 1], table[i][j]);
  190.           strcat(commands[i + 1], ", '");
  191.                  
  192.         }
  193.         else if (j == inLen - 1)
  194.         {
  195.           strncat(commands[i + 1], table[i][j], strlen(table[i][j]));
  196.           strcat(commands[i + 1], "')");
  197.         }
  198.  
  199.         else
  200.         {
  201.            
  202.           strcat(commands[i + 1], table[i][j]);
  203.           strcat(commands[i + 1], "', '");
  204.         }
  205.       }
  206.     }
  207. }
  208.  
  209.  
  210.  
  211.  
  212. //////////////////////////////////////////////////////
  213.  
  214. PGresult* getSqlResult(char *command);
  215.  
  216. PGconn *conn;
  217. int queryStatus = 0;
  218.  
  219. int main(int argc, char* argv[])
  220. {
  221.    char pom[25];
  222.     char dbconninfo[100]="host=localhost port=5432 dbname=";
  223.     int a;
  224.     printf("Database name: \n");
  225.         scanf("%s",pom);
  226.         strcat(dbconninfo,pom);
  227.         strcat(dbconninfo," user=");
  228.     printf("User:\n");
  229.         scanf("%s",pom);
  230.         strcat(dbconninfo,pom);
  231.         strcat(dbconninfo," password=");
  232.     char* pass = getpass("Password: ");
  233.     strcat(dbconninfo,pass);
  234.     conn = PQconnectdb(dbconninfo);
  235.  
  236.     if(PQstatus(conn) == CONNECTION_OK) {
  237.         printf("Polaczenie udane\n\n");
  238.        
  239.    
  240.  
  241.  
  242.      char ***table = (char***) malloc(Z * sizeof(char**)),
  243.        **commands = (char**) malloc((Y + 2) * sizeof(char*)),
  244.        *alterTabS = (char*) malloc((Y + 2) * sizeof(char*));
  245.      int altertab[Y];
  246.        open_file(table, argv[1]);
  247.         alter_f(table, altertab);
  248.        generateCommands(commands, table, argv[1],altertab);
  249.  
  250.        altertabF(table,altertab,alterTabS,argv[1]);
  251.        doSQL(conn,commands[0]);
  252.        doSQL(conn,commands[1]);
  253.        doSQL(conn,alterTabS);
  254.   for(int i=2;i<=outLen;i++){
  255.     doSQL(conn,commands[i]);
  256.   doSQL(conn,commands[outLen+1]);
  257.   }
  258.      // print(conn,argv[1],stdout);
  259.  
  260.   }
  261.      else
  262.         printf("Blad polaczenia: %s\n", PQerrorMessage(conn));
  263.   PQfinish(conn);
  264.   return EXIT_SUCCESS;
  265. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement