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
|
/*
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;
}
|