1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
|
/*
qureje is a database managment tool.
Copyright (C) 2022 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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#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;
}
|