~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to examples/sqlite_server.cc

  • Committer: Brian Aker
  • Date: 2011-11-24 00:05:44 UTC
  • mto: This revision was merged to the branch mainline in revision 2465.
  • Revision ID: brian@tangent.org-20111124000544-186obn61qr3b7r9x
First pass, drizzle_create() no longer takes an argument. This means that we can now change drizzle_st without being concerned about ABI.

Show diffs side-by-side

added added

removed removed

Lines of Context:
56
56
 
57
57
#define DRIZZLE_RETURN_ERROR(__function, __drizzle) \
58
58
{ \
59
 
  printf(__function ":%s\n", drizzle_error(__drizzle)); \
 
59
  fprintf(stderr, __function ":%s\n", drizzle_error(__drizzle)); \
60
60
  return; \
61
61
}
62
62
 
64
64
{ \
65
65
  if ((__ret) != DRIZZLE_RETURN_OK) \
66
66
  { \
67
 
    printf(__function ":%s\n", drizzle_error(__drizzle)); \
 
67
    fprintf(stderr, __function ":%s\n", drizzle_error(__drizzle)); \
68
68
    return ret; \
69
69
  } \
70
70
}
71
71
 
72
72
typedef struct
73
73
{
74
 
  drizzle_st drizzle;
 
74
  drizzle_st *drizzle;
75
75
  drizzle_con_st con;
76
76
  drizzle_result_st result;
77
77
  drizzle_column_st column;
159
159
  sqlite3_open(argv[optind], &(server->db));
160
160
  if (server->db == NULL)
161
161
  {
162
 
    printf("sqlite3_open: could not open sqlite3 db\n");
163
 
    return 1;
164
 
  }
165
 
 
166
 
  if (drizzle_create(&(server->drizzle)) == NULL)
167
 
  {
168
 
    printf("drizzle_create:NULL\n");
169
 
    return 1;
170
 
  }
171
 
 
172
 
  drizzle_add_options(&(server->drizzle), DRIZZLE_FREE_OBJECTS);
173
 
  drizzle_set_verbose(&(server->drizzle), server->verbose);
174
 
 
175
 
  if (drizzle_con_create(&(server->drizzle), con_listen) == NULL)
176
 
  {
177
 
    printf("drizzle_con_create:NULL\n");
 
162
    fprintf(stderr, "sqlite3_open: could not open sqlite3 db\n");
 
163
    return 1;
 
164
  }
 
165
 
 
166
  if ((server->drizzle= drizzle_create()) == NULL)
 
167
  {
 
168
    fprintf(stderr, "drizzle_create:NULL\n");
 
169
    return 1;
 
170
  }
 
171
 
 
172
  drizzle_add_options(server->drizzle, DRIZZLE_FREE_OBJECTS);
 
173
  drizzle_set_verbose(server->drizzle, server->verbose);
 
174
 
 
175
  if (drizzle_con_create(server->drizzle, con_listen) == NULL)
 
176
  {
 
177
    fprintf(stderr, "drizzle_con_create:NULL\n");
178
178
    return 1;
179
179
  }
180
180
 
186
186
 
187
187
  if (drizzle_con_listen(con_listen) != DRIZZLE_RETURN_OK)
188
188
  {
189
 
    printf("drizzle_con_listen:%s\n", drizzle_error(&(server->drizzle)));
 
189
    fprintf(stderr, "drizzle_con_listen:%s\n", drizzle_error(server->drizzle));
190
190
    return 1;
191
191
  }
192
192
 
193
193
  while (1)
194
194
  {
195
 
    (void)drizzle_con_accept(&(server->drizzle), &(server->con), &ret);
 
195
    (void)drizzle_con_accept(server->drizzle, &(server->con), &ret);
196
196
    if (ret != DRIZZLE_RETURN_OK)
197
197
    {
198
 
      printf("drizzle_con_accept:%s\n", drizzle_error(&(server->drizzle)));
 
198
      fprintf(stderr, "drizzle_con_accept:%s\n", drizzle_error(server->drizzle));
199
199
      return 1;
200
200
    }
201
201
 
213
213
  }
214
214
 
215
215
  drizzle_con_free(con_listen);
216
 
  drizzle_free(&(server->drizzle));
 
216
  drizzle_free(server->drizzle);
217
217
  sqlite3_close(server->db);
218
218
  free(con_listen);
219
219
  free(server);
242
242
  drizzle_con_set_max_packet_size(&(server->con), DRIZZLE_MAX_PACKET_SIZE);
243
243
 
244
244
  ret= drizzle_handshake_server_write(&(server->con));
245
 
  DRIZZLE_RETURN_CHECK(ret, "drizzle_handshake_server_write",
246
 
                       &(server->drizzle))
 
245
  DRIZZLE_RETURN_CHECK(ret, "drizzle_handshake_server_write", server->drizzle)
247
246
 
248
247
  ret= drizzle_handshake_client_read(&(server->con));
249
 
  DRIZZLE_RETURN_CHECK(ret, "drizzle_handshake_client_read", &(server->drizzle))
 
248
  DRIZZLE_RETURN_CHECK(ret, "drizzle_handshake_client_read", server->drizzle)
250
249
 
251
250
  if (drizzle_result_create(&(server->con), &(server->result)) == NULL)
252
 
    DRIZZLE_RETURN_ERROR("drizzle_result_create", &(server->drizzle))
 
251
    DRIZZLE_RETURN_ERROR("drizzle_result_create", server->drizzle)
253
252
 
254
253
  ret= drizzle_result_write(&(server->con), &(server->result), true);
255
 
  DRIZZLE_RETURN_CHECK(ret, "drizzle_result_write", &(server->drizzle))
 
254
  DRIZZLE_RETURN_CHECK(ret, "drizzle_result_write", server->drizzle)
256
255
 
257
256
  /* Command loop. */
258
257
  while (1)
267
266
      free(data);
268
267
      return;
269
268
    }
