~drizzle-trunk/drizzle/development

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 *
 *  Copyright (C) 2008 Sun Microsystems, Inc.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; version 2 of the License.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <drizzled/global.h>
#include "libdrizzle_priv.h"

#include "libdrizzle.h"
#include "errmsg.h"
#include "pack.h"

#include <sys/stat.h>
#include <signal.h>
#include <time.h>
#ifdef   HAVE_PWD_H
#include <pwd.h>
#endif

#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#ifdef HAVE_SELECT_H
#include <select.h>
#endif
#ifdef HAVE_SYS_SELECT_H
#include <sys/select.h>
#endif

#ifdef HAVE_POLL
#include <sys/poll.h>
#endif
#ifdef HAVE_SYS_UN_H
#include <sys/un.h>
#endif
#ifndef INADDR_NONE
#define INADDR_NONE  -1
#endif

#include <stdlib.h>
#include <string.h>
#include <mystrings/utf8.h>

unsigned int drizzle_port=0;

#include <errno.h>

unsigned int drizzleclient_get_default_port(void)
{
  return drizzle_port;
}

void drizzleclient_set_default_port(unsigned int port)
{
  drizzle_port= port;
}

/*
  Expand wildcard to a sql string
*/

static void
append_wild(char *to, char *end, const char *wild)
{
  end-=5;          /* Some extra */
  if (wild && wild[0])
  {
    to= strcpy(to," like '");
    to+= 7; /* strlen(" like '"); */

    while (*wild && to < end)
    {
      if (*wild == '\\' || *wild == '\'')
  *to++='\\';
      *to++= *wild++;
    }
    if (*wild)          /* Too small buffer */
      *to++='%';        /* Nicer this way */
    to[0]='\'';
    to[1]=0;
  }
}

#if defined(HAVE_GETPWUID) && defined(NO_GETPWUID_DECL)
struct passwd *getpwuid(uid_t);
char* getlogin(void);
#endif

/**************************************************************************
  Do a query. If query returned rows, free old rows.
  Read data by drizzleclient_store_result or by repeat call of drizzleclient_fetch_row
**************************************************************************/

int
drizzleclient_query(DRIZZLE *drizzle, const char *query)
{
  return drizzleclient_real_query(drizzle,query, (uint32_t) strlen(query));
}


/**************************************************************************
  Return next field of the query results
**************************************************************************/

DRIZZLE_FIELD *
drizzleclient_fetch_field(DRIZZLE_RES *result)
{
  if (result->current_field >= result->field_count)
    return(NULL);
  return &result->fields[result->current_field++];
}


/**************************************************************************
  Move to a specific row and column
**************************************************************************/

void
drizzleclient_data_seek(DRIZZLE_RES *result, uint64_t row)
{
  DRIZZLE_ROWS  *tmp=0;
  if (result->data)
    for (tmp=result->data->data; row-- && tmp ; tmp = tmp->next) ;
  result->current_row=0;
  result->data_cursor = tmp;
}


/*************************************************************************
  put the row or field cursor one a position one got from DRIZZLE_ROW_tell()
  This doesn't restore any data. The next drizzleclient_fetch_row or
  drizzleclient_fetch_field will return the next row or field after the last used
*************************************************************************/

DRIZZLE_ROW_OFFSET
drizzleclient_row_seek(DRIZZLE_RES *result, DRIZZLE_ROW_OFFSET row)
{
  DRIZZLE_ROW_OFFSET return_value=result->data_cursor;
  result->current_row= 0;
  result->data_cursor= row;
  return return_value;
}


DRIZZLE_FIELD_OFFSET
drizzleclient_field_seek(DRIZZLE_RES *result, DRIZZLE_FIELD_OFFSET field_offset)
{
  DRIZZLE_FIELD_OFFSET return_value=result->current_field;
  result->current_field=field_offset;
  return return_value;
}


/*****************************************************************************
  List all tables in a database
  If wild is given then only the tables matching wild is returned
*****************************************************************************/

