feat: update postgres

This commit is contained in:
iridiumR 2023-03-30 18:12:54 +08:00
parent 19eaeb08db
commit bf9d74db8e
No known key found for this signature in database
GPG key ID: 49735733EB1A32C8

View file

@ -10,12 +10,14 @@
static PGconn *conn;
int pg_connect(char *args) {
if (args == NULL) {
int pg_connect(char **args) {
if (args[2] == NULL) {
fprintf(stderr, "pg connect: need dbname\n");
return 1;
}
conn = PQsetdbLogin("localhost", "5432", NULL, NULL, args, "postgres", NULL);
/* 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 */
if (PQstatus(conn) == CONNECTION_BAD) {
fprintf(stderr, "pg connect: Connection to database failed: %s",
PQerrorMessage(conn));
@ -28,14 +30,17 @@ int pg_list() {
fprintf(stderr, "pg list: not connected to database\n");
return 1;
}
/* Declare a PGresult pointer */
PGresult *res =
PQexec(conn, "SELECT table_name FROM information_schema.tables WHERE "
"table_schema = 'public'");
/* Check to see that the result is successful */
if (PQresultStatus(res) != PGRES_TUPLES_OK) {
fprintf(stderr, "pg list: No data retrieved\n");
PQclear(res);
return 1;
}
/* print out the rows */
int rows = PQntuples(res);
for (int i = 0; i < rows; i++) {
if (i != 0)
@ -46,34 +51,36 @@ int pg_list() {
return 0;
}
int pg_show(char *args) {
int pg_show(char **args) {
if (conn == NULL) {
fprintf(stderr, "pg show: not connected to database\n");
return 1;
}
if (args == NULL) {
fprintf(stderr, "pg show: need table name\n");
if (args[2] == NULL) {
fprintf(stderr, \
"pg show: need table name\n\
eg: pg show table_name\n");
return 1;
}
char *query = malloc(100);
sprintf(query, "SELECT * FROM %s", args);
char query[LINE_BUF_SIZE];
sprintf(query, "SELECT * FROM %s", args[2]);
PGresult *res = PQexec(conn, query);
if (PQresultStatus(res) != PGRES_TUPLES_OK) {
fprintf(stderr, "pg show: No data retrieved\n");
PQclear(res);
return 1;
}
/*neatly print the entire table*/
int rows = PQntuples(res);
int cols = PQnfields(res);
//print header
for (int i = 0; i < cols; i++) {
if (i != 0)
printf(", ");
printf("%s ", PQfname(res, i));
}
printf("\n");
//print whole table
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (j != 0)
@ -82,10 +89,49 @@ int pg_show(char *args) {
}
printf("\n");
}
return 0;
}
int pg_insert(char **args) {
return 0;
}
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);
for (int i = 0; i < cols; i++) {
if (i != 0)
printf(", ");
printf("%s ", PQfname(res, i));
}
printf("\n");
free(query);
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");
}
return 0;
}
@ -94,21 +140,43 @@ int dish_pg(char **args) {
fprintf(stderr, "pg: need arguments\n\
possible arguments:\n\
connect: connect to database\n\
disconnect: disconnect from database\n\
list: list all tables\n\
show: show all contents of a table\n");
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");
return 1;
}
if (strcmp(args[1], "connect") == 0) {
pg_connect(args[2]);
}
if (strcmp(args[1], "list") == 0) {
pg_list();
}
if (strcmp(args[1], "show") == 0) {
pg_show(args[2]);
return pg_connect(args);
}
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);
}
else
fprintf(stderr, "pg: invalid argument\n");
return 1;
}
#endif