158
159
sqlite3_open(argv[optind], &(server->db));
159
160
if (server->db == NULL)
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");
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");
182
182
drizzle_con_set_tcp(con_listen, host, port);
186
185
drizzle_con_add_options(con_listen, DRIZZLE_CON_MYSQL);
189
187
if (drizzle_con_listen(con_listen) != DRIZZLE_RETURN_OK)
191
fprintf(stderr, "drizzle_con_listen:%s\n", drizzle_error(server->drizzle));
189
printf("drizzle_con_listen:%s\n", drizzle_error(&(server->drizzle)));
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)
200
fprintf(stderr, "drizzle_con_accept:%s\n", drizzle_error(server->drizzle));
198
printf("drizzle_con_accept:%s\n", drizzle_error(&(server->drizzle)));
204
202
server_run(server);
206
drizzle_con_free(server->con);
204
drizzle_con_free(&(server->con));
232
231
char *sqlite_err;
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);
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)
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))
258
257
/* Command loop. */
261
drizzle_result_free(server->result);
260
drizzle_result_free(&(server->result));
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))
271
DRIZZLE_RETURN_CHECK(ret, "drizzle_con_command_buffer", server->drizzle);
270
DRIZZLE_RETURN_CHECK(ret, "drizzle_con_command_buffer", &(server->drizzle))
273
272
if (server->verbose >= DRIZZLE_VERBOSE_INFO)
275
fprintf(stderr, "Command=%u Data=%s\n", command,
274
printf("Command=%u Data=%s\n", command,
276
275
data == NULL ? "NULL" : (char *)data);
279
if ((server->result= drizzle_result_create(server->con)) == NULL)
281
DRIZZLE_RETURN_ERROR("drizzle_result_create", server->drizzle);
278
if (drizzle_result_create(&(server->con), &(server->result)) == NULL)
279
DRIZZLE_RETURN_ERROR("drizzle_result_create", &(server->drizzle))
284
281
if (command != DRIZZLE_COMMAND_QUERY ||
285
282
!strcasecmp((char *)data, "SHOW DATABASES"))
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))
290
287
if (command == DRIZZLE_COMMAND_FIELD_LIST)
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))
324
321
if (sqlite_ret != SQLITE_OK)
326
323
if (sqlite_err == NULL)
328
fprintf(stderr, "sqlite3_exec failed\n");
324
printf("sqlite3_exec failed\n");
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))
337
fprintf(stderr, "sqlite3_exec:%s\n", sqlite_err);
332
printf("sqlite3_exec:%s\n", sqlite_err);
338
333
sqlite3_free(sqlite_err);
344
339
if (server->rows == 0)
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))
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))
366
361
if (server->send_columns == true)
368
363
server->send_columns= false;
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)
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)
376
DRIZZLE_RETURN_CHECK_VAL(DRIZZLE_RETURN_MEMORY, "drizzle_column_create", server->drizzle)
371
DRIZZLE_RETURN_CHECK_VAL(DRIZZLE_RETURN_MEMORY, "drizzle_column_create",
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]);
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))
398
394
drizzle_column_free(&(server->column));
400
drizzle_result_set_eof(server->result, true);
396
drizzle_result_set_eof(&(server->result), true);
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))
406
402
for (x= 0; x < field_count; x++)
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,
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))
421
417
for (x= 0; x < field_count; x++)
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))
441
437
fields[0]= (drizzle_field_t)SQLITE_SERVER_VERSION;
442
438
sizes[0]= strlen(SQLITE_SERVER_VERSION);
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)
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)
451
DRIZZLE_RETURN_CHECK_VAL(DRIZZLE_RETURN_MEMORY, "drizzle_column_create", server->drizzle)
447
DRIZZLE_RETURN_CHECK_VAL(DRIZZLE_RETURN_MEMORY, "drizzle_column_create",
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");
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))
467
464
drizzle_column_free(&(server->column));
469
drizzle_result_set_eof(server->result, true);
466
drizzle_result_set_eof(&(server->result), true);
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))
474
471
/* This is needed for MySQL and old Drizzle protocol. */
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)
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))
486
483
return DRIZZLE_RETURN_OK;
489
486
static void usage(char *name)
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");