~drizzle-trunk/drizzle/development

390.1.4 by Monty Taylor
More copyright header file fixes.
1
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 *
4
 *  Copyright (C) 2008 Sun Microsystems, Inc.
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
8
 *  the Free Software Foundation; version 2 of the License.
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
 */
1 by brian
clean slate
19
383.1.44 by Monty Taylor
Renamed drizzle.h to libdrizzle.h.
20
#include "libdrizzle.h"
390.1.5 by Monty Taylor
Moved more functions into drizzle.c as part of the split of code.
21
#include "libdrizzle_priv.h"
1 by brian
clean slate
22
#include "errmsg.h"
23
#include <sys/stat.h>
24
#include <signal.h>
25
#include <time.h>
206.3.1 by Patrick Galbraith
Most everything working with client rename
26
#ifdef   HAVE_PWD_H
1 by brian
clean slate
27
#include <pwd.h>
28
#endif
94 by Brian Aker
DOS removal. DOS... hard to believe aye?
29
1 by brian
clean slate
30
#include <sys/socket.h>
31
#include <netinet/in.h>
32
#include <arpa/inet.h>
33
#include <netdb.h>
34
#ifdef HAVE_SELECT_H
35
#include <select.h>
36
#endif
37
#ifdef HAVE_SYS_SELECT_H
38
#include <sys/select.h>
39
#endif
94 by Brian Aker
DOS removal. DOS... hard to believe aye?
40
1 by brian
clean slate
41
#ifdef HAVE_POLL
42
#include <sys/poll.h>
43
#endif
44
#ifdef HAVE_SYS_UN_H
45
#include <sys/un.h>
46
#endif
47
#ifndef INADDR_NONE
206.3.1 by Patrick Galbraith
Most everything working with client rename
48
#define INADDR_NONE  -1
1 by brian
clean slate
49
#endif
50
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...
51
#include <stdlib.h>
52
#include <string.h>
53
1 by brian
clean slate
54
#include <sql_common.h>
264.1.9 by Monty Taylor
Removed version.h (non-public header) from drizzle.h (public header)
55
#include <drizzled/version.h>
1 by brian
clean slate
56
383.1.38 by Monty Taylor
Reworked drizzle_escape_string.
57
/* Borrowed from libicu header */
58
59
#define U8_IS_SINGLE(c) (((c)&0x80)==0)
60
#define U8_LENGTH(c) \
61
    ((uint32_t)(c)<=0x7f ? 1 : \
62
        ((uint32_t)(c)<=0x7ff ? 2 : \
63
            ((uint32_t)(c)<=0xd7ff ? 3 : \
64
                ((uint32_t)(c)<=0xdfff || (uint32_t)(c)>0x10ffff ? 0 : \
65
                    ((uint32_t)(c)<=0xffff ? 3 : 4)\
66
                ) \
67
            ) \
68
        ) \
69
    )
70
71
1 by brian
clean slate
72
#undef net_buffer_length
73
#undef max_allowed_packet
74
206.3.1 by Patrick Galbraith
Most everything working with client rename
75
uint32_t     net_buffer_length= 8192;
76
uint32_t    max_allowed_packet= 1024L*1024L*1024L;
1 by brian
clean slate
77
390.1.5 by Monty Taylor
Moved more functions into drizzle.c as part of the split of code.
78
unsigned int drizzle_port=0;
79
1 by brian
clean slate
80
#include <errno.h>
81
#define SOCKET_ERROR -1
82
83
/*
84
  If allowed through some configuration, then this needs to
85
  be changed
86
*/
87
#define MAX_LONG_DATA_LENGTH 8192
88
#define unsigned_field(A) ((A)->flags & UNSIGNED_FLAG)
89
202.2.2 by Monty Taylor
Merged mysql_server_init into drizzle_create()
90
206.3.1 by Patrick Galbraith
Most everything working with client rename
91
static DRIZZLE_PARAMETERS drizzle_internal_parameters=
1 by brian
clean slate
92
{&max_allowed_packet, &net_buffer_length, 0};
93
287.3.6 by Monty Taylor
Eeek. Final cleanups.
94
const DRIZZLE_PARAMETERS * drizzle_get_parameters(void)
1 by brian
clean slate
95
{
206.3.1 by Patrick Galbraith
Most everything working with client rename
96
  return &drizzle_internal_parameters;
1 by brian
clean slate
97
}
98
390.1.5 by Monty Taylor
Moved more functions into drizzle.c as part of the split of code.
99
unsigned int drizzle_get_default_port(void)
100
{
101
  return drizzle_port;
102
}
103
104
void drizzle_set_default_port(unsigned int port)
105
{
106
  drizzle_port= port;
107
}
108
1 by brian
clean slate
109
/*
110
  Expand wildcard to a sql string
111
*/
112
113
static void
114
append_wild(char *to, char *end, const char *wild)
115
{
206.3.1 by Patrick Galbraith
Most everything working with client rename
116
  end-=5;          /* Some extra */
1 by brian
clean slate
117
  if (wild && wild[0])
118
  {
240.1.2 by Toru Maesaka
str(mov|make) replaced with standard C calls in libdrizzle.c
119
    to= strcpy(to," like '");
120
    to+= 7; /* strlen(" like '"); */
121
1 by brian
clean slate
122
    while (*wild && to < end)
123
    {
124
      if (*wild == '\\' || *wild == '\'')
206.3.1 by Patrick Galbraith
Most everything working with client rename
125
  *to++='\\';
1 by brian
clean slate
126
      *to++= *wild++;
127
    }
206.3.1 by Patrick Galbraith
Most everything working with client rename
128
    if (*wild)          /* Too small buffer */
129
      *to++='%';        /* Nicer this way */
1 by brian
clean slate
130
    to[0]='\'';
131
    to[1]=0;
132
  }
133
}
134
135
/**************************************************************************
136
  Change user and database
137
**************************************************************************/
138
236.3.6 by Andrey Hristov
read_change_user_result doesn't neet buffer and password. They were needed to handle 3.23 authentication, which is no more.
139
int cli_read_change_user_result(DRIZZLE *drizzle)
1 by brian
clean slate
140
{
294 by Brian Aker
libdrizzle has ulong removed.
141
  uint32_t pkt_length;
1 by brian
clean slate
142
206.3.1 by Patrick Galbraith
Most everything working with client rename
143
  pkt_length= cli_safe_read(drizzle);
1 by brian
clean slate
144
  
145
  if (pkt_length == packet_error)
146
    return 1;
147
148
  return 0;
149
}
150
287.3.3 by Monty Taylor
Removed STDCALL macros calls.
151
bool drizzle_change_user(DRIZZLE *drizzle, const char *user,
261.1.2 by Brian Aker
Cleanup/removal of more of the dead protocols.
152
                                 const char *passwd, const char *db)
