~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to examples/sqlite_server.cc

  • Committer: Mark Atwood
  • Date: 2011-11-14 07:30:41 UTC
  • Revision ID: me@mark.atwood.name-20111114073041-mo2hgg8ouseo2kpu
releaseĀ 2011.11.29

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
 
  fprintf(stderr, __function ":%s\n", drizzle_error(__drizzle)); \
 
59
  printf(__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
 
    fprintf(stderr, __function ":%s\n", drizzle_error(__drizzle)); \
 
67
    printf(__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;
75
 
  drizzle_con_st *con;
76
 
  drizzle_result_st *result;
 
74
  drizzle_st drizzle;
 
75
  drizzle_con_st con;
 
76
  drizzle_result_st result;
77
77
  drizzle_column_st column;
78
78
  sqlite3* db;
79
79
  bool send_columns;
95
95
  in_port_t port= 0;
96
96
  drizzle_return_t ret;
97
97
  sqlite_server *server= (sqlite_server*)malloc(sizeof(sqlite_server));
 
98
  drizzle_con_st *con_listen= (drizzle_con_st*)malloc(sizeof(drizzle_con_st));
98
99
 
99
100
  server->db= NULL;
100
101
  server->verbose= DRIZZLE_VERBOSE_NEVER;
158
159
  sqlite3_open(argv[optind], &(server->db));
159
160
  if (server->db == NULL)
160
161
  {
161
 
    fprintf(stderr, "sqlite3_open: could not open sqlite3 db\n");
162
 
    return 1;
163
 
  }
164
 
 
165
 
  if ((server->drizzle= drizzle_create()) == NULL)
166
 
  {
167
 
    fprintf(stderr, "drizzle_create:NULL\n");
168
 
    return 1;
169
 
  }
170
 
 
171
 
  drizzle_set_option(server->drizzle, DRIZZLE_FREE_OBJECTS, true);
172
 
  drizzle_set_verbose(server->drizzle, server->verbose);
173
 
 
174
 
  drizzle_con_st *con_listen;
175
 
  if ((con_listen= drizzle_con_create(server->drizzle)) == NULL)
176
 
  {
177
 
    fprintf(stderr, "drizzle_con_create:NULL\n");
 
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");
178
178
    return 1;
179
179
  }
180
180
 
182
182
  drizzle_con_set_tcp(con_listen, host, port);
183
183
 
184
184
  if (mysql)
185
 
  {
186
185
    drizzle_con_add_options(con_listen, DRIZZLE_CON_MYSQL);
187
 
  }
188
186
 
189
187
  if (drizzle_con_listen(con_listen) != DRIZZLE_RETURN_OK)
190
188
  {
191
 
    fprintf(stderr, "drizzle_con_listen:%s\n", drizzle_error(server->drizzle));
 
189
    printf("drizzle_con_listen:%s\n", drizzle_error(&(server->drizzle)));
192
190
    return 1;
193
191
  }
194
192
 
195
193
  while (1)
196
194
  {
197
 
    server->con= drizzle_con_accept(server->drizzle, &ret);
 
195
    (void)drizzle_con_accept(&(server->drizzle), &(server->con), &ret);
198
196
    if (ret != DRIZZLE_RETURN_OK)
199
197
    {
200
 
      fprintf(stderr, "drizzle_con_accept:%s\n", drizzle_error(server->drizzle));
 
198
      printf("drizzle_con_accept:%s\n", drizzle_error(&(server->drizzle)));
201
199
      return 1;
202
200
    }
203
201
 
204
202
    server_run(server);
205
203
 
206
 
    drizzle_con_free(server->con);
 
204
    drizzle_con_free(&(server->con));
207
205
 
208
206
    if (count > 0)
209
207
    {
215
213
  }
216
214
 
217
215
  drizzle_con_free(con_listen);
218
 
  drizzle_free(server->drizzle);
 
216
  drizzle_free(&(server->drizzle));
219
217
  sqlite3_close(server->db);
 
218
  free(con_listen);
220
219
  free(server);
221
220
 
222
221
  return 0;
232
231
  char *sqlite_err;
233
232
 
234
233
  /* Handshake packets. */
235
 
  drizzle_con_set_protocol_version(server->con, 10);
236
 
  drizzle_con_set_server_version(server->con, "libdrizzle+SQLite");
237
 
  drizzle_con_set_thread_id(server->con, 1);
238
 
  drizzle_con_set_scramble(server->con, (const uint8_t *)"ABCDEFGHIJKLMNOPQRST");
239
 
  drizzle_con_set_capabilities(server->con, DRIZZLE_CAPABILITIES_NONE);
240
 
  drizzle_con_set_charset(server->con, 8);
241
 
  drizzle_con_set_status(server->con, DRIZZLE_CON_STATUS_NONE);
242
 
  drizzle_con_set_max_packet_size(server->con, DRIZZLE_MAX_PACKET_SIZE);
243
 
 
244
 
  ret= drizzle_handshake_server_write(server->con);
245
 
  DRIZZLE_RETURN_CHECK(ret, "drizzle_handshake_server_write", server->drizzle)
246
 
 
247
 
  ret= drizzle_handshake_client_read(server->con);
248
 
  DRIZZLE_RETURN_CHECK(ret, "drizzle_handshake_client_read", server->drizzle)
249
 
 
250
 
  if ((server->result= drizzle_result_create(server->con)) == NULL)
251
 
  {
252
 
    DRIZZLE_RETURN_ERROR("drizzle_result_create", server->drizzle);
253
 
  }
254
 
 
255
 
  ret= drizzle_result_write(server->con, server->result, true);
256
 
  DRIZZLE_RETURN_CHECK(ret, "drizzle_result_write", server->drizzle)
 
234
  drizzle_con_set_protocol_version(&(server->con), 10);
 
235
  drizzle_con_set_server_version(&(server->con), "libdrizzle+SQLite");
 
236
  drizzle_con_set_thread_id(&(server->con), 1);
 
237
  drizzle_con_set_scramble(&(server->con),
 
238
                           (const uint8_t *)"ABCDEFGHIJKLMNOPQRST");
 
239
  drizzle_con_set_capabilities(&(server->con), DRIZZLE_CAPABILITIES_NONE);
 
240
  drizzle_con_set_charset(&(server->con), 8);
 
241
  drizzle_con_set_status(&(server->con), DRIZZLE_CON_STATUS_NONE);
 
242
  drizzle_con_set_max_packet_size(&(server->con), DRIZZLE_MAX_PACKET_SIZE);
 
243
 
 
244
  ret= drizzle_handshake_server_write(&(server->con));
 
245
  DRIZZLE_RETURN_CHECK(ret, "drizzle_handshake_server_write",
 
246
                       &(server->drizzle))
 
247
 
 
248
  ret= drizzle_handshake_client_read(&(server->con));
 
249
  DRIZZLE_RETURN_CHECK(ret, "drizzle_handshake_client_read", &(server->drizzle))
 
250
 
 
251
  if (drizzle_result_create(&(server->con), &(server->result)) == NULL)
 
252
    DRIZZLE_RETURN_ERROR("drizzle_result_create", &(server->drizzle))
 
253
 
 
254
  ret= drizzle_result_write(&(server->con), &(server->result), true);
 
255
  DRIZZLE_RETURN_CHECK(ret, "drizzle_result_write", &(server->drizzle))
257
256
 
258
257
  /* Command loop. */
259
258
  while (1)
260
259
  {
261
 
    drizzle_result_free(server->result);
 
260
    drizzle_result_free(&(server->result));
262
261
    free(data);
263
262
 
264
 
    data= (uint8_t *)drizzle_con_command_buffer(server->con, &command, &total, &ret);
 
263
    data= (uint8_t *)drizzle_con_command_buffer(&(server->con), &command, &total, &ret);
265
264
    if (ret == DRIZZLE_RETURN_LOST_CONNECTION ||
266
265
        (ret == DRIZZLE_RETURN_OK && command == DRIZZLE_COMMAND_QUIT))
267
266
    {
268
267
      free(data);
269
268
      return;
270
269
    }
271
 
    DRIZZLE_RETURN_CHECK(ret, "drizzle_con_command_buffer", server->drizzle);
 
270
    DRIZZLE_RETURN_CHECK(ret, "drizzle_con_command_buffer", &(server->drizzle))
272
271
 
273
272
    if (server->verbose >= DRIZZLE_VERBOSE_INFO)
274
273
    {
275
 
      fprintf(stderr, "Command=%u Data=%s\n", command,
 
274
      printf("Command=%u Data=%s\n", command,
276
275
             data == NULL ? "NULL" : (char *)data);
277
276
    }
278
277
 
279
 
    if ((server->result= drizzle_result_create(server->con)) == NULL)
280
 
    {
281
 
      DRIZZLE_RETURN_ERROR("drizzle_result_create", server->drizzle);
282
 
    }
 
278
    if (drizzle_result_create(&(server->con), &(server->result)) == NULL)
 
279
      DRIZZLE_RETURN_ERROR("drizzle_result_create", &(server->drizzle))
283
280
 
284
281
    if (command != DRIZZLE_COMMAND_QUERY ||
285
282
        !strcasecmp((char *)data, "SHOW DATABASES"))
286
283
    {
287
 
      ret= drizzle_result_write(server->con, server->result, true);
288
 
      DRIZZLE_RETURN_CHECK(ret, "drizzle_result_write", server->drizzle);
 
284
      ret= drizzle_result_write(&(server->con), &(server->result), true);
 
285
      DRIZZLE_RETURN_CHECK(ret, "drizzle_result_write", &(server->drizzle))
289
286
 
290
287
      if (command == DRIZZLE_COMMAND_FIELD_LIST)
291
288
      {
292
 
        drizzle_result_set_eof(server->result, true);
293
 
        ret= drizzle_result_write(server->con, server->result, true);
294
 
        DRIZZLE_RETURN_CHECK(ret, "drizzle_result_write", server->drizzle);
 
289
        drizzle_result_set_eof(&(server->result), true);
 
290
        ret= drizzle_result_write(&(server->con), &(server->result), true);
 
291
        DRIZZLE_RETURN_CHECK(ret, "drizzle_result_write", &(server->drizzle))
295
292
      }
296
293
 
297
294
      continue;
324
321
    if (sqlite_ret != SQLITE_OK)
325
322
    {
326
323
      if (sqlite_err == NULL)
327
 
      {
328
 
        fprintf(stderr, "sqlite3_exec failed\n");
329
 
      }
 
324
        printf("sqlite3_exec failed\n");
330
325
      else
331
326
      {
332
 
        drizzle_result_set_error_code(server->result, (uint16_t)sqlite_ret);
333
 
        drizzle_result_set_error(server->result, sqlite_err);
334
 
        ret= drizzle_result_write(server->con, server->result, true);
335
 
        DRIZZLE_RETURN_CHECK(ret, "drizzle_result_write", server->drizzle)
 
327
        drizzle_result_set_error_code(&(server->result), (uint16_t)sqlite_ret);
 
328
        drizzle_result_set_error(&(server->result), sqlite_err);
 
329
        ret= drizzle_result_write(&(server->con), &(server->result), true);
 
330
        DRIZZLE_RETURN_CHECK(ret, "drizzle_result_write", &(server->drizzle))
336
331
 
337
 
        fprintf(stderr, "sqlite3_exec:%s\n", sqlite_err);
 
332
        printf("sqlite3_exec:%s\n", sqlite_err);
338
333
        sqlite3_free(sqlite_err);
339
334
      }
340
335
 
343
338
 
344
339
    if (server->rows == 0)
345
340
    {
346
 
      drizzle_result_set_column_count(server->result, 0);
347
 
      ret= drizzle_result_write(server->con, server->result, true);
348
 
      DRIZZLE_RETURN_CHECK(ret, "drizzle_result_write", server->drizzle)
 
341
      drizzle_result_set_column_count(&(server->result), 0);
 
342
      ret= drizzle_result_write(&(server->con), &(server->result), true);
 
343
      DRIZZLE_RETURN_CHECK(ret, "drizzle_result_write", &(server->drizzle))
349
344
    }
350
345
    else
351
346
    {
352
 
      drizzle_result_set_eof(server->result, true);
353
 
      ret= drizzle_result_write(server->con, server->result, true);
354
 
      DRIZZLE_RETURN_CHECK(ret, "drizzle_result_write", server->drizzle)
 
347
      drizzle_result_set_eof(&(server->result), true);
 
348
      ret= drizzle_result_write(&(server->con), &(server->result), true);
 
349
      DRIZZLE_RETURN_CHECK(ret, "drizzle_result_write", &(server->drizzle))
355
350
    }
356
351
  }
357
352
}
366
361
  if (server->send_columns == true)
367
362
  {
368
363
    server->send_columns= false;
369
 
    drizzle_result_set_column_count(server->result, (uint16_t)field_count);
370
 
 
371
 
    ret= drizzle_result_write(server->con, server->result, false);
372
 
    DRIZZLE_RETURN_CHECK_VAL(ret, "drizzle_result_write", server->drizzle)
373
 
 
374
 
    if (drizzle_column_create(server->result, &(server->column)) == NULL)
 
364
    drizzle_result_set_column_count(&(server->result), (uint16_t)field_count);
 
365
 
 
366
    ret= drizzle_result_write(&(server->con), &(server->result), false);
 
367
    DRIZZLE_RETURN_CHECK_VAL(ret, "drizzle_result_write", &(server->drizzle))
 
368
 
 
369
    if (drizzle_column_create(&(server->result), &(server->column)) == NULL)
375
370
    {
376
 
      DRIZZLE_RETURN_CHECK_VAL(DRIZZLE_RETURN_MEMORY, "drizzle_column_create", server->drizzle)
 
371
      DRIZZLE_RETURN_CHECK_VAL(DRIZZLE_RETURN_MEMORY, "drizzle_column_create",
 
372
                               &(server->drizzle))
377
373
    }
378
374
 
379
375
    drizzle_column_set_catalog(&(server->column), "sqlite");
391
387
      drizzle_column_set_name(&(server->column), columns[x]);
392
388
      drizzle_column_set_orig_name(&(server->column), columns[x]);
393
389
 
394
 
      ret= drizzle_column_write(server->result, &(server->column));
395
 
      DRIZZLE_RETURN_CHECK_VAL(ret, "drizzle_column_write", server->drizzle)
 
390
      ret= drizzle_column_write(&(server->result), &(server->column));
 
391
      DRIZZLE_RETURN_CHECK_VAL(ret, "drizzle_column_write", &(server->drizzle))
396
392
    }
397
393
 
398
394
    drizzle_column_free(&(server->column));
399
395
 
400
 
    drizzle_result_set_eof(server->result, true);
 
396
    drizzle_result_set_eof(&(server->result), true);
401
397
 
402
 
    ret= drizzle_result_write(server->con, server->result, false);
403
 
    DRIZZLE_RETURN_CHECK_VAL(ret, "drizzle_result_write", server->drizzle)
 
398
    ret= drizzle_result_write(&(server->con), &(server->result), false);
 
399
    DRIZZLE_RETURN_CHECK_VAL(ret, "drizzle_result_write", &(server->drizzle))
404
400
  }
405
401
 
406
402
  for (x= 0; x < field_count; x++)
412
408
  }
413
409
 
414
410
  /* This is needed for MySQL and old Drizzle protocol. */
415
 
  drizzle_result_calc_row_size(server->result, (drizzle_field_t *)fields,
 
411
  drizzle_result_calc_row_size(&(server->result), (drizzle_field_t *)fields,
416
412
                               sizes);
417
413
 
418
 
  ret= drizzle_row_write(server->result);
419
 
  DRIZZLE_RETURN_CHECK_VAL(ret, "drizzle_row_write", server->drizzle)
 
414
  ret= drizzle_row_write(&(server->result));
 
415
  DRIZZLE_RETURN_CHECK_VAL(ret, "drizzle_row_write", &(server->drizzle))
420
416
 
421
417
  for (x= 0; x < field_count; x++)
422
418
  {
423
 
    ret= drizzle_field_write(server->result, (drizzle_field_t)fields[x],
 
419
    ret= drizzle_field_write(&(server->result), (drizzle_field_t)fields[x],
424
420
                             sizes[x], sizes[x]);
425
 
    DRIZZLE_RETURN_CHECK_VAL(ret, "drizzle_field_write", server->drizzle)
 
421
    DRIZZLE_RETURN_CHECK_VAL(ret, "drizzle_field_write", &(server->drizzle))
426
422
  }
427
423
 
428
424
  server->rows++;
441
437
  fields[0]= (drizzle_field_t)SQLITE_SERVER_VERSION;
442
438
  sizes[0]= strlen(SQLITE_SERVER_VERSION);
443
439
 
444
 
  drizzle_result_set_column_count(server->result, 1);
445
 
 
446
 
  ret= drizzle_result_write(server->con, server->result, false);
447
 
  DRIZZLE_RETURN_CHECK_VAL(ret, "drizzle_result_write", server->drizzle)
448
 
 
449
 
  if (drizzle_column_create(server->result, &(server->column)) == NULL)
 
440
  drizzle_result_set_column_count(&(server->result), 1);
 
441
 
 
442
  ret= drizzle_result_write(&(server->con), &(server->result), false);
 
443
  DRIZZLE_RETURN_CHECK_VAL(ret, "drizzle_result_write", &(server->drizzle))
 
444
 
 
445
  if (drizzle_column_create(&(server->result), &(server->column)) == NULL)
450
446
  {
451
 
    DRIZZLE_RETURN_CHECK_VAL(DRIZZLE_RETURN_MEMORY, "drizzle_column_create", server->drizzle)
 
447
    DRIZZLE_RETURN_CHECK_VAL(DRIZZLE_RETURN_MEMORY, "drizzle_column_create",
 
448
                             &(server->drizzle))
452
449
  }
453
450
 
454
451
  drizzle_column_set_catalog(&(server->column), "sqlite");
461
458
  drizzle_column_set_name(&(server->column), "version");
462
459
  drizzle_column_set_orig_name(&(server->column), "version");
463
460
 
464
 
  ret= drizzle_column_write(server->result, &(server->column));
465
 
  DRIZZLE_RETURN_CHECK_VAL(ret, "drizzle_column_write", server->drizzle)
 
461
  ret= drizzle_column_write(&(server->result), &(server->column));
 
462
  DRIZZLE_RETURN_CHECK_VAL(ret, "drizzle_column_write", &(server->drizzle))
466
463
 
467
464
  drizzle_column_free(&(server->column));
468
465
 
469
 
  drizzle_result_set_eof(server->result, true);
 
466
  drizzle_result_set_eof(&(server->result), true);
470
467
 
471
 
  ret= drizzle_result_write(server->con, server->result, false);
472
 
  DRIZZLE_RETURN_CHECK_VAL(ret, "drizzle_result_write", server->drizzle)
 
468
  ret= drizzle_result_write(&(server->con), &(server->result), false);
 
469
  DRIZZLE_RETURN_CHECK_VAL(ret, "drizzle_result_write", &(server->drizzle))
473
470
 
474
471
  /* This is needed for MySQL and old Drizzle protocol. */
475
 
  drizzle_result_calc_row_size(server->result, fields, sizes);
476
 
 
477
 
  ret= drizzle_row_write(server->result);
478
 
  DRIZZLE_RETURN_CHECK_VAL(ret, "drizzle_row_write", server->drizzle)
479
 
 
480
 
  ret= drizzle_field_write(server->result, fields[0], sizes[0], sizes[0]);
481
 
  DRIZZLE_RETURN_CHECK_VAL(ret, "drizzle_field_write", server->drizzle)
482
 
 
483
 
  ret= drizzle_result_write(server->con, server->result, true);
484
 
  DRIZZLE_RETURN_CHECK_VAL(ret, "drizzle_result_write", server->drizzle)
 
472
  drizzle_result_calc_row_size(&(server->result), fields, sizes);
 
473
 
 
474
  ret= drizzle_row_write(&(server->result));
 
475
  DRIZZLE_RETURN_CHECK_VAL(ret, "drizzle_row_write", &(server->drizzle))
 
476
 
 
477
  ret= drizzle_field_write(&(server->result), fields[0], sizes[0], sizes[0]);
 
478
  DRIZZLE_RETURN_CHECK_VAL(ret, "drizzle_field_write", &(server->drizzle))
 
479
 
 
480
  ret= drizzle_result_write(&(server->con), &(server->result), true);
 
481
  DRIZZLE_RETURN_CHECK_VAL(ret, "drizzle_result_write", &(server->drizzle))
485
482
 
486
483
  return DRIZZLE_RETURN_OK;
487
484
}
488
485
 
489
486
static void usage(char *name)
490
487
{
491
 
  fprintf(stderr, "\nusage: %s [-c <count>] [-h <host>] [-m] [-p <port>] [-v] " "<sqlite3 db file>\n", name);
492
 
  fprintf(stderr, "\t-c <count> - Number of connections to accept before exiting\n");
493
 
  fprintf(stderr, "\t-h <host>  - Host to listen on\n");
494
 
  fprintf(stderr, "\t-m         - Use the MySQL protocol\n");
495
 
  fprintf(stderr, "\t-p <port>  - Port to listen on\n");
496
 
  fprintf(stderr, "\t-v         - Increase verbosity level\n");
 
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");
497
495
}