~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to libmysql/libmysql.c

  • Committer: Monty Taylor
  • Date: 2008-07-05 19:00:59 UTC
  • mto: This revision was merged to the branch mainline in revision 63.
  • Revision ID: monty@inaugust.com-20080705190059-3vfbd3ebpmzaw5e5
Fixed unsigned long int, format specifiers and functions. 

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
 */
19
 
 
20
 
#include "libdrizzle.h"
21
 
#include "libdrizzle_priv.h"
 
1
/* Copyright (C) 2000-2004 MySQL AB
 
2
 
 
3
   This program is free software; you can redistribute it and/or modify
 
4
   it under the terms of the GNU General Public License as published by
 
5
   the Free Software Foundation.
 
6
 
 
7
   There are special exceptions to the terms and conditions of the GPL as it
 
8
   is applied to this software. View the full text of the exception in file
 
9
   EXCEPTIONS-CLIENT in the directory of this software distribution.
 
10
 
 
11
   This program is distributed in the hope that it will be useful,
 
12
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
   GNU General Public License for more details.
 
15
 
 
16
   You should have received a copy of the GNU General Public License
 
17
   along with this program; if not, write to the Free Software
 
18
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
 
19
 
 
20
#include <my_global.h>
 
21
#include <my_sys.h>
 
22
#include <my_time.h>
 
23
#include <mysys_err.h>
 
24
#include <m_string.h>
 
25
#include <m_ctype.h>
 
26
#include "mysql.h"
 
27
#include "mysql_version.h"
 
28
#include "mysqld_error.h"
22
29
#include "errmsg.h"
 
30
#include <violite.h>
23
31
#include <sys/stat.h>
24
32
#include <signal.h>
25
33
#include <time.h>
26
 
#ifdef   HAVE_PWD_H
 
34
#ifdef   HAVE_PWD_H
27
35
#include <pwd.h>
28
36
#endif
29
 
 
 
37
#if !defined(MSDOS) && !defined(__WIN__)
30
38
#include <sys/socket.h>
31
39
#include <netinet/in.h>
32
40
#include <arpa/inet.h>
37
45
#ifdef HAVE_SYS_SELECT_H
38
46
#include <sys/select.h>
39
47
#endif
40
 
 
 
48
#endif /* !defined(MSDOS) && !defined(__WIN__) */
41
49
#ifdef HAVE_POLL
42
50
#include <sys/poll.h>
43
51
#endif
44
52
#ifdef HAVE_SYS_UN_H
45
53
#include <sys/un.h>
46
54
#endif
 
55
#include <my_pthread.h>                         /* because of signal()  */
47
56
#ifndef INADDR_NONE
48
 
#define INADDR_NONE  -1
 
57
#define INADDR_NONE     -1
49
58
#endif
50
59
 
51
 
#include <stdlib.h>
52
 
#include <string.h>
53
 
 
54
60
#include <sql_common.h>
55
 
#include <drizzled/version.h>
56
 
 
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
 
 
 
61
#include "client_settings.h"
71
62
 
72
63
#undef net_buffer_length
73
64
#undef max_allowed_packet
74
65
 
75
 
uint32_t     net_buffer_length= 8192;
76
 
uint32_t    max_allowed_packet= 1024L*1024L*1024L;
77
 
 
78
 
unsigned int drizzle_port=0;
79
 
 
 
66
ulong           net_buffer_length=8192;
 
67
ulong           max_allowed_packet= 1024L*1024L*1024L;
 
68
 
 
69
 
 
70
#ifdef EMBEDDED_LIBRARY
 
71
#undef net_flush
 
72
my_bool net_flush(NET *net);
 
73
#endif
 
74
 
 
75
#if defined(MSDOS) || defined(__WIN__)
 
76
/* socket_errno is defined in my_global.h for all platforms */
 
77
#define perror(A)
 
78
#else
80
79
#include <errno.h>
81
 
 
82
 
 
83
 
static DRIZZLE_PARAMETERS drizzle_internal_parameters=
 
80
#define SOCKET_ERROR -1
 
81
#endif /* __WIN__ */
 
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
 
 
90
static void append_wild(char *to,char *end,const char *wild);
 
91
sig_handler my_pipe_sig_handler(int sig);
 
92
 
 
93
static my_bool mysql_client_init= 0;
 
94
static my_bool org_my_init_done= 0;
 
95
 
 
96
 
 
97
/*
 
98
  Initialize the MySQL client library
 
99
 
 
100
  SYNOPSIS
 
101
    mysql_server_init()
 
102
 
 
103
  NOTES
 
104
    Should be called before doing any other calls to the MySQL
 
105
    client library to initialize thread specific variables etc.
 
106
    It's called by mysql_init() to ensure that things will work for
 
107
    old not threaded applications that doesn't call mysql_server_init()
 
108
    directly.
 
109
 
 
110
  RETURN
 
111
    0  ok
 
112
    1  could not initialize environment (out of memory or thread keys)
 
113
*/
 
114
 
 
115
int STDCALL mysql_server_init(int argc __attribute__((unused)),
 
116
                              char **argv __attribute__((unused)),
 
117
                              char **groups __attribute__((unused)))
 
118
{
 
119
  int result= 0;
 
120
  if (!mysql_client_init)
 
121
  {
 
122
    mysql_client_init=1;
 
123
    org_my_init_done=my_init_done;
 
124
    if (my_init())                              /* Will init threads */
 
125
      return 1;
 
126
    init_client_errs();
 
127
    if (!mysql_port)
 
128
    {
 
129
      mysql_port = MYSQL_PORT;
 
130
#ifndef MSDOS
 
131
      {
 
132
        struct servent *serv_ptr;
 
133
        char    *env;
 
134
 
 
135
        /*
 
136
          if builder specifically requested a default port, use that
 
137
          (even if it coincides with our factory default).
 
138
          only if they didn't do we check /etc/services (and, failing
 
139
          on that, fall back to the factory default of 3306).
 
140
          either default can be overridden by the environment variable
 
141
          MYSQL_TCP_PORT, which in turn can be overridden with command
 
142
          line options.
 
143
        */
 
144
 
 
145
#if MYSQL_PORT_DEFAULT == 0
 
146
        if ((serv_ptr = getservbyname("mysql", "tcp")))
 
147
          mysql_port = (uint) ntohs((ushort) serv_ptr->s_port);
 
148
#endif
 
149
        if ((env = getenv("MYSQL_TCP_PORT")))
 
150
          mysql_port =(uint) atoi(env);
 
151
      }
 
152
#endif
 
153
    }
 
154
    if (!mysql_unix_port)
 
155
    {
 
156
      char *env;
 
157
      mysql_unix_port = (char*) MYSQL_UNIX_ADDR;
 
158
      if ((env = getenv("MYSQL_UNIX_PORT")))
 
159
        mysql_unix_port = env;
 
160
    }
 
161
    mysql_debug(NullS);
 
162
#if defined(SIGPIPE)
 
163
    (void) signal(SIGPIPE, SIG_IGN);
 
164
#endif
 
165
  }
 
166
  else
 
167
    result= (int)my_thread_init();         /* Init if new thread */
 
168
  return result;
 
169
}
 
170
 
 
171
 
 
172
/*
 
173
  Free all memory and resources used by the client library
 
174
 
 
175
  NOTES
 
176
    When calling this there should not be any other threads using
 
177
    the library.
 
178
 
 
179
    To make things simpler when used with windows dll's (which calls this
 
180
    function automaticly), it's safe to call this function multiple times.
 
181
*/
 
182
 
 
183
 
 
184
void STDCALL mysql_server_end()
 
185
{
 
186
  if (!mysql_client_init)
 
187
    return;
 
188
 
 
189
  finish_client_errs();
 
190
  vio_end();
 
191
 
 
192
  /* If library called my_init(), free memory allocated by it */
 
193
  if (!org_my_init_done)
 
194
  {
 
195
    my_end(MY_DONT_FREE_DBUG);
 
196
    /* Remove TRACING, if enabled by mysql_debug() */
 
197
    DBUG_POP();
 
198
  }
 
199
  else
 
200
  {
 
201
    free_charsets();
 
202
    mysql_thread_end();
 
203
  }
 
204
 
 
205
  mysql_client_init= org_my_init_done= 0;
 
206
#ifdef EMBEDDED_SERVER
 
207
  if (stderror_file)
 
208
  {
 
209
    fclose(stderror_file);
 
210
    stderror_file= 0;
 
211
  }
 
212
#endif
 
213
}
 
214
 
 
215
static MYSQL_PARAMETERS mysql_internal_parameters=
84
216
{&max_allowed_packet, &net_buffer_length, 0};
85
217
 
86
 
