From 1725cebc74ca965dae25abd4545ef2ab5c4d5ae5 Mon Sep 17 00:00:00 2001 From: "Begerad, Stefan" Date: Thu, 5 Jan 2023 14:35:31 +0100 Subject: [PATCH] feat: add create_table --- create_table/main.c | 90 +++++++++++++++++++++++++++++++++++++ create_table/makefile | 24 ++++++++++ create_table/makefile-fine | 21 +++++++++ create_table/makefile-fine2 | 24 ++++++++++ create_table/readme.md | 23 ++++++++++ server-version/main.c | 9 ++-- 6 files changed, 188 insertions(+), 3 deletions(-) create mode 100644 create_table/main.c create mode 100644 create_table/makefile create mode 100644 create_table/makefile-fine create mode 100644 create_table/makefile-fine2 create mode 100644 create_table/readme.md diff --git a/create_table/main.c b/create_table/main.c new file mode 100644 index 0000000..eb2868e --- /dev/null +++ b/create_table/main.c @@ -0,0 +1,90 @@ +#include +#include +#include + +void do_exit(PGconn *conn, PGresult *res) { + fprintf(stderr, "%s\n", PQerrorMessage(conn)); + /*every statement result should be freed*/ + PQclear(res); + /*close connection and free memory*/ + PQfinish(conn); + exit(EXIT_FAILURE); +} + +int main(int argc, char **argv) { + /*declaration*/ + const char *conninfo; + + printf("main() Started...\n"); + + /*read command line*/ + if (argc > 1){ + printf("main() argv[1]: %s\n", argv[1]); + conninfo = argv[1]; + }else{ + printf("call ./main \n"); + return 1; + } + + /*connect to database*/ + PGconn *conn = PQconnectdb(conninfo); + if (PQstatus(conn) == CONNECTION_BAD) { + fprintf(stderr, "Connection to database failed: %s\n", + PQerrorMessage(conn)); + PQfinish(conn); + exit(1); + }else if(PQstatus(conn)==CONNECTION_OK){ + printf("main() connected to database\n"); + }else{ + printf("main() connection status NOT known\n"); + } + + char *user = PQuser(conn); + char *db_name = PQdb(conn); + char *pswd = PQpass(conn); + printf("User: %s\n", user); + printf("Database name: %s\n", db_name); + printf("Password: %s\n", pswd); + + /*submit SQL statement to the server and wait for the result*/ + PGresult *res = PQexec(conn, "DROP TABLE IF EXISTS cars_cleanup"); + + if (PQresultStatus(res) != PGRES_COMMAND_OK) { + do_exit(conn, res); + }else{ + printf("main() database table cleaned\n"); + } + /*every statement result should be freed*/ + PQclear(res); + + res = PQexec(conn, "CREATE TABLE cars_cleanup(Id INTEGER PRIMARY KEY, Name VARCHAR(20), Price INT)"); + if (PQresultStatus(res) != PGRES_COMMAND_OK) { + do_exit(conn, res); + }else{ + printf("main() database table created\n"); + } + /*every statement result should be freed*/ + PQclear(res); + + res = PQexec(conn, "INSERT INTO cars_cleanup VALUES(1,'Audi',52642)"); + if (PQresultStatus(res) != PGRES_COMMAND_OK){ + do_exit(conn, res); + }else{ + printf("main() record inserted into database table\n"); + } + /*every statement result should be freed*/ + PQclear(res); + + res = PQexec(conn, "INSERT INTO cars_cleanup VALUES(2,'Mercedes',57127)"); + if (PQresultStatus(res) != PGRES_COMMAND_OK) { + do_exit(conn, res); + }else{ + printf("main() record inserted into database table\n"); + } + /*every statement result should be freed*/ + PQclear(res); + + PQfinish(conn); + return 0; +} + diff --git a/create_table/makefile b/create_table/makefile new file mode 100644 index 0000000..b14baca --- /dev/null +++ b/create_table/makefile @@ -0,0 +1,24 @@ +# Docu +# +#$@ is the name of the file to be made +#$? is the names of the changed dependents +#$< the name of the related file that caused the action +#$* the prefix shared by target and dependent files +# +# Others +RM = /bin/rm -f +# +# Source, Executable, Includes, Library Defines +EXE = main +# +# Compiler, Linker Defines +CC = /usr/bin/gcc +# +all: main.o + $(CC) main.c -I/usr/include/postgresql -L/usr/lib/x86_64-linux-gnu -lpq -std=c99 -Wall -o $(EXE) +# +main.o: + $(CC) -c main.c -I/usr/include/postgresql +# Clean Up Objects, Exectuables, Dumps out of source directory +clean: + $(RM) *.o $(EXE) *~ diff --git a/create_table/makefile-fine b/create_table/makefile-fine new file mode 100644 index 0000000..616581c --- /dev/null +++ b/create_table/makefile-fine @@ -0,0 +1,21 @@ +# Docu +# +#$@ is the name of the file to be made +#$? is the names of the changed dependents +#$< the name of the related file that caused the action +#$* the prefix shared by target and dependent files +# +# Others +RM = /bin/rm -f +# +# Source, Executable, Includes, Library Defines +EXE = main +# +# Compiler, Linker Defines +CC = /usr/bin/gcc +# +all: + $(CC) main.c -I/usr/include/postgresql -L/usr/lib/x86_64-linux-gnu -lpq -std=c99 -Wall -o $(EXE) +# Clean Up Objects, Exectuables, Dumps out of source directory +clean: + $(RM) *.o $(EXE) *~ diff --git a/create_table/makefile-fine2 b/create_table/makefile-fine2 new file mode 100644 index 0000000..b14baca --- /dev/null +++ b/create_table/makefile-fine2 @@ -0,0 +1,24 @@ +# Docu +# +#$@ is the name of the file to be made +#$? is the names of the changed dependents +#$< the name of the related file that caused the action +#$* the prefix shared by target and dependent files +# +# Others +RM = /bin/rm -f +# +# Source, Executable, Includes, Library Defines +EXE = main +# +# Compiler, Linker Defines +CC = /usr/bin/gcc +# +all: main.o + $(CC) main.c -I/usr/include/postgresql -L/usr/lib/x86_64-linux-gnu -lpq -std=c99 -Wall -o $(EXE) +# +main.o: + $(CC) -c main.c -I/usr/include/postgresql +# Clean Up Objects, Exectuables, Dumps out of source directory +clean: + $(RM) *.o $(EXE) *~ diff --git a/create_table/readme.md b/create_table/readme.md new file mode 100644 index 0000000..a0dc22d --- /dev/null +++ b/create_table/readme.md @@ -0,0 +1,23 @@ +* build + +``` +make +``` + +* run + +``` +./main 'postgresql://begerad:secret@localhost:5432/sib00_vbn_gtfs' +``` + +* or run + +``` +./main 'postgresql://begerad:secret@localhost:5432/vbn_data' +``` + +* or run + +``` +./main "host=localhost port=5432 user=begerad password= dbname=vbn_data" +``` \ No newline at end of file diff --git a/server-version/main.c b/server-version/main.c index bddeff3..d24c103 100644 --- a/server-version/main.c +++ b/server-version/main.c @@ -3,9 +3,10 @@ #include void do_exit(PGconn *conn) { + fprintf(stderr, "%s\n", PQerrorMessage(conn)); /*close connection and free memory*/ PQfinish(conn); - exit(EXIT_SUCCESS); + exit(EXIT_FAILURE); } int main(int argc, char **argv) { @@ -19,7 +20,8 @@ int main(int argc, char **argv) { printf("main() argv[1]: %s\n", argv[1]); conninfo = argv[1]; }else{ - conninfo = "dbname=postgres"; + printf("call ./main \n"); + return 1; } /*connect to database*/ @@ -46,5 +48,6 @@ int main(int argc, char **argv) { int ver = PQserverVersion(conn); printf("Server version: %d\n", ver); - do_exit(conn); + PQfinish(conn); + return 0; }