~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to libdrizzleclient/libdrizzle.c

  • Committer: Brian Aker
  • Date: 2009-02-08 12:39:10 UTC
  • mfrom: (840.1.21 devel)
  • Revision ID: brian@tangent.org-20090208123910-gaodow8xvkw9ed4l
Merging Monty's work

Show diffs side-by-side

added added

removed removed

Lines of Context:
71
71
static DRIZZLE_PARAMETERS drizzle_internal_parameters=
72
72
{&max_allowed_packet, &net_buffer_length, 0};
73
73
 
74
 
const DRIZZLE_PARAMETERS * drizzle_get_parameters(void)
 
74
const DRIZZLE_PARAMETERS * drizzleclient_get_parameters(void)
75
75
{
76
76
  return &drizzle_internal_parameters;
77
77
}
78
78
 
79
 
unsigned int drizzle_get_default_port(void)
 
79
unsigned int drizzleclient_get_default_port(void)
80
80
{
81
81
  return drizzle_port;
82
82
}
83
83
 
84
 
void drizzle_set_default_port(unsigned int port)
 
84
void drizzleclient_set_default_port(unsigned int port)
85
85
{
86
86
  drizzle_port= port;
87
87
}
116
116
  Change user and database
117
117
**************************************************************************/
118
118
 
119
 
int cli_read_change_user_result(DRIZZLE *drizzle)
 
119
int drizzleclient_cli_read_change_user_result(DRIZZLE *drizzle)
120
120
{
121
121
  uint32_t pkt_length;
122
122
 
123
 
  pkt_length= cli_safe_read(drizzle);
 
123
  pkt_length= drizzleclient_cli_safe_read(drizzle);
124
124
 
125
125
  if (pkt_length == packet_error)
126
126
    return 1;
128
128
  return 0;
129
129
}
130
130
 
131
 
