~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to examples/client.cc

  • Committer: Mark Atwood
  • Date: 2011-11-22 17:04:41 UTC
  • mfrom: (2462.1.3 drizzle-include)
  • Revision ID: me@mark.atwood.name-20111122170441-5dehm0e0ax20z19b
mergeĀ lp:~brianaker/drizzle/fedora-16-fixes

Show diffs side-by-side

added added

removed removed

Lines of Context:
73
73
 
74
74
typedef struct
75
75
{
76
 
  drizzle_st *drizzle;
 
76
  drizzle_st drizzle;
77
77
  bool mysql_protocol;
78
78
  client_con_st *client_con_list;
79
79
  uint32_t client_con_count;
88
88
void column_info(drizzle_column_st *column);
89
89
 
90
90
#define CLIENT_ERROR(__function, __ret, __client) { \
91
 
    fprintf(stderr, __function ":%d:%s\n", __ret, \
92
 
           drizzle_error(((__client)->drizzle))); \
 
91
    printf(__function ":%d:%s\n", __ret, \
 
92
           drizzle_error(&((__client)->drizzle))); \
93
93
    exit(1); }
94
94
 
95
95
int main(int argc, char *argv[])
100
100
  drizzle_return_t ret;
101
101
  uint32_t x;
102
102
  int wait_for_connections= 0;
 
103
  drizzle_con_st *con;
103
104
  client_con_st *client_con;
104
105
  char *host= NULL;
105
106
  in_port_t port= 0;
131
132
        client.level= BUFFER_ALL;
132
133
      else
133
134
      {
134
 
        fprintf(stderr, "Invalid buffer level: %s\n", optarg);
 
135
        printf("Invalid buffer level: %s\n", optarg);
135
136
        exit(0);
136
137
      }
137
138
      break;
197
198
                                                    sizeof(client_con_st));
198
199
    if (client.client_con_list == NULL)
199
200
    {
200
 
      fprintf(stderr, "calloc:%d\n", errno);
 
201
      printf("calloc:%d\n", errno);
201
202
      exit(1);
202
203
    }
203
204
  }
204
205
 
205
206
  /* This may fail if there is other initialization that fails. See docs. */
206
 
  if ((client.drizzle= drizzle_create()) == NULL)
 
207
  if (drizzle_create(&(client.drizzle)) == NULL)
207
208
  {
208
 
    fprintf(stderr, "drizzle_create failed\n");
 
209
    printf("drizzle_create failed\n");
209
210
    exit(1);
210
211
  }
211
212
 
212
213
  if (blocking == 0)
213
 
  {
214
 
    drizzle_set_option(client.drizzle, DRIZZLE_NON_BLOCKING, true);
215
 
  }
 
214
    drizzle_add_options(&(client.drizzle), DRIZZLE_NON_BLOCKING);
216
215
 
217
216
  /* Start all connections, and if in non-blocking mode, return as soon as the
218
217
     connection would block. In blocking mode, this completes the entire
220
219
  for (x= 0; x < client.client_con_count; x++)
221
220
  {
222
221
    /* This may fail if there is other initialization that fails. See docs. */
223
 
    drizzle_con_st *con= drizzle_con_add_tcp(client.drizzle,
224
 
                                             host, port, user, password, db,
225
 
                                             client.mysql_protocol
226
 
                                             ? DRIZZLE_CON_MYSQL
227
 
                                             : DRIZZLE_CON_NONE);
 
222
    con= drizzle_con_add_tcp(&(client.drizzle),
 
223
                              &(client.client_con_list[x].con),
 
224
                              host, port, user, password, db,
 
225
                              client.mysql_protocol
 
226
                              ? DRIZZLE_CON_MYSQL
 
227
                              : DRIZZLE_CON_NONE);
228
228
    if (con == NULL)
229
 
    {
230
229
      CLIENT_ERROR("drizzle_con_add_tcp", 0, &client);
231
 
    }
232
230
    drizzle_con_set_context(&(client.client_con_list[x].con),
233
231
                            &(client.client_con_list[x]));
234
232
 
240
238
     ready. Loop exits when all connections have completed. */
241
239
  while (wait_for_connections != 0)
