From dd6a6b12e3d87d5e53c96ba713b26f66fc59353a Mon Sep 17 00:00:00 2001 From: Stefan Begerad Date: Wed, 15 Mar 2023 15:36:56 +0100 Subject: [PATCH] feat(server-version): initial commit --- README.md | 12 +++++++- server-version/main.c | 53 +++++++++++++++++++++++++++++++++++ server-version/makefile | 24 ++++++++++++++++ server-version/makefile-fine | 21 ++++++++++++++ server-version/makefile-fine2 | 24 ++++++++++++++++ server-version/readme.md | 23 +++++++++++++++ 6 files changed, 156 insertions(+), 1 deletion(-) 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/README.md b/README.md index 93bc2ab..cb642b1 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,13 @@ # rgncycle-libpq -libpq API for regional cycle \ No newline at end of file +libpq API for regional cycle + +## preparation + +``` +sudo apt update +sudo apt install make --no-install-recommends +sudo apt install build-essential --no-install-recommends +sudo apt install libpq-dev --no-install-recommends +``` + diff --git a/server-version/main.c b/server-version/main.c new file mode 100644 index 0000000..d24c103 --- /dev/null +++ b/server-version/main.c @@ -0,0 +1,53 @@ +#include +#include +#include + +void do_exit(PGconn *conn) { + fprintf(stderr, "%s\n", PQerrorMessage(conn)); + /*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)); + 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); + + PQfinish(conn); + return 0; +} 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..3289c59 --- /dev/null +++ b/server-version/readme.md @@ -0,0 +1,23 @@ +* build + +``` +make +``` + +* run + +``` +./main 'postgresql://:@:/' +``` + +* or run + +``` +./main 'postgresql://:@:/' +``` + +* or run + +``` +./main "host= port= user= password=<> dbname=" +```