~drizzle-trunk/drizzle/development

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
44
#include <libdrizzle/drizzle_server.h>
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
61
static void server(drizzle_st *drizzle, drizzle_con_st *con,
62
                   drizzle_result_st *result, drizzle_column_st *column);
63
64
int main(int argc, char *argv[])
65
{
66
  int c;
67
  uint32_t count= 0;
68
  const char *host= NULL;
69
  bool mysql= false;
70
  in_port_t port= 0;
71
  drizzle_verbose_t verbose= DRIZZLE_VERBOSE_NEVER;
72
  drizzle_return_t ret;
73
  drizzle_st drizzle;
1929.1.29 by Stewart Smith
fix examples/server.c stack usage:
74
  drizzle_con_st *con_listen= (drizzle_con_st*)malloc(sizeof(drizzle_con_st));
75
  drizzle_con_st *con= (drizzle_con_st*)malloc(sizeof(drizzle_con_st));
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
76
  drizzle_result_st result;
77
  drizzle_column_st column;
78
79
  while((c = getopt(argc, argv, "c:h:mp:v")) != -1)
80
  {
81
    switch(c)
82
    {
83
    case 'c':
84
      count= (uint32_t)atoi(optarg);
85
      break;
86
87
    case 'h':
88
      host= optarg;
89
      break;
90
91
    case 'm':
92
      mysql= true;
93
      break;
94
95
    case 'p':
96
      port= (in_port_t)atoi(optarg);
97
      break;
98
99
    case 'v':
1992.6.2 by Monty Taylor
Cleaned up for additional gcc 4.5 warnings.
100
      switch(verbose)
101
      {
102
      case DRIZZLE_VERBOSE_NEVER:
103
        verbose= DRIZZLE_VERBOSE_FATAL;
104
        break;
105
      case DRIZZLE_VERBOSE_FATAL:
106
        verbose= DRIZZLE_VERBOSE_ERROR;
107
        break;
108
      case DRIZZLE_VERBOSE_ERROR:
109
        verbose= DRIZZLE_VERBOSE_INFO;
110
        break;
111
      case DRIZZLE_VERBOSE_INFO:
112
        verbose= DRIZZLE_VERBOSE_DEBUG;
113
        break;
114
      case DRIZZLE_VERBOSE_DEBUG:
115
        verbose= DRIZZLE_VERBOSE_CRAZY;
116
        break;
117
      case DRIZZLE_VERBOSE_CRAZY:
118
      case DRIZZLE_VERBOSE_MAX:
119
        break;
120
      }
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
121
      break;
122
123
    default:
124
      printf("\nusage: %s [-c <count>] [-h <host>] [-m] [-p <port>] [-v]\n",
125
             argv[0]);
126
      printf("\t-c <count> - Number of connections to accept before exiting\n");
127
      printf("\t-h <host>  - Host to listen on\n");
128
      printf("\t-m         - Use the MySQL protocol\n");
129
      printf("\t-p <port>  - Port to listen on\n");
130
      printf("\t-v         - Increase verbosity level\n");
131
      return 1;
132
    }
133
  }
134
135
  if (drizzle_create(&drizzle) == NULL)
136
  {
137
    printf("drizzle_create:NULL\n");
138
    return 1;
139
  }
140
141
  drizzle_add_options(&drizzle, DRIZZLE_FREE_OBJECTS);
142
  drizzle_set_verbose(&drizzle, verbose);
143
1929.1.29 by Stewart Smith
fix examples/server.c stack usage:
144
  if (drizzle_con_create(&drizzle, con_listen) == NULL)
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
145
  {
146
    printf("drizzle_con_create:NULL\n");
147
    return 1;
148
  }
149
1929.1.29 by Stewart Smith
fix examples/server.c stack usage:
150
  drizzle_con_add_options(con_listen, DRIZZLE_CON_LISTEN);
151
  drizzle_con_set_tcp(con_listen, host, port);
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
152
153
  if (mysql)
1929.1.29 by Stewart Smith
fix examples/server.c stack usage:
154
    drizzle_con_add_options(con_listen, DRIZZLE_CON_MYSQL);
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
155
1929.1.29 by Stewart Smith
fix examples/server.c stack usage:
156
  if (drizzle_con_listen(con_listen) != DRIZZLE_RETURN_OK)
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
157
  {
158
    printf("drizzle_con_listen:%s\n", drizzle_error(&drizzle));
159
    return 1;
160
  }
161
162
  while (1)
163
  {
1929.1.29 by Stewart Smith
fix examples/server.c stack usage:
164
    (void)drizzle_con_accept(&drizzle, con, &ret);
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
165
    if (ret != DRIZZLE_RETURN_OK)
166
    {
167
      printf("drizzle_con_accept:%s\n", drizzle_error(&drizzle));
168
      return 1;
169
    }
170
1929.1.29 by Stewart Smith
fix examples/server.c stack usage:
171
    server(&drizzle, con, &result, &column);
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
172
1929.1.29 by Stewart Smith
fix examples/server.c stack usage:
173
    drizzle_con_free(con);
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
174
175
    if (count > 0)
176
    {
177
      count--;
178
179
      if (count == 0)
180
        break;
181
    }
182
  }
183
1929.1.29 by Stewart Smith
fix examples/server.c stack usage:
184
  drizzle_con_free(con_listen);
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
185
  drizzle_free(&drizzle);
186
1929.1.29 by Stewart Smith
fix examples/server.c stack usage:
187
  free(con);
188
  free(con_listen);
189
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
190
  return 0;
191
}
192
193
static void server(drizzle_st *drizzle, drizzle_con_st *con,
194
                   drizzle_result_st *result, drizzle_column_st *column)
