~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
/*
206.3.1 by Patrick Galbraith
Most everything working with client rename
21
  This file is included by both libdrizzle.c (the DRIZZLE client C API)
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
206.3.1 by Patrick Galbraith
Most everything working with client rename
28
    drizzle_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
383.1.41 by Monty Taylor
Removed more mysys stuff.
40
#include <stdarg.h>
41
390.1.5 by Monty Taylor
Moved more functions into drizzle.c as part of the split of code.
42
#include <libdrizzle/libdrizzle.h>
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...
43
#include <libdrizzle/net_serv.h>
390.1.5 by Monty Taylor
Moved more functions into drizzle.c as part of the split of code.
44
#include "libdrizzle_priv.h"
542 by Monty Taylor
Cleaned up the last commit.
45
#include <libdrizzle/pack.h>
1 by brian
clean slate
46
212.5.30 by Monty Taylor
Removed my_net.h. Pointless.
47
#include <sys/poll.h>
48
#include <sys/ioctl.h>
49
1 by brian
clean slate
50
#include <netdb.h>
51
52
/* Remove client convenience wrappers */
53
#undef max_allowed_packet
54
#undef net_buffer_length
55
383.1.42 by Monty Taylor
Removed mysys and mystrings from libdrizzle. All gone.
56
#include <libdrizzle/errmsg.h>
1 by brian
clean slate
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>
383.1.49 by Monty Taylor
Got rid of client_settings.h
82
#include "local_infile.h"
83
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...
84
85
86
87
88
1 by brian
clean slate
89
236.3.2 by Andrey Hristov
Move read_user_name from libdrizzle.c to client.c, where it is only used.
90
91
#if defined(HAVE_GETPWUID) && defined(NO_GETPWUID_DECL)
92
struct passwd *getpwuid(uid_t);
93
char* getlogin(void);
94
#endif
95
1 by brian
clean slate
96
97
98
/*****************************************************************************
99
  Read a packet from server. Give error message if socket was down
100
  or packet is an error message
101
*****************************************************************************/
102
206.3.1 by Patrick Galbraith
Most everything working with client rename
103
uint32_t cli_safe_read(DRIZZLE *drizzle)
1 by brian
clean slate
104
{
206.3.1 by Patrick Galbraith
Most everything working with client rename
105
  NET *net= &drizzle->net;
294 by Brian Aker
libdrizzle has ulong removed.
106
  uint32_t len=0;
1 by brian
clean slate
107
108
  if (net->vio != 0)
109
    len=my_net_read(net);
110
111
  if (len == packet_error || len == 0)
112
  {
319.1.1 by Grant Limberg
renamed all instances of MYSQL_ to DRIZZLE_
113
#ifdef DRIZZLE_SERVER
1 by brian
clean slate
114
    if (net->vio && vio_was_interrupted(net->vio))
115
      return (packet_error);
206.3.1 by Patrick Galbraith
Most everything working with client rename
116
#endif /*DRIZZLE_SERVER*/
390.1.5 by Monty Taylor
Moved more functions into drizzle.c as part of the split of code.
117
    drizzle_disconnect(drizzle);
118
    drizzle_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.
119
                      CR_NET_PACKET_TOO_LARGE : CR_SERVER_LOST,
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...
120
                      sqlstate_get_unknown());
1 by brian
clean slate
121
    return (packet_error);
122
  }
123
  if (net->read_pos[0] == 255)
