summaryrefslogtreecommitdiffstats
path: root/db_demo/db.c
blob: 3a58c3f4b39984ee70102a7788c285e51d90e29e (plain)
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#include <libpq-fe.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include "db.h"

#define DB_MAX_FIELDS 64
#define DB_MAX_CHAR   256

static PGconn *g_pConnection = NULL;
static PGresult *g_pResult = NULL;
static char g_pError[DB_MAX_CHAR];


static int db_clear_error(void)
{
  memset(&g_pError, 0, (size_t)(sizeof(char) * DB_MAX_CHAR));
  return DB_OK;

} // db_clear_error


static int db_set_error(const char *f_pError)
{
  db_clear_error();
  strcpy(g_pError, f_pError);
  return DB_OK;

} // db_set_error


static void db_raise_warning(void)
{
  printf("Database exception!\n");
  printf("%s", g_pError);

} // db_raise_warning


static void db_raise_error(void)
{
  db_raise_warning();
  db_close();
  exit(1);

} // db_raise_error


static char *db_get_error(void)
{
  return g_pError;

} // db_get_error



static int db_check_connection(void)
{
  char l_pError[DB_MAX_CHAR];

  if (g_pConnection == NULL)
  {
    sprintf(l_pError, "Database connection lost!");
    db_set_error(l_pError);
    db_raise_error();

    return DB_NO_CONNECTION;
  }

  return DB_OK;

} // db_check_connection


static int db_check_result(void)
{
  char l_pError[DB_MAX_CHAR];

  if (g_pResult == NULL)
  {
    sprintf(l_pError, "Incorrect PostgreSQL result.");
    db_set_error(l_pError);
    db_raise_warning();

    return DB_INCORRECT_RESULT;
  }

  return DB_OK;

} // db_check_result


int db_init(void)
{
  char l_pError[DB_MAX_CHAR];

  g_pConnection = PQsetdbLogin(PGHOST, PGPORT, PGOPTIONS, PGTTY, DBNAME, LOGIN, PWD);

  if (PQstatus(g_pConnection) == CONNECTION_BAD)
  {
    sprintf(l_pError, "Connection to database '%s' failed.\n%s", DBNAME, PQerrorMessage(g_pConnection));
    db_set_error(l_pError);
    db_raise_error();
		return DB_NO_CONNECTION;
	}

  db_clear_error();

  return DB_OK;

} // db_init


int db_close(void)
{
  PQclear(g_pResult);
	PQfinish(g_pConnection);

  return DB_OK;

} // db_close


int db_output(void)
{
  int l_iNTuples, l_iNFields;
  char l_pText[DB_MAX_CHAR];
  int l_iColumnWidth[DB_MAX_FIELDS];
  int l_iMaxColumnWidth[DB_MAX_FIELDS];
  int i, j, k, l;

  // check the db connection
  if (db_check_connection() != DB_OK)
  {
    exit(1);
    return DB_COULD_NOT_OUTPUT;
  }

  // check the validity of the result
  if (db_check_result() != DB_OK)
  {
    return DB_COULD_NOT_OUTPUT;
  }

  l_iNTuples = PQntuples(g_pResult);
  l_iNFields = PQnfields(g_pResult);

  // copy all tuple names incl. field values
  for (i = 0; i < l_iNFields; i++)
  {
    k = 0;
    for (j = 0; j < l_iNTuples; j++)
    {
      strcpy(l_pText, PQgetvalue(g_pResult, j, i));
      l_iColumnWidth[k] = (int)strlen(l_pText);
      k++;
    }

    strcpy(l_pText, PQfname(g_pResult, i));
    l_iMaxColumnWidth[i] = (int)strlen(l_pText);
    for (j = 1; j < l_iNTuples; j++)
    {
      l_iMaxColumnWidth[i] = max(l_iMaxColumnWidth[i], l_iColumnWidth[j]);
    }
  }

  // print column names
  for (i = 0; i < l_iNFields; i++)
  {
    int len;
    if (i) printf("| ");

    strcpy(l_pText, PQfname(g_pResult, i));
    printf("%s", l_pText);
    len = (int)strlen(PQfname(g_pResult, i)) - 1;

    if (i < l_iNFields - 1)
    {
      for (k = 0; k < l_iMaxColumnWidth[i] - len; k++)
      {
        printf(" ");
      }
    }
  }

  printf("\n");

  // print horizontal line
  l = 0;
  for (k = 0; k < l_iNFields; k++) l += l_iMaxColumnWidth[k];
  l += (l_iNFields - 1) * 3;
  for (k = 0; k < l; k++) printf("-");
  printf("\n");

  // print row values
  for (i = 0; i < l_iNTuples; i++)
  {
    for (j = 0; j < l_iNFields; j++)
    {
      int len;
      if (j) printf("| ");

      strcpy(l_pText, PQgetvalue(g_pResult, i, j));
      printf("%s ", l_pText);
      len = (int)strlen(l_pText) - 1;

      if (j < l_iNFields - 1)
      {
        for (k = 1; k < l_iMaxColumnWidth[j] - len; k++)
        {
          printf(" ");
        }
      }
    }
    printf("\n");
  }

  return DB_OK;

} // db_output


int db_execute(const char* f_pQuery)
{
  char l_pError[DB_MAX_CHAR];
  char l_pCmdStatus[DB_MAX_CHAR];

  // check the db connection
  if (db_check_connection() != DB_OK)
  {
    exit(1);
    return DB_COULD_NOT_EXECUTE;
  }

  g_pResult = PQexec(g_pConnection, f_pQuery);

  if (g_pResult == NULL || (PQresultStatus(g_pResult) != PGRES_COMMAND_OK && PQresultStatus(g_pResult) != PGRES_TUPLES_OK))
  {
    sprintf(l_pError, "Unable to execute query: %s\n%s", f_pQuery, PQerrorMessage(g_pConnection));
    db_set_error(l_pError);
    db_raise_warning();

    return DB_COULD_NOT_EXECUTE;
	}

  sprintf(l_pCmdStatus, "%s", PQcmdStatus(g_pResult));

  if (!strcmp(l_pCmdStatus, "SELECT"))
  {
    db_output();
  }
  else
  {
    printf("%s\n", l_pCmdStatus);
  }

  return DB_OK;

} // db_execute