const DRIZZLE_PARAMETERS * drizzle_get_parameters(void)
87
 
{
88
 
  return &drizzle_internal_parameters;
89
 
}
90
 
 
91
 
unsigned int drizzle_get_default_port(void)
92
 
{
93
 
  return drizzle_port;
94
 
}
95
 
 
96
 
void drizzle_set_default_port(unsigned int port)
97
 
{
98
 
  drizzle_port= port;
99
 
}
 
218
MYSQL_PARAMETERS *STDCALL mysql_get_parameters(void)
 
219
{
 
220
  return &mysql_internal_parameters;
 
221
}
 
222
 
 
223
my_bool STDCALL mysql_thread_init()
 
224
{
 
225
  return my_thread_init();
 
226
}
 
227
 
 
228
void STDCALL mysql_thread_end()
 
229
{
 
230
  my_thread_end();
 
231
}
 
232
 
100
233
 
101
234
/*
102
235
  Expand wildcard to a sql string
105
238
static void
106
239
append_wild(char *to, char *end, const char *wild)
107
240
{
108
 
  end-=5;          /* Some extra */
 
241
  end-=5;                                       /* Some extra */
109
242
  if (wild && wild[0])
110
243
  {
111
 
    to= strcpy(to," like '");
112
 
    to+= 7; /* strlen(" like '"); */
113
 
 
 
244
    to=strmov(to," like '");
114
245
    while (*wild && to < end)
115
246
    {
116
247
      if (*wild == '\\' || *wild == '\'')
117
 
  *to++='\\';
 
248
        *to++='\\';
118
249
      *to++= *wild++;
119
250
    }
120
 
    if (*wild)          /* Too small buffer */
121
 
      *to++='%';        /* Nicer this way */
 
251
    if (*wild)                                  /* Too small buffer */
 
252
      *to++='%';                                /* Nicer this way */
122
253
    to[0]='\'';
123
254
    to[1]=0;
124
255
  }
125
256
}
126
257
 
 
258
 
 
259
/**************************************************************************
 
260
  Init debugging if MYSQL_DEBUG environment variable is found
 
261
**************************************************************************/
 
262
 
 
263
void STDCALL
 
264
mysql_debug(const char *debug __attribute__((unused)))
 
265
{
 
266
#ifndef DBUG_OFF
 
267
  char  *env;
 
268
  if (debug)
 
269
  {
 
270
    DBUG_PUSH(debug);
 
271
  }
 
272
  else if ((env = getenv("MYSQL_DEBUG")))
 
273
  {
 
274
    DBUG_PUSH(env);
 
275
#if !defined(_WINVER) && !defined(WINVER)
 
276
    puts("\n-------------------------------------------------------");
 
277
    puts("MYSQL_DEBUG found. libmysql started with the following:");
 
278
    puts(env);
 
279
    puts("-------------------------------------------------------\n");
 
280
#else
 
281
    {
 
282
      char buff[80];
 
283
      buff[sizeof(buff)-1]= 0;
 
284
      strxnmov(buff,sizeof(buff)-1,"libmysql: ", env, NullS);
 
285
      MessageBox((HWND) 0,"Debugging variable MYSQL_DEBUG used",buff,MB_OK);
 
286
    }
 
287
#endif
 
288
  }
 
289
#endif
 
290
}
 
291
 
 
292
 
 
293
/**************************************************************************
 
294
  Ignore SIGPIPE handler
 
295
   ARGSUSED
 
296
**************************************************************************/
 
297
 
 
298
sig_handler
 
299
my_pipe_sig_handler(int sig __attribute__((unused)))
 
300
{
 
301
  DBUG_PRINT("info",("Hit by signal %d",sig));
 
302
#ifdef DONT_REMEMBER_SIGNAL
 
303
  (void) signal(SIGPIPE, my_pipe_sig_handler);
 
304
#endif
 
305
}
 
306
 
 
307
 
 
308
/**************************************************************************
 
309
  Connect to sql server
 
310
  If host == 0 then use localhost
 
311
**************************************************************************/
 
312
 
 
313
#ifdef USE_OLD_FUNCTIONS
 
314
MYSQL * STDCALL
 
315
mysql_connect(MYSQL *mysql,const char *host,
 
316
              const char *user, const char *passwd)
 
317
{
 
318
  MYSQL *res;
 
319
  mysql=mysql_init(mysql);                      /* Make it thread safe */
 
320
  {
 
321
    DBUG_ENTER("mysql_connect");
 
322
    if (!(res=mysql_real_connect(mysql,host,user,passwd,NullS,0,NullS,0)))
 
323
    {
 
324
      if (mysql->free_me)
 
325
        my_free((uchar*) mysql,MYF(0));
 
326
    }
 
327
    mysql->reconnect= 1;
 
328
    DBUG_RETURN(res);
 
329
  }
 
330
}
 
331
#endif
 
332
 
 
333
 
127
334
/**************************************************************************
128
335
  Change user and database
129
336
**************************************************************************/
130
337
 
131
 
int cli_read_change_user_result(DRIZZLE *drizzle)
 
338
int cli_read_change_user_result(MYSQL *mysql, char *buff, const char *passwd)
132
339
{
133
 
  uint32_t pkt_length;
 
340
  NET *net= &mysql->net;
 
341
  ulong pkt_length;
134
342
 
135
 
  pkt_length= cli_safe_read(drizzle);
 
343
  pkt_length= cli_safe_read(mysql);
136
344
  
137
345
  if (pkt_length == packet_error)
138
346
    return 1;
139
347
 
 
348
  if (pkt_length == 1 && net->read_pos[0] == 254 &&
 
349
      mysql->server_capabilities & CLIENT_SECURE_CONNECTION)
 
350
  {
 
351
    /*
 
352
      By sending this very specific reply server asks us to send scrambled
 
353
      password in old format. The reply contains scramble_323.
 
354
    */
 
355
    scramble_323(buff, mysql->scramble, passwd);
 
356
    if (my_net_write(net, (uchar*) buff, SCRAMBLE_LENGTH_323 + 1) ||
 
357
        net_flush(net))
 
358
    {
 
359
      set_mysql_error(mysql, CR_SERVER_LOST, unknown_sqlstate);
 
360
      return 1;
 
361
    }
 
362
    /* Read what server thinks about out new auth message report */
 
363
    if (cli_safe_read(mysql) == packet_error)
 
364
      return 1;
 
365
  }
140
366
  return 0;
141
367
}
142
368
 
143
 
bool drizzle_change_user(DRIZZLE *drizzle, const char *user,
144
 
                                 const char *passwd, const char *db)
 
369
my_bool STDCALL mysql_change_user(MYSQL *mysql, const char *user,
 
370
                                  const char *passwd, const char *db)
