Advertisement
Guest User

Untitled

a guest
Apr 4th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.62 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <stdbool.h>
  5. #include <libpq-fe.h>
  6. #include <unistd.h>
  7. #define BIG 1000
  8. #define SMALL 200
  9.  
  10. void printTuples(PGresult *result){
  11. int m, n;
  12. int nRows = PQntuples(result);
  13. int nFields = PQnfields(result);
  14. printf(" Number of rows returned = %d\n", nRows);
  15. printf(" Number of fields returned = %d\n\n", nFields);
  16. for (m = 0; m < nRows; m++){
  17. for (n = 0; n < nFields; n++){
  18. printf("%16s = %24s;\n", PQfname(result, n), PQgetvalue(result, m, n));
  19. }
  20. printf("\n");
  21. }
  22. }
  23.  
  24. void doSQL(PGconn *conn, char *command){
  25. PGresult *result;
  26. printf("> %s\n", command);
  27. printf("\n_____________________________________\n");
  28. result = PQexec(conn, command);
  29. printf(" Status is : %s\n", PQresStatus(PQresultStatus(result)));
  30. printf(" Rows affected : %s\n", PQcmdTuples(result));
  31. printf(" Result message: %s\n", PQresultErrorMessage(result));
  32. switch (PQresultStatus(result)){
  33. case PGRES_TUPLES_OK:
  34. printTuples(result);
  35. break;
  36. }
  37. printf("_____________________________________\n\n");
  38. PQclear(result);
  39. }
  40.  
  41. char *getField(char *line, int num){
  42. char *tok;
  43. tok = strtok(line, ";");
  44.  
  45. for (tok ; ;){
  46. if (!--num){
  47. return tok;
  48. }
  49. tok = strtok(NULL, ";\n");
  50.  
  51. if (tok == NULL) {
  52. return "null";
  53. }
  54. }
  55. }
  56.  
  57.  
  58. void createConnection(int argc, char *dbname, char *result){
  59. char createdb[SMALL] = { "\0" };
  60. char username[SMALL] = { "\0" };
  61. char *password;
  62. printf("\tLog in\n");
  63. printf("______________________________\n");
  64. if (argc == 2){
  65. printf("Database Name: ");
  66. scanf("%s", dbname);
  67. }
  68. printf("Username: ");
  69. scanf("%s", username);
  70. password = getpass("Password: ");
  71. printf("______________________________\n\n");
  72. sprintf(result, "host=localhost port=5432 dbname=%s user=%s password=%s", dbname, username, password);
  73. }
  74.  
  75.  
  76. void crTlName(char *source, char *result){
  77. strncat(result, source, strlen(source) - 4);
  78. }
  79.  
  80. void dropTl(char *tblName, char *result){
  81. sprintf(result, "DROP TABLE %s;", tblName);
  82. }
  83.  
  84. int countColumns(FILE *file){
  85. char line[BIG] = { "\0" };
  86. fseek(file, 0, 0);
  87. fgets(line, BIG, file);
  88. char *tmp = strdup(line);
  89. int i, counter = 1;
  90. for (i = 0; i < strlen(tmp); i++){
  91. if (tmp[i] == ';'){
  92. counter++;
  93. }
  94. }
  95. free(tmp);
  96. return counter;
  97. }
  98.  
  99. void createHeaders(FILE *file, char *result){
  100. char line[BIG] = { "\0" };
  101. char headers[BIG] = "(";
  102. fseek(file, 0, 0);
  103. fgets(line, BIG, file);
  104. char *tmp = strdup(line);
  105. int i = 1, counter = countColumns(file);
  106. while (counter--){
  107. tmp = strdup(line);
  108. strcat(headers, getField(tmp, i));
  109. strcat(headers, ", ");
  110. i++;
  111. }
  112. strncat(result, headers, strlen(headers) - 2);
  113. strcat(result, ")");
  114. free(tmp);
  115. }
  116.  
  117. void createTbl(FILE *file, char *tblName, char *result){
  118. char line[BIG] = { "\0" };
  119. char columns[BIG] = { "\0" };
  120. fgets(line, BIG, file);
  121. char *tmp = strdup(line);
  122. int i = 1, counter = countColumns(file);
  123. while (counter--){
  124. tmp = strdup(line);
  125. strcat(columns, "\t");
  126. strcat(columns, getField(tmp, i));
  127. strcat(columns, " VARCHAR(20),\n");
  128. i++;
  129. }
  130. sprintf(result, "CREATE TABLE %s(\n\tid SERIAL,\n", tblName);
  131. strncat(result, columns, strlen(columns) - 2);
  132. strcat(result, "\n);");
  133. free(tmp);
  134. }
  135.  
  136. void alterTbl(char *tblName, char *columnName, int size, char *result){
  137. sprintf(result, "ALTER TABLE %s ALTER COLUMN %s TYPE VARCHAR(%d);", tblName, columnName, size);
  138. }
  139.  
  140. void insert(PGconn *conn, FILE *file, char *tblName){
  141. char line[BIG] = { "\0" };
  142. char headers[SMALL] = { "\0" };
  143. createHeaders(file, headers);
  144. int i = 1, j = 0, counter = countColumns(file);
  145. fseek(file, 0, 0);
  146. char head[SMALL] = { "\0" };
  147. fgets(head, SMALL, file);
  148. int maxLength[counter + 1];
  149. for (i = 1; i <= (counter + 1); i++){
  150. maxLength[i] = 20;
  151. }
  152. while (fgets(line, BIG, file)){
  153. char insertTbl[BIG] = "INSERT INTO ";
  154. strcat(insertTbl, tblName);
  155. strcat(insertTbl, headers);
  156. strcat(insertTbl, " VALUES\n(");
  157. char *tmp;
  158. i = 1;
  159. j = counter;
  160. while (j--){
  161. tmp = strdup(line);
  162. strcat(insertTbl, "'");
  163. strcat(insertTbl, getField(tmp, i));
  164. tmp = strdup(line);
  165. if (strlen(getField(tmp, i)) > maxLength[i]){
  166. tmp = strdup(line);
  167. maxLength[i] = strlen(getField(tmp, i));
  168. char alterTblCommand[SMALL];
  169. tmp = strdup(head);
  170. alterTbl(tblName, (char*) getField(tmp, i), maxLength[i], alterTblCommand);
  171. doSQL(conn, alterTblCommand);
  172. }
  173. strcat(insertTbl, "'");
  174. if (j != 0) strcat(insertTbl, ",");
  175. i++;
  176. }
  177. strcat(insertTbl, ");");
  178. doSQL(conn, insertTbl);
  179. }
  180. }
  181.  
  182. void selectTbl(char *tblName, char *result){
  183. sprintf(result, "SELECT * FROM %s;", tblName);
  184. }
  185.  
  186. int main(int argc, char *argv[]){
  187. FILE *file = fopen(argv[1], "r");
  188. char connectionInfo[SMALL] = { "\0" };
  189. createConnection(argc, argv[1], connectionInfo);
  190. PGconn *conn = PQconnectdb(connectionInfo);
  191.  
  192. char tblName[SMALL] = { "\0" };
  193. char dropTblCommand[SMALL] = { "\0" };
  194. char createTblCommand[BIG] = { "\0" };
  195. char selectTblCommand[SMALL] = { "\0" };
  196. int orderBy = 1;
  197. crTlName(argv[1], tblName);
  198. dropTl(tblName, dropTblCommand);
  199. createTbl(file, tblName, createTblCommand);
  200. selectTbl(tblName, selectTblCommand);
  201. doSQL(conn, dropTblCommand);
  202. doSQL(conn, createTblCommand);
  203. insert(conn, file, tblName);
  204. doSQL(conn, selectTblCommand);
  205.  
  206. PQfinish(conn);
  207. return 0;
  208. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement