~drizzle-trunk/drizzle/development

382 by Monty Taylor
Removed bogus copyright headers.
1
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 *
390.1.4 by Monty Taylor
More copyright header file fixes.
4
 *  Copyright (C) 2008 Sun Microsystems, Inc.
382 by Monty Taylor
Removed bogus copyright headers.
5
 *
6
 *  This program is free software; you can redistribute it and/or modify
7
 *  it under the terms of the GNU General Public License as published by
390.1.4 by Monty Taylor
More copyright header file fixes.
8
 *  the Free Software Foundation; version 2 of the License.
382 by Monty Taylor
Removed bogus copyright headers.
9
 *
10
 *  This program is distributed in the hope that it will be useful,
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 *  GNU General Public License for more details.
14
 *
15
 *  You should have received a copy of the GNU General Public License
16
 *  along with this program; if not, write to the Free Software
17
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
 */
19
1 by brian
clean slate
20
/*
779.3.37 by Monty Taylor
Renmaed libdrizzle in the tree to libdrizzleclient to avoid namespace clashes
21
  This file is included by both libdrizzleclient.c (the DRIZZLE client C API)
206.3.1 by Patrick Galbraith
Most everything working with client rename
22
  and the drizzled server to connect to another DRIZZLE server.
1 by brian
clean slate
23
24
  The differences for the two cases are:
25
26
  - Things that only works for the client:
27
  - Trying to automaticly determinate user name if not supplied to
840.1.21 by Monty Taylor
ZOMG. Renamed all the rest of the stuff in libdrizzleclient to be drizzleclient_*. I love commandline perl.
28
    drizzleclient_connect()
1 by brian
clean slate
29
  - Support for reading local file with LOAD DATA LOCAL
30
  - SHARED memory handling
31
  - Prepared statements
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
32
1 by brian
clean slate
33
  - Things that only works for the server
34
  - Alarm handling on connect
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
35
1 by brian
clean slate
36
  In all other cases, the code should be idential for the client and
37
  server.
206.3.1 by Patrick Galbraith
Most everything working with client rename
38
*/
1 by brian
clean slate
39
779.3.37 by Monty Taylor
Renmaed libdrizzle in the tree to libdrizzleclient to avoid namespace clashes
40
41
#include "libdrizzle_priv.h"
42
#include "net_serv.h"
43
#include "pack.h"
44
#include "errmsg.h"
45
#include "drizzle_methods.h"
46
383.1.41 by Monty Taylor
Removed more mysys stuff.
47
#include <stdarg.h>
212.5.30 by Monty Taylor
Removed my_net.h. Pointless.
48
#include <sys/poll.h>
49
#include <sys/ioctl.h>
50
1 by brian
clean slate
51
#include <netdb.h>
52
53
/* Remove client convenience wrappers */
54
#undef max_allowed_packet
55
#undef net_buffer_length
56
57
58
#include <sys/stat.h>
59
#include <signal.h>
60
#include <time.h>
206.3.1 by Patrick Galbraith
Most everything working with client rename
61
#ifdef   HAVE_PWD_H
1 by brian
clean slate
62
#include <pwd.h>
63
#endif
94 by Brian Aker
DOS removal. DOS... hard to believe aye?
64
1 by brian
clean slate
65
#include <sys/socket.h>
66
#include <netinet/in.h>
67
#include <arpa/inet.h>
68
#include <netdb.h>
69
#ifdef HAVE_SELECT_H
70
#  include <select.h>
71
#endif
72
#ifdef HAVE_SYS_SELECT_H
73
#include <sys/select.h>
74
#endif
94 by Brian Aker
DOS removal. DOS... hard to believe aye?
75
77.1.57 by Monty Taylor
Removed dual-compile needs on client.c. Get the symbols now from libdrizzle.
76
#include <sys/un.h>
1 by brian
clean slate
77
78
#include <errno.h>
79
80
538 by Monty Taylor
Moved gettext.h into drizzled in anticipation of the new client lib.
81
#include <drizzled/gettext.h>
1 by brian
clean slate
82
968.2.13 by Monty Taylor
libdrizzleclient is no longer exported - it's now just a temporary conv. lib to be deleted. So we convert it to C++. Now, we have no pure-C code that we're responsible.
83
using namespace std;
236.3.2 by Andrey Hristov
Move read_user_name from libdrizzle.c to client.c, where it is only used.
84
85
#if defined(HAVE_GETPWUID) && defined(NO_GETPWUID_DECL)
86
struct passwd *getpwuid(uid_t);
87
char* getlogin(void);
88
#endif
89
1 by brian
clean slate
90
91
/*****************************************************************************
92
  Read a packet from server. Give error message if socket was down
93
  or packet is an error message
94
*****************************************************************************/
971.3.3 by Eric Day
Cleaning up drizzleclient from drizzled.cc.
95
96
/* I'm not sure if this is even used anymore, but now that libdrizzleclient is
97
   server only, this is safe to set here. */
971.3.9 by Eric Day
Removed dependency for hex convert, fixed a few Protocol class issues for Solaris.
98
extern "C" {
99
  bool safe_read_error_impl(NET *net)
100
  {
101
    if (net->vio)
102
      return drizzleclient_vio_was_interrupted(net->vio);
103
    return false;
104
  }
971.3.3 by Eric Day
Cleaning up drizzleclient from drizzled.cc.
105
}
106
107
safe_read_error_hook_func safe_read_error_hook= safe_read_error_impl;
1 by brian
clean slate
108
840.1.20 by Monty Taylor
Renamed non-prefixed things from libdrizzleclient to drizzleclient.
109
uint32_t drizzleclient_cli_safe_read(DRIZZLE *drizzle)
1 by brian
clean slate
110
{
206.3.1 by Patrick Galbraith
Most everything working with client rename
111
  NET *net= &drizzle->net;
294 by Brian Aker
libdrizzle has ulong removed.
112
  uint32_t len=0;
1 by brian
clean slate
113
114
  if (net->vio != 0)
840.1.17 by Monty Taylor
Renamed my_net_* to drizzleclient_net_* to help with namespace issues.
115
    len=drizzleclient_net_read(net);
1 by brian
clean slate
116
117
  if (len == packet_error || len == 0)
118
  {
722.2.12 by Monty Taylor
Removed assert(0); Added function ptr in libdrizzle.
119
    if (safe_read_error_hook != NULL)
120
      if (safe_read_error_hook(net))
121
        return (packet_error);
840.1.21 by Monty Taylor
ZOMG. Renamed all the rest of the stuff in libdrizzleclient to be drizzleclient_*. I love commandline perl.
122
    drizzleclient_disconnect(drizzle);
123
    drizzleclient_set_error(drizzle, net->last_errno == CR_NET_PACKET_TOO_LARGE ?
383.1.42 by Monty Taylor
Removed mysys and mystrings from libdrizzle. All gone.
124
                      CR_NET_PACKET_TOO_LARGE : CR_SERVER_LOST,
840.1.20 by Monty Taylor
Renamed non-prefixed things from libdrizzleclient to drizzleclient.
125
                      drizzleclient_sqlstate_get_unknown());
1 by brian
clean slate
126
    return (packet_error);
127
  }
128
  if (net->read_pos[0] == 255)
129
  {
130
    if (len > 3)
131
    {
132
      char *pos=(char*) net->read_pos+1;
133
      net->last_errno=uint2korr(pos);
134
      pos+=2;
135
      len-=2;
264.2.4 by Andrey Hristov
Remove support for the old, pre-4.1, protocol
136
      if (pos[0] == '#')
1 by brian
clean slate
137
      {
520.6.1 by Monty Taylor
Removed common.h from common_includes.h.
138
        strncpy(net->sqlstate, pos+1, LIBDRIZZLE_SQLSTATE_LENGTH);
139
        pos+= LIBDRIZZLE_SQLSTATE_LENGTH+1;
1 by brian
clean slate
140
      }
141
      else
142
      {
143
        /*
144
          The SQL state hasn't been received -- it should be reset to HY000
145
          (unknown error sql state).
146
        */
147
840.1.20 by Monty Taylor
Renamed non-prefixed things from libdrizzleclient to drizzleclient.
148
        strcpy(net->sqlstate, drizzleclient_sqlstate_get_unknown());
1 by brian
clean slate
149
      }
150
395 by Brian Aker
Fixed uint/ushort issue in libdrizzle
151
      strncpy(net->last_error,(char*) pos, min((uint32_t) len,
240.1.4 by Toru Maesaka
str(mov|make) completely taken out from libdrizzle
152
              (uint32_t) sizeof(net->last_error)-1));
1 by brian
clean slate
153
    }
154
    else
840.1.21 by Monty Taylor
ZOMG. Renamed all the rest of the stuff in libdrizzleclient to be drizzleclient_*. I love commandline perl.
155
      drizzleclient_set_error(drizzle, CR_UNKNOWN_ERROR, drizzleclient_sqlstate_get_unknown());
1 by brian
clean slate
156
    /*
157
      Cover a protocol design error: error packet does not
158
      contain the server status. Therefore, the client has no way
159
      to find out whether there are more result sets of
160
      a multiple-result-set statement pending. Luckily, in 5.0 an
161
      error always aborts execution of a statement, wherever it is
162
      a multi-statement or a stored procedure, so it should be
163
      safe to unconditionally turn off the flag here.
164
    */
206.3.1 by Patrick Galbraith
Most everything working with client rename
165
    drizzle->server_status&= ~SERVER_MORE_RESULTS_EXISTS;
1 by brian
clean slate
166
167
    return(packet_error);
168
  }
169
  return len;
170
}
171
164 by Brian Aker
Commit cleanup of export types.
172
bool
840.1.20 by Monty Taylor
Renamed non-prefixed things from libdrizzleclient to drizzleclient.
173
drizzleclient_cli_advanced_command(DRIZZLE *drizzle, enum enum_server_command command,
206.3.1 by Patrick Galbraith
Most everything working with client rename
174
         const unsigned char *header, uint32_t header_length,
175
         const unsigned char *arg, uint32_t arg_length, bool skip_check)
