~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/item/param.cc

  • Committer: Monty Taylor
  • Date: 2009-01-09 07:02:24 UTC
  • mto: (779.1.2 devel)
  • mto: This revision was merged to the branch mainline in revision 784.
  • Revision ID: mordred@inaugust.com-20090109070224-prwl5p52mfql3zfw
Split out readline.

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
 */
19
19
 
20
20
#include <drizzled/server_includes.h>
 
21
#include CSTDINT_H
21
22
#include <drizzled/session.h>
22
23
#include <drizzled/item/uint.h>
23
24
#include <drizzled/item/null.h>
24
25
#include <drizzled/item/float.h>
25
26
#include <drizzled/item/param.h>
26
 
#include <drizzled/sql_string.h>
27
 
#include <mystrings/utf8.h>
 
27
#include CMATH_H
28
28
 
29
29
Item *Item_param::safe_charset_converter(const CHARSET_INFO * const tocs)
30
30
{
128
128
  char *end;
129
129
 
130
130
  end= str+length;
131
 
  str2my_decimal((uint32_t)E_DEC_FATAL_ERROR, str, &decimal_value, &end);
 
131
  str2my_decimal((uint)E_DEC_FATAL_ERROR, str, &decimal_value, &end);
132
132
  state= DECIMAL_VALUE;
133
133
  decimals= decimal_value.frac;
134
134
  max_length= my_decimal_precision_to_length(decimal_value.precision(),
504
504
  {
505
505
    if (str->reserve(MAX_DATE_STRING_REP_LENGTH))
506
506
      break;
507
 
    str->length((uint32_t) my_TIME_to_str(&value.time, (char*) str->ptr()));
 
507
    str->length((uint) my_TIME_to_str(&value.time, (char*) str->ptr()));
508
508
    str->set_charset(&my_charset_bin);
509
509
    return str;
510
510
  }
516
516
  return str;
517
517
}
518
518
 
519
 
/* TODO: fact next two functions out */
520
 
/**
521
 
    Transforms a string into "" or its expression in 0x... form.
522
 
*/
523
 
 
524
 
static char *str_to_hex(char *to, const char *from, uint32_t len)
525
 
{
526
 
  if (len)
527
 
  {
528
 
    *to++= '0';
529
 
    *to++= 'x';
530
 
    to= drizzleclient_drizzleclient_octet2hex(to, from, len);
531
 
  }
532
 
  else
533
 
    to= strcpy(to, "\"\"")+2;
534
 
  return to;                               // pointer to end 0 of 'to'
535
 
}
536
 
 
537
 
 
538
 
 
539
 
/*
540
 
  Add escape characters to a string (blob?) to make it suitable for a insert
541
 
  to should at least have place for length*2+1 chars
542
 
  Returns the length of the to string
543
 
*/
544
 
 
545
 
uint32_t
546
 
drizzleclient_escape_string(char *to,const char *from, uint32_t length)
547
 
{
548
 
  const char *to_start= to;
549
 
  const char *end, *to_end=to_start + 2*length;
550
 
  bool overflow= false;
551
 
  for (end= from + length; from < end; from++)
552
 
  {
553
 
    uint32_t tmp_length;
554
 
    char escape= 0;
555
 
    if (!U8_IS_SINGLE(*from))
556
 
    {
557
 
      tmp_length= U8_LENGTH(*(uint32_t*)from);
558
 
      if (to + tmp_length > to_end)
559
 
      {
560
 
        overflow= true;
561
 
        break;
562
 
      }
563
 
      while (tmp_length--)
564
 
        *to++= *from++;
565
 
      from--;
566
 
      continue;
567
 
    }
568
 
    switch (*from) {
569
 
    case 0:                             /* Must be escaped for 'mysql' */
570
 
      escape= '0';
571
 
      break;
572
 
    case '\n':                          /* Must be escaped for logs */
573
 
      escape= 'n';
574
 
      break;
575
 
    case '\r':
576
 
      escape= 'r';
577
 
      break;
578
 
    case '\\':
579
 
      escape= '\\';
580
 
      break;
581
 
    case '\'':
582
 
      escape= '\'';
583
 
      break;
584
 
    case '"':                           /* Better safe than sorry */
585
 
      escape= '"';
586
 
      break;
587
 
    case '\032':                        /* This gives problems on Win32 */
588
 
      escape= 'Z';
589
 
      break;
590
 
    }
591
 
    if (escape)
592
 
    {
593
 
      if (to + 2 > to_end)
594
 
      {
595
 
        overflow= true;
596
 
        break;
597
 
      }
598
 
      *to++= '\\';
599
 
      *to++= escape;
600
 
    }
601
 
    else
602
 
    {
603
 
      if (to + 1 > to_end)
604
 
      {
605
 
        overflow= true;
606
 
        break;
607
 
      }
608
 
      *to++= *from;
609
 
    }
610
 
  }
611
 
  *to= 0;
612
 
  return overflow ? (size_t) -1 : (size_t) (to - to_start);
613
 
}
614
 
 
615
 
/**
616
 
  Append a version of the 'from' string suitable for use in a query to
617
 
  the 'to' string.  To generate a correct escaping, the character set
618
 
  information in 'csinfo' is used.
619
 
*/
620
 
 
621
 
static int
622
 
append_query_string(const CHARSET_INFO * const csinfo,
623
 
                    String const *from, String *to)
624
 
{
625
 
  char *beg, *ptr;
626
 
  uint32_t const orig_len= to->length();
627
 
  if (to->reserve(orig_len + from->length()*2+3))
628
 
    return 1;
629
 
 
630
 
  beg= to->c_ptr_quick() + to->length();
631
 
  ptr= beg;
632
 
  if (csinfo->escape_with_backslash_is_dangerous)
633
 
    ptr= str_to_hex(ptr, from->ptr(), from->length());
634
 
  else
635
 
  {
636
 
    *ptr++= '\'';
637
 
    ptr+= drizzleclient_escape_string(ptr, from->ptr(), from->length());
638
 
    *ptr++='\'';
639
 
  }
640
 
  to->length(orig_len + ptr - beg);
641
 
  return 0;
642
 
}
643
 
 
644
 
 
645
519
/**
646
520
  Return Param item values in string format, for generating the dynamic
647
521
  query used in update/binary logs.
682
556
      buf= str->c_ptr_quick();
683
557
      ptr= buf;
684
558
      *ptr++= '\'';
685
 
      ptr+= (uint32_t) my_TIME_to_str(&value.time, ptr);
 
559
      ptr+= (uint) my_TIME_to_str(&value.time, ptr);
686
560
      *ptr++= '\'';
687
561
      str->length((uint32_t) (ptr - buf));
688
562
      break;