DRIZZLE_RES *
drizzleclient_list_tables(DRIZZLE *drizzle, const char *wild)
{
  char buff[255];
  char *ptr= strcpy(buff, "show tables");
  ptr+= 11; /* strlen("show tables"); */

  append_wild(ptr,buff+sizeof(buff),wild);
  if (drizzleclient_query(drizzle,buff))
    return(0);
  return (drizzleclient_store_result(drizzle));
}


DRIZZLE_FIELD *drizzleclient_cli_list_fields(DRIZZLE *drizzle)
{
  DRIZZLE_DATA *query;
  if (!(query= drizzleclient_cli_read_rows(drizzle,(DRIZZLE_FIELD*) 0, 8)))
    return NULL;

  drizzle->field_count= (uint32_t) query->rows;
  return drizzleclient_unpack_fields(query, drizzle->field_count, 1);
}

int
drizzleclient_shutdown(DRIZZLE *drizzle)
{
  return(simple_command(drizzle, COM_SHUTDOWN, 0, 0, 0));
}


const char *drizzleclient_cli_read_statistics(DRIZZLE *drizzle)
{
  drizzle->net.read_pos[drizzle->packet_length]=0;  /* End of stat string */
  if (!drizzle->net.read_pos[0])
  {
    drizzleclient_set_error(drizzle, CR_WRONG_HOST_INFO, drizzleclient_sqlstate_get_unknown());
    return drizzle->net.last_error;
  }
  return (char*) drizzle->net.read_pos;
}


int
drizzleclient_ping(DRIZZLE *drizzle)
{
  int res;
  res= simple_command(drizzle,COM_PING,0,0,0);
  if (res == CR_SERVER_LOST && drizzle->reconnect)
    res= simple_command(drizzle,COM_PING,0,0,0);
  return(res);
}


const char *
drizzleclient_get_server_info(const DRIZZLE *drizzle)
{
  return((char*) drizzle->server_version);
}


const char *
drizzleclient_get_host_info(const DRIZZLE *drizzle)
{
  return(drizzle->host_info);
}


uint32_t
drizzleclient_get_proto_info(const DRIZZLE *drizzle)
{
  return (drizzle->protocol_version);
}

const char *
drizzleclient_get_client_info(void)
{
  return (char*) VERSION;
}

uint32_t drizzleclient_get_client_version(void)
{
  return DRIZZLE_VERSION_ID;
}

bool drizzleclient_eof(const DRIZZLE_RES *res)
{
  return res->eof;
}

const DRIZZLE_FIELD * drizzleclient_fetch_field_direct(const DRIZZLE_RES *res, unsigned int fieldnr)
{
  return &(res)->fields[fieldnr];
}

const DRIZZLE_FIELD * drizzleclient_fetch_fields(const DRIZZLE_RES *res)
{
  return res->fields;
}

DRIZZLE_ROW_OFFSET drizzleclient_row_tell(const DRIZZLE_RES *res)
{
  return res->data_cursor;
}

DRIZZLE_FIELD_OFFSET drizzleclient_field_tell(const DRIZZLE_RES *res)
{
  return res->current_field;
}

/* DRIZZLE */

unsigned int drizzleclient_field_count(const DRIZZLE *drizzle)
{
  return drizzle->field_count;
}

uint64_t drizzleclient_affected_rows(const DRIZZLE *drizzle)
{
  return drizzle->affected_rows;
}

uint64_t drizzleclient_insert_id(const DRIZZLE *drizzle)
{
  return drizzle->insert_id;
}

const char * drizzleclient_sqlstate(const DRIZZLE *drizzle)
{
  return drizzle ? drizzle->net.sqlstate : drizzleclient_sqlstate_get_cant_connect();
}

uint32_t drizzleclient_warning_count(const DRIZZLE *drizzle)
{
  return drizzle->warning_count;
}

const char * drizzleclient_info(const DRIZZLE *drizzle)
{
  return drizzle->info;
}

uint32_t drizzleclient_thread_id(const DRIZZLE *drizzle)
{
  return drizzle->thread_id;
}

/****************************************************************************
  Some support functions
****************************************************************************/

/*
  Add escape characters to a string (blob?) to make it suitable for a insert
  to should at least have place for length*2+1 chars
  Returns the length of the to string
*/