270
 
    DRIZZLE_RETURN_CHECK(ret, "drizzle_con_command_buffer", &(server->drizzle))
 
269
    DRIZZLE_RETURN_CHECK(ret, "drizzle_con_command_buffer", server->drizzle)
271
270
 
272
271
    if (server->verbose >= DRIZZLE_VERBOSE_INFO)
273
272
    {
274
 
      printf("Command=%u Data=%s\n", command,
 
273
      fprintf(stderr, "Command=%u Data=%s\n", command,
275
274
             data == NULL ? "NULL" : (char *)data);
276
275
    }
277
276
 
278
277
    if (drizzle_result_create(&(server->con), &(server->result)) == NULL)
279
 
      DRIZZLE_RETURN_ERROR("drizzle_result_create", &(server->drizzle))
 
278
      DRIZZLE_RETURN_ERROR("drizzle_result_create", server->drizzle)
280
279
 
281
280
    if (command != DRIZZLE_COMMAND_QUERY ||
282
281
        !strcasecmp((char *)data, "SHOW DATABASES"))
283
282
    {
284
283
      ret= drizzle_result_write(&(server->con), &(server->result), true);
285
 
      DRIZZLE_RETURN_CHECK(ret, "drizzle_result_write", &(server->drizzle))
 
284
      DRIZZLE_RETURN_CHECK(ret, "drizzle_result_write", server->drizzle)
286
285
 
287
286
      if (command == DRIZZLE_COMMAND_FIELD_LIST)
288
287
      {
289
288
        drizzle_result_set_eof(&(server->result), true);
290
289
        ret= drizzle_result_write(&(server->con), &(server->result), true);
291
 
        DRIZZLE_RETURN_CHECK(ret, "drizzle_result_write", &(server->drizzle))
 
290
        DRIZZLE_RETURN_CHECK(ret, "drizzle_result_write", server->drizzle)
292
291
      }
293
292
 
294
293
      continue;
321
320
    if (sqlite_ret != SQLITE_OK)
322
321
    {
323
322
      if (sqlite_err == NULL)
324
 
        printf("sqlite3_exec failed\n");
 
323
      {
 
324
        fprintf(stderr, "sqlite3_exec failed\n");
 
325
      }
325
326
      else
326
327
      {
327
328
        drizzle_result_set_error_code(&(server->result), (uint16_t)sqlite_ret);
328
329
        drizzle_result_set_error(&(server->result), sqlite_err);
329
330
        ret= drizzle_result_write(&(server->con), &(server->result), true);
330
 
        DRIZZLE_RETURN_CHECK(ret, "drizzle_result_write", &(server->drizzle))
 
331
        DRIZZLE_RETURN_CHECK(ret, "drizzle_result_write", server->drizzle)
331
332
 
332
 
        printf("sqlite3_exec:%s\n", sqlite_err);
 
333
        fprintf(stderr, "sqlite3_exec:%s\n", sqlite_err);
333
334
        sqlite3_free(sqlite_err);
334
335
      }
335
336
 
340
341
    {
341
342
      drizzle_result_set_column_count(&(server->result), 0);
342
343
      ret= drizzle_result_write(&(server->con), &(server->result), true);
343
 
      DRIZZLE_RETURN_CHECK(ret, "drizzle_result_write", &(server->drizzle))
 
344
      DRIZZLE_RETURN_CHECK(ret, "drizzle_result_write", server->drizzle)
344
345
    }
345
346
    else
346
347
    {
347
348
      drizzle_result_set_eof(&(server->result), true);
348
349
      ret= drizzle_result_write(&(server->con), &(server->result), true);
349
 
      DRIZZLE_RETURN_CHECK(ret, "drizzle_result_write", &(server->drizzle))
 
350
      DRIZZLE_RETURN_CHECK(ret, "drizzle_result_write", server->drizzle)
350
351
    }
351
352
  }
352
353
}
364
365
    drizzle_result_set_column_count(&(server->result), (uint16_t)field_count);
365
366
 
366
367
    ret= drizzle_result_write(&(server->con), &(server->result), false);
367
 
    DRIZZLE_RETURN_CHECK_VAL(ret, "drizzle_result_write", &(server->drizzle))
 
368
    DRIZZLE_RETURN_CHECK_VAL(ret, "drizzle_result_write", server->drizzle)
368
369
 
369
370
    if (drizzle_column_create(&(server->result), &(server->column)) == NULL)
370
371
    {
371
 
      DRIZZLE_RETURN_CHECK_VAL(DRIZZLE_RETURN_MEMORY, "drizzle_column_create",
372
 
                               &(server->drizzle))
 
372
      DRIZZLE_RETURN_CHECK_VAL(DRIZZLE_RETURN_MEMORY, "drizzle_column_create", server->drizzle)
373
373
    }
374
374
 
375
375
    drizzle_column_set_catalog(&(server->column), "sqlite");
388
388
      drizzle_column_set_orig_name(&(server->column), columns[x]);
389
389
 
390
390
      ret= drizzle_column_write(&(server->result), &(server->column));
391
 
      DRIZZLE_RETURN_CHECK_VAL(ret, "drizzle_column_write", &(server->drizzle))
 
391
      DRIZZLE_RETURN_CHECK_VAL(ret, "drizzle_column_write", server->drizzle)
392
392
    }