145
371
{
146
372
  char buff[USERNAME_LENGTH+SCRAMBLED_PASSWORD_CHAR_LENGTH+NAME_LEN+2];
147
373
  char *end= buff;
148
374
  int rc;
 
375
  CHARSET_INFO *saved_cs= mysql->charset;
 
376
 
 
377
  DBUG_ENTER("mysql_change_user");
 
378
 
 
379
  /* Get the connection-default character set. */
 
380
 
 
381
  if (mysql_init_character_set(mysql))
 
382
  {
 
383
    mysql->charset= saved_cs;
 
384
    DBUG_RETURN(TRUE);
 
385
  }
149
386
 
150
387
  /* Use an empty string instead of NULL. */
151
388
 
155
392
    passwd="";
156
393
 
157
394
  /* Store user into the buffer */
158
 
  end= strncpy(end, user, USERNAME_LENGTH) + USERNAME_LENGTH + 1;
 
395
  end= strmake(end, user, USERNAME_LENGTH) + 1;
159
396
 
160
397
  /* write scrambled password according to server capabilities */
161
398
  if (passwd[0])
162
399
  {
 
400
    if (mysql->server_capabilities & CLIENT_SECURE_CONNECTION)
163
401
    {
164
402
      *end++= SCRAMBLE_LENGTH;
 
403
      scramble(end, mysql->scramble, passwd);
165
404
      end+= SCRAMBLE_LENGTH;
166
405
    }
 
406
    else
 
407
    {
 
408
      scramble_323(end, mysql->scramble, passwd);
 
409
      end+= SCRAMBLE_LENGTH_323 + 1;
 
410
    }
167
411
  }
168
412
  else
169
413
    *end++= '\0';                               /* empty password */
170
414
  /* Add database if needed */
171
 
  end= strncpy(end, db ? db : "", NAME_LEN) + NAME_LEN + 1;
 
415
  end= strmake(end, db ? db : "", NAME_LEN) + 1;
172
416
 
173
417
  /* Add character set number. */
174
 
  if (drizzle->server_capabilities & CLIENT_SECURE_CONNECTION)
 
418
 
 
419
  if (mysql->server_capabilities & CLIENT_SECURE_CONNECTION)
175
420
  {
176
 
    int2store(end, (uint16_t) 45); // utf8mb4 number from mystrings/ctype-utf8.c
 
421
    int2store(end, (ushort) mysql->charset->number);
177
422
    end+= 2;
178
423
  }
179
424
 
180
425
  /* Write authentication package */
181
 
  (void)simple_command(drizzle,COM_CHANGE_USER, (unsigned char*) buff, (uint32_t) (end-buff), 1);
 
426
  simple_command(mysql,COM_CHANGE_USER, (uchar*) buff, (ulong) (end-buff), 1);
182
427
 
183
 
  rc= (*drizzle->methods->read_change_user_result)(drizzle);
 
428
  rc= (*mysql->methods->read_change_user_result)(mysql, buff, passwd);
184
429
 
185
430
  if (rc == 0)
186
431
  {
187
432
    /* Free old connect information */
188
 
    if(drizzle->user)
189
 
      free(drizzle->user);
190
 
    if(drizzle->passwd)
191
 
      free(drizzle->passwd);
192
 
    if(drizzle->db)
193
 
      free(drizzle->db);
 
433
    my_free(mysql->user,MYF(MY_ALLOW_ZERO_PTR));
 
434
    my_free(mysql->passwd,MYF(MY_ALLOW_ZERO_PTR));
 
435
    my_free(mysql->db,MYF(MY_ALLOW_ZERO_PTR));
194
436
 
195
437
    /* alloc new connect information */
196
 
    drizzle->user= strdup(user);
197
 
    drizzle->passwd= strdup(passwd);
198
 
    drizzle->db= db ? strdup(db) : 0;
 
438
    mysql->user=  my_strdup(user,MYF(MY_WME));
 
439
    mysql->passwd=my_strdup(passwd,MYF(MY_WME));
 
440
    mysql->db=    db ? my_strdup(db,MYF(MY_WME)) : 0;
 
441
  }
 
442
  else
 
443
  {
 
444
    mysql->charset= saved_cs;
199
445
  }
200
446
 
201
 
  return(rc);
 
447
  DBUG_RETURN(rc);
202
448
}
203
449
 
204
450
#if defined(HAVE_GETPWUID) && defined(NO_GETPWUID_DECL)
206
452
char* getlogin(void);
207
453
#endif
208
454
 
 
455
void read_user_name(char *name)
 
456
{
 
457
  DBUG_ENTER("read_user_name");
 
458
  if (geteuid() == 0)
 
459
    (void) strmov(name,"root");         /* allow use of surun */
 
460
  else
 
461
  {
 
462
#ifdef HAVE_GETPWUID
 
463
    struct passwd *skr;
 
464
    const char *str;
 
465
    if ((str=getlogin()) == NULL)
 
466
    {
 
467
      if ((skr=getpwuid(geteuid())) != NULL)
 
468
        str=skr->pw_name;
 
469
      else if (!(str=getenv("USER")) && !(str=getenv("LOGNAME")) &&
 
470
               !(str=getenv("LOGIN")))
 
471
        str="UNKNOWN_USER";
 
472
    }
 
473
    (void) strmake(name,str,USERNAME_LENGTH);
 
474
#elif HAVE_CUSERID
 
475
    (void) cuserid(name);
 
476
#else
 
477
    strmov(name,"UNKNOWN_USER");
 
478
#endif
 
479
  }
 
480
  DBUG_VOID_RETURN;
 
481
}
 
482
 
 
483
my_bool handle_local_infile(MYSQL *mysql, const char *net_filename)
 
484
{
 
485
  my_bool result= 1;
 
486
  uint packet_length=MY_ALIGN(mysql->net.max_packet-16,IO_SIZE);
 
487
  NET *net= &mysql->net;
 
488
  int readcount;
 
489
  void *li_ptr;          /* pass state to local_infile functions */
 
490
  char *buf;            /* buffer to be filled by local_infile_read */
 
491
  struct st_mysql_options *options= &mysql->options;
 
492
  DBUG_ENTER("handle_local_infile");
 
493
 
 
494
  /* check that we've got valid callback functions */
 
495
  if (!(options->local_infile_init &&
 
496
        options->local_infile_read &&
 
497
        options->local_infile_end &&
 
498
        options->local_infile_error))
 
499
  {
 
500
    /* if any of the functions is invalid, set the default */
 
501
    mysql_set_local_infile_default(mysql);
 
502
  }
 
503
 
 
504
  /* copy filename into local memory and allocate read buffer */
 
505
  if (!(buf=my_malloc(packet_length, MYF(0))))
 
506
  {
 
507
    set_mysql_error(mysql, CR_OUT_OF_MEMORY, unknown_sqlstate);
 
508
    DBUG_RETURN(1);
 
509
  }
 
510
 
 
511
  /* initialize local infile (open file, usually) */
 
512
  if ((*options->local_infile_init)(&li_ptr, net_filename,
 
513
    options->local_infile_userdata))
 
514
  {
 
515
    VOID(my_net_write(net,(const uchar*) "",0)); /* Server needs one packet */
 
516
    net_flush(net);
 
517
    strmov(net->sqlstate, unknown_sqlstate);
 
518
    net->last_errno=
 
519
      (*options->local_infile_error)(li_ptr,
 
520
                                     net->last_error,
 
521
                                     sizeof(net->last_error)-1);
 
522
    goto err;
 
523
  }
 
524
 
 
525
  /* read blocks of data from local infile callback */
 
526
  while ((readcount =
 
527
          (*options->local_infile_read)(li_ptr, buf,
 
528
                                        packet_length)) > 0)
 
529
  {
 
530
    if (my_net_write(net, (uchar*) buf, readcount))
 
531
    {
 
532
      DBUG_PRINT("error",
 
533
                 ("Lost connection to MySQL server during LOAD DATA of local file"));
 
534
      set_mysql_error(mysql, CR_SERVER_LOST, unknown_sqlstate);
 
535
      goto err;
 
536
    }
 
537
  }
 
538
 
 
539
  /* Send empty packet to mark end of file */
 
540
  if (my_net_write(net, (const uchar*) "", 0) || net_flush(net))
 
541
  {
 
542
    set_mysql_error(mysql, CR_SERVER_LOST, unknown_sqlstate);
 
543
    goto err;
 
544
  }
 
545
 
 
546
  if (readcount < 0)
 
547
  {
 
548
    net->last_errno=
 
549
      (*options->local_infile_error)(li_ptr,
 
550
                                     net->last_error,
 
551
                                     sizeof(net->last_error)-1);
 
552
    goto err;
 
553
  }
 
554
 
 
555
  result=0;                                     /* Ok */
 
556
 
 
557
err:
 
558
  /* free up memory allocated with _init, usually */
 
559
  (*options->local_infile_end)(li_ptr);
 
560
  my_free(buf, MYF(0));
 
561
  DBUG_RETURN(result);
 
562
}
 
563
 
 
564
 
 
565
/****************************************************************************
 
566
  Default handlers for LOAD LOCAL INFILE
 
567
****************************************************************************/
 
568
 
 
569
typedef struct st_default_local_infile
 
570
{
 
571
  int fd;
 
572
  int error_num;
 
573
  const char *filename;
 
574
  char error_msg[LOCAL_INFILE_ERROR_LEN];
 
575
} default_local_infile_data;
 
576
 
 
577
 
 
578
/*
 
579
  Open file for LOAD LOCAL INFILE
 
580
 
 
581
  SYNOPSIS
 
582
    default_local_infile_init()
 
583
    ptr                 Store pointer to internal data here
 
584
    filename            File name to open. This may be in unix format !
 
585
 
 
586
 
 
587
  NOTES
 
588
    Even if this function returns an error, the load data interface
 
589
    guarantees that default_local_infile_end() is called.
 
590
 
 
591
  RETURN
 
592
    0   ok
 
593
    1   error
 
594
*/
 
595
 
 
596
static int default_local_infile_init(void **ptr, const char *filename,
 
597
             void *userdata __attribute__ ((unused)))
 
598
{
 
599
  default_local_infile_data *data;
 
600
  char tmp_name[FN_REFLEN];
 
601
 
 
602
  if (!(*ptr= data= ((default_local_infile_data *)
 
603
                     my_malloc(sizeof(default_local_infile_data),  MYF(0)))))
 
604
    return 1; /* out of memory */
 
605
 
 
606
  data->error_msg[0]= 0;
 
607
  data->error_num=    0;
 
608
  data->filename= filename;
 
609
 
 
610
  fn_format(tmp_name, filename, "", "", MY_UNPACK_FILENAME);
 
611
  if ((data->fd = my_open(tmp_name, O_RDONLY, MYF(0))) < 0)
 
612
  {
 
613
    data->error_num= my_errno;
 
614
    my_snprintf(data->error_msg, sizeof(data->error_msg)-1,
 
615
                EE(EE_FILENOTFOUND), tmp_name, data->error_num);
 
616
    return 1;
 
617
  }
 
618
  return 0; /* ok */
 
619
}
 
620
 
 
621
 
 
622
/*
 
623
  Read data for LOAD LOCAL INFILE
 
624
 
 
625
  SYNOPSIS
 
626
    default_local_infile_read()
 
627
    ptr                 Points to handle allocated by _init
 
628
    buf                 Read data here
 
629
    buf_len             Ammount of data to read
 
630
 
 
631
  RETURN
 
632
    > 0         number of bytes read
 
633
    == 0        End of data
 
634
    < 0         Error
 
635
*/
 
636
 
 
637
static int default_local_infile_read(void *ptr, char *buf, uint buf_len)
 
638
{
 
639
  int count;
 
640
  default_local_infile_data*data = (default_local_infile_data *) ptr;
 
641
 
 
642
  if ((count= (int) my_read(data->fd, (uchar *) buf, buf_len, MYF(0))) < 0)
 
643
  {
 
644
    data->error_num= EE_READ; /* the errmsg for not entire file read */
 
645
    my_snprintf(data->error_msg, sizeof(data->error_msg)-1,
 
646
                EE(EE_READ),
 
647
                data->filename, my_errno);
 
648
  }
 
649
  return count;
 
650
}
 
651
 
 
652
 
 
653
/*
 
654
  Read data for LOAD LOCAL INFILE
 
655
 
 
656
  SYNOPSIS
 
657
    default_local_infile_end()
 
658
    ptr                 Points to handle allocated by _init
 
659
                        May be NULL if _init failed!
 
660
 
 
661
  RETURN
 
662
*/
 
663
 
 
664
static void default_local_infile_end(void *ptr)
 
665
{
 
666
  default_local_infile_data *data= (default_local_infile_data *) ptr;
 
667
  if (data)                                     /* If not error on open */
 
668
  {
 
669
    if (data->fd >= 0)
 
670
      my_close(data->fd, MYF(MY_WME));
 
671
    my_free(ptr, MYF(MY_WME));
 
672
  }
 
673
}
 
674
 
 
675
 
 
676
/*
 
677
  Return error from LOAD LOCAL INFILE
 
678
 
 
679
  SYNOPSIS
 
680
    default_local_infile_end()
 
681
    ptr                 Points to handle allocated by _init
 
682
                        May be NULL if _init failed!
 
683
    error_msg           Store error text here
 
684
    error_msg_len       Max lenght of error_msg
 
685
 
 
686
  RETURN
 
687
    error message number
 
688
*/
 
689
 
 
690
static int
 
691
default_local_infile_error(void *ptr, char *error_msg, uint error_msg_len)
 
692
{
 
693
  default_local_infile_data *data = (default_local_infile_data *) ptr;
 
694
  if (data)                                     /* If not error on open */
 
695
  {
 
696
    strmake(error_msg, data->error_msg, error_msg_len);
 
697
    return data->error_num;
 
698
  }
 
699
  /* This can only happen if we got error on malloc of handle */
 
700
  strmov(error_msg, ER(CR_OUT_OF_MEMORY));
 
701
  return CR_OUT_OF_MEMORY;
 
702
}
 
703
 
 
704
 
 
705
void
 
706
mysql_set_local_infile_handler(MYSQL *mysql,
 
707
                               int (*local_infile_init)(void **, const char *,
 
708
                               void *),
 
709
                               int (*local_infile_read)(void *, char *, uint),
 
710
                               void (*local_infile_end)(void *),
 
711
                               int (*local_infile_error)(void *, char *, uint),
 
712
                               void *userdata)
 
713
{
 
714
  mysql->options.local_infile_init=  local_infile_init;
 
715
  mysql->options.local_infile_read=  local_infile_read;
 
716
  mysql->options.local_infile_end=   local_infile_end;
 
717
  mysql->options.local_infile_error= local_infile_error;
 
718
  mysql->options.local_infile_userdata = userdata;
 
719
}
 
720
 
 
721
 
 
722
void mysql_set_local_infile_default(MYSQL *mysql)
 
723
{
 
724
  mysql->options.local_infile_init=  default_local_infile_init;
 
725
  mysql->options.local_infile_read=  default_local_infile_read;
 
726
  mysql->options.local_infile_end=   default_local_infile_end;
 
727
  mysql->options.local_infile_error= default_local_infile_error;
 
728
}
 
729
 
 
730
 
209
731
/**************************************************************************
210
732
  Do a query. If query returned rows, free old rows.
211
 
  Read data by drizzle_store_result or by repeat call of drizzle_fetch_row
 
733
  Read data by mysql_store_result or by repeat call of mysql_fetch_row
212
734
**************************************************************************/
213
735
 
214
 
int
215
 
drizzle_query(DRIZZLE *drizzle, const char *query)
 
736
int STDCALL
 
737
mysql_query(MYSQL *mysql, const char *query)
216
738
{
217
 
  return drizzle_real_query(drizzle,query, (uint32_t) strlen(query));
 
739
  return mysql_real_query(mysql,query, (uint) strlen(query));
218
740
}
219
741
 
220
742
 
222
744
  Return next field of the query results
223
745
**************************************************************************/
224
746
 
225
 
DRIZZLE_FIELD *
226
 
drizzle_fetch_field(DRIZZLE_RES *result)
 
747
MYSQL_FIELD * STDCALL
 
