Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <libpq-fe.h>
- #include <string.h>
- #include <termios.h>
- //funkcja pobierajaca od uzytkownika dane do logowania (podawanie hasla jest ukryte!)
- char *ConnectionData(){
- struct termios oflags, nflags;
- char *username;
- char *password;
- username=malloc(20*sizeof(char));
- password=malloc(20*sizeof(char));
- printf("Prosze podac nazwe uzytkownika:\n");
- scanf("%s", username);
- /*wylacz echo */
- tcgetattr(fileno(stdin), &oflags);
- nflags = oflags;
- nflags.c_lflag &= ~ECHO;
- nflags.c_lflag |= ECHONL;
- if (tcsetattr(fileno(stdin), TCSANOW, &nflags) != 0) {
- perror("tcsetattr");
- }
- printf("Prosze podac haslo (najwyzsze standardy bezpieczenstwa!):\n");
- scanf("%s", password);
- fgets(password, sizeof(password), stdin);
- password[strlen(password) - 1] = 0;
- /* wlacz echo */
- if (tcsetattr(fileno(stdin), TCSANOW, &oflags) != 0) {
- perror("tcsetattr");
- }
- char *conninfo;
- conninfo=malloc(100*sizeof(char));
- strcpy(conninfo, "dbname=");
- strcat(conninfo, username);
- strcat(conninfo, " user=");
- strcat(conninfo, username);
- strcat(conninfo, " password=");
- strcat(conninfo, password);
- return (char *)conninfo;
- }
- //funkcja wysylajaca zapytanie
- void Query(PGconn *connection, char *command)
- {
- PGresult *result;
- result = PQexec(connection, command);
- if(PQresultStatus(result)==PGRES_TUPLES_OK) {
- int a, b;
- int row_number = PQntuples(result);
- int field_number = PQnfields(result);
- for(a = 0; a < row_number; a++) {
- for(b = 0; b < field_number; b++)
- printf(" %s = %s(%d), ", PQfname(result, b), PQgetvalue(result, a, b),PQgetlength(result, a, b));
- printf("\n");
- }
- }
- printf("\n");
- printf("%s\n", PQresStatus(PQresultStatus(result)));
- printf("number of rows affected: %s\n", PQcmdTuples(result));
- printf("result: %s\n", PQresultErrorMessage(result));
- PQclear(result);
- }
- int tableExists(PGconn *connection, char *table)
- {
- PGresult *result;
- char *statment;
- statment=malloc(150*sizeof(char));
- strcpy(statment, "SELECT * FROM information_schema.tables WHERE table_name='");
- strcat(statment, table);
- strcat(statment, "';");
- result = PQexec(connection, statment);
- int flag = atoi(PQcmdTuples(result));
- PQclear(result);
- if (flag==0){
- printf("Tabela '%s' nie istnieje", table);
- return 0;
- }
- return 1;
- }
- int columnExists(PGconn *connection, char *table, char *column)
- {
- PGresult *result;
- char *statment;
- statment=malloc(150*sizeof(char));
- strcpy(statment, "SELECT * FROM information_schema.columns WHERE table_name='");
- strcat(statment, table);
- strcat(statment, "' AND column_name='");
- strcat(statment, column);
- strcat(statment, "';");
- result = PQexec(connection, statment);
- int flag = atoi(PQcmdTuples(result));
- PQclear(result);
- if (flag==0){
- printf("Kolumna '%s' nie istnieje", column);
- return 0;
- }
- return 1;
- }
- main(){
- char *conninfo;
- conninfo=malloc(100*sizeof(char));
- conninfo=ConnectionData();
- PGconn *connection = PQconnectdb(conninfo);
- if(PQstatus(connection) == CONNECTION_OK){
- printf("Polaczono!\n");
- printf("PGDBNAME = %s\n", PQdb(connection));
- printf("PGUSER = %s\n", PQuser(connection));
- printf("PGHOST = %s\n", PQhost(connection));
- printf("PGPORT = %s\n", PQport(connection));
- printf("OPTIONS = %s\n", PQoptions(connection));
- }
- else{
- printf("Blad Polaczenia!\n");
- printf("Polaczenie nieudane: %s\n", PQerrorMessage(connection));
- }
- //zakladam "menu"
- int closemenu=0;
- while(closemenu != 1){
- char option[20];
- /*wyswietlam opcje*/
- printf("\n\n\nDostepne opcje:\n");
- printf("WYSWIETL - wyswietli na ekranie zawartosc wybranej tabeli\n");
- printf("DODAJ - dodaje rekord do tabeli\n");
- printf("USUN - usuwa rekordy z wybranej tabeli\n");
- printf("EDYTUJ - edytuje rekord w tabeli\n");
- printf("ZNAJDZ - wyszukuje konkretnych danych z wybranej tabeli\n");
- printf("WYZWALACZ - doda trigger udaremniajacy dodawanie nowych rekordow do wybranej tabeli\n");
- printf("USUNWYZWALACZ - usunie wybrany wyzwalacz\n");
- printf("HTML - utworzy strone html z zawartoscia wybranych tabel\n");
- printf("Q - wychodzi z programu\n");
- scanf("%s", option);
- if(!strcmp(option, "WYSWIETL"))
- {
- char table[50], statm[200];
- printf("Podaj nazwe tabeli: ");
- scanf("%s", table);
- //sprawdzam czy tabela istnieje
- if(tableExists(connection, table)==0) continue;
- sprintf(statm, "SELECT * FROM %s;", table);
- printf("WYSWIETLAM TABELE %s:\n", table);
- Query(connection, statm);
- }
- else if(!strcmp(option, "DODAJ"))
- {
- char table[30], statm[200];
- PGresult *result;
- int a, b;
- printf("Podaj nazwe tabeli: ");
- scanf("%s", table);
- if(tableExists(connection, table)==0) continue;
- //potrzeba informacji o nazwach kolumn
- sprintf(statm, "SELECT column_name FROM information_schema.columns WHERE table_name='%s';", table);
- result = PQexec(connection, statm);
- int row_number = PQntuples(result);
- int field_number = PQnfields(result);
- if(PQresultStatus(result)==PGRES_TUPLES_OK) {
- //tworze polecenie
- sprintf(statm, "INSERT INTO %s (", table);
- for(a = 0; a < row_number; a++) {
- for(b = 0; b < field_number; b++) {
- char *column = PQgetvalue(result, a, b);
- strcat(statm, column);
- if (row_number>a+1)
- strcat(statm, ", ");
- }
- }
- strcat(statm, ") VALUES (");
- for(a = 0; a < row_number; a++) {
- for(b = 0; b < field_number; b++) {
- char value[150];
- char *column = PQgetvalue(result, a, b);
- printf("%s=", column);
- scanf("%s", value);
- strcat(statm, "'");
- strcat(statm, value);
- strcat(statm, "'");
- if (row_number > a+1)
- strcat(statm, ", ");
- }
- }
- strcat(statm, ");");
- }
- //oolecenie kompletne
- result = PQexec(connection, statm);
- printf("\n");
- printf("%s\n", PQresStatus(PQresultStatus(result)));
- printf("number of rows affected: %s\n", PQcmdTuples(result));
- printf("result: %s\n", PQresultErrorMessage(result));
- PQclear(result);
- }
- else if (!strcmp(option, "USUN"))
- {
- char table[30], statm[200];
- printf("Podaj tabele: ");
- scanf("%s", table);
- if(tableExists(connection, table)==0) continue;
- sprintf(statm, "DELETE FROM %s;", table);
- Query(connection, statm);
- }
- else if(!strcmp(option, "EDYTUJ"))
- {
- char table[50];
- char column1[50];
- char column2[50];
- char field[50];
- char field2[50];
- char statm[200];
- //sprintf(sql, "SELECT * FROM information_schema.tables WHERE table_name='%s';", table);
- printf("Podaj tabele: ");
- scanf("%s", table);
- if(tableExists(connection, table)==0) continue;
- printf("Podaj kolumne do edycji: ");
- scanf("%s", column1);
- if(columnExists(connection, table, column1)==0) continue;
- printf("Podaj nowa wartosc pola ");
- scanf("%s", field);
- printf("Podaj nazwe kolumny po ktorej chcesz okreslic, ktory rekord ma zostac zmodyfikowany (wpisujac * zmodyfikujesz wszystkie rekordy) : ");
- scanf("%s", column2);
- if (!strcmp(column2, "*")) {
- sprintf(statm, "UPDATE %s SET %s='%s';", table, column1, field);
- Query(connection, statm);
- continue;
- }
- if(columnExists(connection, table, column2)==0) continue;
- printf("Podaj wartosc wybranej kolumny: ");
- scanf("%s", field2);
- sprintf(statm, "UPDATE %s SET %s='%s' WHERE %s='%s'", table, column1, field, column2, field2);
- Query(connection, statm);
- }
- else if(!strcmp(option, "ZNAJDZ"))
- {
- char table[50];
- char column[50];
- char data[50];
- char statm[200];
- printf("Podaj tabele: ");
- scanf("%s", table);
- if(tableExists(connection, table)==0) continue;
- printf("Podaj kolumne: ");
- scanf("%s", column);
- if(columnExists(connection, table, column)==0) continue;
- printf("Podaj jakich danych szukac: ");
- scanf("%s", data);
- sprintf(statm, "SELECT * FROM %s WHERE %s='%s';", table, column, data);
- printf("WYSWIETLAM WSZYSTKIE REKORDY TABELI %s GDZIE %s MA WARTOSC %s:\n", table, column, data);
- Query(connection, statm);
- }
- else if(!strcmp(option, "WYZWALACZ"))
- {
- char table[50];
- char statm[200];
- char trigger[50];
- char Q[20];
- int n;
- printf("Czy chcesz wyswietlic liste wszystkich wyzwalaczy, ktore mozesz zainstalowac?[Y/N] ");
- scanf("%s", Q);
- if(!strcmp(Q,"Y")){
- printf("(1) - uniemozliwia dodawanie rekordow do podanej tabeli)\n");
- printf("(2) - uniemozliwia usuwanie rekordow z podanej tabeli)\n");
- printf("(3) - wyzwalacz podczas usuwania konia wyswietla jak kon mial na imie i kto go posiadal\n");
- }
- printf("Ktory numer chcesz zainstalowac?");
- scanf("%d", &n);
- printf("Podaj nazwe triggera: ");
- scanf("%s", trigger);
- switch(n){
- case 1:
- {
- printf("Podaj tabele: ");
- scanf("%s", table);
- if(tableExists(connection, table)==0) continue;
- sprintf(statm, "CREATE TRIGGER %s BEFORE INSERT ON %s FOR EACH ROW EXECUTE PROCEDURE dont_insert();", trigger, table);
- Query(connection, "CREATE OR REPLACE FUNCTION dont_insert() RETURNS TRIGGER AS $$ BEGIN RETURN NULL; END; $$ LANGUAGE 'plpgsql';");
- Query(connection, statm);
- }
- case 2:
- {
- printf("Podaj tabele: ");
- scanf("%s", table);
- if(tableExists(connection, table)==0) continue;
- sprintf(statm, "CREATE TRIGGER %s BEFORE DELETE ON %s FOR EACH ROW EXECUTE PROCEDURE dont_delete();", trigger, table);
- Query(connection, "CREATE OR REPLACE FUNCTION dont_delete() RETURNS TRIGGER AS $$ BEGIN RETURN NULL; END; $$ LANGUAGE 'plpgsql';");
- Query(connection, statm);
- }
- case 3:
- sprintf(statm, "CREATE TRIGGER kon_wlasciciel AFTER DELETE ON wlasciciel_has_kon FOR EACH ROW EXECUTE PROCEDURE trig_kon_wlasciciel();");
- Query(connection, "CREATE OR REPLACE FUNCTION trig_kon_wlasciciel() RETURNS TRIGGER AS $$ DECLARE po_nazwa VARCHAR(20)"
- "; prz_nazwa VARCHAR(20); BEGIN po_nazwa:=(SELECT imie FROM kon WHERE idkon=OLD.idkon); prz_nazwa:=(SELECT imie FROM wlasciciel WHERE idwlasciciel=OLD.idwlasciciel);"
- "RAISE NOTICE 'Usunieto konia % wlascicielowi %.', prz_nazwa, po_nazwa;"
- "RETURN NEW; END; $$ LANGUAGE 'plpgsql';");
- Query(connection, statm);
- }
- }
- else if(!strcmp(option, "USUNWYZWALACZ"))
- {
- char table[50];
- char statm[200];
- char trigger[50];
- char Q[10];
- char statm2[200];
- printf("Podaj tabele: ");
- scanf("%s", table);
- if(tableExists(connection, table)==0) continue;
- printf("Czy chcesz wyswietlic liste wszystkich wyzwalaczy znajdujacych sie na tabeli %s?[Y/N] ", table);
- scanf("%s", Q);
- if(!strcmp(Q,"Y")){
- sprintf(statm2, "SELECT event_object_table,trigger_name,event_manipulation,action_statement,action_timing FROM information_schema.triggers WHERE event_object_table='%s' ORDER BY event_object_table,event_manipulation;", table);
- Query(connection, statm2);
- }
- printf("Podaj nazwe triggera, ktory chcesz usunac: ");
- scanf("%s", trigger);
- sprintf(statm, "DROP TRIGGER %s ON %s;", trigger, table);
- Query(connection, statm);
- }
- else if(!strcmp(option, "HTML"))
- {
- PGresult *result;
- char table[30];
- char name[15];
- char statm[200];
- printf("Podaj nazwe pliku, ktory chcesz stworzyc ");
- scanf("%s", name);
- FILE *fp = fopen(name, "w");
- if (!fp) {
- printf("Wystapil blad przy tworzeniu plikui\n");
- continue;
- }
- fprintf(fp, "<!DOCTYPE html>\n"
- "<html>\n"
- "<head>\n"
- "<style>table,th,td\n"
- "{border:1px solid black; width 600px;}\n"
- "th{background:green;}\n"
- "td{color:red;}\n"
- "</style>\n"
- "<title>Hop Hop</title>\n"
- "</head>\n"
- "<body>\n");
- printf("Podaj tabele (EOF [CRTL + D] konczy wprowadzanie): ");
- while (scanf("%s", table) != EOF) {
- if(tableExists(connection, table)==0) continue;
- sprintf(statm, "SELECT * FROM %s;", table);
- result = PQexec(connection, statm);
- fprintf(fp, "<h1>%s</h1>"
- "<table>\n", table);
- if(PQresultStatus(result)==PGRES_TUPLES_OK){
- int a, b;
- int row_number = PQntuples(result);
- int field_number = PQnfields(result);
- fprintf(fp, "<tr>\n");
- for(a = 0; a < field_number; a++)
- {
- fprintf(fp, "<th>");
- fprintf(fp, "%s", PQfname(result, a));
- fprintf(fp, "</th>\n");
- }
- fprintf(fp, "</tr>\n");
- for(a = 0; a < row_number; a++) {
- fprintf(fp, "<tr>");
- for(b = 0; b < field_number; b++)
- {
- fprintf(fp, "<td>");
- fprintf(fp, "%s", PQgetvalue(result, a, b));
- fprintf(fp, "</td>");
- }
- fprintf(fp, "</tr>\n");
- }
- fprintf(fp, "</table>");
- PQclear(result);
- }
- printf("Podaj tabele (EOF [CRTL + D] konczy wprowadzanie): ");
- }
- fprintf(fp, "</body>\n"
- "</html>");
- fclose(fp);
- printf("\nWygenerowano plik %s! Moze jego kod nie jest najpiekniejszy, ale za to ladnie sie waliduje!\n", name);
- }
- else if(!strcmp(option, "Q"))
- {
- closemenu=1;
- }
- }
- PQfinish(connection);
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement