summaryrefslogtreecommitdiff
path: root/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'main.c')
-rw-r--r--main.c117
1 files changed, 4 insertions, 113 deletions
diff --git a/main.c b/main.c
index 587b41e..7358551 100644
--- a/main.c
+++ b/main.c
@@ -16,131 +16,22 @@
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;
+ if (qureje_connect(conn) != 0) {
+ fprintf(stderr, "Cannot establish connection with the database!\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");
+ printf("Connection established.\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;
-}