feat(builtin): add support for pgsql

This commit is contained in:
iridiumR 2023-03-29 23:01:10 +08:00
parent f99caf42f3
commit 4171428076
No known key found for this signature in database
GPG key ID: 49735733EB1A32C8
6 changed files with 140 additions and 4 deletions

View file

@ -1,7 +1,7 @@
SHELL := /bin/bash SHELL := /bin/bash
CC = gcc CC = gcc
SRC = main.c loop.c exec.c line.c builtin.c SRC = main.c loop.c exec.c line.c builtin.c postgres.c
LINK= -lreadline LINK= -lreadline -lpq
OBJ = $(SRC:.c=.ro) OBJ = $(SRC:.c=.ro)
DBGOBJ = $(SRC:.c=.do) DBGOBJ = $(SRC:.c=.do)
EXE = dish EXE = dish

View file

@ -22,11 +22,11 @@ int dish_echo(char **args);
int dish_laststatus(char **args); int dish_laststatus(char **args);
char *builtin_cmd[] = {"cd", "help", "exit", "clear", "setenv", char *builtin_cmd[] = {"cd", "help", "exit", "clear", "setenv",
"getenv", "unsetenv", "echo", "laststatus"}; "getenv", "unsetenv", "echo", "laststatus", defined_builtin};
int (*builtin_func[])(char **) = { int (*builtin_func[])(char **) = {
&dish_cd, &dish_help, &dish_exit, &dish_clear, &dish_setenv, &dish_cd, &dish_help, &dish_exit, &dish_clear, &dish_setenv,
&dish_getenv, &dish_unsetenv, &dish_echo, &dish_laststatus}; &dish_getenv, &dish_unsetenv, &dish_echo, &dish_laststatus, defined_builtin_func};
int getBuiltinNum() { return sizeof(builtin_cmd) / sizeof(char *); } int getBuiltinNum() { return sizeof(builtin_cmd) / sizeof(char *); }

View file

@ -8,6 +8,11 @@
#define _BUILTIN_H_ #define _BUILTIN_H_
#include "config.h" #include "config.h"
#ifdef POSTGRES
#include "postgres.h"
#endif
int getBuiltinNum(); int getBuiltinNum();
#endif #endif

View file

@ -17,4 +17,11 @@
#define ANSI_COLOR_CYAN "\x1b[36m" #define ANSI_COLOR_CYAN "\x1b[36m"
#define ANSI_COLOR_RESET "\x1b[0m" #define ANSI_COLOR_RESET "\x1b[0m"
/* enable postgres support*/
#define POSTGRES
#ifdef POSTGRES
#define defined_builtin "pg"
#define defined_builtin_func &dish_pg
#endif
#endif #endif

114
postgres.c Normal file
View file

@ -0,0 +1,114 @@
#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;
int pg_connect(char *args) {
if (args == NULL) {
fprintf(stderr, "pg connect: need dbname\n");
return 1;
}
conn = PQsetdbLogin("localhost", "5432", NULL, NULL, args, "postgres", NULL);
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;
}
PGresult *res =
PQexec(conn, "SELECT table_name FROM information_schema.tables WHERE "
"table_schema = 'public'");
if (PQresultStatus(res) != PGRES_TUPLES_OK) {
fprintf(stderr, "pg list: No data retrieved\n");
PQclear(res);
return 1;
}
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;
}
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");
return 1;
}
char *query = malloc(100);
sprintf(query, "SELECT * FROM %s", args);
PGresult *res = PQexec(conn, query);
if (PQresultStatus(res) != PGRES_TUPLES_OK) {
fprintf(stderr, "pg show: No data retrieved\n");
PQclear(res);
return 1;
}
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)
printf(", ");
printf("%s ", PQgetvalue(res, i, j));
}
printf("\n");
}
printf("\n");
free(query);
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\
list: list all tables\n\
show: show all contents of a table\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 0;
}
#endif

10
postgres.h Normal file
View file

@ -0,0 +1,10 @@
#include "config.h"
#ifdef POSTGRES
#ifndef __POSTGRES_H__
#define __POSTGRES_H__
int dish_pg(char **args);
#endif
#endif