393
393
 
394
394
    drizzle_column_free(&(server->column));
396
396
    drizzle_result_set_eof(&(server->result), true);
397
397
 
398
398
    ret= drizzle_result_write(&(server->con), &(server->result), false);
399
 
    DRIZZLE_RETURN_CHECK_VAL(ret, "drizzle_result_write", &(server->drizzle))
 
399
    DRIZZLE_RETURN_CHECK_VAL(ret, "drizzle_result_write", server->drizzle)
400
400
  }
401
401
 
402
402
  for (x= 0; x < field_count; x++)
412
412
                               sizes);
413
413
 
414
414
  ret= drizzle_row_write(&(server->result));
415
 
  DRIZZLE_RETURN_CHECK_VAL(ret, "drizzle_row_write", &(server->drizzle))
 
415
  DRIZZLE_RETURN_CHECK_VAL(ret, "drizzle_row_write", server->drizzle)
416
416
 
417
417
  for (x= 0; x < field_count; x++)
418
418
  {
419
419
    ret= drizzle_field_write(&(server->result), (drizzle_field_t)fields[x],
420
420
                             sizes[x], sizes[x]);
421
 
    DRIZZLE_RETURN_CHECK_VAL(ret, "drizzle_field_write", &(server->drizzle))
 
421
    DRIZZLE_RETURN_CHECK_VAL(ret, "drizzle_field_write", server->drizzle)
422
422
  }
423
423
 
424
424
  server->rows++;
440
440
  drizzle_result_set_column_count(&(server->result), 1);
441
441
 
