Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include <unistd.h>
- #include <libpq-fe.h>
- #define X 500
- #define Y 500
- #define Z 500
- int outLen = 0, inLen = 0, globalGuard = 0, alter=0;
- void doSQL(PGconn *conn, char *command)
- {
- PGresult *result;
- printf("%s\n", command);
- result = PQexec(conn, command);
- printf("status is : %s\n", PQresStatus(PQresultStatus(result)));
- printf("#rows affected: %s\n", PQcmdTuples(result));
- printf("result message: %s\n", PQresultErrorMessage(result));
- switch(PQresultStatus(result)) {
- case PGRES_TUPLES_OK:
- {
- int n = 0, m = 0;
- int nrows = PQntuples(result);
- int nfields = PQnfields(result);
- printf("number of rows returned = %d\n", nrows);
- printf("number of fields returned = %d\n", nfields);
- for(m = 0; m < nrows; m++) {
- for(n = 0; n < nfields; n++)
- printf(" %s = %s", PQfname(result, n),PQgetvalue(result,m,n));
- printf("\n");
- }
- }
- }
- PQclear(result);
- }
- /*void create_table (PGconn *conn)
- {
- if(PQstatus(conn) == CONNECTION_OK) {
- printf("connection made\n");
- doSQL(conn, "DROP TABLE number");
- doSQL(conn, "CREATE TABLE number(value INTEGER PRIMARY KEY, name VARCHAR)");
- doSQL(conn, "INSERT INTO number values(42, 'The Answer')");
- doSQL(conn, "INSERT INTO number values(29, 'My Age')");
- doSQL(conn, "INSERT INTO number values(30, 'Anniversary')");
- doSQL(conn, "INSERT INTO number values(66, 'Clickety-Click')");
- doSQL(conn, "SELECT * FROM number");
- doSQL(conn, "UPDATE number SET name = 'Zaphod' WHERE value = 42");
- doSQL(conn, "DELETE FROM number WHERE value = 29");
- doSQL(conn, "SELECT * FROM number");
- }
- else
- printf("connection failed: %s\n", PQerrorMessage(conn));
- }*/
- void open_file(char ***table,char *data)
- {
- FILE *file = fopen(data, "r");
- int i, j, lenGuard;
- char *c = (char*) malloc(Y * sizeof(char)),
- **helpTable = (char**) malloc(X * sizeof(char*)),
- *pch;
- for (i = 0; i < Z; i++)
- {
- if (fgets(c, X, file) == NULL) break;
- table[i] = (char**) malloc(X * sizeof(char*));
- helpTable[i] = (char*) malloc((strlen(c) + 1) * sizeof(char));
- strcpy(helpTable[i], c);
- pch = strtok(helpTable[i], ";");
- for (j = 0; pch != NULL; j++)
- {
- lenGuard = j;
- table[i][j] = (char*) malloc((strlen(pch) + 1) * sizeof(char));
- strcpy(table[i][j], pch);
- pch = strtok(NULL, ";");
- if (i == 0) inLen = lenGuard;
- else if (lenGuard > inLen) globalGuard = 1;
- }
- if (lenGuard < inLen) globalGuard = 1;
- }
- outLen = i;
- fclose(file);
- free(c);
- }
- //////////////////////////////
- void alter_f(char ***table, int help_tab[])
- {
- for (int i=1;i<outLen;i++){
- for (int j=1;j<inLen;j++){
- if (strlen(table[i][j])>20){
- help_tab[alter]=j;
- alter++;
- }
- }
- }
- }
- void altertabF(char ***table, int alttab[], char *alterTabS,char *name)
- {
- if(alttab[0]!=0){
- strcpy(alterTabS, "ALTER TABLE ");
- strcat(alterTabS, name);
- strcat(alterTabS, " ALTER COLUMN ");
- strcat(alterTabS, table[0][alttab[0]]);
- strcat(alterTabS, " TYPE VARCHAR(");
- strcat(alterTabS, "40");
- strcat(alterTabS, ")");
- alttab[0]=0;
- //p++;
- }
- }
- void generateCommands(char **commands, char ***table, char *data, int *alttab)
- {
- int i, j;
- char *name = data;
- for (i = strlen(data) - 4; i <= strlen(data); i++) name[i] = 0;
- commands[0] = (char*) malloc((X + 1) * sizeof(char));
- strcpy(commands[0], "DROP TABLE ");
- strcat(commands[0], name);
- commands[outLen + 1] = (char*) malloc((X + 1) * sizeof(char));
- strcpy(commands[outLen + 1], "SELECT * FROM ");
- strcat(commands[outLen + 1], name);
- for (i = 0; i < 1; i++)
- {
- commands[i + 1] = (char*) malloc((X + 1) * sizeof(char));
- for (j = 0; j < inLen; j++)
- {
- if (i == 0)
- {
- if (j == 0)
- {
- strcpy(commands[i + 1], "CREATE TABLE ");
- strcat(commands[i + 1], name);
- strcat(commands[i + 1], "(");
- strcat(commands[i + 1], table[i][j]);
- strcat(commands[i + 1], " VARCHAR(20) UNIQUE, ");
- }
- else if (j == inLen - 1)
- {
- strncat(commands[i + 1], table[i][j], strlen(table[i][j]));
- strcat(commands[i + 1], " VARCHAR(20))");
- }
- else
- {
- strcat(commands[i + 1], table[i][j]);
- strcat(commands[i + 1], " VARCHAR(20), ");
- }
- }
- }
- }
- for (i = 1; i < outLen; i++)
- {
- commands[i + 1] = (char*) malloc((X + 1) * sizeof(char));
- for (j = 0; j < inLen; j++){
- if (j == 0)
- {
- strcpy(commands[i + 1], "INSERT INTO ");
- strcat(commands[i + 1], name);
- strcat(commands[i + 1], " values(");
- strcat(commands[i + 1], table[i][j]);
- strcat(commands[i + 1], ", '");
- }
- else if (j == inLen - 1)
- {
- strncat(commands[i + 1], table[i][j], strlen(table[i][j]));
- strcat(commands[i + 1], "')");
- }
- else
- {
- strcat(commands[i + 1], table[i][j]);
- strcat(commands[i + 1], "', '");
- }
- }
- }
- }
- //////////////////////////////////////////////////////
- PGresult* getSqlResult(char *command);
- PGconn *conn;
- int queryStatus = 0;
- int main(int argc, char* argv[])
- {
- char pom[25];
- char dbconninfo[100]="host=localhost port=5432 dbname=";
- int a;
- printf("Database name: \n");
- scanf("%s",pom);
- strcat(dbconninfo,pom);
- strcat(dbconninfo," user=");
- printf("User:\n");
- scanf("%s",pom);
- strcat(dbconninfo,pom);
- strcat(dbconninfo," password=");
- char* pass = getpass("Password: ");
- strcat(dbconninfo,pass);
- conn = PQconnectdb(dbconninfo);
- if(PQstatus(conn) == CONNECTION_OK) {
- printf("Polaczenie udane\n\n");
- char ***table = (char***) malloc(Z * sizeof(char**)),
- **commands = (char**) malloc((Y + 2) * sizeof(char*)),
- *alterTabS = (char*) malloc((Y + 2) * sizeof(char*));
- int altertab[Y];
- open_file(table, argv[1]);
- alter_f(table, altertab);
- generateCommands(commands, table, argv[1],altertab);
- altertabF(table,altertab,alterTabS,argv[1]);
- doSQL(conn,commands[0]);
- doSQL(conn,commands[1]);
- doSQL(conn,alterTabS);
- for(int i=2;i<=outLen;i++){
- doSQL(conn,commands[i]);
- doSQL(conn,commands[outLen+1]);
- }
- // print(conn,argv[1],stdout);
- }
- else
- printf("Blad polaczenia: %s\n", PQerrorMessage(conn));
- PQfinish(conn);
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement