~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
2244.1.1 by Monty Taylor
Split libdrizzle into 1.0 and 2.0. Applied the C++ changes to 2.0 branch.
37
#include <config.h>
1971.2.1 by kalebral at gmail
update files that did not have license or had incorrect license structure
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 <string.h>
43
#include <strings.h>
44
#include <unistd.h>
45
2449.1.2 by Brian Aker
Additional fixes for libdrizzle.
46
#include <libdrizzle-2.0/drizzle_server.h>
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
47
#include <sqlite3.h>
48
49
#define SQLITE_SERVER_VERSION "SQLite Server using libdrizzle 0.1"
50
51
#define DRIZZLE_RETURN_CHECK(__ret, __function, __drizzle) \
52
{ \
53
  if ((__ret) != DRIZZLE_RETURN_OK) \
54
    DRIZZLE_RETURN_ERROR(__function, __drizzle) \
55
}
56
57
#define DRIZZLE_RETURN_ERROR(__function, __drizzle) \
58
{ \
2463.1.2 by Brian Aker
First pass, drizzle_create() no longer takes an argument. This means that we can now change drizzle_st without being concerned about ABI.
59
  fprintf(stderr, __function ":%s\n", drizzle_error(__drizzle)); \
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
60
  return; \
61
}
62
63
#define DRIZZLE_RETURN_CHECK_VAL(__ret, __function, __drizzle) \
64
{ \
65
  if ((__ret) != DRIZZLE_RETURN_OK) \
66
  { \
2463.1.2 by Brian Aker
First pass, drizzle_create() no longer takes an argument. This means that we can now change drizzle_st without being concerned about ABI.
67
    fprintf(stderr, __function ":%s\n", drizzle_error(__drizzle)); \
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
68
    return ret; \
69
  } \
70
}
71
72
typedef struct
73
{
2463.1.2 by Brian Aker
First pass, drizzle_create() no longer takes an argument. This means that we can now change drizzle_st without being concerned about ABI.
74
  drizzle_st *drizzle;
2463.1.4 by Brian Aker
Remove con from being passed object.
75
  drizzle_con_st *con;
2463.1.5 by Brian Aker
Move result over to being behind API.
76
  drizzle_result_st *result;
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
77
  drizzle_column_st column;
78
  sqlite3* db;
79
  bool send_columns;
80
  drizzle_verbose_t verbose;
81
  uint64_t rows;
82
} sqlite_server;
83
84
static void server_run(sqlite_server *server);
85
static int row_cb(void *data, int field_count, char **fields, char **columns);
86
static drizzle_return_t send_version(sqlite_server *server);
87
static void usage(char *name);
88
89
int main(int argc, char *argv[])
90
{
91
  int c;
92
  uint32_t count= 0;
93
  const char *host= NULL;
94
  bool mysql= false;
95
  in_port_t port= 0;
96
  drizzle_return_t ret;
1929.1.35 by Stewart Smith
typo
97
  sqlite_server *server= (sqlite_server*)malloc(sizeof(sqlite_server));
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
98
1929.1.34 by Stewart Smith
fix sqlite_server stack usage
99
  server->db= NULL;
100
  server->verbose= DRIZZLE_VERBOSE_NEVER;
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
101
102
  while((c = getopt(argc, argv, "c:h:mp:v")) != -1)
103
  {
104
    switch(c)
105
    {
106
    case 'c':
107
      count= (uint32_t)atoi(optarg);
108
      break;
109
110
    case 'h':
111
      host= optarg;
112
      break;
113
114
    case 'm':
115
      mysql= true;
116
      break;
117
118
    case 'p':
119
      port= (in_port_t)atoi(optarg);
120
      break;
121
122
    case 'v':
1992.6.2 by Monty Taylor
Cleaned up for additional gcc 4.5 warnings.
123
      switch(server->verbose)
124
      {
125
      case DRIZZLE_VERBOSE_NEVER:
126
        server->verbose= DRIZZLE_VERBOSE_FATAL;
127
        break;
128
      case DRIZZLE_VERBOSE_FATAL:
129
        server->verbose= DRIZZLE_VERBOSE_ERROR;
130
        break;
131
      case DRIZZLE_VERBOSE_ERROR:
132
        server->verbose= DRIZZLE_VERBOSE_INFO;
133
        break;
134
      case DRIZZLE_VERBOSE_INFO:
135
        server->verbose= DRIZZLE_VERBOSE_DEBUG;
136
        break;
137
      case DRIZZLE_VERBOSE_DEBUG:
138
        server->verbose= DRIZZLE_VERBOSE_CRAZY;
139
        break;
140
      case DRIZZLE_VERBOSE_CRAZY:
141
      case DRIZZLE_VERBOSE_MAX:
142
        break;
143
      }
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
144
      break;
145
146
    default:
147
      usage(argv[0]);
148
      return 1;
149
    }
150
  }
151
152
  if (argc != (optind + 1))
153
  {
154
    usage(argv[0]);
155
    return 1;
156
  }
157
1929.1.34 by Stewart Smith
fix sqlite_server stack usage
158
  sqlite3_open(argv[optind], &(server->db));
159
  if (server->db == NULL)
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
160
  {
2463.1.2 by Brian Aker
First pass, drizzle_create() no longer takes an argument. This means that we can now change drizzle_st without being concerned about ABI.
161
    fprintf(stderr, "sqlite3_open: could not open sqlite3 db\n");
162
    return 1;
163
  }
164
165
  if ((server->drizzle= drizzle_create()) == NULL)
166
  {
167
    fprintf(stderr, "drizzle_create:NULL\n");
168
    return 1;
169
  }
170
2463.1.3 by Brian Aker
Redo the way options are handled.
171
  drizzle_set_option(server->drizzle, DRIZZLE_FREE_OBJECTS, true);
2463.1.2 by Brian Aker
First pass, drizzle_create() no longer takes an argument. This means that we can now change drizzle_st without being concerned about ABI.
172
  drizzle_set_verbose(server->drizzle, server->verbose);
173
2463.1.4 by Brian Aker
Remove con from being passed object.
174
  drizzle_con_st *con_listen;
175
  if ((con_listen= drizzle_con_create(server->drizzle)) == NULL)
2463.1.2 by Brian Aker
First pass, drizzle_create() no longer takes an argument. This means that we can now change drizzle_st without being concerned about ABI.
176
  {
177
    fprintf(stderr, "drizzle_con_create:NULL\n");
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
178
    return 1;
179
  }
180
1929.1.33 by Stewart Smith
fix large stack usag ein examples/sqlite_server.c:
181
  drizzle_con_add_options(con_listen, DRIZZLE_CON_LISTEN);
182
  drizzle_con_set_tcp(con_listen, host, port);
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
183
184
  if (mysql)
2463.1.4 by Brian Aker
Remove con from being passed object.
185
  {
1929.1.33 by Stewart Smith
fix large stack usag ein examples/sqlite_server.c:
186
    drizzle_con_add_options(con_listen, DRIZZLE_CON_MYSQL);
2463.1.4 by Brian Aker
Remove con from being passed object.
187
  }
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
188
1929.1.33 by Stewart Smith
fix large stack usag ein examples/sqlite_server.c:
189
  if (drizzle_con_listen(con_listen) != DRIZZLE_RETURN_OK)
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
190
  {
2463.1.2 by Brian Aker
First pass, drizzle_create() no longer takes an argument. This means that we can now change drizzle_st without being concerned about ABI.
191
    fprintf(stderr, "drizzle_con_listen:%s\n", drizzle_error(server->drizzle));
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
192
    return 1;
193
  }
194
195
  while (1)
196
  {
2463.1.4 by Brian Aker
Remove con from being passed object.
197
    server->con= drizzle_con_accept(server->drizzle, &ret);
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
198
    if (ret != DRIZZLE_RETURN_OK)
199
    {
2463.1.2 by Brian Aker
First pass, drizzle_create() no longer takes an argument. This means that we can now change drizzle_st without being concerned about ABI.
200
      fprintf(stderr, "drizzle_con_accept:%s\n", drizzle_error(server->drizzle));
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
201
      return 1;
202
    }
203
1929.1.34 by Stewart Smith
fix sqlite_server stack usage
204
    server_run(server);
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
205
2463.1.4 by Brian Aker
Remove con from being passed object.
206
    drizzle_con_free(server->con);
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
207
208
    if (count > 0)
209
    {
210
      count--;
211
212
      if (count == 0)
213
        break;
214
    }
215
  }
216
1929.1.33 by Stewart Smith
fix large stack usag ein examples/sqlite_server.c:
217
  drizzle_con_free(con_listen);
2463.1.2 by Brian Aker
First pass, drizzle_create() no longer takes an argument. This means that we can now change drizzle_st without being concerned about ABI.
218
  drizzle_free(server->drizzle);
1929.1.34 by Stewart Smith
fix sqlite_server stack usage
219
  sqlite3_close(server->db);
220
  free(server);
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
221
222
  return 0;
223
}
224
225
static void server_run(sqlite_server *server)
226
{
227
  drizzle_return_t ret;
228
  drizzle_command_t command;
229
  uint8_t *data= NULL;
230
  size_t total;
231
  int sqlite_ret;
232
  char *sqlite_err;
233
234
  /* Handshake packets. */
2463.1.4 by Brian Aker
Remove con from being passed object.
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);
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
243
2463.1.4 by Brian Aker
Remove con from being passed object.
244
  ret= drizzle_handshake_server_write(server->con);
2463.1.2 by Brian Aker
First pass, drizzle_create() no longer takes an argument. This means that we can now change drizzle_st without being concerned about ABI.
245
  DRIZZLE_RETURN_CHECK(ret, "drizzle_handshake_server_write", server->drizzle)
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
246
2463.1.4 by Brian Aker
Remove con from being passed object.
247
  ret= drizzle_handshake_client_read(server->con);
2463.1.2 by Brian Aker
First pass, drizzle_create() no longer takes an argument. This means that we can now change drizzle_st without being concerned about ABI.
248
  DRIZZLE_RETURN_CHECK(ret, "drizzle_handshake_client_read", server->drizzle)
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
249
2463.1.5 by Brian Aker
Move result over to being behind API.
250
  if ((server->result= drizzle_result_create(server->con)) == NULL)
251
  {
252
    DRIZZLE_RETURN_ERROR("drizzle_result_create", server->drizzle);
253
  }
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
254
2463.1.5 by Brian Aker
Move result over to being behind API.
255
  ret= drizzle_result_write(server->con, server->result, true);
2463.1.2 by Brian Aker
First pass, drizzle_create() no longer takes an argument. This means that we can now change drizzle_st without being concerned about ABI.
256
  DRIZZLE_RETURN_CHECK(ret, "drizzle_result_write", server->drizzle)
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
257
258
  /* Command loop. */
