~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to examples/sqlite_server.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:
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;
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));
99
98
 
100
99
  server->db= NULL;
101
100
  server->verbose= DRIZZLE_VERBOSE_NEVER;
159
158
  sqlite3_open(argv[optind], &(server->db));
160
159
  if (server->db == NULL)
161
160
  {
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");
 
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");
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
  {
185
186
    drizzle_con_add_options(con_listen, DRIZZLE_CON_MYSQL);
 
187
  }
186
188
 
187
189
  if (drizzle_con_listen(con_listen) != DRIZZLE_RETURN_OK)
188
190
  {
189
 
    printf("drizzle_con_listen:%s\n", drizzle_error(&(server->drizzle)));
 
191
    fprintf(stderr, "drizzle_con_listen:%s\n", drizzle_error(server->drizzle));
190
192
    return 1;
191
193
  }
192
194
 
193
195
  while (1)
194
196
  {
195
 
    (void)drizzle_con_accept(&(server->drizzle), &(server->con), &ret);
 
197
    server->con= drizzle_con_accept(server->drizzle, &ret);
196
198
    if (ret != DRIZZLE_RETURN_OK)
197
199
    {
198
 
      printf("drizzle_con_accept:%s\n", drizzle_error(&(server->drizzle)));
 
200
      fprintf(stderr, "drizzle_con_accept:%s\n", drizzle_error(server->drizzle));
199
201
      return 1;
200
202
    }
201
203
 
202
204
    server_run(server);
203
205
 
204
 
    drizzle_con_free(&(server->con));
 
206
    drizzle_con_free(server->con);
205
207
 
206
208
    if (count > 0)
207
209
    {
213
215
  }
214
216
 
215
217
  drizzle_con_free(con_listen);
216
 
  drizzle_free(&(server->drizzle));
 
218
  drizzle_free(server->drizzle);
217
219
  sqlite3_close(server->db);
218
 
  free(con_listen);
219
220
  free(server);
220
221
 
221
222
  return 0;
231
232
  char *sqlite_err;
232
233
 
233
234
  /* Handshake packets. */
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))
 
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)
256
257
 
257
258
  /* Command loop. */
258
259
  while (1)
259
260
  {
260
 
    drizzle_result_free(&(server->result));
 
261
    drizzle_result_free(server->result);
261
262
    free(data);
262
263
 
263
 
    data= (uint8_t *)drizzle_con_command_buffer(&(server->con), &command, &total, &ret);
 
264
    data= (uint8_t *)drizzle_con_command_buffer(server->con, &command, &total, &ret);
264
265
    if (ret == DRIZZLE_RETURN_LOST_CONNECTION ||
265
266
        (ret == DRIZZLE_RETURN_OK && command == DRIZZLE_COMMAND_QUIT))
266
267
    {
267
268
      free(data);
268
269
      return;
269
270
    }
270
 
    DRIZZLE_RETURN_CHECK(ret, "drizzle_con_command_buffer", &(server->drizzle))
 
271
    DRIZZLE_RETURN_CHECK(ret, "drizzle_con_command_buffer", server->drizzle);
271
272
 
272
273
    if (server->verbose >= DRIZZLE_VERBOSE_INFO)
273
274
    {
274
 
      printf("Command=%u Data=%s\n", command,
 
275
      fprintf(stderr, "Command=%u Data=%s\n", command,
275
276
             data == NULL ? "NULL" : (char *)data);
276
277
    }
277
278
 
278
 
    if (drizzle_result_create(&(server->con), &(server->result)) == NULL)
279
 
      DRIZZLE_RETURN_ERROR("drizzle_result_create", &(server->drizzle))
 
279
    if ((server->result= drizzle_result_create(server->con)) == NULL)
 
280
    {
 
281
      DRIZZLE_RETURN_ERROR("drizzle_result_create", server->drizzle);
 
282
    }
280
283
 
281
284
    if (command != DRIZZLE_COMMAND_QUERY ||
282
285
        !strcasecmp((char *)data, "SHOW DATABASES"))
283
286
    {
284
 
      ret= drizzle_result_write(&(server->con), &(server->result), true);
285
 
      DRIZZLE_RETURN_CHECK(ret, "drizzle_result_write", &(server->drizzle))
 
287
      ret= drizzle_result_write(server->con, server->result, true);
 
288
      DRIZZLE_RETURN_CHECK(ret, "drizzle_result_write", server->drizzle);
286
289
 
287
290
      if (command == DRIZZLE_COMMAND_FIELD_LIST)
288
291
      {
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))
 
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);
292
295
      }
293
296
 
294
297
      continue;
321
324
    if (sqlite_ret != SQLITE_OK)
322
325
    {
323
326
      if (sqlite_err == NULL)
324
 
        printf("sqlite3_exec failed\n");
 
327
      {
 
328
        fprintf(stderr, "sqlite3_exec failed\n");
 
329
      }
325
330
      else
326
331
      {
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))
 
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)
331
336
 
332
 
        printf("sqlite3_exec:%s\n", sqlite_err);
 
337
        fprintf(stderr, "sqlite3_exec:%s\n", sqlite_err);
333
338
        sqlite3_free(sqlite_err);
334
339
      }
335
340
 
338
343
 
339
344
    if (server->rows == 0)
340
345
    {
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))
 
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)
344
349
    }
345
350
    else
346
351
    {
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))
 
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)
350
355
    }
