SHARE
TWEET

Untitled

a guest Mar 20th, 2017 78 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //================================================================================================================================
  2. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++LOAD+
  3. //================================================================================================================================
  4.  
  5.     if(argc==2 && strcmp(argv[1],"load")==0)
  6.         printf("Enter file name!\n");
  7.     if(argc==1){
  8.         printf("\n+------------------------------------------------------+\n\t\tPOSGRESQL DATABASE FROM FILE\n+------------------------------------------------------+\n");
  9.         printf(">show option list:\n\t~$./dbo\n");
  10.         printf(">load table from file_name file:\n\t~$./dbo load file_name\n");
  11.         printf(">show tables in html page\n\t~$./dbo table_name1 table_name2 > file.html\n\n");
  12.         printf("+------------------------------------------------------+\n");
  13.     }
  14.     //---------------------------------------------------------------------------
  15.     //---------------------------------------------------------------------------
  16.     else{
  17.     if(argc>2 && strcmp(argv[1],"load")==0){
  18.     int filesCounter=0,i=0,j=0,k=0;
  19.     char fileName[300]={'\0'};
  20.     char columnNamesLine[300]={'\0'};
  21. for(filesCounter=2;filesCounter<argc;filesCounter++){
  22.     for(i=0;i<300;i++)
  23.         fileName[i]='\0';
  24.     strcat(fileName,argv[filesCounter]);
  25.     j=0;
  26.      while(fileName[j]!='.' && fileName!='\0')
  27.         j++;
  28.     char tableName[j];
  29.     for(i=0;i<j;i++)tableName[i]='\0';
  30.     for(i=0;i<j;i++)
  31.         tableName[i]=fileName[i];
  32.     FILE *plik = fopen(fileName, "r");
  33.     if (plik == NULL){
  34.     printf("\nCannot open \"%s\" file.\n",fileName);
  35.     exit(-1);    // trzeba dołączyć <stdlib.h>
  36.     }
  37.     else{
  38.     system("clear");
  39.     printf("\t\tPOSGRESQL DATABASE FROM FILE\n--------------------------------------------------------\n");
  40.     printf("File \"%s\" opened succesfully.",fileName);
  41.     printf("\n--------------------------------------------------------\n");
  42.     fgets(columnNamesLine,300,plik);
  43.     }
  44.     int columnNamesLineLength= arlen(columnNamesLine);
  45.     int columnsCounter=1;
  46.     for(i=0;i<columnNamesLineLength;i++)
  47.         if(columnNamesLine[i]==';')
  48.             columnsCounter++;
  49.     char columnNames[columnsCounter][50];
  50.     for(i=0;i<columnsCounter;i++)
  51.         for(j=0;j<50;j++)
  52.             columnNames[i][j]='\0';
  53.     j=0;
  54.     k=0;
  55.  
  56.     for(i=0;i<(columnNamesLineLength-1);i++){
  57.         if(columnNamesLine[i]!=';'){
  58.             columnNames[j][k]=columnNamesLine[i];
  59.             k++;
  60.         }
  61.         else{
  62.             k=0;
  63.             j++;
  64.         }
  65.     }
  66.  
  67.     char tableId[30]={'\0'};
  68.     strcat(tableId,"id_");
  69.     strcat(tableId,tableName);
  70.     printf("Tablename: %s \n",tableName);
  71.     printf("Columns[%d]:\n",columnsCounter+1);
  72.     printf("1. %s [PRIMARY KEY]\n",tableId);
  73.     for(i=0;i<columnsCounter;i++){
  74.         printf("%d. %s\n",i+2,columnNames[i]);
  75.     }
  76.     //---------------------------------------------------------------------------
  77. printf("\n--------------------------------------------------------\n");
  78.     char logData[150]="host=localhost port=5432 dbname=";
  79.     char password[50]={'\0'};
  80.     char dbname[50]={'\0'};
  81.     char user[50]={'\0'};
  82.     printf("DBname: ");
  83.     scanf("%s",&dbname);
  84.     strcat(logData,dbname);
  85.     char createdb[30]={'\0'};
  86.     strcat(createdb,"createdb ");
  87.     strcat(createdb,dbname);
  88.     strcat(createdb," 2>/dev/null");
  89.     system(createdb);
  90.     printf("User: ");
  91.     scanf("%s",&user);
  92.     strcat(logData," user=");
  93.     strcat(logData,user);
  94.     printf("Password: ");
  95.     system("stty -echo");
  96.     scanf("%s",&password);
  97.     system("stty echo");
  98.     strcat(logData," password=");
  99.     strcat(logData,password);
  100. //-----------------------------------------
  101.     system("clear");
  102.   PGconn *conn = PQconnectdb(logData);
  103.  
  104.   if(PQstatus(conn) == CONNECTION_OK) {
  105.     printf(">Tables loaded succesfully!\n");
  106.  
  107. //----------------------------------------DROP
  108.     char dropTable[200]={'\0'};
  109.     strcat(dropTable,"DROP TABLE ");
  110.     strcat(dropTable, tableName);
  111.     strcat(dropTable, ";");
  112.     PQexec(conn, dropTable);
  113.  
  114. //----------------------------------------CREATE
  115.  
  116.     char createTable[200]={'\0'};
  117.     strcat(createTable,"CREATE TABLE ");
  118.     strcat(createTable, tableName);
  119.     strcat(createTable, "(");
  120.     strcat(createTable, tableId);
  121.     strcat(createTable," SERIAL PRIMARY KEY ");
  122.     for(i=0;i<columnsCounter;i++){
  123.         strcat(createTable,", ");
  124.         strcat(createTable,columnNames[i]);
  125.         strcat(createTable," VARCHAR(20)");
  126.     }
  127.     strcat(createTable,");");
  128.     PQexec(conn, createTable);
  129. //----------------------------------------INSERT FrOM FILE
  130.     char valuesLine[500]={'\0'};
  131.     char values[columnsCounter][50];
  132.     int valuesLineLength=0;
  133.     int valueLength=0;
  134.     char insert[500]={'\0'};
  135.     char alterTable[500]={'\0'};
  136.     char numb[100]={'\0'};
  137.     int wtf=0;
  138. while(feof(plik)!=1){
  139.     for(i=0;i<columnsCounter;i++)
  140.         for(j=0;j<50;j++)
  141.             values[i][j]='\0';
  142.     for(i=0;i<500;i++)
  143.         valuesLine[i]='\0';
  144.     fgets(valuesLine,500,plik);
  145.         valuesLineLength=arlen(valuesLine);
  146.     j=0;
  147.     k=0;
  148.     for(i=0;i<(valuesLineLength-1);i++){
  149.         if(valuesLine[i]!=';'){
  150.             values[j][k]=valuesLine[i];
  151.             k++;
  152.         }
  153.         else{
  154.             k=0;
  155.             j++;
  156.         }
  157.     }
  158.     for(i=0;i<columnsCounter;i++){
  159.         valueLength=arlen(values[i]);
  160.         if(valueLength>=20){
  161.             for(int alte=0;alte<500;alte++)
  162.                 alterTable[alte]='\0';
  163.             strcat(alterTable,"ALTER TABLE ");
  164.             strcat(alterTable,tableName);
  165.        
  166.             strcat(alterTable," ALTER COLUMN ");
  167.             strcat(alterTable,columnNames[i]);
  168.             strcat(alterTable," TYPE VARCHAR(");
  169.             sprintf(numb, "%d", (valueLength+2));
  170.             strcat(alterTable,numb);
  171.             strcat(alterTable,");");
  172.             PQexec(conn,alterTable);
  173.         }
  174.     }
  175.  
  176.     if((j+1)!=columnsCounter){
  177.         for(i=(j+1);i<columnsCounter;i++)
  178.             strcat(values[i],"NULL");
  179.     }
  180.     for(i=0;i<500;i++)
  181.         insert[i]='\0';
  182.     strcat(insert, "INSERT INTO ");
  183.     strcat(insert,tableName);
  184.     strcat(insert,"(");
  185.     strcat(insert,columnNames[0]);
  186.     for(wtf=1;wtf<columnsCounter;wtf++){
  187.         strcat(insert,",");
  188.         strcat(insert,columnNames[wtf]);
  189.     }
  190.     strcat(insert,") VALUES(");
  191.     strcat(insert,"'");
  192.     strcat(insert,values[0]);
  193.     strcat(insert,"'");
  194.     for(i=1;i<columnsCounter;i++){
  195.         strcat(insert,",'");
  196.         strcat(insert,values[i]);
  197.         strcat(insert,"'");
  198.     }
  199.     strcat(insert,");");
  200.     PQexec(conn, insert);
  201. }
  202.  
  203.   }
  204.   else
  205.     printf(">CONNECTION FAILED:\n %s\n", PQerrorMessage(conn));
  206.  
  207.   PQfinish(conn);
  208. }
  209.   return EXIT_SUCCESS;
  210.  
  211. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top