748
mysql_fetch_field(MYSQL_RES *result)
227
749
{
228
750
  if (result->current_field >= result->field_count)
229
751
    return(NULL);
235
757
  Move to a specific row and column
236
758
**************************************************************************/
237
759
 
238
 
void
239
 
drizzle_data_seek(DRIZZLE_RES *result, uint64_t row)
 
760
void STDCALL
 
761
mysql_data_seek(MYSQL_RES *result, my_ulonglong row)
240
762
{
241
 
  DRIZZLE_ROWS  *tmp=0;
 
763
  MYSQL_ROWS    *tmp=0;
 
764
  DBUG_PRINT("info",("mysql_data_seek(%ld)",(long) row));
242
765
  if (result->data)
243
766
    for (tmp=result->data->data; row-- && tmp ; tmp = tmp->next) ;
244
767
  result->current_row=0;
247
770
 
248
771
 
249
772
/*************************************************************************
250
 
  put the row or field cursor one a position one got from DRIZZLE_ROW_tell()
251
 
  This doesn't restore any data. The next drizzle_fetch_row or
252
 
  drizzle_fetch_field will return the next row or field after the last used
 
773
  put the row or field cursor one a position one got from mysql_row_tell()
 
774
  This doesn't restore any data. The next mysql_fetch_row or
 
775
  mysql_fetch_field will return the next row or field after the last used
253
776
*************************************************************************/
254
777
 
255
 
DRIZZLE_ROW_OFFSET
256
 
drizzle_row_seek(DRIZZLE_RES *result, DRIZZLE_ROW_OFFSET row)
 
778
MYSQL_ROW_OFFSET STDCALL
 
779
mysql_row_seek(MYSQL_RES *result, MYSQL_ROW_OFFSET row)
257
780
{
258
 
  DRIZZLE_ROW_OFFSET return_value=result->data_cursor;
 
781
  MYSQL_ROW_OFFSET return_value=result->data_cursor;
259
782
  result->current_row= 0;
260
783
  result->data_cursor= row;
261
784
  return return_value;
262
785
}
263
786
 
264
787
 
265
 
DRIZZLE_FIELD_OFFSET
266
 
drizzle_field_seek(DRIZZLE_RES *result, DRIZZLE_FIELD_OFFSET field_offset)
 
788
MYSQL_FIELD_OFFSET STDCALL
 
789
mysql_field_seek(MYSQL_RES *result, MYSQL_FIELD_OFFSET field_offset)
267
790
{
268
 
  DRIZZLE_FIELD_OFFSET return_value=result->current_field;
 
791
  MYSQL_FIELD_OFFSET return_value=result->current_field;
269
792
  result->current_field=field_offset;
270
793
  return return_value;
271
794
}
272
795
 
273
796
 
274
797
/*****************************************************************************
 
798
  List all databases
 
799
*****************************************************************************/
 
800
 
 
801
MYSQL_RES * STDCALL
 
802
mysql_list_dbs(MYSQL *mysql, const char *wild)
 
803
{
 
804
  char buff[255];
 
805
  DBUG_ENTER("mysql_list_dbs");
 
806
 
 
807
  append_wild(strmov(buff,"show databases"),buff+sizeof(buff),wild);
 
808
  if (mysql_query(mysql,buff))
 
809
    DBUG_RETURN(0);
 
810
  DBUG_RETURN (mysql_store_result(mysql));
 
811
}
 
812
 
 
813
 
 
814
/*****************************************************************************
275
815
  List all tables in a database
276
816
  If wild is given then only the tables matching wild is returned
277
817
*****************************************************************************/
278
818
 
279
 
DRIZZLE_RES *
280
 
drizzle_list_tables(DRIZZLE *drizzle, const char *wild)
 
819
MYSQL_RES * STDCALL
 
820
mysql_list_tables(MYSQL *mysql, const char *wild)
281
821
{
282
822
  char buff[255];
283
 
  char *ptr= strcpy(buff, "show tables");
284
 
  ptr+= 11; /* strlen("show tables"); */
 
823
  DBUG_ENTER("mysql_list_tables");
285
824
 
286
 
  append_wild(ptr,buff+sizeof(buff),wild);
287
 
  if (drizzle_query(drizzle,buff))
288
 
    return(0);
289
 
  return (drizzle_store_result(drizzle));
 
825
  append_wild(strmov(buff,"show tables"),buff+sizeof(buff),wild);
 
826
  if (mysql_query(mysql,buff))
 
827
    DBUG_RETURN(0);
 
828
  DBUG_RETURN (mysql_store_result(mysql));
290
829
}
291
830
 
292
831
 
293
 
DRIZZLE_FIELD *cli_list_fields(DRIZZLE *drizzle)
 
832
MYSQL_FIELD *cli_list_fields(MYSQL *mysql)
294
833
{
295
 
  DRIZZLE_DATA *query;
296
 
  if (!(query= cli_read_rows(drizzle,(DRIZZLE_FIELD*) 0, 8)))
 
834
  MYSQL_DATA *query;
 
835
  if (!(query= cli_read_rows(mysql,(MYSQL_FIELD*) 0, 
 
836
                             protocol_41(mysql) ? 8 : 6)))
297
837
    return NULL;
298
838
 
299
 
  drizzle->field_count= (uint32_t) query->rows;
300
 
  return unpack_fields(query, drizzle->field_count, 1);
 
839
  mysql->field_count= (uint) query->rows;
 
840
  return unpack_fields(query,&mysql->field_alloc,
 
841
                       mysql->field_count, 1, mysql->server_capabilities);
301
842
}
302
843
 
303
844
 
308
849
  show fields in 'table' like "wild"
309
850
**************************************************************************/
310
851
 
311
 
DRIZZLE_RES *
312
 
drizzle_list_fields(DRIZZLE *drizzle, const char *table, const char *wild)
 
852
MYSQL_RES * STDCALL
 
853
mysql_list_fields(MYSQL *mysql, const char *table, const char *wild)
313
854
{
314
 
  DRIZZLE_RES   *result;
315
 
  DRIZZLE_FIELD *fields;
316
 
  char buff[257], *end;
317
 
 
318
 
  end= strncpy(buff, table, 128) + 128;
319
 
  end= strncpy(end+1, wild ? wild : "", 128) + 128;
320
 
 
321
 
  free_old_query(drizzle);
322
 
  if (simple_command(drizzle, COM_FIELD_LIST, (unsigned char*) buff,
323
 
                     (uint32_t) (end-buff), 1) ||
324
 
      !(fields= (*drizzle->methods->list_fields)(drizzle)))
325
 
    return(NULL);
326
 
 
327
 
  if (!(result = (DRIZZLE_RES *) malloc(sizeof(DRIZZLE_RES))))
328
 
    return(NULL);
329
 
 
330
 
  memset(result, 0, sizeof(DRIZZLE_RES));
331
 
 
332
 
  result->methods= drizzle->methods;
333
 
  drizzle->fields=0;
334
 
  result->field_count = drizzle->field_count;
 
855
  MYSQL_RES   *result;
 
856
  MYSQL_FIELD *fields;
 
857
  char       buff[257],*end;
 
858
  DBUG_ENTER("mysql_list_fields");
 
859
  DBUG_PRINT("enter",("table: '%s'  wild: '%s'",table,wild ? wild : ""));
 
860
 
 
861
  end=strmake(strmake(buff, table,128)+1,wild ? wild : "",128);
 
862
  free_old_query(mysql);
 
863
  if (simple_command(mysql, COM_FIELD_LIST, (uchar*) buff,
 
864
                     (ulong) (end-buff), 1) ||
 
865
      !(fields= (*mysql->methods->list_fields)(mysql)))
 
866
    DBUG_RETURN(NULL);
 
867
 
 
868
  if (!(result = (MYSQL_RES *) my_malloc(sizeof(MYSQL_RES),
 
869
                                         MYF(MY_WME | MY_ZEROFILL))))
 
870
    DBUG_RETURN(NULL);
 
871
 
 
872
  result->methods= mysql->methods;
 
873
  result->field_alloc=mysql->field_alloc;
 
874
  mysql->fields=0;
 
875
  result->field_count = mysql->field_count;
335
876
  result->fields= fields;
336
877
  result->eof=1;
337
 
  return(result);
 
878
  DBUG_RETURN(result);
338
879
}
339
880
 
340
881
/* List all running processes (threads) in server */
341
882
 
342
 
DRIZZLE_RES *
343
 
drizzle_list_processes(DRIZZLE *drizzle)
344
 
{
345
 
  DRIZZLE_DATA *fields;
346
 
  uint32_t field_count;
347
 
  unsigned char *pos;
348
 
 
349
 
  if (simple_command(drizzle,COM_PROCESS_INFO,0,0,0))
350
 
    return(0);
351
 
  free_old_query(drizzle);
352
 
  pos=(unsigned char*) drizzle->net.read_pos;
353
 
  field_count=(uint32_t) net_field_length(&pos);
354
 
  if (!(fields = (*drizzle->methods->read_rows)(drizzle,(DRIZZLE_FIELD*) 0, 7)))
355
 
    return(NULL);
356
 
  if (!(drizzle->fields=unpack_fields(fields, field_count, 0)))
357
 
    return(0);
358
 
  drizzle->status=DRIZZLE_STATUS_GET_RESULT;
359
 
  drizzle->field_count=field_count;
360
 
  return(drizzle_store_result(drizzle));
361
 
}
362
 
 
363
 
 
364
 
int
365
 
drizzle_shutdown(DRIZZLE *drizzle)
366
 
{
367
 
  return(simple_command(drizzle, COM_SHUTDOWN, 0, 0, 0));
368
 
}
369
 
 
370
 
 
371
 
int
372
 
drizzle_refresh(DRIZZLE *drizzle, uint32_t options)
373
 
{
374
 
  unsigned char bits[1];
375
 
  bits[0]= (unsigned char) options;
376
 
  return(simple_command(drizzle, COM_REFRESH, bits, 1, 0));
377
 
}
378
 
 
379
 
 
380
 
int32_t
381
 
drizzle_kill(DRIZZLE *drizzle, uint32_t pid)
382
 
{
383
 
  unsigned char buff[4];
 
883
MYSQL_RES * STDCALL
 
884
mysql_list_processes(MYSQL *mysql)
 
885
{
 
886
  MYSQL_DATA *fields;
 
887
  uint field_count;
 
888
  uchar *pos;
 
889
  DBUG_ENTER("mysql_list_processes");
 
890
 
 
891
  if (simple_command(mysql,COM_PROCESS_INFO,0,0,0))
 
892
    DBUG_RETURN(0);
 
893
  free_old_query(mysql);
 
894
  pos=(uchar*) mysql->net.read_pos;
 
895
  field_count=(uint) net_field_length(&pos);
 
896
  if (!(fields = (*mysql->methods->read_rows)(mysql,(MYSQL_FIELD*) 0,
 
897
                                              protocol_41(mysql) ? 7 : 5)))
 
898
    DBUG_RETURN(NULL);
 
899
  if (!(mysql->fields=unpack_fields(fields,&mysql->field_alloc,field_count,0,
 
900
                                    mysql->server_capabilities)))
 
901
    DBUG_RETURN(0);
 
902
  mysql->status=MYSQL_STATUS_GET_RESULT;
 
903
  mysql->field_count=field_count;
 
904
  DBUG_RETURN(mysql_store_result(mysql));
 
905
}
 
906
 
 
907
 
 
908
#ifdef USE_OLD_FUNCTIONS
 
909
int  STDCALL
 
910
mysql_create_db(MYSQL *mysql, const char *db)
 
911
{
 
912
  DBUG_ENTER("mysql_createdb");
 
913
  DBUG_PRINT("enter",("db: %s",db));
 
914
  DBUG_RETURN(simple_command(mysql,COM_CREATE_DB,db, (ulong) strlen(db),0));
 
915
}
 
916
 
 
917
 
 
918
int  STDCALL
 
919
mysql_drop_db(MYSQL *mysql, const char *db)
 
920
{
 
921
  DBUG_ENTER("mysql_drop_db");
 
922
  DBUG_PRINT("enter",("db: %s",db));
 
923
  DBUG_RETURN(simple_command(mysql,COM_DROP_DB,db,(ulong) strlen(db),0));
 
924
}
 
925
#endif
 
926
 
 
927
 
 
928
int STDCALL
 
929
mysql_shutdown(MYSQL *mysql, enum mysql_enum_shutdown_level shutdown_level)
 
930
{
 
931
  uchar level[1];
 
932
  DBUG_ENTER("mysql_shutdown");
 
933
  level[0]= (uchar) shutdown_level;
 
934
  DBUG_RETURN(simple_command(mysql, COM_SHUTDOWN, level, 1, 0));
 
935
}
 
936
 
 
937
 
 
938
int STDCALL
 
939
mysql_refresh(MYSQL *mysql,uint options)
 
940
{
 
941
  uchar bits[1];
 
942
  DBUG_ENTER("mysql_refresh");
 
943
  bits[0]= (uchar) options;
 
944
  DBUG_RETURN(simple_command(mysql, COM_REFRESH, bits, 1, 0));
 
945
}
 
946
 
 
947
 
 
948
int STDCALL
 
949
mysql_kill(MYSQL *mysql,ulong pid)
 
950
{
 
951
  uchar buff[4];
 
952
  DBUG_ENTER("mysql_kill");
384
953
  int4store(buff,pid);
385
 
  return(simple_command(drizzle,COM_PROCESS_KILL,buff,sizeof(buff),0));
386
 
}
387
 
 
388
 
 
389
 
int
390
 
drizzle_set_server_option(DRIZZLE *drizzle, enum enum_drizzle_set_option option)
391
 
{
392
 
  unsigned char buff[2];
393
 
  int2store(buff, (uint32_t) option);
394
 
  return(simple_command(drizzle, COM_SET_OPTION, buff, sizeof(buff), 0));
395
 
}
396
 
 
397
 
 
398
 
const char *cli_read_statistics(DRIZZLE *drizzle)
399
 
{
400
 
  drizzle->net.read_pos[drizzle->packet_length]=0;  /* End of stat string */
401
 
  if (!drizzle->net.read_pos[0])
 
954
  DBUG_RETURN(simple_command(mysql,COM_PROCESS_KILL,buff,sizeof(buff),0));
 
955
}
 
956
 
 
957
 
 
958
int STDCALL
 
959
mysql_set_server_option(MYSQL *mysql, enum enum_mysql_set_option option)
 
960
{
 
961
  uchar buff[2];
 
962
  DBUG_ENTER("mysql_set_server_option");
 
963
  int2store(buff, (uint) option);
 
964
  DBUG_RETURN(simple_command(mysql, COM_SET_OPTION, buff, sizeof(buff), 0));
 
965
}
 
966
 
 
967
 
 
968
int STDCALL
 
969
mysql_dump_debug_info(MYSQL *mysql)
 
970
{
 
971
  DBUG_ENTER("mysql_dump_debug_info");
 
972
  DBUG_RETURN(simple_command(mysql,COM_DEBUG,0,0,0));
 
973
}
 
974
 
 
975
 
 
976
const char *cli_read_statistics(MYSQL *mysql)
 
977
{
 
978
  mysql->net.read_pos[mysql->packet_length]=0;  /* End of stat string */
 
979
  if (!mysql->net.read_pos[0])
402
980
  {
403
 
    drizzle_set_error(drizzle, CR_WRONG_HOST_INFO, sqlstate_get_unknown());
404
 
    return drizzle->net.last_error;
 
981
    set_mysql_error(mysql, CR_WRONG_HOST_INFO, unknown_sqlstate);
 
982
    return mysql->net.last_error;
405
983
  }
406
 
  return (char*) drizzle->net.read_pos;
407
 
}
408
 
 
409
 
 
410
 
int
411
 
drizzle_ping(DRIZZLE *drizzle)
 
984
  return (char*) mysql->net.read_pos;
 
985
}
 
986
 
 
987
 
 
988
const char * STDCALL
 
989
mysql_stat(MYSQL *mysql)
 
990
{
 
991
  DBUG_ENTER("mysql_stat");
 
992
  if (simple_command(mysql,COM_STATISTICS,0,0,0))
 
993
    DBUG_RETURN(mysql->net.last_error);
 
994
  DBUG_RETURN((*mysql->methods->read_statistics)(mysql));
 
995
}
 
996
 
 
997
 
 
998
int STDCALL
 
999
mysql_ping(MYSQL *mysql)
412
1000
{
413
1001
  int res;
414
 
  res= simple_command(drizzle,COM_PING,0,0,0);
415
 
  if (res == CR_SERVER_LOST && drizzle->reconnect)
416
 
    res= simple_command(drizzle,COM_PING,0,0,0);
417
 
  return(res);
418
 
}
419
 
 
420
 
 
421
 
const char *
422
 
drizzle_get_server_info(const DRIZZLE *drizzle)
423
 
{
424
 
  return((char*) drizzle->server_version);
425
 
}
426
 
 
427
 
 
428
 
const char *
429
 
drizzle_get_host_info(const DRIZZLE *drizzle)
430
 
{
431
 
  return(drizzle->host_info);
432
 
}
433
 
 
434
 
 
435
 
uint32_t
436
 
drizzle_get_proto_info(const DRIZZLE *drizzle)
437
 
{
438
 
  return (drizzle->protocol_version);
439
 
}
440
 
 
441
 
const char *
442
 
drizzle_get_client_info(void)
443
 
{
444
 
  return (char*) DRIZZLE_SERVER_VERSION;
445
 
}
446
 
 
447
 
uint32_t drizzle_get_client_version(void)
448
 
{
449
 
  return DRIZZLE_VERSION_ID;
450
 
}
451
 
 
452
 
bool drizzle_eof(const DRIZZLE_RES *res)
 
1002
  DBUG_ENTER("mysql_ping");
 
1003
  res= simple_command(mysql,COM_PING,0,0,0);
 
1004
  if (res == CR_SERVER_LOST && mysql->reconnect)
 
1005
    res= simple_command(mysql,COM_PING,0,0,0);
 
1006
  DBUG_RETURN(res);
 
1007
}
 
1008
 
 
1009
 
 
1010
const char * STDCALL
 
1011
mysql_get_server_info(MYSQL *mysql)
 
1012
{
 
1013
  return((char*) mysql->server_version);
 
1014
}
 
1015
 
 
1016
 
 
1017
const char * STDCALL
 
1018
mysql_get_host_info(MYSQL *mysql)
 
1019
{
 
1020
  return(mysql->host_info);
 
1021
}
 
1022
 
 
1023
 
 
1024
uint STDCALL
 
1025
mysql_get_proto_info(MYSQL *mysql)
 
1026
{
 
1027
  return (mysql->protocol_version);
 
1028
}
 
1029
 
 
1030
const char * STDCALL
 
1031
mysql_get_client_info(void)
 
1032
{
 
1033
  return (char*) MYSQL_SERVER_VERSION;
 
1034
}
 
1035
 
 
1036
ulong STDCALL mysql_get_client_version(void)
 
1037
{
 
1038
  return MYSQL_VERSION_ID;
 
1039
}
 
1040
 
 
1041
my_bool STDCALL mysql_eof(MYSQL_RES *res)
453
1042
{
454
1043
  return res->eof;
455
1044
}
456
1045
 
457
 
const DRIZZLE_FIELD * drizzle_fetch_field_direct(const DRIZZLE_RES *res, unsigned int fieldnr)
 
1046
MYSQL_FIELD * STDCALL mysql_fetch_field_direct(MYSQL_RES *res,uint fieldnr)
458
1047
{
459
1048
  return &(res)->fields[fieldnr];
460
1049
}
461
1050
 
462
 
const DRIZZLE_FIELD * drizzle_fetch_fields(const DRIZZLE_RES *res)
 
1051
MYSQL_FIELD * STDCALL mysql_fetch_fields(MYSQL_RES *res)
463
1052
{
464
 
  return res->fields;
 
1053
  return (res)->fields;
465
1054
}
466
1055
 
467
 
DRIZZLE_ROW_OFFSET drizzle_row_tell(const DRIZZLE_RES *res)
 
1056
MYSQL_ROW_OFFSET STDCALL mysql_row_tell(MYSQL_RES *res)
468
1057
{
469
1058
  return res->data_cursor;
470
1059
}
471
1060
 
472
 
DRIZZLE_FIELD_OFFSET drizzle_field_tell(const DRIZZLE_RES *res)
473
 
{
474
 
  return res->current_field;
475
 
}
476
 
 
477
 
/* DRIZZLE */
478
 
 
479
 
unsigned int drizzle_field_count(const DRIZZLE *drizzle)
480
 
{
481
 
  return drizzle->field_count;
482
 
}
483
 
 
484
 
uint64_t drizzle_affected_rows(const DRIZZLE *drizzle)
485
 
{
486
 
  return drizzle->affected_rows;
487
 
}
488
 
 
489
 
uint64_t drizzle_insert_id(const DRIZZLE *drizzle)
490
 
{
491
 
  return drizzle->insert_id;
492
 
}
493
 
 
494
 
const char * drizzle_sqlstate(const DRIZZLE *drizzle)
495
 
{
496
 
  return drizzle ? drizzle->net.sqlstate : sqlstate_get_cant_connect();
497
 
}
498
 
 
499
 
uint32_t drizzle_warning_count(const DRIZZLE *drizzle)
500
 
{
501
 
  return drizzle->warning_count;
502
 
}
503
 
 
504
 
const char * drizzle_info(const DRIZZLE *drizzle)
505
 
{
506
 
  return drizzle->info;
507
 
}
508
 
 
509
 
uint32_t drizzle_thread_id(const DRIZZLE *drizzle)
510
 
{
511
 
  return drizzle->thread_id;
 
1061
MYSQL_FIELD_OFFSET STDCALL mysql_field_tell(MYSQL_RES *res)
 
1062
{
 
1063
  return (res)->current_field;
 
1064
}
 
1065
 
 
1066
/* MYSQL */
 
1067
 
 
1068
unsigned int STDCALL mysql_field_count(MYSQL *mysql)
 
1069
{
 
1070
  return mysql->field_count;
 
1071
}
 
1072
 
 
1073
my_ulonglong STDCALL mysql_affected_rows(MYSQL *mysql)
 
1074
{
 
1075
  return mysql->affected_rows;
 
1076
}
 
1077
 
 
1078
my_ulonglong STDCALL mysql_insert_id(MYSQL *mysql)
 
1079
{
 
1080
  return mysql->insert_id;
 
1081
}
 
1082
 
 
1083
const char *STDCALL mysql_sqlstate(MYSQL *mysql)
 
1084
{
 
1085
  return mysql ? mysql->net.sqlstate : cant_connect_sqlstate;
 
1086
}
 
1087
 
 
1088
uint STDCALL mysql_warning_count(MYSQL *mysql)
 
1089
{
 
1090
  return mysql->warning_count;
 
1091
}
 
1092
 
 
1093
const char *STDCALL mysql_info(MYSQL *mysql)
 
1094
{
 
1095
  return mysql->info;
 
1096
}
 
1097
 
 
1098
ulong STDCALL mysql_thread_id(MYSQL *mysql)
 
1099
{
 
1100
  return (mysql)->thread_id;
 
1101
}
 
1102
 
 
1103
const char * STDCALL mysql_character_set_name(MYSQL *mysql)
 
1104
{
 
1105
  return mysql->charset->csname;
 
1106
}
 
1107
 
 
1108
void STDCALL mysql_get_character_set_info(MYSQL *mysql, MY_CHARSET_INFO *csinfo)
 
1109
{
 
1110
  csinfo->number   = mysql->charset->number;
 
1111
  csinfo->state    = mysql->charset->state;
 
1112
  csinfo->csname   = mysql->charset->csname;
 
1113
  csinfo->name     = mysql->charset->name;
 
1114
  csinfo->comment  = mysql->charset->comment;
 
1115
  csinfo->mbminlen = mysql->charset->mbminlen;
 
1116
  csinfo->mbmaxlen = mysql->charset->mbmaxlen;
 
1117
 
 
1118
  if (mysql->options.charset_dir)
 
1119
    csinfo->dir = mysql->options.charset_dir;
 
1120
  else
 
1121
    csinfo->dir = charsets_dir;
 
1122
}
 
1123
 
 
1124
uint STDCALL mysql_thread_safe(void)
 
1125
{
 
1126
  return 1;
 
1127
}
 
1128
 
 
1129
 
 
1130
my_bool STDCALL mysql_embedded(void)
 
1131
{
 
1132
#ifdef EMBEDDED_LIBRARY
 
1133
  return 1;
 
1134
#else
 
1135
  return 0;
 
1136
#endif
512
1137
}
513
1138
 
514
1139
/****************************************************************************
521
1146
 
522
1147
void my_net_local_init(NET *net)
523
1148
{
524
 
  net->max_packet=   (uint32_t) net_buffer_length;
 
1149
  net->max_packet=   (uint) net_buffer_length;
525
1150
  my_net_set_read_timeout(net, CLIENT_NET_READ_TIMEOUT);
526
1151
  my_net_set_write_timeout(net, CLIENT_NET_WRITE_TIMEOUT);
527
1152
  net->retry_count=  1;
528
 
  net->max_packet_size= (net_buffer_length > max_allowed_packet) ?
529
 
    net_buffer_length : max_allowed_packet;
 
1153
  net->max_packet_size= max(net_buffer_length, max_allowed_packet);
530
1154
}
531
1155
 
532
1156
/*
533
1157
  This function is used to create HEX string that you
534
1158
  can use in a SQL statement in of the either ways:
535
 
    INSERT INTO blob_column VALUES (0xAABBCC);  (any DRIZZLE version)
536
 
    INSERT INTO blob_column VALUES (X'AABBCC'); 
 
1159
    INSERT INTO blob_column VALUES (0xAABBCC);  (any MySQL version)
 
1160
    INSERT INTO blob_column VALUES (X'AABBCC'); (4.1 and higher)
537
1161
  
538
1162
  The string in "from" is encoded to a HEX string.
539
1163
  The result is placed in "to" and a terminating null byte is appended.
541
1165
  The string pointed to by "from" must be "length" bytes long.
542
1166
  You must allocate the "to" buffer to be at least length*2+1 bytes long.
543
1167
  Each character needs two bytes, and you need room for the terminating
544
 
  null byte. When drizzle_hex_string() returns, the contents of "to" will
 
1168
  null byte. When mysql_hex_string() returns, the contents of "to" will
545
1169
  be a null-terminated string. The return value is the length of the
546
 
  encoded string, not including the terminating null character.  The return value does not contain any leading 0x or a leading X' and
 
1170
  encoded string, not including the terminating null character.
 
1171
 
 
1172
  The return value does not contain any leading 0x or a leading X' and
547
1173
  trailing '. The caller must supply whichever of those is desired.
548
1174
*/
549
1175
 
550
 
uint32_t
551
 
drizzle_hex_string(char *to, const char *from, uint32_t length)
 
1176
ulong STDCALL
 
1177
mysql_hex_string(char *to, const char *from, ulong length)
552
1178
{
553
1179
  char *to0= to;
554
1180
  const char *end;
559
1185
    *to++= _dig_vec_upper[((unsigned char) *from) & 0x0F];
560
1186
  }
561
1187
  *to= '\0';
562
 
  return (uint32_t) (to-to0);
 
1188
  return (ulong) (to-to0);
563
1189
}
564
1190
 
565
1191
/*
568
1194
  Returns the length of the to string
569
1195
*/
570
1196
 
571
 
uint32_t
572
 
drizzle_escape_string(char *to,const char *from, uint32_t length)
573
 
{
574
 
  const char *to_start= to;
575
 
  const char *end, *to_end=to_start + 2*length;
576
 
  bool overflow= false;
577
 
  for (end= from + length; from < end; from++)
 
1197
ulong STDCALL
 
1198
mysql_escape_string(char *to,const char *from,ulong length)
 
1199
{
 
1200
  return escape_string_for_mysql(default_charset_info, to, 0, from, length);
 
1201
}
 
1202
 
 
1203
ulong STDCALL
 
1204
mysql_real_escape_string(MYSQL *mysql, char *to,const char *from,
 
1205
                         ulong length)
 
1206
{
 
1207
  if (mysql->server_status & SERVER_STATUS_NO_BACKSLASH_ESCAPES)
 
1208
    return escape_quotes_for_mysql(mysql->charset, to, 0, from, length);
 
1209
  return escape_string_for_mysql(mysql->charset, to, 0, from, length);
 
1210
}
 
1211
 
 
1212
void STDCALL
 
1213
myodbc_remove_escape(MYSQL *mysql,char *name)
 
1214
{
 
1215
  char *to;
 
1216
#ifdef USE_MB
 
1217
  my_bool use_mb_flag=use_mb(mysql->charset);
 
1218
  char *end;
 
1219
  if (use_mb_flag)
 
1220
    for (end=name; *end ; end++) ;
 
1221
#endif
 
1222
 
 
1223
  for (to=name ; *name ; name++)
578
1224
  {
579
 
    uint32_t tmp_length;
580
 
    char escape= 0;
581
 
    if (!U8_IS_SINGLE(*from))
 
1225
#ifdef USE_MB
 
1226
    int l;
 
1227
    if (use_mb_flag && (l = my_ismbchar( mysql->charset, name , end ) ) )
582
1228
    {
583
 
      tmp_length= U8_LENGTH(*from);
584
 
      if (to + tmp_length > to_end)
585
 
      {
586
 
        overflow= true;
587
 
        break;
588
 
      }
589
 
      while (tmp_length--)
590
 
        *to++= *from++;
591
 
      from--;
 
1229
      while (l--)
 
1230
        *to++ = *name++;
 
1231
      name--;
592
1232
      continue;
593
1233
    }
594
 
    switch (*from) {
595
 
    case 0:                             /* Must be escaped for 'mysql' */
596
 
      escape= '0';
597
 
      break;
598
 
    case '\n':                          /* Must be escaped for logs */
599
 
      escape= 'n';
600
 
      break;
601
 
    case '\r':
602
 
      escape= 'r';
603
 
      break;
604
 
    case '\\':
605
 
      escape= '\\';
606
 
      break;
607
 
    case '\'':
608
 
      escape= '\'';
609
 
      break;
610
 
    case '"':                           /* Better safe than sorry */
611
 
      escape= '"';
612
 
      break;
613
 
    case '\032':                        /* This gives problems on Win32 */
614
 
      escape= 'Z';
615
 
      break;
616
 
    }
617
 
    if (escape)
618
 
    {
619
 
      if (to + 2 > to_end)
620
 
      {
621
 
        overflow= true;
622
 
        break;
623
 
      }
624
 
      *to++= '\\';
625
 
      *to++= escape;
626
 
    }
627
 
    else
628
 
    {
629
 
      if (to + 1 > to_end)
630
 
      {
631
 
        overflow= true;
632
 
        break;
633
 
      }
634
 
      *to++= *from;
635
 
    }
 
1234
#endif
 
1235
    if (*name == '\\' && name[1])
 
1236
      name++;
 
1237
    *to++= *name;
636
1238
  }
637
 
  *to= 0;
638
 
  return overflow ? (size_t) -1 : (size_t) (to - to_start);
 
1239
  *to=0;
639
1240
}
640
1241
 
641
 
int cli_unbuffered_fetch(DRIZZLE *drizzle, char **row)
 
1242
int cli_unbuffered_fetch(MYSQL *mysql, char **row)
642
1243
{
643
 
  if (packet_error == cli_safe_read(drizzle))
 
1244
  if (packet_error == cli_safe_read(mysql))
644
1245
    return 1;
645
1246
 
646
 
  *row= ((drizzle->net.read_pos[0] == DRIZZLE_PROTOCOL_NO_MORE_DATA) ? NULL :
647
 
   (char*) (drizzle->net.read_pos+1));
 
1247
  *row= ((mysql->net.read_pos[0] == 254) ? NULL :
 
1248
         (char*) (mysql->net.read_pos+1));
648
1249
  return 0;
649
1250
}
650
1251
 
656
1257
  Commit the current transaction
657
1258
*/
658
1259
 
659
 
bool drizzle_commit(DRIZZLE *drizzle)
 
1260
my_bool STDCALL mysql_commit(MYSQL * mysql)
660
1261
{
661
 
  return((bool) drizzle_real_query(drizzle, "commit", 6));
 
1262
  DBUG_ENTER("mysql_commit");
 
1263
  DBUG_RETURN((my_bool) mysql_real_query(mysql, "commit", 6));
662
1264
}
663
1265
 
664
1266
/*
665
1267
  Rollback the current transaction
666
1268
*/
667
1269
 
668
 
bool drizzle_rollback(DRIZZLE *drizzle)
 
1270
my_bool STDCALL mysql_rollback(MYSQL * mysql)
669
1271
{
670
 
  return((bool) drizzle_real_query(drizzle, "rollback", 8));
 
1272
  DBUG_ENTER("mysql_rollback");
 
1273
  DBUG_RETURN((my_bool) mysql_real_query(mysql, "rollback", 8));
671
1274
}
672
1275
 
673
1276
 
675
1278
  Set autocommit to either true or false
676
1279
*/
677
1280
 
678
 
bool drizzle_autocommit(DRIZZLE *drizzle, bool auto_mode)
 
1281
my_bool STDCALL mysql_autocommit(MYSQL * mysql, my_bool auto_mode)
679
1282
{
680
 
  return((bool) drizzle_real_query(drizzle, auto_mode ?
 
1283
  DBUG_ENTER("mysql_autocommit");
 
1284
  DBUG_PRINT("enter", ("mode : %d", auto_mode));
 
1285
 
 
1286
  DBUG_RETURN((my_bool) mysql_real_query(mysql, auto_mode ?
681
1287
                                         "set autocommit=1":"set autocommit=0",
682
1288
                                         16));
683
1289
}
689
1295
 
690
1296
/*
691
1297
  Returns true/false to indicate whether any more query results exist
692
 
  to be read using drizzle_next_result()
 
1298
  to be read using mysql_next_result()
693
1299
*/
694
1300
 
695
 
bool drizzle_more_results(const DRIZZLE *drizzle)
 
1301
my_bool STDCALL mysql_more_results(MYSQL *mysql)
696
1302
{
697
 
  return (drizzle->server_status & SERVER_MORE_RESULTS_EXISTS) ? true:false;
 
1303
  my_bool res;
 
1304
  DBUG_ENTER("mysql_more_results");
 
1305
 
 
1306
  res= ((mysql->server_status & SERVER_MORE_RESULTS_EXISTS) ? 1: 0);
 
1307
  DBUG_PRINT("exit",("More results exists ? %d", res));
 
1308
  DBUG_RETURN(res);
698
1309
}
699
1310
 
700
1311
 
701
1312
/*
702
1313
  Reads and returns the next query results
703
1314
*/
704
 
int drizzle_next_result(DRIZZLE *drizzle)
 
1315
int STDCALL mysql_next_result(MYSQL *mysql)
705
1316
{
706
 
  if (drizzle->status != DRIZZLE_STATUS_READY)
 
1317
  DBUG_ENTER("mysql_next_result");
 
1318
 
 
1319
  if (mysql->status != MYSQL_STATUS_READY)
707
1320
  {
708
 
    drizzle_set_error(drizzle, CR_COMMANDS_OUT_OF_SYNC, sqlstate_get_unknown());
709
 
    return(1);
 
1321
    set_mysql_error(mysql, CR_COMMANDS_OUT_OF_SYNC, unknown_sqlstate);
 
1322
    DBUG_RETURN(1);
710
1323
  }
711
1324
 
712
 
  net_clear_error(&drizzle->net);
713
 
  drizzle->affected_rows= ~(uint64_t) 0;
714
 
 
715
 
  if (drizzle->server_status & SERVER_MORE_RESULTS_EXISTS)
716
 
    return((*drizzle->methods->next_result)(drizzle));
717
 
 
718
 
  return(-1);        /* No more results */
719
 
}
720
 
 
721
 
 
722
 
DRIZZLE_RES * drizzle_use_result(DRIZZLE *drizzle)
723
 
{
724
 
  return (*drizzle->methods->use_result)(drizzle);
725
 
}
726
 
 
727
 
bool drizzle_read_query_result(DRIZZLE *drizzle)
728
 
{
729
 
  return (*drizzle->methods->read_query_result)(drizzle);
 
1325
  net_clear_error(&mysql->net);
 
1326
  mysql->affected_rows= ~(my_ulonglong) 0;
 
1327
 
 
1328
  if (mysql->server_status & SERVER_MORE_RESULTS_EXISTS)
 
1329
    DBUG_RETURN((*mysql->methods->next_result)(mysql));
 
1330
 
 
1331
  DBUG_RETURN(-1);                              /* No more results */
 
1332
}
 
1333
 
 
1334
 
 
1335
MYSQL_RES * STDCALL mysql_use_result(MYSQL *mysql)
 
1336
{
 
1337
  return (*mysql->methods->use_result)(mysql);
 
1338
}
 
1339
 
 
1340
my_bool STDCALL mysql_read_query_result(MYSQL *mysql)
 
1341
{
 
1342
  return (*mysql->methods->read_query_result)(mysql);
730
1343
}
731
1344