442
442
  ret= drizzle_result_write(&(server->con), &(server->result), false);
443
 
  DRIZZLE_RETURN_CHECK_VAL(ret, "drizzle_result_write", &(server->drizzle))
 
443
  DRIZZLE_RETURN_CHECK_VAL(ret, "drizzle_result_write", server->drizzle)
444
444
 
445
445
  if (drizzle_column_create(&(server->result), &(server->column)) == NULL)
446
446
  {
447
 
    DRIZZLE_RETURN_CHECK_VAL(DRIZZLE_RETURN_MEMORY, "drizzle_column_create",
448
 
                             &(server->drizzle))
 
447
    DRIZZLE_RETURN_CHECK_VAL(DRIZZLE_RETURN_MEMORY, "drizzle_column_create", server->drizzle)
449
448
  }
450
449
 
451
450
  drizzle_column_set_catalog(&(server->column), "sqlite");
459
458
  drizzle_column_set_orig_name(&(server->column), "version");
460
459
 
461
460
  ret= drizzle_column_write(&(server->result), &(server->column));
462
 
  DRIZZLE_RETURN_CHECK_VAL(ret, "drizzle_column_write", &(server->drizzle))
 
461
  DRIZZLE_RETURN_CHECK_VAL(ret, "drizzle_column_write", server->drizzle)
463
462
 
464
463
  drizzle_column_free(&(server->column));
465
464
 
466
465
  drizzle_result_set_eof(&(server->result), true);
467
466
 
468
467
  ret= drizzle_result_write(&(server->con), &(server->result), false);
469
 
  DRIZZLE_RETURN_CHECK_VAL(ret, "drizzle_result_write", &(server->drizzle))
 
468
  DRIZZLE_RETURN_CHECK_VAL(ret, "drizzle_result_write", server->drizzle)
470
469
 
471
470
  /* This is needed for MySQL and old Drizzle protocol. */
472
471
  drizzle_result_calc_row_size(&(server->result), fields, sizes);
473
472
 
474
473
  ret= drizzle_row_write(&(server->result));
475
 
  DRIZZLE_RETURN_CHECK_VAL(ret, "drizzle_row_write", &(server->drizzle))
 
474
  DRIZZLE_RETURN_CHECK_VAL(ret, "drizzle_row_write", server->drizzle)
476
475
 
477
476
  ret= drizzle_field_write(&(server->result), fields[0], sizes[0], sizes[0]);
478
 
  DRIZZLE_RETURN_CHECK_VAL(ret, "drizzle_field_write", &(server->drizzle))
 
477
  DRIZZLE_RETURN_CHECK_VAL(ret, "drizzle_field_write", server->drizzle)
479
478
 
480
479
  ret= drizzle_result_write(&(server->con), &(server->result), true);
481
 
  DRIZZLE_RETURN_CHECK_VAL(ret, "drizzle_result_write", &(server->drizzle))
 
480
  DRIZZLE_RETURN_CHECK_VAL(ret, "drizzle_result_write", server->drizzle)
482
481
 
483
482
  return DRIZZLE_RETURN_OK;
484
483
}
485
484
 
486
485
static void usage(char *name)
487
486
{
488
 
  printf("\nusage: %s [-c <count>] [-h <host>] [-m] [-p <port>] [-v] "
489
 
         "<sqlite3 db file>\n", name);
490
 
  printf("\t-c <count> - Number of connections to accept before exiting\n");
491
 
  printf("\t-h <host>  - Host to listen on\n");
492
 
  printf("\t-m         - Use the MySQL protocol\n");
493
 
  printf("\t-p <port>  - Port to listen on\n");
494
 
  printf("\t-v         - Increase verbosity level\n");
 
487
  fprintf(stderr, "\nusage: %s [-c <count>] [-h <host>] [-m] [-p <port>] [-v] " "<sqlite3 db file>\n", name);
 
488
  fprintf(stderr, "\t-c <count> - Number of connections to accept before exiting\n");
 
489
  fprintf(stderr, "\t-h <host>  - Host to listen on\n");
 
490
  fprintf(stderr, "\t-m         - Use the MySQL protocol\n");
 
491
  fprintf(stderr, "\t-p <port>  - Port to listen on\n");
 
492
  fprintf(stderr, "\t-v         - Increase verbosity level\n");
495
493
}