124
  {
125
    if (len > 3)
126
    {
127
      char *pos=(char*) net->read_pos+1;
128
      net->last_errno=uint2korr(pos);
129
      pos+=2;
130
      len-=2;
264.2.4 by Andrey Hristov
Remove support for the old, pre-4.1, protocol
131
      if (pos[0] == '#')
1 by brian
clean slate
132
      {
520.6.1 by Monty Taylor
Removed common.h from common_includes.h.
133
        strncpy(net->sqlstate, pos+1, LIBDRIZZLE_SQLSTATE_LENGTH);
134
        pos+= LIBDRIZZLE_SQLSTATE_LENGTH+1;
1 by brian
clean slate
135
      }
136
      else
137
      {
138
        /*
139
          The SQL state hasn't been received -- it should be reset to HY000
140
          (unknown error sql state).
141
        */
142
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...
143
        strcpy(net->sqlstate, sqlstate_get_unknown());
1 by brian
clean slate
144
      }
145
395 by Brian Aker
Fixed uint/ushort issue in libdrizzle
146
      strncpy(net->last_error,(char*) pos, min((uint32_t) len,
240.1.4 by Toru Maesaka
str(mov|make) completely taken out from libdrizzle
147
              (uint32_t) sizeof(net->last_error)-1));
1 by brian
clean slate
148
    }
149
    else
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...
150
      drizzle_set_error(drizzle, CR_UNKNOWN_ERROR, sqlstate_get_unknown());
1 by brian
clean slate
151
    /*
152
      Cover a protocol design error: error packet does not
153
      contain the server status. Therefore, the client has no way
154
      to find out whether there are more result sets of
155
      a multiple-result-set statement pending. Luckily, in 5.0 an
156
      error always aborts execution of a statement, wherever it is
157
      a multi-statement or a stored procedure, so it should be
158
      safe to unconditionally turn off the flag here.
159
    */
206.3.1 by Patrick Galbraith
Most everything working with client rename
160
    drizzle->server_status&= ~SERVER_MORE_RESULTS_EXISTS;
1 by brian
clean slate
161
162
    return(packet_error);
163
  }
164
  return len;
165
}
166
164 by Brian Aker
Commit cleanup of export types.
167
bool
206.3.1 by Patrick Galbraith
Most everything working with client rename
168
cli_advanced_command(DRIZZLE *drizzle, enum enum_server_command command,
169
         const unsigned char *header, uint32_t header_length,
170
         const unsigned char *arg, uint32_t arg_length, bool skip_check)