259
  while (1)
260
  {
2463.1.5 by Brian Aker
Move result over to being behind API.
261
    drizzle_result_free(server->result);
2353.3.1 by Mark Atwood
fix cppcheck redundantIfDelete0 warnings. It is safe to deallocate a NULL pointer
262
    free(data);
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
263
2463.1.4 by Brian Aker
Remove con from being passed object.
264
    data= (uint8_t *)drizzle_con_command_buffer(server->con, &command, &total, &ret);
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
265
    if (ret == DRIZZLE_RETURN_LOST_CONNECTION ||
266
        (ret == DRIZZLE_RETURN_OK && command == DRIZZLE_COMMAND_QUIT))
267
    {
2353.3.1 by Mark Atwood
fix cppcheck redundantIfDelete0 warnings. It is safe to deallocate a NULL pointer
268
      free(data);
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
269
      return;
270
    }
2463.1.5 by Brian Aker
Move result over to being behind API.
271
    DRIZZLE_RETURN_CHECK(ret, "drizzle_con_command_buffer", server->drizzle);
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
272
273
    if (server->verbose >= DRIZZLE_VERBOSE_INFO)
274
    {
2463.1.2 by Brian Aker
First pass, drizzle_create() no longer takes an argument. This means that we can now change drizzle_st without being concerned about ABI.
275
      fprintf(stderr, "Command=%u Data=%s\n", command,
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
276
             data == NULL ? "NULL" : (char *)data);
277
    }
278
2463.1.5 by Brian Aker
Move result over to being behind API.
279
    if ((server->result= drizzle_result_create(server->con)) == NULL)
280
    {
281
      DRIZZLE_RETURN_ERROR("drizzle_result_create", server->drizzle);
282
    }
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
283
284
    if (command != DRIZZLE_COMMAND_QUERY ||
285
        !strcasecmp((char *)data, "SHOW DATABASES"))
286
    {
2463.1.5 by Brian Aker
Move result over to being behind API.
287
      ret= drizzle_result_write(server->con, server->result, true);
288
      DRIZZLE_RETURN_CHECK(ret, "drizzle_result_write", server->drizzle);
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
289
290
      if (command == DRIZZLE_COMMAND_FIELD_LIST)
291
      {
2463.1.5 by Brian Aker
Move result over to being behind API.
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);
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
295
      }
296
297
      continue;
298
    }
