~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to libdrizzle/libdrizzle.c

Merged.

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