File: test.cc

package info (click to toggle)
sqlxx 2.2.1-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,600 kB
  • ctags: 189
  • sloc: sh: 6,631; cpp: 1,379; makefile: 89
file content (402 lines) | stat: -rw-r--r-- 11,659 bytes parent folder | download | duplicates (3)
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
#include <iostream>
#include <sqlxx.h>
#include <unistd.h>
#include <strutilsxx.h>
#include <time.h>

using namespace std;
using namespace sqlxx;
using namespace strutilsxx;

#define MYSQLUSER	"root"
#define MYSQLPASS	""
#define MYSQLDB		"sqlxxtest"
#define MYSQLDEFDB	"mysql"
#define MYSQLHOST	"localhost"
#define MYSQLPORT	3128
#define MYSQLDRIVER     "/usr/lib/libmyodbc.so"
#define MYSQLSOCKET	"/var/run/mysqld/mysqld.sock"

#define PGSQLUSER	"postgres"
#define PGSQLPASS	""
#define PGSQLDB		"sqlxxtest"
#define PGSQLDEFDB      "postgres"
#define PGSQLHOST	"localhost"
#define PGSQLPORT	5432
#define PGSQLDRIVER	"/usr/lib/libpsqlodbc.so"
#define PGSQLSOCKET	"/var/run/postgresql/.s.PGSQL.5432"

#define CREATEDB "create database sqlxxtest;"
#define DROPDB "drop database sqlxxtest;"

#define CREATETABLE "create table test (id int4 not null, name text, zahli int8, zahlf float, nichts text null);"
#define DROPTABLE "drop table test;"

int iTime;

