159
158
sqlite3_open(argv[optind], &(server->db));
160
159
if (server->db == NULL)
162
printf("sqlite3_open: could not open sqlite3 db\n");
166
if (drizzle_create(&(server->drizzle)) == NULL)
168
printf("drizzle_create:NULL\n");
172
drizzle_add_options(&(server->drizzle), DRIZZLE_FREE_OBJECTS);
173
drizzle_set_verbose(&(server->drizzle), server->verbose);
175
if (drizzle_con_create(&(server->drizzle), con_listen) == NULL)
177
printf("drizzle_con_create:NULL\n");
161
fprintf(stderr, "sqlite3_open: could not open sqlite3 db\n");
165
if ((server->drizzle= drizzle_create()) == NULL)
167
fprintf(stderr, "drizzle_create:NULL\n");
171
drizzle_set_option(server->drizzle, DRIZZLE_FREE_OBJECTS, true);
172
drizzle_set_verbose(server->drizzle, server->verbose);
174
drizzle_con_st *con_listen;
175
if ((con_listen= drizzle_con_create(server->drizzle)) == NULL)
177
fprintf(stderr, "drizzle_con_create:NULL\n");
182
182
drizzle_con_set_tcp(con_listen, host, port);
185
186
drizzle_con_add_options(con_listen, DRIZZLE_CON_MYSQL);
187
189
if (drizzle_con_listen(con_listen) != DRIZZLE_RETURN_OK)
189
printf("drizzle_con_listen:%s\n", drizzle_error(&(server->drizzle)));
191
fprintf(stderr, "drizzle_con_listen:%s\n", drizzle_error(server->drizzle));
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)
198
printf("drizzle_con_accept:%s\n", drizzle_error(&(server->drizzle)));
200
fprintf(stderr, "drizzle_con_accept:%s\n", drizzle_error(server->drizzle));
202
204
server_run(server);
204
drizzle_con_free(&(server->con));
206
drizzle_con_free(server->con);
231
232
char *sqlite_err;
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);
244
ret= drizzle_handshake_server_write(&(server->con));
245
DRIZZLE_RETURN_CHECK(ret, "drizzle_handshake_server_write",
248
ret= drizzle_handshake_client_read(&(server->con));
249
DRIZZLE_RETURN_CHECK(ret, "drizzle_handshake_client_read", &(server->drizzle))
251
if (drizzle_result_create(&(server->con), &(server->result)) == NULL)
252
DRIZZLE_RETURN_ERROR("drizzle_result_create", &(server->drizzle))
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);
244
ret= drizzle_handshake_server_write(server->con);
245
DRIZZLE_RETURN_CHECK(ret, "drizzle_handshake_server_write", server->drizzle)
247
ret= drizzle_handshake_client_read(server->con);
248
DRIZZLE_RETURN_CHECK(ret, "drizzle_handshake_client_read", server->drizzle)
250
if ((server->result= drizzle_result_create(server->con)) == NULL)
252
DRIZZLE_RETURN_ERROR("drizzle_result_create", server->drizzle);
255
ret= drizzle_result_write(server->con, server->result, true);
256
DRIZZLE_RETURN_CHECK(ret, "drizzle_result_write", server->drizzle)
257
258
/* Command loop. */
260
drizzle_result_free(&(server->result));
261
drizzle_result_free(server->result);
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))
270
DRIZZLE_RETURN_CHECK(ret, "drizzle_con_command_buffer", &(server->drizzle))
271
DRIZZLE_RETURN_CHECK(ret, "drizzle_con_command_buffer", server->drizzle);
272
273
if (server->verbose >= DRIZZLE_VERBOSE_INFO)
274
printf("Command=%u Data=%s\n", command,
275
fprintf(stderr, "Command=%u Data=%s\n", command,
275
276
data == NULL ? "NULL" : (char *)data);
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)
281
DRIZZLE_RETURN_ERROR("drizzle_result_create", server->drizzle);
281
284
if (command != DRIZZLE_COMMAND_QUERY ||
282
285
!strcasecmp((char *)data, "SHOW DATABASES"))
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);
287
290
if (command == DRIZZLE_COMMAND_FIELD_LIST)
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);
321
324
if (sqlite_ret != SQLITE_OK)
323
326
if (sqlite_err == NULL)
324
printf("sqlite3_exec failed\n");
328
fprintf(stderr, "sqlite3_exec failed\n");
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)
332
printf("sqlite3_exec:%s\n", sqlite_err);
337
fprintf(stderr, "sqlite3_exec:%s\n", sqlite_err);
333
338
sqlite3_free(sqlite_err);
339
344
if (server->rows == 0)
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)
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)
361
366
if (server->send_columns == true)
363
368
server->send_columns= false;
364
drizzle_result_set_column_count(&(server->result), (uint16_t)field_count);
366
ret= drizzle_result_write(&(server->con), &(server->result), false);
367
DRIZZLE_RETURN_CHECK_VAL(ret, "drizzle_result_write", &(server->drizzle))
369
if (drizzle_column_create(&(server->result), &(server->column)) == NULL)
369
drizzle_result_set_column_count(server->result, (uint16_t)field_count);
371
ret= drizzle_result_write(server->con, server->result, false);
372
DRIZZLE_RETURN_CHECK_VAL(ret, "drizzle_result_write", server->drizzle)
374
if (drizzle_column_create(server->result, &(server->column)) == NULL)
371
DRIZZLE_RETURN_CHECK_VAL(DRIZZLE_RETURN_MEMORY, "drizzle_column_create",
376
DRIZZLE_RETURN_CHECK_VAL(DRIZZLE_RETURN_MEMORY, "drizzle_column_create", server->drizzle)
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]);
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)
394
398
drizzle_column_free(&(server->column));
396
drizzle_result_set_eof(&(server->result), true);
400
drizzle_result_set_eof(server->result, true);
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)
402
406
for (x= 0; x < field_count; x++)
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,
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)
417
421
for (x= 0; x < field_count; x++)
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)
437
441
fields[0]= (drizzle_field_t)SQLITE_SERVER_VERSION;
438
442
sizes[0]= strlen(SQLITE_SERVER_VERSION);
440
drizzle_result_set_column_count(&(server->result), 1);
442
ret= drizzle_result_write(&(server->con), &(server->result), false);
443
DRIZZLE_RETURN_CHECK_VAL(ret, "drizzle_result_write", &(server->drizzle))
445
if (drizzle_column_create(&(server->result), &(server->column)) == NULL)
444
drizzle_result_set_column_count(server->result, 1);
446
ret= drizzle_result_write(server->con, server->result, false);
447
DRIZZLE_RETURN_CHECK_VAL(ret, "drizzle_result_write", server->drizzle)
449
if (drizzle_column_create(server->result, &(server->column)) == NULL)
447
DRIZZLE_RETURN_CHECK_VAL(DRIZZLE_RETURN_MEMORY, "drizzle_column_create",
451
DRIZZLE_RETURN_CHECK_VAL(DRIZZLE_RETURN_MEMORY, "drizzle_column_create", server->drizzle)
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");
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)
464
467
drizzle_column_free(&(server->column));
466
drizzle_result_set_eof(&(server->result), true);
469
drizzle_result_set_eof(server->result, true);
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)
471
474
/* This is needed for MySQL and old Drizzle protocol. */
472
drizzle_result_calc_row_size(&(server->result), fields, sizes);
474
ret= drizzle_row_write(&(server->result));
475
DRIZZLE_RETURN_CHECK_VAL(ret, "drizzle_row_write", &(server->drizzle))
477
ret= drizzle_field_write(&(server->result), fields[0], sizes[0], sizes[0]);
478
DRIZZLE_RETURN_CHECK_VAL(ret, "drizzle_field_write", &(server->drizzle))
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);
477
ret= drizzle_row_write(server->result);
478
DRIZZLE_RETURN_CHECK_VAL(ret, "drizzle_row_write", server->drizzle)
480
ret= drizzle_field_write(server->result, fields[0], sizes[0], sizes[0]);
481
DRIZZLE_RETURN_CHECK_VAL(ret, "drizzle_field_write", server->drizzle)
483
ret= drizzle_result_write(server->con, server->result, true);
484
DRIZZLE_RETURN_CHECK_VAL(ret, "drizzle_result_write", server->drizzle)
483
486
return DRIZZLE_RETURN_OK;
486
489
static void usage(char *name)
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");