242
240
  {
243
 
    ret= drizzle_con_wait(client.drizzle);
 
241
    ret= drizzle_con_wait(&(client.drizzle));
244
242
    if (ret != DRIZZLE_RETURN_OK)
245
 
    {
246
243
      CLIENT_ERROR("drizzle_con_wait", ret, &client);
247
 
    }
248
244
 
249
 
    drizzle_con_st* con;
250
 
    while ((con= drizzle_con_ready(client.drizzle)) != NULL)
 
245
    while ((con= drizzle_con_ready(&(client.drizzle))) != NULL)
251
246
    {
252
247
      client_con= (client_con_st *)drizzle_con_context(con);
253
248
 
254
249
      if (client_process(&client, client_con) == 0)
255
 
      {
256
250
        wait_for_connections--;
257
 
      }
258
251
    }
259
252
  }
260
253
 
261
254
  for (x= 0; x < client.client_con_count; x++)
262
 
  {
263
255
    drizzle_con_free(&(client.client_con_list[x].con));
264
 
  }
265
256
 
266
 
  drizzle_free(client.drizzle);
 
257
  drizzle_free(&(client.drizzle));
267
258
 
268
259
  free(client.client_con_list);
269
260
 
292
283
    (void)drizzle_query(&(client_con->con), &(client_con->result),
293
284
                        client->query, client->query_len, &ret);
294
285
    if (ret == DRIZZLE_RETURN_IO_WAIT)
295
 
    {
296
286
      return 1;
297
 
    }
298
287
    else if (ret != DRIZZLE_RETURN_OK)
299
 
    {
300
288
      CLIENT_ERROR("drizzle_query", ret, client);
301
 
    }
302
289
 
303
290
    result_info(&(client_con->result));
304
291
 
305
292
    if (drizzle_result_column_count(&(client_con->result)) == 0)
306
 
    {
307
293
      break;
308
 
    }
309
294
 
310
295
    client_con->state= CLIENT_FIELDS;
311
296
 
314
299
    {
315
300
      ret= drizzle_result_buffer(&(client_con->result));
316
301
      if (ret == DRIZZLE_RETURN_IO_WAIT)
317
 
      {
318
302
        return 1;
319
 
      }
320
303
      else if (ret != DRIZZLE_RETURN_OK)
321
 
      {
322
304
        CLIENT_ERROR("drizzle_result_buffer", ret, client);
323
 
      }
324
305
 
325
306
      while ((column= drizzle_column_next(&(client_con->result))) != NULL)
326
307
        column_info(column);
332
313
        column= drizzle_column_read(&(client_con->result),
333
314
                                    &(client_con->column), &ret);
334
315
        if (ret == DRIZZLE_RETURN_IO_WAIT)
335
 
        {
336
316
          return 1;
337
 
        }
338
317
        else if (ret != DRIZZLE_RETURN_OK)
339
 
        {
340
318
          CLIENT_ERROR("drizzle_column_read", ret, client);
341
 
        }
342
319
 
343
320
        if (column == NULL)
344
321
          break;
358
335
      {
359
336
        field_sizes= drizzle_row_field_sizes(&(client_con->result));
360
337
 
361
 
        printf("Row: %" PRId64 "\n", drizzle_row_current(&(client_con->result)));
 
338
        printf("Row: %" PRId64 "\n",
 
339
               drizzle_row_current(&(client_con->result)));
362
340
 
363
341
        for (x= 0; x < drizzle_result_column_count(&(client_con->result)); x++)
364
342
        {
365
343
          if (row[x] == NULL)
366
 
          {
367
344
            printf("     (NULL)\n");
368
 
          }
369
345
          else
370
346
          {
371
 
            printf("     (%"PRIu64") %.*s\n", static_cast<uint64_t>(field_sizes[x]), (int32_t)field_sizes[x], row[x]);
 
347
            printf("     (%"PRIu64") %.*s\n", static_cast<uint64_t>(field_sizes[x]), (int32_t)field_sizes[x],
 
348
                   row[x]);
372
349
          }
373
350
        }
374
351
 
427
404
            CLIENT_ERROR("drizzle_field_read", ret, client);
428
405
 
429
406
          if (field == NULL)
430
 
          {
431
407
            printf("     (NULL)");
432
 
          }
433
408
          else if (offset > 0)
434
 
          {
435
409
            printf("%.*s", (int32_t)length, field);
436
 
          }
437
410
          else
438
 
          {
439
 
            printf("     (%" PRIu64 " %.*s", (uint64_t)total, (int32_t)length, field);
440
 
          }
 
411
            printf("     (%" PRIu64 " %.*s", (uint64_t)total,
 
412
                            (int32_t)length, field);
441
413
 
442
414
          if (offset + length == total)
443
 
          {
444
415
            printf("\n");
445
 
          }
446
416
 
447
417
          /* If we buffered the entire field, be sure to free it. */
448
418
          if (client->level == BUFFER_FIELD)