summaryrefslogtreecommitdiff
path: root/ndb/test/ndbapi/old_dirs/lmc-bench/src/user/old/userInterface.c
blob: bacf1861ddeb0440c13f91618e1b5d65e07722b7 (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
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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
/* Copyright (C) 2003 MySQL AB

   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 2 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, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */

/***************************************************************
* I N C L U D E D   F I L E S                                  *
***************************************************************/

#include <ndb_global.h>

#include "userInterface.h"
#include "userHandle.h"

/***************************************************************
* L O C A L   C O N S T A N T S                                *
***************************************************************/

/***************************************************************
* L O C A L   D A T A   S T R U C T U R E S                    *
***************************************************************/

/***************************************************************
* L O C A L   F U N C T I O N S                                *
***************************************************************/

extern int localDbPrepare(UserHandle *uh);

static int dbCreate(UserHandle *uh);

/***************************************************************
* L O C A L   D A T A                                          *
***************************************************************/

static char *create_subscriber_table = 
"CREATE TABLE subscriber(\
subscriberNumber CHAR(12) NOT NULL primary key,\
subscriberName   CHAR(32) NOT NULL,\
groupId          INT      NOT NULL,\
location         INT      NOT NULL,\
activeSessions   INT      NOT NULL,\
changedBy        CHAR(32) NOT NULL,\
changedTime      CHAR(32) NOT NULL)";

static char *create_group_table = 
"CREATE TABLE userGroup(\
groupId          INT      NOT NULL primary key,\
groupName        CHAR(32) NOT NULL,\
allowRead        INT      NOT NULL,\
allowInsert      INT      NOT NULL,\
allowDelete      INT      NOT NULL)";

static char *create_server_table = "CREATE TABLE server(\
serverId         INT      NOT NULL,\
subscriberSuffix CHAR(2)  NOT NULL,\
serverName       CHAR(32) NOT NULL,\
noOfRead         INT      NOT NULL,\
noOfInsert       INT      NOT NULL,\
noOfDelete       INT      NOT NULL,\
PRIMARY KEY(serverId,subscriberSuffix))";

static char *create_session_table = 
"CREATE TABLE userSession(\
subscriberNumber CHAR(12) NOT NULL,\
serverId         INT      NOT NULL,\
sessionData      CHAR(2000) NOT NULL,\
PRIMARY KEY(subscriberNumber,serverId))";

/***************************************************************
* P U B L I C   D A T A                                        *
***************************************************************/


/***************************************************************
****************************************************************
* L O C A L   F U N C T I O N S   C O D E   S E C T I O N      *
****************************************************************
***************************************************************/

/***************************************************************
****************************************************************
* P U B L I C   F U N C T I O N S   C O D E   S E C T I O N    *
****************************************************************
***************************************************************/

/*-----------------------------------*/
/* Time related Functions            */
/*                                   */
/* Returns a double value in seconds */
/*-----------------------------------*/
double userGetTime(void)
{
   static int initialized = 0;
   static struct timeval initTime;
   double timeValue;

   if( !initialized ) {
      initialized = 1;
      gettimeofday(&initTime, 0);
      timeValue = 0.0;
   }
   else {
      struct timeval tv;
      double s;
      double us;

      gettimeofday(&tv, 0);
      s  = (double)tv.tv_sec  - (double)initTime.tv_sec;
      us = (double)tv.tv_usec - (double)initTime.tv_usec;

      timeValue = s + (us / 1000000.0);
   }

   return(timeValue);
}


void handle_error(SQLHDBC  hdbc,
                  SQLHENV  henv, 
                  SQLHSTMT hstmt, 
                  SQLRETURN rc,
                  char *filename,
                  int lineno)
{
#define MSG_LNG 512

   int isError = 0; 
   SQLRETURN     ret = SQL_SUCCESS;
   SQLCHAR       szSqlState[MSG_LNG];    /* SQL state string  */
   SQLCHAR       szErrorMsg[MSG_LNG];    /* Error msg text buffer pointer */
   SQLINTEGER    pfNativeError;          /* Native error code */
   SQLSMALLINT   pcbErrorMsg;            /* Error msg text Available bytes */

   if ( rc == SQL_SUCCESS || rc == SQL_NO_DATA_FOUND )
      return;
   else if ( rc == SQL_INVALID_HANDLE ) {
      printf("ERROR in %s, line %d: invalid handle\n",
               filename, lineno);
      isError = 1;
   }
   else if ( rc == SQL_SUCCESS_WITH_INFO ) {
      printf("WARNING in %s, line %d\n",
               filename, lineno);
      isError = 0;
   }
   else if ( rc == SQL_ERROR ) {
      printf("ERROR in %s, line %d\n",
               filename, lineno);
      isError = 1;
   }

   fflush(stdout);

   while ( ret == SQL_SUCCESS || ret == SQL_SUCCESS_WITH_INFO ) {
      ret = SQLError(henv, hdbc, hstmt, szSqlState, &pfNativeError, szErrorMsg,
                     MSG_LNG, &pcbErrorMsg);

      switch (ret) {
         case SQL_SUCCESS:
         case SQL_SUCCESS_WITH_INFO:
            printf("%s\n*** ODBC Error/Warning = %s, "
                    "Additional Error/Warning = %d\n",
                    szErrorMsg, szSqlState, pfNativeError);

            if(ret == SQL_SUCCESS_WITH_INFO)
               printf("(Note: error message was truncated.\n");
            break;

         case SQL_INVALID_HANDLE:
           printf("Call to SQLError failed with return code of "
                    "SQL_INVALID_HANDLE.\n");
           break;

         case SQL_ERROR:
            printf("Call to SQLError failed with return code of SQL_ERROR.\n");
            break;

         case SQL_NO_DATA_FOUND:
            break;

         default:
           printf("Call to SQLError failed with return code of %d.\n", ret);
      }
   }

   if ( isError )
      exit(1);
}

static int dbCreate(UserHandle *uh)
{
   SQLRETURN rc;
   SQLHSTMT  creatstmt;

   if(!uh) return(-1);

   rc = SQLAllocStmt(uh->hdbc, &creatstmt);
   if( rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
      printf("Unable to allocate create statement\n");
      return(-1);
   }

   rc = SQLExecDirect(creatstmt,(SQLCHAR *)create_subscriber_table, SQL_NTS);
   if( rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
      printf("Unable to create subscriber table\n");
      return(-1);
   }

   rc = SQLExecDirect(creatstmt,(SQLCHAR *)create_group_table, SQL_NTS);
   if( rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
      printf("Unable to create group table\n");
      return(-1);
   }

   rc = SQLExecDirect(creatstmt,(SQLCHAR *)create_server_table, SQL_NTS);
   if( rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
      printf("Unable to create server table\n");
      return(-1);
   }

   rc = SQLExecDirect(creatstmt,(SQLCHAR *)create_session_table, SQL_NTS);
   if( rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
      printf("Unable to create session table\n");
      return(-1);
   }

   rc = SQLTransact(uh->henv, uh->hdbc, SQL_COMMIT);
   if( rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
      printf("Unable to commit all create table\n");
      return(-1);
   }

   rc = SQLFreeStmt(creatstmt, SQL_DROP);
   if( rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
      printf("Unable to free create statement\n");
      return(-1);
   }

   return(0);
}

UserHandle *userDbConnect(uint32 createDb, char *dbName)
{
   char      connStrIn[512]; /* ODBC Connection String */
   char      connStrOut[2048];
   SQLRETURN rc;
   UserHandle *uh;

   /*--------------------------*/
   /* Build the Connect string */
   /*--------------------------*/
   sprintf(connStrIn,
           "AutoCreate=%d;OverWrite=%d;DSN=%s",
           createDb ? 1 : 0,
           createDb ? 1 : 0,
           dbName);

   uh = calloc(1, sizeof(UserHandle));
   if( !uh ) {
      printf("Unable to allocate memory for Handle\n");
      return(0);
   }

   /*---------------------------------*/
   /* Allocate the Environment Handle */
   /*---------------------------------*/
   rc = SQLAllocEnv(&uh->henv);

   if( rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
      printf("Unable to allocate Environment Handle\n");
      return(0);
   }

   /*--------------------------------*/
   /* Allocate the DB Connect Handle */
   /*--------------------------------*/
   rc = SQLAllocConnect(uh->henv, &uh->hdbc);
   if( rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
      printf("Unable to allocate a connection handle\n");
      return(0);
   }

   /*-------------------------*/
   /* Connect to the Database */
   /*-------------------------*/
   rc = SQLDriverConnect(uh->hdbc, NULL, 
                        (SQLCHAR *)connStrIn, SQL_NTS,
                        (SQLCHAR *)connStrOut, sizeof (connStrOut), 
                         NULL, SQL_DRIVER_NOPROMPT);
   if( rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
handle_error(uh->hdbc, uh->henv, NULL, rc, __FILE__, __LINE__);
      printf("Unable to connect to database server\n");
      return(0);
   }

   rc = SQLSetConnectOption(uh->hdbc, SQL_AUTOCOMMIT, SQL_AUTOCOMMIT_OFF);
   if( rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
      printf("Unable to set connection option\n");
      return(0);
   }

   rc = SQLAllocStmt(uh->hdbc, &uh->stmt);
   if( rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
      printf("Unable to allocate immediate statement\n");
      return(0);
   }

   if( createDb )
      dbCreate(uh);

   if( localDbPrepare(uh) < 0 )
      return(0);

   return(uh);
}

void userDbDisconnect(UserHandle *uh)
{
   SQLRETURN rc;

   if(!uh) return;

   rc = SQLDisconnect(uh->hdbc);

   SQLFreeConnect(uh->hdbc);
   SQLFreeEnv(uh->henv);
   free(uh);
}

int userDbInsertServer(UserHandle        *uh,
                       ServerId         serverId,
	               SubscriberSuffix suffix,
	               ServerName       name)
{
   SQLRETURN rc;
   char buf[1000];

   if(!uh) return(-1);

   sprintf(buf, "insert into server values (%d,'%.*s','%s',0,0,0)",
           serverId,
           SUBSCRIBER_NUMBER_SUFFIX_LENGTH, suffix,
           name);

   rc = SQLExecDirect(uh->stmt, (unsigned char *)buf, SQL_NTS);
   if( rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
      printf("Unable to execute insert server\n");
      return(-1);
   }

   return( userDbCommit(uh) );
}

int userDbInsertSubscriber(UserHandle        *uh,
	                   SubscriberNumber number,
                           uint32           groupId,
	                   SubscriberName   name)
{
   SQLRETURN rc;
   char buf[1000];

   if(!uh) return(-1);

   sprintf(buf, "insert into subscriber values ('%s','%s',%d,0,0,'','')",
           number,
           name,
           groupId);

   rc = SQLExecDirect(uh->stmt, (unsigned char*)buf, SQL_NTS);
   if( rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
      printf("Unable to execute insert subscriber\n");
      return(-1);
   }

   return( userDbCommit(uh) );
}

int userDbInsertGroup(UserHandle  *uh,
		      GroupId    groupId, 
		      GroupName  name,
		      Permission allowRead,
		      Permission allowInsert,
		      Permission allowDelete)
{
   SQLRETURN rc;
   char buf[1000];

   if(!uh) return(-1);

   sprintf(buf, "insert into usergroup values (%d,'%s',%d,%d,%d)",
           groupId,
           name,           
           allowRead,
           allowInsert,
           allowDelete);

   rc = SQLExecDirect(uh->stmt, (unsigned char*)buf, SQL_NTS);
   if( rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
      printf("Unable to execute insert group\n");
      return(-1);
   }

   return( userDbCommit(uh) );
}

int userDbCommit(UserHandle *uh)
{
   SQLRETURN rc;
   if(!uh) return(-1);

   rc = SQLTransact(uh->henv, uh->hdbc, SQL_COMMIT);
   if( rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
handle_error(uh->hdbc, uh->henv, 0, rc, __FILE__, __LINE__);
      printf("Unable to commit Transaction\n");
      return(-1);
   }

   return(0);
}

int userDbRollback(UserHandle *uh)
{
   SQLRETURN rc;
   if(!uh) return(-1);

   rc = SQLTransact(uh->henv, uh->hdbc, SQL_ROLLBACK);
   if( rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
      printf("Unable to rollback Transaction\n");
      return(-1);
   }

   return(0);
}

void userCheckpoint(UserHandle *uh)
{
   SQLRETURN rc;
   if(!uh) return;

   rc = SQLExecDirect(uh->stmt, (SQLCHAR *)"call ttCheckpointFuzzy", SQL_NTS);
   userDbCommit(uh);
}