diff options
author | monty@mashka.mysql.fi <> | 2002-10-02 17:55:12 +0300 |
---|---|---|
committer | monty@mashka.mysql.fi <> | 2002-10-02 17:55:12 +0300 |
commit | 0f394576131c2f66046673420ca8b1311981a614 (patch) | |
tree | a7ce8fe730bd0e06166bb15edcbe5aef252c2c3b /client | |
parent | 7377f546316b13c2563fede4c3ed2506d29a93ac (diff) | |
parent | 4d705988d562cac60d44e22f8d6e76df63432c72 (diff) | |
download | mariadb-git-0f394576131c2f66046673420ca8b1311981a614.tar.gz |
Merge with 4.0.4
Diffstat (limited to 'client')
-rw-r--r-- | client/mysql.cc | 86 | ||||
-rw-r--r-- | client/mysqldump.c | 11 | ||||
-rw-r--r-- | client/mysqltest.c | 143 | ||||
-rw-r--r-- | client/sql_string.cc | 206 | ||||
-rw-r--r-- | client/sql_string.h | 94 |
5 files changed, 376 insertions, 164 deletions
diff --git a/client/mysql.cc b/client/mysql.cc index 33e72946cda..7341132b8e0 100644 --- a/client/mysql.cc +++ b/client/mysql.cc @@ -40,7 +40,7 @@ #include <signal.h> #include <violite.h> -const char *VER= "12.14"; +const char *VER= "13.0"; /* Don't try to make a nice table if the data is too big */ #define MAX_COLUMN_LENGTH 1024 @@ -346,6 +346,7 @@ int main(int argc,char *argv[]) if (!status.batch) ignore_errors=1; // Don't abort monitor signal(SIGINT, mysql_end); // Catch SIGINT to clean up + signal(SIGQUIT, mysql_end); // Catch SIGQUIT to clean up /* Run in interactive mode like the ingres/postgres monitor @@ -871,14 +872,14 @@ static COMMANDS *find_command (char *name,char cmd_char) } else { - while (isspace(*name)) + while (my_isspace(system_charset_info,*name)) name++; if (strchr(name,';') || strstr(name,"\\g")) return ((COMMANDS *) 0); if ((end=strcont(name," \t"))) { len=(uint) (end - name); - while (isspace(*end)) + while (my_isspace(system_charset_info,*end)) end++; if (!*end) end=0; // no arguments to function @@ -890,7 +891,8 @@ static COMMANDS *find_command (char *name,char cmd_char) for (uint i= 0; commands[i].name; i++) { if (commands[i].func && - ((name && !my_casecmp(name,commands[i].name,len) && + ((name && + !my_strncasecmp(system_charset_info,name,commands[i].name,len) && !commands[i].name[len] && (!end || (end && commands[i].takes_params))) || !name && commands[i].cmd_char == cmd_char)) @@ -918,12 +920,13 @@ static bool add_line(String &buffer,char *line,char *in_string) for (pos=out=line ; (inchar= (uchar) *pos) ; pos++) { - if (isspace(inchar) && out == line && buffer.is_empty()) + if (my_isspace(system_charset_info,inchar) && out == line && + buffer.is_empty()) continue; #ifdef USE_MB int l; - if (use_mb(default_charset_info) && - (l = my_ismbchar(default_charset_info, pos, strend))) { + if (use_mb(system_charset_info) && + (l = my_ismbchar(system_charset_info, pos, strend))) { while (l--) *out++ = *pos++; pos--; @@ -986,7 +989,7 @@ static bool add_line(String &buffer,char *line,char *in_string) } else if (!*in_string && (inchar == '#' || inchar == '-' && pos[1] == '-' && - isspace(pos[2]))) + my_isspace(system_charset_info,pos[2]))) break; // comment to end of line else { // Add found char to buffer @@ -1362,9 +1365,9 @@ com_clear(String *buffer,char *line __attribute__((unused))) static int com_go(String *buffer,char *line __attribute__((unused))) { - char buff[160],time_buff[32]; + char buff[200], time_buff[32], *pos; MYSQL_RES *result; - ulong timer; + ulong timer, warnings; uint error=0; if (!status.batch) @@ -1393,7 +1396,8 @@ com_go(String *buffer,char *line __attribute__((unused))) (void) com_print(buffer,0); if (skip_updates && - (buffer->length() < 4 || my_sortcmp(buffer->ptr(),"SET ",4))) + (buffer->length() < 4 || my_sortcmp(system_charset_info,buffer->ptr(), + "SET ",4))) { (void) put_info("Ignoring query to other database",INFO_INFO); return 0; @@ -1446,7 +1450,7 @@ com_go(String *buffer,char *line __attribute__((unused))) { if (!mysql_num_rows(result) && ! quick) { - sprintf(buff,"Empty set%s",time_buff); + strmov(buff, "Empty set"); } else { @@ -1461,20 +1465,30 @@ com_go(String *buffer,char *line __attribute__((unused))) print_tab_data(result); else print_table_data(result); - sprintf(buff,"%ld %s in set%s", + sprintf(buff,"%ld %s in set", (long) mysql_num_rows(result), - (long) mysql_num_rows(result) == 1 ? "row" : "rows", - time_buff); + (long) mysql_num_rows(result) == 1 ? "row" : "rows"); end_pager(); } } else if (mysql_affected_rows(&mysql) == ~(ulonglong) 0) - sprintf(buff,"Query OK%s",time_buff); + strmov(buff,"Query OK"); else - sprintf(buff,"Query OK, %ld %s affected%s", + sprintf(buff,"Query OK, %ld %s affected", (long) mysql_affected_rows(&mysql), - (long) mysql_affected_rows(&mysql) == 1 ? "row" : "rows", - time_buff); + (long) mysql_affected_rows(&mysql) == 1 ? "row" : "rows"); + + pos=strend(buff); + if ((warnings= mysql_warning_count(&mysql))) + { + *pos++= ','; + *pos++= ' '; + pos=int2str(warnings, pos, 10); + pos=strmov(pos, " warning"); + if (warnings != 1) + *pos++= 's'; + } + strmov(pos, time_buff); put_info(buff,INFO_RESULT); if (mysql_info(&mysql)) put_info(mysql_info(&mysql),INFO_RESULT); @@ -1771,8 +1785,9 @@ safe_put_field(const char *pos,ulong length) { #ifdef USE_MB int l; - if (use_mb(default_charset_info) && - (l = my_ismbchar(default_charset_info, pos, end))) { + if (use_mb(system_charset_info) && + (l = my_ismbchar(system_charset_info, pos, end))) + { while (l--) tee_putc(*pos++, PAGER); pos--; @@ -1832,7 +1847,7 @@ com_tee(String *buffer, char *line __attribute__((unused))) if (status.batch) return 0; - while (isspace(*line)) + while (my_isspace(system_charset_info,*line)) line++; if (!(param = strchr(line, ' '))) // if outfile wasn't given, use the default { @@ -1851,11 +1866,12 @@ com_tee(String *buffer, char *line __attribute__((unused))) } /* eliminate the spaces before the parameters */ - while (isspace(*param)) + while (my_isspace(system_charset_info,*param)) param++; end= strmake(file_name, param, sizeof(file_name) - 1); /* remove end space from command line */ - while (end > file_name && (isspace(end[-1]) || iscntrl(end[-1]))) + while (end > file_name && (my_isspace(system_charset_info,end[-1]) || + my_iscntrl(system_charset_info,end[-1]))) end--; end[0]= 0; if (end == file_name) @@ -1895,7 +1911,7 @@ com_pager(String *buffer, char *line __attribute__((unused))) if (status.batch) return 0; /* Skip space from file name */ - while (isspace(*line)) + while (my_isspace(system_charset_info,*line)) line++; if (!(param = strchr(line, ' '))) // if pager was not given, use the default { @@ -1911,10 +1927,11 @@ com_pager(String *buffer, char *line __attribute__((unused))) } else { - while (isspace(*param)) + while (my_isspace(system_charset_info,*param)) param++; end=strmake(pager_name, param, sizeof(pager_name)-1); - while (end > pager_name && (isspace(end[-1]) || iscntrl(end[-1]))) + while (end > pager_name && (my_isspace(system_charset_info,end[-1]) || + my_iscntrl(system_charset_info,end[-1]))) end--; end[0]=0; strmov(pager, pager_name); @@ -2050,7 +2067,7 @@ com_connect(String *buffer, char *line) if (buffer) { - while (isspace(*line)) + while (my_isspace(system_charset_info,*line)) line++; strnmov(buff,line,sizeof(buff)-1); // Don't destroy history if (buff[0] == '\\') // Short command @@ -2096,15 +2113,16 @@ static int com_source(String *buffer, char *line) FILE *sql_file; /* Skip space from file name */ - while (isspace(*line)) + while (my_isspace(system_charset_info,*line)) line++; if (!(param = strchr(line, ' '))) // Skip command name return put_info("Usage: \\. <filename> | source <filename>", INFO_ERROR, 0); - while (isspace(*param)) + while (my_isspace(system_charset_info,*param)) param++; end=strmake(source_name,param,sizeof(source_name)-1); - while (end > source_name && (isspace(end[-1]) || iscntrl(end[-1]))) + while (end > source_name && (my_isspace(system_charset_info,end[-1]) || + my_iscntrl(system_charset_info,end[-1]))) end--; end[0]=0; unpack_filename(source_name,source_name); @@ -2145,7 +2163,7 @@ com_use(String *buffer __attribute__((unused)), char *line) char *tmp; char buff[256]; - while (isspace(*line)) + while (my_isspace(system_charset_info,*line)) line++; strnmov(buff,line,sizeof(buff)-1); // Don't destroy history if (buff[0] == '\\') // Short command @@ -2329,7 +2347,7 @@ com_status(String *buffer __attribute__((unused)), tee_fprintf(stdout, "Protocol version:\t%d\n", mysql_get_proto_info(&mysql)); tee_fprintf(stdout, "Connection:\t\t%s\n", mysql_get_host_info(&mysql)); tee_fprintf(stdout, "Client characterset:\t%s\n", - default_charset_info->name); + system_charset_info->name); tee_fprintf(stdout, "Server characterset:\t%s\n", mysql.charset->name); if (strstr(mysql_get_host_info(&mysql),"TCP/IP") || ! mysql.unix_socket) tee_fprintf(stdout, "TCP port:\t\t%d\n", mysql.port); @@ -2438,7 +2456,7 @@ static void remove_cntrl(String &buffer) { char *start,*end; end=(start=(char*) buffer.ptr())+buffer.length(); - while (start < end && !isgraph(end[-1])) + while (start < end && !my_isgraph(system_charset_info,end[-1])) end--; buffer.length((uint) (end-start)); } diff --git a/client/mysqldump.c b/client/mysqldump.c index 39e9714660a..21358272109 100644 --- a/client/mysqldump.c +++ b/client/mysqldump.c @@ -518,7 +518,7 @@ static my_bool test_if_special_chars(const char *str) { #if MYSQL_VERSION_ID >= 32300 for ( ; *str ; str++) - if (!isvar(*str) && *str != '$') + if (!my_isvar(system_charset_info,*str) && *str != '$') return 1; #endif return 0; @@ -1036,7 +1036,8 @@ static void dumpTable(uint numFields, char *table) /* change any strings ("inf","nan",..) into NULL */ char *ptr = row[i]; dynstr_append(&extended_row, - (!isalpha(*ptr)) ? ptr : "NULL"); + (!my_isalpha(system_charset_info,*ptr)) ? + ptr : "NULL"); } } else @@ -1068,9 +1069,11 @@ static void dumpTable(uint numFields, char *table) char *ptr = row[i]; if (opt_xml) fprintf(md_result_file, "\t\t<%s>%s</%s>\n", - field->name,!isalpha(*ptr) ?ptr: "NULL",field->name); + field->name, + !my_isalpha(system_charset_info,*ptr) ?ptr: "NULL",field->name); else - fputs((!isalpha(*ptr)) ? ptr : "NULL", md_result_file); + fputs((!my_isalpha(system_charset_info,*ptr)) ? + ptr : "NULL", md_result_file); } } else diff --git a/client/mysqltest.c b/client/mysqltest.c index 533713b4f43..036130f2d80 100644 --- a/client/mysqltest.c +++ b/client/mysqltest.c @@ -311,7 +311,7 @@ static int eval_result = 0; void mysql_enable_rpl_parse(MYSQL* mysql __attribute__((unused))) {} void mysql_disable_rpl_parse(MYSQL* mysql __attribute__((unused))) {} int mysql_rpl_parse_enabled(MYSQL* mysql __attribute__((unused))) { return 1; } -int mysql_rpl_probe(MYSQL *mysql __attribute__((unused))) { return 1; } +my_bool mysql_rpl_probe(MYSQL *mysql __attribute__((unused))) { return 1; } #endif #define MAX_SERVER_ARGS 20 @@ -482,9 +482,9 @@ void init_parser() int hex_val(int c) { - if (isdigit(c)) + if (my_isdigit(system_charset_info,c)) return c - '0'; - else if ((c = tolower(c)) >= 'a' && c <= 'f') + else if ((c = my_tolower(system_charset_info,c)) >= 'a' && c <= 'f') return c - 'a' + 10; else return -1; @@ -594,7 +594,7 @@ VAR* var_get(const char* var_name, const char** var_name_end, my_bool raw, { const char* save_var_name = var_name, *end; end = (var_name_end) ? *var_name_end : 0; - while (isvar(*var_name) && var_name != end) + while (my_isvar(system_charset_info,*var_name) && var_name != end) ++var_name; if (var_name == save_var_name) { @@ -757,7 +757,7 @@ int do_server_op(struct st_query* q,const char* op) com_p=strmov(com_p,"_exec "); if (!*p) die("Missing server name in server_%s\n",op); - while (*p && !isspace(*p)) + while (*p && !my_isspace(system_charset_info,*p)) { *com_p++=*p++; } @@ -790,7 +790,7 @@ int do_require_version(struct st_query* q) if (!*p) die("Missing version argument in require_version\n"); ver_arg = p; - while (*p && !isspace(*p)) + while (*p && !my_isspace(system_charset_info,*p)) p++; *p = 0; ver_arg_len = p - ver_arg; @@ -820,7 +820,7 @@ int do_source(struct st_query* q) if (!*p) die("Missing file name in source\n"); name = p; - while (*p && !isspace(*p)) + while (*p && !my_isspace(system_charset_info,*p)) p++; *p = 0; @@ -1057,11 +1057,11 @@ int do_let(struct st_query* q) if (!*p) die("Missing variable name in let\n"); var_name = p; - while (*p && (*p != '=' || isspace(*p))) + while (*p && (*p != '=' || my_isspace(system_charset_info,*p))) p++; var_name_end = p; if (*p == '=') p++; - while (*p && isspace(*p)) + while (*p && my_isspace(system_charset_info,*p)) p++; var_val_start = p; return var_set(var_name, var_name_end, var_val_start, q->end); @@ -1090,8 +1090,8 @@ int do_disable_rpl_parse(struct st_query* q __attribute__((unused))) int do_sleep(struct st_query* q, my_bool real_sleep) { - char* p=q->first_argument; - while (*p && isspace(*p)) + char *p=q->first_argument; + while (*p && my_isspace(system_charset_info,*p)) p++; if (!*p) die("Missing argument in sleep\n"); @@ -1107,7 +1107,7 @@ static void get_file_name(char *filename, struct st_query* q) char* p=q->first_argument; strnmov(filename, p, FN_REFLEN); /* Remove end space */ - while (p > filename && isspace(p[-1])) + while (p > filename && my_isspace(system_charset_info,p[-1])) p--; p[0]=0; } @@ -1193,7 +1193,7 @@ static char *get_string(char **to_ptr, char **from_ptr, if (*from != ' ' && *from) die("Wrong string argument in %s\n", q->query); - while (isspace(*from)) /* Point to next string */ + while (my_isspace(system_charset_info,*from)) /* Point to next string */ from++; *to =0; /* End of string marker */ @@ -1250,8 +1250,8 @@ static void get_replace(struct st_query *q) insert_pointer_name(&to_array,to); } for (i=1,pos=word_end_chars ; i < 256 ; i++) - if (isspace(i)) - *pos++=i; + if (my_isspace(system_charset_info,i)) + *pos++= i; *pos=0; /* End pointer */ if (!(glob_replace=init_replace((char**) from_array.typelib.type_names, (char**) to_array.typelib.type_names, @@ -1287,7 +1287,7 @@ int select_connection(char *p) if (!*p) die("Missing connection name in connect\n"); name = p; - while (*p && !isspace(*p)) + while (*p && !my_isspace(system_charset_info,*p)) p++; *p = 0; @@ -1313,7 +1313,7 @@ int close_connection(struct st_query* q) if (!*p) die("Missing connection name in connect\n"); name = p; - while (*p && !isspace(*p)) + while (*p && !my_isspace(system_charset_info,*p)) p++; *p = 0; @@ -1349,11 +1349,13 @@ int close_connection(struct st_query* q) char* safe_get_param(char* str, char** arg, const char* msg) { DBUG_ENTER("safe_get_param"); - while (*str && isspace(*str)) str++; + while (*str && my_isspace(system_charset_info,*str)) + str++; *arg = str; for (; *str && *str != ',' && *str != ')' ; str++) { - if (isspace(*str)) *str = 0; + if (my_isspace(system_charset_info,*str)) + *str = 0; } if (!*str) die(msg); @@ -1635,7 +1637,7 @@ int read_line(char* buf, int size) { state = R_COMMENT; } - else if (isspace(c)) + else if (my_isspace(system_charset_info,c)) { if (c == '\n') start_lineno= ++*lineno; /* Query hasn't started yet */ @@ -1761,7 +1763,7 @@ int read_query(struct st_query** q_ptr) { expected_errno = 0; p++; - for (;isdigit(*p);p++) + for (;my_isdigit(system_charset_info,*p);p++) expected_errno = expected_errno * 10 + *p - '0'; q->expected_errno[0] = expected_errno; q->expected_errno[1] = 0; @@ -1769,25 +1771,28 @@ int read_query(struct st_query** q_ptr) } } - while (*p && isspace(*p)) p++ ; + while (*p && my_isspace(system_charset_info,*p)) + p++ ; if (*p == '@') { p++; p1 = q->record_file; - while (!isspace(*p) && + while (!my_isspace(system_charset_info,*p) && p1 < q->record_file + sizeof(q->record_file) - 1) *p1++ = *p++; *p1 = 0; } } - while (*p && isspace(*p)) p++; + while (*p && my_isspace(system_charset_info,*p)) + p++; if (!(q->query_buf=q->query=my_strdup(p,MYF(MY_WME)))) die(NullS); /* Calculate first word and first argument */ - for (p=q->query; *p && !isspace(*p) ; p++) ; + for (p=q->query; *p && !my_isspace(system_charset_info,*p) ; p++) ; q->first_word_len = (uint) (p - q->query); - while (*p && isspace(*p)) p++; + while (*p && my_isspace(system_charset_info,*p)) + p++; q->first_argument=p; q->end = strend(q->query); parser.read_lines++; @@ -2028,6 +2033,36 @@ static void replace_dynstr_append_mem(DYNAMIC_STRING *ds, const char *val, dynstr_append_mem(ds, val, len); } +/* + Append all results to the dynamic string separated with '\t' +*/ + +static void append_result(DYNAMIC_STRING *ds, MYSQL_RES *res) +{ + MYSQL_ROW row; + int num_fields= mysql_num_fields(res); + unsigned long *lengths; + while ((row = mysql_fetch_row(res))) + { + int i; + lengths = mysql_fetch_lengths(res); + for (i = 0; i < num_fields; i++) + { + const char *val= row[i]; + ulonglong len= lengths[i]; + if (!val) + { + val = "NULL"; + len = 4; + } + if (i) + dynstr_append_mem(ds, "\t", 1); + replace_dynstr_append_mem(ds, val, len); + } + dynstr_append_mem(ds, "\n", 1); + } +} + /* * flags control the phased/stages of query execution to be performed @@ -2038,12 +2073,7 @@ static void replace_dynstr_append_mem(DYNAMIC_STRING *ds, const char *val, int run_query(MYSQL* mysql, struct st_query* q, int flags) { MYSQL_RES* res = 0; - MYSQL_FIELD* fields; - MYSQL_ROW row; - int num_fields,i, error = 0; - unsigned long* lengths; - char* val; - int len; + int i, error = 0; DYNAMIC_STRING *ds; DYNAMIC_STRING ds_tmp; DYNAMIC_STRING eval_query; @@ -2152,45 +2182,37 @@ int run_query(MYSQL* mysql, struct st_query* q, int flags) goto end; } - if (!res) - goto end; - - if (!disable_result_log) + if (!disable_result_log && res) { - fields = mysql_fetch_fields(res); - num_fields = mysql_num_fields(res); + int num_fields= mysql_num_fields(res); + MYSQL_FIELD *fields= mysql_fetch_fields(res); for (i = 0; i < num_fields; i++) { if (i) dynstr_append_mem(ds, "\t", 1); dynstr_append(ds, fields[i].name); } - dynstr_append_mem(ds, "\n", 1); + append_result(ds, res); + } - while ((row = mysql_fetch_row(res))) + /* Add all warnings to the result */ + if (!disable_result_log && mysql_warning_count(mysql)) + { + MYSQL_RES *warn_res= mysql_warnings(mysql); + if (!warn_res) + verbose_msg("Warning count is %d but didn't get any warnings\n", + mysql_warning_count(mysql)); + else { - lengths = mysql_fetch_lengths(res); - for (i = 0; i < num_fields; i++) - { - val = (char*)row[i]; - len = lengths[i]; - - if (!val) - { - val = (char*)"NULL"; - len = 4; - } - - if (i) - dynstr_append_mem(ds, "\t", 1); - replace_dynstr_append_mem(ds, val, len); - } - dynstr_append_mem(ds, "\n", 1); + dynstr_append_mem(ds, "Warnings:\n", 10); + append_result(ds, warn_res); + mysql_free_result(warn_res); } - if (glob_replace) - free_replace(); } + if (glob_replace) + free_replace(); + if (record) { if (!q->record_file[0] && !result_file) @@ -2301,7 +2323,8 @@ static void var_from_env(const char* name, const char* def_val) static void init_var_hash() { VAR* v; - if (hash_init(&var_hash, 1024, 0, 0, get_var_key, var_free, MYF(0))) + if (hash_init(&var_hash, system_charset_info, + 1024, 0, 0, get_var_key, var_free, MYF(0))) die("Variable hash initialization failed"); var_from_env("MASTER_MYPORT", "9306"); var_from_env("SLAVE_MYPORT", "9307"); diff --git a/client/sql_string.cc b/client/sql_string.cc index 3c5e481eaad..cf9e9f62507 100644 --- a/client/sql_string.cc +++ b/client/sql_string.cc @@ -234,7 +234,7 @@ bool String::fill(uint32 max_length,char fill_char) void String::strip_sp() { - while (str_length && isspace(Ptr[str_length-1])) + while (str_length && my_isspace(str_charset,Ptr[str_length-1])) str_length--; } @@ -296,10 +296,10 @@ uint32 String::numchars() register uint32 n=0,mblen; register const char *mbstr=Ptr; register const char *end=mbstr+str_length; - if (use_mb(default_charset_info)) + if (use_mb(str_charset)) { while (mbstr < end) { - if ((mblen=my_ismbchar(default_charset_info, mbstr,end))) mbstr+=mblen; + if ((mblen=my_ismbchar(str_charset, mbstr,end))) mbstr+=mblen; else ++mbstr; ++n; } @@ -316,11 +316,11 @@ int String::charpos(int i,uint32 offset) register uint32 mblen; register const char *mbstr=Ptr+offset; register const char *end=Ptr+str_length; - if (use_mb(default_charset_info)) + if (use_mb(str_charset)) { if (i<=0) return i; while (i && mbstr < end) { - if ((mblen=my_ismbchar(default_charset_info, mbstr,end))) mbstr+=mblen; + if ((mblen=my_ismbchar(str_charset, mbstr,end))) mbstr+=mblen; else ++mbstr; --i; } @@ -361,6 +361,39 @@ skipp: return -1; } +/* + Search after a string without regarding to case + This needs to be replaced when we have character sets per string +*/ + +int String::strstr_case(const String &s,uint32 offset) +{ + if (s.length()+offset <= str_length) + { + if (!s.length()) + return ((int) offset); // Empty string is always found + + register const char *str = Ptr+offset; + register const char *search=s.ptr(); + const char *end=Ptr+str_length-s.length()+1; + const char *search_end=s.ptr()+s.length(); +skipp: + while (str != end) + { + if (str_charset->sort_order[*str++] == str_charset->sort_order[*search]) + { + register char *i,*j; + i=(char*) str; j=(char*) search+1; + while (j != search_end) + if (str_charset->sort_order[*i++] != + str_charset->sort_order[*j++]) + goto skipp; + return (int) (str-Ptr) -1; + } + } + } + return -1; +} /* ** Search string from end. Offset is offset to the end of string @@ -428,6 +461,44 @@ bool String::replace(uint32 offset,uint32 arg_length,const String &to) return FALSE; } +// added by Holyfoot for "geometry" needs +int String::reserve(uint32 space_needed, uint32 grow_by) +{ + if (Alloced_length < str_length + space_needed) + { + if (realloc(Alloced_length + max(space_needed, grow_by) - 1)) + return TRUE; + } + return FALSE; +} + +void String::qs_append(const char *str) +{ + int len = strlen(str); + memcpy(Ptr + str_length, str, len + 1); + str_length += len; +} + +void String::qs_append(double d) +{ + char *buff = Ptr + str_length; + sprintf(buff,"%.14g", d); + str_length += strlen(buff); +} + +void String::qs_append(double *d) +{ + double ld; + float8get(ld, d); + qs_append(ld); +} + +void String::qs_append(const char &c) +{ + Ptr[str_length] = c; + str_length += sizeof(c); +} + int sortcmp(const String *x,const String *y) { @@ -436,15 +507,15 @@ int sortcmp(const String *x,const String *y) uint32 x_len=x->length(),y_len=y->length(),len=min(x_len,y_len); #ifdef USE_STRCOLL - if (use_strcoll(default_charset_info)) + if (use_strcoll(x->str_charset)) { #ifndef CMP_ENDSPACE - while (x_len && isspace(s[x_len-1])) + while (x_len && my_isspace(x->str_charset,s[x_len-1])) x_len--; - while (y_len && isspace(t[y_len-1])) + while (y_len && my_isspace(x->str_charset,t[y_len-1])) y_len--; #endif - return my_strnncoll(default_charset_info, + return my_strnncoll(x->str_charset, (unsigned char *)s,x_len,(unsigned char *)t,y_len); } else @@ -454,9 +525,10 @@ int sortcmp(const String *x,const String *y) y_len-=len; while (len--) { - if (my_sort_order[(uchar) *s++] != my_sort_order[(uchar) *t++]) - return ((int) my_sort_order[(uchar) s[-1]] - - (int) my_sort_order[(uchar) t[-1]]); + if (x->str_charset->sort_order[(uchar) *s++] != + x->str_charset->sort_order[(uchar) *t++]) + return ((int) x->str_charset->sort_order[(uchar) s[-1]] - + (int) x->str_charset->sort_order[(uchar) t[-1]]); } #ifndef CMP_ENDSPACE /* Don't compare end space in strings */ @@ -465,14 +537,14 @@ int sortcmp(const String *x,const String *y) { const char *end=t+y_len; for (; t != end ; t++) - if (!isspace(*t)) + if (!my_isspace(x->str_charset,*t)) return -1; } else { const char *end=s+x_len; for (; s != end ; s++) - if (!isspace(*s)) + if (!my_isspace(x->str_charset,*s)) return 1; } return 0; @@ -520,11 +592,10 @@ String *copy_if_not_alloced(String *to,String *from,uint32 from_length) /* Make it easier to handle different charactersets */ #ifdef USE_MB -#define INC_PTR(A,B) A+=((use_mb_flag && \ - my_ismbchar(default_charset_info,A,B)) ? \ - my_ismbchar(default_charset_info,A,B) : 1) +#define INC_PTR(cs,A,B) A+=((use_mb_flag && \ + my_ismbchar(cs,A,B)) ? my_ismbchar(cs,A,B) : 1) #else -#define INC_PTR(A,B) A++ +#define INC_PTR(cs,A,B) A++ #endif /* @@ -535,18 +606,18 @@ String *copy_if_not_alloced(String *to,String *from,uint32 from_length) */ #ifdef LIKE_CMP_TOUPPER -#define likeconv(A) (uchar) toupper(A) +#define likeconv(s,A) (uchar) my_toupper(s,A) #else -#define likeconv(A) (uchar) my_sort_order[(uchar) (A)] +#define likeconv(s,A) (uchar) (s)->sort_order[(uchar) (A)] #endif -static int wild_case_compare(const char *str,const char *str_end, - const char *wildstr,const char *wildend, - char escape) +int wild_case_compare(CHARSET_INFO *cs, const char *str,const char *str_end, + const char *wildstr,const char *wildend, + char escape) { int result= -1; // Not found, using wildcards #ifdef USE_MB - bool use_mb_flag=use_mb(default_charset_info); + bool use_mb_flag=use_mb(cs); #endif while (wildstr != wildend) { @@ -557,7 +628,7 @@ static int wild_case_compare(const char *str,const char *str_end, #ifdef USE_MB int l; if (use_mb_flag && - (l = my_ismbchar(default_charset_info, wildstr, wildend))) + (l = my_ismbchar(cs, wildstr, wildend))) { if (str+l > str_end || memcmp(str, wildstr, l) != 0) return 1; @@ -566,7 +637,7 @@ static int wild_case_compare(const char *str,const char *str_end, } else #endif - if (str == str_end || likeconv(*wildstr++) != likeconv(*str++)) + if (str == str_end || likeconv(cs,*wildstr++) != likeconv(cs,*str++)) return(1); // No match if (wildstr == wildend) return (str != str_end); // Match if both are at end @@ -576,9 +647,9 @@ static int wild_case_compare(const char *str,const char *str_end, { do { - if (str == str_end) // Skipp one char if possible + if (str == str_end) // Skip one char if possible return (result); - INC_PTR(str,str_end); + INC_PTR(cs,str,str_end); } while (++wildstr < wildend && *wildstr == wild_one); if (wildstr == wildend) break; @@ -595,7 +666,7 @@ static int wild_case_compare(const char *str,const char *str_end, { if (str == str_end) return (-1); - INC_PTR(str,str_end); + INC_PTR(cs,str,str_end); continue; } break; // Not a wild character @@ -613,10 +684,10 @@ static int wild_case_compare(const char *str,const char *str_end, int mblen; LINT_INIT(mblen); if (use_mb_flag) - mblen = my_ismbchar(default_charset_info, wildstr, wildend); + mblen = my_ismbchar(cs, wildstr, wildend); #endif - INC_PTR(wildstr,wildend); // This is compared trough cmp - cmp=likeconv(cmp); + INC_PTR(cs,wildstr,wildend); // This is compared trough cmp + cmp=likeconv(cs,cmp); do { #ifdef USE_MB @@ -634,26 +705,26 @@ static int wild_case_compare(const char *str,const char *str_end, break; } } - else if (!my_ismbchar(default_charset_info, str, str_end) && - likeconv(*str) == cmp) + else if (!my_ismbchar(cs, str, str_end) && + likeconv(cs,*str) == cmp) { str++; break; } - INC_PTR(str, str_end); + INC_PTR(cs,str, str_end); } } else { #endif /* USE_MB */ - while (str != str_end && likeconv(*str) != cmp) + while (str != str_end && likeconv(cs,*str) != cmp) str++; if (str++ == str_end) return (-1); #ifdef USE_MB } #endif { - int tmp=wild_case_compare(str,str_end,wildstr,wildend,escape); + int tmp=wild_case_compare(cs,str,str_end,wildstr,wildend,escape); if (tmp <= 0) return (tmp); } @@ -667,17 +738,23 @@ static int wild_case_compare(const char *str,const char *str_end, int wild_case_compare(String &match,String &wild, char escape) { - return wild_case_compare(match.ptr(),match.ptr()+match.length(), - wild.ptr(), wild.ptr()+wild.length(),escape); + DBUG_ENTER("wild_case_compare"); + DBUG_PRINT("enter",("match='%s', wild='%s', escape='%c'" + ,match.ptr(),wild.ptr(),escape)); + DBUG_RETURN(wild_case_compare(match.str_charset,match.ptr(),match.ptr()+match.length(), + wild.ptr(), wild.ptr()+wild.length(),escape)); } /* ** The following is used when using LIKE on binary strings */ -static int wild_compare(const char *str,const char *str_end, - const char *wildstr,const char *wildend,char escape) +int wild_compare(const char *str,const char *str_end, + const char *wildstr,const char *wildend,char escape) { + DBUG_ENTER("wild_compare"); + DBUG_PRINT("enter",("str='%s', str_end='%s', wildstr='%s', wildend='%s', escape='%c'" + ,str,str_end,wildstr,wildend,escape)); int result= -1; // Not found, using wildcards while (wildstr != wildend) { @@ -686,17 +763,21 @@ static int wild_compare(const char *str,const char *str_end, if (*wildstr == escape && wildstr+1 != wildend) wildstr++; if (str == str_end || *wildstr++ != *str++) - return(1); + { + DBUG_RETURN(1); + } if (wildstr == wildend) - return (str != str_end); // Match if both are at end + { + DBUG_RETURN(str != str_end); // Match if both are at end + } result=1; // Found an anchor char } if (*wildstr == wild_one) { do { - if (str == str_end) // Skipp one char if possible - return (result); + if (str == str_end) // Skip one char if possible + DBUG_RETURN(result); str++; } while (*++wildstr == wild_one && wildstr != wildend); if (wildstr == wildend) @@ -713,17 +794,22 @@ static int wild_compare(const char *str,const char *str_end, if (*wildstr == wild_one) { if (str == str_end) - return (-1); + { + DBUG_RETURN(-1); + } str++; continue; } break; // Not a wild character } if (wildstr == wildend) - return(0); // Ok if wild_many is last + { + DBUG_RETURN(0); // Ok if wild_many is last + } if (str == str_end) - return -1; - + { + DBUG_RETURN(-1); + } char cmp; if ((cmp= *wildstr) == escape && wildstr+1 != wildend) cmp= *++wildstr; @@ -732,22 +818,32 @@ static int wild_compare(const char *str,const char *str_end, { while (str != str_end && *str != cmp) str++; - if (str++ == str_end) return (-1); + if (str++ == str_end) + { + DBUG_RETURN(-1); + } { int tmp=wild_compare(str,str_end,wildstr,wildend,escape); if (tmp <= 0) - return (tmp); + { + DBUG_RETURN(tmp); + } } } while (str != str_end && wildstr[0] != wild_many); - return(-1); + DBUG_RETURN(-1); } } - return (str != str_end ? 1 : 0); + DBUG_RETURN(str != str_end ? 1 : 0); } int wild_compare(String &match,String &wild, char escape) { - return wild_compare(match.ptr(),match.ptr()+match.length(), - wild.ptr(), wild.ptr()+wild.length(),escape); + DBUG_ENTER("wild_compare"); + DBUG_PRINT("enter",("match='%s', wild='%s', escape='%c'" + ,match.ptr(),wild.ptr(),escape)); + DBUG_RETURN(wild_compare(match.ptr(),match.ptr()+match.length(), + wild.ptr(), wild.ptr()+wild.length(),escape)); } + + diff --git a/client/sql_string.h b/client/sql_string.h index cffe78936a0..811e49a0d02 100644 --- a/client/sql_string.h +++ b/client/sql_string.h @@ -24,31 +24,57 @@ #define NOT_FIXED_DEC 31 #endif +class String; +int sortcmp(const String *a,const String *b); +int stringcmp(const String *a,const String *b); +String *copy_if_not_alloced(String *a,String *b,uint32 arg_length); +int wild_case_compare(String &match,String &wild,char escape); +int wild_compare(String &match,String &wild,char escape); + class String { char *Ptr; uint32 str_length,Alloced_length; bool alloced; + CHARSET_INFO *str_charset; public: String() - { Ptr=0; str_length=Alloced_length=0; alloced=0; } + { + Ptr=0; str_length=Alloced_length=0; alloced=0; + str_charset=default_charset_info; + } String(uint32 length_arg) - { alloced=0; Alloced_length=0; (void) real_alloc(length_arg); } + { + alloced=0; Alloced_length=0; (void) real_alloc(length_arg); + str_charset=default_charset_info; + } String(const char *str) - { Ptr=(char*) str; str_length=(uint) strlen(str); Alloced_length=0; alloced=0;} + { + Ptr=(char*) str; str_length=(uint) strlen(str); Alloced_length=0; alloced=0; + str_charset=default_charset_info; + } String(const char *str,uint32 len) - { Ptr=(char*) str; str_length=len; Alloced_length=0; alloced=0;} + { + Ptr=(char*) str; str_length=len; Alloced_length=0; alloced=0; + str_charset=default_charset_info; + } String(char *str,uint32 len) - { Ptr=(char*) str; Alloced_length=str_length=len; alloced=0;} + { + Ptr=(char*) str; Alloced_length=str_length=len; alloced=0; + str_charset=default_charset_info; + } String(const String &str) - { Ptr=str.Ptr ; str_length=str.str_length ; - Alloced_length=str.Alloced_length; alloced=0; } - + { + Ptr=str.Ptr ; str_length=str.str_length ; + Alloced_length=str.Alloced_length; alloced=0; + str_charset=str.str_charset; + } static void *operator new(size_t size) { return (void*) sql_alloc((uint) size); } static void operator delete(void *ptr_arg,size_t size) /*lint -e715 */ { sql_element_free(ptr_arg); } ~String() { free(); } + inline CHARSET_INFO *charset() const { return str_charset; } inline uint32 length() const { return str_length;} inline uint32 alloced_length() const { return Alloced_length;} inline char& operator [] (uint32 i) const { return Ptr[i]; } @@ -124,7 +150,7 @@ public: char *new_ptr; if (!(new_ptr=(char*) my_realloc(Ptr,arg_length,MYF(0)))) { - (void) my_free(Ptr,MYF(0)); + Alloced_length = 0; real_alloc(arg_length); } else @@ -153,6 +179,7 @@ public: bool append(const char *s,uint32 arg_length=0); bool append(IO_CACHE* file, uint32 arg_length); int strstr(const String &search,uint32 offset=0); // Returns offset to substring or -1 + int strstr_case(const String &s,uint32 offset=0); int strrstr(const String &search,uint32 offset=0); // Returns offset to substring or -1 bool replace(uint32 offset,uint32 arg_length,const String &to); inline bool append(char chr) @@ -171,8 +198,8 @@ public: } bool fill(uint32 max_length,char fill); void strip_sp(); - inline void caseup() { ::caseup(Ptr,str_length); } - inline void casedn() { ::casedn(Ptr,str_length); } + inline void caseup() { my_caseup(str_charset,Ptr,str_length); } + inline void casedn() { my_casedn(str_charset,Ptr,str_length); } friend int sortcmp(const String *a,const String *b); friend int stringcmp(const String *a,const String *b); friend String *copy_if_not_alloced(String *a,String *b,uint32 arg_length); @@ -180,4 +207,49 @@ public: friend int wild_compare(String &match,String &wild,char escape); uint32 numchars(); int charpos(int i,uint32 offset=0); + +// added by Holyfoot for "geometry" needs + int reserve(uint32 space_needed) + { + return realloc(str_length + space_needed); + } + int reserve(uint32 space_needed, uint32 grow_by); + +// these append operations do NOT check alloced memory +// q_*** methods writes values of parameters itself +// qs_*** methods writes string representation of value + void q_append(const char &c) + { + Ptr[str_length++] = c; + } + void q_append(const uint32 &n) + { + int4store(Ptr + str_length, n); + str_length += 4; + } + void q_append(double d) + { + float8store(Ptr + str_length, d); + str_length += 8; + } + void q_append(double *d) + { + float8store(Ptr + str_length, *d); + str_length += 8; + } + void q_append(const char *data, uint32 data_len) + { + memcpy(Ptr + str_length, data, data_len); + str_length += data_len; + } + + void WriteAtPosition(int position, uint32 value) + { + int4store(Ptr + position,value); + } + + void qs_append(const char *str); + void qs_append(double d); + void qs_append(double *d); + void qs_append(const char &c); }; |