2
* Drizzle Client & Protocol Library
4
* Copyright (C) 2008 Eric Day (eday@oddments.org)
7
* Use and distribution licensed under the BSD license. See
8
* the COPYING file in this directory for full text.
16
#include <libdrizzle/drizzle_server.h>
18
#define DRIZZLE_FIELD_MAX 32
19
#define DRIZZLE_RESULT_ROWS 20
21
#define DRIZZLE_RETURN_CHECK(__ret, __function, __drizzle) \
23
if ((__ret) != DRIZZLE_RETURN_OK) \
24
DRIZZLE_RETURN_ERROR(__function, __drizzle) \
27
#define DRIZZLE_RETURN_ERROR(__function, __drizzle) \
29
printf(__function ":%s\n", drizzle_error(__drizzle)); \
33
static void server(drizzle_st *drizzle, drizzle_con_st *con,
34
drizzle_result_st *result, drizzle_column_st *column);
36
int main(int argc, char *argv[])
40
const char *host= NULL;
43
drizzle_verbose_t verbose= DRIZZLE_VERBOSE_NEVER;
46
drizzle_con_st con_listen;
48
drizzle_result_st result;
49
drizzle_column_st column;
51
while((c = getopt(argc, argv, "c:h:mp:v")) != -1)
56
count= (uint32_t)atoi(optarg);
68
port= (in_port_t)atoi(optarg);
76
printf("\nusage: %s [-c <count>] [-h <host>] [-m] [-p <port>] [-v]\n",
78
printf("\t-c <count> - Number of connections to accept before exiting\n");
79
printf("\t-h <host> - Host to listen on\n");
80
printf("\t-m - Use the MySQL protocol\n");
81
printf("\t-p <port> - Port to listen on\n");
82
printf("\t-v - Increase verbosity level\n");
87
if (drizzle_create(&drizzle) == NULL)
89
printf("drizzle_create:NULL\n");
93
drizzle_add_options(&drizzle, DRIZZLE_FREE_OBJECTS);
94
drizzle_set_verbose(&drizzle, verbose);
96
if (drizzle_con_create(&drizzle, &con_listen) == NULL)
98
printf("drizzle_con_create:NULL\n");
102
drizzle_con_add_options(&con_listen, DRIZZLE_CON_LISTEN);
103
drizzle_con_set_tcp(&con_listen, host, port);
106
drizzle_con_add_options(&con_listen, DRIZZLE_CON_MYSQL);
108
if (drizzle_con_listen(&con_listen) != DRIZZLE_RETURN_OK)
110
printf("drizzle_con_listen:%s\n", drizzle_error(&drizzle));
116
(void)drizzle_con_accept(&drizzle, &con, &ret);
117
if (ret != DRIZZLE_RETURN_OK)
119
printf("drizzle_con_accept:%s\n", drizzle_error(&drizzle));
123
server(&drizzle, &con, &result, &column);
125
drizzle_con_free(&con);
136
drizzle_con_free(&con_listen);
137
drizzle_free(&drizzle);
142
static void server(drizzle_st *drizzle, drizzle_con_st *con,
143
drizzle_result_st *result, drizzle_column_st *column)
145
drizzle_return_t ret;
146
drizzle_command_t command;
150
char field1[DRIZZLE_FIELD_MAX];
151
char field2[DRIZZLE_FIELD_MAX];
158
/* Handshake packets. */
159
drizzle_con_set_protocol_version(con, 10);
160
drizzle_con_set_server_version(con, "libdrizzle example 1.2.3");
161
drizzle_con_set_thread_id(con, 1);
162
drizzle_con_set_scramble(con, (const uint8_t *)"ABCDEFGHIJKLMNOPQRST");
163
drizzle_con_set_capabilities(con, DRIZZLE_CAPABILITIES_NONE);
164
drizzle_con_set_charset(con, 8);
165
drizzle_con_set_status(con, DRIZZLE_CON_STATUS_NONE);
166
drizzle_con_set_max_packet_size(con, DRIZZLE_MAX_PACKET_SIZE);
168
ret= drizzle_handshake_server_write(con);
169
DRIZZLE_RETURN_CHECK(ret, "drizzle_handshake_server_write", drizzle)
171
ret= drizzle_handshake_client_read(con);
172
DRIZZLE_RETURN_CHECK(ret, "drizzle_handshake_client_read", drizzle)
174
if (drizzle_result_create(con, result) == NULL)
175
DRIZZLE_RETURN_ERROR("drizzle_result_create", drizzle)
177
ret= drizzle_result_write(con, result, true);
178
DRIZZLE_RETURN_CHECK(ret, "drizzle_result_write", drizzle)
183
drizzle_result_free(result);
187
data= drizzle_con_command_buffer(con, &command, &total, &ret);
188
if (ret == DRIZZLE_RETURN_LOST_CONNECTION ||
189
(ret == DRIZZLE_RETURN_OK && command == DRIZZLE_COMMAND_QUIT))
195
DRIZZLE_RETURN_CHECK(ret, "drizzle_con_command_buffer", drizzle)
197
if (drizzle_result_create(con, result) == NULL)
198
DRIZZLE_RETURN_ERROR("drizzle_result_create", drizzle)
200
if (command != DRIZZLE_COMMAND_QUERY)
202
ret= drizzle_result_write(con, result, true);
203
DRIZZLE_RETURN_CHECK(ret, "drizzle_result_write", drizzle)
207
drizzle_result_set_column_count(result, 2);
209
ret= drizzle_result_write(con, result, false);
210
DRIZZLE_RETURN_CHECK(ret, "drizzle_result_write", drizzle)
213
if (drizzle_column_create(result, column) == NULL)
214
DRIZZLE_RETURN_ERROR("drizzle_column_create", drizzle)
216
drizzle_column_set_catalog(column, "default");
217
drizzle_column_set_db(column, "drizzle_test_db");
218
drizzle_column_set_table(column, "drizzle_test_table");
219
drizzle_column_set_orig_table(column, "drizzle_test_table");
220
drizzle_column_set_name(column, "test_column_1");
221
drizzle_column_set_orig_name(column, "test_column_1");
222
drizzle_column_set_charset(column, 8);
223
drizzle_column_set_size(column, DRIZZLE_FIELD_MAX);
224
drizzle_column_set_type(column, DRIZZLE_COLUMN_TYPE_VARCHAR);
226
ret= drizzle_column_write(result, column);
227
DRIZZLE_RETURN_CHECK(ret, "drizzle_column_write", drizzle)
229
drizzle_column_set_name(column, "test_column_2");
230
drizzle_column_set_orig_name(column, "test_column_2");
232
ret= drizzle_column_write(result, column);
233
DRIZZLE_RETURN_CHECK(ret, "drizzle_column_write", drizzle)
235
drizzle_column_free(column);
237
drizzle_result_set_eof(result, true);
239
ret= drizzle_result_write(con, result, false);
240
DRIZZLE_RETURN_CHECK(ret, "drizzle_result_write", drizzle)
243
for (x= 0; x < DRIZZLE_RESULT_ROWS; x++)
245
size[0]= (size_t)snprintf(field[0], DRIZZLE_FIELD_MAX,
246
"field %" PRIu64 "-1", x);
247
if (size[0] >= DRIZZLE_FIELD_MAX)
248
size[0]= DRIZZLE_FIELD_MAX - 1;
250
size[1]= (size_t)snprintf(field[1], DRIZZLE_FIELD_MAX,
251
"field %" PRIu64 "-2", x);
252
if (size[1] >= DRIZZLE_FIELD_MAX)
253
size[1]= DRIZZLE_FIELD_MAX - 1;
255
/* This is needed for MySQL and old Drizzle protocol. */
256
drizzle_result_calc_row_size(result, (drizzle_field_t *)field, size);
258
ret= drizzle_row_write(result);
259
DRIZZLE_RETURN_CHECK(ret, "drizzle_row_write", drizzle)
262
ret= drizzle_field_write(result, (drizzle_field_t)field[0], size[0],
264
DRIZZLE_RETURN_CHECK(ret, "drizzle_field_write", drizzle)
266
ret= drizzle_field_write(result, (drizzle_field_t)field[1], size[1],
268
DRIZZLE_RETURN_CHECK(ret, "drizzle_field_write", drizzle)
271
ret= drizzle_result_write(con, result, true);
272
DRIZZLE_RETURN_CHECK(ret, "drizzle_result_write", drizzle)