1 by brian
clean slate
171
{
206.3.1 by Patrick Galbraith
Most everything working with client rename
172
  NET *net= &drizzle->net;
277 by Brian Aker
Cleanup of my_bool from extra and libdrizzle
173
  bool result= 1;
174
  bool stmt_skip= false;
1 by brian
clean slate
175
206.3.1 by Patrick Galbraith
Most everything working with client rename
176
  if (drizzle->net.vio == 0)
177
  {            /* Do reconnect if possible */
178
    if (drizzle_reconnect(drizzle) || stmt_skip)
51.3.5 by Jay Pipes
Merged in from trunk.
179
      return(1);
1 by brian
clean slate
180
  }
206.3.1 by Patrick Galbraith
Most everything working with client rename
181
  if (drizzle->status != DRIZZLE_STATUS_READY ||
182
      drizzle->server_status & SERVER_MORE_RESULTS_EXISTS)
1 by brian
clean slate
183
  {
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...
184
    drizzle_set_error(drizzle, CR_COMMANDS_OUT_OF_SYNC,
185
                      sqlstate_get_unknown());
51.3.5 by Jay Pipes
Merged in from trunk.
186
    return(1);
1 by brian
clean slate
187
  }
188
189
  net_clear_error(net);
206.3.1 by Patrick Galbraith
Most everything working with client rename
190
  drizzle->info=0;
191
  drizzle->affected_rows= ~(uint64_t) 0;
1 by brian
clean slate
192
  /*
193
    We don't want to clear the protocol buffer on COM_QUIT, because if
194
    the previous command was a shutdown command, we may have the
195
    response for the COM_QUIT already in the communication buffer
196
  */
206.3.1 by Patrick Galbraith
Most everything working with client rename
197
  net_clear(&drizzle->net, (command != COM_QUIT));
1 by brian
clean slate
198
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...
199
  if (net_write_command(net,(unsigned char) command, header, header_length,
206.3.1 by Patrick Galbraith
Most everything working with client rename
200
      arg, arg_length))
1 by brian
clean slate
201
  {
383.1.42 by Monty Taylor
Removed mysys and mystrings from libdrizzle. All gone.
202
    if (net->last_errno == CR_NET_PACKET_TOO_LARGE)
1 by brian
clean slate
203
    {
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...
204
      drizzle_set_error(drizzle, CR_NET_PACKET_TOO_LARGE, sqlstate_get_unknown());
1 by brian
clean slate
205
      goto end;
206
    }
390.1.5 by Monty Taylor
Moved more functions into drizzle.c as part of the split of code.
207
    drizzle_disconnect(drizzle);
206.3.1 by Patrick Galbraith
Most everything working with client rename
208
    if (drizzle_reconnect(drizzle) || stmt_skip)
1 by brian
clean slate
209
      goto end;
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...
210
    if (net_write_command(net,(unsigned char) command, header, header_length,
206.3.1 by Patrick Galbraith
Most everything working with client rename
211
        arg, arg_length))
1 by brian
clean slate
212
    {
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...
213
      drizzle_set_error(drizzle, CR_SERVER_GONE_ERROR, sqlstate_get_unknown());
1 by brian
clean slate
214
      goto end;
215
    }
216
  }
217
  result=0;
218
  if (!skip_check)
206.3.1 by Patrick Galbraith
Most everything working with client rename
219
    result= ((drizzle->packet_length=cli_safe_read(drizzle)) == packet_error ?
220
       1 : 0);
1 by brian
clean slate
221
end:
51.3.5 by Jay Pipes
Merged in from trunk.
222
  return(result);
1 by brian
clean slate
223
}
224
206.3.1 by Patrick Galbraith
Most everything working with client rename
225
void free_old_query(DRIZZLE *drizzle)
1 by brian
clean slate
226
{
206.3.1 by Patrick Galbraith
Most everything working with client rename
227
  if (drizzle->fields)
383.1.40 by Monty Taylor
More mysys removal from libdrizzle. Got rid of MEM_ROOT related stuff.
228
  {
229
    /* TODO - we need to de-alloc field storage */
230
    free(drizzle->fields->catalog);
231
    free(drizzle->fields->db);
232
    free(drizzle->fields->table);
233
    free(drizzle->fields->org_table);
234
    free(drizzle->fields->name);
235
    free(drizzle->fields->org_name);
236
    free(drizzle->fields->def);
237
    free(drizzle->fields);
238
239
  }
240
  /* init_alloc_root(&drizzle->field_alloc,8192,0); */ /* Assume rowlength < 8192 */
206.3.1 by Patrick Galbraith
Most everything working with client rename
241
  drizzle->fields= 0;
242
  drizzle->field_count= 0;      /* For API */
243
  drizzle->warning_count= 0;
244
  drizzle->info= 0;
51.3.5 by Jay Pipes
Merged in from trunk.
245
  return;
1 by brian
clean slate
246
}
247
248
249
250
251
287.3.3 by Monty Taylor
Removed STDCALL macros calls.
252
void
206.3.1 by Patrick Galbraith
Most everything working with client rename
253
drizzle_free_result(DRIZZLE_RES *result)
1 by brian
clean slate
254
{
255
  if (result)
256
  {
206.3.1 by Patrick Galbraith
Most everything working with client rename
257
    DRIZZLE *drizzle= result->handle;
258
    if (drizzle)
1 by brian
clean slate
259
    {
206.3.1 by Patrick Galbraith
Most everything working with client rename
260
      if (drizzle->unbuffered_fetch_owner == &result->unbuffered_fetch_cancelled)
261
        drizzle->unbuffered_fetch_owner= 0;
262
      if (drizzle->status == DRIZZLE_STATUS_USE_RESULT)
1 by brian
clean slate
263
      {
206.3.1 by Patrick Galbraith
Most everything working with client rename
264
        (*drizzle->methods->flush_use_result)(drizzle);
265
        drizzle->status=DRIZZLE_STATUS_READY;
266
        if (drizzle->unbuffered_fetch_owner)
267
          *drizzle->unbuffered_fetch_owner= true;
1 by brian
clean slate
268
      }
269
    }
270
    free_rows(result->data);
383.1.40 by Monty Taylor
More mysys removal from libdrizzle. Got rid of MEM_ROOT related stuff.
271
    /* TODO: free result->fields */
1 by brian
clean slate
272
    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...
273
      free((unsigned char*) result->row);
274
    free((unsigned char*) result);
275
  }
276
}
277
278
279
1 by brian
clean slate
280
281
/* Read all rows (fields or data) from server */
282
294 by Brian Aker
libdrizzle has ulong removed.
283
DRIZZLE_DATA *cli_read_rows(DRIZZLE *drizzle, DRIZZLE_FIELD *DRIZZLE_FIELDs, uint32_t fields)
1 by brian
clean slate
284
{
294 by Brian Aker
libdrizzle has ulong removed.
285
  uint32_t  field;
286
  uint32_t pkt_len;
287
  uint32_t len;
288
  unsigned char *cp;
206.3.1 by Patrick Galbraith
Most everything working with client rename
289
  char  *to, *end_to;
290
  DRIZZLE_DATA *result;
291
  DRIZZLE_ROWS **prev_ptr,*cur;
292
  NET *net = &drizzle->net;
1 by brian
clean slate
293
206.3.1 by Patrick Galbraith
Most everything working with client rename
294
  if ((pkt_len= cli_safe_read(drizzle)) == packet_error)
51.3.5 by Jay Pipes
Merged in from trunk.
295
    return(0);
383.1.41 by Monty Taylor
Removed more mysys stuff.
296
  if (!(result=(DRIZZLE_DATA*) malloc(sizeof(DRIZZLE_DATA))))
1 by brian
clean slate
297
  {
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...
298
    drizzle_set_error(drizzle, CR_OUT_OF_MEMORY,
299
                      sqlstate_get_unknown());
51.3.5 by Jay Pipes
Merged in from trunk.
300
    return(0);
1 by brian
clean slate
301
  }
383.1.41 by Monty Taylor
Removed more mysys stuff.
302
  memset(result, 0, sizeof(DRIZZLE_DATA));
1 by brian
clean slate
303
  prev_ptr= &result->data;
304
  result->rows=0;
305
  result->fields=fields;
306
307
  /*
264.2.4 by Andrey Hristov
Remove support for the old, pre-4.1, protocol
308
    The last EOF packet is either a 254 (0xFE) character followed by 1-7 status bytes.
1 by brian
clean slate
309
310
    This doesn't conflict with normal usage of 254 which stands for a
311
    string where the length of the string is 8 bytes. (see net_field_length())
312
  */
313
264.2.4 by Andrey Hristov
Remove support for the old, pre-4.1, protocol
314
  while (*(cp=net->read_pos) != DRIZZLE_PROTOCOL_NO_MORE_DATA || pkt_len >= 8)
1 by brian
clean slate
315
  {
316
    result->rows++;
383.1.40 by Monty Taylor
More mysys removal from libdrizzle. Got rid of MEM_ROOT related stuff.
317
    if (!(cur= (DRIZZLE_ROWS*) malloc(sizeof(DRIZZLE_ROWS))) ||
318
        !(cur->data= ((DRIZZLE_ROW) malloc((fields+1)*sizeof(char *)+pkt_len))))
1 by brian
clean slate
319
    {
320
      free_rows(result);
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...
321
      drizzle_set_error(drizzle, CR_OUT_OF_MEMORY, sqlstate_get_unknown());
51.3.5 by Jay Pipes
Merged in from trunk.
322
      return(0);
1 by brian
clean slate
323
    }
324
    *prev_ptr=cur;
325
    prev_ptr= &cur->next;
326
    to= (char*) (cur->data+fields+1);
327
    end_to=to+pkt_len-1;
328
    for (field=0 ; field < fields ; field++)
329
    {
294 by Brian Aker
libdrizzle has ulong removed.
330
      if ((len= net_field_length(&cp)) == NULL_LENGTH)
206.3.1 by Patrick Galbraith
Most everything working with client rename
331
      {            /* null field */
294 by Brian Aker
libdrizzle has ulong removed.
332
        cur->data[field] = 0;
1 by brian
clean slate
333
      }
334
      else
335
      {
294 by Brian Aker
libdrizzle has ulong removed.
336
        cur->data[field] = to;
337
        if (len > (uint32_t) (end_to - to))
1 by brian
clean slate
338
        {
339
          free_rows(result);
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...
340
          drizzle_set_error(drizzle, CR_MALFORMED_PACKET,
341
                            sqlstate_get_unknown());
51.3.5 by Jay Pipes
Merged in from trunk.
342
          return(0);
1 by brian
clean slate
343
        }
294 by Brian Aker
libdrizzle has ulong removed.
344
        memcpy(to, cp, len);
345
        to[len]=0;
346
        to+=len+1;
347
        cp+=len;
348
        if (DRIZZLE_FIELDs)
349
        {
350
          if (DRIZZLE_FIELDs[field].max_length < len)
351
            DRIZZLE_FIELDs[field].max_length=len;
352
        }
1 by brian
clean slate
353
      }
354
    }
206.3.1 by Patrick Galbraith
Most everything working with client rename
355
    cur->data[field]=to;      /* End of last field */
356
    if ((pkt_len=cli_safe_read(drizzle)) == packet_error)
1 by brian
clean slate
357
    {
358
      free_rows(result);
51.3.5 by Jay Pipes
Merged in from trunk.
359
      return(0);
1 by brian
clean slate
360
    }
361
  }
206.3.1 by Patrick Galbraith
Most everything working with client rename
362
  *prev_ptr=0;          /* last pointer is null */
363
  if (pkt_len > 1)        /* DRIZZLE 4.1 protocol */
1 by brian
clean slate
364
  {
206.3.1 by Patrick Galbraith
Most everything working with client rename
365
    drizzle->warning_count= uint2korr(cp+1);
366
    drizzle->server_status= uint2korr(cp+3);
1 by brian
clean slate
367
  }
51.3.5 by Jay Pipes
Merged in from trunk.
368
  return(result);
1 by brian
clean slate
369
}
370
371
/*
372
  Read one row. Uses packet buffer as storage for fields.
373
  When next packet is read, the previous field values are destroyed
374
*/
375
376
164 by Brian Aker
Commit cleanup of export types.
377
static int32_t
206.3.1 by Patrick Galbraith
Most everything working with client rename
378
read_one_row(DRIZZLE *drizzle, uint32_t fields, DRIZZLE_ROW row, uint32_t *lengths)
1 by brian
clean slate
379
{
395 by Brian Aker
Fixed uint/ushort issue in libdrizzle
380
  uint32_t field;
294 by Brian Aker
libdrizzle has ulong removed.
381
  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...
382
  unsigned char *pos, *prev_pos, *end_pos;
206.3.1 by Patrick Galbraith
Most everything working with client rename
383
  NET *net= &drizzle->net;
1 by brian
clean slate
384
206.3.1 by Patrick Galbraith
Most everything working with client rename
385
  if ((pkt_len=cli_safe_read(drizzle)) == packet_error)
1 by brian
clean slate
386
    return -1;
264.2.4 by Andrey Hristov
Remove support for the old, pre-4.1, protocol
387
  if (pkt_len <= 8 && net->read_pos[0] == DRIZZLE_PROTOCOL_NO_MORE_DATA)
1 by brian
clean slate
388
  {
206.3.1 by Patrick Galbraith
Most everything working with client rename
389
    if (pkt_len > 1)        /* DRIZZLE 4.1 protocol */
1 by brian
clean slate
390
    {
206.3.1 by Patrick Galbraith
Most everything working with client rename
391
      drizzle->warning_count= uint2korr(net->read_pos+1);
392
      drizzle->server_status= uint2korr(net->read_pos+3);
1 by brian
clean slate
393
    }
206.3.1 by Patrick Galbraith
Most everything working with client rename
394
    return 1;        /* End of data */
1 by brian
clean slate
395
  }
206.3.1 by Patrick Galbraith
Most everything working with client rename
396
  prev_pos= 0;        /* allowed to write at packet[-1] */
1 by brian
clean slate
397
  pos=net->read_pos;
398
  end_pos=pos+pkt_len;
399
  for (field=0 ; field < fields ; field++)
400
  {
294 by Brian Aker
libdrizzle has ulong removed.
401
    if ((len= net_field_length(&pos)) == NULL_LENGTH)
206.3.1 by Patrick Galbraith
Most everything working with client rename
402
    {            /* null field */
1 by brian
clean slate
403
      row[field] = 0;
404
      *lengths++=0;
405
    }
406
    else
407
    {
294 by Brian Aker
libdrizzle has ulong removed.
408
      if (len > (uint32_t) (end_pos - pos))
1 by brian
clean slate
409
      {
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...
410
        drizzle_set_error(drizzle, CR_UNKNOWN_ERROR,
411
                          sqlstate_get_unknown());
1 by brian
clean slate
412
        return -1;
413
      }
414
      row[field] = (char*) pos;
415
      pos+=len;
416
      *lengths++=len;
417
    }
418
    if (prev_pos)
206.3.1 by Patrick Galbraith
Most everything working with client rename
419
      *prev_pos=0;        /* Terminate prev field */
1 by brian
clean slate
420
    prev_pos=pos;
421
  }
206.3.1 by Patrick Galbraith
Most everything working with client rename
422
  row[field]=(char*) prev_pos+1;    /* End of last field */
423
  *prev_pos=0;          /* Terminate last field */
1 by brian
clean slate
424
  return 0;
425
}
426
427
428
/**************************************************************************
429
  Return next row of the query results
430
**************************************************************************/
431
287.3.3 by Monty Taylor
Removed STDCALL macros calls.
432
DRIZZLE_ROW
206.3.1 by Patrick Galbraith
Most everything working with client rename
433
drizzle_fetch_row(DRIZZLE_RES *res)
1 by brian
clean slate
434
{
435
  if (!res->data)
206.3.1 by Patrick Galbraith
Most everything working with client rename
436
  {            /* Unbufferred fetch */
1 by brian
clean slate
437
    if (!res->eof)
438
    {
206.3.1 by Patrick Galbraith
Most everything working with client rename
439
      DRIZZLE *drizzle= res->handle;
440
      if (drizzle->status != DRIZZLE_STATUS_USE_RESULT)
1 by brian
clean slate
441
      {
390.1.5 by Monty Taylor
Moved more functions into drizzle.c as part of the split of code.
442
        drizzle_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...
443
                          res->unbuffered_fetch_cancelled ?
444
                          CR_FETCH_CANCELED : CR_COMMANDS_OUT_OF_SYNC,
445
                          sqlstate_get_unknown());
1 by brian
clean slate
446
      }
206.3.1 by Patrick Galbraith
Most everything working with client rename
447
      else if (!(read_one_row(drizzle, res->field_count, res->row, res->lengths)))
1 by brian
clean slate
448
      {
206.3.1 by Patrick Galbraith
Most everything working with client rename
449
  res->row_count++;
450
  return(res->current_row=res->row);
1 by brian
clean slate
451
      }
452
      res->eof=1;
206.3.1 by Patrick Galbraith
Most everything working with client rename
453
      drizzle->status=DRIZZLE_STATUS_READY;
1 by brian
clean slate
454
      /*
455
        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
456
        started new query after drizzle_stmt_close():
1 by brian
clean slate
457
      */
206.3.1 by Patrick Galbraith
Most everything working with client rename
458
      if (drizzle->unbuffered_fetch_owner == &res->unbuffered_fetch_cancelled)
459
        drizzle->unbuffered_fetch_owner= 0;
460
      /* Don't clear handle in drizzle_free_result */
1 by brian
clean slate
461
      res->handle=0;
462
    }
206.3.1 by Patrick Galbraith
Most everything working with client rename
463
    return((DRIZZLE_ROW) NULL);
1 by brian
clean slate
464
  }
