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