summaryrefslogtreecommitdiff
path: root/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'main.c')
-rw-r--r--main.c146
1 files changed, 146 insertions, 0 deletions
diff --git a/main.c b/main.c
new file mode 100644
index 0000000..587b41e
--- /dev/null
+++ b/main.c
@@ -0,0 +1,146 @@
+/*
+ qureje is a database managment tool.
+ Copyright (C) 2021 Alessandro Iezzi <aiezzi AT alessandroiezzi PERIOD it>
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <https://www.gnu.org/licenses/>.
+*/
+
+#include <libpq-fe.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "database.h"
+
+char *qureje_parse_arguments(int, char **, qureje_conn *);
+char *qureje_param(char *, char *);
+
+int main(int argc, char** argv)
+{
+ qureje_conn *conn = qureje_conn_create_from_arguments(argc, argv);
+ PGconn *conn1;
+ /*PGresult *res;*/
+ /*int rowcount, colcount, i, j, firstcol;*/
+
+ char *conninfo = qureje_parse_arguments(argc, argv, conn);
+ printf("%s\n", conn->type);
+
+ /* parameter type should be guessed by PostgreSQL */
+ /*const Oid paramTypes[1] = { 0 };*/
+
+ /* parameter value */
+ /*const char * const paramValues[1] = { "pg_database" };*/
+
+ /*
+ * Using an empty connectstring will use default values for everything.
+ * If set, the environment variables PGHOST, PGDATABASE, PGPORT and
+ * PGUSER will be used.
+ */
+ conn1 = PQconnectdb(conninfo);
+
+ /*
+ * This can only happen if there is not enough memory
+ * to allocate the PGconn structure.
+ */
+ if (conn1 == NULL) {
+ fprintf(stderr, "Out of memory connecting to PostgreSQL.\n");
+ return 1;
+ }
+
+ /* check if the connection attempt worked */
+ if (PQstatus(conn1) != CONNECTION_OK) {
+ fprintf(stderr, "%s\n", PQerrorMessage(conn1));
+ /*
+ * Even if the connection failed, the PGconn structure has been
+ * allocated and must be freed.
+ */
+ PQfinish(conn1);
+ return 1;
+ }
+ printf("Hello, World!\n");
+
+ return 0;
+}
+
+char *
+qureje_parse_arguments(int argc, char **argv, qureje_conn *conn)
+{
+ int i = 1, totsize = 0, paramc = 0, verbose = 0;
+ char *conninfo;
+ char **paramv;
+
+ if (argc <= 1) {
+ return "";
+ }
+
+ paramv = (char **) malloc(sizeof (char **) * argc);
+
+ if (strcmp ("-t", argv[i]) == 0 && i < argc) {
+ conn->type = argv[++i];
+ }
+
+ if (conn->type == NULL) {
+ puts("You must specify the database driver.");
+ exit(1);
+ return NULL;
+ }
+
+ while (i < argc) {
+ /* these are all options with a value */
+ if (strcmp ("-h", argv[i]) == 0) {
+ paramv[paramc++] = qureje_param("host", argv[++i]);
+ } else if (strcmp ("-U", argv[i]) == 0) {
+ paramv[paramc++] = qureje_param("user", argv[++i]);
+ } else if (strcmp ("-v", argv[i]) == 0) {
+ verbose = 1;
+ }
+
+ ++i;
+ }
+
+ for (i = 0; i < paramc; i++) {
+ totsize += strlen(paramv[i]);
+ if (i < paramc) totsize ++;
+ }
+
+ conninfo = (char *) malloc(sizeof (char *) * totsize);
+ memset (conninfo, '\0', totsize);
+
+ for (i = 0; i < paramc; i++) {
+ strcat(conninfo, paramv[i]);
+ if (i < argc - 1) strcat(conninfo, " ");
+ }
+
+ if (verbose == 1) {
+ printf("connection info:\n%s\n", conninfo);
+ }
+
+ return conninfo;
+}
+
+char *
+qureje_param(char *parname, char *src)
+{
+ /* the src's legth + the paraname length + '=' + '\0' */
+ int strc = strlen(src) + strlen(parname) + 2;
+ char *param = (char *) malloc(sizeof (char) * strc);
+ memset(param, '\0', strc);
+
+ strcat(param, parname);
+ strcat(param, "=");
+ strcat(param, src);
+
+ return param;
+}