00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <config.h>
00021
00022 #include "quote.h"
00023
00024 #include <drizzled/lex_string.h>
00025
00026 namespace drizzled
00027 {
00028
00029 inline
00030 static uint32_t get_esc_bit(unsigned char *mask, unsigned char num)
00031 {
00032 return (1 & (*((mask) + ((num) >> 3))) >> ((num) & 7));
00033 }
00034
00052 String *Item_func_quote::val_str(String *str)
00053 {
00054 assert(fixed == 1);
00055
00056
00057
00058
00059
00060 static unsigned char escmask[32]=
00061 {
00062 0x01, 0x00, 0x00, 0x04, 0x80, 0x00, 0x00, 0x00,
00063 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00,
00064 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
00065 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
00066 };
00067
00068 char *from, *to, *end, *start;
00069 String *arg= args[0]->val_str(str);
00070 uint32_t arg_length, new_length;
00071 if (!arg)
00072 {
00073
00074 str->copy(STRING_WITH_LEN("NULL"), collation.collation);
00075 null_value= 0;
00076 return str;
00077 }
00078
00079 arg_length= arg->length();
00080 new_length= arg_length+2;
00081
00082 for (from= (char*) arg->ptr(), end= from + arg_length; from < end; from++)
00083 new_length+= get_esc_bit(escmask, (unsigned char) *from);
00084
00085 tmp_value.alloc(new_length);
00086
00087
00088
00089
00090 to= (char*) tmp_value.ptr() + new_length - 1;
00091 *to--= '\'';
00092 for (start= (char*) arg->ptr(),end= start + arg_length; end-- != start; to--)
00093 {
00094
00095
00096
00097
00098 switch (*end) {
00099 case 0:
00100 *to--= '0';
00101 *to= '\\';
00102 break;
00103 case '\032':
00104 *to--= 'Z';
00105 *to= '\\';
00106 break;
00107 case '\'':
00108 case '\\':
00109 *to--= *end;
00110 *to= '\\';
00111 break;
00112 default:
00113 *to= *end;
00114 break;
00115 }
00116 }
00117 *to= '\'';
00118 tmp_value.length(new_length);
00119 tmp_value.set_charset(collation.collation);
00120 null_value= 0;
00121 return &tmp_value;
00122 }
00123
00124 }