summaryrefslogtreecommitdiff
path: root/database.c
diff options
context:
space:
mode:
Diffstat (limited to 'database.c')
-rw-r--r--database.c136
1 files changed, 116 insertions, 20 deletions
diff --git a/database.c b/database.c
index f13e8a2..751e36d 100644
--- a/database.c
+++ b/database.c
@@ -14,49 +14,145 @@
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 "database.h"
+#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-static int MAX_SIZE_DBMS_NAME = 7; /* including \0 */
-static int MAX_SIZE_CONN_FIELD = 129; /* including \0 */
-
-static char *
-str_create(int size)
-{
- char *str = (char *) malloc(sizeof (char) * size);
- memset(str, '\0', size);
+#include "database.h"
+#include "utils.h"
+#ifdef PSQL
+#include "postgresql.h"
+#endif
- return str;
-}
+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 = str_create(MAX_SIZE_DBMS_NAME);
- conn->host = str_create(MAX_SIZE_CONN_FIELD);
- conn->user = str_create(MAX_SIZE_CONN_FIELD);
- conn->password = str_create(MAX_SIZE_CONN_FIELD);
- conn->dbname = str_create(MAX_SIZE_CONN_FIELD);
+ 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;
}
-void
-qureje_connect()
+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;
}