351
356
  }
352
357
}
361
366
  if (server->send_columns == true)
362
367
  {
363
368
    server->send_columns= false;
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)
 
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)
370
375
    {
371
 
      DRIZZLE_RETURN_CHECK_VAL(DRIZZLE_RETURN_MEMORY, "drizzle_column_create",
372
 
                               &(server->drizzle))
 
376
      DRIZZLE_RETURN_CHECK_VAL(DRIZZLE_RETURN_MEMORY, "drizzle_column_create", server->drizzle)
373
377
    }
374
378
 
375
379
    drizzle_column_set_catalog(&(server->column), "sqlite");
387
391
      drizzle_column_set_name(&(server->column), columns[x]);
388
392
      drizzle_column_set_orig_name(&(server->column), columns[x]);
389
393
 
390
 
      ret= drizzle_column_write(&(server->result), &(server->column));
391
 
      DRIZZLE_RETURN_CHECK_VAL(ret, "drizzle_column_write", &(server->drizzle))
 
394
      ret= drizzle_column_write(server->result, &(server->column));
 
395
      DRIZZLE_RETURN_CHECK_VAL(ret, "drizzle_column_write", server->drizzle)
392
396
    }
393
397
 
394
398
    drizzle_column_free(&(server->column));
395
399
 
396
 
    drizzle_result_set_eof(&(server->result), true);
 
400
    drizzle_result_set_eof(server->result, true);
397
401
 
398
 
    ret= drizzle_result_write(&(server->con), &(server->result), false);
399
 
    DRIZZLE_RETURN_CHECK_VAL(ret, "drizzle_result_write", &(server->drizzle))
 
402
    ret= drizzle_result_write(server->con, server->result, false);
 
403
    DRIZZLE_RETURN_CHECK_VAL(ret, "drizzle_result_write", server->drizzle)
400
404
  }
401
405
 
402
406
  for (x= 0; x < field_count; x++)
408
412
  }
409
413
 
410
414
  /* This is needed for MySQL and old Drizzle protocol. */
411
 
  drizzle_result_calc_row_size(&(server->result), (drizzle_field_t *)fields,
 
415
  drizzle_result_calc_row_size(server->result, (drizzle_field_t *)fields,
412
416
                               sizes);
413
417
 
414
 
  ret= drizzle_row_write(&(server->result));
415
 
  DRIZZLE_RETURN_CHECK_VAL(ret, "drizzle_row_write", &(server->drizzle))
 
418
  ret= drizzle_row_write(server->result);
 
419
  DRIZZLE_RETURN_CHECK_VAL(ret, "drizzle_row_write", server->drizzle)
416
420
 
417
421
  for (x= 0; x < field_count; x++)
418
422
  {
419
 
    ret= drizzle_field_write(&(server->result), (drizzle_field_t)fields[x],
 
423
    ret= drizzle_field_write(server->result, (drizzle_field_t)fields[x],
420
424
                             sizes[x], sizes[x]);
421
 
    DRIZZLE_RETURN_CHECK_VAL(ret, "drizzle_field_write", &(server->drizzle))
 
425
    DRIZZLE_RETURN_CHECK_VAL(ret, "drizzle_field_write", server->drizzle)
422
426
  }
423
427
 
424
428
  server->rows++;
437
441
  fields[0]= (drizzle_field_t)SQLITE_SERVER_VERSION;
438
442
  sizes[0]= strlen(SQLITE_SERVER_VERSION);
439
443
 
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)
 
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)
446
450
  {
447
 
    DRIZZLE_RETURN_CHECK_VAL(DRIZZLE_RETURN_MEMORY, "drizzle_column_create",
448
 
                             &(server->drizzle))
 
451
    DRIZZLE_RETURN_CHECK_VAL(DRIZZLE_RETURN_MEMORY, "drizzle_column_create", server->drizzle)
449
452
  }
450
453
 
451
454
  drizzle_column_set_catalog(&(server->column), "sqlite");
458
461
  drizzle_column_set_name(&(server->column), "version");
459
462
  drizzle_column_set_orig_name(&(server->column), "version");
460
463
 
461
 
  ret= drizzle_column_write(&(server->result), &(server->column));
462
 
  DRIZZLE_RETURN_CHECK_VAL(ret, "drizzle_column_write", &(server->drizzle))
 
464
  ret= drizzle_column_write(server->result, &(server->column));
 
465
  DRIZZLE_RETURN_CHECK_VAL(ret, "drizzle_column_write", server->drizzle)
463
466
 
464
467
  drizzle_column_free(&(server->column));
465
468
 
466
 
  drizzle_result_set_eof(&(server->result), true);
 
469
  drizzle_result_set_eof(server->result, true);
467
470
 
468
 
  ret= drizzle_result_write(&(server->con), &(server->result), false);
469
 
  DRIZZLE_RETURN_CHECK_VAL(ret, "drizzle_result_write", &(server->drizzle))
 
471
  ret= drizzle_result_write(server->con, server->result, false);
 
472
  DRIZZLE_RETURN_CHECK_VAL(ret, "drizzle_result_write", server->drizzle)
470
473
 
471
474
  /* This is needed for MySQL and old Drizzle protocol. */
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))
 
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)
482
485
 
483
486
  return DRIZZLE_RETURN_OK;
484
487
}
485
488
 
486
489
static void usage(char *name)
487
490
{
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");
 
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");
495
497
}