195
{
196
  drizzle_return_t ret;
197
  drizzle_command_t command;
198
  uint8_t *data= NULL;
199
  size_t total;
200
  char *field[2];
201
  char field1[DRIZZLE_FIELD_MAX];
202
  char field2[DRIZZLE_FIELD_MAX];
203
  size_t size[2];
204
  uint64_t x;
205
206
  field[0]= field1;
207
  field[1]= field2;
208
209
  /* Handshake packets. */
210
  drizzle_con_set_protocol_version(con, 10);
211
  drizzle_con_set_server_version(con, "libdrizzle example 1.2.3");
212
  drizzle_con_set_thread_id(con, 1);
213
  drizzle_con_set_scramble(con, (const uint8_t *)"ABCDEFGHIJKLMNOPQRST");
214
  drizzle_con_set_capabilities(con, DRIZZLE_CAPABILITIES_NONE);
215
  drizzle_con_set_charset(con, 8);
216
  drizzle_con_set_status(con, DRIZZLE_CON_STATUS_NONE);
217
  drizzle_con_set_max_packet_size(con, DRIZZLE_MAX_PACKET_SIZE);
218
219
  ret= drizzle_handshake_server_write(con);
220
  DRIZZLE_RETURN_CHECK(ret, "drizzle_handshake_server_write", drizzle)
221
222
  ret= drizzle_handshake_client_read(con);
223
  DRIZZLE_RETURN_CHECK(ret, "drizzle_handshake_client_read", drizzle)
224
225
  if (drizzle_result_create(con, result) == NULL)
226
    DRIZZLE_RETURN_ERROR("drizzle_result_create", drizzle)
227
228
  ret= drizzle_result_write(con, result, true);
229
  DRIZZLE_RETURN_CHECK(ret, "drizzle_result_write", drizzle)
230
231
  /* Command loop. */
232
  while (1)
233
  {
234
    drizzle_result_free(result);
235
    if (data != NULL)
236
      free(data);
237
1992.6.2 by Monty Taylor
Cleaned up for additional gcc 4.5 warnings.
238
    data= (uint8_t *)drizzle_con_command_buffer(con, &command, &total, &ret);
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
239
    if (ret == DRIZZLE_RETURN_LOST_CONNECTION ||
240
        (ret == DRIZZLE_RETURN_OK && command == DRIZZLE_COMMAND_QUIT))
241
    {
242
      if (data != NULL)
243
        free(data);
244
      return;
245
    }
246
    DRIZZLE_RETURN_CHECK(ret, "drizzle_con_command_buffer", drizzle)
247
248
    if (drizzle_result_create(con, result) == NULL)
249
      DRIZZLE_RETURN_ERROR("drizzle_result_create", drizzle)
250
251
    if (command != DRIZZLE_COMMAND_QUERY)
252
    {
253
      ret= drizzle_result_write(con, result, true);
254
      DRIZZLE_RETURN_CHECK(ret, "drizzle_result_write", drizzle)
255
      continue;
256
    }
257
258
    drizzle_result_set_column_count(result, 2);
259
260
    ret= drizzle_result_write(con, result, false);
261
    DRIZZLE_RETURN_CHECK(ret, "drizzle_result_write", drizzle)
262
263
    /* Columns. */
264
    if (drizzle_column_create(result, column) == NULL)
265
      DRIZZLE_RETURN_ERROR("drizzle_column_create", drizzle)
266
267
    drizzle_column_set_catalog(column, "default");
268
    drizzle_column_set_db(column, "drizzle_test_db");
269
    drizzle_column_set_table(column, "drizzle_test_table");
270
    drizzle_column_set_orig_table(column, "drizzle_test_table");
271
    drizzle_column_set_name(column, "test_column_1");
272
    drizzle_column_set_orig_name(column, "test_column_1");
273
    drizzle_column_set_charset(column, 8);
274
    drizzle_column_set_size(column, DRIZZLE_FIELD_MAX);
275
    drizzle_column_set_type(column, DRIZZLE_COLUMN_TYPE_VARCHAR);
276
277
    ret= drizzle_column_write(result, column);
278
    DRIZZLE_RETURN_CHECK(ret, "drizzle_column_write", drizzle)
279
280
    drizzle_column_set_name(column, "test_column_2");
281
    drizzle_column_set_orig_name(column, "test_column_2");
282
283
    ret= drizzle_column_write(result, column);
284
    DRIZZLE_RETURN_CHECK(ret, "drizzle_column_write", drizzle)
285
286
    drizzle_column_free(column);
287
288
    drizzle_result_set_eof(result, true);
289
290
    ret= drizzle_result_write(con, result, false);
291
    DRIZZLE_RETURN_CHECK(ret, "drizzle_result_write", drizzle)
292
293
    /* Rows. */
294
    for (x= 0; x < DRIZZLE_RESULT_ROWS; x++)
295
    {
296
      size[0]= (size_t)snprintf(field[0], DRIZZLE_FIELD_MAX,
297
                                "field %" PRIu64 "-1", x);
298
      if (size[0] >= DRIZZLE_FIELD_MAX)
299
        size[0]= DRIZZLE_FIELD_MAX - 1;
300
301
      size[1]= (size_t)snprintf(field[1], DRIZZLE_FIELD_MAX,
302
                                "field %" PRIu64 "-2", x);
303
      if (size[1] >= DRIZZLE_FIELD_MAX)
304
        size[1]= DRIZZLE_FIELD_MAX - 1;
305
306
      /* This is needed for MySQL and old Drizzle protocol. */
307
      drizzle_result_calc_row_size(result, (drizzle_field_t *)field, size);
308
309
      ret= drizzle_row_write(result);
310
      DRIZZLE_RETURN_CHECK(ret, "drizzle_row_write", drizzle)
311
312
      /* Fields. */
313
      ret= drizzle_field_write(result, (drizzle_field_t)field[0], size[0],
314
                               size[0]);
315
      DRIZZLE_RETURN_CHECK(ret, "drizzle_field_write", drizzle)
316
317
      ret= drizzle_field_write(result, (drizzle_field_t)field[1], size[1],
318
                               size[1]);
319
      DRIZZLE_RETURN_CHECK(ret, "drizzle_field_write", drizzle)
320
    }
321
322
    ret= drizzle_result_write(con, result, true);
323
    DRIZZLE_RETURN_CHECK(ret, "drizzle_result_write", drizzle)
324
  }
325
}