465
  {
206.3.1 by Patrick Galbraith
Most everything working with client rename
466
    DRIZZLE_ROW tmp;
1 by brian
clean slate
467
    if (!res->data_cursor)
468
    {
206.3.1 by Patrick Galbraith
Most everything working with client rename
469
      return(res->current_row=(DRIZZLE_ROW) NULL);
1 by brian
clean slate
470
    }
471
    tmp = res->data_cursor->data;
472
    res->data_cursor = res->data_cursor->next;
51.3.5 by Jay Pipes
Merged in from trunk.
473
    return(res->current_row=tmp);
1 by brian
clean slate
474
  }
475
}
476
477
478
/**************************************************************************
479
  Get column lengths of the current row
206.3.1 by Patrick Galbraith
Most everything working with client rename
480
  If one uses drizzle_use_result, res->lengths contains the length information,
1 by brian
clean slate
481
  else the lengths are calculated from the offset between pointers.
482
**************************************************************************/
483
287.3.3 by Monty Taylor
Removed STDCALL macros calls.
484
uint32_t *
206.3.1 by Patrick Galbraith
Most everything working with client rename
485
drizzle_fetch_lengths(DRIZZLE_RES *res)
1 by brian
clean slate
486
{
206.3.1 by Patrick Galbraith
Most everything working with client rename
487
  DRIZZLE_ROW column;
1 by brian
clean slate
488
489
  if (!(column=res->current_row))
206.3.1 by Patrick Galbraith
Most everything working with client rename
490
    return 0;          /* Something is wrong */
1 by brian
clean slate
491
  if (res->data)
492
    (*res->methods->fetch_lengths)(res->lengths, column, res->field_count);
493
  return res->lengths;
494
}
495
496
287.3.3 by Monty Taylor
Removed STDCALL macros calls.
497
int
206.3.1 by Patrick Galbraith
Most everything working with client rename
498
drizzle_options(DRIZZLE *drizzle,enum drizzle_option option, const void *arg)
1 by brian
clean slate
499
{
500
  switch (option) {
206.3.1 by Patrick Galbraith
Most everything working with client rename
501
  case DRIZZLE_OPT_CONNECT_TIMEOUT:
395 by Brian Aker
Fixed uint/ushort issue in libdrizzle
502
    drizzle->options.connect_timeout= *(uint32_t*) arg;
206.3.1 by Patrick Galbraith
Most everything working with client rename
503
    break;
504
  case DRIZZLE_OPT_READ_TIMEOUT:
395 by Brian Aker
Fixed uint/ushort issue in libdrizzle
505
    drizzle->options.read_timeout= *(uint32_t*) arg;
206.3.1 by Patrick Galbraith
Most everything working with client rename
506
    break;
507
  case DRIZZLE_OPT_WRITE_TIMEOUT:
395 by Brian Aker
Fixed uint/ushort issue in libdrizzle
508
    drizzle->options.write_timeout= *(uint32_t*) arg;
206.3.1 by Patrick Galbraith
Most everything working with client rename
509
    break;
510
  case DRIZZLE_OPT_COMPRESS:
511
    drizzle->options.compress= 1;      /* Remember for connect */
512
    drizzle->options.client_flag|= CLIENT_COMPRESS;
513
    break;
514
  case DRIZZLE_OPT_LOCAL_INFILE:      /* Allow LOAD DATA LOCAL ?*/
395 by Brian Aker
Fixed uint/ushort issue in libdrizzle
515
    if (!arg || (*(uint32_t*) arg) ? 1 : 0)
206.3.1 by Patrick Galbraith
Most everything working with client rename
516
      drizzle->options.client_flag|= CLIENT_LOCAL_FILES;
1 by brian
clean slate
517
    else
206.3.1 by Patrick Galbraith
Most everything working with client rename
518
      drizzle->options.client_flag&= ~CLIENT_LOCAL_FILES;
519
    break;
520
  case DRIZZLE_READ_DEFAULT_FILE:
383.1.41 by Monty Taylor
Removed more mysys stuff.
521
    if (drizzle->options.my_cnf_file != NULL)
522
      free(drizzle->options.my_cnf_file);
523
    drizzle->options.my_cnf_file=strdup(arg);
206.3.1 by Patrick Galbraith
Most everything working with client rename
524
    break;
525
  case DRIZZLE_READ_DEFAULT_GROUP:
383.1.41 by Monty Taylor
Removed more mysys stuff.
526
    if (drizzle->options.my_cnf_group != NULL)
527
      free(drizzle->options.my_cnf_group);
528
    drizzle->options.my_cnf_group=strdup(arg);
206.3.1 by Patrick Galbraith
Most everything working with client rename
529
    break;
530
  case DRIZZLE_OPT_PROTOCOL:
531
    break;
532
  case DRIZZLE_OPT_USE_REMOTE_CONNECTION:
533
  case DRIZZLE_OPT_GUESS_CONNECTION:
534
    drizzle->options.methods_to_use= option;
535
    break;
536
  case DRIZZLE_SET_CLIENT_IP:
383.1.41 by Monty Taylor
Removed more mysys stuff.
537
    drizzle->options.client_ip= strdup(arg);
206.3.1 by Patrick Galbraith
Most everything working with client rename
538
    break;
539
  case DRIZZLE_SECURE_AUTH:
266.7.7 by Andy Lester
const happiness
540
    drizzle->options.secure_auth= *(const bool *) arg;
206.3.1 by Patrick Galbraith
Most everything working with client rename
541
    break;
542
  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...
543
    drizzle->options.report_data_truncation= (*(const bool *) arg) ? 1 : 0;
206.3.1 by Patrick Galbraith
Most everything working with client rename
544
    break;
545
  case DRIZZLE_OPT_RECONNECT:
266.7.7 by Andy Lester
const happiness
546
    drizzle->reconnect= *(const bool *) arg;
206.3.1 by Patrick Galbraith
Most everything working with client rename
547
    break;
548
  case DRIZZLE_OPT_SSL_VERIFY_SERVER_CERT:
266.7.7 by Andy Lester
const happiness
549
    if (*(const bool*) arg)
206.3.1 by Patrick Galbraith
Most everything working with client rename
550
      drizzle->options.client_flag|= CLIENT_SSL_VERIFY_SERVER_CERT;
1 by brian
clean slate
551
    else
206.3.1 by Patrick Galbraith
Most everything working with client rename
552
      drizzle->options.client_flag&= ~CLIENT_SSL_VERIFY_SERVER_CERT;
1 by brian
clean slate
553
    break;
554
  default:
51.3.5 by Jay Pipes
Merged in from trunk.
555
    return(1);
1 by brian
clean slate
556
  }
51.3.5 by Jay Pipes
Merged in from trunk.
557
  return(0);
1 by brian
clean slate
558
}
559
560
561
/****************************************************************************
206.3.1 by Patrick Galbraith
Most everything working with client rename
562
  Functions to get information from the DRIZZLE structure
1 by brian
clean slate
563
  These are functions to make shared libraries more usable.
564
****************************************************************************/
565
206.3.1 by Patrick Galbraith
Most everything working with client rename
566
/* DRIZZLE_RES */
287.3.3 by Monty Taylor
Removed STDCALL macros calls.
567
uint64_t drizzle_num_rows(const DRIZZLE_RES *res)
1 by brian
clean slate
568
{
569
  return res->row_count;
570
}
571
287.3.3 by Monty Taylor
Removed STDCALL macros calls.
572
unsigned int drizzle_num_fields(const DRIZZLE_RES *res)
1 by brian
clean slate
573
{
574
  return res->field_count;
575
}
576
577
578
/*
579
  Get version number for server in a form easy to test on
580
581
  SYNOPSIS
206.3.1 by Patrick Galbraith
Most everything working with client rename
582
    drizzle_get_server_version()
583
    drizzle Connection
1 by brian
clean slate
584
585
  EXAMPLE
586
    4.1.0-alfa ->  40100
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
587
1 by brian
clean slate
588
  NOTES
589
    We will ensure that a newer server always has a bigger number.
590
591
  RETURN
592
   Signed number > 323000
593
*/
594
287.3.3 by Monty Taylor
Removed STDCALL macros calls.
595
uint32_t
236.3.5 by Andrey Hristov
Constify libdrizzle functions
596
drizzle_get_server_version(const DRIZZLE *drizzle)
1 by brian
clean slate
597
{
395 by Brian Aker
Fixed uint/ushort issue in libdrizzle
598
  uint32_t major, minor, version;
206.3.1 by Patrick Galbraith
Most everything working with client rename
599
  char *pos= drizzle->server_version, *end_pos;
395 by Brian Aker
Fixed uint/ushort issue in libdrizzle
600
  major=   (uint32_t) strtoul(pos, &end_pos, 10);  pos=end_pos+1;
601
  minor=   (uint32_t) strtoul(pos, &end_pos, 10);  pos=end_pos+1;
602
  version= (uint32_t) strtoul(pos, &end_pos, 10);
294 by Brian Aker
libdrizzle has ulong removed.
603
  return (uint32_t) major*10000L+(uint32_t) (minor*100+version);
1 by brian
clean slate
604
}
605