~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to examples/client.cc

  • Committer: Mark Atwood
  • Date: 2011-11-30 07:06:47 UTC
  • mfrom: (2463.1.6 drizzle-include)
  • Revision ID: me@mark.atwood.name-20111130070647-ixp7oalp70hkbt6l
mergeĀ lp:~brianaker/drizzle/libdrizzle-2.0-not-install

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