299
300
    if (strstr((char *)data, "@@version") != NULL)
301
    {
302
      ret= send_version(server);
303
      if (ret != DRIZZLE_RETURN_OK)
304
        return;
305
306
      continue;
307
    }
308
309
    server->send_columns= true;
310
    server->rows= 0;
311
312
    if (!strcasecmp((char *)data, "SHOW TABLES"))
313
    {
314
      sqlite_ret= sqlite3_exec(server->db,
315
                            "SELECT name FROM sqlite_master WHERE type='table'",
316
                               row_cb, server, &sqlite_err);
317
    }
318
    else
319
    {
320
      sqlite_ret= sqlite3_exec(server->db, (char *)data, row_cb, server,
321
                               &sqlite_err);
322
    }
323
324
    if (sqlite_ret != SQLITE_OK)
325
    {
326
      if (sqlite_err == NULL)
2463.1.2 by Brian Aker
First pass, drizzle_create() no longer takes an argument. This means that we can now change drizzle_st without being concerned about ABI.
327
      {
328
        fprintf(stderr, "sqlite3_exec failed\n");
329
      }
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
330
      else
331
      {
2463.1.5 by Brian Aker
Move result over to being behind API.
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);
2463.1.2 by Brian Aker
First pass, drizzle_create() no longer takes an argument. This means that we can now change drizzle_st without being concerned about ABI.
335
        DRIZZLE_RETURN_CHECK(ret, "drizzle_result_write", server->drizzle)
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
336
2463.1.2 by Brian Aker
First pass, drizzle_create() no longer takes an argument. This means that we can now change drizzle_st without being concerned about ABI.
337
        fprintf(stderr, "sqlite3_exec:%s\n", sqlite_err);
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
338
        sqlite3_free(sqlite_err);
339
      }
340
341
      return;
342
    }
343
344
    if (server->rows == 0)
345
    {
2463.1.5 by Brian Aker
Move result over to being behind API.
346
      drizzle_result_set_column_count(server->result, 0);
347
      ret= drizzle_result_write(server->con, server->result, true);
2463.1.2 by Brian Aker
First pass, drizzle_create() no longer takes an argument. This means that we can now change drizzle_st without being concerned about ABI.
348
      DRIZZLE_RETURN_CHECK(ret, "drizzle_result_write", server->drizzle)
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
349
    }
350
    else
351
    {
2463.1.5 by Brian Aker
Move result over to being behind API.
352
      drizzle_result_set_eof(server->result, true);
353
      ret= drizzle_result_write(server->con, server->result, true);
2463.1.2 by Brian Aker
First pass, drizzle_create() no longer takes an argument. This means that we can now change drizzle_st without being concerned about ABI.
354
      DRIZZLE_RETURN_CHECK(ret, "drizzle_result_write", server->drizzle)
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
355
    }
356
  }
