1712.1.1
by Monty Taylor
Merged libdrizzle directly into tree. |
1 |
/*
|
2 |
* Drizzle Client & Protocol Library
|
|
3 |
*
|
|
4 |
* Copyright (C) 2008 Eric Day (eday@oddments.org)
|
|
5 |
* All rights reserved.
|
|
6 |
*
|
|
1971.2.1
by kalebral at gmail
update files that did not have license or had incorrect license structure |
7 |
* Redistribution and use in source and binary forms, with or without
|
8 |
* modification, are permitted provided that the following conditions are
|
|
9 |
* met:
|
|
10 |
*
|
|
11 |
* * Redistributions of source code must retain the above copyright
|
|
12 |
* notice, this list of conditions and the following disclaimer.
|
|
13 |
*
|
|
14 |
* * Redistributions in binary form must reproduce the above
|
|
15 |
* copyright notice, this list of conditions and the following disclaimer
|
|
16 |
* in the documentation and/or other materials provided with the
|
|
17 |
* distribution.
|
|
18 |
*
|
|
19 |
* * The names of its contributors may not be used to endorse or
|
|
20 |
* promote products derived from this software without specific prior
|
|
21 |
* written permission.
|
|
22 |
*
|
|
23 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
24 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
25 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
26 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
27 |
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
28 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
29 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
30 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
31 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
32 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
33 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
34 |
*
|
|
1712.1.1
by Monty Taylor
Merged libdrizzle directly into tree. |
35 |
*/
|
36 |
||
2173.2.1
by Monty Taylor
Fixes incorrect usage of include |
37 |
#include <config.h> |
2164.1.1
by Monty Taylor
Merged in outstanding changes from the win32 work. |
38 |
|
1712.1.1
by Monty Taylor
Merged libdrizzle directly into tree. |
39 |
#include <errno.h> |
40 |
#include <stdio.h> |
|
41 |
#include <stdlib.h> |
|
42 |
#include <unistd.h> |
|
43 |
||
2463.1.3
by Brian Aker
Redo the way options are handled. |
44 |
#include <libdrizzle-2.0/drizzle_server.h> |
1712.1.1
by Monty Taylor
Merged libdrizzle directly into tree. |
45 |
|
46 |
#define DRIZZLE_FIELD_MAX 32
|
|
47 |
#define DRIZZLE_RESULT_ROWS 20
|
|
48 |
||
49 |
#define DRIZZLE_RETURN_CHECK(__ret, __function, __drizzle) \
|
|
50 |
{ \
|
|
51 |
if ((__ret) != DRIZZLE_RETURN_OK) \
|
|
52 |
DRIZZLE_RETURN_ERROR(__function, __drizzle) \
|
|
53 |
}
|
|
54 |
||
55 |
#define DRIZZLE_RETURN_ERROR(__function, __drizzle) \
|
|
56 |
{ \
|
|
57 |
printf(__function ":%s\n", drizzle_error(__drizzle)); \
|
|
58 |
return; \
|
|
59 |
}
|
|
60 |
||
2463.1.5
by Brian Aker
Move result over to being behind API. |
61 |
static void server(drizzle_st *drizzle, drizzle_con_st *con, drizzle_column_st *column); |
1712.1.1
by Monty Taylor
Merged libdrizzle directly into tree. |
62 |
|
63 |
int main(int argc, char *argv[]) |
|
64 |
{
|
|
65 |
int c; |
|
66 |
uint32_t count= 0; |
|
67 |
const char *host= NULL; |
|
68 |
bool mysql= false; |
|
69 |
in_port_t port= 0; |
|
70 |
drizzle_verbose_t verbose= DRIZZLE_VERBOSE_NEVER; |
|
71 |
drizzle_return_t ret; |
|
2463.1.3
by Brian Aker
Redo the way options are handled. |
72 |
drizzle_st *drizzle; |
1712.1.1
by Monty Taylor
Merged libdrizzle directly into tree. |
73 |
drizzle_column_st column; |
74 |
||
75 |
while((c = getopt(argc, argv, "c:h:mp:v")) != -1) |
|
76 |
{
|
|
77 |
switch(c) |
|
78 |
{
|
|
79 |
case 'c': |
|
80 |
count= (uint32_t)atoi(optarg); |
|
81 |
break; |
|
82 |
||
83 |
case 'h': |
|
84 |
host= optarg; |
|
85 |
break; |
|
86 |
||
87 |
case 'm': |
|
88 |
mysql= true; |
|
89 |
break; |
|
90 |
||
91 |
case 'p': |
|
92 |
port= (in_port_t)atoi(optarg); |
|
93 |
break; |
|
94 |
||
95 |
case 'v': |
|
1992.6.2
by Monty Taylor
Cleaned up for additional gcc 4.5 warnings. |
96 |
switch(verbose) |
97 |
{
|
|
98 |
case DRIZZLE_VERBOSE_NEVER: |
|
99 |
verbose= DRIZZLE_VERBOSE_FATAL; |
|
100 |
break; |
|
101 |
case DRIZZLE_VERBOSE_FATAL: |
|
102 |
verbose= DRIZZLE_VERBOSE_ERROR; |
|
103 |
break; |
|
104 |
case DRIZZLE_VERBOSE_ERROR: |
|
105 |
verbose= DRIZZLE_VERBOSE_INFO; |
|
106 |
break; |
|
107 |
case DRIZZLE_VERBOSE_INFO: |
|
108 |
verbose= DRIZZLE_VERBOSE_DEBUG; |
|
109 |
break; |
|
110 |
case DRIZZLE_VERBOSE_DEBUG: |
|
111 |
verbose= DRIZZLE_VERBOSE_CRAZY; |
|
112 |
break; |
|
113 |
case DRIZZLE_VERBOSE_CRAZY: |
|
114 |
case DRIZZLE_VERBOSE_MAX: |
|
115 |
break; |
|
116 |
}
|
|
1712.1.1
by Monty Taylor
Merged libdrizzle directly into tree. |
117 |
break; |
118 |
||
119 |
default: |
|
120 |
printf("\nusage: %s [-c <count>] [-h <host>] [-m] [-p <port>] [-v]\n", |
|
121 |
argv[0]); |
|
122 |
printf("\t-c <count> - Number of connections to accept before exiting\n"); |
|
123 |
printf("\t-h <host> - Host to listen on\n"); |
|
124 |
printf("\t-m - Use the MySQL protocol\n"); |
|
125 |
printf("\t-p <port> - Port to listen on\n"); |
|
126 |
printf("\t-v - Increase verbosity level\n"); |
|
127 |
return 1; |
|
128 |
}
|
|
129 |
}
|
|
130 |
||
2463.1.3
by Brian Aker
Redo the way options are handled. |
131 |
if ((drizzle= drizzle_create()) == NULL) |
1712.1.1
by Monty Taylor
Merged libdrizzle directly into tree. |
132 |
{
|
2463.1.3
by Brian Aker
Redo the way options are handled. |
133 |
fprintf(stderr, "drizzle_create:NULL\n"); |
1712.1.1
by Monty Taylor
Merged libdrizzle directly into tree. |
134 |
return 1; |
135 |
}
|
|
136 |
||
2463.1.3
by Brian Aker
Redo the way options are handled. |
137 |
drizzle_set_option(drizzle, DRIZZLE_FREE_OBJECTS, true); |
138 |
drizzle_set_verbose(drizzle, verbose); |
|
1712.1.1
by Monty Taylor
Merged libdrizzle directly into tree. |
139 |
|
2463.1.4
by Brian Aker
Remove con from being passed object. |
140 |
drizzle_con_st* con_listen; |
141 |
if ((con_listen= drizzle_con_create(drizzle)) == NULL) |
|
1712.1.1
by Monty Taylor
Merged libdrizzle directly into tree. |
142 |
{
|
2463.1.3
by Brian Aker
Redo the way options are handled. |
143 |
fprintf(stderr, "drizzle_con_create:NULL\n"); |
1712.1.1
by Monty Taylor
Merged libdrizzle directly into tree. |
144 |
return 1; |
145 |
}
|
|
146 |
||
1929.1.29
by Stewart Smith
fix examples/server.c stack usage: |
147 |
drizzle_con_add_options(con_listen, DRIZZLE_CON_LISTEN); |
148 |
drizzle_con_set_tcp(con_listen, host, port); |
|
1712.1.1
by Monty Taylor
Merged libdrizzle directly into tree. |
149 |
|
150 |
if (mysql) |
|
2463.1.3
by Brian Aker
Redo the way options are handled. |
151 |
{
|
1929.1.29
by Stewart Smith
fix examples/server.c stack usage: |
152 |
drizzle_con_add_options(con_listen, DRIZZLE_CON_MYSQL); |
2463.1.3
by Brian Aker
Redo the way options are handled. |
153 |
}
|
1712.1.1
by Monty Taylor
Merged libdrizzle directly into tree. |
154 |
|
1929.1.29
by Stewart Smith
fix examples/server.c stack usage: |
155 |
if (drizzle_con_listen(con_listen) != DRIZZLE_RETURN_OK) |
1712.1.1
by Monty Taylor
Merged libdrizzle directly into tree. |
156 |
{
|
2463.1.3
by Brian Aker
Redo the way options are handled. |
157 |
fprintf(stderr, "drizzle_con_listen:%s\n", drizzle_error(drizzle)); |
1712.1.1
by Monty Taylor
Merged libdrizzle directly into tree. |
158 |
return 1; |
159 |
}
|
|
160 |
||
161 |
while (1) |
|
162 |
{
|
|
2463.1.4
by Brian Aker
Remove con from being passed object. |
163 |
drizzle_con_st *con= drizzle_con_accept(drizzle, &ret); |
1712.1.1
by Monty Taylor
Merged libdrizzle directly into tree. |
164 |
if (ret != DRIZZLE_RETURN_OK) |
165 |
{
|
|
2463.1.3
by Brian Aker
Redo the way options are handled. |
166 |
fprintf(stderr, "drizzle_con_accept:%s\n", drizzle_error(drizzle)); |
1712.1.1
by Monty Taylor
Merged libdrizzle directly into tree. |
167 |
return 1; |
168 |
}
|
|
169 |
||
2463.1.5
by Brian Aker
Move result over to being behind API. |
170 |
server(drizzle, con, &column); |
1712.1.1
by Monty Taylor
Merged libdrizzle directly into tree. |
171 |
|
1929.1.29
by Stewart Smith
fix examples/server.c stack usage: |
172 |
drizzle_con_free(con); |
1712.1.1
by Monty Taylor
Merged libdrizzle directly into tree. |
173 |
|
174 |
if (count > 0) |
|
175 |
{
|
|
176 |
count--; |
|
177 |
||
178 |
if (count == 0) |
|
179 |
break; |
|
180 |
}
|
|
181 |
}
|
|
182 |
||
1929.1.29
by Stewart Smith
fix examples/server.c stack usage: |
183 |
drizzle_con_free(con_listen); |
2463.1.3
by Brian Aker
Redo the way options are handled. |
184 |
drizzle_free(drizzle); |
1712.1.1
by Monty Taylor
Merged libdrizzle directly into tree. |
185 |
|
186 |
return 0; |
|
187 |
}
|
|
188 |
||
189 |
static void server(drizzle_st *drizzle, drizzle_con_st *con, |
|
2463.1.5
by Brian Aker
Move result over to being behind API. |
190 |
drizzle_column_st *column) |
1712.1.1
by Monty Taylor
Merged libdrizzle directly into tree. |
191 |
{
|
192 |
drizzle_return_t ret; |
|
193 |
drizzle_command_t command; |
|
194 |
uint8_t *data= NULL; |
|
195 |
size_t total; |
|
196 |
char *field[2]; |
|
197 |
char field1[DRIZZLE_FIELD_MAX]; |
|
198 |
char field2[DRIZZLE_FIELD_MAX]; |
|
199 |
size_t size[2]; |
|
200 |
uint64_t x; |
|
201 |
||
202 |
field[0]= field1; |
|
203 |
field[1]= field2; |
|
204 |
||
205 |
/* Handshake packets. */
|
|
206 |
drizzle_con_set_protocol_version(con, 10); |
|
207 |
drizzle_con_set_server_version(con, "libdrizzle example 1.2.3"); |
|
208 |
drizzle_con_set_thread_id(con, 1); |
|
209 |
drizzle_con_set_scramble(con, (const uint8_t *)"ABCDEFGHIJKLMNOPQRST"); |
|
210 |
drizzle_con_set_capabilities(con, DRIZZLE_CAPABILITIES_NONE); |
|
211 |
drizzle_con_set_charset(con, 8); |
|
212 |
drizzle_con_set_status(con, DRIZZLE_CON_STATUS_NONE); |
|
213 |
drizzle_con_set_max_packet_size(con, DRIZZLE_MAX_PACKET_SIZE); |
|
214 |
||
215 |
ret= drizzle_handshake_server_write(con); |
|
216 |
DRIZZLE_RETURN_CHECK(ret, "drizzle_handshake_server_write", drizzle) |
|
217 |
||
218 |
ret= drizzle_handshake_client_read(con); |
|
2463.1.5
by Brian Aker
Move result over to being behind API. |
219 |
DRIZZLE_RETURN_CHECK(ret, "drizzle_handshake_client_read", drizzle); |
1712.1.1
by Monty Taylor
Merged libdrizzle directly into tree. |
220 |
|
2463.1.5
by Brian Aker
Move result over to being behind API. |
221 |
drizzle_result_st *result; |
222 |
if ((result= drizzle_result_create(con)) == NULL) |
|
223 |
{
|
|
1712.1.1
by Monty Taylor
Merged libdrizzle directly into tree. |
224 |
DRIZZLE_RETURN_ERROR("drizzle_result_create", drizzle) |
2463.1.5
by Brian Aker
Move result over to being behind API. |
225 |
}
|
1712.1.1
by Monty Taylor
Merged libdrizzle directly into tree. |
226 |
|
227 |
ret= drizzle_result_write(con, result, true); |
|
228 |
DRIZZLE_RETURN_CHECK(ret, "drizzle_result_write", drizzle) |
|
229 |
||
230 |
/* Command loop. */
|
|
231 |
while (1) |
|
232 |
{
|
|
233 |
drizzle_result_free(result); |
|
2353.3.1
by Mark Atwood
fix cppcheck redundantIfDelete0 warnings. It is safe to deallocate a NULL pointer |
234 |
free(data); |
1712.1.1
by Monty Taylor
Merged libdrizzle directly into tree. |
235 |
|
1992.6.2
by Monty Taylor
Cleaned up for additional gcc 4.5 warnings. |
236 |
data= (uint8_t *)drizzle_con_command_buffer(con, &command, &total, &ret); |
1712.1.1
by Monty Taylor
Merged libdrizzle directly into tree. |
237 |
if (ret == DRIZZLE_RETURN_LOST_CONNECTION || |
238 |
(ret == DRIZZLE_RETURN_OK && command == DRIZZLE_COMMAND_QUIT)) |
|
239 |
{
|
|
2353.3.1
by Mark Atwood
fix cppcheck redundantIfDelete0 warnings. It is safe to deallocate a NULL pointer |
240 |
free(data); |
1712.1.1
by Monty Taylor
Merged libdrizzle directly into tree. |
241 |
return; |
242 |
}
|
|
243 |
DRIZZLE_RETURN_CHECK(ret, "drizzle_con_command_buffer", drizzle) |
|
244 |
||
2463.1.5
by Brian Aker
Move result over to being behind API. |
245 |
if ((result= drizzle_result_create(con)) == NULL) |
246 |
{
|
|
1712.1.1
by Monty Taylor
Merged libdrizzle directly into tree. |
247 |
DRIZZLE_RETURN_ERROR("drizzle_result_create", drizzle) |
2463.1.5
by Brian Aker
Move result over to being behind API. |
248 |
}
|
1712.1.1
by Monty Taylor
Merged libdrizzle directly into tree. |
249 |
|
250 |
if (command != DRIZZLE_COMMAND_QUERY) |
|
251 |
{
|
|
252 |
ret= drizzle_result_write(con, result, true); |
|
253 |
DRIZZLE_RETURN_CHECK(ret, "drizzle_result_write", drizzle) |
|
254 |
continue; |
|
255 |
}
|
|
256 |
||
257 |
drizzle_result_set_column_count(result, 2); |
|
258 |
||
259 |
ret= drizzle_result_write(con, result, false); |
|
260 |
DRIZZLE_RETURN_CHECK(ret, "drizzle_result_write", drizzle) |
|
261 |
||
262 |
/* Columns. */
|
|
263 |
if (drizzle_column_create(result, column) == NULL) |
|
2463.1.5
by Brian Aker
Move result over to being behind API. |
264 |
{
|
1712.1.1
by Monty Taylor
Merged libdrizzle directly into tree. |
265 |
DRIZZLE_RETURN_ERROR("drizzle_column_create", drizzle) |
2463.1.5
by Brian Aker
Move result over to being behind API. |
266 |
}
|
1712.1.1
by Monty Taylor
Merged libdrizzle directly into tree. |
267 |
|
268 |
drizzle_column_set_catalog(column, "default"); |
|
269 |
drizzle_column_set_db(column, "drizzle_test_db"); |
|
270 |
drizzle_column_set_table(column, "drizzle_test_table"); |
|
271 |
drizzle_column_set_orig_table(column, "drizzle_test_table"); |
|
272 |
drizzle_column_set_name(column, "test_column_1"); |
|
273 |
drizzle_column_set_orig_name(column, "test_column_1"); |
|
274 |
drizzle_column_set_charset(column, 8); |
|
275 |
drizzle_column_set_size(column, DRIZZLE_FIELD_MAX); |
|
276 |
drizzle_column_set_type(column, DRIZZLE_COLUMN_TYPE_VARCHAR); |
|
277 |
||
278 |
ret= drizzle_column_write(result, column); |
|
279 |
DRIZZLE_RETURN_CHECK(ret, "drizzle_column_write", drizzle) |
|
280 |
||
281 |
drizzle_column_set_name(column, "test_column_2"); |
|
282 |
drizzle_column_set_orig_name(column, "test_column_2"); |
|
283 |
||
284 |
ret= drizzle_column_write(result, column); |
|
285 |
DRIZZLE_RETURN_CHECK(ret, "drizzle_column_write", drizzle) |
|
286 |
||
287 |
drizzle_column_free(column); |
|
288 |
||
289 |
drizzle_result_set_eof(result, true); |
|
290 |
||
291 |
ret= drizzle_result_write(con, result, false); |
|
292 |
DRIZZLE_RETURN_CHECK(ret, "drizzle_result_write", drizzle) |
|
293 |
||
294 |
/* Rows. */
|
|
295 |
for (x= 0; x < DRIZZLE_RESULT_ROWS; x++) |
|
296 |
{
|
|
297 |
size[0]= (size_t)snprintf(field[0], DRIZZLE_FIELD_MAX, |
|
298 |
"field %" PRIu64 "-1", x); |
|
299 |
if (size[0] >= DRIZZLE_FIELD_MAX) |
|
300 |
size[0]= DRIZZLE_FIELD_MAX - 1; |
|
301 |
||
302 |
size[1]= (size_t)snprintf(field[1], DRIZZLE_FIELD_MAX, |
|
303 |
"field %" PRIu64 "-2", x); |
|
304 |
if (size[1] >= DRIZZLE_FIELD_MAX) |
|
305 |
size[1]= DRIZZLE_FIELD_MAX - 1; |
|
306 |
||
307 |
/* This is needed for MySQL and old Drizzle protocol. */
|
|
308 |
drizzle_result_calc_row_size(result, (drizzle_field_t *)field, size); |
|
309 |
||
310 |
ret= drizzle_row_write(result); |
|
311 |
DRIZZLE_RETURN_CHECK(ret, "drizzle_row_write", drizzle) |
|
312 |
||
313 |
/* Fields. */
|
|
314 |
ret= drizzle_field_write(result, (drizzle_field_t)field[0], size[0], |
|
315 |
size[0]); |
|
316 |
DRIZZLE_RETURN_CHECK(ret, "drizzle_field_write", drizzle) |
|
317 |
||
318 |
ret= drizzle_field_write(result, (drizzle_field_t)field[1], size[1], |
|
319 |
size[1]); |
|
320 |
DRIZZLE_RETURN_CHECK(ret, "drizzle_field_write", drizzle) |
|
321 |
}
|
|
322 |
||
323 |
ret= drizzle_result_write(con, result, true); |
|
324 |
DRIZZLE_RETURN_CHECK(ret, "drizzle_result_write", drizzle) |
|
325 |
}
|
|
326 |
}
|