From 284211e443f011c7ac78682850bcf89bfd91d195 Mon Sep 17 00:00:00 2001 From: "Begerad, Stefan" Date: Thu, 5 Jan 2023 14:12:06 +0100 Subject: [PATCH] feat: add server-version --- example1/readme.md | 6 +++++ lib-version/main.c | 3 +-- lib-version/makefile | 5 +++- lib-version/makefile-fine2 | 24 +++++++++++++++++ server-version/main.c | 50 +++++++++++++++++++++++++++++++++++ server-version/makefile | 24 +++++++++++++++++ server-version/makefile-fine | 21 +++++++++++++++ server-version/makefile-fine2 | 24 +++++++++++++++++ server-version/readme.md | 23 ++++++++++++++++ 9 files changed, 177 insertions(+), 3 deletions(-) create mode 100644 lib-version/makefile-fine2 create mode 100644 server-version/main.c create mode 100644 server-version/makefile create mode 100644 server-version/makefile-fine create mode 100644 server-version/makefile-fine2 create mode 100644 server-version/readme.md diff --git a/example1/readme.md b/example1/readme.md index 85760ff..47c05b6 100644 --- a/example1/readme.md +++ b/example1/readme.md @@ -14,4 +14,10 @@ make ``` ./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/lib-version/main.c b/lib-version/main.c index 9e88959..bc184b8 100644 --- a/lib-version/main.c +++ b/lib-version/main.c @@ -1,9 +1,8 @@ #include #include int main() { + /*return version of libpq*/ int lib_ver = PQlibVersion(); printf("Version of libpq: %d\n", lib_ver); - printf("Version of libpq: %d\n", lib_ver); - printf("Version of libpq: %d\n", lib_ver); return 0; } diff --git a/lib-version/makefile b/lib-version/makefile index 616581c..b14baca 100644 --- a/lib-version/makefile +++ b/lib-version/makefile @@ -14,8 +14,11 @@ EXE = main # Compiler, Linker Defines CC = /usr/bin/gcc # -all: +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/lib-version/makefile-fine2 b/lib-version/makefile-fine2 new file mode 100644 index 0000000..b14baca --- /dev/null +++ b/lib-version/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/server-version/main.c b/server-version/main.c new file mode 100644 index 0000000..bddeff3 --- /dev/null +++ b/server-version/main.c @@ -0,0 +1,50 @@ +#include +#include +#include + +void do_exit(PGconn *conn) { + /*close connection and free memory*/ + PQfinish(conn); + exit(EXIT_SUCCESS); +} + +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{ + conninfo = "dbname=postgres"; + } + + /*connect to database*/ + PGconn *conn = PQconnectdb(conninfo); + + if (PQstatus(conn) == CONNECTION_BAD) { + fprintf(stderr, "Connection to database failed: %s\n", + PQerrorMessage(conn)); + do_exit(conn); + }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); + + /*return integer representing database version*/ + int ver = PQserverVersion(conn); + printf("Server version: %d\n", ver); + + do_exit(conn); +} diff --git a/server-version/makefile b/server-version/makefile new file mode 100644 index 0000000..b14baca --- /dev/null +++ b/server-version/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/server-version/makefile-fine b/server-version/makefile-fine new file mode 100644 index 0000000..616581c --- /dev/null +++ b/server-version/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/server-version/makefile-fine2 b/server-version/makefile-fine2 new file mode 100644 index 0000000..b14baca --- /dev/null +++ b/server-version/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/server-version/readme.md b/server-version/readme.md new file mode 100644 index 0000000..a0dc22d --- /dev/null +++ b/server-version/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