bool drizzle_change_user(DRIZZLE *drizzle, const char *user,
 
131
bool drizzleclient_change_user(DRIZZLE *drizzle, const char *user,
132
132
                                 const char *passwd, const char *db)
133
133
{
134
134
  char buff[USERNAME_LENGTH+SCRAMBLED_PASSWORD_CHAR_LENGTH+NAME_LEN+2];
196
196
 
197
197
/**************************************************************************
198
198
  Do a query. If query returned rows, free old rows.
199
 
  Read data by drizzle_store_result or by repeat call of drizzle_fetch_row
 
199
  Read data by drizzleclient_store_result or by repeat call of drizzleclient_fetch_row
200
200
**************************************************************************/
201
201
 
202
202
int
203
 
drizzle_query(DRIZZLE *drizzle, const char *query)
 
203
drizzleclient_query(DRIZZLE *drizzle, const char *query)
204
204
{
205
 
  return drizzle_real_query(drizzle,query, (uint32_t) strlen(query));
 
205
  return drizzleclient_real_query(drizzle,query, (uint32_t) strlen(query));
206
206
}
207
207
 
208
208
 
211
211
**************************************************************************/
212
212
 
213
213
DRIZZLE_FIELD *
214
 
drizzle_fetch_field(DRIZZLE_RES *result)
 
214
drizzleclient_fetch_field(DRIZZLE_RES *result)
215
215
{
216
216
  if (result->current_field >= result->field_count)
217
217
    return(NULL);
224
224
**************************************************************************/
225
225
 
226
226
void
227
 
drizzle_data_seek(DRIZZLE_RES *result, uint64_t row)
 
227
drizzleclient_data_seek(DRIZZLE_RES *result, uint64_t row)
228
228
{
229
229
  DRIZZLE_ROWS  *tmp=0;
230
230
  if (result->data)
236
236
 
237
237
/*************************************************************************
238
238
  put the row or field cursor one a position one got from DRIZZLE_ROW_tell()
239
 
  This doesn't restore any data. The next drizzle_fetch_row or
240
 
  drizzle_fetch_field will return the next row or field after the last used
 
239
  This doesn't restore any data. The next drizzleclient_fetch_row or
 
240
  drizzleclient_fetch_field will return the next row or field after the last used
241
241
*************************************************************************/
242
242
 
243
243
DRIZZLE_ROW_OFFSET
244
 
drizzle_row_seek(DRIZZLE_RES *result, DRIZZLE_ROW_OFFSET row)
 
244
drizzleclient_row_seek(DRIZZLE_RES *result, DRIZZLE_ROW_OFFSET row)
245
245
{
246
246
  DRIZZLE_ROW_OFFSET return_value=result->data_cursor;
247
247
  result->current_row= 0;
251
251
 
252
252
 
253
253
DRIZZLE_FIELD_OFFSET
254
 
drizzle_field_seek(DRIZZLE_RES *result, DRIZZLE_FIELD_OFFSET field_offset)
 
254
drizzleclient_field_seek(DRIZZLE_RES *result, DRIZZLE_FIELD_OFFSET field_offset)
255
255
{
256
256
  DRIZZLE_FIELD_OFFSET return_value=result->current_field;
257
257
  result->current_field=field_offset;
265
265
*****************************************************************************/
266
266
 
267
267
DRIZZLE_RES *
268
 
drizzle_list_tables(DRIZZLE *drizzle, const char *wild)
 
268
drizzleclient_list_tables(DRIZZLE *drizzle, const char *wild)
269
269
{
270
270
  char buff[255];
271
271
  char *ptr= strcpy(buff, "show tables");
272
272
  ptr+= 11; /* strlen("show tables"); */
273
273
 
274
274
  append_wild(ptr,buff+sizeof(buff),wild);
275
 
  if (drizzle_query(drizzle,buff))
 
275
  if (drizzleclient_query(drizzle,buff))
276
276
    return(0);
277
 
  return (drizzle_store_result(drizzle));
 
277
  return (drizzleclient_store_result(drizzle));
278
278
}
279
279
 
280
280
 
281
 
DRIZZLE_FIELD *cli_list_fields(DRIZZLE *drizzle)
 
281
DRIZZLE_FIELD *drizzleclient_cli_list_fields(DRIZZLE *drizzle)
282
282
{
283
283
  DRIZZLE_DATA *query;
284
 
  if (!(query= cli_read_rows(drizzle,(DRIZZLE_FIELD*) 0, 8)))
 
284
  if (!(query= drizzleclient_cli_read_rows(drizzle,(DRIZZLE_FIELD*) 0, 8)))
285
285
    return NULL;
286
286
 
287
287
  drizzle->field_count= (uint32_t) query->rows;
288
 
  return unpack_fields(query, drizzle->field_count, 1);
 
288
  return drizzleclient_unpack_fields(query, drizzle->field_count, 1);
289
289
}
290
290
 
291
291
 
297
297
**************************************************************************/
298
298
 
299
299
DRIZZLE_RES *
300
 
drizzle_list_fields(DRIZZLE *drizzle, const char *table, const char *wild)
 
300
drizzleclient_list_fields(DRIZZLE *drizzle, const char *table, const char *wild)
301
301
{
302
302
  DRIZZLE_RES   *result;
303
303
  DRIZZLE_FIELD *fields;
306
306
  end= strncpy(buff, table, 128) + 128;
307
307
  end= strncpy(end+1, wild ? wild : "", 128) + 128;
308
308
 
309
 
  free_old_query(drizzle);
 
309
  drizzleclient_free_old_query(drizzle);
310
310
  if (simple_command(drizzle, COM_FIELD_LIST, (unsigned char*) buff,
311
311
                     (uint32_t) (end-buff), 1) ||
312
312
      !(fields= (*drizzle->methods->list_fields)(drizzle)))
328
328
/* List all running processes (threads) in server */
329
329
 
330
330
DRIZZLE_RES *
331
 
drizzle_list_processes(DRIZZLE *drizzle)
 
331
drizzleclient_list_processes(DRIZZLE *drizzle)
332
332
{
333
333
  DRIZZLE_DATA *fields;
334
334
  uint32_t field_count;
336
336
 
337
337
  if (simple_command(drizzle,COM_PROCESS_INFO,0,0,0))
338
338
    return(0);
339
 
  free_old_query(drizzle);
 
339
  drizzleclient_free_old_query(drizzle);
340
340
  pos=(unsigned char*) drizzle->net.read_pos;
341
 
  field_count=(uint32_t) net_field_length(&pos);
 
341
  field_count=(uint32_t) drizzleclient_net_field_length(&pos);
342
342
  if (!(fields = (*drizzle->methods->read_rows)(drizzle,(DRIZZLE_FIELD*) 0, 7)))
343
343
    return(NULL);
344
 
  if (!(drizzle->fields=unpack_fields(fields, field_count, 0)))
 
344
  if (!(drizzle->fields=drizzleclient_unpack_fields(fields, field_count, 0)))
345
345
    return(0);
346
346
  drizzle->status=DRIZZLE_STATUS_GET_RESULT;
347
347
  drizzle->field_count=field_count;
348
 
  return(drizzle_store_result(drizzle));
 
348
  return(drizzleclient_store_result(drizzle));
349
349
}
350
350
 
351
351
 
352
352
int
353
 
drizzle_shutdown(DRIZZLE *drizzle)
 
353
drizzleclient_shutdown(DRIZZLE *drizzle)
354
354
{
355
355
  return(simple_command(drizzle, COM_SHUTDOWN, 0, 0, 0));
356
356
}
357
357
 
358
358
 
359
359
int
360
 
drizzle_refresh(DRIZZLE *drizzle, uint32_t options)
 
360
drizzleclient_refresh(DRIZZLE *drizzle, uint32_t options)
361
361
{
362
362
  unsigned char bits[1];
363
363
  bits[0]= (unsigned char) options;
366
366
 
367
367
 
368
368
int32_t
369
 
drizzle_kill(DRIZZLE *drizzle, uint32_t pid)
 
369
drizzleclient_kill(DRIZZLE *drizzle, uint32_t pid)
370
370
{
371
371
  unsigned char buff[4];
372
372
  int4store(buff,pid);
375
375
 
376
376
 
377
377
int
378
 
drizzle_set_server_option(DRIZZLE *drizzle, enum enum_drizzle_set_option option)
 
378
drizzleclient_set_server_option(DRIZZLE *drizzle, enum enum_drizzle_set_option option)
379
379
{
380
380
  unsigned char buff[2];
381
381
  int2store(buff, (uint32_t) option);
383
383
}
384
384
 
385
385
 
386
 
const char *cli_read_statistics(DRIZZLE *drizzle)
 
386
const char *drizzleclient_cli_read_statistics(DRIZZLE *drizzle)
387
387
{
388
388
  drizzle->net.read_pos[drizzle->packet_length]=0;  /* End of stat string */
389
389
  if (!drizzle->net.read_pos[0])
390
390
  {
391
 
    drizzle_set_error(drizzle, CR_WRONG_HOST_INFO, sqlstate_get_unknown());
 
391
    drizzleclient_set_error(drizzle, CR_WRONG_HOST_INFO, drizzleclient_sqlstate_get_unknown());
392
392
    return drizzle->net.last_error;
393
393
  }
394
394
  return (char*) drizzle->net.read_pos;
396
396
 
397
397
 
398
398
int
399
 
drizzle_ping(DRIZZLE *drizzle)
 
399
drizzleclient_ping(DRIZZLE *drizzle)
400
400
{
401
401
  int res;
402
402
  res= simple_command(drizzle,COM_PING,0,0,0);
407
407
 
408
408
 
409
409
const char *
410
 
drizzle_get_server_info(const DRIZZLE *drizzle)
 
410
drizzleclient_get_server_info(const DRIZZLE *drizzle)
411
411
{
412
412
  return((char*) drizzle->server_version);
413
413
}
414
414
 
415
415
 
416
416
const char *
417
 
drizzle_get_host_info(const DRIZZLE *drizzle)
 
417
drizzleclient_get_host_info(const DRIZZLE *drizzle)
418
418
{
419
419
  return(drizzle->host_info);
420
420
}
421
421
 
422
422
 
423
423
uint32_t
424
 
drizzle_get_proto_info(const DRIZZLE *drizzle)
 
424
drizzleclient_get_proto_info(const DRIZZLE *drizzle)
425
425
{
426
426
  return (drizzle->protocol_version);
427
427
}
428
428
 
429
429
const char *
430
 
drizzle_get_client_info(void)
 
430
drizzleclient_get_client_info(void)
431
431
{
432
432
  return (char*) VERSION;
433
433
}
434
434
 
435
 
uint32_t drizzle_get_client_version(void)
 
435
uint32_t drizzleclient_get_client_version(void)
436
436
{
437
437
  return DRIZZLE_VERSION_ID;
438
438
}
439
439
 
440
 
bool drizzle_eof(const DRIZZLE_RES *res)
 
440
bool drizzleclient_eof(const DRIZZLE_RES *res)
441
441
{
442
442
  return res->eof;
443
443
}
444
444
 
445
 
const DRIZZLE_FIELD * drizzle_fetch_field_direct(const DRIZZLE_RES *res, unsigned int fieldnr)
 
445
const DRIZZLE_FIELD * drizzleclient_fetch_field_direct(const DRIZZLE_RES *res, unsigned int fieldnr)
446
446
{
447
447
  return &(res)->fields[fieldnr];
448
448
}
449
449
 
450
 
const DRIZZLE_FIELD * drizzle_fetch_fields(const DRIZZLE_RES *res)
 
450
const DRIZZLE_FIELD * drizzleclient_fetch_fields(const DRIZZLE_RES *res)
451
451
{
452
452
  return res->fields;
453
453
}
454
454
 
455
 
DRIZZLE_ROW_OFFSET drizzle_row_tell(const DRIZZLE_RES *res)
 
455
DRIZZLE_ROW_OFFSET drizzleclient_row_tell(const DRIZZLE_RES *res)
456
456
{
457
457
  return res->data_cursor;
458
458
}
459
459
 
460
 
DRIZZLE_FIELD_OFFSET drizzle_field_tell(const DRIZZLE_RES *res)
 
460
DRIZZLE_FIELD_OFFSET drizzleclient_field_tell(const DRIZZLE_RES *res)
461
461
{
462
462
  return res->current_field;
463
463
}
464
464
 
465
465
/* DRIZZLE */
466
466
 
467
 
unsigned int drizzle_field_count(const DRIZZLE *drizzle)
 
467
unsigned int drizzleclient_field_count(const DRIZZLE *drizzle)
468
468
{
469
469
  return drizzle->field_count;
470
470
}
471
471
 
472
 
uint64_t drizzle_affected_rows(const DRIZZLE *drizzle)
 
472
uint64_t drizzleclient_affected_rows(const DRIZZLE *drizzle)
473
473
{
474
474
  return drizzle->affected_rows;
475
475
}
476
476
 
477
 
uint64_t drizzle_insert_id(const DRIZZLE *drizzle)
 
477
uint64_t drizzleclient_insert_id(const DRIZZLE *drizzle)
478
478
{
479
479
  return drizzle->insert_id;
480
480
}
481
481
 
482
 
const char * drizzle_sqlstate(const DRIZZLE *drizzle)
 
482
const char * drizzleclient_sqlstate(const DRIZZLE *drizzle)
483
483
{
484
 
  return drizzle ? drizzle->net.sqlstate : sqlstate_get_cant_connect();
 
484
  return drizzle ? drizzle->net.sqlstate : drizzleclient_sqlstate_get_cant_connect();
485
485
}
486
486
 
487
 
uint32_t drizzle_warning_count(const DRIZZLE *drizzle)
 
487
uint32_t drizzleclient_warning_count(const DRIZZLE *drizzle)
488
488
{
489
489
  return drizzle->warning_count;
490
490
}
491
491
 
492
 
const char * drizzle_info(const DRIZZLE *drizzle)
 
492
const char * drizzleclient_info(const DRIZZLE *drizzle)
493
493
{
494
494
  return drizzle->info;
495
495
}
496
496
 
497
 
uint32_t drizzle_thread_id(const DRIZZLE *drizzle)
 
497
uint32_t drizzleclient_thread_id(const DRIZZLE *drizzle)
498
498
{
499
499
  return drizzle->thread_id;
500
500
}
504
504
****************************************************************************/
505
505
 
506
506
/*
507
 
  Functions called my my_net_init() to set some application specific variables
 
507
  Functions called my drizzleclient_net_init() to set some application specific variables
508
508
*/
509
509
 
510
 
void my_net_local_init(NET *net)
 
510
void drizzleclient_net_local_init(NET *net)
511
511
{
512
512
  net->max_packet=   (uint32_t) net_buffer_length;
513
 
  my_net_set_read_timeout(net, CLIENT_NET_READ_TIMEOUT);
514
 
  my_net_set_write_timeout(net, CLIENT_NET_WRITE_TIMEOUT);
 
513
  drizzleclient_net_set_read_timeout(net, CLIENT_NET_READ_TIMEOUT);
 
514
  drizzleclient_net_set_write_timeout(net, CLIENT_NET_WRITE_TIMEOUT);
515
515
  net->retry_count=  1;
516
516
  net->max_packet_size= (net_buffer_length > max_allowed_packet) ?
517
517
    net_buffer_length : max_allowed_packet;
524
524
*/
525
525
 
526
526
uint32_t
527
 
drizzle_escape_string(char *to,const char *from, uint32_t length)
 
527
drizzleclient_escape_string(char *to,const char *from, uint32_t length)
528
528
{
529
529
  const char *to_start= to;
530
530
  const char *end, *to_end=to_start + 2*length;
593
593
  return overflow ? (size_t) -1 : (size_t) (to - to_start);
594
594
}
595
595
 
596
 
int cli_unbuffered_fetch(DRIZZLE *drizzle, char **row)
 
596
int drizzleclient_cli_unbuffered_fetch(DRIZZLE *drizzle, char **row)
597
597
{
598
 
  if (packet_error == cli_safe_read(drizzle))
 
598
  if (packet_error == drizzleclient_cli_safe_read(drizzle))
599
599
    return 1;
600
600
 
601
601
  *row= ((drizzle->net.read_pos[0] == DRIZZLE_PROTOCOL_NO_MORE_DATA) ? NULL :
611
611
  Commit the current transaction
612
612
*/
613
613
 
614
 
bool drizzle_commit(DRIZZLE *drizzle)
 
614
bool drizzleclient_commit(DRIZZLE *drizzle)
615
615
{
616
 
  return((bool) drizzle_real_query(drizzle, "commit", 6));
 
616
  return((bool) drizzleclient_real_query(drizzle, "commit", 6));
617
617
}
618
618
 
619
619
/*
620
620
  Rollback the current transaction
621
621
*/
622
622
 
623
 
bool drizzle_rollback(DRIZZLE *drizzle)
 
623
bool drizzleclient_rollback(DRIZZLE *drizzle)
624
624
{
625
 
  return((bool) drizzle_real_query(drizzle, "rollback", 8));
 
625
  return((bool) drizzleclient_real_query(drizzle, "rollback", 8));
626
626
}
627
627
 
628
628
 
630
630
  Set autocommit to either true or false
631
631
*/
632
632
 
633
 
bool drizzle_autocommit(DRIZZLE *drizzle, bool auto_mode)
 
633
bool drizzleclient_autocommit(DRIZZLE *drizzle, bool auto_mode)
634
634
{
635
 
  return((bool) drizzle_real_query(drizzle, auto_mode ?
 
635
  return((bool) drizzleclient_real_query(drizzle, auto_mode ?
636
636
                                         "set autocommit=1":"set autocommit=0",
637
637
                                         16));
638
638
}
644
644
 
645
645
/*
646
646
  Returns true/false to indicate whether any more query results exist
647
 
  to be read using drizzle_next_result()
 
647
  to be read using drizzleclient_next_result()
648
648
*/
649
649
 
650
 
bool drizzle_more_results(const DRIZZLE *drizzle)
 
650
bool drizzleclient_more_results(const DRIZZLE *drizzle)
651
651
{
652
652
  return (drizzle->server_status & SERVER_MORE_RESULTS_EXISTS) ? true:false;
653
653
}
656
656
/*
657
657
  Reads and returns the next query results
658
658
*/
659
 
int drizzle_next_result(DRIZZLE *drizzle)
 
659
int drizzleclient_next_result(DRIZZLE *drizzle)
660
660
{
661
661
  if (drizzle->status != DRIZZLE_STATUS_READY)
662
662
  {
663
 
    drizzle_set_error(drizzle, CR_COMMANDS_OUT_OF_SYNC, sqlstate_get_unknown());
 
663
    drizzleclient_set_error(drizzle, CR_COMMANDS_OUT_OF_SYNC, drizzleclient_sqlstate_get_unknown());
664
664
    return(1);
665
665
  }
666
666
 
667
 
  net_clear_error(&drizzle->net);
 
667
  drizzleclient_drizzleclient_net_clear_error(&drizzle->net);
668
668
  drizzle->affected_rows= ~(uint64_t) 0;
669
669
 
670
670
  if (drizzle->server_status & SERVER_MORE_RESULTS_EXISTS)
674
674
}
675
675
 
676
676
 
677
 
DRIZZLE_RES * drizzle_use_result(DRIZZLE *drizzle)
 
677
DRIZZLE_RES * drizzleclient_use_result(DRIZZLE *drizzle)
678
678
{
679
679
  return (*drizzle->methods->use_result)(drizzle);
680
680
}
681
681
 
682
 
bool drizzle_read_query_result(DRIZZLE *drizzle)
 
682
bool drizzleclient_read_query_result(DRIZZLE *drizzle)
683
683
{
684
684
  return (*drizzle->methods->read_query_result)(drizzle);
685
685
}