uint32_t
drizzleclient_escape_string(char *to,const char *from, uint32_t length)
{
  const char *to_start= to;
  const char *end, *to_end=to_start + 2*length;
  bool overflow= false;
  for (end= from + length; from < end; from++)
  {
    uint32_t tmp_length;
    char escape= 0;
    if (!U8_IS_SINGLE(*from))
    {
      tmp_length= U8_LENGTH(*(uint32_t*)from);
      if (to + tmp_length > to_end)
      {
        overflow= true;
        break;
      }
      while (tmp_length--)
        *to++= *from++;
      from--;
      continue;
    }
    switch (*from) {
    case 0:                             /* Must be escaped for 'mysql' */
      escape= '0';
      break;
    case '\n':                          /* Must be escaped for logs */
      escape= 'n';
      break;
    case '\r':
      escape= 'r';
      break;
    case '\\':
      escape= '\\';
      break;
    case '\'':
      escape= '\'';
      break;
    case '"':                           /* Better safe than sorry */
      escape= '"';
      break;
    case '\032':                        /* This gives problems on Win32 */
      escape= 'Z';
      break;
    }
    if (escape)
    {
      if (to + 2 > to_end)
      {
        overflow= true;
        break;
      }
      *to++= '\\';
      *to++= escape;
    }
    else
    {
      if (to + 1 > to_end)
      {
        overflow= true;
        break;
      }
      *to++= *from;
    }
  }
  *to= 0;
  return overflow ? (size_t) -1 : (size_t) (to - to_start);
}

int drizzleclient_cli_unbuffered_fetch(DRIZZLE *drizzle, char **row)
{
  if (packet_error == drizzleclient_cli_safe_read(drizzle))
    return 1;

  *row= ((drizzle->net.read_pos[0] == DRIZZLE_PROTOCOL_NO_MORE_DATA) ? NULL :
   (char*) (drizzle->net.read_pos+1));
  return 0;
}

/********************************************************************
 Transactional APIs
*********************************************************************/

/*
  Commit the current transaction
*/

bool drizzleclient_commit(DRIZZLE *drizzle)
{
  return((bool) drizzleclient_real_query(drizzle, "commit", 6));
}

/*
  Rollback the current transaction
*/

bool drizzleclient_rollback(DRIZZLE *drizzle)
{
  return((bool) drizzleclient_real_query(drizzle, "rollback", 8));
}


/*
  Set autocommit to either true or false
*/

bool drizzleclient_autocommit(DRIZZLE *drizzle, bool auto_mode)
{
  return((bool) drizzleclient_real_query(drizzle, auto_mode ?
                                         "set autocommit=1":"set autocommit=0",
                                         16));
}


/********************************************************************
 Multi query execution + SPs APIs
*********************************************************************/

/*
  Returns true/false to indicate whether any more query results exist
  to be read using drizzleclient_next_result()
*/

bool drizzleclient_more_results(const DRIZZLE *drizzle)
{
  return (drizzle->server_status & SERVER_MORE_RESULTS_EXISTS) ? true:false;
}


/*
  Reads and returns the next query results
*/
int drizzleclient_next_result(DRIZZLE *drizzle)
{
  if (drizzle->status != DRIZZLE_STATUS_READY)
  {
    drizzleclient_set_error(drizzle, CR_COMMANDS_OUT_OF_SYNC, drizzleclient_sqlstate_get_unknown());
    return(1);
  }

  drizzleclient_drizzleclient_net_clear_error(&drizzle->net);
  drizzle->affected_rows= ~(uint64_t) 0;

  if (drizzle->server_status & SERVER_MORE_RESULTS_EXISTS)
    return((*drizzle->methods->next_result)(drizzle));

  return(-1);        /* No more results */
}


DRIZZLE_RES * drizzleclient_use_result(DRIZZLE *drizzle)
{
  return (*drizzle->methods->use_result)(drizzle);
}

bool drizzleclient_read_query_result(DRIZZLE *drizzle)
{
  return (*drizzle->methods->read_query_result)(drizzle);
}