1 by brian
clean slate
176
{
206.3.1 by Patrick Galbraith
Most everything working with client rename
177
  NET *net= &drizzle->net;
277 by Brian Aker
Cleanup of my_bool from extra and libdrizzle
178
  bool result= 1;
179
  bool stmt_skip= false;
1 by brian
clean slate
180
206.3.1 by Patrick Galbraith
Most everything working with client rename
181
  if (drizzle->net.vio == 0)
182
  {            /* Do reconnect if possible */
840.1.21 by Monty Taylor
ZOMG. Renamed all the rest of the stuff in libdrizzleclient to be drizzleclient_*. I love commandline perl.
183
    if (drizzleclient_reconnect(drizzle) || stmt_skip)
51.3.5 by Jay Pipes
Merged in from trunk.
184
      return(1);
1 by brian
clean slate
185
  }
206.3.1 by Patrick Galbraith
Most everything working with client rename
186
  if (drizzle->status != DRIZZLE_STATUS_READY ||
187
      drizzle->server_status & SERVER_MORE_RESULTS_EXISTS)
1 by brian
clean slate
188
  {
840.1.21 by Monty Taylor
ZOMG. Renamed all the rest of the stuff in libdrizzleclient to be drizzleclient_*. I love commandline perl.
189
    drizzleclient_set_error(drizzle, CR_COMMANDS_OUT_OF_SYNC,
840.1.20 by Monty Taylor
Renamed non-prefixed things from libdrizzleclient to drizzleclient.
190
                      drizzleclient_sqlstate_get_unknown());
51.3.5 by Jay Pipes
Merged in from trunk.
191
    return(1);
1 by brian
clean slate
192
  }
193
840.1.20 by Monty Taylor
Renamed non-prefixed things from libdrizzleclient to drizzleclient.
194
  drizzleclient_drizzleclient_net_clear_error(net);
206.3.1 by Patrick Galbraith
Most everything working with client rename
195
  drizzle->info=0;
196
  drizzle->affected_rows= ~(uint64_t) 0;
1 by brian
clean slate
197
  /*
198
    We don't want to clear the protocol buffer on COM_QUIT, because if
199
    the previous command was a shutdown command, we may have the
200
    response for the COM_QUIT already in the communication buffer
201
  */
840.1.20 by Monty Taylor
Renamed non-prefixed things from libdrizzleclient to drizzleclient.
202
  drizzleclient_net_clear(&drizzle->net, (command != COM_QUIT));
1 by brian
clean slate
203
840.1.20 by Monty Taylor
Renamed non-prefixed things from libdrizzleclient to drizzleclient.
204
  if (drizzleclient_net_write_command(net,(unsigned char) command, header, header_length,
206.3.1 by Patrick Galbraith
Most everything working with client rename
205
      arg, arg_length))
1 by brian
clean slate
206
  {
383.1.42 by Monty Taylor
Removed mysys and mystrings from libdrizzle. All gone.
207
    if (net->last_errno == CR_NET_PACKET_TOO_LARGE)
1 by brian
clean slate
208
    {
840.1.21 by Monty Taylor
ZOMG. Renamed all the rest of the stuff in libdrizzleclient to be drizzleclient_*. I love commandline perl.
209
      drizzleclient_set_error(drizzle, CR_NET_PACKET_TOO_LARGE, drizzleclient_sqlstate_get_unknown());
1 by brian
clean slate
210
      goto end;
211
    }
840.1.21 by Monty Taylor
ZOMG. Renamed all the rest of the stuff in libdrizzleclient to be drizzleclient_*. I love commandline perl.
212
    drizzleclient_disconnect(drizzle);
213
    if (drizzleclient_reconnect(drizzle) || stmt_skip)
1 by brian
clean slate
214
      goto end;
840.1.20 by Monty Taylor
Renamed non-prefixed things from libdrizzleclient to drizzleclient.
215
    if (drizzleclient_net_write_command(net,(unsigned char) command, header, header_length,
206.3.1 by Patrick Galbraith
Most everything working with client rename
216
        arg, arg_length))
1 by brian
clean slate
217
    {
840.1.21 by Monty Taylor
ZOMG. Renamed all the rest of the stuff in libdrizzleclient to be drizzleclient_*. I love commandline perl.
218
      drizzleclient_set_error(drizzle, CR_SERVER_GONE_ERROR, drizzleclient_sqlstate_get_unknown());
1 by brian
clean slate
219
      goto end;
220
    }
221
  }
222
  result=0;
223
  if (!skip_check)
840.1.20 by Monty Taylor
Renamed non-prefixed things from libdrizzleclient to drizzleclient.
224
    result= ((drizzle->packet_length=drizzleclient_cli_safe_read(drizzle)) == packet_error ?
206.3.1 by Patrick Galbraith
Most everything working with client rename
225
       1 : 0);
1 by brian
clean slate
226
end:
51.3.5 by Jay Pipes
Merged in from trunk.
227
  return(result);
1 by brian
clean slate
228
}
229
840.1.20 by Monty Taylor
Renamed non-prefixed things from libdrizzleclient to drizzleclient.
230
void drizzleclient_free_old_query(DRIZZLE *drizzle)
1 by brian
clean slate
231
{
206.3.1 by Patrick Galbraith
Most everything working with client rename
232
  if (drizzle->fields)
383.1.40 by Monty Taylor
More mysys removal from libdrizzle. Got rid of MEM_ROOT related stuff.
233
  {
234
    /* TODO - we need to de-alloc field storage */
235
    free(drizzle->fields->catalog);
236
    free(drizzle->fields->db);
237
    free(drizzle->fields->table);
238
    free(drizzle->fields->org_table);
239
    free(drizzle->fields->name);
240
    free(drizzle->fields->org_name);
241
    free(drizzle->fields->def);
242
    free(drizzle->fields);
243
244
  }
245
  /* init_alloc_root(&drizzle->field_alloc,8192,0); */ /* Assume rowlength < 8192 */
206.3.1 by Patrick Galbraith
Most everything working with client rename
246
  drizzle->fields= 0;
247
  drizzle->field_count= 0;      /* For API */
248
  drizzle->warning_count= 0;
249
  drizzle->info= 0;
51.3.5 by Jay Pipes
Merged in from trunk.
250
  return;
1 by brian
clean slate
251
}
252
253
254
255
256
287.3.3 by Monty Taylor
Removed STDCALL macros calls.
257
void
840.1.21 by Monty Taylor
ZOMG. Renamed all the rest of the stuff in libdrizzleclient to be drizzleclient_*. I love commandline perl.
258
drizzleclient_free_result(DRIZZLE_RES *result)
1 by brian
clean slate
259
{
260
  if (result)
261
  {
206.3.1 by Patrick Galbraith
Most everything working with client rename
262
    DRIZZLE *drizzle= result->handle;
263
    if (drizzle)
1 by brian
clean slate
264
    {
206.3.1 by Patrick Galbraith
Most everything working with client rename
265
      if (drizzle->unbuffered_fetch_owner == &result->unbuffered_fetch_cancelled)
266
        drizzle->unbuffered_fetch_owner= 0;
267
      if (drizzle->status == DRIZZLE_STATUS_USE_RESULT)
1 by brian
clean slate
268
      {
206.3.1 by Patrick Galbraith
Most everything working with client rename
269
        (*drizzle->methods->flush_use_result)(drizzle);
270
        drizzle->status=DRIZZLE_STATUS_READY;
271
        if (drizzle->unbuffered_fetch_owner)
272
          *drizzle->unbuffered_fetch_owner= true;
1 by brian
clean slate
273
      }
274
    }
840.1.20 by Monty Taylor
Renamed non-prefixed things from libdrizzleclient to drizzleclient.
275
    drizzleclient_free_rows(result->data);
383.1.40 by Monty Taylor
More mysys removal from libdrizzle. Got rid of MEM_ROOT related stuff.
276
    /* TODO: free result->fields */
1 by brian
clean slate
277
    if (result->row)
390.1.6 by Monty Taylor
Oh dear god the changes. The changes. I'd tell you what they are, but I'd just be making stuff up. Suffice it to day it's mostly all around splitting files in libdrizzle into different files and removing interdepends. And whatever else I happened to see...
278
      free((unsigned char*) result->row);
279
    free((unsigned char*) result);
280
  }
281
}
282
283
284
1 by brian
clean slate
285
286
/* Read all rows (fields or data) from server */
287
840.1.20 by Monty Taylor
Renamed non-prefixed things from libdrizzleclient to drizzleclient.
288
DRIZZLE_DATA *drizzleclient_cli_read_rows(DRIZZLE *drizzle, DRIZZLE_FIELD *DRIZZLE_FIELDs, uint32_t fields)
1 by brian
clean slate
289
{
294 by Brian Aker
libdrizzle has ulong removed.
290
  uint32_t  field;
291
  uint32_t pkt_len;
292
  uint32_t len;
293
  unsigned char *cp;
206.3.1 by Patrick Galbraith
Most everything working with client rename
294
  char  *to, *end_to;
295
  DRIZZLE_DATA *result;
296
  DRIZZLE_ROWS **prev_ptr,*cur;
297
  NET *net = &drizzle->net;
1 by brian
clean slate
298
840.1.20 by Monty Taylor
Renamed non-prefixed things from libdrizzleclient to drizzleclient.
299
  if ((pkt_len= drizzleclient_cli_safe_read(drizzle)) == packet_error)
51.3.5 by Jay Pipes
Merged in from trunk.
300
    return(0);
383.1.41 by Monty Taylor
Removed more mysys stuff.
301
  if (!(result=(DRIZZLE_DATA*) malloc(sizeof(DRIZZLE_DATA))))
1 by brian
clean slate
302
  {
840.1.21 by Monty Taylor
ZOMG. Renamed all the rest of the stuff in libdrizzleclient to be drizzleclient_*. I love commandline perl.
303
    drizzleclient_set_error(drizzle, CR_OUT_OF_MEMORY,
840.1.20 by Monty Taylor
Renamed non-prefixed things from libdrizzleclient to drizzleclient.
304
                      drizzleclient_sqlstate_get_unknown());
51.3.5 by Jay Pipes
Merged in from trunk.
305
    return(0);
1 by brian
clean slate
306
  }
383.1.41 by Monty Taylor
Removed more mysys stuff.
307
  memset(result, 0, sizeof(DRIZZLE_DATA));
1 by brian
clean slate
308
  prev_ptr= &result->data;
309
  result->rows=0;
310
  result->fields=fields;
311
312
  /*
264.2.4 by Andrey Hristov
Remove support for the old, pre-4.1, protocol
313
    The last EOF packet is either a 254 (0xFE) character followed by 1-7 status bytes.
1 by brian
clean slate
314
315
    This doesn't conflict with normal usage of 254 which stands for a
840.1.20 by Monty Taylor
Renamed non-prefixed things from libdrizzleclient to drizzleclient.
316
    string where the length of the string is 8 bytes. (see drizzleclient_net_field_length())
1 by brian
clean slate
317
  */
318
264.2.4 by Andrey Hristov
Remove support for the old, pre-4.1, protocol
319
  while (*(cp=net->read_pos) != DRIZZLE_PROTOCOL_NO_MORE_DATA || pkt_len >= 8)
1 by brian
clean slate
320
  {
321
    result->rows++;
383.1.40 by Monty Taylor
More mysys removal from libdrizzle. Got rid of MEM_ROOT related stuff.
322
    if (!(cur= (DRIZZLE_ROWS*) malloc(sizeof(DRIZZLE_ROWS))) ||
323
        !(cur->data= ((DRIZZLE_ROW) malloc((fields+1)*sizeof(char *)+pkt_len))))
1 by brian
clean slate
324
    {
840.1.20 by Monty Taylor
Renamed non-prefixed things from libdrizzleclient to drizzleclient.
325
      drizzleclient_free_rows(result);
840.1.21 by Monty Taylor
ZOMG. Renamed all the rest of the stuff in libdrizzleclient to be drizzleclient_*. I love commandline perl.
326
      drizzleclient_set_error(drizzle, CR_OUT_OF_MEMORY, drizzleclient_sqlstate_get_unknown());
51.3.5 by Jay Pipes
Merged in from trunk.
327
      return(0);
1 by brian
clean slate
328
    }
329
    *prev_ptr=cur;
330
    prev_ptr= &cur->next;
331
    to= (char*) (cur->data+fields+1);
332
    end_to=to+pkt_len-1;
333
    for (field=0 ; field < fields ; field++)
334
    {
840.1.20 by Monty Taylor
Renamed non-prefixed things from libdrizzleclient to drizzleclient.
335
      if ((len= drizzleclient_net_field_length(&cp)) == NULL_LENGTH)
206.3.1 by Patrick Galbraith
Most everything working with client rename
336
      {            /* null field */
294 by Brian Aker
libdrizzle has ulong removed.
337
        cur->data[field] = 0;
1 by brian
clean slate
338
      }
339
      else
340
      {
294 by Brian Aker
libdrizzle has ulong removed.
341
        cur->data[field] = to;
342
        if (len > (uint32_t) (end_to - to))
1 by brian
clean slate
343
        {
840.1.20 by Monty Taylor
Renamed non-prefixed things from libdrizzleclient to drizzleclient.
344
          drizzleclient_free_rows(result);
840.1.21 by Monty Taylor
ZOMG. Renamed all the rest of the stuff in libdrizzleclient to be drizzleclient_*. I love commandline perl.
345
          drizzleclient_set_error(drizzle, CR_MALFORMED_PACKET,
840.1.20 by Monty Taylor
Renamed non-prefixed things from libdrizzleclient to drizzleclient.
346
                            drizzleclient_sqlstate_get_unknown());
51.3.5 by Jay Pipes
Merged in from trunk.
347
          return(0);
1 by brian
clean slate
348
        }
294 by Brian Aker
libdrizzle has ulong removed.
349
        memcpy(to, cp, len);
350
        to[len]=0;
351
        to+=len+1;
352
        cp+=len;
353
        if (DRIZZLE_FIELDs)
354
        {
355
          if (DRIZZLE_FIELDs[field].max_length < len)
356
            DRIZZLE_FIELDs[field].max_length=len;
357
        }
1 by brian
clean slate
358
      }
359
    }
206.3.1 by Patrick Galbraith
Most everything working with client rename
360
    cur->data[field]=to;      /* End of last field */
840.1.20 by Monty Taylor
Renamed non-prefixed things from libdrizzleclient to drizzleclient.
361
    if ((pkt_len=drizzleclient_cli_safe_read(drizzle)) == packet_error)
1 by brian
clean slate
362
    {
840.1.20 by Monty Taylor
Renamed non-prefixed things from libdrizzleclient to drizzleclient.
363
      drizzleclient_free_rows(result);
51.3.5 by Jay Pipes
Merged in from trunk.
364
      return(0);
1 by brian
clean slate
365
    }
366
  }
206.3.1 by Patrick Galbraith
Most everything working with client rename
367
  *prev_ptr=0;          /* last pointer is null */
368
  if (pkt_len > 1)        /* DRIZZLE 4.1 protocol */
1 by brian
clean slate
369
  {
206.3.1 by Patrick Galbraith
Most everything working with client rename
370
    drizzle->warning_count= uint2korr(cp+1);
371
    drizzle->server_status= uint2korr(cp+3);
1 by brian
clean slate
372
  }
51.3.5 by Jay Pipes
Merged in from trunk.
373
  return(result);
1 by brian
clean slate
374
}
375
376
/*
377
  Read one row. Uses packet buffer as storage for fields.
378
  When next packet is read, the previous field values are destroyed
379
*/
380
381
164 by Brian Aker
Commit cleanup of export types.
382
static int32_t
206.3.1 by Patrick Galbraith
Most everything working with client rename
383
read_one_row(DRIZZLE *drizzle, uint32_t fields, DRIZZLE_ROW row, uint32_t *lengths)
1 by brian
clean slate
384
{
395 by Brian Aker
Fixed uint/ushort issue in libdrizzle
385
  uint32_t field;
294 by Brian Aker
libdrizzle has ulong removed.
386
  uint32_t pkt_len,len;
390.1.6 by Monty Taylor
Oh dear god the changes. The changes. I'd tell you what they are, but I'd just be making stuff up. Suffice it to day it's mostly all around splitting files in libdrizzle into different files and removing interdepends. And whatever else I happened to see...
387
  unsigned char *pos, *prev_pos, *end_pos;
206.3.1 by Patrick Galbraith
Most everything working with client rename
388
  NET *net= &drizzle->net;
1 by brian
clean slate
389
840.1.20 by Monty Taylor
Renamed non-prefixed things from libdrizzleclient to drizzleclient.
390
  if ((pkt_len=drizzleclient_cli_safe_read(drizzle)) == packet_error)
1 by brian
clean slate
391
    return -1;
264.2.4 by Andrey Hristov
Remove support for the old, pre-4.1, protocol
392
  if (pkt_len <= 8 && net->read_pos[0] == DRIZZLE_PROTOCOL_NO_MORE_DATA)
1 by brian
clean slate
393
  {
206.3.1 by Patrick Galbraith
Most everything working with client rename
394
    if (pkt_len > 1)        /* DRIZZLE 4.1 protocol */
1 by brian
clean slate
395
    {
206.3.1 by Patrick Galbraith
Most everything working with client rename
396
      drizzle->warning_count= uint2korr(net->read_pos+1);
397
      drizzle->server_status= uint2korr(net->read_pos+3);
1 by brian
clean slate
398
    }
206.3.1 by Patrick Galbraith
Most everything working with client rename
399
    return 1;        /* End of data */
1 by brian
clean slate
400
  }
206.3.1 by Patrick Galbraith
Most everything working with client rename
401
  prev_pos= 0;        /* allowed to write at packet[-1] */
1 by brian
clean slate
402
  pos=net->read_pos;
403
  end_pos=pos+pkt_len;
404
  for (field=0 ; field < fields ; field++)
405
  {
840.1.20 by Monty Taylor
Renamed non-prefixed things from libdrizzleclient to drizzleclient.
406
    if ((len= drizzleclient_net_field_length(&pos)) == NULL_LENGTH)
206.3.1 by Patrick Galbraith
Most everything working with client rename
407
    {            /* null field */
1 by brian
clean slate
408
      row[field] = 0;
409
      *lengths++=0;
410
    }
411
    else
412
    {
294 by Brian Aker
libdrizzle has ulong removed.
413
      if (len > (uint32_t) (end_pos - pos))
1 by brian
clean slate
414
      {
840.1.21 by Monty Taylor
ZOMG. Renamed all the rest of the stuff in libdrizzleclient to be drizzleclient_*. I love commandline perl.
415
        drizzleclient_set_error(drizzle, CR_UNKNOWN_ERROR,
840.1.20 by Monty Taylor
Renamed non-prefixed things from libdrizzleclient to drizzleclient.
416
                          drizzleclient_sqlstate_get_unknown());
1 by brian
clean slate
417
        return -1;
418
      }
419
      row[field] = (char*) pos;
420
      pos+=len;
421
      *lengths++=len;
422
    }
423
    if (prev_pos)
206.3.1 by Patrick Galbraith
Most everything working with client rename
424
      *prev_pos=0;        /* Terminate prev field */
1 by brian
clean slate
425
    prev_pos=pos;
426
  }
206.3.1 by Patrick Galbraith
Most everything working with client rename
427
  row[field]=(char*) prev_pos+1;    /* End of last field */
428
  *prev_pos=0;          /* Terminate last field */
1 by brian
clean slate
429
  return 0;
430
}
431
432
433
/**************************************************************************
434
  Return next row of the query results
435
**************************************************************************/
436
287.3.3 by Monty Taylor
Removed STDCALL macros calls.
437
DRIZZLE_ROW
840.1.21 by Monty Taylor
ZOMG. Renamed all the rest of the stuff in libdrizzleclient to be drizzleclient_*. I love commandline perl.
438
drizzleclient_fetch_row(DRIZZLE_RES *res)
1 by brian
clean slate
439
{
440
  if (!res->data)
206.3.1 by Patrick Galbraith
Most everything working with client rename
441
  {            /* Unbufferred fetch */
1 by brian
clean slate
442
    if (!res->eof)
443
    {
206.3.1 by Patrick Galbraith
Most everything working with client rename
444
      DRIZZLE *drizzle= res->handle;
445
      if (drizzle->status != DRIZZLE_STATUS_USE_RESULT)
1 by brian
clean slate
446
      {
840.1.21 by Monty Taylor
ZOMG. Renamed all the rest of the stuff in libdrizzleclient to be drizzleclient_*. I love commandline perl.
447
        drizzleclient_set_error(drizzle,
390.1.6 by Monty Taylor
Oh dear god the changes. The changes. I'd tell you what they are, but I'd just be making stuff up. Suffice it to day it's mostly all around splitting files in libdrizzle into different files and removing interdepends. And whatever else I happened to see...
448
                          res->unbuffered_fetch_cancelled ?
449
                          CR_FETCH_CANCELED : CR_COMMANDS_OUT_OF_SYNC,
840.1.20 by Monty Taylor
Renamed non-prefixed things from libdrizzleclient to drizzleclient.
450
                          drizzleclient_sqlstate_get_unknown());
1 by brian
clean slate
451
      }
206.3.1 by Patrick Galbraith
Most everything working with client rename
452
      else if (!(read_one_row(drizzle, res->field_count, res->row, res->lengths)))
1 by brian
clean slate
453
      {
206.3.1 by Patrick Galbraith
Most everything working with client rename
454
  res->row_count++;
455
  return(res->current_row=res->row);
1 by brian
clean slate
456
      }
457
      res->eof=1;
206.3.1 by Patrick Galbraith
Most everything working with client rename
458
      drizzle->status=DRIZZLE_STATUS_READY;
1 by brian
clean slate
459
      /*
460
        Reset only if owner points to us: there is a chance that somebody
206.3.1 by Patrick Galbraith
Most everything working with client rename
461
        started new query after drizzle_stmt_close():
1 by brian
clean slate
462
      */
206.3.1 by Patrick Galbraith
Most everything working with client rename
463
      if (drizzle->unbuffered_fetch_owner == &res->unbuffered_fetch_cancelled)
464
        drizzle->unbuffered_fetch_owner= 0;
840.1.21 by Monty Taylor
ZOMG. Renamed all the rest of the stuff in libdrizzleclient to be drizzleclient_*. I love commandline perl.
465
      /* Don't clear handle in drizzleclient_free_result */
1 by brian
clean slate
466
      res->handle=0;
467
    }
206.3.1 by Patrick Galbraith
Most everything working with client rename
468
    return((DRIZZLE_ROW) NULL);
1 by brian
clean slate
469
  }
470
  {
206.3.1 by Patrick Galbraith
Most everything working with client rename
471
    DRIZZLE_ROW tmp;
1 by brian
clean slate
472
    if (!res->data_cursor)
473
    {
206.3.1 by Patrick Galbraith
Most everything working with client rename
474
      return(res->current_row=(DRIZZLE_ROW) NULL);
1 by brian
clean slate
475
    }
476
    tmp = res->data_cursor->data;
477
    res->data_cursor = res->data_cursor->next;
51.3.5 by Jay Pipes
Merged in from trunk.
478
    return(res->current_row=tmp);
1 by brian
clean slate
479
  }
480
}
481
482
483
/**************************************************************************
484
  Get column lengths of the current row
840.1.21 by Monty Taylor
ZOMG. Renamed all the rest of the stuff in libdrizzleclient to be drizzleclient_*. I love commandline perl.
485
  If one uses drizzleclient_use_result, res->lengths contains the length information,
1 by brian
clean slate
486
  else the lengths are calculated from the offset between pointers.
487
**************************************************************************/
488
287.3.3 by Monty Taylor
Removed STDCALL macros calls.
489
uint32_t *
840.1.21 by Monty Taylor
ZOMG. Renamed all the rest of the stuff in libdrizzleclient to be drizzleclient_*. I love commandline perl.
490
drizzleclient_fetch_lengths(DRIZZLE_RES *res)
1 by brian
clean slate
491
{
206.3.1 by Patrick Galbraith
Most everything working with client rename
492
  DRIZZLE_ROW column;
1 by brian
clean slate
493
494
  if (!(column=res->current_row))
206.3.1 by Patrick Galbraith
Most everything working with client rename
495
    return 0;          /* Something is wrong */
1 by brian
clean slate
496
  if (res->data)
497
    (*res->methods->fetch_lengths)(res->lengths, column, res->field_count);
498
  return res->lengths;
499
}
500
501
287.3.3 by Monty Taylor
Removed STDCALL macros calls.
502
int
840.1.21 by Monty Taylor
ZOMG. Renamed all the rest of the stuff in libdrizzleclient to be drizzleclient_*. I love commandline perl.
503
drizzleclient_options(DRIZZLE *drizzle,enum drizzle_option option, const void *arg)
1 by brian
clean slate
504
{
505
  switch (option) {
206.3.1 by Patrick Galbraith
Most everything working with client rename
506
  case DRIZZLE_OPT_CONNECT_TIMEOUT:
395 by Brian Aker
Fixed uint/ushort issue in libdrizzle
507
    drizzle->options.connect_timeout= *(uint32_t*) arg;
206.3.1 by Patrick Galbraith
Most everything working with client rename
508
    break;
509
  case DRIZZLE_OPT_READ_TIMEOUT:
395 by Brian Aker
Fixed uint/ushort issue in libdrizzle
510
    drizzle->options.read_timeout= *(uint32_t*) arg;
206.3.1 by Patrick Galbraith
Most everything working with client rename
511
    break;
512
  case DRIZZLE_OPT_WRITE_TIMEOUT:
395 by Brian Aker
Fixed uint/ushort issue in libdrizzle
513
    drizzle->options.write_timeout= *(uint32_t*) arg;
206.3.1 by Patrick Galbraith
Most everything working with client rename
514
    break;
515
  case DRIZZLE_OPT_COMPRESS:
516
    drizzle->options.compress= 1;      /* Remember for connect */
517
    drizzle->options.client_flag|= CLIENT_COMPRESS;
518
    break;
519
  case DRIZZLE_READ_DEFAULT_FILE:
383.1.41 by Monty Taylor
Removed more mysys stuff.
520
    if (drizzle->options.my_cnf_file != NULL)
521
      free(drizzle->options.my_cnf_file);
968.2.13 by Monty Taylor
libdrizzleclient is no longer exported - it's now just a temporary conv. lib to be deleted. So we convert it to C++. Now, we have no pure-C code that we're responsible.
522
    drizzle->options.my_cnf_file=strdup((char *)arg);
206.3.1 by Patrick Galbraith
Most everything working with client rename
523
    break;
524
  case DRIZZLE_READ_DEFAULT_GROUP:
383.1.41 by Monty Taylor
Removed more mysys stuff.
525
    if (drizzle->options.my_cnf_group != NULL)
526
      free(drizzle->options.my_cnf_group);
968.2.13 by Monty Taylor
libdrizzleclient is no longer exported - it's now just a temporary conv. lib to be deleted. So we convert it to C++. Now, we have no pure-C code that we're responsible.
527
    drizzle->options.my_cnf_group=strdup((char *)arg);
206.3.1 by Patrick Galbraith
Most everything working with client rename
528
    break;
529
  case DRIZZLE_OPT_PROTOCOL:
530
    break;
531
  case DRIZZLE_OPT_USE_REMOTE_CONNECTION:
532
  case DRIZZLE_OPT_GUESS_CONNECTION:
533
    drizzle->options.methods_to_use= option;
534
    break;
535
  case DRIZZLE_SET_CLIENT_IP:
968.2.13 by Monty Taylor
libdrizzleclient is no longer exported - it's now just a temporary conv. lib to be deleted. So we convert it to C++. Now, we have no pure-C code that we're responsible.
536
    drizzle->options.client_ip= strdup((char *)arg);
206.3.1 by Patrick Galbraith
Most everything working with client rename
537
    break;
538
  case DRIZZLE_SECURE_AUTH:
266.7.7 by Andy Lester
const happiness
539
    drizzle->options.secure_auth= *(const bool *) arg;
206.3.1 by Patrick Galbraith
Most everything working with client rename
540
    break;
541
  case DRIZZLE_REPORT_DATA_TRUNCATION:
390.1.6 by Monty Taylor
Oh dear god the changes. The changes. I'd tell you what they are, but I'd just be making stuff up. Suffice it to day it's mostly all around splitting files in libdrizzle into different files and removing interdepends. And whatever else I happened to see...
542
    drizzle->options.report_data_truncation= (*(const bool *) arg) ? 1 : 0;
206.3.1 by Patrick Galbraith
Most everything working with client rename
543
    break;
544
  case DRIZZLE_OPT_RECONNECT:
266.7.7 by Andy Lester
const happiness
545
    drizzle->reconnect= *(const bool *) arg;
206.3.1 by Patrick Galbraith
Most everything working with client rename
546
    break;
547
  case DRIZZLE_OPT_SSL_VERIFY_SERVER_CERT:
266.7.7 by Andy Lester
const happiness
548
    if (*(const bool*) arg)
206.3.1 by Patrick Galbraith
Most everything working with client rename
549
      drizzle->options.client_flag|= CLIENT_SSL_VERIFY_SERVER_CERT;
1 by brian
clean slate
550
    else
206.3.1 by Patrick Galbraith
Most everything working with client rename
551
      drizzle->options.client_flag&= ~CLIENT_SSL_VERIFY_SERVER_CERT;
1 by brian
clean slate
552
    break;
553
  default:
51.3.5 by Jay Pipes
Merged in from trunk.
554
    return(1);
1 by brian
clean slate
555
  }
51.3.5 by Jay Pipes
Merged in from trunk.
556
  return(0);
1 by brian
clean slate
557
}
558
559
560
/****************************************************************************
206.3.1 by Patrick Galbraith
Most everything working with client rename
561
  Functions to get information from the DRIZZLE structure
1 by brian
clean slate
562
  These are functions to make shared libraries more usable.
563
****************************************************************************/
564
206.3.1 by Patrick Galbraith
Most everything working with client rename
565
/* DRIZZLE_RES */
840.1.21 by Monty Taylor
ZOMG. Renamed all the rest of the stuff in libdrizzleclient to be drizzleclient_*. I love commandline perl.
566
uint64_t drizzleclient_num_rows(const DRIZZLE_RES *res)
1 by brian
clean slate
567
{
568
  return res->row_count;
569
}
570
840.1.21 by Monty Taylor
ZOMG. Renamed all the rest of the stuff in libdrizzleclient to be drizzleclient_*. I love commandline perl.
571
unsigned int drizzleclient_num_fields(const DRIZZLE_RES *res)
1 by brian
clean slate
572
{
573
  return res->field_count;
574
}
575
576
577
/*
578
  Get version number for server in a form easy to test on
579
580
  SYNOPSIS
840.1.21 by Monty Taylor
ZOMG. Renamed all the rest of the stuff in libdrizzleclient to be drizzleclient_*. I love commandline perl.
581
    drizzleclient_get_server_version()
206.3.1 by Patrick Galbraith
Most everything working with client rename
582
    drizzle Connection
1 by brian
clean slate
583
584
  EXAMPLE
585
    4.1.0-alfa ->  40100
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
586
1 by brian
clean slate
587
  NOTES
588
    We will ensure that a newer server always has a bigger number.
589
590
  RETURN
591
   Signed number > 323000
592
*/
593
287.3.3 by Monty Taylor
Removed STDCALL macros calls.
594
uint32_t
840.1.21 by Monty Taylor
ZOMG. Renamed all the rest of the stuff in libdrizzleclient to be drizzleclient_*. I love commandline perl.
595
drizzleclient_get_server_version(const DRIZZLE *drizzle)
1 by brian
clean slate
596
{
395 by Brian Aker
Fixed uint/ushort issue in libdrizzle
597
  uint32_t major, minor, version;
206.3.1 by Patrick Galbraith
Most everything working with client rename
598
  char *pos= drizzle->server_version, *end_pos;
395 by Brian Aker
Fixed uint/ushort issue in libdrizzle
599
  major=   (uint32_t) strtoul(pos, &end_pos, 10);  pos=end_pos+1;
600
  minor=   (uint32_t) strtoul(pos, &end_pos, 10);  pos=end_pos+1;
601
  version= (uint32_t) strtoul(pos, &end_pos, 10);
294 by Brian Aker
libdrizzle has ulong removed.
602
  return (uint32_t) major*10000L+(uint32_t) (minor*100+version);
1 by brian
clean slate
603
}
604