1 by brian
clean slate
153
{
154
  char buff[USERNAME_LENGTH+SCRAMBLED_PASSWORD_CHAR_LENGTH+NAME_LEN+2];
155
  char *end= buff;
156
  int rc;
157
158
  /* Use an empty string instead of NULL. */
159
160
  if (!user)
161
    user="";
162
  if (!passwd)
163
    passwd="";
164
165
  /* Store user into the buffer */
240.1.2 by Toru Maesaka
str(mov|make) replaced with standard C calls in libdrizzle.c
166
  end= strncpy(end, user, USERNAME_LENGTH) + USERNAME_LENGTH + 1;
1 by brian
clean slate
167
168
  /* write scrambled password according to server capabilities */
169
  if (passwd[0])
170
  {
171
    {
172
      *end++= SCRAMBLE_LENGTH;
173
      end+= SCRAMBLE_LENGTH;
174
    }
175
  }
176
  else
177
    *end++= '\0';                               /* empty password */
178
  /* Add database if needed */
240.1.2 by Toru Maesaka
str(mov|make) replaced with standard C calls in libdrizzle.c
179
  end= strncpy(end, db ? db : "", NAME_LEN) + NAME_LEN + 1;
1 by brian
clean slate
180
181
  /* Add character set number. */
206.3.1 by Patrick Galbraith
Most everything working with client rename
182
  if (drizzle->server_capabilities & CLIENT_SECURE_CONNECTION)
1 by brian
clean slate
183
  {
395 by Brian Aker
Fixed uint/ushort issue in libdrizzle
184
    int2store(end, (uint16_t) 45); // utf8mb4 number from mystrings/ctype-utf8.c
1 by brian
clean slate
185
    end+= 2;
186
  }
187
188
  /* Write authentication package */
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...
189
  (void)simple_command(drizzle,COM_CHANGE_USER, (unsigned char*) buff, (uint32_t) (end-buff), 1);
1 by brian
clean slate
190
236.3.6 by Andrey Hristov
read_change_user_result doesn't neet buffer and password. They were needed to handle 3.23 authentication, which is no more.
191
  rc= (*drizzle->methods->read_change_user_result)(drizzle);
1 by brian
clean slate
192
193
  if (rc == 0)
194
  {
195
    /* Free old connect information */
207.2.3 by Toru Maesaka
Merge from trunk and conflicts resolved
196
    if(drizzle->user)
197
      free(drizzle->user);
198
    if(drizzle->passwd)
199
      free(drizzle->passwd);
200
    if(drizzle->db)
201
      free(drizzle->db);
1 by brian
clean slate
202
203
    /* alloc new connect information */
240.1.1 by Toru Maesaka
removed my_strdup and dead code from libdrizzle
204
    drizzle->user= strdup(user);
205
    drizzle->passwd= strdup(passwd);
206
    drizzle->db= db ? strdup(db) : 0;
1 by brian
clean slate
207
  }
208
51.3.5 by Jay Pipes
Merged in from trunk.
209
  return(rc);
1 by brian
clean slate
210
}
211
212
#if defined(HAVE_GETPWUID) && defined(NO_GETPWUID_DECL)
213
struct passwd *getpwuid(uid_t);
214
char* getlogin(void);
215
#endif
216
217
/**************************************************************************
218
  Do a query. If query returned rows, free old rows.
206.3.1 by Patrick Galbraith
Most everything working with client rename
219
  Read data by drizzle_store_result or by repeat call of drizzle_fetch_row
1 by brian
clean slate
220
**************************************************************************/
221
287.3.3 by Monty Taylor
Removed STDCALL macros calls.
222
int
206.3.1 by Patrick Galbraith
Most everything working with client rename
223
drizzle_query(DRIZZLE *drizzle, const char *query)
1 by brian
clean slate
224
{
395 by Brian Aker
Fixed uint/ushort issue in libdrizzle
225
  return drizzle_real_query(drizzle,query, (uint32_t) strlen(query));
1 by brian
clean slate
226
}
227
228
229
/**************************************************************************
230
  Return next field of the query results
231
**************************************************************************/
232
287.3.3 by Monty Taylor
Removed STDCALL macros calls.
233
DRIZZLE_FIELD *
206.3.1 by Patrick Galbraith
Most everything working with client rename
234
drizzle_fetch_field(DRIZZLE_RES *result)
1 by brian
clean slate
235
{
236
  if (result->current_field >= result->field_count)
237
    return(NULL);
238
  return &result->fields[result->current_field++];
239
}
240
241
242
/**************************************************************************
243
  Move to a specific row and column
244
**************************************************************************/
245
287.3.3 by Monty Taylor
Removed STDCALL macros calls.
246
void
206.3.1 by Patrick Galbraith
Most everything working with client rename
247
drizzle_data_seek(DRIZZLE_RES *result, uint64_t row)
1 by brian
clean slate
248
{
206.3.1 by Patrick Galbraith
Most everything working with client rename
249
  DRIZZLE_ROWS  *tmp=0;
1 by brian
clean slate
250
  if (result->data)
251
    for (tmp=result->data->data; row-- && tmp ; tmp = tmp->next) ;
252
  result->current_row=0;
253
  result->data_cursor = tmp;
254
}
255
256
257
/*************************************************************************
206.3.1 by Patrick Galbraith
Most everything working with client rename
258
  put the row or field cursor one a position one got from DRIZZLE_ROW_tell()
259
  This doesn't restore any data. The next drizzle_fetch_row or
260
  drizzle_fetch_field will return the next row or field after the last used
1 by brian
clean slate
261
*************************************************************************/
262
287.3.3 by Monty Taylor
Removed STDCALL macros calls.
263
DRIZZLE_ROW_OFFSET
206.3.1 by Patrick Galbraith
Most everything working with client rename
264
drizzle_row_seek(DRIZZLE_RES *result, DRIZZLE_ROW_OFFSET row)
1 by brian
clean slate
265
{
206.3.1 by Patrick Galbraith
Most everything working with client rename
266
  DRIZZLE_ROW_OFFSET return_value=result->data_cursor;
1 by brian
clean slate
267
  result->current_row= 0;
268
  result->data_cursor= row;
269
  return return_value;
270
}
271
272
287.3.3 by Monty Taylor
Removed STDCALL macros calls.
273
DRIZZLE_FIELD_OFFSET
206.3.1 by Patrick Galbraith
Most everything working with client rename
274
drizzle_field_seek(DRIZZLE_RES *result, DRIZZLE_FIELD_OFFSET field_offset)
1 by brian
clean slate
275
{
206.3.1 by Patrick Galbraith
Most everything working with client rename
276
  DRIZZLE_FIELD_OFFSET return_value=result->current_field;
1 by brian
clean slate
277
  result->current_field=field_offset;
278
  return return_value;
279
}
280
281
282
/*****************************************************************************
283
  List all tables in a database
284
  If wild is given then only the tables matching wild is returned
285
*****************************************************************************/
286
287.3.3 by Monty Taylor
Removed STDCALL macros calls.
287
DRIZZLE_RES *
206.3.1 by Patrick Galbraith
Most everything working with client rename
288
drizzle_list_tables(DRIZZLE *drizzle, const char *wild)
1 by brian
clean slate
289
{
290
  char buff[255];
240.1.2 by Toru Maesaka
str(mov|make) replaced with standard C calls in libdrizzle.c
291
  char *ptr= strcpy(buff, "show tables");
292
  ptr+= 11; /* strlen("show tables"); */
1 by brian
clean slate
293
240.1.2 by Toru Maesaka
str(mov|make) replaced with standard C calls in libdrizzle.c
294
  append_wild(ptr,buff+sizeof(buff),wild);
206.3.1 by Patrick Galbraith
Most everything working with client rename
295
  if (drizzle_query(drizzle,buff))
51.3.5 by Jay Pipes
Merged in from trunk.
296
    return(0);
206.3.1 by Patrick Galbraith
Most everything working with client rename
297
  return (drizzle_store_result(drizzle));
1 by brian
clean slate
298
}
299
300
206.3.1 by Patrick Galbraith
Most everything working with client rename
301
DRIZZLE_FIELD *cli_list_fields(DRIZZLE *drizzle)
1 by brian
clean slate
302
{
206.3.1 by Patrick Galbraith
Most everything working with client rename
303
  DRIZZLE_DATA *query;
264.2.4 by Andrey Hristov
Remove support for the old, pre-4.1, protocol
304
  if (!(query= cli_read_rows(drizzle,(DRIZZLE_FIELD*) 0, 8)))
1 by brian
clean slate
305
    return NULL;
306
395 by Brian Aker
Fixed uint/ushort issue in libdrizzle
307
  drizzle->field_count= (uint32_t) query->rows;
383.1.40 by Monty Taylor
More mysys removal from libdrizzle. Got rid of MEM_ROOT related stuff.
308
  return unpack_fields(query, drizzle->field_count, 1);
1 by brian
clean slate
309
}
310
311
312
/**************************************************************************
313
  List all fields in a table
314
  If wild is given then only the fields matching wild is returned
315
  Instead of this use query:
316
  show fields in 'table' like "wild"
317
**************************************************************************/
318
287.3.3 by Monty Taylor
Removed STDCALL macros calls.
319
DRIZZLE_RES *
206.3.1 by Patrick Galbraith
Most everything working with client rename
320
drizzle_list_fields(DRIZZLE *drizzle, const char *table, const char *wild)
1 by brian
clean slate
321
{
206.3.1 by Patrick Galbraith
Most everything working with client rename
322
  DRIZZLE_RES   *result;
323
  DRIZZLE_FIELD *fields;
240.1.2 by Toru Maesaka
str(mov|make) replaced with standard C calls in libdrizzle.c
324
  char buff[257], *end;
325
326
  end= strncpy(buff, table, 128) + 128;
327
  end= strncpy(end+1, wild ? wild : "", 128) + 128;
328
206.3.1 by Patrick Galbraith
Most everything working with client rename
329
  free_old_query(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...
330
  if (simple_command(drizzle, COM_FIELD_LIST, (unsigned char*) buff,
294 by Brian Aker
libdrizzle has ulong removed.
331
                     (uint32_t) (end-buff), 1) ||
206.3.1 by Patrick Galbraith
Most everything working with client rename
332
      !(fields= (*drizzle->methods->list_fields)(drizzle)))
333
    return(NULL);
334
207.2.3 by Toru Maesaka
Merge from trunk and conflicts resolved
335
  if (!(result = (DRIZZLE_RES *) malloc(sizeof(DRIZZLE_RES))))
206.3.1 by Patrick Galbraith
Most everything working with client rename
336
    return(NULL);
337
266.3.2 by Andrew Garner
memory was not initialized for new results in drizzle_list_fields
338
  memset(result, 0, sizeof(DRIZZLE_RES));
339
206.3.1 by Patrick Galbraith
Most everything working with client rename
340
  result->methods= drizzle->methods;
341
  drizzle->fields=0;
342
  result->field_count = drizzle->field_count;
1 by brian
clean slate
343
  result->fields= fields;
344
  result->eof=1;
51.3.5 by Jay Pipes
Merged in from trunk.
345
  return(result);
1 by brian
clean slate
346
}
347
348
/* List all running processes (threads) in server */
349
287.3.3 by Monty Taylor
Removed STDCALL macros calls.
350
DRIZZLE_RES *
206.3.1 by Patrick Galbraith
Most everything working with client rename
351
drizzle_list_processes(DRIZZLE *drizzle)
1 by brian
clean slate
352
{
206.3.1 by Patrick Galbraith
Most everything working with client rename
353
  DRIZZLE_DATA *fields;
395 by Brian Aker
Fixed uint/ushort issue in libdrizzle
354
  uint32_t field_count;
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...
355
  unsigned char *pos;
1 by brian
clean slate
356
206.3.1 by Patrick Galbraith
Most everything working with client rename
357
  if (simple_command(drizzle,COM_PROCESS_INFO,0,0,0))
51.3.5 by Jay Pipes
Merged in from trunk.
358
    return(0);
206.3.1 by Patrick Galbraith
Most everything working with client rename
359
  free_old_query(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...
360
  pos=(unsigned char*) drizzle->net.read_pos;
395 by Brian Aker
Fixed uint/ushort issue in libdrizzle
361
  field_count=(uint32_t) net_field_length(&pos);
264.2.4 by Andrey Hristov
Remove support for the old, pre-4.1, protocol
362
  if (!(fields = (*drizzle->methods->read_rows)(drizzle,(DRIZZLE_FIELD*) 0, 7)))
51.3.5 by Jay Pipes
Merged in from trunk.
363
    return(NULL);
383.1.40 by Monty Taylor
More mysys removal from libdrizzle. Got rid of MEM_ROOT related stuff.
364
  if (!(drizzle->fields=unpack_fields(fields, field_count, 0)))
51.3.5 by Jay Pipes
Merged in from trunk.
365
    return(0);
206.3.1 by Patrick Galbraith
Most everything working with client rename
366
  drizzle->status=DRIZZLE_STATUS_GET_RESULT;
367
  drizzle->field_count=field_count;
368
  return(drizzle_store_result(drizzle));
1 by brian
clean slate
369
}
370
371
287.3.3 by Monty Taylor
Removed STDCALL macros calls.
372
int
438.1.9 by Brian Aker
Removed dead shutdown code.
373
drizzle_shutdown(DRIZZLE *drizzle)
1 by brian
clean slate
374
{
480 by Brian Aker
Two regression fixes.
375
  return(simple_command(drizzle, COM_SHUTDOWN, 0, 0, 0));
1 by brian
clean slate
376
}
377
378
287.3.3 by Monty Taylor
Removed STDCALL macros calls.
379
int
438.1.9 by Brian Aker
Removed dead shutdown code.
380
drizzle_refresh(DRIZZLE *drizzle, uint32_t options)
1 by brian
clean slate
381
{
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 bits[1];
383
  bits[0]= (unsigned char) options;
206.3.1 by Patrick Galbraith
Most everything working with client rename
384
  return(simple_command(drizzle, COM_REFRESH, bits, 1, 0));
1 by brian
clean slate
385
}
386
387
287.3.3 by Monty Taylor
Removed STDCALL macros calls.
388
int32_t
206.3.1 by Patrick Galbraith
Most everything working with client rename
389
drizzle_kill(DRIZZLE *drizzle, uint32_t pid)
1 by brian
clean slate
390
{
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...
391
  unsigned char buff[4];
1 by brian
clean slate
392
  int4store(buff,pid);
206.3.1 by Patrick Galbraith
Most everything working with client rename
393
  return(simple_command(drizzle,COM_PROCESS_KILL,buff,sizeof(buff),0));
1 by brian
clean slate
394
}
395
396
287.3.3 by Monty Taylor
Removed STDCALL macros calls.
397
int
206.3.1 by Patrick Galbraith
Most everything working with client rename
398
drizzle_set_server_option(DRIZZLE *drizzle, enum enum_drizzle_set_option option)
1 by brian
clean slate
399
{
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...
400
  unsigned char buff[2];
395 by Brian Aker
Fixed uint/ushort issue in libdrizzle
401
  int2store(buff, (uint32_t) option);
206.3.1 by Patrick Galbraith
Most everything working with client rename
402
  return(simple_command(drizzle, COM_SET_OPTION, buff, sizeof(buff), 0));
1 by brian
clean slate
403
}
404
405
206.3.1 by Patrick Galbraith
Most everything working with client rename
406
const char *cli_read_statistics(DRIZZLE *drizzle)
1 by brian
clean slate
407
{
206.3.1 by Patrick Galbraith
Most everything working with client rename
408
  drizzle->net.read_pos[drizzle->packet_length]=0;  /* End of stat string */
409
  if (!drizzle->net.read_pos[0])
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_WRONG_HOST_INFO, sqlstate_get_unknown());
206.3.1 by Patrick Galbraith
Most everything working with client rename
412
    return drizzle->net.last_error;
1 by brian
clean slate
413
  }
206.3.1 by Patrick Galbraith
Most everything working with client rename
414
  return (char*) drizzle->net.read_pos;
1 by brian
clean slate
415
}
416
417
287.3.3 by Monty Taylor
Removed STDCALL macros calls.
418
int
206.3.1 by Patrick Galbraith
Most everything working with client rename
419
drizzle_ping(DRIZZLE *drizzle)
1 by brian
clean slate
420
{
421
  int res;
206.3.1 by Patrick Galbraith
Most everything working with client rename
422
  res= simple_command(drizzle,COM_PING,0,0,0);
423
  if (res == CR_SERVER_LOST && drizzle->reconnect)
424
    res= simple_command(drizzle,COM_PING,0,0,0);
51.3.5 by Jay Pipes
Merged in from trunk.
425
  return(res);
1 by brian
clean slate
426
}
427
428
287.3.3 by Monty Taylor
Removed STDCALL macros calls.
429
const char *
236.3.5 by Andrey Hristov
Constify libdrizzle functions
430
drizzle_get_server_info(const DRIZZLE *drizzle)
1 by brian
clean slate
431
{
206.3.1 by Patrick Galbraith
Most everything working with client rename
432
  return((char*) drizzle->server_version);
1 by brian
clean slate
433
}
434
435
287.3.3 by Monty Taylor
Removed STDCALL macros calls.
436
const char *
236.3.5 by Andrey Hristov
Constify libdrizzle functions
437
drizzle_get_host_info(const DRIZZLE *drizzle)
1 by brian
clean slate
438
{
206.3.1 by Patrick Galbraith
Most everything working with client rename
439
  return(drizzle->host_info);
1 by brian
clean slate
440
}
441
442
395 by Brian Aker
Fixed uint/ushort issue in libdrizzle
443
uint32_t
236.3.5 by Andrey Hristov
Constify libdrizzle functions
444
drizzle_get_proto_info(const DRIZZLE *drizzle)
1 by brian
clean slate
445
{
206.3.1 by Patrick Galbraith
Most everything working with client rename
446
  return (drizzle->protocol_version);
1 by brian
clean slate
447
}
448
287.3.3 by Monty Taylor
Removed STDCALL macros calls.
449
const char *
206.3.1 by Patrick Galbraith
Most everything working with client rename
450
drizzle_get_client_info(void)
1 by brian
clean slate
451
{
319.1.1 by Grant Limberg
renamed all instances of MYSQL_ to DRIZZLE_
452
  return (char*) DRIZZLE_SERVER_VERSION;
1 by brian
clean slate
453
}
454
287.3.3 by Monty Taylor
Removed STDCALL macros calls.
455
uint32_t drizzle_get_client_version(void)
1 by brian
clean slate
456
{
319.1.1 by Grant Limberg
renamed all instances of MYSQL_ to DRIZZLE_
457
  return DRIZZLE_VERSION_ID;
1 by brian
clean slate
458
}
459
287.3.3 by Monty Taylor
Removed STDCALL macros calls.
460
bool drizzle_eof(const DRIZZLE_RES *res)
1 by brian
clean slate
461
{
462
  return res->eof;
463
}
464
287.3.3 by Monty Taylor
Removed STDCALL macros calls.
465
const DRIZZLE_FIELD * drizzle_fetch_field_direct(const DRIZZLE_RES *res, unsigned int fieldnr)
1 by brian
clean slate
466
{
467
  return &(res)->fields[fieldnr];
468
}
469
287.3.3 by Monty Taylor
Removed STDCALL macros calls.
470
const DRIZZLE_FIELD * drizzle_fetch_fields(const DRIZZLE_RES *res)
1 by brian
clean slate
471
{
236.3.5 by Andrey Hristov
Constify libdrizzle functions
472
  return res->fields;
1 by brian
clean slate
473
}
474
287.3.3 by Monty Taylor
Removed STDCALL macros calls.
475
DRIZZLE_ROW_OFFSET drizzle_row_tell(const DRIZZLE_RES *res)
1 by brian
clean slate
476
{
477
  return res->data_cursor;
478
}
479
287.3.3 by Monty Taylor
Removed STDCALL macros calls.
480
DRIZZLE_FIELD_OFFSET drizzle_field_tell(const DRIZZLE_RES *res)
1 by brian
clean slate
481
{
236.3.5 by Andrey Hristov
Constify libdrizzle functions
482
  return res->current_field;
1 by brian
clean slate
483
}
484
206.3.1 by Patrick Galbraith
Most everything working with client rename
485
/* DRIZZLE */
486
287.3.3 by Monty Taylor
Removed STDCALL macros calls.
487
unsigned int drizzle_field_count(const DRIZZLE *drizzle)
206.3.1 by Patrick Galbraith
Most everything working with client rename
488
{
489
  return drizzle->field_count;
490
}
491
287.3.3 by Monty Taylor
Removed STDCALL macros calls.
492
uint64_t drizzle_affected_rows(const DRIZZLE *drizzle)
206.3.1 by Patrick Galbraith
Most everything working with client rename
493
{
494
  return drizzle->affected_rows;
495
}
496
287.3.3 by Monty Taylor
Removed STDCALL macros calls.
497
uint64_t drizzle_insert_id(const DRIZZLE *drizzle)
206.3.1 by Patrick Galbraith
Most everything working with client rename
498
{
499
  return drizzle->insert_id;
500
}
501
287.3.6 by Monty Taylor
Eeek. Final cleanups.
502
const char * drizzle_sqlstate(const DRIZZLE *drizzle)
206.3.1 by Patrick Galbraith
Most everything working with client rename
503
{
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...
504
  return drizzle ? drizzle->net.sqlstate : sqlstate_get_cant_connect();
206.3.1 by Patrick Galbraith
Most everything working with client rename
505
}
506
287.3.3 by Monty Taylor
Removed STDCALL macros calls.
507
uint32_t drizzle_warning_count(const DRIZZLE *drizzle)
206.3.1 by Patrick Galbraith
Most everything working with client rename
508
{
509
  return drizzle->warning_count;
510
}
511
287.3.6 by Monty Taylor
Eeek. Final cleanups.
512
const char * drizzle_info(const DRIZZLE *drizzle)
206.3.1 by Patrick Galbraith
Most everything working with client rename
513
{
514
  return drizzle->info;
515
}
516
287.3.3 by Monty Taylor
Removed STDCALL macros calls.
517
uint32_t drizzle_thread_id(const DRIZZLE *drizzle)
206.3.1 by Patrick Galbraith
Most everything working with client rename
518
{
236.3.5 by Andrey Hristov
Constify libdrizzle functions
519
  return drizzle->thread_id;
206.3.1 by Patrick Galbraith
Most everything working with client rename
520
}
521
1 by brian
clean slate
522
/****************************************************************************
523
  Some support functions
524
****************************************************************************/
525
526
/*
527
  Functions called my my_net_init() to set some application specific variables
528
*/
529
530
void my_net_local_init(NET *net)
531
{
395 by Brian Aker
Fixed uint/ushort issue in libdrizzle
532
  net->max_packet=   (uint32_t) net_buffer_length;
1 by brian
clean slate
533
  my_net_set_read_timeout(net, CLIENT_NET_READ_TIMEOUT);
534
  my_net_set_write_timeout(net, CLIENT_NET_WRITE_TIMEOUT);
535
  net->retry_count=  1;
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...
536
  net->max_packet_size= (net_buffer_length > max_allowed_packet) ?
537
    net_buffer_length : max_allowed_packet;
1 by brian
clean slate
538
}
539
540
/*
541
  This function is used to create HEX string that you
542
  can use in a SQL statement in of the either ways:
206.3.1 by Patrick Galbraith
Most everything working with client rename
543
    INSERT INTO blob_column VALUES (0xAABBCC);  (any DRIZZLE version)
264.2.4 by Andrey Hristov
Remove support for the old, pre-4.1, protocol
544
    INSERT INTO blob_column VALUES (X'AABBCC'); 
1 by brian
clean slate
545
  
546
  The string in "from" is encoded to a HEX string.
547
  The result is placed in "to" and a terminating null byte is appended.
548
  
549
  The string pointed to by "from" must be "length" bytes long.
550
  You must allocate the "to" buffer to be at least length*2+1 bytes long.
551
  Each character needs two bytes, and you need room for the terminating
206.3.1 by Patrick Galbraith
Most everything working with client rename
552
  null byte. When drizzle_hex_string() returns, the contents of "to" will
1 by brian
clean slate
553
  be a null-terminated string. The return value is the length of the
383.1.38 by Monty Taylor
Reworked drizzle_escape_string.
554
  encoded string, not including the terminating null character.  The return value does not contain any leading 0x or a leading X' and
1 by brian
clean slate
555
  trailing '. The caller must supply whichever of those is desired.
556
*/
557
287.3.3 by Monty Taylor
Removed STDCALL macros calls.
558
uint32_t
206.3.1 by Patrick Galbraith
Most everything working with client rename
559
drizzle_hex_string(char *to, const char *from, uint32_t length)
1 by brian
clean slate
560
{
561
  char *to0= to;
562
  const char *end;
563
            
564
  for (end= from + length; from < end; from++)
565
  {
566
    *to++= _dig_vec_upper[((unsigned char) *from) >> 4];
567
    *to++= _dig_vec_upper[((unsigned char) *from) & 0x0F];
568
  }
569
  *to= '\0';
164 by Brian Aker
Commit cleanup of export types.
570
  return (uint32_t) (to-to0);
1 by brian
clean slate
571
}
572
573
/*
574
  Add escape characters to a string (blob?) to make it suitable for a insert
575
  to should at least have place for length*2+1 chars
576
  Returns the length of the to string
577
*/
578
287.3.3 by Monty Taylor
Removed STDCALL macros calls.
579
uint32_t
206.3.1 by Patrick Galbraith
Most everything working with client rename
580
drizzle_escape_string(char *to,const char *from, uint32_t length)
1 by brian
clean slate
581
{
383.1.38 by Monty Taylor
Reworked drizzle_escape_string.
582
  const char *to_start= to;
583
  const char *end, *to_end=to_start + 2*length;
584
  bool overflow= false;
585
  for (end= from + length; from < end; from++)
586
  {
587
    uint32_t tmp_length;
588
    char escape= 0;
589
    if (!U8_IS_SINGLE(*from))
590
    {
591
      tmp_length= U8_LENGTH(*from);
592
      if (to + tmp_length > to_end)
593
      {
594
        overflow= true;
595
        break;
596
      }
597
      while (tmp_length--)
598
        *to++= *from++;
599
      from--;
600
      continue;
601
    }
602
    switch (*from) {
603
    case 0:                             /* Must be escaped for 'mysql' */
604
      escape= '0';
605
      break;
606
    case '\n':                          /* Must be escaped for logs */
607
      escape= 'n';
608
      break;
609
    case '\r':
610
      escape= 'r';
611
      break;
612
    case '\\':
613
      escape= '\\';
614
      break;
615
    case '\'':
616
      escape= '\'';
617
      break;
618
    case '"':                           /* Better safe than sorry */
619
      escape= '"';
620
      break;
621
    case '\032':                        /* This gives problems on Win32 */
622
      escape= 'Z';
623
      break;
624
    }
625
    if (escape)
626
    {
627
      if (to + 2 > to_end)
628
      {
629
        overflow= true;
630
        break;
631
      }
632
      *to++= '\\';
633
      *to++= escape;
634
    }
635
    else
636
    {
637
      if (to + 1 > to_end)
638
      {
639
        overflow= true;
640
        break;
641
      }
642
      *to++= *from;
643
    }
644
  }
645
  *to= 0;
646
  return overflow ? (size_t) -1 : (size_t) (to - to_start);
1 by brian
clean slate
647
}
648
206.3.1 by Patrick Galbraith
Most everything working with client rename
649
int cli_unbuffered_fetch(DRIZZLE *drizzle, char **row)
1 by brian
clean slate
650
{
206.3.1 by Patrick Galbraith
Most everything working with client rename
651
  if (packet_error == cli_safe_read(drizzle))
1 by brian
clean slate
652
    return 1;
653
236.3.3 by Andrey Hristov
Define a constant, instead of a magic number
654
  *row= ((drizzle->net.read_pos[0] == DRIZZLE_PROTOCOL_NO_MORE_DATA) ? NULL :
206.3.1 by Patrick Galbraith
Most everything working with client rename
655
   (char*) (drizzle->net.read_pos+1));
1 by brian
clean slate
656
  return 0;
657
}
658
659
/********************************************************************
660
 Transactional APIs
661
*********************************************************************/
662
663
/*
664
  Commit the current transaction
665
*/
666
287.3.3 by Monty Taylor
Removed STDCALL macros calls.
667
bool drizzle_commit(DRIZZLE *drizzle)
1 by brian
clean slate
668
{
207.2.3 by Toru Maesaka
Merge from trunk and conflicts resolved
669
  return((bool) drizzle_real_query(drizzle, "commit", 6));
1 by brian
clean slate
670
}
671
672
/*
673
  Rollback the current transaction
674
*/
675
287.3.3 by Monty Taylor
Removed STDCALL macros calls.
676
bool drizzle_rollback(DRIZZLE *drizzle)
1 by brian
clean slate
677
{
207.2.3 by Toru Maesaka
Merge from trunk and conflicts resolved
678
  return((bool) drizzle_real_query(drizzle, "rollback", 8));
1 by brian
clean slate
679
}
680
681
682
/*
683
  Set autocommit to either true or false
684
*/
685
287.3.3 by Monty Taylor
Removed STDCALL macros calls.
686
bool drizzle_autocommit(DRIZZLE *drizzle, bool auto_mode)
1 by brian
clean slate
687
{
207.2.3 by Toru Maesaka
Merge from trunk and conflicts resolved
688
  return((bool) drizzle_real_query(drizzle, auto_mode ?
1 by brian
clean slate
689
                                         "set autocommit=1":"set autocommit=0",
690
                                         16));
691
}
692
693
694
/********************************************************************
695
 Multi query execution + SPs APIs
696
*********************************************************************/
697
698
/*
699
  Returns true/false to indicate whether any more query results exist
206.3.1 by Patrick Galbraith
Most everything working with client rename
700
  to be read using drizzle_next_result()
1 by brian
clean slate
701
*/
702
287.3.3 by Monty Taylor
Removed STDCALL macros calls.
703
bool drizzle_more_results(const DRIZZLE *drizzle)
1 by brian
clean slate
704
{
236.3.3 by Andrey Hristov
Define a constant, instead of a magic number
705
  return (drizzle->server_status & SERVER_MORE_RESULTS_EXISTS) ? true:false;
1 by brian
clean slate
706
}
707
708
709
/*
710
  Reads and returns the next query results
711
*/
287.3.3 by Monty Taylor
Removed STDCALL macros calls.
712
int drizzle_next_result(DRIZZLE *drizzle)
1 by brian
clean slate
713
{
206.3.1 by Patrick Galbraith
Most everything working with client rename
714
  if (drizzle->status != DRIZZLE_STATUS_READY)
1 by brian
clean slate
715
  {
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...
716
    drizzle_set_error(drizzle, CR_COMMANDS_OUT_OF_SYNC, sqlstate_get_unknown());
51.3.5 by Jay Pipes
Merged in from trunk.
717
    return(1);
1 by brian
clean slate
718
  }
719
206.3.1 by Patrick Galbraith
Most everything working with client rename
720
  net_clear_error(&drizzle->net);
721
  drizzle->affected_rows= ~(uint64_t) 0;
722
723
  if (drizzle->server_status & SERVER_MORE_RESULTS_EXISTS)
724
    return((*drizzle->methods->next_result)(drizzle));
725
726
  return(-1);        /* No more results */
727
}
728
729
287.3.3 by Monty Taylor
Removed STDCALL macros calls.
730
DRIZZLE_RES * drizzle_use_result(DRIZZLE *drizzle)
206.3.1 by Patrick Galbraith
Most everything working with client rename
731
{
732
  return (*drizzle->methods->use_result)(drizzle);
733
}
734
287.3.3 by Monty Taylor
Removed STDCALL macros calls.
735
bool drizzle_read_query_result(DRIZZLE *drizzle)
206.3.1 by Patrick Galbraith
Most everything working with client rename
736
{
737
  return (*drizzle->methods->read_query_result)(drizzle);
1 by brian
clean slate
738
}
739