/* qureje is a database managment tool. Copyright (C) 2022 Alessandro Iezzi 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 . */ #include #include #include #include "database.h" #include "utils.h" #ifdef PSQL #include "postgresql.h" #endif static int MAX_SIZE_DBMS_NAME = 15; static int MAX_SIZE_CONN_FIELD = 129; /* including \0 */ qureje_conn * qureje_conn_create() { qureje_conn *conn = (qureje_conn *) malloc (sizeof (qureje_conn)); conn->type = NONE; conn->host = strcrt(MAX_SIZE_CONN_FIELD); conn->user = strcrt(MAX_SIZE_CONN_FIELD); conn->password = strcrt(MAX_SIZE_CONN_FIELD); conn->dbname = strcrt(MAX_SIZE_CONN_FIELD); return conn; } static int opt_get_type(qureje_conn *conn, char *opt, char *optv) { if (strcmp("-t", opt) == 0 || strcmp("--type", opt) == 0) { if (strcmp("psql", optv) == 0) { conn->type = POSTGRESQL; } else if (strcmp("mariadb", optv) == 0) { conn->type = MARIADB; } else { conn->type = NONE; } return 0; } return 1; } static int opt_get_user(qureje_conn *conn, char *opt, char *optv) { if (strcmp("-u", opt) == 0 || strcmp("--username", opt) == 0) { strncpy(conn->user, optv, MAX_SIZE_CONN_FIELD); return 0; } return 1; } static void opt_get_dbname(qureje_conn *conn, char *opt) { strncpy(conn->dbname, opt, MAX_SIZE_DBMS_NAME); } qureje_conn * qureje_conn_create_from_arguments(int argc, char **argv) { int i, j; qureje_conn *conn = qureje_conn_create(); /* Parameters: -t, --type -u, --username -p, --password -P, --no-password -h, --hostname */ for (i = 1, j = 2; i < argc; i++, j++) { if (opt_get_type(conn, argv[i], argv[j]) == 0 || opt_get_user(conn, argv[i], argv[j]) == 0) { i = j++; continue; } /* this call must be the last one */ opt_get_dbname(conn, argv[i]); } return conn; } int qureje_connect(qureje_conn *conn) { #ifdef PSQL if (conn->type == POSTGRESQL) { printf("Establishing a PostgreSQL connection.\n"); return qureje_psql_connect(conn); } #endif return 0; } char * qureje_get_type(qureje_conn *conn) { int max_length = 11; char *str_type = strcrt(max_length); switch (conn->type) { case POSTGRESQL: #ifdef PSQL strncpy(str_type, "PostgreSQL", max_length); #else printf("This program is not compiled with the support to PostgreSQL!\n"); exit(1); #endif break; case MARIADB: strncpy(str_type, "MariaDB", max_length); break; default: strncpy(str_type, "None", max_length); break; } return str_type; } char * qureje_get_user(qureje_conn *conn) { return conn->user; } char * qureje_get_dbname(qureje_conn *conn) { return conn->dbname; }