Advertisement
Guest User

Untitled

a guest
Apr 5th, 2016
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.21 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <libpq-fe.h>
  4. #include <string.h>
  5. #include <termios.h>
  6.  
  7. //funkcja pobierajaca od uzytkownika dane do logowania (podawanie hasla jest ukryte!)
  8. char *ConnectionData(){
  9. struct termios oflags, nflags;
  10. char *username;
  11. char *password;
  12. username=malloc(20*sizeof(char));
  13. password=malloc(20*sizeof(char));
  14.  
  15. printf("Prosze podac nazwe uzytkownika:\n");
  16. scanf("%s", username);
  17.  
  18. /*wylacz echo */
  19. tcgetattr(fileno(stdin), &oflags);
  20. nflags = oflags;
  21. nflags.c_lflag &= ~ECHO;
  22. nflags.c_lflag |= ECHONL;
  23.  
  24. if (tcsetattr(fileno(stdin), TCSANOW, &nflags) != 0) {
  25. perror("tcsetattr");
  26. }
  27.  
  28. printf("Prosze podac haslo (najwyzsze standardy bezpieczenstwa!):\n");
  29. scanf("%s", password);
  30. fgets(password, sizeof(password), stdin);
  31. password[strlen(password) - 1] = 0;
  32.  
  33. /* wlacz echo */
  34. if (tcsetattr(fileno(stdin), TCSANOW, &oflags) != 0) {
  35. perror("tcsetattr");
  36. }
  37.  
  38. char *conninfo;
  39. conninfo=malloc(100*sizeof(char));
  40.  
  41. strcpy(conninfo, "dbname=");
  42. strcat(conninfo, username);
  43. strcat(conninfo, " user=");
  44. strcat(conninfo, username);
  45. strcat(conninfo, " password=");
  46. strcat(conninfo, password);
  47.  
  48. return (char *)conninfo;
  49. }
  50. //funkcja wysylajaca zapytanie
  51. void Query(PGconn *connection, char *command)
  52. {
  53. PGresult *result;
  54. result = PQexec(connection, command);
  55.  
  56. if(PQresultStatus(result)==PGRES_TUPLES_OK) {
  57. int a, b;
  58. int row_number = PQntuples(result);
  59. int field_number = PQnfields(result);
  60.  
  61. for(a = 0; a < row_number; a++) {
  62. for(b = 0; b < field_number; b++)
  63. printf(" %s = %s(%d), ", PQfname(result, b), PQgetvalue(result, a, b),PQgetlength(result, a, b));
  64. printf("\n");
  65. }
  66.  
  67. }
  68. printf("\n");
  69. printf("%s\n", PQresStatus(PQresultStatus(result)));
  70. printf("number of rows affected: %s\n", PQcmdTuples(result));
  71. printf("result: %s\n", PQresultErrorMessage(result));
  72.  
  73. PQclear(result);
  74. }
  75.  
  76.  
  77. int tableExists(PGconn *connection, char *table)
  78. {
  79.  
  80. PGresult *result;
  81. char *statment;
  82. statment=malloc(150*sizeof(char));
  83. strcpy(statment, "SELECT * FROM information_schema.tables WHERE table_name='");
  84. strcat(statment, table);
  85. strcat(statment, "';");
  86. result = PQexec(connection, statment);
  87. int flag = atoi(PQcmdTuples(result));
  88. PQclear(result);
  89. if (flag==0){
  90. printf("Tabela '%s' nie istnieje", table);
  91. return 0;
  92. }
  93. return 1;
  94. }
  95.  
  96. int columnExists(PGconn *connection, char *table, char *column)
  97. {
  98. PGresult *result;
  99. char *statment;
  100. statment=malloc(150*sizeof(char));
  101. strcpy(statment, "SELECT * FROM information_schema.columns WHERE table_name='");
  102. strcat(statment, table);
  103. strcat(statment, "' AND column_name='");
  104. strcat(statment, column);
  105. strcat(statment, "';");
  106. result = PQexec(connection, statment);
  107. int flag = atoi(PQcmdTuples(result));
  108. PQclear(result);
  109. if (flag==0){
  110. printf("Kolumna '%s' nie istnieje", column);
  111. return 0;
  112. }
  113. return 1;
  114. }
  115.  
  116.  
  117. main(){
  118.  
  119. char *conninfo;
  120. conninfo=malloc(100*sizeof(char));
  121. conninfo=ConnectionData();
  122.  
  123.  
  124. PGconn *connection = PQconnectdb(conninfo);
  125. if(PQstatus(connection) == CONNECTION_OK){
  126. printf("Polaczono!\n");
  127. printf("PGDBNAME = %s\n", PQdb(connection));
  128. printf("PGUSER = %s\n", PQuser(connection));
  129. printf("PGHOST = %s\n", PQhost(connection));
  130. printf("PGPORT = %s\n", PQport(connection));
  131. printf("OPTIONS = %s\n", PQoptions(connection));
  132. }
  133.  
  134. else{
  135. printf("Blad Polaczenia!\n");
  136. printf("Polaczenie nieudane: %s\n", PQerrorMessage(connection));
  137. }
  138. //zakladam "menu"
  139. int closemenu=0;
  140.  
  141. while(closemenu != 1){
  142. char option[20];
  143. /*wyswietlam opcje*/
  144. printf("\n\n\nDostepne opcje:\n");
  145. printf("WYSWIETL - wyswietli na ekranie zawartosc wybranej tabeli\n");
  146. printf("DODAJ - dodaje rekord do tabeli\n");
  147. printf("USUN - usuwa rekordy z wybranej tabeli\n");
  148. printf("EDYTUJ - edytuje rekord w tabeli\n");
  149. printf("ZNAJDZ - wyszukuje konkretnych danych z wybranej tabeli\n");
  150. printf("WYZWALACZ - doda trigger udaremniajacy dodawanie nowych rekordow do wybranej tabeli\n");
  151. printf("USUNWYZWALACZ - usunie wybrany wyzwalacz\n");
  152. printf("HTML - utworzy strone html z zawartoscia wybranych tabel\n");
  153. printf("Q - wychodzi z programu\n");
  154.  
  155. scanf("%s", option);
  156.  
  157. if(!strcmp(option, "WYSWIETL"))
  158. {
  159.  
  160. char table[50], statm[200];
  161.  
  162. printf("Podaj nazwe tabeli: ");
  163. scanf("%s", table);
  164. //sprawdzam czy tabela istnieje
  165. if(tableExists(connection, table)==0) continue;
  166.  
  167. sprintf(statm, "SELECT * FROM %s;", table);
  168. printf("WYSWIETLAM TABELE %s:\n", table);
  169. Query(connection, statm);
  170.  
  171. }
  172. else if(!strcmp(option, "DODAJ"))
  173. {
  174.  
  175. char table[30], statm[200];
  176. PGresult *result;
  177. int a, b;
  178.  
  179. printf("Podaj nazwe tabeli: ");
  180. scanf("%s", table);
  181. if(tableExists(connection, table)==0) continue;
  182. //potrzeba informacji o nazwach kolumn
  183. sprintf(statm, "SELECT column_name FROM information_schema.columns WHERE table_name='%s';", table);
  184. result = PQexec(connection, statm);
  185. int row_number = PQntuples(result);
  186. int field_number = PQnfields(result);
  187. if(PQresultStatus(result)==PGRES_TUPLES_OK) {
  188. //tworze polecenie
  189. sprintf(statm, "INSERT INTO %s (", table);
  190.  
  191. for(a = 0; a < row_number; a++) {
  192. for(b = 0; b < field_number; b++) {
  193. char *column = PQgetvalue(result, a, b);
  194. strcat(statm, column);
  195. if (row_number>a+1)
  196. strcat(statm, ", ");
  197. }
  198. }
  199. strcat(statm, ") VALUES (");
  200.  
  201. for(a = 0; a < row_number; a++) {
  202. for(b = 0; b < field_number; b++) {
  203. char value[150];
  204. char *column = PQgetvalue(result, a, b);
  205. printf("%s=", column);
  206. scanf("%s", value);
  207. strcat(statm, "'");
  208. strcat(statm, value);
  209. strcat(statm, "'");
  210.  
  211. if (row_number > a+1)
  212. strcat(statm, ", ");
  213. }
  214. }
  215.  
  216. strcat(statm, ");");
  217.  
  218. }
  219.  
  220. //oolecenie kompletne
  221. result = PQexec(connection, statm);
  222.  
  223. printf("\n");
  224. printf("%s\n", PQresStatus(PQresultStatus(result)));
  225. printf("number of rows affected: %s\n", PQcmdTuples(result));
  226. printf("result: %s\n", PQresultErrorMessage(result));
  227.  
  228. PQclear(result);
  229.  
  230. }
  231. else if (!strcmp(option, "USUN"))
  232. {
  233. char table[30], statm[200];
  234.  
  235. printf("Podaj tabele: ");
  236. scanf("%s", table);
  237. if(tableExists(connection, table)==0) continue;
  238.  
  239. sprintf(statm, "DELETE FROM %s;", table);
  240. Query(connection, statm);
  241. }
  242. else if(!strcmp(option, "EDYTUJ"))
  243. {
  244. char table[50];
  245. char column1[50];
  246. char column2[50];
  247. char field[50];
  248. char field2[50];
  249. char statm[200];
  250. //sprintf(sql, "SELECT * FROM information_schema.tables WHERE table_name='%s';", table);
  251. printf("Podaj tabele: ");
  252. scanf("%s", table);
  253. if(tableExists(connection, table)==0) continue;
  254.  
  255. printf("Podaj kolumne do edycji: ");
  256. scanf("%s", column1);
  257.  
  258. if(columnExists(connection, table, column1)==0) continue;
  259.  
  260. printf("Podaj nowa wartosc pola ");
  261. scanf("%s", field);
  262.  
  263. printf("Podaj nazwe kolumny po ktorej chcesz okreslic, ktory rekord ma zostac zmodyfikowany (wpisujac * zmodyfikujesz wszystkie rekordy) : ");
  264. scanf("%s", column2);
  265. if (!strcmp(column2, "*")) {
  266. sprintf(statm, "UPDATE %s SET %s='%s';", table, column1, field);
  267. Query(connection, statm);
  268. continue;
  269. }
  270.  
  271. if(columnExists(connection, table, column2)==0) continue;
  272.  
  273. printf("Podaj wartosc wybranej kolumny: ");
  274. scanf("%s", field2);
  275.  
  276. sprintf(statm, "UPDATE %s SET %s='%s' WHERE %s='%s'", table, column1, field, column2, field2);
  277. Query(connection, statm);
  278.  
  279. }
  280. else if(!strcmp(option, "ZNAJDZ"))
  281. {
  282. char table[50];
  283. char column[50];
  284. char data[50];
  285. char statm[200];
  286.  
  287. printf("Podaj tabele: ");
  288. scanf("%s", table);
  289. if(tableExists(connection, table)==0) continue;
  290.  
  291. printf("Podaj kolumne: ");
  292. scanf("%s", column);
  293. if(columnExists(connection, table, column)==0) continue;
  294.  
  295. printf("Podaj jakich danych szukac: ");
  296. scanf("%s", data);
  297. sprintf(statm, "SELECT * FROM %s WHERE %s='%s';", table, column, data);
  298. printf("WYSWIETLAM WSZYSTKIE REKORDY TABELI %s GDZIE %s MA WARTOSC %s:\n", table, column, data);
  299. Query(connection, statm);
  300. }
  301. else if(!strcmp(option, "WYZWALACZ"))
  302. {
  303. char table[50];
  304. char statm[200];
  305. char trigger[50];
  306. char Q[20];
  307. int n;
  308. printf("Czy chcesz wyswietlic liste wszystkich wyzwalaczy, ktore mozesz zainstalowac?[Y/N] ");
  309. scanf("%s", Q);
  310. if(!strcmp(Q,"Y")){
  311. printf("(1) - uniemozliwia dodawanie rekordow do podanej tabeli)\n");
  312. printf("(2) - uniemozliwia usuwanie rekordow z podanej tabeli)\n");
  313. printf("(3) - wyzwalacz podczas usuwania konia wyswietla jak kon mial na imie i kto go posiadal\n");
  314. }
  315. printf("Ktory numer chcesz zainstalowac?");
  316. scanf("%d", &n);
  317. printf("Podaj nazwe triggera: ");
  318. scanf("%s", trigger);
  319. switch(n){
  320. case 1:
  321. {
  322. printf("Podaj tabele: ");
  323. scanf("%s", table);
  324. if(tableExists(connection, table)==0) continue;
  325. sprintf(statm, "CREATE TRIGGER %s BEFORE INSERT ON %s FOR EACH ROW EXECUTE PROCEDURE dont_insert();", trigger, table);
  326. Query(connection, "CREATE OR REPLACE FUNCTION dont_insert() RETURNS TRIGGER AS $$ BEGIN RETURN NULL; END; $$ LANGUAGE 'plpgsql';");
  327. Query(connection, statm);
  328. }
  329. case 2:
  330. {
  331. printf("Podaj tabele: ");
  332. scanf("%s", table);
  333. if(tableExists(connection, table)==0) continue;
  334. sprintf(statm, "CREATE TRIGGER %s BEFORE DELETE ON %s FOR EACH ROW EXECUTE PROCEDURE dont_delete();", trigger, table);
  335. Query(connection, "CREATE OR REPLACE FUNCTION dont_delete() RETURNS TRIGGER AS $$ BEGIN RETURN NULL; END; $$ LANGUAGE 'plpgsql';");
  336. Query(connection, statm);
  337. }
  338. case 3:
  339. sprintf(statm, "CREATE TRIGGER kon_wlasciciel AFTER DELETE ON wlasciciel_has_kon FOR EACH ROW EXECUTE PROCEDURE trig_kon_wlasciciel();");
  340. Query(connection, "CREATE OR REPLACE FUNCTION trig_kon_wlasciciel() RETURNS TRIGGER AS $$ DECLARE po_nazwa VARCHAR(20)"
  341. "; 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);"
  342. "RAISE NOTICE 'Usunieto konia % wlascicielowi %.', prz_nazwa, po_nazwa;"
  343. "RETURN NEW; END; $$ LANGUAGE 'plpgsql';");
  344. Query(connection, statm);
  345. }
  346.  
  347.  
  348. }
  349. else if(!strcmp(option, "USUNWYZWALACZ"))
  350. {
  351. char table[50];
  352. char statm[200];
  353. char trigger[50];
  354. char Q[10];
  355. char statm2[200];
  356.  
  357.  
  358.  
  359.  
  360. printf("Podaj tabele: ");
  361. scanf("%s", table);
  362. if(tableExists(connection, table)==0) continue;
  363.  
  364. printf("Czy chcesz wyswietlic liste wszystkich wyzwalaczy znajdujacych sie na tabeli %s?[Y/N] ", table);
  365. scanf("%s", Q);
  366. if(!strcmp(Q,"Y")){
  367. 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);
  368. Query(connection, statm2);
  369. }
  370.  
  371.  
  372.  
  373. printf("Podaj nazwe triggera, ktory chcesz usunac: ");
  374. scanf("%s", trigger);
  375.  
  376. sprintf(statm, "DROP TRIGGER %s ON %s;", trigger, table);
  377. Query(connection, statm);
  378. }
  379. else if(!strcmp(option, "HTML"))
  380. {
  381. PGresult *result;
  382. char table[30];
  383. char name[15];
  384. char statm[200];
  385. printf("Podaj nazwe pliku, ktory chcesz stworzyc ");
  386. scanf("%s", name);
  387. FILE *fp = fopen(name, "w");
  388. if (!fp) {
  389. printf("Wystapil blad przy tworzeniu plikui\n");
  390. continue;
  391. }
  392. fprintf(fp, "<!DOCTYPE html>\n"
  393. "<html>\n"
  394. "<head>\n"
  395. "<style>table,th,td\n"
  396. "{border:1px solid black; width 600px;}\n"
  397. "th{background:green;}\n"
  398. "td{color:red;}\n"
  399. "</style>\n"
  400. "<title>Hop Hop</title>\n"
  401. "</head>\n"
  402. "<body>\n");
  403.  
  404. printf("Podaj tabele (EOF [CRTL + D] konczy wprowadzanie): ");
  405. while (scanf("%s", table) != EOF) {
  406.  
  407. if(tableExists(connection, table)==0) continue;
  408.  
  409. sprintf(statm, "SELECT * FROM %s;", table);
  410. result = PQexec(connection, statm);
  411.  
  412. fprintf(fp, "<h1>%s</h1>"
  413. "<table>\n", table);
  414. if(PQresultStatus(result)==PGRES_TUPLES_OK){
  415. int a, b;
  416. int row_number = PQntuples(result);
  417. int field_number = PQnfields(result);
  418.  
  419. fprintf(fp, "<tr>\n");
  420. for(a = 0; a < field_number; a++)
  421. {
  422. fprintf(fp, "<th>");
  423. fprintf(fp, "%s", PQfname(result, a));
  424. fprintf(fp, "</th>\n");
  425. }
  426. fprintf(fp, "</tr>\n");
  427.  
  428. for(a = 0; a < row_number; a++) {
  429. fprintf(fp, "<tr>");
  430. for(b = 0; b < field_number; b++)
  431. {
  432. fprintf(fp, "<td>");
  433. fprintf(fp, "%s", PQgetvalue(result, a, b));
  434. fprintf(fp, "</td>");
  435. }
  436. fprintf(fp, "</tr>\n");
  437. }
  438. fprintf(fp, "</table>");
  439. PQclear(result);
  440. }
  441. printf("Podaj tabele (EOF [CRTL + D] konczy wprowadzanie): ");
  442. }
  443. fprintf(fp, "</body>\n"
  444. "</html>");
  445. fclose(fp);
  446. printf("\nWygenerowano plik %s! Moze jego kod nie jest najpiekniejszy, ale za to ladnie sie waliduje!\n", name);
  447. }
  448. else if(!strcmp(option, "Q"))
  449. {
  450. closemenu=1;
  451. }
  452. }
  453. PQfinish(connection);
  454. return EXIT_SUCCESS;
  455.  
  456. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement