2023-03-29 15:01:10 +00:00
|
|
|
#include "postgres.h"
|
|
|
|
#include <libpq-fe.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <strings.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#ifdef POSTGRES
|
|
|
|
|
|
|
|
static PGconn *conn;
|
|
|
|
|
2023-03-30 10:12:54 +00:00
|
|
|
int pg_connect(char **args) {
|
|
|
|
if (args[2] == NULL) {
|
2023-03-29 15:01:10 +00:00
|
|
|
fprintf(stderr, "pg connect: need dbname\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2023-03-30 10:12:54 +00:00
|
|
|
/* Make a connection to the database */
|
|
|
|
conn = PQsetdbLogin("localhost", "5432", NULL, NULL, args[2], "postgres", NULL);
|
|
|
|
/* Check to see that the backend connection was successfully made */
|
2023-03-29 15:01:10 +00:00
|
|
|
if (PQstatus(conn) == CONNECTION_BAD) {
|
|
|
|
fprintf(stderr, "pg connect: Connection to database failed: %s",
|
|
|
|
PQerrorMessage(conn));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
int pg_list() {
|
|
|
|
if (conn == NULL) {
|
|
|
|
fprintf(stderr, "pg list: not connected to database\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2023-03-30 10:12:54 +00:00
|
|
|
/* Declare a PGresult pointer */
|
2023-03-29 15:01:10 +00:00
|
|
|
PGresult *res =
|
|
|
|
PQexec(conn, "SELECT table_name FROM information_schema.tables WHERE "
|
|
|
|
"table_schema = 'public'");
|
2023-03-30 10:12:54 +00:00
|
|
|
/* Check to see that the result is successful */
|
2023-03-29 15:01:10 +00:00
|
|
|
if (PQresultStatus(res) != PGRES_TUPLES_OK) {
|
|
|
|
fprintf(stderr, "pg list: No data retrieved\n");
|
|
|
|
PQclear(res);
|
|
|
|
return 1;
|
|
|
|
}
|
2023-03-30 10:12:54 +00:00
|
|
|
/* print out the rows */
|
2023-03-29 15:01:10 +00:00
|
|
|
int rows = PQntuples(res);
|
|
|
|
for (int i = 0; i < rows; i++) {
|
|
|
|
if (i != 0)
|
|
|
|
printf(", ");
|
|
|
|
printf("%s ", PQgetvalue(res, i, 0));
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-03-30 10:12:54 +00:00
|
|
|
int pg_show(char **args) {
|
2023-03-29 15:01:10 +00:00
|
|
|
if (conn == NULL) {
|
|
|
|
fprintf(stderr, "pg show: not connected to database\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2023-03-30 10:12:54 +00:00
|
|
|
if (args[2] == NULL) {
|
|
|
|
fprintf(stderr, \
|
|
|
|
"pg show: need table name\n\
|
|
|
|
eg: pg show table_name\n");
|
2023-03-29 15:01:10 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2023-03-30 10:12:54 +00:00
|
|
|
|
|
|
|
char query[LINE_BUF_SIZE];
|
|
|
|
sprintf(query, "SELECT * FROM %s", args[2]);
|
2023-03-29 15:01:10 +00:00
|
|
|
PGresult *res = PQexec(conn, query);
|
|
|
|
if (PQresultStatus(res) != PGRES_TUPLES_OK) {
|
|
|
|
fprintf(stderr, "pg show: No data retrieved\n");
|
|
|
|
PQclear(res);
|
|
|
|
return 1;
|
|
|
|
}
|
2023-03-30 10:12:54 +00:00
|
|
|
/*neatly print the entire table*/
|
2023-03-29 15:01:10 +00:00
|
|
|
int rows = PQntuples(res);
|
|
|
|
int cols = PQnfields(res);
|
|
|
|
|
|
|
|
for (int i = 0; i < cols; i++) {
|
|
|
|
if (i != 0)
|
|
|
|
printf(", ");
|
|
|
|
printf("%s ", PQfname(res, i));
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
for (int i = 0; i < rows; i++) {
|
|
|
|
for (int j = 0; j < cols; j++) {
|
|
|
|
if (j != 0)
|
|
|
|
printf(", ");
|
|
|
|
printf("%s ", PQgetvalue(res, i, j));
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
}
|
2023-03-30 10:12:54 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int pg_insert(char **args) {
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2023-03-29 15:01:10 +00:00
|
|
|
|
2023-03-30 10:12:54 +00:00
|
|
|
int pg_run(char **args) {
|
|
|
|
if (conn == NULL) {
|
|
|
|
fprintf(stderr, "pg run: not connected to database\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (args[2] == NULL) {
|
|
|
|
fprintf(stderr, \
|
|
|
|
"pg run: need command\n\
|
|
|
|
eg: pg run \"SELECT * FROM table_name\"\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
PGresult *res = PQexec(conn, args[2]);
|
|
|
|
if (PQresultStatus(res) != PGRES_TUPLES_OK) {
|
|
|
|
fprintf(stderr, "pg run: No data retrieved\n");
|
|
|
|
PQclear(res);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
/*neatly print the result*/
|
|
|
|
int rows = PQntuples(res);
|
|
|
|
int cols = PQnfields(res);
|
2023-03-29 15:01:10 +00:00
|
|
|
|
2023-03-30 10:12:54 +00:00
|
|
|
for (int i = 0; i < cols; i++) {
|
|
|
|
if (i != 0)
|
|
|
|
printf(", ");
|
|
|
|
printf("%s ", PQfname(res, i));
|
|
|
|
}
|
2023-03-29 15:01:10 +00:00
|
|
|
printf("\n");
|
2023-03-30 10:12:54 +00:00
|
|
|
for (int i = 0; i < rows; i++) {
|
|
|
|
for (int j = 0; j < cols; j++) {
|
|
|
|
if (j != 0)
|
|
|
|
printf(", ");
|
|
|
|
printf("%s ", PQgetvalue(res, i, j));
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
}
|
2023-03-29 15:01:10 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int dish_pg(char **args) {
|
|
|
|
if (args[1] == NULL) {
|
|
|
|
fprintf(stderr, "pg: need arguments\n\
|
|
|
|
possible arguments:\n\
|
|
|
|
connect: connect to database\n\
|
2023-03-30 10:12:54 +00:00
|
|
|
disconnect: disconnect from database\n\
|
2023-03-29 15:01:10 +00:00
|
|
|
list: list all tables\n\
|
2023-03-30 10:12:54 +00:00
|
|
|
show: show all contents of a table\n\
|
|
|
|
insert: insert into a table\n\
|
|
|
|
delete: delete from a table\n\
|
|
|
|
run: run a custom command\n");
|
2023-03-29 15:01:10 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (strcmp(args[1], "connect") == 0) {
|
2023-03-30 10:12:54 +00:00
|
|
|
return pg_connect(args);
|
2023-03-29 15:01:10 +00:00
|
|
|
}
|
2023-03-30 10:12:54 +00:00
|
|
|
|
|
|
|
else if (strcmp(args[1], "disconnect") == 0) {
|
|
|
|
if (conn != NULL) {
|
|
|
|
PQfinish(conn);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
fprintf(stderr, "pg disconnect: not connected to database\n");
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
} else if (strcmp(args[1], "list") == 0) {
|
|
|
|
return pg_list();
|
|
|
|
|
|
|
|
} else if (strcmp(args[1], "show") == 0) {
|
|
|
|
return pg_show(args);
|
|
|
|
|
|
|
|
} else if (strcmp(args[1], "insert") == 0) {
|
|
|
|
return pg_insert(args);
|
|
|
|
|
|
|
|
} else if(strcmp(args[1],"run")==0)
|
|
|
|
{
|
|
|
|
return pg_run(args);
|
2023-03-29 15:01:10 +00:00
|
|
|
}
|
2023-03-30 10:12:54 +00:00
|
|
|
else
|
|
|
|
fprintf(stderr, "pg: invalid argument\n");
|
2023-03-29 15:01:10 +00:00
|
|
|
|
2023-03-30 10:12:54 +00:00
|
|
|
return 1;
|
2023-03-29 15:01:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|