~drizzle-trunk/drizzle/development

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