void main(void) {
  CSQL SQL;
  CSQLResult *Result;
  int iDBType, iConnectType, iAccessType, iCount;
  int iRows;
  short iCols;
  string sFields, sName, sNull;
  float fZahl;
  int iZahl, iID;
  bool bError;
  
  try {
    for(iDBType=0;iDBType<2;iDBType++) {
      // Loop DB Types (0=Mysql, 1=Postgres)
      for (iAccessType=0;iAccessType<2;iAccessType++) {
        // Loop Access Types (0=Native, 1=ODBC)
        for (iConnectType=0;iConnectType<2;iConnectType++) {
          // Loop Connect Types (0=Network, 1=Socket)

          // Postgres with ODBC and Socket is currently not working
          if (iDBType && iAccessType && iConnectType) continue;
                      
          switch (iDBType) {
            case 0:
              SQL.setUsername(MYSQLUSER);
              SQL.setPassword(MYSQLPASS);
              SQL.setDatabase("");
              if (iConnectType) {
                SQL.setSocket(MYSQLSOCKET);
                SQL.setHostname("");
                SQL.setPort(0);
              }
              else {
                SQL.setHostname(MYSQLHOST);
                SQL.setPort(MYSQLPORT);
                SQL.setSocket("");
              }
              if (iAccessType) {
                SQL.setDriver(MYSQLDRIVER);
                SQL.setType(SQLXX_ODBC);
              } else {
                SQL.setDriver("");
                SQL.setType(SQLXX_MYSQL);
              }
              break;
            case 1:
              SQL.setUsername(PGSQLUSER);
              SQL.setPassword(PGSQLPASS);
              SQL.setDatabase("");
              if (iConnectType) {
                SQL.setSocket(PGSQLSOCKET);
                SQL.setHostname("");
                SQL.setPort(0);
              }
              else {
                SQL.setHostname(PGSQLHOST);
                SQL.setPort(PGSQLPORT);
                SQL.setSocket("");
              }
              if (iAccessType) {
                SQL.setDriver(PGSQLDRIVER);
                SQL.setType(SQLXX_ODBC);
              } else {
                SQL.setType(SQLXX_POSTGRES);
                SQL.setDriver("");
              }
              break;
          }
          
          if (iDBType)
            cout << "Connecting to PostgreSQL";
          else
            cout << "Connecting to MySQL";
          if (iAccessType)
            cout << " (ODBC)";
          else
            cout << " (Native)";
          if (iConnectType)
            cout << " over Socket";
          else
            cout << " over Network";
            
          cout << "... ";

          if (iAccessType)          
            switch (iDBType) {
              case 0:
                SQL.setDatabase(MYSQLDEFDB);
                break;
              case 1:
               SQL.setDatabase(PGSQLDEFDB);
            }
          else
            SQL.setDatabase(""); 
          SQL.connect();
          cout << "OK" << endl;
          
          cout << "Creating test database... ";
          SQL.execQuery(CREATEDB);
          cout << "OK" << endl;

          cout << "Reconnecting with new database name... ";
          SQL.disconnect();

          switch (iDBType) {
            case 0:
              SQL.setDatabase(MYSQLDB);
              break;
            case 1:
              SQL.setDatabase(PGSQLDB);
          }
          SQL.connect();
          cout << "OK" << endl;

          cout << "Creating table test... ";
          SQL.execQuery(CREATETABLE);
          cout << "OK" << endl;

          cout << "Inserting 100 items... ";
          for (iCount=0;iCount<100;iCount++) {
            SQL.execQuery(format(
              "INSERT INTO test VALUES (%i,'Item %i',%i,%f,NULL);",
              SQL.getNewID("test","id"),iCount,iCount,25.4
            ));
          }
          cout << "OK" << endl;
          
          cout << "Open query... ";
          Result=SQL.openQuery("SELECT * from test");
          cout << "OK" << endl;
          
          cout << "Counting rows... ";
          iRows=Result->getNumRows();
          cout << iRows;
          if (iRows==100)
            cout << " -> OK" << endl;
          else
            cout << " -> ERROR!" << endl;
          
          cout << "Counting cols... ";
          iCols=Result->getNumCols();
          cout << iCols;
          if (iCols==5)
            cout << " -> OK" << endl;
          else
            cout << " -> ERROR!" << endl;
            
          cout << "Getting field names... ";
          sFields=Result->getColName(0);
          sFields+=" ";
          sFields+=Result->getColName(1);
          sFields+=" ";
          sFields+=Result->getColName(2);
          sFields+=" ";
          sFields+=Result->getColName(3);
          sFields+=" ";
          sFields+=Result->getColName(4);
          cout << sFields;
          if (sFields=="id name zahli zahlf nichts")
            cout << " -> OK" << endl;
          else
            cout << " -> ERROR!" << endl;
            
          cout << "Getting field types... ";
          Result->getColType(0);
          Result->getColType(1);
          Result->getColType(2);
          Result->getColType(3);
          Result->getColType(4);
          cout << "OK" << endl;
          
          cout << "Checking boundary in getColName... ";
          try {
            sFields=Result->getColName(5);
            cout << "ERROR!" << endl;
          }
          catch (sqlxx_error E) {
            cout << "OK" << endl;
          }
          
          cout << "Checking boundary in getColType... ";
          try {
            Result->getColType(5);
            cout << "ERROR!" << endl;
          }
          catch (sqlxx_error E) {
            cout << "OK" << endl;
          }
          
          cout << "Checking boundary in get... ";
          try {
            Result->get(5);
            cout << "ERROR!" << endl;
          }
          catch (sqlxx_error E) {
            cout << "OK" << endl;
          }

          cout << "Checking boundary in getInt... ";
          try {
            Result->getInt(5);
            cout << "ERROR!" << endl;
          }
          catch (sqlxx_error E) {
            cout << "OK" << endl;
          }


          cout << "Checking boundary in getFloat... ";
          try {
            Result->getFloat(5);
            cout << "ERROR!" << endl;
          }
          catch (sqlxx_error E) {
            cout << "OK" << endl;
          }
          

          cout << "Checking boundary in isNull... ";
          try {
            Result->isNull(5);
            cout << "ERROR!" << endl;
          }
          catch (sqlxx_error E) {
            cout << "OK" << endl;
          }

          cout << "Checking 'unknown field' name in get... ";
          try {
            Result->get("non-existant");
            cout << "ERROR!" << endl;
          }
          catch (sqlxx_error E) {
            cout << "OK" << endl;
          }

          cout << "Checking 'unknown field' name in getInt... ";
          try {
            Result->getInt("non-existant");
            cout << "ERROR!" << endl;
          }
          catch (sqlxx_error E) {
            cout << "OK" << endl;
          }

          cout << "Checking 'unknown field' name in getFloat... ";
          try {
            Result->getFloat("non-existant");
            cout << "ERROR!" << endl;
          }
          catch (sqlxx_error E) {
            cout << "OK" << endl;
          }

          cout << "Checking 'unknown field' name in isNull... ";
          try {
            Result->isNull("non-existant");
            cout << "ERROR!" << endl;
          }
          catch (sqlxx_error E) {
            cout << "OK" << endl;
          }

          cout << "Fetching data... ";
          bError=false;
          while (Result->fetch()) {
            iID=Result->getInt(0);
            sName=Result->get(1);
            iZahl=Result->getInt(2);
            fZahl=Result->getFloat(3);
            sNull=Result->get(4);
            if (!Result->isNull(4)) bError=true;
            if (fZahl!=25.4f) bError=true;
            if (iZahl!=iID-1) bError=true;
            if (sName!=format("Item %i",iZahl)) bError=true;
            iID=Result->getInt("id");
            sName=Result->get("name");
            iZahl=Result->getInt("zahli");
            fZahl=Result->getFloat("zahlf");
            sNull=Result->get("nichts");
            if (!Result->isNull("nichts")) bError=true;
            if (fZahl!=25.4f) bError=true;
            if (iZahl!=iID-1) bError=true;
            if (sName!=format("Item %i",iZahl)) bError=true;
          }
          if (!bError)
            cout << "OK" << endl;
          else
            cout << "ERROR!" << endl;
            
          cout << "Checking getQueries()... ";
          if (SQL.getQueries().size()==1)
            cout << "OK" << endl;
          else
            cout << "ERROR!" << endl;
          
            
          cout << "Checking countQueries()... ";
          if (SQL.countQueries()==1)
            cout << "OK" << endl;
          else
            cout << "ERROR!" << endl;
            
          cout << "Closing query... ";
          SQL.closeQuery(Result);
          cout << "OK" << endl;
          
          cout << "Checking countQueries()... ";
          if (SQL.countQueries()==0)
            cout << "OK" << endl;
          else
            cout << "ERROR!" << endl;
            
          cout << "Dropping table test... ";
          SQL.execQuery(DROPTABLE);
          cout << "OK" << endl;
          
          
          cout << "Checking isConnected... ";
          if (!SQL.isConnected())
            cout << "ERROR!" << endl;
          else
            cout << "OK" << endl;


          cout << "Reconnecting without database... ";
          SQL.disconnect();
          if (iAccessType)          
            switch (iDBType) {
              case 0:
                SQL.setDatabase(MYSQLDEFDB);
                break;
              case 1:
               SQL.setDatabase(PGSQLDEFDB);
            }
          else
            SQL.setDatabase(""); 
          SQL.connect();
          cout << "OK" << endl;
          
          cout << "Waiting 5 seconds... ";
          cout.flush();
          sleep(5);
          cout << "OK" << endl;
          
          cout << "Dropping test database... ";
          SQL.execQuery(DROPDB);
          cout << "OK" << endl;
          
          cout << "Disconnecting... ";
          SQL.disconnect();
          cout << "OK" << endl;

          cout << "Checking isConnected... ";
          if (SQL.isConnected())
            cout << "ERROR!" << endl;
          else
            cout << "OK" << endl;
        }
      }
    }
  }
  catch (sqlxx_error E) {
    cerr << E.what() << endl;
  }
}