357
}
358
359
static int row_cb(void *data, int field_count, char **fields, char **columns)
360
{
361
  sqlite_server *server= (sqlite_server *)data;
362
  drizzle_return_t ret;
363
  int x;
1929.1.36 by Stewart Smith
one last sqlite_server stack fix
364
  size_t *sizes= (size_t*)malloc(sizeof(size_t)*8192);
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
365
366
  if (server->send_columns == true)
367
  {
368
    server->send_columns= false;
2463.1.5 by Brian Aker
Move result over to being behind API.
369
    drizzle_result_set_column_count(server->result, (uint16_t)field_count);
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
370
2463.1.5 by Brian Aker
Move result over to being behind API.
371
    ret= drizzle_result_write(server->con, server->result, false);
2463.1.2 by Brian Aker
First pass, drizzle_create() no longer takes an argument. This means that we can now change drizzle_st without being concerned about ABI.
372
    DRIZZLE_RETURN_CHECK_VAL(ret, "drizzle_result_write", server->drizzle)
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
373
2463.1.5 by Brian Aker
Move result over to being behind API.
374
    if (drizzle_column_create(server->result, &(server->column)) == NULL)
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
375
    {
2463.1.2 by Brian Aker
First pass, drizzle_create() no longer takes an argument. This means that we can now change drizzle_st without being concerned about ABI.
376
      DRIZZLE_RETURN_CHECK_VAL(DRIZZLE_RETURN_MEMORY, "drizzle_column_create", server->drizzle)
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
377
    }
378
379
    drizzle_column_set_catalog(&(server->column), "sqlite");
380
    drizzle_column_set_db(&(server->column), "sqlite_db");
381
    drizzle_column_set_table(&(server->column), "sqlite_table");
382
    drizzle_column_set_orig_table(&(server->column), "sqlite_table");
383
    drizzle_column_set_charset(&(server->column), 8);
384
    drizzle_column_set_type(&(server->column), DRIZZLE_COLUMN_TYPE_VARCHAR);
385
386
    for (x= 0; x < field_count; x++)
387
    {
388
      drizzle_column_set_size(&(server->column),
389
                              fields[x] == NULL ?
390
                              0 : (uint32_t)strlen(fields[x]));
391
      drizzle_column_set_name(&(server->column), columns[x]);
392
      drizzle_column_set_orig_name(&(server->column), columns[x]);
393
2463.1.5 by Brian Aker
Move result over to being behind API.
394
      ret= drizzle_column_write(server->result, &(server->column));
2463.1.2 by Brian Aker
First pass, drizzle_create() no longer takes an argument. This means that we can now change drizzle_st without being concerned about ABI.
395
      DRIZZLE_RETURN_CHECK_VAL(ret, "drizzle_column_write", server->drizzle)
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
396
    }
397
398
    drizzle_column_free(&(server->column));
399
2463.1.5 by Brian Aker
Move result over to being behind API.
400
    drizzle_result_set_eof(server->result, true);
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
401
2463.1.5 by Brian Aker
Move result over to being behind API.
402
    ret= drizzle_result_write(server->con, server->result, false);
2463.1.2 by Brian Aker
First pass, drizzle_create() no longer takes an argument. This means that we can now change drizzle_st without being concerned about ABI.
403
    DRIZZLE_RETURN_CHECK_VAL(ret, "drizzle_result_write", server->drizzle)
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
404
  }
405
406
  for (x= 0; x < field_count; x++)
407
  {
408
    if (fields[x] == NULL)
409
      sizes[x]= 0;
410
    else
411
      sizes[x]= strlen(fields[x]);
412
  }
413
414
  /* This is needed for MySQL and old Drizzle protocol. */
2463.1.5 by Brian Aker
Move result over to being behind API.
415
  drizzle_result_calc_row_size(server->result, (drizzle_field_t *)fields,
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
416
                               sizes);
417
2463.1.5 by Brian Aker
Move result over to being behind API.
418
  ret= drizzle_row_write(server->result);
2463.1.2 by Brian Aker
First pass, drizzle_create() no longer takes an argument. This means that we can now change drizzle_st without being concerned about ABI.
419
  DRIZZLE_RETURN_CHECK_VAL(ret, "drizzle_row_write", server->drizzle)
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
420
421
  for (x= 0; x < field_count; x++)
422
  {
2463.1.5 by Brian Aker
Move result over to being behind API.
423
    ret= drizzle_field_write(server->result, (drizzle_field_t)fields[x],
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
424
                             sizes[x], sizes[x]);
2463.1.2 by Brian Aker
First pass, drizzle_create() no longer takes an argument. This means that we can now change drizzle_st without being concerned about ABI.
425
    DRIZZLE_RETURN_CHECK_VAL(ret, "drizzle_field_write", server->drizzle)
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
426
  }
427
428
  server->rows++;
429
1929.1.36 by Stewart Smith
one last sqlite_server stack fix
430
  free(sizes);
431
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
432
  return 0;
433
}
434
435
static drizzle_return_t send_version(sqlite_server *server)
436
{
437
  drizzle_return_t ret;
438
  drizzle_field_t fields[1];
439
  size_t sizes[1];
440
441
  fields[0]= (drizzle_field_t)SQLITE_SERVER_VERSION;
442
  sizes[0]= strlen(SQLITE_SERVER_VERSION);
443
2463.1.5 by Brian Aker
Move result over to being behind API.
444
  drizzle_result_set_column_count(server->result, 1);
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
445
2463.1.5 by Brian Aker
Move result over to being behind API.
446
  ret= drizzle_result_write(server->con, server->result, false);
2463.1.2 by Brian Aker
First pass, drizzle_create() no longer takes an argument. This means that we can now change drizzle_st without being concerned about ABI.
447
  DRIZZLE_RETURN_CHECK_VAL(ret, "drizzle_result_write", server->drizzle)
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
448
2463.1.5 by Brian Aker
Move result over to being behind API.
449
  if (drizzle_column_create(server->result, &(server->column)) == NULL)
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
450
  {
2463.1.2 by Brian Aker
First pass, drizzle_create() no longer takes an argument. This means that we can now change drizzle_st without being concerned about ABI.
451
    DRIZZLE_RETURN_CHECK_VAL(DRIZZLE_RETURN_MEMORY, "drizzle_column_create", server->drizzle)
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
452
  }
453
454
  drizzle_column_set_catalog(&(server->column), "sqlite");
455
  drizzle_column_set_db(&(server->column), "sqlite_db");
456
  drizzle_column_set_table(&(server->column), "sqlite_table");
457
  drizzle_column_set_orig_table(&(server->column), "sqlite_table");
458
  drizzle_column_set_charset(&(server->column), 8);
459
  drizzle_column_set_type(&(server->column), DRIZZLE_COLUMN_TYPE_VARCHAR);
460
  drizzle_column_set_size(&(server->column), (uint32_t)sizes[0]);
461
  drizzle_column_set_name(&(server->column), "version");
462
  drizzle_column_set_orig_name(&(server->column), "version");
463
2463.1.5 by Brian Aker
Move result over to being behind API.
464
  ret= drizzle_column_write(server->result, &(server->column));
2463.1.2 by Brian Aker
First pass, drizzle_create() no longer takes an argument. This means that we can now change drizzle_st without being concerned about ABI.
465
  DRIZZLE_RETURN_CHECK_VAL(ret, "drizzle_column_write", server->drizzle)
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
466
467
  drizzle_column_free(&(server->column));
468
2463.1.5 by Brian Aker
Move result over to being behind API.
469
  drizzle_result_set_eof(server->result, true);
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
470
2463.1.5 by Brian Aker
Move result over to being behind API.
471
  ret= drizzle_result_write(server->con, server->result, false);
2463.1.2 by Brian Aker
First pass, drizzle_create() no longer takes an argument. This means that we can now change drizzle_st without being concerned about ABI.
472
  DRIZZLE_RETURN_CHECK_VAL(ret, "drizzle_result_write", server->drizzle)
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
473
474
  /* This is needed for MySQL and old Drizzle protocol. */
2463.1.5 by Brian Aker
Move result over to being behind API.
475
  drizzle_result_calc_row_size(server->result, fields, sizes);
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
476
2463.1.5 by Brian Aker
Move result over to being behind API.
477
  ret= drizzle_row_write(server->result);
2463.1.2 by Brian Aker
First pass, drizzle_create() no longer takes an argument. This means that we can now change drizzle_st without being concerned about ABI.
478
  DRIZZLE_RETURN_CHECK_VAL(ret, "drizzle_row_write", server->drizzle)
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
479
2463.1.5 by Brian Aker
Move result over to being behind API.
480
  ret= drizzle_field_write(server->result, fields[0], sizes[0], sizes[0]);
2463.1.2 by Brian Aker
First pass, drizzle_create() no longer takes an argument. This means that we can now change drizzle_st without being concerned about ABI.
481
  DRIZZLE_RETURN_CHECK_VAL(ret, "drizzle_field_write", server->drizzle)
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
482
2463.1.5 by Brian Aker
Move result over to being behind API.
483
  ret= drizzle_result_write(server->con, server->result, true);
2463.1.2 by Brian Aker
First pass, drizzle_create() no longer takes an argument. This means that we can now change drizzle_st without being concerned about ABI.
484
  DRIZZLE_RETURN_CHECK_VAL(ret, "drizzle_result_write", server->drizzle)
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
485
486
  return DRIZZLE_RETURN_OK;
487
}
488
489
static void usage(char *name)
490
{
2463.1.2 by Brian Aker
First pass, drizzle_create() no longer takes an argument. This means that we can now change drizzle_st without being concerned about ABI.
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");
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
497
}