diff options
author | unknown <pem@mysql.com> | 2004-05-07 18:52:06 +0200 |
---|---|---|
committer | unknown <pem@mysql.com> | 2004-05-07 18:52:06 +0200 |
commit | e9c1e75b48e5d2c0047a3e88b35667a33d6395e4 (patch) | |
tree | 2f7b236a8721d14f1b398964b898d922fd133131 /sql | |
parent | f3d691a970627f34ed825a9cf7b84520dcdd43b3 (diff) | |
parent | e3211fbd6a59c3dc6a97066c97ab86bfc67d382f (diff) | |
download | mariadb-git-e9c1e75b48e5d2c0047a3e88b35667a33d6395e4.tar.gz |
Merge 4.1 -> 5.0
BitKeeper/etc/ignore:
auto-union
BitKeeper/etc/logging_ok:
auto-union
VC++Files/sql/mysqld.dsp:
Auto merged
configure.in:
Auto merged
include/my_global.h:
Auto merged
include/mysql_com.h:
Auto merged
libmysql/libmysql.c:
Auto merged
libmysqld/Makefile.am:
Auto merged
myisam/myisamchk.c:
Auto merged
myisam/myisamdef.h:
Auto merged
mysql-test/install_test_db.sh:
Auto merged
mysql-test/r/func_time.result:
Auto merged
mysql-test/r/mysqldump.result:
Auto merged
mysql-test/r/show_check.result:
Auto merged
mysql-test/r/subselect.result:
Auto merged
mysql-test/r/union.result:
Auto merged
mysql-test/t/func_time.test:
Auto merged
mysql-test/t/subselect.test:
Auto merged
scripts/make_binary_distribution.sh:
Auto merged
scripts/mysql_install_db.sh:
Auto merged
sql/ha_berkeley.cc:
Auto merged
mysql-test/t/rpl_error_ignored_table.test:
Auto merged
sql/ha_berkeley.h:
Auto merged
sql/ha_innodb.cc:
Auto merged
sql/ha_innodb.h:
Auto merged
sql/ha_myisam.cc:
Auto merged
sql/handler.cc:
Auto merged
sql/handler.h:
Auto merged
sql/item.cc:
Auto merged
sql/item.h:
Auto merged
sql/item_cmpfunc.cc:
Auto merged
sql/item_cmpfunc.h:
Auto merged
sql/item_subselect.cc:
Auto merged
sql/item_sum.cc:
Auto merged
sql/item_sum.h:
Auto merged
sql/lex.h:
Auto merged
sql/log.cc:
Auto merged
sql/mysql_priv.h:
Auto merged
sql/mysqld.cc:
Auto merged
sql/protocol.cc:
Auto merged
sql/records.cc:
Auto merged
sql/set_var.cc:
Auto merged
sql/sql_acl.cc:
Auto merged
sql/sql_acl.h:
Auto merged
sql/sql_base.cc:
Auto merged
sql/sql_cache.cc:
Auto merged
sql/sql_class.cc:
Auto merged
sql/sql_class.h:
Auto merged
sql/sql_db.cc:
Auto merged
sql/sql_delete.cc:
Auto merged
sql/sql_insert.cc:
Auto merged
sql/sql_lex.cc:
Auto merged
sql/sql_load.cc:
Auto merged
sql/sql_prepare.cc:
Auto merged
sql/sql_select.h:
Auto merged
sql/sql_show.cc:
Auto merged
sql/sql_string.cc:
Auto merged
sql/sql_test.cc:
Auto merged
sql/sql_update.cc:
Auto merged
sql/sql_yacc.yy:
Auto merged
sql/table.h:
Auto merged
tests/client_test.c:
Auto merged
Diffstat (limited to 'sql')
102 files changed, 7506 insertions, 1782 deletions
diff --git a/sql/Makefile.am b/sql/Makefile.am index 045b3e64698..2d63d7fef76 100644 --- a/sql/Makefile.am +++ b/sql/Makefile.am @@ -16,12 +16,11 @@ #called from the top level Makefile - MYSQLDATAdir = $(localstatedir) MYSQLSHAREdir = $(pkgdatadir) MYSQLBASEdir= $(prefix) INCLUDES = @MT_INCLUDES@ \ - @bdb_includes@ @innodb_includes@ \ + @bdb_includes@ @innodb_includes@ @ndbcluster_includes@ \ -I$(top_srcdir)/include -I$(top_srcdir)/regex \ -I$(srcdir) $(openssl_includes) WRAPLIBS= @WRAPLIBS@ @@ -37,11 +36,12 @@ LDADD = @isam_libs@ \ ../mysys/libmysys.a \ ../dbug/libdbug.a \ ../regex/libregex.a \ - ../strings/libmystrings.a + ../strings/libmystrings.a mysqld_LDADD = @MYSQLD_EXTRA_LDFLAGS@ \ @bdb_libs@ @innodb_libs@ @pstack_libs@ \ @innodb_system_libs@ \ + @ndbcluster_libs@ @ndbcluster_system_libs@ \ $(LDADD) $(CXXLDFLAGS) $(WRAPLIBS) @LIBDL@ @openssl_libs@ noinst_HEADERS = item.h item_func.h item_sum.h item_cmpfunc.h \ item_strfunc.h item_timefunc.h item_uniq.h \ @@ -52,12 +52,13 @@ noinst_HEADERS = item.h item_func.h item_sum.h item_cmpfunc.h \ field.h handler.h \ ha_isammrg.h ha_isam.h ha_myisammrg.h\ ha_heap.h ha_myisam.h ha_berkeley.h ha_innodb.h \ - opt_range.h protocol.h \ + ha_ndbcluster.h opt_range.h protocol.h \ sql_select.h structs.h table.h sql_udf.h hash_filo.h\ lex.h lex_symbol.h sql_acl.h sql_crypt.h \ log_event.h sql_repl.h slave.h \ stacktrace.h sql_sort.h sql_cache.h set_var.h \ spatial.h gstream.h client_settings.h \ + examples/ha_example.h \ sp_head.h sp_pcontext.h sp_rcontext.h sp.h sp_cache.h mysqld_SOURCES = sql_lex.cc sql_handler.cc \ item.cc item_sum.cc item_buff.cc item_func.cc \ @@ -76,11 +77,11 @@ mysqld_SOURCES = sql_lex.cc sql_handler.cc \ procedure.cc item_uniq.cc sql_test.cc \ log.cc log_event.cc init.cc derror.cc sql_acl.cc \ unireg.cc des_key_file.cc \ - time.cc opt_range.cc opt_sum.cc \ + discover.cc time.cc opt_range.cc opt_sum.cc \ records.cc filesort.cc handler.cc \ ha_heap.cc ha_myisam.cc ha_myisammrg.cc \ ha_berkeley.cc ha_innodb.cc \ - ha_isam.cc ha_isammrg.cc \ + ha_isam.cc ha_isammrg.cc ha_ndbcluster.cc \ sql_db.cc sql_table.cc sql_rename.cc sql_crypt.cc \ sql_load.cc mf_iocache.cc field_conv.cc sql_show.cc \ sql_udf.cc sql_analyse.cc sql_analyse.h sql_cache.cc \ @@ -88,6 +89,7 @@ mysqld_SOURCES = sql_lex.cc sql_handler.cc \ client.c sql_client.cc mini_client_errors.c pack.c\ stacktrace.c repl_failsafe.h repl_failsafe.cc sql_olap.cc\ gstream.cc spatial.cc sql_help.cc protocol_cursor.cc \ + examples/ha_example.cc \ sp_head.cc sp_pcontext.cc sp_rcontext.cc sp.cc \ sp_cache.cc gen_lex_hash_SOURCES = gen_lex_hash.cc diff --git a/sql/discover.cc b/sql/discover.cc new file mode 100644 index 00000000000..e260f44a8db --- /dev/null +++ b/sql/discover.cc @@ -0,0 +1,172 @@ +/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB + + 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; either version 2 of the License, or + (at your option) any later version. + + 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ + + +/* Functions for discover of frm file from handler */ + +#include "mysql_priv.h" +#include <my_dir.h> + +/* + Read the contents of a .frm file + + SYNOPSIS + readfrm() + + name path to table-file "db/name" + frmdata frm data + len length of the read frmdata + + RETURN VALUES + 0 ok + 1 Could not open file + 2 Could not stat file + 3 Could not allocate data for read + Could not read file + + frmdata and len are set to 0 on error +*/ + +int readfrm(const char *name, + const void **frmdata, uint *len) +{ + int error; + char index_file[FN_REFLEN]; + File file; + ulong read_len; + char *read_data; + MY_STAT state; + DBUG_ENTER("readfrm"); + DBUG_PRINT("enter",("name: '%s'",name)); + + *frmdata= NULL; // In case of errors + *len= 0; + error= 1; + if ((file=my_open(fn_format(index_file,name,"",reg_ext,4), + O_RDONLY | O_SHARE, + MYF(0))) < 0) + goto err_end; + + // Get length of file + error= 2; + if (my_fstat(file, &state, MYF(0))) + goto err; + read_len= state.st_size; + + // Read whole frm file + error= 3; + read_data= 0; + if (read_string(file, &read_data, read_len)) + goto err; + + // Setup return data + *frmdata= (void*) read_data; + *len= read_len; + error= 0; + + err: + if (file > 0) + VOID(my_close(file,MYF(MY_WME))); + + err_end: /* Here when no file */ + DBUG_RETURN (error); +} /* readfrm */ + + +/* + Write the content of a frm data pointer + to a frm file + + SYNOPSIS + writefrm() + + name path to table-file "db/name" + frmdata frm data + len length of the frmdata + + RETURN VALUES + 0 ok + 2 Could not write file +*/ + +int writefrm(const char *name, const void *frmdata, uint len) +{ + File file; + char index_file[FN_REFLEN]; + int error; + DBUG_ENTER("writefrm"); + DBUG_PRINT("enter",("name: '%s' len: %d ",name,len)); + //DBUG_DUMP("frmdata", (char*)frmdata, len); + + error= 0; + if ((file=my_create(fn_format(index_file,name,"",reg_ext,4), + CREATE_MODE,O_RDWR | O_TRUNC,MYF(MY_WME))) >= 0) + { + if (my_write(file,(byte*)frmdata,len,MYF(MY_WME | MY_NABP))) + error= 2; + } + VOID(my_close(file,MYF(0))); + DBUG_RETURN(error); +} /* writefrm */ + + + + +/* + Try to discover table from handler and + if found, write the frm file to disk. + + RETURN VALUES: + 0 : Table existed in handler and created + on disk if so requested + 1 : Table does not exist + >1 : error + +*/ + +int create_table_from_handler(const char *db, + const char *name, + bool create_if_found) +{ + int error= 0; + const void* frmblob = NULL; + char path[FN_REFLEN]; + uint frmlen = 0; + DBUG_ENTER("create_table_from_handler"); + DBUG_PRINT("enter", ("create_if_found: %d", create_if_found)); + + if (ha_discover(db, name, &frmblob, &frmlen)) + DBUG_RETURN(1); // Table does not exist + + // Table exists in handler + if (create_if_found) + { + (void)strxnmov(path,FN_REFLEN,mysql_data_home,"/",db,"/",name,NullS); + // Save the frm file + error = writefrm(path, frmblob, frmlen); + } + + err: + if (frmblob) + my_free((char*) frmblob,MYF(0)); + DBUG_RETURN(error); +} + +int table_exists_in_handler(const char *db, + const char *name) +{ + return (create_table_from_handler(db, name, false) == 0); +} diff --git a/sql/examples/ha_example.cc b/sql/examples/ha_example.cc new file mode 100644 index 00000000000..e2463761e67 --- /dev/null +++ b/sql/examples/ha_example.cc @@ -0,0 +1,300 @@ +/* Copyright (C) 2003 MySQL AB + + 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; either version 2 of the License, or + (at your option) any later version. + + 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ + +#ifdef __GNUC__ +#pragma implementation // gcc: Class implementation +#endif + +#include <mysql_priv.h> + +#ifdef HAVE_EXAMPLE_DB +#include "ha_example.h" + +/* Variables for example share methods */ +pthread_mutex_t example_mutex; +static HASH example_open_tables; +static int example_init= 0; + +static byte* example_get_key(EXAMPLE_SHARE *share,uint *length, + my_bool not_used __attribute__((unused))) +{ + *length=share->table_name_length; + return (byte*) share->table_name; +} + + +/* + Example of simple lock controls. +*/ +static EXAMPLE_SHARE *get_share(const char *table_name, TABLE *table) +{ + EXAMPLE_SHARE *share; + uint length; + char *tmp_name; + + if (!example_init) + { + /* Hijack a mutex for init'ing the storage engine */ + pthread_mutex_lock(&LOCK_mysql_create_db); + if (!example_init) + { + example_init++; + VOID(pthread_mutex_init(&example_mutex,MY_MUTEX_INIT_FAST)); + (void) hash_init(&example_open_tables,system_charset_info,32,0,0, + (hash_get_key) example_get_key,0,0); + } + pthread_mutex_unlock(&LOCK_mysql_create_db); + } + pthread_mutex_lock(&example_mutex); + length=(uint) strlen(table_name); + + if (!(share=(EXAMPLE_SHARE*) hash_search(&example_open_tables, + (byte*) table_name, + length))) + { + if (!(share=(EXAMPLE_SHARE *) + my_multi_malloc(MYF(MY_WME | MY_ZEROFILL), + &share, sizeof(*share), + &tmp_name, length+1, + NullS))) + { + pthread_mutex_unlock(&example_mutex); + return NULL; + } + + share->use_count=0; + share->table_name_length=length; + share->table_name=tmp_name; + strmov(share->table_name,table_name); + if (my_hash_insert(&example_open_tables, (byte*) share)) + goto error; + thr_lock_init(&share->lock); + pthread_mutex_init(&share->mutex,MY_MUTEX_INIT_FAST); + } + share->use_count++; + pthread_mutex_unlock(&example_mutex); + + return share; + +error2: + thr_lock_delete(&share->lock); + pthread_mutex_destroy(&share->mutex); +error: + pthread_mutex_unlock(&example_mutex); + my_free((gptr) share, MYF(0)); + + return NULL; +} + + +/* + Free lock controls. +*/ +static int free_share(EXAMPLE_SHARE *share) +{ + pthread_mutex_lock(&example_mutex); + if (!--share->use_count) + { + hash_delete(&example_open_tables, (byte*) share); + thr_lock_delete(&share->lock); + pthread_mutex_destroy(&share->mutex); + my_free((gptr) share, MYF(0)); + } + pthread_mutex_unlock(&example_mutex); + + return 0; +} + + +const char **ha_example::bas_ext() const +{ static const char *ext[]= { NullS }; return ext; } + + +int ha_example::open(const char *name, int mode, uint test_if_locked) +{ + DBUG_ENTER("ha_example::open"); + + if (!(share = get_share(name, table))) + DBUG_RETURN(1); + thr_lock_data_init(&share->lock,&lock,NULL); + + DBUG_RETURN(0); +} + +int ha_example::close(void) +{ + DBUG_ENTER("ha_example::close"); + DBUG_RETURN(free_share(share)); +} + +int ha_example::write_row(byte * buf) +{ + DBUG_ENTER("ha_example::write_row"); + DBUG_RETURN(HA_ERR_NOT_IMPLEMENTED); +} + +int ha_example::update_row(const byte * old_data, byte * new_data) +{ + + DBUG_ENTER("ha_example::update_row"); + DBUG_RETURN(HA_ERR_NOT_IMPLEMENTED); +} + +int ha_example::delete_row(const byte * buf) +{ + DBUG_ENTER("ha_example::delete_row"); + DBUG_RETURN(HA_ERR_NOT_IMPLEMENTED); +} + +int ha_example::index_read(byte * buf, const byte * key, + uint key_len __attribute__((unused)), + enum ha_rkey_function find_flag + __attribute__((unused))) +{ + DBUG_ENTER("ha_example::index_read"); + DBUG_RETURN(HA_ERR_NOT_IMPLEMENTED); +} + +int ha_example::index_read_idx(byte * buf, uint index, const byte * key, + uint key_len __attribute__((unused)), + enum ha_rkey_function find_flag + __attribute__((unused))) +{ + DBUG_ENTER("ha_example::index_read_idx"); + DBUG_RETURN(HA_ERR_NOT_IMPLEMENTED); +} + + +int ha_example::index_next(byte * buf) +{ + DBUG_ENTER("ha_example::index_next"); + DBUG_RETURN(HA_ERR_NOT_IMPLEMENTED); +} + +int ha_example::index_prev(byte * buf) +{ + DBUG_ENTER("ha_example::index_prev"); + DBUG_RETURN(HA_ERR_NOT_IMPLEMENTED); +} + +int ha_example::index_first(byte * buf) +{ + DBUG_ENTER("ha_example::index_first"); + DBUG_RETURN(HA_ERR_NOT_IMPLEMENTED); +} + +int ha_example::index_last(byte * buf) +{ + DBUG_ENTER("ha_example::index_last"); + DBUG_RETURN(HA_ERR_NOT_IMPLEMENTED); +} + +int ha_example::rnd_init(bool scan) +{ + DBUG_ENTER("ha_example::rnd_init"); + DBUG_RETURN(HA_ERR_NOT_IMPLEMENTED); +} + +int ha_example::rnd_next(byte *buf) +{ + DBUG_ENTER("ha_example::rnd_next"); + DBUG_RETURN(HA_ERR_END_OF_FILE); +} + +void ha_example::position(const byte *record) +{ + DBUG_ENTER("ha_example::position"); + DBUG_VOID_RETURN; +} + +int ha_example::rnd_pos(byte * buf, byte *pos) +{ + DBUG_ENTER("ha_example::rnd_pos"); + DBUG_RETURN(HA_ERR_NOT_IMPLEMENTED); +} + +void ha_example::info(uint flag) +{ + DBUG_ENTER("ha_example::info"); + DBUG_VOID_RETURN; +} + +int ha_example::extra(enum ha_extra_function operation) +{ + DBUG_ENTER("ha_example::extra"); + DBUG_RETURN(0); +} + +int ha_example::reset(void) +{ + DBUG_ENTER("ha_example::reset"); + DBUG_RETURN(0); +} + + +int ha_example::delete_all_rows() +{ + DBUG_ENTER("ha_example::delete_all_rows"); + DBUG_RETURN(HA_ERR_NOT_IMPLEMENTED); +} + +int ha_example::external_lock(THD *thd, int lock_type) +{ + DBUG_ENTER("ha_example::external_lock"); + DBUG_RETURN(0); +} + +THR_LOCK_DATA **ha_example::store_lock(THD *thd, + THR_LOCK_DATA **to, + enum thr_lock_type lock_type) +{ + if (lock_type != TL_IGNORE && lock.type == TL_UNLOCK) + lock.type=lock_type; + *to++= &lock; + return to; +} + +int ha_example::delete_table(const char *name) +{ + DBUG_ENTER("ha_example::delete_table"); + /* This is not implemented but we want someone to be able that it works. */ + DBUG_RETURN(0); +} + +int ha_example::rename_table(const char * from, const char * to) +{ + DBUG_ENTER("ha_example::rename_table "); + DBUG_RETURN(HA_ERR_NOT_IMPLEMENTED); +} + +ha_rows ha_example::records_in_range(int inx, + const byte *start_key,uint start_key_len, + enum ha_rkey_function start_search_flag, + const byte *end_key,uint end_key_len, + enum ha_rkey_function end_search_flag) +{ + DBUG_ENTER("ha_example::records_in_range "); + DBUG_RETURN(records); // HA_ERR_NOT_IMPLEMENTED +} + + +int ha_example::create(const char *name, TABLE *table_arg, HA_CREATE_INFO *create_info) +{ + DBUG_ENTER("ha_example::create"); + /* This is not implemented but we want someone to be able that it works. */ + DBUG_RETURN(0); +} +#endif /* HAVE_EXAMPLE_DB */ diff --git a/sql/examples/ha_example.h b/sql/examples/ha_example.h new file mode 100644 index 00000000000..466632a1795 --- /dev/null +++ b/sql/examples/ha_example.h @@ -0,0 +1,91 @@ +/* Copyright (C) 2003 MySQL AB + + 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; either version 2 of the License, or + (at your option) any later version. + + 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ + +typedef struct st_example_share { + char *table_name; + uint table_name_length,use_count; + pthread_mutex_t mutex; + THR_LOCK lock; +} EXAMPLE_SHARE; + +class ha_example: public handler +{ + THR_LOCK_DATA lock; /* MySQL lock */ + EXAMPLE_SHARE *share; /* Shared lock info */ + +public: + ha_example(TABLE *table): handler(table) + { + } + ~ha_example() + { + } + const char *table_type() const { return "EXAMPLE"; } + const char *index_type(uint inx) { return "NONE"; } + const char **bas_ext() const; + ulong table_flags() const + { + return 0; + } + ulong index_flags(uint inx) const + { + return 0; + } + uint max_record_length() const { return HA_MAX_REC_LENGTH; } + uint max_keys() const { return 0; } + uint max_key_parts() const { return 0; } + uint max_key_length() const { return 0; } + /* + Called in test_quick_select to determine if indexes should be used. + */ + virtual double scan_time() { return (double) (records+deleted) / 20.0+10; } + /* The next method will never be called */ + virtual double read_time(ha_rows rows) { return (double) rows / 20.0+1; } + virtual bool fast_key_read() { return 1;} + + int open(const char *name, int mode, uint test_if_locked); + int close(void); + int write_row(byte * buf); + int update_row(const byte * old_data, byte * new_data); + int delete_row(const byte * buf); + int index_read(byte * buf, const byte * key, + uint key_len, enum ha_rkey_function find_flag); + int index_read_idx(byte * buf, uint idx, const byte * key, + uint key_len, enum ha_rkey_function find_flag); + int index_next(byte * buf); + int index_prev(byte * buf); + int index_first(byte * buf); + int index_last(byte * buf); + int rnd_init(bool scan=1); + int rnd_next(byte *buf); + int rnd_pos(byte * buf, byte *pos); + void position(const byte *record); + void info(uint); + int extra(enum ha_extra_function operation); + int reset(void); + int external_lock(THD *thd, int lock_type); + int delete_all_rows(void); + ha_rows records_in_range(int inx, const byte *start_key,uint start_key_len, + enum ha_rkey_function start_search_flag, + const byte *end_key,uint end_key_len, + enum ha_rkey_function end_search_flag); + int delete_table(const char *from); + int rename_table(const char * from, const char * to); + int create(const char *name, TABLE *form, HA_CREATE_INFO *create_info); + + THR_LOCK_DATA **store_lock(THD *thd, THR_LOCK_DATA **to, + enum thr_lock_type lock_type); +}; diff --git a/sql/field.cc b/sql/field.cc index d099da2d959..fdf314972c8 100644 --- a/sql/field.cc +++ b/sql/field.cc @@ -327,7 +327,7 @@ bool Field::send_binary(Protocol *protocol) { char buff[MAX_FIELD_WIDTH]; String tmp(buff,sizeof(buff),charset()); - val_str(&tmp,&tmp); + val_str(&tmp); return protocol->store(tmp.ptr(), tmp.length(), tmp.charset()); } @@ -396,8 +396,8 @@ uint Field::fill_cache_field(CACHE_FIELD *copy) bool Field::get_date(TIME *ltime,uint fuzzydate) { char buff[40]; - String tmp(buff,sizeof(buff),&my_charset_bin),tmp2,*res; - if (!(res=val_str(&tmp,&tmp2)) || + String tmp(buff,sizeof(buff),&my_charset_bin),*res; + if (!(res=val_str(&tmp)) || str_to_TIME(res->ptr(),res->length(),ltime,fuzzydate) <= TIMESTAMP_DATETIME_ERROR) return 1; @@ -407,8 +407,8 @@ bool Field::get_date(TIME *ltime,uint fuzzydate) bool Field::get_time(TIME *ltime) { char buff[40]; - String tmp(buff,sizeof(buff),&my_charset_bin),tmp2,*res; - if (!(res=val_str(&tmp,&tmp2)) || + String tmp(buff,sizeof(buff),&my_charset_bin),*res; + if (!(res=val_str(&tmp)) || str_to_time(res->ptr(),res->length(),ltime)) return 1; return 0; @@ -2308,7 +2308,12 @@ int Field_float::store(double nr) else { max_value= (log_10[field_length]-1)/log_10[dec]; - nr= floor(nr*log_10[dec]+0.5)/log_10[dec]; + /* + The following comparison is needed to not get an overflow if nr + is close to FLT_MAX + */ + if (fabs(nr) < FLT_MAX/10.0e+32) + nr= floor(nr*log_10[dec]+0.5)/log_10[dec]; } if (nr < -max_value) { @@ -2603,7 +2608,8 @@ int Field_double::store(double nr) else { max_value= (log_10[field_length]-1)/log_10[dec]; - nr= floor(nr*log_10[dec]+0.5)/log_10[dec]; + if (fabs(nr) < DBL_MAX/10.0e+32) + nr= floor(nr*log_10[dec]+0.5)/log_10[dec]; } if (nr < -max_value) { @@ -3087,16 +3093,16 @@ longlong Field_timestamp::val_int(void) } -String *Field_timestamp::val_str(String *val_buffer, - String *val_ptr __attribute__((unused))) +String *Field_timestamp::val_str(String *val_buffer, String *val_ptr) { uint32 temp, temp2; time_t time_arg; struct tm *l_time; struct tm tm_tmp; + char *to; val_buffer->alloc(field_length+1); - char *to= (char*) val_buffer->ptr(); + to= (char*) val_buffer->ptr(); val_buffer->length(field_length); #ifdef WORDS_BIGENDIAN @@ -4360,7 +4366,7 @@ int Field_varstring::store(const char *from,uint length,CHARSET_INFO *cs) set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED); error= 1; } - memcpy(ptr+2,from,length); + memcpy(ptr+HA_KEY_BLOB_LENGTH,from,length); int2store(ptr, length); return error; } @@ -4389,18 +4395,18 @@ int Field_varstring::store(longlong nr) double Field_varstring::val_real(void) { int not_used; - uint length=uint2korr(ptr)+2; + uint length=uint2korr(ptr)+HA_KEY_BLOB_LENGTH; CHARSET_INFO *cs=charset(); - return my_strntod(cs,ptr+2,length,(char**)0, ¬_used); + return my_strntod(cs, ptr+HA_KEY_BLOB_LENGTH, length, (char**)0, ¬_used); } longlong Field_varstring::val_int(void) { int not_used; - uint length=uint2korr(ptr)+2; + uint length=uint2korr(ptr)+HA_KEY_BLOB_LENGTH; CHARSET_INFO *cs=charset(); - return my_strntoll(cs,ptr+2,length,10,NULL, ¬_used); + return my_strntoll(cs,ptr+HA_KEY_BLOB_LENGTH,length,10,NULL, ¬_used); } @@ -4408,7 +4414,7 @@ String *Field_varstring::val_str(String *val_buffer __attribute__((unused)), String *val_ptr) { uint length=uint2korr(ptr); - val_ptr->set((const char*) ptr+2,length,field_charset); + val_ptr->set((const char*) ptr+HA_KEY_BLOB_LENGTH,length,field_charset); return val_ptr; } @@ -4418,18 +4424,21 @@ int Field_varstring::cmp(const char *a_ptr, const char *b_ptr) uint a_length=uint2korr(a_ptr); uint b_length=uint2korr(b_ptr); int diff; - diff=my_strnncoll(field_charset, - (const uchar*)a_ptr+2,min(a_length,b_length), - (const uchar*)b_ptr+2,min(a_length,b_length)); + diff= my_strnncoll(field_charset, + (const uchar*) a_ptr+HA_KEY_BLOB_LENGTH, + min(a_length,b_length), + (const uchar*) b_ptr+HA_KEY_BLOB_LENGTH, + min(a_length,b_length)); return diff ? diff : (int) (a_length - b_length); } void Field_varstring::sort_string(char *to,uint length) { uint tot_length=uint2korr(ptr); - tot_length=my_strnxfrm(field_charset, - (unsigned char *) to, length, - (unsigned char *)ptr+2, tot_length); + tot_length= my_strnxfrm(field_charset, + (uchar*) to, length, + (uchar*) ptr+HA_KEY_BLOB_LENGTH, + tot_length); if (tot_length < length) field_charset->cset->fill(field_charset, to+tot_length,length-tot_length, binary() ? (char) 0 : ' '); @@ -4454,7 +4463,7 @@ char *Field_varstring::pack(char *to, const char *from, uint max_length) if (max_length > 255) *to++= (char) (length >> 8); if (length) - memcpy(to, from+2, length); + memcpy(to, from+HA_KEY_BLOB_LENGTH, length); return to+length; } @@ -4474,7 +4483,7 @@ const char *Field_varstring::unpack(char *to, const char *from) to[1] = *from++; } if (length) - memcpy(to+2, from, length); + memcpy(to+HA_KEY_BLOB_LENGTH, from, length); return from+length; } @@ -4485,8 +4494,8 @@ int Field_varstring::pack_cmp(const char *a, const char *b, uint key_length) uint b_length; if (key_length > 255) { - a_length=uint2korr(a); a+=2; - b_length=uint2korr(b); b+=2; + a_length=uint2korr(a); a+= 2; + b_length=uint2korr(b); b+= 2; } else { @@ -4494,32 +4503,32 @@ int Field_varstring::pack_cmp(const char *a, const char *b, uint key_length) b_length= (uint) (uchar) *b++; } return my_strnncoll(field_charset, - (const uchar *)a,a_length, - (const uchar *)b,b_length); + (const uchar*) a, a_length, + (const uchar*) b, b_length); } int Field_varstring::pack_cmp(const char *b, uint key_length) { - char *a=ptr+2; - uint a_length=uint2korr(ptr); + char *a= ptr+HA_KEY_BLOB_LENGTH; + uint a_length= uint2korr(ptr); uint b_length; if (key_length > 255) { - b_length=uint2korr(b); b+=2; + b_length=uint2korr(b); b+= 2; } else { b_length= (uint) (uchar) *b++; } return my_strnncoll(field_charset, - (const uchar *)a,a_length, - (const uchar *)b,b_length); + (const uchar*) a, a_length, + (const uchar*) b, b_length); } uint Field_varstring::packed_col_length(const char *data_ptr, uint length) { if (length > 255) - return uint2korr(data_ptr)+2; + return uint2korr(data_ptr)+HA_KEY_BLOB_LENGTH; else return (uint) ((uchar) *data_ptr)+1; } @@ -4532,22 +4541,21 @@ uint Field_varstring::max_packed_col_length(uint max_length) void Field_varstring::get_key_image(char *buff, uint length, CHARSET_INFO *cs, imagetype type) { - length-= HA_KEY_BLOB_LENGTH; uint f_length=uint2korr(ptr); if (f_length > length) f_length= length; int2store(buff,length); - memcpy(buff+2,ptr+2,length); + memcpy(buff+HA_KEY_BLOB_LENGTH, ptr+HA_KEY_BLOB_LENGTH, length); #ifdef HAVE_purify if (f_length < length) - bzero(buff+2+f_length, (length-f_length)); + bzero(buff+HA_KEY_BLOB_LENGTH+f_length, (length-f_length)); #endif } void Field_varstring::set_key_image(char *buff,uint length, CHARSET_INFO *cs) { length=uint2korr(buff); // Real length is here - (void) Field_varstring::store(buff+2, length, cs); + (void) Field_varstring::store(buff+HA_KEY_BLOB_LENGTH, length, cs); } @@ -4800,7 +4808,6 @@ int Field_blob::cmp_binary(const char *a_ptr, const char *b_ptr, void Field_blob::get_key_image(char *buff,uint length, CHARSET_INFO *cs, imagetype type) { - length-= HA_KEY_BLOB_LENGTH; uint32 blob_length= get_length(ptr); char *blob; @@ -4839,18 +4846,18 @@ void Field_blob::get_key_image(char *buff,uint length, Must clear this as we do a memcmp in opt_range.cc to detect identical keys */ - bzero(buff+2+blob_length, (length-blob_length)); + bzero(buff+HA_KEY_BLOB_LENGTH+blob_length, (length-blob_length)); length=(uint) blob_length; } int2store(buff,length); get_ptr(&blob); - memcpy(buff+2,blob,length); + memcpy(buff+HA_KEY_BLOB_LENGTH, blob, length); } void Field_blob::set_key_image(char *buff,uint length, CHARSET_INFO *cs) { - length=uint2korr(buff); - (void) Field_blob::store(buff+2,length,cs); + length= uint2korr(buff); + (void) Field_blob::store(buff+HA_KEY_BLOB_LENGTH, length, cs); } @@ -4858,16 +4865,16 @@ int Field_blob::key_cmp(const byte *key_ptr, uint max_key_length) { char *blob1; uint blob_length=get_length(ptr); - max_key_length-=2; memcpy_fixed(&blob1,ptr+packlength,sizeof(char*)); return Field_blob::cmp(blob1,min(blob_length, max_key_length), - (char*) key_ptr+2,uint2korr(key_ptr)); + (char*) key_ptr+HA_KEY_BLOB_LENGTH, + uint2korr(key_ptr)); } int Field_blob::key_cmp(const byte *a,const byte *b) { - return Field_blob::cmp((char*) a+2,uint2korr(a), - (char*) b+2,uint2korr(b)); + return Field_blob::cmp((char*) a+HA_KEY_BLOB_LENGTH, uint2korr(a), + (char*) b+HA_KEY_BLOB_LENGTH, uint2korr(b)); } @@ -4883,8 +4890,8 @@ void Field_blob::sort_string(char *to,uint length) memcpy_fixed(&blob,ptr+packlength,sizeof(char*)); blob_length=my_strnxfrm(field_charset, - (unsigned char *)to, length, - (unsigned char *)blob, blob_length); + (uchar*) to, length, + (uchar*) blob, blob_length); if (blob_length < length) field_charset->cset->fill(field_charset, to+blob_length, length-blob_length, @@ -4966,8 +4973,8 @@ int Field_blob::pack_cmp(const char *a, const char *b, uint key_length) b_length= (uint) (uchar) *b++; } return my_strnncoll(field_charset, - (const uchar *)a,a_length, - (const uchar *)b,b_length); + (const uchar*) a, a_length, + (const uchar*) b, b_length); } @@ -4989,8 +4996,8 @@ int Field_blob::pack_cmp(const char *b, uint key_length) b_length= (uint) (uchar) *b++; } return my_strnncoll(field_charset, - (const uchar *)a,a_length, - (const uchar *)b,b_length); + (const uchar*) a, a_length, + (const uchar*) b, b_length); } /* Create a packed key that will be used for storage from a MySQL row */ @@ -5026,7 +5033,7 @@ char *Field_blob::pack_key_from_key_image(char *to, const char *from, if (max_length > 255) *to++= (char) (length >> 8); if (length) - memcpy(to, from+2, length); + memcpy(to, from+HA_KEY_BLOB_LENGTH, length); return to+length; } @@ -5049,11 +5056,12 @@ uint Field_blob::max_packed_col_length(uint max_length) void Field_geom::get_key_image(char *buff, uint length, CHARSET_INFO *cs, imagetype type) { - length-= HA_KEY_BLOB_LENGTH; - ulong blob_length= get_length(ptr); char *blob; const char *dummy; MBR mbr; + ulong blob_length= get_length(ptr); + Geometry_buffer buffer; + Geometry *gobj; if (blob_length < SRID_SIZE) { @@ -5061,8 +5069,6 @@ void Field_geom::get_key_image(char *buff, uint length, CHARSET_INFO *cs, return; } get_ptr(&blob); - Geometry_buffer buffer; - Geometry *gobj; gobj= Geometry::create_from_wkb(&buffer, blob + SRID_SIZE, blob_length - SRID_SIZE); if (gobj->get_mbr(&mbr, &dummy)) @@ -5555,7 +5561,7 @@ uint32 calc_pack_length(enum_field_types type,uint32 length) switch (type) { case FIELD_TYPE_STRING: case FIELD_TYPE_DECIMAL: return (length); - case FIELD_TYPE_VAR_STRING: return (length+2); + case FIELD_TYPE_VAR_STRING: return (length+HA_KEY_BLOB_LENGTH); case FIELD_TYPE_YEAR: case FIELD_TYPE_TINY : return 1; case FIELD_TYPE_SHORT : return 2; @@ -5789,7 +5795,7 @@ create_field::create_field(Field *old_field,Field *orig_field) my_ptrdiff_t diff= (my_ptrdiff_t) (orig_field->table->rec_buff_length*2); orig_field->move_field(diff); // Points now at default_values bool is_null=orig_field->is_real_null(); - orig_field->val_str(&tmp,&tmp); + orig_field->val_str(&tmp); orig_field->move_field(-diff); // Back to record[0] if (!is_null) { diff --git a/sql/field.h b/sql/field.h index 75bb96f2f6d..002a7228164 100644 --- a/sql/field.h +++ b/sql/field.h @@ -92,12 +92,26 @@ public: utype unireg_check_arg, const char *field_name_arg, struct st_table *table_arg); virtual ~Field() {} + /* Store functions returns 1 on overflow and -1 on fatal error */ virtual int store(const char *to,uint length,CHARSET_INFO *cs)=0; virtual int store(double nr)=0; virtual int store(longlong nr)=0; virtual void store_time(TIME *ltime,timestamp_type t_type); virtual double val_real(void)=0; virtual longlong val_int(void)=0; + inline String *val_str(String *str) { return val_str(str, str); } + /* + val_str(buf1, buf2) gets two buffers and should use them as follows: + if it needs a temp buffer to convert result to string - use buf1 + example Field_tiny::val_str() + if the value exists as a string already - use buf2 + example Field_string::val_str() + consequently, buf2 may be created as 'String buf;' - no memory + will be allocated for it. buf1 will be allocated to hold a + value if it's too small. Using allocated buffer for buf2 may result in + an unnecessary free (and later, may be an alloc). + This trickery is used to decrease a number of malloc calls. + */ virtual String *val_str(String*,String *)=0; virtual Item_result result_type () const=0; virtual Item_result cmp_type () const { return result_type(); } @@ -199,7 +213,8 @@ public: { memcpy(buff,ptr,length); } inline void set_image(char *buff,uint length, CHARSET_INFO *cs) { memcpy(ptr,buff,length); } - virtual void get_key_image(char *buff,uint length, CHARSET_INFO *cs, imagetype type) + virtual void get_key_image(char *buff,uint length, CHARSET_INFO *cs, + imagetype type) { get_image(buff,length,cs); } virtual void set_key_image(char *buff,uint length, CHARSET_INFO *cs) { set_image(buff,length,cs); } diff --git a/sql/field_conv.cc b/sql/field_conv.cc index 5632c63c521..0974c552364 100644 --- a/sql/field_conv.cc +++ b/sql/field_conv.cc @@ -274,7 +274,7 @@ static void do_copy_blob(Copy_field *copy) static void do_conv_blob(Copy_field *copy) { - copy->from_field->val_str(©->tmp,©->tmp); + copy->from_field->val_str(©->tmp); ((Field_blob *) copy->to_field)->store(copy->tmp.ptr(), copy->tmp.length(), copy->tmp.charset()); @@ -286,7 +286,7 @@ static void do_save_blob(Copy_field *copy) { char buff[MAX_FIELD_WIDTH]; String res(buff,sizeof(buff),copy->tmp.charset()); - copy->from_field->val_str(&res,&res); + copy->from_field->val_str(&res); copy->tmp.copy(res); ((Field_blob *) copy->to_field)->store(copy->tmp.ptr(), copy->tmp.length(), @@ -298,7 +298,7 @@ static void do_field_string(Copy_field *copy) { char buff[MAX_FIELD_WIDTH]; copy->tmp.set_quick(buff,sizeof(buff),copy->tmp.charset()); - copy->from_field->val_str(©->tmp,©->tmp); + copy->from_field->val_str(©->tmp); copy->to_field->store(copy->tmp.c_ptr_quick(),copy->tmp.length(),copy->tmp.charset()); } @@ -559,7 +559,7 @@ void field_conv(Field *to,Field *from) if (to->type() == FIELD_TYPE_BLOB) { // Be sure the value is stored Field_blob *blob=(Field_blob*) to; - from->val_str(&blob->value,&blob->value); + from->val_str(&blob->value); if (!blob->value.is_alloced() && from->real_type() != FIELD_TYPE_STRING) blob->value.copy(); @@ -574,7 +574,7 @@ void field_conv(Field *to,Field *from) { char buff[MAX_FIELD_WIDTH]; String result(buff,sizeof(buff),from->charset()); - from->val_str(&result,&result); + from->val_str(&result); to->store(result.c_ptr_quick(),result.length(),from->charset()); } else if (from->result_type() == REAL_RESULT) diff --git a/sql/gen_lex_hash.cc b/sql/gen_lex_hash.cc index 5dc7c50e04c..7a445ed8c4d 100644 --- a/sql/gen_lex_hash.cc +++ b/sql/gen_lex_hash.cc @@ -461,7 +461,7 @@ int main(int argc,char **argv) hash_map= sql_functions_map;\n\ register uint32 cur_struct= uint4korr(hash_map+((len-1)*4));\n\ \n\ - for(;;){\n\ + for (;;){\n\ register uchar first_char= (uchar)cur_struct;\n\ \n\ if (first_char == 0)\n\ @@ -492,7 +492,7 @@ int main(int argc,char **argv) hash_map= symbols_map;\n\ register uint32 cur_struct= uint4korr(hash_map+((len-1)*4));\n\ \n\ - for(;;){\n\ + for (;;){\n\ register uchar first_char= (uchar)cur_struct;\n\ \n\ if (first_char==0){\n\ diff --git a/sql/ha_berkeley.cc b/sql/ha_berkeley.cc index 87eced3d149..df5a45480c6 100644 --- a/sql/ha_berkeley.cc +++ b/sql/ha_berkeley.cc @@ -1709,6 +1709,7 @@ int ha_berkeley::extra(enum ha_extra_function operation) int ha_berkeley::reset(void) { + ha_berkeley::extra(HA_EXTRA_RESET); key_read=0; // Reset to state after open return 0; } diff --git a/sql/ha_berkeley.h b/sql/ha_berkeley.h index d25a9bbab69..525c82febd0 100644 --- a/sql/ha_berkeley.h +++ b/sql/ha_berkeley.h @@ -88,7 +88,7 @@ class ha_berkeley: public handler public: ha_berkeley(TABLE *table): handler(table), alloc_ptr(0),rec_buff(0), file(0), int_table_flags(HA_REC_NOT_IN_SEQ | - HA_KEYPOS_TO_RNDPOS | HA_LASTKEY_ORDER | + HA_KEYPOS_TO_RNDPOS | HA_LASTKEY_ORDER | HA_FAST_KEY_READ | HA_NULL_KEY | HA_BLOB_KEY | HA_NOT_EXACT_COUNT | HA_PRIMARY_KEY_IN_READ_INDEX | HA_DROP_BEFORE_CREATE | HA_AUTO_PART_KEY | HA_TABLE_SCAN_ON_INDEX | @@ -107,7 +107,6 @@ class ha_berkeley: public handler uint max_key_length() const { return MAX_KEY_LENGTH; } uint extra_rec_buf_length() { return BDB_HIDDEN_PRIMARY_KEY_LENGTH; } ha_rows estimate_number_of_rows(); - bool fast_key_read() { return 1;} const key_map *keys_to_use_for_scanning() { return &key_map_full; } bool has_transactions() { return 1;} diff --git a/sql/ha_heap.cc b/sql/ha_heap.cc index 94105fb9409..47978d647ec 100644 --- a/sql/ha_heap.cc +++ b/sql/ha_heap.cc @@ -196,11 +196,6 @@ int ha_heap::extra(enum ha_extra_function operation) return heap_extra(file,operation); } -int ha_heap::reset(void) -{ - return heap_extra(file,HA_EXTRA_RESET); -} - int ha_heap::delete_all_rows() { heap_clear(file); diff --git a/sql/ha_heap.h b/sql/ha_heap.h index feadc0c3c0f..68406202c76 100644 --- a/sql/ha_heap.h +++ b/sql/ha_heap.h @@ -40,7 +40,7 @@ class ha_heap: public handler const char **bas_ext() const; ulong table_flags() const { - return (HA_READ_RND_SAME | HA_NO_INDEX | HA_KEYPOS_TO_RNDPOS | + return (HA_READ_RND_SAME | HA_FAST_KEY_READ | HA_KEYPOS_TO_RNDPOS | HA_NO_BLOBS | HA_NULL_KEY | HA_REC_NOT_IN_SEQ); } ulong index_flags(uint inx) const @@ -58,7 +58,6 @@ class ha_heap: public handler double scan_time() { return (double) (records+deleted) / 20.0+10; } double read_time(uint index, uint ranges, ha_rows rows) { return (double) rows / 20.0+1; } - virtual bool fast_key_read() { return 1;} int open(const char *name, int mode, uint test_if_locked); int close(void); @@ -81,7 +80,6 @@ class ha_heap: public handler void position(const byte *record); void info(uint); int extra(enum ha_extra_function operation); - int reset(void); int external_lock(THD *thd, int lock_type); int delete_all_rows(void); ha_rows records_in_range(int inx, const byte *start_key,uint start_key_len, diff --git a/sql/ha_innodb.cc b/sql/ha_innodb.cc index 664968aaef4..4c4c32691e5 100644 --- a/sql/ha_innodb.cc +++ b/sql/ha_innodb.cc @@ -15,7 +15,9 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ /* This file defines the InnoDB handler: the interface between MySQL and -InnoDB */ +InnoDB +NOTE: You can only use noninlined InnoDB functions in this file, because we +have disables the InnoDB inlining in this file. */ /* TODO list for the InnoDB handler in 4.1: - Check if the query_id is now right also in prepared and executed stats @@ -74,6 +76,7 @@ extern "C" { #include "../innobase/include/btr0cur.h" #include "../innobase/include/btr0btr.h" #include "../innobase/include/fsp0fsp.h" +#include "../innobase/include/sync0sync.h" #include "../innobase/include/fil0fil.h" } @@ -321,70 +324,49 @@ convert_error_code_to_mysql( } } -extern "C" { /***************************************************************** Prints info of a THD object (== user session thread) to the standard output. NOTE that /mysql/innobase/trx/trx0trx.c must contain the prototype for this function! */ - +extern "C" void innobase_mysql_print_thd( /*=====================*/ - char* buf, /* in/out: buffer where to print, must be at least - 400 bytes */ + FILE* f, /* in: output stream */ void* input_thd)/* in: pointer to a MySQL THD object */ { THD* thd; - char* old_buf = buf; thd = (THD*) input_thd; - /* We cannot use the return value of normal sprintf() as this is - not portable to some old non-Posix Unixes, e.g., some old SCO - Unixes */ - - buf += my_sprintf(buf, - (buf, "MySQL thread id %lu, query id %lu", - thd->thread_id, thd->query_id)); - if (thd->host) { - *buf = ' '; - buf++; - buf = strnmov(buf, thd->host, 30); - } + fprintf(f, "MySQL thread id %lu, query id %lu", + thd->thread_id, thd->query_id); + if (thd->host) { + putc(' ', f); + fputs(thd->host, f); + } - if (thd->ip) { - *buf = ' '; - buf++; - buf=strnmov(buf, thd->ip, 20); - } + if (thd->ip) { + putc(' ', f); + fputs(thd->ip, f); + } if (thd->user) { - *buf = ' '; - buf++; - buf=strnmov(buf, thd->user, 20); + putc(' ', f); + fputs(thd->user, f); } - if (thd->proc_info) { - *buf = ' '; - buf++; - buf=strnmov(buf, thd->proc_info, 50); - } - - if (thd->query) { - *buf = '\n'; - buf++; - buf=strnmov(buf, thd->query, 150); - } - - buf[0] = '\n'; - buf[1] = '\0'; /* Note that we must put a null character here to end - the printed string */ + if (thd->proc_info) { + putc(' ', f); + fputs(thd->proc_info, f); + } - /* We test the printed length did not overrun the buffer length of - 400 bytes */ + if (thd->query) { + putc(' ', f); + fputs(thd->query, f); + } - ut_a(strlen(old_buf) < 400); -} + putc('\n', f); } /************************************************************************* @@ -627,12 +609,11 @@ innobase_query_caching_of_table_permitted( return((my_bool)FALSE); } -extern "C" { /********************************************************************* Invalidates the MySQL query cache for the table. NOTE that the exact prototype of this function has to be in /innobase/row/row0ins.c! */ - +extern "C" void innobase_invalidate_query_cache( /*============================*/ @@ -652,6 +633,17 @@ innobase_invalidate_query_cache( TRUE); #endif } + +/********************************************************************* +Get the quote character to be used in SQL identifiers. */ +extern "C" +char +mysql_get_identifier_quote_char(void) +/*=================================*/ + /* out: quote character to be + used in SQL identifiers */ +{ + return '`'; } /********************************************************************* @@ -2105,24 +2097,20 @@ ha_innobase::write_row( if (prebuilt->trx != (trx_t*) current_thd->transaction.all.innobase_tid) { - char err_buf[2000]; - fprintf(stderr, "InnoDB: Error: the transaction object for the table handle is at\n" -"InnoDB: %lx, but for the current thread it is at %lx\n", - (ulong)prebuilt->trx, - (ulong)current_thd->transaction.all.innobase_tid); - - ut_sprintf_buf(err_buf, ((byte*)prebuilt) - 100, 200); - fprintf(stderr, -"InnoDB: Dump of 200 bytes around prebuilt: %.1000s\n", err_buf); - - ut_sprintf_buf(err_buf, +"InnoDB: %p, but for the current thread it is at %p\n", + prebuilt->trx, + current_thd->transaction.all.innobase_tid); + fputs("InnoDB: Dump of 200 bytes around prebuilt: ", stderr); + ut_print_buf(stderr, ((const byte*)prebuilt) - 100, 200); + fputs("\n" + "InnoDB: Dump of 200 bytes around transaction.all: ", + stderr); + ut_print_buf(stderr, ((byte*)(&(current_thd->transaction.all))) - 100, 200); - fprintf(stderr, -"InnoDB: Dump of 200 bytes around transaction.all: %.1000s\n", err_buf); - - ut_a(0); + putc('\n', stderr); + ut_error; } statistic_increment(ha_write_count, &LOCK_status); @@ -3393,12 +3381,9 @@ create_index( field = form->field[j]; - if (strlen(field->field_name) - == strlen(key_part->field->field_name) - && 0 == ut_cmp_in_lower_case( + if (0 == ut_cmp_in_lower_case( (char*)field->field_name, - (char*)key_part->field->field_name, - strlen(field->field_name))) { + (char*)key_part->field->field_name)) { /* Found the corresponding column */ break; @@ -3765,7 +3750,8 @@ ha_innobase::delete_table( /* Drop the table in InnoDB */ - error = row_drop_table_for_mysql(norm_name, trx); + error = row_drop_table_for_mysql(norm_name, trx, + thd->lex->sql_command == SQLCOM_DROP_DB); /* Flush the log to reduce probability that the .frm files and the InnoDB data dictionary get out-of-sync if the user runs @@ -3804,7 +3790,7 @@ innobase_drop_database( trx_t* trx; char* ptr; int error; - char namebuf[10000]; + char* namebuf; /* Get the transaction associated with the current thd, or create one if not yet created */ @@ -3824,6 +3810,7 @@ innobase_drop_database( } ptr++; + namebuf = my_malloc(len + 2, MYF(0)); memcpy(namebuf, ptr, len); namebuf[len] = '/'; @@ -3840,6 +3827,7 @@ innobase_drop_database( } error = row_drop_database_for_mysql(namebuf, trx); + my_free(namebuf, MYF(0)); /* Flush the log to reduce probability that the .frm files and the InnoDB data dictionary get out-of-sync if the user runs @@ -4364,15 +4352,18 @@ ha_innobase::update_table_comment( info on foreign keys */ const char* comment)/* in: table comment defined by user */ { - row_prebuilt_t* prebuilt = (row_prebuilt_t*)innobase_prebuilt; - uint length = strlen(comment); - char* str = my_malloc(length + 16500, MYF(0)); - char* pos; + uint length = strlen(comment); + char* str; + row_prebuilt_t* prebuilt = (row_prebuilt_t*)innobase_prebuilt; /* We do not know if MySQL can call this function before calling external_lock(). To be safe, update the thd of the current table handle. */ + if(length > 64000 - 3) { + return((char*)comment); /* string too long */ + } + update_thd(current_thd); prebuilt->trx->op_info = (char*)"returning table comment"; @@ -4381,37 +4372,47 @@ ha_innobase::update_table_comment( possible adaptive hash latch to avoid deadlocks of threads */ trx_search_latch_release_if_reserved(prebuilt->trx); - - if (!str) { - prebuilt->trx->op_info = (char*)""; + str = NULL; - return((char*)comment); - } + if (FILE* file = tmpfile()) { + long flen; - pos = str; - if (length) { - pos=strmov(str, comment); - *pos++=';'; - *pos++=' '; - } + /* output the data to a temporary file */ + fprintf(file, "InnoDB free: %lu kB", + (ulong) fsp_get_available_space_in_free_extents( + prebuilt->table->space)); - pos += my_sprintf(pos, - (pos,"InnoDB free: %lu kB", - (ulong) fsp_get_available_space_in_free_extents( - prebuilt->table->space))); + dict_print_info_on_foreign_keys(FALSE, file, prebuilt->table); + flen = ftell(file); + if(length + flen + 3 > 64000) { + flen = 64000 - 3 - length; + } - /* We assume 16000 - length bytes of space to print info; the limit - 16000 bytes is arbitrary, and MySQL could handle at least 64000 - bytes */ - - if (length < 16000) { - dict_print_info_on_foreign_keys(FALSE, pos, 16000 - length, - prebuilt->table); + ut_ad(flen > 0); + + /* allocate buffer for the full string, and + read the contents of the temporary file */ + + str = my_malloc(length + flen + 3, MYF(0)); + + if (str) { + char* pos = str + length; + if(length) { + memcpy(str, comment, length); + *pos++ = ';'; + *pos++ = ' '; + } + rewind(file); + flen = fread(pos, 1, flen, file); + pos[flen] = 0; + } + + fclose(file); } prebuilt->trx->op_info = (char*)""; - return(str); + return(str ? str : (char*) comment); } /*********************************************************************** @@ -4425,7 +4426,7 @@ ha_innobase::get_foreign_key_create_info(void) MUST be freed with ::free_foreign_key_create_info */ { row_prebuilt_t* prebuilt = (row_prebuilt_t*)innobase_prebuilt; - char* str; + char* str = 0; ut_a(prebuilt != NULL); @@ -4435,23 +4436,47 @@ ha_innobase::get_foreign_key_create_info(void) update_thd(current_thd); - prebuilt->trx->op_info = (char*)"getting info on foreign keys"; + if (FILE* file = tmpfile()) { + long flen; - /* In case MySQL calls this in the middle of a SELECT query, release - possible adaptive hash latch to avoid deadlocks of threads */ + prebuilt->trx->op_info = (char*)"getting info on foreign keys"; - trx_search_latch_release_if_reserved(prebuilt->trx); - - str = (char*)ut_malloc(10000); + /* In case MySQL calls this in the middle of a SELECT query, + release possible adaptive hash latch to avoid + deadlocks of threads */ - str[0] = '\0'; - - dict_print_info_on_foreign_keys(TRUE, str, 9000, prebuilt->table); + trx_search_latch_release_if_reserved(prebuilt->trx); - prebuilt->trx->op_info = (char*)""; + /* output the data to a temporary file */ + dict_print_info_on_foreign_keys(TRUE, file, prebuilt->table); + prebuilt->trx->op_info = (char*)""; + + flen = ftell(file); + if(flen > 64000 - 1) { + flen = 64000 - 1; + } + + ut_ad(flen >= 0); + + /* allocate buffer for the string, and + read the contents of the temporary file */ + + str = my_malloc(flen + 1, MYF(0)); + + if (str) { + rewind(file); + flen = fread(str, 1, flen, file); + str[flen] = 0; + } + + fclose(file); + } else { + /* unable to create temporary file */ + str = my_malloc(1, MYF(MY_ZEROFILL)); + } return(str); -} +} /*********************************************************************** Checks if a table is referenced by a foreign key. The MySQL manual states that @@ -4484,7 +4509,7 @@ ha_innobase::free_foreign_key_create_info( char* str) /* in, own: create info string to free */ { if (str) { - ut_free(str); + my_free(str, MYF(0)); } } @@ -4544,16 +4569,6 @@ ha_innobase::extra( } /********************************************************************** -????????????? */ - -int -ha_innobase::reset(void) -/*====================*/ -{ - return(0); -} - -/********************************************************************** MySQL calls this function at the start of each SQL statement inside LOCK TABLES. Inside LOCK TABLES the ::external_lock method does not work to mark SQL statement borders. Note also a special case: if a temporary table @@ -4640,12 +4655,11 @@ the SQL statement in case of an error. */ int ha_innobase::external_lock( /*=======================*/ - /* out: 0 or error code */ + /* out: 0 */ THD* thd, /* in: handle to the user thread */ int lock_type) /* in: lock type */ { row_prebuilt_t* prebuilt = (row_prebuilt_t*) innobase_prebuilt; - int error = 0; trx_t* trx; DBUG_ENTER("ha_innobase::external_lock"); @@ -4713,11 +4727,21 @@ ha_innobase::external_lock( } if (prebuilt->select_lock_type != LOCK_NONE) { + if (thd->in_lock_tables) { + ulint error; + error = row_lock_table_for_mysql(prebuilt); + + if (error != DB_SUCCESS) { + error = convert_error_code_to_mysql( + error, user_thd); + DBUG_RETURN(error); + } + } trx->mysql_n_tables_locked++; } - DBUG_RETURN(error); + DBUG_RETURN(0); } /* MySQL is releasing a table lock */ @@ -4725,6 +4749,9 @@ ha_innobase::external_lock( trx->n_mysql_tables_in_use--; prebuilt->mysql_has_locked = FALSE; auto_inc_counter_for_this_stat = 0; + if (trx->n_tables_locked) { + row_unlock_table_for_mysql(trx); + } /* If the MySQL lock count drops to zero we know that the current SQL statement has ended */ @@ -4756,7 +4783,7 @@ ha_innobase::external_lock( } } - DBUG_RETURN(error); + DBUG_RETURN(0); } /**************************************************************************** @@ -4768,7 +4795,6 @@ innodb_show_status( /*===============*/ THD* thd) /* in: the MySQL query thread of the caller */ { - char* buf; Protocol *protocol= thd->protocol; trx_t* trx; @@ -4785,34 +4811,56 @@ innodb_show_status( innobase_release_stat_resources(trx); - /* We let the InnoDB Monitor to output at most 60 kB of text, add - a safety margin of 100 kB for buffer overruns */ + /* We let the InnoDB Monitor to output at most 64000 bytes of text. */ - buf = (char*)ut_malloc(160 * 1024); + long flen; + char* str; - srv_sprintf_innodb_monitor(buf, 60 * 1024); + mutex_enter_noninline(&srv_monitor_file_mutex); + rewind(srv_monitor_file); + srv_printf_innodb_monitor(srv_monitor_file); + flen = ftell(srv_monitor_file); + os_file_set_eof(srv_monitor_file); + if(flen > 64000 - 1) { + flen = 64000 - 1; + } - List<Item> field_list; + ut_ad(flen > 0); - field_list.push_back(new Item_empty_string("Status", strlen(buf))); + /* allocate buffer for the string, and + read the contents of the temporary file */ - if (protocol->send_fields(&field_list, 1)) + if (!(str = my_malloc(flen + 1, MYF(0)))) { - ut_free(buf); - DBUG_RETURN(-1); + mutex_exit_noninline(&srv_monitor_file_mutex); + DBUG_RETURN(-1); } - protocol->prepare_for_resend(); - protocol->store(buf, strlen(buf), system_charset_info); + rewind(srv_monitor_file); + flen = fread(str, 1, flen, srv_monitor_file); - ut_free(buf); + mutex_exit_noninline(&srv_monitor_file_mutex); + + List<Item> field_list; + + field_list.push_back(new Item_empty_string("Status", flen)); + + if (protocol->send_fields(&field_list, 1)) { + + my_free(str, MYF(0)); + + DBUG_RETURN(-1); + } + + protocol->prepare_for_resend(); + protocol->store(str, flen, system_charset_info); + my_free(str, MYF(0)); if (protocol->write()) DBUG_RETURN(-1); - send_eof(thd); - - DBUG_RETURN(0); + send_eof(thd); + DBUG_RETURN(0); } /**************************************************************************** diff --git a/sql/ha_innodb.h b/sql/ha_innodb.h index c1a1b57472a..62f5e85b5d4 100644 --- a/sql/ha_innodb.h +++ b/sql/ha_innodb.h @@ -89,7 +89,7 @@ class ha_innobase: public handler int_table_flags(HA_REC_NOT_IN_SEQ | HA_KEYPOS_TO_RNDPOS | HA_LASTKEY_ORDER | - HA_NULL_KEY | + HA_NULL_KEY | HA_FAST_KEY_READ | HA_BLOB_KEY | HA_CAN_SQL_HANDLER | HA_NOT_EXACT_COUNT | @@ -123,7 +123,8 @@ class ha_innobase: public handler whose size is > MAX_KEY_LENGTH */ uint max_key_length() const { return((MAX_KEY_LENGTH <= 3500) ? MAX_KEY_LENGTH : 3500);} - bool fast_key_read() { return 1;} + uint max_key_part_length() { return((MAX_KEY_LENGTH <= 3500) ? + MAX_KEY_LENGTH : 3500);} const key_map *keys_to_use_for_scanning() { return &key_map_full; } bool has_transactions() { return 1;} @@ -161,7 +162,6 @@ class ha_innobase: public handler int optimize(THD* thd,HA_CHECK_OPT* check_opt); int discard_or_import_tablespace(my_bool discard); int extra(enum ha_extra_function operation); - int reset(void); int external_lock(THD *thd, int lock_type); int start_stmt(THD *thd); diff --git a/sql/ha_isam.cc b/sql/ha_isam.cc index 52fea2f7a15..09c2aeceafc 100644 --- a/sql/ha_isam.cc +++ b/sql/ha_isam.cc @@ -237,11 +237,6 @@ int ha_isam::extra(enum ha_extra_function operation) return nisam_extra(file,operation); } -int ha_isam::reset(void) -{ - return nisam_extra(file,HA_EXTRA_RESET); -} - int ha_isam::external_lock(THD *thd, int lock_type) { if (!table->tmp_table) diff --git a/sql/ha_isam.h b/sql/ha_isam.h index 129777e68e5..2c8ec274145 100644 --- a/sql/ha_isam.h +++ b/sql/ha_isam.h @@ -69,7 +69,6 @@ class ha_isam: public handler my_off_t row_position() { return nisam_position(file); } void info(uint); int extra(enum ha_extra_function operation); - int reset(void); int external_lock(THD *thd, int lock_type); ha_rows records_in_range(int inx, const byte *start_key,uint start_key_len, diff --git a/sql/ha_isammrg.cc b/sql/ha_isammrg.cc index 8f7056a15fa..20e2b4db423 100644 --- a/sql/ha_isammrg.cc +++ b/sql/ha_isammrg.cc @@ -171,11 +171,6 @@ int ha_isammrg::extra(enum ha_extra_function operation) return !mrg_extra(file,operation) ? 0 : my_errno ? my_errno : -1; } -int ha_isammrg::reset(void) -{ - return !mrg_extra(file,HA_EXTRA_RESET) ? 0 : my_errno ? my_errno : -1; -} - int ha_isammrg::external_lock(THD *thd, int lock_type) { return !mrg_lock_database(file,lock_type) ? 0 : my_errno ? my_errno : -1; diff --git a/sql/ha_isammrg.h b/sql/ha_isammrg.h index e5846d20212..289277a9dac 100644 --- a/sql/ha_isammrg.h +++ b/sql/ha_isammrg.h @@ -63,10 +63,10 @@ class ha_isammrg: public handler my_off_t row_position() { return mrg_position(file); } void info(uint); int extra(enum ha_extra_function operation); - int reset(void); int external_lock(THD *thd, int lock_type); uint lock_count(void) const; int create(const char *name, TABLE *form, HA_CREATE_INFO *create_info); THR_LOCK_DATA **store_lock(THD *thd, THR_LOCK_DATA **to, enum thr_lock_type lock_type); + uint8 table_cache_type() { return HA_CACHE_TBL_NOCACHE; } }; diff --git a/sql/ha_myisam.cc b/sql/ha_myisam.cc index c992e92edb8..dfb41efa88a 100644 --- a/sql/ha_myisam.cc +++ b/sql/ha_myisam.cc @@ -811,78 +811,94 @@ int ha_myisam::preload_keys(THD* thd, HA_CHECK_OPT *check_opt) } /* - Deactive all not unique index that can be recreated fast + disable indexes, making it persistent if requested + SYNOPSIS + disable_indexes(all, save) + all disable all indexes + if not set only non-unique indexes will be disabled + [all=1 is NOT IMPLEMENTED YET] + save save the disabled state, so that it will persist + between queries/threads/reboots + [save=0 is NOT IMPLEMENTED YET] +*/ +int ha_myisam::disable_indexes(bool all, bool save) +{ + mi_extra(file, HA_EXTRA_NO_KEYS, 0); + info(HA_STATUS_CONST); // Read new key info + return 0; +} + +int ha_myisam::enable_indexes() +{ + if (file->s->state.key_map == set_bits(ulonglong, file->s->base.keys)) + return 0; + + int error=0; + THD *thd=current_thd; + MI_CHECK param; + const char *save_proc_info=thd->proc_info; + thd->proc_info="Creating index"; + myisamchk_init(¶m); + param.op_name = (char*) "recreating_index"; + param.testflag = (T_SILENT | T_REP_BY_SORT | T_QUICK | + T_CREATE_MISSING_KEYS); + param.myf_rw&= ~MY_WAIT_IF_FULL; + param.sort_buffer_length= thd->variables.myisam_sort_buff_size; + param.tmpdir=&mysql_tmpdir_list; + error=repair(thd,param,0) != HA_ADMIN_OK; + info(HA_STATUS_CONST); + thd->proc_info=save_proc_info; + return error; +} + +/* + prepare for a many-rows insert operation + e.g. - disable indexes (if they can be recreated fast) or + activate special bulk-insert optimizations SYNOPSIS - deactivate_non_unique_index() - rows Rows to be inserted - 0 if we don't know - HA_POS_ERROR if we want to force disabling - and make it permanent (save on disk) + start_bulk_insert(rows) + rows Rows to be inserted + 0 if we don't know */ -void ha_myisam::deactivate_non_unique_index(ha_rows rows) +void ha_myisam::start_bulk_insert(ha_rows rows) { - MYISAM_SHARE* share = file->s; - if (share->state.key_map == ((ulonglong) 1L << share->base.keys)-1) + THD *thd=current_thd; + ulong size= min(thd->variables.read_buff_size, table->avg_row_length*rows); + + /* don't enable row cache if too few rows */ + if (!rows && rows > MI_MIN_ROWS_TO_USE_WRITE_CACHE) + mi_extra(file, HA_EXTRA_WRITE_CACHE, (void*) &size); + + can_enable_indexes= (file->s->state.key_map == + set_bits(ulonglong, file->s->base.keys)); + + if (!(specialflag & SPECIAL_SAFE_MODE)) { - if (!(specialflag & SPECIAL_SAFE_MODE)) + /* + Only disable old index if the table was empty and we are inserting + a lot of rows. + We should not do this for only a few rows as this is slower and + we don't want to update the key statistics based of only a few rows. + */ + if (file->state->records == 0 && can_enable_indexes && + (!rows || rows >= MI_MIN_ROWS_TO_DISABLE_INDEXES)) + mi_disable_non_unique_index(file,rows); + else + if (!file->bulk_insert && + (!rows || rows >= MI_MIN_ROWS_TO_USE_BULK_INSERT)) { - if (rows == HA_POS_ERROR) // force disable and save it on disk! - mi_extra(file, HA_EXTRA_NO_KEYS, 0); - else - { - /* - Only disable old index if the table was empty and we are inserting - a lot of rows. - We should not do this for only a few rows as this is slower and - we don't want to update the key statistics based of only a few rows. - */ - if (file->state->records == 0 && - (!rows || rows >= MI_MIN_ROWS_TO_DISABLE_INDEXES)) - mi_disable_non_unique_index(file,rows); - else if (!file->bulk_insert && - (!rows || rows >= MI_MIN_ROWS_TO_USE_BULK_INSERT)) - mi_init_bulk_insert(file, - current_thd->variables.bulk_insert_buff_size, - rows); - } + mi_init_bulk_insert(file, thd->variables.bulk_insert_buff_size, rows); } - enable_activate_all_index=1; - info(HA_STATUS_CONST); // Read new key info } - else - enable_activate_all_index=0; } - -bool ha_myisam::activate_all_index(THD *thd) +int ha_myisam::end_bulk_insert() { - int error=0; - MI_CHECK param; - MYISAM_SHARE* share = file->s; - DBUG_ENTER("activate_all_index"); - mi_end_bulk_insert(file); - if (enable_activate_all_index && - share->state.key_map != set_bits(ulonglong, share->base.keys)) - { - const char *save_proc_info=thd->proc_info; - thd->proc_info="Creating index"; - myisamchk_init(¶m); - param.op_name = (char*) "recreating_index"; - param.testflag = (T_SILENT | T_REP_BY_SORT | T_QUICK | - T_CREATE_MISSING_KEYS); - param.myf_rw&= ~MY_WAIT_IF_FULL; - param.sort_buffer_length= thd->variables.myisam_sort_buff_size; - param.tmpdir=&mysql_tmpdir_list; - error=repair(thd,param,0) != HA_ADMIN_OK; - info(HA_STATUS_CONST); - thd->proc_info=save_proc_info; - } - else - enable_activate_all_index=1; - DBUG_RETURN(error); + int err=mi_extra(file, HA_EXTRA_NO_CACHE, 0); + return err ? err : can_enable_indexes ? enable_indexes() : 0; } @@ -1115,12 +1131,6 @@ int ha_myisam::extra_opt(enum ha_extra_function operation, ulong cache_size) return mi_extra(file, operation, (void*) &cache_size); } - -int ha_myisam::reset(void) -{ - return mi_extra(file, HA_EXTRA_RESET, 0); -} - int ha_myisam::delete_all_rows() { return mi_delete_all_rows(file); diff --git a/sql/ha_myisam.h b/sql/ha_myisam.h index 4d66639690d..ca318b02778 100644 --- a/sql/ha_myisam.h +++ b/sql/ha_myisam.h @@ -39,7 +39,7 @@ class ha_myisam: public handler MI_INFO *file; ulong int_table_flags; char *data_file_name, *index_file_name; - bool enable_activate_all_index; + bool can_enable_indexes; int repair(THD *thd, MI_CHECK ¶m, bool optimize); public: @@ -48,7 +48,7 @@ class ha_myisam: public handler HA_NULL_KEY | HA_CAN_FULLTEXT | HA_CAN_SQL_HANDLER | HA_DUPP_POS | HA_BLOB_KEY | HA_AUTO_PART_KEY | HA_FILE_BASED | HA_HAS_GEOMETRY), - enable_activate_all_index(1) + can_enable_indexes(1) {} ~ha_myisam() {} const char *table_type() const { return "MyISAM"; } @@ -103,11 +103,12 @@ class ha_myisam: public handler void info(uint); int extra(enum ha_extra_function operation); int extra_opt(enum ha_extra_function operation, ulong cache_size); - int reset(void); int external_lock(THD *thd, int lock_type); int delete_all_rows(void); - void deactivate_non_unique_index(ha_rows rows); - bool activate_all_index(THD *thd); + int disable_indexes(bool all, bool save); + int enable_indexes(); + void start_bulk_insert(ha_rows rows); + int end_bulk_insert(); ha_rows records_in_range(int inx, const byte *start_key,uint start_key_len, enum ha_rkey_function start_search_flag, diff --git a/sql/ha_myisammrg.cc b/sql/ha_myisammrg.cc index 7c36f6c6e0e..041d9eaead3 100644 --- a/sql/ha_myisammrg.cc +++ b/sql/ha_myisammrg.cc @@ -271,12 +271,6 @@ int ha_myisammrg::extra_opt(enum ha_extra_function operation, ulong cache_size) return myrg_extra(file, operation, (void*) &cache_size); } - -int ha_myisammrg::reset(void) -{ - return myrg_extra(file,HA_EXTRA_RESET,0); -} - int ha_myisammrg::external_lock(THD *thd, int lock_type) { return myrg_lock_database(file,lock_type); diff --git a/sql/ha_myisammrg.h b/sql/ha_myisammrg.h index ea53b40739d..c0f81a77a1e 100644 --- a/sql/ha_myisammrg.h +++ b/sql/ha_myisammrg.h @@ -79,7 +79,6 @@ class ha_myisammrg: public handler void info(uint); int extra(enum ha_extra_function operation); int extra_opt(enum ha_extra_function operation, ulong cache_size); - int reset(void); int external_lock(THD *thd, int lock_type); uint lock_count(void) const; int create(const char *name, TABLE *form, HA_CREATE_INFO *create_info); diff --git a/sql/ha_ndbcluster.cc b/sql/ha_ndbcluster.cc new file mode 100644 index 00000000000..3bc322878d1 --- /dev/null +++ b/sql/ha_ndbcluster.cc @@ -0,0 +1,2943 @@ +/* Copyright (C) 2000-2003 MySQL AB + + 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; either version 2 of the License, or + (at your option) any later version. + + 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +*/ + +/* + This file defines the NDB Cluster handler: the interface between MySQL and + NDB Cluster +*/ + +/* + TODO + After CREATE DATABASE gör discover pÃ¥ alla tabeller i den databasen + +*/ + + +#ifdef __GNUC__ +#pragma implementation // gcc: Class implementation +#endif + +#include "mysql_priv.h" + +#ifdef HAVE_NDBCLUSTER_DB +#include <my_dir.h> +#include "ha_ndbcluster.h" +#include <ndbapi/NdbApi.hpp> +#include <ndbapi/NdbScanFilter.hpp> + +#define USE_DISCOVER_ON_STARTUP +//#define USE_NDB_POOL + +// Default value for parallelism +static const int parallelism= 240; + +#define NDB_HIDDEN_PRIMARY_KEY_LENGTH 8 + +#define ERR_PRINT(err) \ + DBUG_PRINT("error", ("Error: %d message: %s", err.code, err.message)) + +#define ERR_RETURN(err) \ +{ \ + ERR_PRINT(err); \ + DBUG_RETURN(ndb_to_mysql_error(&err)); \ +} + +// Typedefs for long names +typedef NdbDictionary::Column NDBCOL; +typedef NdbDictionary::Table NDBTAB; +typedef NdbDictionary::Index NDBINDEX; +typedef NdbDictionary::Dictionary NDBDICT; + +bool ndbcluster_inited= false; + +// Handler synchronization +pthread_mutex_t ndbcluster_mutex; + +// Table lock handling +static HASH ndbcluster_open_tables; + +static byte *ndbcluster_get_key(NDB_SHARE *share,uint *length, + my_bool not_used __attribute__((unused))); +static NDB_SHARE *get_share(const char *table_name); +static void free_share(NDB_SHARE *share); + +static int packfrm(const void *data, uint len, const void **pack_data, uint *pack_len); +static int unpackfrm(const void **data, uint *len, + const void* pack_data); + +/* + Error handling functions +*/ + +struct err_code_mapping +{ + int ndb_err; + int my_err; +}; + +static const err_code_mapping err_map[]= +{ + { 626, HA_ERR_KEY_NOT_FOUND }, + { 630, HA_ERR_FOUND_DUPP_KEY }, + { 893, HA_ERR_FOUND_DUPP_UNIQUE }, + { 721, HA_ERR_TABLE_EXIST }, + { 241, HA_ERR_OLD_METADATA }, + { -1, -1 } +}; + + +static int ndb_to_mysql_error(const NdbError *err) +{ + uint i; + for (i=0 ; err_map[i].ndb_err != err->code ; i++) + { + if (err_map[i].my_err == -1) + return err->code; + } + return err_map[i].my_err; +} + + +/* + Take care of the error that occured in NDB + + RETURN + 0 No error + # The mapped error code +*/ + +int ha_ndbcluster::ndb_err(NdbConnection *trans) +{ + const NdbError err= trans->getNdbError(); + if (!err.code) + return 0; // Don't log things to DBUG log if no error + DBUG_ENTER("ndb_err"); + + ERR_PRINT(err); + switch (err.classification) { + case NdbError::SchemaError: + { + NDBDICT *dict= m_ndb->getDictionary(); + DBUG_PRINT("info", ("invalidateTable %s", m_tabname)); + dict->invalidateTable(m_tabname); + break; + } + default: + break; + } + DBUG_RETURN(ndb_to_mysql_error(&err)); +} + + +/* + Instruct NDB to set the value of the hidden primary key +*/ + +bool ha_ndbcluster::set_hidden_key(NdbOperation *ndb_op, + uint fieldnr, const byte *field_ptr) +{ + DBUG_ENTER("set_hidden_key"); + DBUG_RETURN(ndb_op->equal(fieldnr, (char*)field_ptr, + NDB_HIDDEN_PRIMARY_KEY_LENGTH) != 0); +} + + +/* + Instruct NDB to set the value of one primary key attribute +*/ + +int ha_ndbcluster::set_ndb_key(NdbOperation *ndb_op, Field *field, + uint fieldnr, const byte *field_ptr) +{ + uint32 pack_len= field->pack_length(); + DBUG_ENTER("set_ndb_key"); + DBUG_PRINT("enter", ("%d: %s, ndb_type: %u, len=%d", + fieldnr, field->field_name, field->type(), + pack_len)); + DBUG_DUMP("key", (char*)field_ptr, pack_len); + + switch (field->type()) { + case MYSQL_TYPE_DECIMAL: + case MYSQL_TYPE_TINY: + case MYSQL_TYPE_SHORT: + case MYSQL_TYPE_LONG: + case MYSQL_TYPE_FLOAT: + case MYSQL_TYPE_DOUBLE: + case MYSQL_TYPE_TIMESTAMP: + case MYSQL_TYPE_LONGLONG: + case MYSQL_TYPE_INT24: + case MYSQL_TYPE_DATE: + case MYSQL_TYPE_TIME: + case MYSQL_TYPE_DATETIME: + case MYSQL_TYPE_YEAR: + case MYSQL_TYPE_NEWDATE: + case MYSQL_TYPE_ENUM: + case MYSQL_TYPE_SET: + case MYSQL_TYPE_VAR_STRING: + case MYSQL_TYPE_STRING: + // Common implementation for most field types + DBUG_RETURN(ndb_op->equal(fieldnr, (char*) field_ptr, pack_len) != 0); + + case MYSQL_TYPE_TINY_BLOB: + case MYSQL_TYPE_MEDIUM_BLOB: + case MYSQL_TYPE_LONG_BLOB: + case MYSQL_TYPE_BLOB: + case MYSQL_TYPE_NULL: + case MYSQL_TYPE_GEOMETRY: + default: + // Unhandled field types + DBUG_PRINT("error", ("Field type %d not supported", field->type())); + DBUG_RETURN(2); + } + DBUG_RETURN(3); +} + + +/* + Instruct NDB to set the value of one attribute +*/ + +int ha_ndbcluster::set_ndb_value(NdbOperation *ndb_op, Field *field, + uint fieldnr) +{ + const byte* field_ptr= field->ptr; + uint32 pack_len= field->pack_length(); + DBUG_ENTER("set_ndb_value"); + DBUG_PRINT("enter", ("%d: %s, type: %u, len=%d, is_null=%s", + fieldnr, field->field_name, field->type(), + pack_len, field->is_null()?"Y":"N")); + DBUG_DUMP("value", (char*) field_ptr, pack_len); + + if (field->is_null()) + { + // Set value to NULL + DBUG_RETURN((ndb_op->setValue(fieldnr, (char*)NULL, pack_len) != 0)); + } + + switch (field->type()) { + case MYSQL_TYPE_DECIMAL: + case MYSQL_TYPE_TINY: + case MYSQL_TYPE_SHORT: + case MYSQL_TYPE_LONG: + case MYSQL_TYPE_FLOAT: + case MYSQL_TYPE_DOUBLE: + case MYSQL_TYPE_TIMESTAMP: + case MYSQL_TYPE_LONGLONG: + case MYSQL_TYPE_INT24: + case MYSQL_TYPE_DATE: + case MYSQL_TYPE_TIME: + case MYSQL_TYPE_DATETIME: + case MYSQL_TYPE_YEAR: + case MYSQL_TYPE_NEWDATE: + case MYSQL_TYPE_ENUM: + case MYSQL_TYPE_SET: + case MYSQL_TYPE_VAR_STRING: + case MYSQL_TYPE_STRING: + // Common implementation for most field types + DBUG_RETURN(ndb_op->setValue(fieldnr, (char*)field_ptr, pack_len) != 0); + + case MYSQL_TYPE_TINY_BLOB: + case MYSQL_TYPE_MEDIUM_BLOB: + case MYSQL_TYPE_LONG_BLOB: + case MYSQL_TYPE_BLOB: + case MYSQL_TYPE_NULL: + case MYSQL_TYPE_GEOMETRY: + default: + // Unhandled field types + DBUG_PRINT("error", ("Field type %d not supported", field->type())); + DBUG_RETURN(2); + } + DBUG_RETURN(3); +} + + +/* + Instruct NDB to fetch one field + - data is read directly into buffer provided by field_ptr + if it's NULL, data is read into memory provided by NDBAPI +*/ + +int ha_ndbcluster::get_ndb_value(NdbOperation *op, + uint field_no, byte *field_ptr) +{ + DBUG_ENTER("get_ndb_value"); + DBUG_PRINT("enter", ("field_no: %d", field_no)); + m_value[field_no]= op->getValue(field_no, field_ptr); + DBUG_RETURN(m_value == NULL); +} + + +/* + Get metadata for this table from NDB + + IMPLEMENTATION + - save the NdbDictionary::Table for easy access + - check that frm-file on disk is equal to frm-file + of table accessed in NDB + - build a list of the indexes for the table +*/ + +int ha_ndbcluster::get_metadata(const char *path) +{ + NDBDICT *dict= m_ndb->getDictionary(); + const NDBTAB *tab; + const void *data, *pack_data; + const char **key_name; + uint ndb_columns, mysql_columns, length, pack_length, i; + int error; + DBUG_ENTER("get_metadata"); + DBUG_PRINT("enter", ("m_tabname: %s, path: %s", m_tabname, path)); + + if (!(tab= dict->getTable(m_tabname))) + ERR_RETURN(dict->getNdbError()); + DBUG_PRINT("info", ("Table schema version: %d", tab->getObjectVersion())); + + /* + This is the place to check that the table we got from NDB + is equal to the one on local disk + */ + ndb_columns= (uint) tab->getNoOfColumns(); + mysql_columns= table->fields; + if (table->primary_key == MAX_KEY) + ndb_columns--; + if (ndb_columns != mysql_columns) + { + DBUG_PRINT("error", + ("Wrong number of columns, ndb: %d mysql: %d", + ndb_columns, mysql_columns)); + DBUG_RETURN(HA_ERR_OLD_METADATA); + } + + /* + Compare FrmData in NDB with frm file from disk. + */ + error= 0; + if (readfrm(path, &data, &length) || + packfrm(data, length, &pack_data, &pack_length)) + { + my_free((char*)data, MYF(MY_ALLOW_ZERO_PTR)); + my_free((char*)pack_data, MYF(MY_ALLOW_ZERO_PTR)); + DBUG_RETURN(1); + } + + if ((pack_length != tab->getFrmLength()) || + (memcmp(pack_data, tab->getFrmData(), pack_length))) + { + DBUG_PRINT("error", + ("metadata, pack_length: %d getFrmLength: %d memcmp: %d", + pack_length, tab->getFrmLength(), + memcmp(pack_data, tab->getFrmData(), pack_length))); + DBUG_DUMP("pack_data", (char*)pack_data, pack_length); + DBUG_DUMP("frm", (char*)tab->getFrmData(), tab->getFrmLength()); + error= HA_ERR_OLD_METADATA; + } + my_free((char*)data, MYF(0)); + my_free((char*)pack_data, MYF(0)); + if (error) + DBUG_RETURN(error); + + // All checks OK, lets use the table + m_table= (void*)tab; + + for (i= 0; i < MAX_KEY; i++) + m_indextype[i]= UNDEFINED_INDEX; + + // Save information about all known indexes + for (i= 0; i < table->keys; i++) + m_indextype[i] = get_index_type_from_table(i); + + DBUG_RETURN(0); +} + +/* + Decode the type of an index from information + provided in table object +*/ +NDB_INDEX_TYPE ha_ndbcluster::get_index_type_from_table(uint index_no) const +{ + if (index_no == table->primary_key) + return PRIMARY_KEY_INDEX; + else + return ((table->key_info[index_no].flags & HA_NOSAME) ? + UNIQUE_INDEX : + ORDERED_INDEX); +} + + +void ha_ndbcluster::release_metadata() +{ + DBUG_ENTER("release_metadata"); + DBUG_PRINT("enter", ("m_tabname: %s", m_tabname)); + + m_table= NULL; + + DBUG_VOID_RETURN; +} + +static const ulong index_type_flags[]= +{ + /* UNDEFINED_INDEX */ + 0, + + /* PRIMARY_KEY_INDEX */ + HA_ONLY_WHOLE_INDEX | + HA_WRONG_ASCII_ORDER | + HA_NOT_READ_PREFIX_LAST, + + /* UNIQUE_INDEX */ + HA_ONLY_WHOLE_INDEX | + HA_WRONG_ASCII_ORDER | + HA_NOT_READ_PREFIX_LAST, + + /* ORDERED_INDEX */ + HA_READ_NEXT | + HA_READ_PREV | + HA_NOT_READ_AFTER_KEY +}; + +static const int index_flags_size= sizeof(index_type_flags)/sizeof(ulong); + +inline const char* ha_ndbcluster::get_index_name(uint idx_no) const +{ + return table->keynames.type_names[idx_no]; +} + +inline NDB_INDEX_TYPE ha_ndbcluster::get_index_type(uint idx_no) const +{ + DBUG_ASSERT(idx_no < MAX_KEY); + return m_indextype[idx_no]; +} + + +/* + Get the flags for an index + + RETURN + flags depending on the type of the index. +*/ + +inline ulong ha_ndbcluster::index_flags(uint idx_no) const +{ + DBUG_ENTER("index_flags"); + DBUG_ASSERT(get_index_type_from_table(idx_no) < index_flags_size); + DBUG_RETURN(index_type_flags[get_index_type_from_table(idx_no)]); +} + + +int ha_ndbcluster::set_primary_key(NdbOperation *op, const byte *key) +{ + KEY* key_info= table->key_info + table->primary_key; + KEY_PART_INFO* key_part= key_info->key_part; + KEY_PART_INFO* end= key_part+key_info->key_parts; + DBUG_ENTER("set_primary_key"); + + for (; key_part != end; key_part++) + { + Field* field= key_part->field; + if (set_ndb_key(op, field, + key_part->fieldnr-1, key)) + ERR_RETURN(op->getNdbError()); + key += key_part->length; + } + DBUG_RETURN(0); +} + + +int ha_ndbcluster::set_primary_key(NdbOperation *op) +{ + DBUG_ENTER("set_primary_key"); + KEY* key_info= table->key_info + table->primary_key; + KEY_PART_INFO* key_part= key_info->key_part; + KEY_PART_INFO* end= key_part+key_info->key_parts; + + for (; key_part != end; key_part++) + { + Field* field= key_part->field; + if (set_ndb_key(op, field, + key_part->fieldnr-1, field->ptr)) + ERR_RETURN(op->getNdbError()); + } + DBUG_RETURN(0); +} + + +/* + Read one record from NDB using primary key +*/ + +int ha_ndbcluster::pk_read(const byte *key, uint key_len, byte *buf) +{ + uint no_fields= table->fields, i; + NdbConnection *trans= m_active_trans; + NdbOperation *op; + THD *thd= current_thd; + DBUG_ENTER("pk_read"); + DBUG_PRINT("enter", ("key_len: %u", key_len)); + DBUG_DUMP("key", (char*)key, key_len); + + if (!(op= trans->getNdbOperation(m_tabname)) || op->readTuple() != 0) + goto err; + + if (table->primary_key == MAX_KEY) + { + // This table has no primary key, use "hidden" primary key + DBUG_PRINT("info", ("Using hidden key")); + DBUG_DUMP("key", (char*)key, 8); + if (set_hidden_key(op, no_fields, key)) + goto err; + // Read key at the same time, for future reference + if (get_ndb_value(op, no_fields, NULL)) + goto err; + } + else + { + int res; + if ((res= set_primary_key(op, key))) + return res; + } + + // Read non-key field(s) + for (i= 0; i < no_fields; i++) + { + Field *field= table->field[i]; + if (thd->query_id == field->query_id) + { + if (get_ndb_value(op, i, field->ptr)) + goto err; + } + else + { + // Attribute was not to be read + m_value[i]= NULL; + } + } + + if (trans->execute(NoCommit, IgnoreError) != 0) + { + table->status= STATUS_NOT_FOUND; + DBUG_RETURN(ndb_err(trans)); + } + + // The value have now been fetched from NDB + unpack_record(buf); + table->status= 0; + DBUG_RETURN(0); + + err: + ERR_RETURN(trans->getNdbError()); +} + + +/* + Read one record from NDB using unique secondary index +*/ + +int ha_ndbcluster::unique_index_read(const byte *key, + uint key_len, byte *buf) +{ + NdbConnection *trans= m_active_trans; + const char *index_name; + NdbIndexOperation *op; + THD *thd= current_thd; + byte *key_ptr; + KEY* key_info; + KEY_PART_INFO *key_part, *end; + uint i; + DBUG_ENTER("unique_index_read"); + DBUG_PRINT("enter", ("key_len: %u, index: %u", key_len, active_index)); + DBUG_DUMP("key", (char*)key, key_len); + + index_name= get_index_name(active_index); + if (!(op= trans->getNdbIndexOperation(index_name, m_tabname)) || + op->readTuple() != 0) + ERR_RETURN(trans->getNdbError()); + + // Set secondary index key(s) + key_ptr= (byte *) key; + key_info= table->key_info + active_index; + DBUG_ASSERT(key_info->key_length == key_len); + end= (key_part= key_info->key_part) + key_info->key_parts; + + for (i= 0; key_part != end; key_part++, i++) + { + if (set_ndb_key(op, key_part->field, i, key_ptr)) + ERR_RETURN(trans->getNdbError()); + key_ptr+= key_part->length; + } + + // Get non-index attribute(s) + for (i= 0; i < table->fields; i++) + { + Field *field= table->field[i]; + if ((thd->query_id == field->query_id) || + (field->flags & PRI_KEY_FLAG)) + { + if (get_ndb_value(op, i, field->ptr)) + ERR_RETURN(op->getNdbError()); + } + else + { + // Attribute was not to be read + m_value[i]= NULL; + } + } + + if (trans->execute(NoCommit, IgnoreError) != 0) + { + table->status= STATUS_NOT_FOUND; + DBUG_RETURN(ndb_err(trans)); + } + // The value have now been fetched from NDB + unpack_record(buf); + table->status= 0; + DBUG_RETURN(0); +} + +/* + Get the next record of a started scan +*/ + +inline int ha_ndbcluster::next_result(byte *buf) +{ + NdbConnection *trans= m_active_trans; + NdbResultSet *cursor= m_active_cursor; + DBUG_ENTER("next_result"); + + if (cursor->nextResult() == 0) + { + // One more record found + unpack_record(buf); + table->status= 0; + DBUG_RETURN(0); + } + table->status= STATUS_NOT_FOUND; + if (ndb_err(trans)) + ERR_RETURN(trans->getNdbError()); + + // No more records + DBUG_PRINT("info", ("No more records")); + DBUG_RETURN(HA_ERR_END_OF_FILE); +} + + +/* + Read record(s) from NDB using ordered index scan +*/ + +int ha_ndbcluster::ordered_index_scan(const byte *key, uint key_len, + byte *buf, + enum ha_rkey_function find_flag) +{ + uint no_fields= table->fields; + uint tot_len, i; + NdbConnection *trans= m_active_trans; + NdbResultSet *cursor= m_active_cursor; + NdbScanOperation *op; + const char *bound_str= NULL; + const char *index_name; + NdbOperation::BoundType bound_type = NdbOperation::BoundEQ; + bool can_be_handled_by_ndb= FALSE; + byte *key_ptr; + KEY *key_info; + THD* thd = current_thd; + DBUG_ENTER("ordered_index_scan"); + DBUG_PRINT("enter", ("index: %u", active_index)); + DBUG_PRINT("enter", ("Starting new ordered scan on %s", m_tabname)); + + index_name= get_index_name(active_index); + if (!(op= trans->getNdbScanOperation(index_name, m_tabname))) + ERR_RETURN(trans->getNdbError()); + if (!(cursor= op->readTuples(parallelism))) + ERR_RETURN(trans->getNdbError()); + m_active_cursor= cursor; + + switch (find_flag) { + case HA_READ_KEY_EXACT: /* Find first record else error */ + bound_str= "HA_READ_KEY_EXACT"; + bound_type= NdbOperation::BoundEQ; + can_be_handled_by_ndb= TRUE; + break; + case HA_READ_KEY_OR_NEXT: /* Record or next record */ + bound_str= "HA_READ_KEY_OR_NEXT"; + bound_type= NdbOperation::BoundLE; + can_be_handled_by_ndb= TRUE; + break; + case HA_READ_KEY_OR_PREV: /* Record or previous */ + bound_str= "HA_READ_KEY_OR_PREV"; + bound_type= NdbOperation::BoundGE; + can_be_handled_by_ndb= TRUE; + break; + case HA_READ_AFTER_KEY: /* Find next rec. after key-record */ + bound_str= "HA_READ_AFTER_KEY"; + bound_type= NdbOperation::BoundLT; + can_be_handled_by_ndb= TRUE; + break; + case HA_READ_BEFORE_KEY: /* Find next rec. before key-record */ + bound_str= "HA_READ_BEFORE_KEY"; + bound_type= NdbOperation::BoundGT; + can_be_handled_by_ndb= TRUE; + break; + case HA_READ_PREFIX: /* Key which as same prefix */ + bound_str= "HA_READ_PREFIX"; + break; + case HA_READ_PREFIX_LAST: /* Last key with the same prefix */ + bound_str= "HA_READ_PREFIX_LAST"; + break; + case HA_READ_PREFIX_LAST_OR_PREV: + /* Last or prev key with the same prefix */ + bound_str= "HA_READ_PREFIX_LAST_OR_PREV"; + break; + default: + bound_str= "UNKNOWN"; + break; + } + DBUG_PRINT("info", ("find_flag: %s, bound_type: %d," + "can_be_handled_by_ndb: %d", + bound_str, bound_type, can_be_handled_by_ndb)); + if (!can_be_handled_by_ndb) + DBUG_RETURN(1); + + // Set bounds using key data + tot_len= 0; + key_ptr= (byte *) key; + key_info= table->key_info + active_index; + for (i= 0; i < key_info->key_parts; i++) + { + Field* field= key_info->key_part[i].field; + uint32 field_len= field->pack_length(); + DBUG_PRINT("info", ("Set index bound on %s", + field->field_name)); + DBUG_DUMP("key", (char*)key_ptr, field_len); + + if (op->setBound(field->field_name, + bound_type, + key_ptr, + field_len) != 0) + ERR_RETURN(op->getNdbError()); + + key_ptr+= field_len; + tot_len+= field_len; + if (tot_len >= key_len) + break; + } + + // Define attributes to read + for (i= 0; i < no_fields; i++) + { + Field *field= table->field[i]; + if ((thd->query_id == field->query_id) || + (field->flags & PRI_KEY_FLAG)) + { + if (get_ndb_value(op, i, field->ptr)) + ERR_RETURN(op->getNdbError()); + } + else + { + m_value[i]= NULL; + } + } + + if (table->primary_key == MAX_KEY) + { + DBUG_PRINT("info", ("Getting hidden key")); + // Scanning table with no primary key + int hidden_no= no_fields; +#ifndef DBUG_OFF + const NDBTAB *tab= (NDBTAB *) m_table; + if (!tab->getColumn(hidden_no)) + DBUG_RETURN(1); +#endif + if (get_ndb_value(op, hidden_no, NULL)) + ERR_RETURN(op->getNdbError()); + } + + if (trans->execute(NoCommit) != 0) + DBUG_RETURN(ndb_err(trans)); + DBUG_PRINT("exit", ("Scan started successfully")); + DBUG_RETURN(next_result(buf)); +} + + +#if 0 +/* + Read record(s) from NDB using full table scan with filter + */ + +int ha_ndbcluster::filtered_scan(const byte *key, uint key_len, + byte *buf, + enum ha_rkey_function find_flag) +{ + uint no_fields= table->fields; + NdbConnection *trans= m_active_trans; + NdbResultSet *cursor= m_active_cursor; + + DBUG_ENTER("filtered_scan"); + DBUG_PRINT("enter", ("key_len: %u, index: %u", + key_len, active_index)); + DBUG_DUMP("key", (char*)key, key_len); + DBUG_PRINT("info", ("Starting a new filtered scan on %s", + m_tabname)); + NdbScanOperation *op= trans->getNdbScanOperation(m_tabname); + if (!op) + ERR_RETURN(trans->getNdbError()); + + cursor= op->readTuples(parallelism); + if (!cursor) + ERR_RETURN(trans->getNdbError()); + m_active_cursor= cursor; + + { + // Start scan filter + NdbScanFilter sf(op); + sf.begin(); + + // Set filter using the supplied key data + byte *key_ptr= (byte *) key; + uint tot_len= 0; + KEY* key_info= table->key_info + active_index; + for (uint k= 0; k < key_info->key_parts; k++) + { + KEY_PART_INFO* key_part= key_info->key_part+k; + Field* field= key_part->field; + uint ndb_fieldnr= key_part->fieldnr-1; + DBUG_PRINT("key_part", ("fieldnr: %d", ndb_fieldnr)); + // const NDBCOL *col= tab->getColumn(ndb_fieldnr); + uint32 field_len= field->pack_length(); + DBUG_DUMP("key", (char*)key, field_len); + + DBUG_PRINT("info", ("Column %s, type: %d, len: %d", + field->field_name, field->real_type(), field_len)); + + // Define scan filter + if (field->real_type() == MYSQL_TYPE_STRING) + sf.eq(ndb_fieldnr, key_ptr, field_len); + else + { + if (field_len == 8) + sf.eq(ndb_fieldnr, (Uint64)*key_ptr); + else if (field_len <= 4) + sf.eq(ndb_fieldnr, (Uint32)*key_ptr); + else + DBUG_RETURN(1); + } + + key_ptr += field_len; + tot_len += field_len; + + if (tot_len >= key_len) + break; + } + // End scan filter + sf.end(); + } + + // Define attributes to read + for (uint field_no= 0; field_no < no_fields; field_no++) + { + Field *field= table->field[field_no]; + + // Read attribute + DBUG_PRINT("get", ("%d: %s", field_no, field->field_name)); + if (get_ndb_value(op, field_no, field->ptr)) + ERR_RETURN(op->getNdbError()); + } + + if (table->primary_key == MAX_KEY) + { + DBUG_PRINT("info", ("Getting hidden key")); + // Scanning table with no primary key + int hidden_no= no_fields; +#ifndef DBUG_OFF + const NDBTAB *tab= (NDBTAB *) m_table; + if (!tab->getColumn(hidden_no)) + DBUG_RETURN(1); +#endif + if (get_ndb_value(op, hidden_no, NULL)) + ERR_RETURN(op->getNdbError()); + } + + if (trans->execute(NoCommit) != 0) + DBUG_RETURN(ndb_err(trans)); + DBUG_PRINT("exit", ("Scan started successfully")); + DBUG_RETURN(next_result(buf)); +} +#endif + + +/* + Read records from NDB using full table scan + */ + +int ha_ndbcluster::full_table_scan(byte *buf) +{ + uint i; + THD *thd= current_thd; + NdbConnection *trans= m_active_trans; + NdbResultSet *cursor; + NdbScanOperation *op; + + DBUG_ENTER("full_table_scan"); + DBUG_PRINT("enter", ("Starting new scan on %s", m_tabname)); + + if (!(op=trans->getNdbScanOperation(m_tabname))) + ERR_RETURN(trans->getNdbError()); + if (!(cursor= op->readTuples(parallelism))) + ERR_RETURN(trans->getNdbError()); + m_active_cursor= cursor; + + // Define attributes to read + for (i= 0; i < table->fields; i++) + { + Field *field= table->field[i]; + if ((thd->query_id == field->query_id) || + (field->flags & PRI_KEY_FLAG)) + { + if (get_ndb_value(op, i, field->ptr)) + ERR_RETURN(op->getNdbError()); + } + else + { + m_value[i]= NULL; + } + } + + if (table->primary_key == MAX_KEY) + { + DBUG_PRINT("info", ("Getting hidden key")); + // Scanning table with no primary key + int hidden_no= table->fields; +#ifndef DBUG_OFF + const NDBTAB *tab= (NDBTAB *) m_table; + if (!tab->getColumn(hidden_no)) + DBUG_RETURN(1); +#endif + if (get_ndb_value(op, hidden_no, NULL)) + ERR_RETURN(op->getNdbError()); + } + + if (trans->execute(NoCommit) != 0) + DBUG_RETURN(ndb_err(trans)); + DBUG_PRINT("exit", ("Scan started successfully")); + DBUG_RETURN(next_result(buf)); +} + + +/* + Insert one record into NDB +*/ + +int ha_ndbcluster::write_row(byte *record) +{ + uint i; + NdbConnection *trans= m_active_trans; + NdbOperation *op; + int res; + DBUG_ENTER("write_row"); + + statistic_increment(ha_write_count,&LOCK_status); + if (table->timestamp_default_now) + update_timestamp(record+table->timestamp_default_now-1); + if (table->next_number_field && record == table->record[0]) + update_auto_increment(); + + if (!(op= trans->getNdbOperation(m_tabname))) + ERR_RETURN(trans->getNdbError()); + + res= (m_use_write) ? op->writeTuple() :op->insertTuple(); + if (res != 0) + ERR_RETURN(trans->getNdbError()); + + if (table->primary_key == MAX_KEY) + { + // Table has hidden primary key + Uint64 auto_value= m_ndb->getAutoIncrementValue(m_tabname); + if (set_hidden_key(op, table->fields, (const byte*)&auto_value)) + ERR_RETURN(op->getNdbError()); + } + else + { + int res; + if ((res= set_primary_key(op))) + return res; + } + + // Set non-key attribute(s) + for (i= 0; i < table->fields; i++) + { + Field *field= table->field[i]; + if (!(field->flags & PRI_KEY_FLAG) && + set_ndb_value(op, field, i)) + ERR_RETURN(op->getNdbError()); + } + + /* + Execute write operation + NOTE When doing inserts with many values in + each INSERT statement it should not be necessary + to NoCommit the transaction between each row. + Find out how this is detected! + */ + if (trans->execute(NoCommit) != 0) + DBUG_RETURN(ndb_err(trans)); + DBUG_RETURN(0); +} + + +/* Compare if a key in a row has changed */ + +int ha_ndbcluster::key_cmp(uint keynr, const byte * old_row, + const byte * new_row) +{ + KEY_PART_INFO *key_part=table->key_info[keynr].key_part; + KEY_PART_INFO *end=key_part+table->key_info[keynr].key_parts; + + for (; key_part != end ; key_part++) + { + if (key_part->null_bit) + { + if ((old_row[key_part->null_offset] & key_part->null_bit) != + (new_row[key_part->null_offset] & key_part->null_bit)) + return 1; + } + if (key_part->key_part_flag & (HA_BLOB_PART | HA_VAR_LENGTH)) + { + + if (key_part->field->cmp_binary((char*) (old_row + key_part->offset), + (char*) (new_row + key_part->offset), + (ulong) key_part->length)) + return 1; + } + else + { + if (memcmp(old_row+key_part->offset, new_row+key_part->offset, + key_part->length)) + return 1; + } + } + return 0; +} + +/* + Update one record in NDB using primary key +*/ + +int ha_ndbcluster::update_row(const byte *old_data, byte *new_data) +{ + THD *thd= current_thd; + NdbConnection *trans= m_active_trans; + NdbOperation *op; + uint i; + DBUG_ENTER("update_row"); + + statistic_increment(ha_update_count,&LOCK_status); + if (table->timestamp_on_update_now) + update_timestamp(new_data+table->timestamp_on_update_now-1); + + if (!(op= trans->getNdbOperation(m_tabname)) || + op->updateTuple() != 0) + ERR_RETURN(trans->getNdbError()); + + if (table->primary_key == MAX_KEY) + { + // This table has no primary key, use "hidden" primary key + DBUG_PRINT("info", ("Using hidden key")); + + // Require that the PK for this record has previously been + // read into m_value + uint no_fields= table->fields; + NdbRecAttr* rec= m_value[no_fields]; + DBUG_ASSERT(rec); + DBUG_DUMP("key", (char*)rec->aRef(), NDB_HIDDEN_PRIMARY_KEY_LENGTH); + + if (set_hidden_key(op, no_fields, rec->aRef())) + ERR_RETURN(op->getNdbError()); + } + else + { + /* Check for update of primary key and return error */ + if (key_cmp(table->primary_key, old_data, new_data)) + DBUG_RETURN(HA_ERR_UNSUPPORTED); + + int res; + if ((res= set_primary_key(op, old_data + table->null_bytes))) + DBUG_RETURN(res); + } + + // Set non-key attribute(s) + for (i= 0; i < table->fields; i++) + { + + Field *field= table->field[i]; + if ((thd->query_id == field->query_id) && + (!(field->flags & PRI_KEY_FLAG)) && + set_ndb_value(op, field, i)) + ERR_RETURN(op->getNdbError()); + } + + // Execute update operation + if (trans->execute(NoCommit) != 0) + DBUG_RETURN(ndb_err(trans)); + + DBUG_RETURN(0); +} + + +/* + Delete one record from NDB, using primary key +*/ + +int ha_ndbcluster::delete_row(const byte *record) +{ + NdbConnection *trans= m_active_trans; + NdbOperation *op; + DBUG_ENTER("delete_row"); + + statistic_increment(ha_delete_count,&LOCK_status); + + if (!(op=trans->getNdbOperation(m_tabname)) || + op->deleteTuple() != 0) + ERR_RETURN(trans->getNdbError()); + + if (table->primary_key == MAX_KEY) + { + // This table has no primary key, use "hidden" primary key + DBUG_PRINT("info", ("Using hidden key")); + uint no_fields= table->fields; + NdbRecAttr* rec= m_value[no_fields]; + DBUG_ASSERT(rec != NULL); + + if (set_hidden_key(op, no_fields, rec->aRef())) + ERR_RETURN(op->getNdbError()); + } + else + { + int res; + if ((res= set_primary_key(op))) + return res; + } + + // Execute delete operation + if (trans->execute(NoCommit) != 0) + DBUG_RETURN(ndb_err(trans)); + DBUG_RETURN(0); +} + +/* + Unpack a record read from NDB + + SYNOPSIS + unpack_record() + buf Buffer to store read row + + NOTE + The data for each row is read directly into the + destination buffer. This function is primarily + called in order to check if any fields should be + set to null. +*/ + +void ha_ndbcluster::unpack_record(byte* buf) +{ + uint row_offset= (uint) (buf - table->record[0]); + Field **field, **end; + NdbRecAttr **value= m_value; + DBUG_ENTER("unpack_record"); + + // Set null flag(s) + bzero(buf, table->null_bytes); + for (field= table->field, end= field+table->fields; + field < end; + field++, value++) + { + if (*value && (*value)->isNULL()) + (*field)->set_null(row_offset); + } + +#ifndef DBUG_OFF + // Read and print all values that was fetched + if (table->primary_key == MAX_KEY) + { + // Table with hidden primary key + int hidden_no= table->fields; + const NDBTAB *tab= (NDBTAB *) m_table; + const NDBCOL *hidden_col= tab->getColumn(hidden_no); + NdbRecAttr* rec= m_value[hidden_no]; + DBUG_ASSERT(rec); + DBUG_PRINT("hidden", ("%d: %s \"%llu\"", hidden_no, + hidden_col->getName(), rec->u_64_value())); + } + print_results(); +#endif + DBUG_VOID_RETURN; +} + + +/* + Utility function to print/dump the fetched field + */ + +void ha_ndbcluster::print_results() +{ + const NDBTAB *tab= (NDBTAB*) m_table; + DBUG_ENTER("print_results"); + +#ifndef DBUG_OFF + if (!_db_on_) + DBUG_VOID_RETURN; + + for (uint f=0; f<table->fields;f++) + { + Field *field; + const NDBCOL *col; + NdbRecAttr *value; + + if (!(value= m_value[f])) + { + fprintf(DBUG_FILE, "Field %d was not read\n", f); + continue; + } + field= table->field[f]; + DBUG_DUMP("field->ptr", (char*)field->ptr, field->pack_length()); + col= tab->getColumn(f); + fprintf(DBUG_FILE, "%d: %s\t", f, col->getName()); + + if (value->isNULL()) + { + fprintf(DBUG_FILE, "NULL\n"); + continue; + } + + switch (col->getType()) { + case NdbDictionary::Column::Blob: + case NdbDictionary::Column::Undefined: + fprintf(DBUG_FILE, "Unknown type: %d", col->getType()); + break; + case NdbDictionary::Column::Tinyint: { + char value= *field->ptr; + fprintf(DBUG_FILE, "Tinyint\t%d", value); + break; + } + case NdbDictionary::Column::Tinyunsigned: { + unsigned char value= *field->ptr; + fprintf(DBUG_FILE, "Tinyunsigned\t%u", value); + break; + } + case NdbDictionary::Column::Smallint: { + short value= *field->ptr; + fprintf(DBUG_FILE, "Smallint\t%d", value); + break; + } + case NdbDictionary::Column::Smallunsigned: { + unsigned short value= *field->ptr; + fprintf(DBUG_FILE, "Smallunsigned\t%u", value); + break; + } + case NdbDictionary::Column::Mediumint: { + byte value[3]; + memcpy(value, field->ptr, 3); + fprintf(DBUG_FILE, "Mediumint\t%d,%d,%d", value[0], value[1], value[2]); + break; + } + case NdbDictionary::Column::Mediumunsigned: { + byte value[3]; + memcpy(value, field->ptr, 3); + fprintf(DBUG_FILE, "Mediumunsigned\t%u,%u,%u", value[0], value[1], value[2]); + break; + } + case NdbDictionary::Column::Int: { + fprintf(DBUG_FILE, "Int\t%lld", field->val_int()); + break; + } + case NdbDictionary::Column::Unsigned: { + Uint32 value= (Uint32) *field->ptr; + fprintf(DBUG_FILE, "Unsigned\t%u", value); + break; + } + case NdbDictionary::Column::Bigint: { + Int64 value= (Int64) *field->ptr; + fprintf(DBUG_FILE, "Bigint\t%lld", value); + break; + } + case NdbDictionary::Column::Bigunsigned: { + Uint64 value= (Uint64) *field->ptr; + fprintf(DBUG_FILE, "Bigunsigned\t%llu", value); + break; + } + case NdbDictionary::Column::Float: { + float value= (float) *field->ptr; + fprintf(DBUG_FILE, "Float\t%f", value); + break; + } + case NdbDictionary::Column::Double: { + double value= (double) *field->ptr; + fprintf(DBUG_FILE, "Double\t%f", value); + break; + } + case NdbDictionary::Column::Decimal: { + char *value= field->ptr; + + fprintf(DBUG_FILE, "Decimal\t'%-*s'", field->pack_length(), value); + break; + } + case NdbDictionary::Column::Char:{ + char buf[field->pack_length()+1]; + char *value= (char *) field->ptr; + snprintf(buf, field->pack_length(), "%s", value); + fprintf(DBUG_FILE, "Char\t'%s'", buf); + break; + } + case NdbDictionary::Column::Varchar: + case NdbDictionary::Column::Binary: + case NdbDictionary::Column::Varbinary: { + char *value= (char *) field->ptr; + fprintf(DBUG_FILE, "'%s'", value); + break; + } + case NdbDictionary::Column::Datetime: { + Uint64 value= (Uint64) *field->ptr; + fprintf(DBUG_FILE, "Datetime\t%llu", value); + break; + } + case NdbDictionary::Column::Timespec: { + Uint64 value= (Uint64) *field->ptr; + fprintf(DBUG_FILE, "Timespec\t%llu", value); + break; + } + } + fprintf(DBUG_FILE, "\n"); + + } +#endif + DBUG_VOID_RETURN; +} + + +int ha_ndbcluster::index_init(uint index) +{ + DBUG_ENTER("index_init"); + DBUG_PRINT("enter", ("index: %u", index)); + DBUG_RETURN(handler::index_init(index)); +} + + +int ha_ndbcluster::index_end() +{ + DBUG_ENTER("index_end"); + DBUG_RETURN(rnd_end()); +} + + +int ha_ndbcluster::index_read(byte *buf, + const byte *key, uint key_len, + enum ha_rkey_function find_flag) +{ + DBUG_ENTER("index_read"); + DBUG_PRINT("enter", ("active_index: %u, key_len: %u, find_flag: %d", + active_index, key_len, find_flag)); + + int error= 1; + statistic_increment(ha_read_key_count, &LOCK_status); + + switch (get_index_type(active_index)){ + case PRIMARY_KEY_INDEX: + error= pk_read(key, key_len, buf); + break; + + case UNIQUE_INDEX: + error= unique_index_read(key, key_len, buf); + break; + + case ORDERED_INDEX: + error= ordered_index_scan(key, key_len, buf, find_flag); + break; + + default: + case UNDEFINED_INDEX: + break; + } + DBUG_RETURN(error); +} + + +int ha_ndbcluster::index_read_idx(byte *buf, uint index_no, + const byte *key, uint key_len, + enum ha_rkey_function find_flag) +{ + statistic_increment(ha_read_key_count,&LOCK_status); + DBUG_ENTER("index_read_idx"); + DBUG_PRINT("enter", ("index_no: %u, key_len: %u", index_no, key_len)); + index_init(index_no); + DBUG_RETURN(index_read(buf, key, key_len, find_flag)); +} + + +int ha_ndbcluster::index_next(byte *buf) +{ + DBUG_ENTER("index_next"); + + int error = 1; + statistic_increment(ha_read_next_count,&LOCK_status); + if (get_index_type(active_index) == PRIMARY_KEY_INDEX) + error= HA_ERR_END_OF_FILE; + else + error = next_result(buf); + DBUG_RETURN(error); +} + + +int ha_ndbcluster::index_prev(byte *buf) +{ + DBUG_ENTER("index_prev"); + statistic_increment(ha_read_prev_count,&LOCK_status); + DBUG_RETURN(1); +} + + +int ha_ndbcluster::index_first(byte *buf) +{ + DBUG_ENTER("index_first"); + statistic_increment(ha_read_first_count,&LOCK_status); + DBUG_RETURN(1); +} + + +int ha_ndbcluster::index_last(byte *buf) +{ + DBUG_ENTER("index_last"); + statistic_increment(ha_read_last_count,&LOCK_status); + DBUG_RETURN(1); +} + + +int ha_ndbcluster::rnd_init(bool scan) +{ + NdbResultSet *cursor= m_active_cursor; + DBUG_ENTER("rnd_init"); + DBUG_PRINT("enter", ("scan: %d", scan)); + // Check that cursor is not defined + if (cursor) + DBUG_RETURN(1); + index_init(table->primary_key); + DBUG_RETURN(0); +} + + +int ha_ndbcluster::rnd_end() +{ + NdbResultSet *cursor= m_active_cursor; + DBUG_ENTER("rnd_end"); + + if (cursor) + { + DBUG_PRINT("info", ("Closing the cursor")); + cursor->close(); + m_active_cursor= NULL; + } + DBUG_RETURN(0); +} + + +int ha_ndbcluster::rnd_next(byte *buf) +{ + DBUG_ENTER("rnd_next"); + statistic_increment(ha_read_rnd_next_count, &LOCK_status); + int error = 1; + if (!m_active_cursor) + error = full_table_scan(buf); + else + error = next_result(buf); + DBUG_RETURN(error); +} + + +/* + An "interesting" record has been found and it's pk + retrieved by calling position + Now it's time to read the record from db once + again +*/ + +int ha_ndbcluster::rnd_pos(byte *buf, byte *pos) +{ + DBUG_ENTER("rnd_pos"); + statistic_increment(ha_read_rnd_count,&LOCK_status); + // The primary key for the record is stored in pos + // Perform a pk_read using primary key "index" + DBUG_RETURN(pk_read(pos, ref_length, buf)); +} + + +/* + Store the primary key of this record in ref + variable, so that the row can be retrieved again later + using "reference" in rnd_pos +*/ + +void ha_ndbcluster::position(const byte *record) +{ + KEY *key_info; + KEY_PART_INFO *key_part; + KEY_PART_INFO *end; + byte *buff; + DBUG_ENTER("position"); + + if (table->primary_key != MAX_KEY) + { + key_info= table->key_info + table->primary_key; + key_part= key_info->key_part; + end= key_part + key_info->key_parts; + buff= ref; + + for (; key_part != end; key_part++) + { + if (key_part->null_bit) { + /* Store 0 if the key part is a NULL part */ + if (record[key_part->null_offset] + & key_part->null_bit) { + *buff++= 1; + continue; + } + *buff++= 0; + } + memcpy(buff, record + key_part->offset, key_part->length); + buff += key_part->length; + } + } + else + { + // No primary key, get hidden key + DBUG_PRINT("info", ("Getting hidden key")); + int hidden_no= table->fields; + NdbRecAttr* rec= m_value[hidden_no]; + const NDBTAB *tab= (NDBTAB *) m_table; + const NDBCOL *hidden_col= tab->getColumn(hidden_no); + DBUG_ASSERT(hidden_col->getPrimaryKey() && + hidden_col->getAutoIncrement() && + rec != NULL && + ref_length == NDB_HIDDEN_PRIMARY_KEY_LENGTH); + memcpy(ref, (const void*)rec->aRef(), ref_length); + } + + DBUG_DUMP("ref", (char*)ref, ref_length); + DBUG_VOID_RETURN; +} + + +void ha_ndbcluster::info(uint flag) +{ + DBUG_ENTER("info"); + DBUG_PRINT("enter", ("flag: %d", flag)); + + if (flag & HA_STATUS_POS) + DBUG_PRINT("info", ("HA_STATUS_POS")); + if (flag & HA_STATUS_NO_LOCK) + DBUG_PRINT("info", ("HA_STATUS_NO_LOCK")); + if (flag & HA_STATUS_TIME) + DBUG_PRINT("info", ("HA_STATUS_TIME")); + if (flag & HA_STATUS_CONST) + DBUG_PRINT("info", ("HA_STATUS_CONST")); + if (flag & HA_STATUS_VARIABLE) + DBUG_PRINT("info", ("HA_STATUS_VARIABLE")); + if (flag & HA_STATUS_ERRKEY) + DBUG_PRINT("info", ("HA_STATUS_ERRKEY")); + if (flag & HA_STATUS_AUTO) + DBUG_PRINT("info", ("HA_STATUS_AUTO")); + DBUG_VOID_RETURN; +} + + +int ha_ndbcluster::extra(enum ha_extra_function operation) +{ + DBUG_ENTER("extra"); + switch (operation) { + case HA_EXTRA_NORMAL: /* Optimize for space (def) */ + DBUG_PRINT("info", ("HA_EXTRA_NORMAL")); + break; + case HA_EXTRA_QUICK: /* Optimize for speed */ + DBUG_PRINT("info", ("HA_EXTRA_QUICK")); + break; + case HA_EXTRA_RESET: /* Reset database to after open */ + DBUG_PRINT("info", ("HA_EXTRA_RESET")); + break; + case HA_EXTRA_CACHE: /* Cash record in HA_rrnd() */ + DBUG_PRINT("info", ("HA_EXTRA_CACHE")); + break; + case HA_EXTRA_NO_CACHE: /* End cacheing of records (def) */ + DBUG_PRINT("info", ("HA_EXTRA_NO_CACHE")); + break; + case HA_EXTRA_NO_READCHECK: /* No readcheck on update */ + DBUG_PRINT("info", ("HA_EXTRA_NO_READCHECK")); + break; + case HA_EXTRA_READCHECK: /* Use readcheck (def) */ + DBUG_PRINT("info", ("HA_EXTRA_READCHECK")); + break; + case HA_EXTRA_KEYREAD: /* Read only key to database */ + DBUG_PRINT("info", ("HA_EXTRA_KEYREAD")); + break; + case HA_EXTRA_NO_KEYREAD: /* Normal read of records (def) */ + DBUG_PRINT("info", ("HA_EXTRA_NO_KEYREAD")); + break; + case HA_EXTRA_NO_USER_CHANGE: /* No user is allowed to write */ + DBUG_PRINT("info", ("HA_EXTRA_NO_USER_CHANGE")); + break; + case HA_EXTRA_KEY_CACHE: + DBUG_PRINT("info", ("HA_EXTRA_KEY_CACHE")); + break; + case HA_EXTRA_NO_KEY_CACHE: + DBUG_PRINT("info", ("HA_EXTRA_NO_KEY_CACHE")); + break; + case HA_EXTRA_WAIT_LOCK: /* Wait until file is avalably (def) */ + DBUG_PRINT("info", ("HA_EXTRA_WAIT_LOCK")); + break; + case HA_EXTRA_NO_WAIT_LOCK: /* If file is locked, return quickly */ + DBUG_PRINT("info", ("HA_EXTRA_NO_WAIT_LOCK")); + break; + case HA_EXTRA_WRITE_CACHE: /* Use write cache in ha_write() */ + DBUG_PRINT("info", ("HA_EXTRA_WRITE_CACHE")); + break; + case HA_EXTRA_FLUSH_CACHE: /* flush write_record_cache */ + DBUG_PRINT("info", ("HA_EXTRA_FLUSH_CACHE")); + break; + case HA_EXTRA_NO_KEYS: /* Remove all update of keys */ + DBUG_PRINT("info", ("HA_EXTRA_NO_KEYS")); + break; + case HA_EXTRA_KEYREAD_CHANGE_POS: /* Keyread, but change pos */ + DBUG_PRINT("info", ("HA_EXTRA_KEYREAD_CHANGE_POS")); /* xxxxchk -r must be used */ + break; + case HA_EXTRA_REMEMBER_POS: /* Remember pos for next/prev */ + DBUG_PRINT("info", ("HA_EXTRA_REMEMBER_POS")); + break; + case HA_EXTRA_RESTORE_POS: + DBUG_PRINT("info", ("HA_EXTRA_RESTORE_POS")); + break; + case HA_EXTRA_REINIT_CACHE: /* init cache from current record */ + DBUG_PRINT("info", ("HA_EXTRA_REINIT_CACHE")); + break; + case HA_EXTRA_FORCE_REOPEN: /* Datafile have changed on disk */ + DBUG_PRINT("info", ("HA_EXTRA_FORCE_REOPEN")); + break; + case HA_EXTRA_FLUSH: /* Flush tables to disk */ + DBUG_PRINT("info", ("HA_EXTRA_FLUSH")); + break; + case HA_EXTRA_NO_ROWS: /* Don't write rows */ + DBUG_PRINT("info", ("HA_EXTRA_NO_ROWS")); + break; + case HA_EXTRA_RESET_STATE: /* Reset positions */ + DBUG_PRINT("info", ("HA_EXTRA_RESET_STATE")); + break; + case HA_EXTRA_IGNORE_DUP_KEY: /* Dup keys don't rollback everything*/ + DBUG_PRINT("info", ("HA_EXTRA_IGNORE_DUP_KEY")); + + DBUG_PRINT("info", ("Turning ON use of write instead of insert")); + m_use_write= TRUE; + break; + case HA_EXTRA_NO_IGNORE_DUP_KEY: + DBUG_PRINT("info", ("HA_EXTRA_NO_IGNORE_DUP_KEY")); + DBUG_PRINT("info", ("Turning OFF use of write instead of insert")); + m_use_write= false; + break; + case HA_EXTRA_RETRIEVE_ALL_COLS: /* Retrieve all columns, not just those + where field->query_id is the same as + the current query id */ + DBUG_PRINT("info", ("HA_EXTRA_RETRIEVE_ALL_COLS")); + break; + case HA_EXTRA_PREPARE_FOR_DELETE: + DBUG_PRINT("info", ("HA_EXTRA_PREPARE_FOR_DELETE")); + break; + case HA_EXTRA_PREPARE_FOR_UPDATE: /* Remove read cache if problems */ + DBUG_PRINT("info", ("HA_EXTRA_PREPARE_FOR_UPDATE")); + break; + case HA_EXTRA_PRELOAD_BUFFER_SIZE: + DBUG_PRINT("info", ("HA_EXTRA_PRELOAD_BUFFER_SIZE")); + break; + case HA_EXTRA_RETRIEVE_PRIMARY_KEY: + DBUG_PRINT("info", ("HA_EXTRA_RETRIEVE_PRIMARY_KEY")); + break; + case HA_EXTRA_CHANGE_KEY_TO_UNIQUE: + DBUG_PRINT("info", ("HA_EXTRA_CHANGE_KEY_TO_UNIQUE")); + break; + case HA_EXTRA_CHANGE_KEY_TO_DUP: + DBUG_PRINT("info", ("HA_EXTRA_CHANGE_KEY_TO_DUP")); + break; + + } + + DBUG_RETURN(0); +} + + +int ha_ndbcluster::extra_opt(enum ha_extra_function operation, ulong cache_size) +{ + DBUG_ENTER("extra_opt"); + DBUG_PRINT("enter", ("cache_size: %d", cache_size)); + DBUG_RETURN(extra(operation)); +} + + +int ha_ndbcluster::reset() +{ + DBUG_ENTER("reset"); + // Reset what? + DBUG_RETURN(1); +} + + +const char **ha_ndbcluster::bas_ext() const +{ static const char *ext[1] = { NullS }; return ext; } + + +/* + How many seeks it will take to read through the table + This is to be comparable to the number returned by records_in_range so + that we can decide if we should scan the table or use keys. +*/ + +double ha_ndbcluster::scan_time() +{ + return rows2double(records/3); +} + + +THR_LOCK_DATA **ha_ndbcluster::store_lock(THD *thd, + THR_LOCK_DATA **to, + enum thr_lock_type lock_type) +{ + DBUG_ENTER("store_lock"); + + if (lock_type != TL_IGNORE && m_lock.type == TL_UNLOCK) + { + + /* If we are not doing a LOCK TABLE, then allow multiple + writers */ + + if ((lock_type >= TL_WRITE_CONCURRENT_INSERT && + lock_type <= TL_WRITE) && !thd->in_lock_tables) + lock_type= TL_WRITE_ALLOW_WRITE; + + /* In queries of type INSERT INTO t1 SELECT ... FROM t2 ... + MySQL would use the lock TL_READ_NO_INSERT on t2, and that + would conflict with TL_WRITE_ALLOW_WRITE, blocking all inserts + to t2. Convert the lock to a normal read lock to allow + concurrent inserts to t2. */ + + if (lock_type == TL_READ_NO_INSERT && !thd->in_lock_tables) + lock_type= TL_READ; + + m_lock.type=lock_type; + } + *to++= &m_lock; + + DBUG_RETURN(to); +} + +#ifndef DBUG_OFF +#define PRINT_OPTION_FLAGS(t) { \ + if (t->options & OPTION_NOT_AUTOCOMMIT) \ + DBUG_PRINT("thd->options", ("OPTION_NOT_AUTOCOMMIT")); \ + if (t->options & OPTION_BEGIN) \ + DBUG_PRINT("thd->options", ("OPTION_BEGIN")); \ + if (t->options & OPTION_TABLE_LOCK) \ + DBUG_PRINT("thd->options", ("OPTION_TABLE_LOCK")); \ +} +#else +#define PRINT_OPTION_FLAGS(t) +#endif + + +/* + As MySQL will execute an external lock for every new table it uses + we can use this to start the transactions. + If we are in auto_commit mode we just need to start a transaction + for the statement, this will be stored in transaction.stmt. + If not, we have to start a master transaction if there doesn't exist + one from before, this will be stored in transaction.all + + When a table lock is held one transaction will be started which holds + the table lock and for each statement a hupp transaction will be started + */ + +int ha_ndbcluster::external_lock(THD *thd, int lock_type) +{ + int error=0; + NdbConnection* trans= NULL; + + DBUG_ENTER("external_lock"); + DBUG_PRINT("enter", ("transaction.ndb_lock_count: %d", + thd->transaction.ndb_lock_count)); + + /* + Check that this handler instance has a connection + set up to the Ndb object of thd + */ + if (check_ndb_connection()) + DBUG_RETURN(1); + + if (lock_type != F_UNLCK) + { + if (!thd->transaction.ndb_lock_count++) + { + PRINT_OPTION_FLAGS(thd); + + if (!(thd->options & (OPTION_NOT_AUTOCOMMIT | OPTION_BEGIN | OPTION_TABLE_LOCK))) + { + // Autocommit transaction + DBUG_ASSERT(!thd->transaction.stmt.ndb_tid); + DBUG_PRINT("trans",("Starting transaction stmt")); + + trans= m_ndb->startTransaction(); + if (trans == NULL) + { + thd->transaction.ndb_lock_count--; // We didn't get the lock + ERR_RETURN(m_ndb->getNdbError()); + } + thd->transaction.stmt.ndb_tid= trans; + } + else + { + if (!thd->transaction.all.ndb_tid) + { + // Not autocommit transaction + // A "master" transaction ha not been started yet + DBUG_PRINT("trans",("starting transaction, all")); + + trans= m_ndb->startTransaction(); + if (trans == NULL) + { + thd->transaction.ndb_lock_count--; // We didn't get the lock + ERR_RETURN(m_ndb->getNdbError()); + } + + /* + If this is the start of a LOCK TABLE, a table look + should be taken on the table in NDB + + Check if it should be read or write lock + */ + if (thd->options & (OPTION_TABLE_LOCK)) + { + //lockThisTable(); + DBUG_PRINT("info", ("Locking the table..." )); + } + + thd->transaction.all.ndb_tid= trans; + } + } + } + /* + This is the place to make sure this handler instance + has a started transaction. + + The transaction is started by the first handler on which + MySQL Server calls external lock + + Other handlers in the same stmt or transaction should use + the same NDB transaction. This is done by setting up the m_active_trans + pointer to point to the NDB transaction. + */ + + m_active_trans= thd->transaction.all.ndb_tid ? + (NdbConnection*)thd->transaction.all.ndb_tid: + (NdbConnection*)thd->transaction.stmt.ndb_tid; + DBUG_ASSERT(m_active_trans); + + } + else + { + if (!--thd->transaction.ndb_lock_count) + { + DBUG_PRINT("trans", ("Last external_lock")); + PRINT_OPTION_FLAGS(thd); + + if (thd->transaction.stmt.ndb_tid) + { + /* + Unlock is done without a transaction commit / rollback. + This happens if the thread didn't update any rows + We must in this case close the transaction to release resources + */ + DBUG_PRINT("trans",("ending non-updating transaction")); + m_ndb->closeTransaction(m_active_trans); + thd->transaction.stmt.ndb_tid= 0; + } + } + m_active_trans= NULL; + } + DBUG_RETURN(error); +} + +/* + When using LOCK TABLE's external_lock is only called when the actual + TABLE LOCK is done. + Under LOCK TABLES, each used tables will force a call to start_stmt. +*/ + +int ha_ndbcluster::start_stmt(THD *thd) +{ + int error=0; + DBUG_ENTER("start_stmt"); + PRINT_OPTION_FLAGS(thd); + + NdbConnection *trans= (NdbConnection*)thd->transaction.stmt.ndb_tid; + if (!trans){ + DBUG_PRINT("trans",("Starting transaction stmt")); + + NdbConnection *tablock_trans= + (NdbConnection*)thd->transaction.all.ndb_tid; + DBUG_PRINT("info", ("tablock_trans: %x", tablock_trans)); + DBUG_ASSERT(tablock_trans); trans= m_ndb->hupp(tablock_trans); + if (trans == NULL) + ERR_RETURN(m_ndb->getNdbError()); + thd->transaction.stmt.ndb_tid= trans; + } + m_active_trans= trans; + + DBUG_RETURN(error); +} + + +/* + Commit a transaction started in NDB + */ + +int ndbcluster_commit(THD *thd, void *ndb_transaction) +{ + int res= 0; + Ndb *ndb= (Ndb*)thd->transaction.ndb; + NdbConnection *trans= (NdbConnection*)ndb_transaction; + + DBUG_ENTER("ndbcluster_commit"); + DBUG_PRINT("transaction",("%s", + trans == thd->transaction.stmt.ndb_tid ? + "stmt" : "all")); + DBUG_ASSERT(ndb && trans); + + if (trans->execute(Commit) != 0) + { + const NdbError err= trans->getNdbError(); + ERR_PRINT(err); + res= ndb_to_mysql_error(&err); + } + ndb->closeTransaction(trans); + DBUG_RETURN(res); +} + + +/* + Rollback a transaction started in NDB + */ + +int ndbcluster_rollback(THD *thd, void *ndb_transaction) +{ + int res= 0; + Ndb *ndb= (Ndb*)thd->transaction.ndb; + NdbConnection *trans= (NdbConnection*)ndb_transaction; + + DBUG_ENTER("ndbcluster_rollback"); + DBUG_PRINT("transaction",("%s", + trans == thd->transaction.stmt.ndb_tid ? + "stmt" : "all")); + DBUG_ASSERT(ndb && trans); + + if (trans->execute(Rollback) != 0) + { + const NdbError err= trans->getNdbError(); + ERR_PRINT(err); + res= ndb_to_mysql_error(&err); + } + ndb->closeTransaction(trans); + DBUG_RETURN(0); +} + + +/* + Map MySQL type to the corresponding NDB type + */ + +inline NdbDictionary::Column::Type +mysql_to_ndb_type(enum enum_field_types mysql_type, bool unsigned_flg) +{ + switch(mysql_type) { + case MYSQL_TYPE_DECIMAL: + return NdbDictionary::Column::Char; + case MYSQL_TYPE_TINY: + return (unsigned_flg) ? + NdbDictionary::Column::Tinyunsigned : + NdbDictionary::Column::Tinyint; + case MYSQL_TYPE_SHORT: + return (unsigned_flg) ? + NdbDictionary::Column::Smallunsigned : + NdbDictionary::Column::Smallint; + case MYSQL_TYPE_LONG: + return (unsigned_flg) ? + NdbDictionary::Column::Unsigned : + NdbDictionary::Column::Int; + case MYSQL_TYPE_TIMESTAMP: + return NdbDictionary::Column::Unsigned; + case MYSQL_TYPE_LONGLONG: + return (unsigned_flg) ? + NdbDictionary::Column::Bigunsigned : + NdbDictionary::Column::Bigint; + case MYSQL_TYPE_INT24: + return (unsigned_flg) ? + NdbDictionary::Column::Mediumunsigned : + NdbDictionary::Column::Mediumint; + break; + case MYSQL_TYPE_FLOAT: + return NdbDictionary::Column::Float; + case MYSQL_TYPE_DOUBLE: + return NdbDictionary::Column::Double; + case MYSQL_TYPE_DATETIME : + return NdbDictionary::Column::Datetime; + case MYSQL_TYPE_DATE : + case MYSQL_TYPE_NEWDATE : + case MYSQL_TYPE_TIME : + case MYSQL_TYPE_YEAR : + // Missing NDB data types, mapped to char + return NdbDictionary::Column::Char; + case MYSQL_TYPE_ENUM : + return NdbDictionary::Column::Char; + case MYSQL_TYPE_SET : + return NdbDictionary::Column::Char; + case MYSQL_TYPE_TINY_BLOB : + case MYSQL_TYPE_MEDIUM_BLOB : + case MYSQL_TYPE_LONG_BLOB : + case MYSQL_TYPE_BLOB : + return NdbDictionary::Column::Blob; + case MYSQL_TYPE_VAR_STRING : + return NdbDictionary::Column::Varchar; + case MYSQL_TYPE_STRING : + return NdbDictionary::Column::Char; + case MYSQL_TYPE_NULL : + case MYSQL_TYPE_GEOMETRY : + return NdbDictionary::Column::Undefined; + } + return NdbDictionary::Column::Undefined; +} + + +/* + Create a table in NDB Cluster + */ + +int ha_ndbcluster::create(const char *name, + TABLE *form, + HA_CREATE_INFO *info) +{ + NDBTAB tab; + NdbDictionary::Column::Type ndb_type; + NDBCOL col; + uint pack_length, length, i; + int res; + const void *data, *pack_data; + const char **key_name= form->keynames.type_names; + char name2[FN_HEADLEN]; + + DBUG_ENTER("create"); + DBUG_PRINT("enter", ("name: %s", name)); + fn_format(name2, name, "", "",2); // Remove the .frm extension + set_dbname(name2); + set_tabname(name2); + + DBUG_PRINT("table", ("name: %s", m_tabname)); + tab.setName(m_tabname); + tab.setLogging(!(info->options & HA_LEX_CREATE_TMP_TABLE)); + + // Save frm data for this table + if (readfrm(name, &data, &length)) + DBUG_RETURN(1); + if (packfrm(data, length, &pack_data, &pack_length)) + DBUG_RETURN(2); + + DBUG_PRINT("info", ("setFrm data=%x, len=%d", pack_data, pack_length)); + tab.setFrm(pack_data, pack_length); + my_free((char*)data, MYF(0)); + my_free((char*)pack_data, MYF(0)); + + for (i= 0; i < form->fields; i++) + { + Field *field= form->field[i]; + ndb_type= mysql_to_ndb_type(field->real_type(), + field->flags & UNSIGNED_FLAG); + DBUG_PRINT("info", ("name: %s, type: %u, pack_length: %d", + field->field_name, field->real_type(), + field->pack_length())); + col.setName(field->field_name); + col.setType(ndb_type); + if ((ndb_type == NdbDictionary::Column::Char) || + (ndb_type == NdbDictionary::Column::Varchar)) + col.setLength(field->pack_length()); + else + col.setLength(1); + col.setNullable(field->maybe_null()); + col.setPrimaryKey(field->flags & PRI_KEY_FLAG); + if (field->flags & AUTO_INCREMENT_FLAG) + { + DBUG_PRINT("info", ("Found auto_increment key")); + col.setAutoIncrement(TRUE); + ulonglong value = info->auto_increment_value ? + info->auto_increment_value -1 : + (ulonglong) 0; + DBUG_PRINT("info", ("initial value=%ld", value)); +// col.setInitialAutIncValue(value); + } + else + col.setAutoIncrement(false); + + tab.addColumn(col); + } + + // No primary key, create shadow key as 64 bit, auto increment + if (form->primary_key == MAX_KEY) + { + DBUG_PRINT("info", ("Generating shadow key")); + col.setName("$PK"); + col.setType(NdbDictionary::Column::Bigunsigned); + col.setLength(1); + col.setNullable(false); + col.setPrimaryKey(TRUE); + col.setAutoIncrement(TRUE); + tab.addColumn(col); + } + + my_errno= 0; + if (check_ndb_connection()) + { + my_errno= HA_ERR_NO_CONNECTION; + DBUG_RETURN(my_errno); + } + + // Create the table in NDB + NDBDICT *dict= m_ndb->getDictionary(); + if (dict->createTable(tab)) + { + const NdbError err= dict->getNdbError(); + ERR_PRINT(err); + my_errno= ndb_to_mysql_error(&err); + DBUG_RETURN(my_errno); + } + DBUG_PRINT("info", ("Table %s/%s created successfully", + m_dbname, m_tabname)); + + // Fetch table from NDB, check that it exists + const NDBTAB *tab2= dict->getTable(m_tabname); + if (tab2 == NULL) + { + const NdbError err= dict->getNdbError(); + ERR_PRINT(err); + my_errno= ndb_to_mysql_error(&err); + DBUG_RETURN(my_errno); + } + + // Create secondary indexes + for (i= 0; i < form->keys; i++) + { + DBUG_PRINT("info", ("Found index %u: %s", i, key_name[i])); + if (i == form->primary_key) + { + DBUG_PRINT("info", ("Skipping it, PK already created")); + continue; + } + + DBUG_PRINT("info", ("Creating index %u: %s", i, key_name[i])); + res= create_index(key_name[i], + form->key_info + i); + switch(res){ + case 0: + // OK + break; + default: + DBUG_PRINT("error", ("Failed to create index %u", i)); + drop_table(); + my_errno= res; + goto err_end; + } + } + +err_end: + DBUG_RETURN(my_errno); +} + + +/* + Create an index in NDB Cluster + */ + +int ha_ndbcluster::create_index(const char *name, + KEY *key_info){ + NdbDictionary::Dictionary *dict= m_ndb->getDictionary(); + KEY_PART_INFO *key_part= key_info->key_part; + KEY_PART_INFO *end= key_part + key_info->key_parts; + + DBUG_ENTER("create_index"); + DBUG_PRINT("enter", ("name: %s ", name)); + + // Check that an index with the same name do not already exist + if (dict->getIndex(name, m_tabname)) + ERR_RETURN(dict->getNdbError()); + + NdbDictionary::Index ndb_index(name); + if (key_info->flags & HA_NOSAME) + ndb_index.setType(NdbDictionary::Index::UniqueHashIndex); + else + { + ndb_index.setType(NdbDictionary::Index::OrderedIndex); + // TODO Only temporary ordered indexes supported + ndb_index.setLogging(false); + } + ndb_index.setTable(m_tabname); + + for (; key_part != end; key_part++) + { + Field *field= key_part->field; + DBUG_PRINT("info", ("attr: %s", field->field_name)); + ndb_index.addColumnName(field->field_name); + } + + if (dict->createIndex(ndb_index)) + ERR_RETURN(dict->getNdbError()); + + // Success + DBUG_PRINT("info", ("Created index %s", name)); + DBUG_RETURN(0); +} + + +/* + Rename a table in NDB Cluster +*/ + +int ha_ndbcluster::rename_table(const char *from, const char *to) +{ + char new_tabname[FN_HEADLEN]; + + DBUG_ENTER("ha_ndbcluster::rename_table"); + set_dbname(from); + set_tabname(from); + set_tabname(to, new_tabname); + + if (check_ndb_connection()) { + my_errno= HA_ERR_NO_CONNECTION; + DBUG_RETURN(my_errno); + } + + int result= alter_table_name(m_tabname, new_tabname); + if (result == 0) + set_tabname(to); + + DBUG_RETURN(result); +} + + +/* + Rename a table in NDB Cluster using alter table + */ + +int ha_ndbcluster::alter_table_name(const char *from, const char *to) +{ + NDBDICT *dict= m_ndb->getDictionary(); + const NDBTAB *orig_tab; + DBUG_ENTER("alter_table_name_table"); + DBUG_PRINT("enter", ("Renaming %s to %s", from, to)); + + if (!(orig_tab= dict->getTable(from))) + ERR_RETURN(dict->getNdbError()); + + NdbDictionary::Table copy_tab= dict->getTableForAlteration(from); + copy_tab.setName(to); + if (dict->alterTable(copy_tab) != 0) + ERR_RETURN(dict->getNdbError()); + + m_table= NULL; + + DBUG_RETURN(0); +} + + +/* + Delete a table from NDB Cluster + */ + +int ha_ndbcluster::delete_table(const char *name) +{ + DBUG_ENTER("delete_table"); + DBUG_PRINT("enter", ("name: %s", name)); + set_dbname(name); + set_tabname(name); + + if (check_ndb_connection()) + DBUG_RETURN(HA_ERR_NO_CONNECTION); + DBUG_RETURN(drop_table()); +} + + +/* + Drop a table in NDB Cluster + */ + +int ha_ndbcluster::drop_table() +{ + NdbDictionary::Dictionary *dict= m_ndb->getDictionary(); + + DBUG_ENTER("drop_table"); + DBUG_PRINT("enter", ("Deleting %s", m_tabname)); + + if (dict->dropTable(m_tabname)) + { + const NdbError err= dict->getNdbError(); + if (err.code == 709) + ; // 709: No such table existed + else + ERR_RETURN(dict->getNdbError()); + } + release_metadata(); + DBUG_RETURN(0); +} + + +/* + Drop a database in NDB Cluster + */ + +int ndbcluster_drop_database(const char *path) +{ + DBUG_ENTER("ndbcluster_drop_database"); + // TODO drop all tables for this database + DBUG_RETURN(1); +} + + +longlong ha_ndbcluster::get_auto_increment() +{ + // NOTE If number of values to be inserted is known + // the autoincrement cache could be used here + Uint64 auto_value= m_ndb->getAutoIncrementValue(m_tabname); + return (longlong)auto_value; +} + + +/* + Constructor for the NDB Cluster table handler + */ + +ha_ndbcluster::ha_ndbcluster(TABLE *table_arg): + handler(table_arg), + m_active_trans(NULL), + m_active_cursor(NULL), + m_ndb(NULL), + m_table(NULL), + m_table_flags(HA_REC_NOT_IN_SEQ | + HA_KEYPOS_TO_RNDPOS | + HA_NOT_EXACT_COUNT | + HA_NO_WRITE_DELAYED | + HA_NO_PREFIX_CHAR_KEYS | + HA_NO_BLOBS | + HA_DROP_BEFORE_CREATE | + HA_NOT_READ_AFTER_KEY), + m_use_write(false) +{ + + DBUG_ENTER("ha_ndbcluster"); + + m_tabname[0]= '\0'; + m_dbname[0]= '\0'; + + // TODO Adjust number of records and other parameters for proper + // selection of scan/pk access + records= 100; + block_size= 1024; + + DBUG_VOID_RETURN; +} + + +/* + Destructor for NDB Cluster table handler + */ + +ha_ndbcluster::~ha_ndbcluster() +{ + DBUG_ENTER("~ha_ndbcluster"); + + release_metadata(); + + // Check for open cursor/transaction + DBUG_ASSERT(m_active_cursor == NULL); + DBUG_ASSERT(m_active_trans == NULL); + + DBUG_VOID_RETURN; +} + + +/* + Open a table for further use + - fetch metadata for this table from NDB + - check that table exists +*/ + +int ha_ndbcluster::open(const char *name, int mode, uint test_if_locked) +{ + KEY *key; + DBUG_ENTER("open"); + DBUG_PRINT("enter", ("name: %s mode: %d test_if_locked: %d", + name, mode, test_if_locked)); + + // Setup ref_length to make room for the whole + // primary key to be written in the ref variable + + if (table->primary_key != MAX_KEY) + { + key= table->key_info+table->primary_key; + ref_length= key->key_length; + DBUG_PRINT("info", (" ref_length: %d", ref_length)); + } + // Init table lock structure + if (!(m_share=get_share(name))) + DBUG_RETURN(1); + thr_lock_data_init(&m_share->lock,&m_lock,(void*) 0); + + set_dbname(name); + set_tabname(name); + + if (check_ndb_connection()) + DBUG_RETURN(HA_ERR_NO_CONNECTION); + + DBUG_RETURN(get_metadata(name)); +} + + +/* + Close the table + - release resources setup by open() + */ + +int ha_ndbcluster::close(void) +{ + DBUG_ENTER("close"); + free_share(m_share); + release_metadata(); + m_ndb= NULL; + DBUG_RETURN(0); +} + + +Ndb* ha_ndbcluster::seize_ndb() +{ + Ndb* ndb; + DBUG_ENTER("seize_ndb"); + +#ifdef USE_NDB_POOL + // Seize from pool + ndb= Ndb::seize(); +#else + ndb= new Ndb(""); +#endif + if (ndb->init(NDB_MAX_TRANSACTIONS) != 0) + { + ERR_PRINT(ndb->getNdbError()); + /* + TODO + Alt.1 If init fails because to many allocated Ndb + wait on condition for a Ndb object to be released. + Alt.2 Seize/release from pool, wait until next release + */ + delete ndb; + ndb= NULL; + } + DBUG_RETURN(ndb); +} + + +void ha_ndbcluster::release_ndb(Ndb* ndb) +{ + DBUG_ENTER("release_ndb"); +#ifdef USE_NDB_POOL + // Release to pool + Ndb::release(ndb); +#else + delete ndb; +#endif + DBUG_VOID_RETURN; +} + + +/* + If this thread already has a Ndb object allocated + in current THD, reuse it. Otherwise + seize a Ndb object, assign it to current THD and use it. + + Having a Ndb object also means that a connection to + NDB cluster has been opened. The connection is + checked. + +*/ + +int ha_ndbcluster::check_ndb_connection() +{ + THD* thd= current_thd; + Ndb* ndb; + DBUG_ENTER("check_ndb_connection"); + + if (!thd->transaction.ndb) + { + ndb= seize_ndb(); + if (!ndb) + DBUG_RETURN(2); + thd->transaction.ndb= ndb; + } + m_ndb= (Ndb*)thd->transaction.ndb; + m_ndb->setDatabaseName(m_dbname); + if (m_ndb->waitUntilReady() != 0) + { + DBUG_PRINT("error", ("Ndb was not ready")); + DBUG_RETURN(3); + } + DBUG_RETURN(0); +} + +void ndbcluster_close_connection(THD *thd) +{ + Ndb* ndb; + DBUG_ENTER("ndbcluster_close_connection"); + ndb= (Ndb*)thd->transaction.ndb; + ha_ndbcluster::release_ndb(ndb); + thd->transaction.ndb= NULL; + DBUG_VOID_RETURN; +} + + +/* + Try to discover one table from NDB + */ + +int ndbcluster_discover(const char *dbname, const char *name, + const void** frmblob, uint* frmlen) +{ + uint len; + const void* data; + const NDBTAB* tab; + DBUG_ENTER("ndbcluster_discover"); + DBUG_PRINT("enter", ("db: %s, name: %s", dbname, name)); + + Ndb ndb(dbname); + if ((ndb.init() != 0) && (ndb.waitUntilReady() != 0)) + ERR_RETURN(ndb.getNdbError()); + + if (!(tab= ndb.getDictionary()->getTable(name))) + { + DBUG_PRINT("info", ("Table %s not found", name)); + DBUG_RETURN(1); + } + + DBUG_PRINT("info", ("Found table %s", tab->getName())); + + len= tab->getFrmLength(); + if (len == 0 || tab->getFrmData() == NULL) + { + DBUG_PRINT("No frm data found", + ("Table is probably created via NdbApi")); + DBUG_RETURN(2); + } + + if (unpackfrm(&data, &len, tab->getFrmData())) + DBUG_RETURN(3); + + *frmlen= len; + *frmblob= data; + + DBUG_RETURN(0); +} + +static Ndb* g_ndb= NULL; + +#ifdef USE_DISCOVER_ON_STARTUP +/* + Dicover tables from NDB Cluster + - fetch a list of tables from NDB + - store the frm file for each table on disk + - if the table has an attached frm file + - if the database of the table exists +*/ + +int ndb_discover_tables() +{ + uint i; + NdbDictionary::Dictionary::List list; + NdbDictionary::Dictionary* dict; + char path[FN_REFLEN]; + DBUG_ENTER("ndb_discover_tables"); + + /* List tables in NDB Cluster kernel */ + dict= g_ndb->getDictionary(); + if (dict->listObjects(list, + NdbDictionary::Object::UserTable) != 0) + ERR_RETURN(g_ndb->getNdbError()); + + for (i= 0 ; i < list.count ; i++) + { + NdbDictionary::Dictionary::List::Element& t= list.elements[i]; + + DBUG_PRINT("discover", ("%d: %s/%s", t.id, t.database, t.name)); + if (create_table_from_handler(t.database, t.name, true)) + DBUG_PRINT("info", ("Could not discover %s/%s", t.database, t.name)); + } + DBUG_RETURN(0); +} +#endif + + +/* + Initialise all gloal variables before creating + a NDB Cluster table handler + */ + +bool ndbcluster_init() +{ + DBUG_ENTER("ndbcluster_init"); + // Create a Ndb object to open the connection to NDB + g_ndb= new Ndb("sys"); + if (g_ndb->init() != 0) + { + ERR_PRINT (g_ndb->getNdbError()); + DBUG_RETURN(TRUE); + } + if (g_ndb->waitUntilReady() != 0) + { + ERR_PRINT (g_ndb->getNdbError()); + DBUG_RETURN(TRUE); + } + (void) hash_init(&ndbcluster_open_tables,system_charset_info,32,0,0, + (hash_get_key) ndbcluster_get_key,0,0); + pthread_mutex_init(&ndbcluster_mutex,MY_MUTEX_INIT_FAST); + ndbcluster_inited= 1; +#ifdef USE_DISCOVER_ON_STARTUP + if (ndb_discover_tables() != 0) + DBUG_RETURN(TRUE); +#endif + DBUG_RETURN(false); +} + + +/* + End use of the NDB Cluster table handler + - free all global variables allocated by + ndcluster_init() +*/ + +bool ndbcluster_end() +{ + DBUG_ENTER("ndbcluster_end"); + delete g_ndb; + g_ndb= NULL; + if (!ndbcluster_inited) + DBUG_RETURN(0); + hash_free(&ndbcluster_open_tables); +#ifdef USE_NDB_POOL + ndb_pool_release(); +#endif + pthread_mutex_destroy(&ndbcluster_mutex); + ndbcluster_inited= 0; + DBUG_RETURN(0); +} + + +/* + Set m_tabname from full pathname to table file + */ + +void ha_ndbcluster::set_tabname(const char *path_name) +{ + char *end, *ptr; + + /* Scan name from the end */ + end= strend(path_name)-1; + ptr= end; + while (ptr >= path_name && *ptr != '\\' && *ptr != '/') { + ptr--; + } + uint name_len= end - ptr; + memcpy(m_tabname, ptr + 1, end - ptr); + m_tabname[name_len]= '\0'; +#ifdef __WIN__ + /* Put to lower case */ + ptr= m_tabname; + + while (*ptr != '\0') { + *ptr = tolower(*ptr); + ptr++; + } +#endif +} + +/** + * Set a given location from full pathname to table file + * + */ +void +ha_ndbcluster::set_tabname(const char *path_name, char * tabname) +{ + char *end, *ptr; + + /* Scan name from the end */ + end = strend(path_name)-1; + ptr = end; + while (ptr >= path_name && *ptr != '\\' && *ptr != '/') { + ptr--; + } + uint name_len = end - ptr; + memcpy(tabname, ptr + 1, end - ptr); + tabname[name_len] = '\0'; +#ifdef __WIN__ + /* Put to lower case */ + ptr = tabname; + + while (*ptr != '\0') { + *ptr= tolower(*ptr); + ptr++; + } +#endif +} + + +/* + Set m_dbname from full pathname to table file + + */ + +void ha_ndbcluster::set_dbname(const char *path_name) +{ + char *end, *ptr; + + /* Scan name from the end */ + ptr= strend(path_name)-1; + while (ptr >= path_name && *ptr != '\\' && *ptr != '/') { + ptr--; + } + ptr--; + end= ptr; + while (ptr >= path_name && *ptr != '\\' && *ptr != '/') { + ptr--; + } + uint name_len= end - ptr; + memcpy(m_dbname, ptr + 1, name_len); + m_dbname[name_len]= '\0'; +#ifdef __WIN__ + /* Put to lower case */ + + ptr= m_dbname; + + while (*ptr != '\0') { + *ptr= tolower(*ptr); + ptr++; + } +#endif +} + + +ha_rows +ha_ndbcluster::records_in_range(int inx, + const byte *start_key,uint start_key_len, + enum ha_rkey_function start_search_flag, + const byte *end_key,uint end_key_len, + enum ha_rkey_function end_search_flag) +{ + ha_rows records= 10; + KEY* key_info= table->key_info + inx; + uint key_length= key_info->key_length; + + DBUG_ENTER("records_in_range"); + DBUG_PRINT("enter", ("inx: %d", inx)); + DBUG_PRINT("enter", ("start_key: %x, start_key_len: %d", start_key, start_key_len)); + DBUG_PRINT("enter", ("start_search_flag: %d", start_search_flag)); + DBUG_PRINT("enter", ("end_key: %x, end_key_len: %d", end_key, end_key_len)); + DBUG_PRINT("enter", ("end_search_flag: %d", end_search_flag)); + + /* + Check that start_key_len is equal to + the length of the used index and + prevent partial scan/read of hash indexes by returning HA_POS_ERROR + */ + NDB_INDEX_TYPE idx_type= get_index_type(inx); + if ((idx_type == UNIQUE_INDEX || idx_type == PRIMARY_KEY_INDEX) && + start_key_len < key_length) + { + DBUG_PRINT("warning", ("Tried to use index which required" + "full key length: %d, HA_POS_ERROR", + key_length)); + records= HA_POS_ERROR; + } + DBUG_RETURN(records); +} + + +/* + Handling the shared NDB_SHARE structure that is needed to + provide table locking. + It's also used for sharing data with other NDB handlers + in the same MySQL Server. There is currently not much + data we want to or can share. + */ + +static byte* ndbcluster_get_key(NDB_SHARE *share,uint *length, + my_bool not_used __attribute__((unused))) +{ + *length=share->table_name_length; + return (byte*) share->table_name; +} + +static NDB_SHARE* get_share(const char *table_name) +{ + NDB_SHARE *share; + pthread_mutex_lock(&ndbcluster_mutex); + uint length=(uint) strlen(table_name); + if (!(share=(NDB_SHARE*) hash_search(&ndbcluster_open_tables, + (byte*) table_name, + length))) + { + if ((share=(NDB_SHARE *) my_malloc(sizeof(*share)+length+1, + MYF(MY_WME | MY_ZEROFILL)))) + { + share->table_name_length=length; + share->table_name=(char*) (share+1); + strmov(share->table_name,table_name); + if (my_hash_insert(&ndbcluster_open_tables, (byte*) share)) + { + pthread_mutex_unlock(&ndbcluster_mutex); + my_free((gptr) share,0); + return 0; + } + thr_lock_init(&share->lock); + pthread_mutex_init(&share->mutex,MY_MUTEX_INIT_FAST); + } + } + share->use_count++; + pthread_mutex_unlock(&ndbcluster_mutex); + return share; +} + + +static void free_share(NDB_SHARE *share) +{ + pthread_mutex_lock(&ndbcluster_mutex); + if (!--share->use_count) + { + hash_delete(&ndbcluster_open_tables, (byte*) share); + thr_lock_delete(&share->lock); + pthread_mutex_destroy(&share->mutex); + my_free((gptr) share, MYF(0)); + } + pthread_mutex_unlock(&ndbcluster_mutex); +} + + + +/* + Internal representation of the frm blob + +*/ + +struct frm_blob_struct +{ + struct frm_blob_header + { + uint ver; // Version of header + uint orglen; // Original length of compressed data + uint complen; // Compressed length of data, 0=uncompressed + } head; + char data[1]; +}; + + + +static int packfrm(const void *data, uint len, + const void **pack_data, uint *pack_len) +{ + int error; + ulong org_len, comp_len; + uint blob_len; + frm_blob_struct* blob; + DBUG_ENTER("packfrm"); + DBUG_PRINT("enter", ("data: %x, len: %d", data, len)); + + error= 1; + org_len = len; + if (my_compress((byte*)data, &org_len, &comp_len)) + goto err; + + DBUG_PRINT("info", ("org_len: %d, comp_len: %d", org_len, comp_len)); + DBUG_DUMP("compressed", (char*)data, org_len); + + error= 2; + blob_len= sizeof(frm_blob_struct::frm_blob_header)+org_len; + if (!(blob= (frm_blob_struct*) my_malloc(blob_len,MYF(MY_WME)))) + goto err; + + // Store compressed blob in machine independent format + int4store((char*)(&blob->head.ver), 1); + int4store((char*)(&blob->head.orglen), comp_len); + int4store((char*)(&blob->head.complen), org_len); + + // Copy frm data into blob, already in machine independent format + memcpy(blob->data, data, org_len); + + *pack_data = blob; + *pack_len = blob_len; + error = 0; + + DBUG_PRINT("exit", ("pack_data: %x, pack_len: %d", *pack_data, *pack_len)); +err: + DBUG_RETURN(error); + +} + + +static int unpackfrm(const void **unpack_data, uint *unpack_len, + const void *pack_data) +{ + const frm_blob_struct *blob = (frm_blob_struct*)pack_data; + byte *data; + ulong complen, orglen, ver; + DBUG_ENTER("unpackfrm"); + DBUG_PRINT("enter", ("pack_data: %x", pack_data)); + + complen= uint4korr((char*)&blob->head.complen); + orglen= uint4korr((char*)&blob->head.orglen); + ver= uint4korr((char*)&blob->head.ver); + + DBUG_PRINT("blob",("ver: %d complen: %d orglen: %d", + ver,complen,orglen)); + DBUG_DUMP("blob->data", (char*) blob->data, complen); + + if (ver != 1) + DBUG_RETURN(1); + if (!(data = my_malloc(max(orglen, complen), MYF(MY_WME)))) + DBUG_RETURN(2); + memcpy(data, blob->data, complen); + + if (my_uncompress(data, &complen, &orglen)) + { + my_free((char*)data, MYF(0)); + DBUG_RETURN(3); + } + + *unpack_data = data; + *unpack_len = complen; + + DBUG_PRINT("exit", ("frmdata: %x, len: %d", *unpack_data, *unpack_len)); + + DBUG_RETURN(0); +} +#endif /* HAVE_NDBCLUSTER_DB */ diff --git a/sql/ha_ndbcluster.h b/sql/ha_ndbcluster.h new file mode 100644 index 00000000000..bd601f39fc4 --- /dev/null +++ b/sql/ha_ndbcluster.h @@ -0,0 +1,218 @@ +/* Copyright (C) 2000-2003 MySQL AB + + 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; either version 2 of the License, or + (at your option) any later version. + + 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ + +/* + This file defines the NDB Cluster handler: the interface between MySQL and + NDB Cluster +*/ + +/* The class defining a handle to an NDB Cluster table */ + +#ifdef __GNUC__ +#pragma interface /* gcc class implementation */ +#endif + +#include <ndbapi_limits.h> +#include <ndb_types.h> + +class Ndb; // Forward declaration +class NdbOperation; // Forward declaration +class NdbConnection; // Forward declaration +class NdbRecAttr; // Forward declaration +class NdbResultSet; // Forward declaration + +typedef enum ndb_index_type { + UNDEFINED_INDEX = 0, + PRIMARY_KEY_INDEX = 1, + UNIQUE_INDEX = 2, + ORDERED_INDEX = 3 +} NDB_INDEX_TYPE; + + +typedef struct st_ndbcluster_share { + THR_LOCK lock; + pthread_mutex_t mutex; + char *table_name; + uint table_name_length,use_count; +} NDB_SHARE; + +class ha_ndbcluster: public handler +{ + public: + ha_ndbcluster(TABLE *table); + ~ha_ndbcluster(); + + int open(const char *name, int mode, uint test_if_locked); + int close(void); + + int write_row(byte *buf); + int update_row(const byte *old_data, byte *new_data); + int delete_row(const byte *buf); + int index_init(uint index); + int index_end(); + int index_read(byte *buf, const byte *key, uint key_len, + enum ha_rkey_function find_flag); + int index_read_idx(byte *buf, uint index, const byte *key, uint key_len, + enum ha_rkey_function find_flag); + int index_next(byte *buf); + int index_prev(byte *buf); + int index_first(byte *buf); + int index_last(byte *buf); + int rnd_init(bool scan=1); + int rnd_end(); + int rnd_next(byte *buf); + int rnd_pos(byte *buf, byte *pos); + void position(const byte *record); + + void info(uint); + int extra(enum ha_extra_function operation); + int extra_opt(enum ha_extra_function operation, ulong cache_size); + int reset(); + int external_lock(THD *thd, int lock_type); + int start_stmt(THD *thd); + const char * table_type() const { return("ndbcluster");} + const char ** bas_ext() const; + ulong table_flags(void) const { return m_table_flags; } + ulong index_flags(uint idx) const; + uint max_record_length() const { return NDB_MAX_TUPLE_SIZE; }; + uint max_keys() const { return MAX_KEY; } + uint max_key_parts() const { return NDB_MAX_NO_OF_ATTRIBUTES_IN_KEY; }; + uint max_key_length() const { return NDB_MAX_KEY_SIZE;}; + + int rename_table(const char *from, const char *to); + int delete_table(const char *name); + int create(const char *name, TABLE *form, HA_CREATE_INFO *info); + THR_LOCK_DATA **store_lock(THD *thd, + THR_LOCK_DATA **to, + enum thr_lock_type lock_type); + + bool low_byte_first() const + { +#ifdef WORDS_BIGENDIAN + return false; +#else + return true; +#endif + } + bool has_transactions() { return true; } + + const char* index_type(uint key_number) { + switch (get_index_type(key_number)) { + case ORDERED_INDEX: + return "BTREE"; + case UNIQUE_INDEX: + case PRIMARY_KEY_INDEX: + default: + return "HASH"; + } + } + + double scan_time(); + ha_rows records_in_range(int inx, + const byte *start_key,uint start_key_len, + enum ha_rkey_function start_search_flag, + const byte *end_key,uint end_key_len, + enum ha_rkey_function end_search_flag); + + + static Ndb* seize_ndb(); + static void release_ndb(Ndb* ndb); + uint8 table_cache_type() { return HA_CACHE_TBL_NOCACHE; } + + private: + int alter_table_name(const char *from, const char *to); + int drop_table(); + int create_index(const char *name, KEY *key_info); + int initialize_autoincrement(const void* table); + int get_metadata(const char* path); + void release_metadata(); + const char* get_index_name(uint idx_no) const; + NDB_INDEX_TYPE get_index_type(uint idx_no) const; + NDB_INDEX_TYPE get_index_type_from_table(uint index_no) const; + + int pk_read(const byte *key, uint key_len, + byte *buf); + int unique_index_read(const byte *key, uint key_len, + byte *buf); + int ordered_index_scan(const byte *key, uint key_len, + byte *buf, + enum ha_rkey_function find_flag); + int full_table_scan(byte * buf); + int next_result(byte *buf); +#if 0 + int filtered_scan(const byte *key, uint key_len, + byte *buf, + enum ha_rkey_function find_flag); +#endif + + void unpack_record(byte *buf); + + void set_dbname(const char *pathname); + void set_tabname(const char *pathname); + void set_tabname(const char *pathname, char *tabname); + + bool set_hidden_key(NdbOperation*, + uint fieldnr, const byte* field_ptr); + int set_ndb_key(NdbOperation*, Field *field, + uint fieldnr, const byte* field_ptr); + int set_ndb_value(NdbOperation*, Field *field, uint fieldnr); + int get_ndb_value(NdbOperation*, uint fieldnr, byte *field_ptr); + int set_primary_key(NdbOperation *op, const byte *key); + int set_primary_key(NdbOperation *op); + int key_cmp(uint keynr, const byte * old_row, const byte * new_row); + void print_results(); + + longlong get_auto_increment(); + + int ndb_err(NdbConnection*); + + private: + int check_ndb_connection(); + + NdbConnection *m_active_trans; + NdbResultSet *m_active_cursor; + Ndb *m_ndb; + void *m_table; + char m_dbname[FN_HEADLEN]; + //char m_schemaname[FN_HEADLEN]; + char m_tabname[FN_HEADLEN]; + ulong m_table_flags; + THR_LOCK_DATA m_lock; + NDB_SHARE *m_share; + NDB_INDEX_TYPE m_indextype[MAX_KEY]; + NdbRecAttr *m_value[NDB_MAX_ATTRIBUTES_IN_TABLE]; + bool m_use_write; +}; + +bool ndbcluster_init(void); +bool ndbcluster_end(void); + +int ndbcluster_commit(THD *thd, void* ndb_transaction); +int ndbcluster_rollback(THD *thd, void* ndb_transaction); + +void ndbcluster_close_connection(THD *thd); + +int ndbcluster_discover(const char* dbname, const char* name, + const void** frmblob, uint* frmlen); +int ndbcluster_drop_database(const char* path); + + + + + + + + diff --git a/sql/handler.cc b/sql/handler.cc index f9be90c9379..944d3421d75 100644 --- a/sql/handler.cc +++ b/sql/handler.cc @@ -32,11 +32,17 @@ #ifdef HAVE_BERKELEY_DB #include "ha_berkeley.h" #endif +#ifdef HAVE_EXAMPLE_DB +#include "examples/ha_example.h" +#endif #ifdef HAVE_INNOBASE_DB #include "ha_innodb.h" #else #define innobase_query_caching_of_table_permitted(X,Y,Z) 1 #endif +#ifdef HAVE_NDBCLUSTER_DB +#include "ha_ndbcluster.h" +#endif #include <myisampack.h> #include <errno.h> @@ -48,7 +54,7 @@ ulong ha_read_count, ha_write_count, ha_delete_count, ha_update_count, ha_read_key_count, ha_read_next_count, ha_read_prev_count, ha_read_first_count, ha_read_last_count, ha_commit_count, ha_rollback_count, - ha_read_rnd_count, ha_read_rnd_next_count; + ha_read_rnd_count, ha_read_rnd_next_count, ha_discover_count; static SHOW_COMP_OPTION have_yes= SHOW_OPTION_YES; @@ -76,6 +82,12 @@ struct show_table_type_st sys_table_types[]= "Supports transactions and page-level locking", DB_TYPE_BERKELEY_DB}, {"BERKELEYDB",&have_berkeley_db, "Alias for BDB", DB_TYPE_BERKELEY_DB}, + {"NDBCLUSTER", &have_ndbcluster, + "Clustered, fault tolerant memory based tables", DB_TYPE_NDBCLUSTER}, + {"NDB", &have_ndbcluster, + "Alias for NDBCLUSTER", DB_TYPE_NDBCLUSTER}, + {"EXAMPLE",&have_example_db, + "Example storage engine", DB_TYPE_EXAMPLE_DB}, {NullS, NULL, NullS, DB_TYPE_UNKNOWN} }; @@ -172,6 +184,14 @@ handler *get_new_handler(TABLE *table, enum db_type db_type) case DB_TYPE_INNODB: return new ha_innobase(table); #endif +#ifdef HAVE_EXAMPLE_DB + case DB_TYPE_EXAMPLE_DB: + return new ha_example(table); +#endif +#ifdef HAVE_NDBCLUSTER_DB + case DB_TYPE_NDBCLUSTER: + return new ha_ndbcluster(table); +#endif case DB_TYPE_HEAP: return new ha_heap(table); default: // should never happen @@ -216,6 +236,18 @@ int ha_init() opt_using_transactions=1; } #endif +#ifdef HAVE_NDBCLUSTER_DB + if (have_ndbcluster == SHOW_OPTION_YES) + { + if (ndbcluster_init()) + { + have_ndbcluster= SHOW_OPTION_DISABLED; + error= 1; + } + else + opt_using_transactions=1; + } +#endif return error; } @@ -243,6 +275,10 @@ int ha_panic(enum ha_panic_function flag) if (have_innodb == SHOW_OPTION_YES) error|=innobase_end(); #endif +#ifdef HAVE_NDBCLUSTER_DB + if (have_ndbcluster == SHOW_OPTION_YES) + error|=ndbcluster_end(); +#endif return error; } /* ha_panic */ @@ -252,6 +288,10 @@ void ha_drop_database(char* path) if (have_innodb == SHOW_OPTION_YES) innobase_drop_database(path); #endif +#ifdef HAVE_NDBCLUSTER_DB + if (have_ndbcluster == SHOW_OPTION_YES) + ndbcluster_drop_database(path); +#endif } void ha_close_connection(THD* thd) @@ -260,6 +300,10 @@ void ha_close_connection(THD* thd) if (have_innodb == SHOW_OPTION_YES) innobase_close_connection(thd); #endif +#ifdef HAVE_NDBCLUSTER_DB + if (have_ndbcluster == SHOW_OPTION_YES) + ndbcluster_close_connection(thd); +#endif } /* @@ -419,6 +463,19 @@ int ha_commit_trans(THD *thd, THD_TRANS* trans) WRITE_CACHE, (my_off_t) 0, 0, 1); thd->transaction.trans_log.end_of_file= max_binlog_cache_size; } +#ifdef HAVE_NDBCLUSTER_DB + if (trans->ndb_tid) + { + if ((error=ndbcluster_commit(thd,trans->ndb_tid))) + { + my_error(ER_ERROR_DURING_COMMIT, MYF(0), error); + error=1; + } + if (trans == &thd->transaction.all) + operation_done= transaction_commited= 1; + trans->ndb_tid=0; + } +#endif #ifdef HAVE_BERKELEY_DB if (trans->bdb_tid) { @@ -472,6 +529,18 @@ int ha_rollback_trans(THD *thd, THD_TRANS *trans) if (opt_using_transactions) { bool operation_done=0; +#ifdef HAVE_NDBCLUSTER_DB + if (trans->ndb_tid) + { + if ((error=ndbcluster_rollback(thd, trans->ndb_tid))) + { + my_error(ER_ERROR_DURING_ROLLBACK, MYF(0), error); + error=1; + } + trans->ndb_tid = 0; + operation_done=1; + } +#endif #ifdef HAVE_BERKELEY_DB if (trans->bdb_tid) { @@ -1153,8 +1222,10 @@ bool handler::caching_allowed(THD* thd, char* table_key, ** Some general functions that isn't in the handler class ****************************************************************************/ - /* Initiates table-file and calls apropriate database-creator */ - /* Returns 1 if something got wrong */ +/* + Initiates table-file and calls apropriate database-creator + Returns 1 if something got wrong +*/ int ha_create_table(const char *name, HA_CREATE_INFO *create_info, bool update_create_info) @@ -1170,7 +1241,7 @@ int ha_create_table(const char *name, HA_CREATE_INFO *create_info, { update_create_info_from_table(create_info, &table); if (table.file->table_flags() & HA_DROP_BEFORE_CREATE) - table.file->delete_table(name); // Needed for BDB tables + table.file->delete_table(name); } if (lower_case_table_names == 2 && !(table.file->table_flags() & HA_FILE_BASED)) @@ -1289,3 +1360,163 @@ int ha_change_key_cache(KEY_CACHE *old_key_cache, mi_change_key_cache(old_key_cache, new_key_cache); return 0; } + + +/* + Try to discover one table from handler(s) +*/ + +int ha_discover(const char* dbname, const char* name, + const void** frmblob, uint* frmlen) +{ + int error= 1; // Table does not exist in any handler + DBUG_ENTER("ha_discover"); + DBUG_PRINT("enter", ("db: %s, name: %s", dbname, name)); +#ifdef HAVE_NDBCLUSTER_DB + if (have_ndbcluster == SHOW_OPTION_YES) + error= ndbcluster_discover(dbname, name, frmblob, frmlen); +#endif + if (!error) + statistic_increment(ha_discover_count,&LOCK_status); + DBUG_RETURN(error); +} + + +/* + Read first row between two ranges. + Store ranges for future calls to read_range_next + + SYNOPSIS + read_range_first() + start_key Start key. Is 0 if no min range + end_key End key. Is 0 if no max range + sorted Set to 1 if result should be sorted per key + + NOTES + Record is read into table->record[0] + + RETURN + 0 Found row + HA_ERR_END_OF_FILE No rows in range + # Error code +*/ + +int handler::read_range_first(const key_range *start_key, + const key_range *end_key, + bool sorted) +{ + int result; + DBUG_ENTER("handler::read_range_first"); + + end_range= 0; + if (end_key) + { + end_range= &save_end_range; + save_end_range= *end_key; + key_compare_result_on_equal= ((end_key->flag == HA_READ_BEFORE_KEY) ? 1 : + (end_key->flag == HA_READ_AFTER_KEY) ? -1 : 0); + } + range_key_part= table->key_info[active_index].key_part; + + + if (!start_key) // Read first record + result= index_first(table->record[0]); + else + result= index_read(table->record[0], + start_key->key, + start_key->length, + start_key->flag); + if (result) + DBUG_RETURN((result == HA_ERR_KEY_NOT_FOUND || + result == HA_ERR_END_OF_FILE) ? HA_ERR_END_OF_FILE : + result); + + DBUG_RETURN (compare_key(end_range) <= 0 ? 0 : HA_ERR_END_OF_FILE); +} + + +/* + Read next row between two ranges. + + SYNOPSIS + read_range_next() + eq_range Set to 1 if start_key == end_key + + NOTES + Record is read into table->record[0] + + RETURN + 0 Found row + HA_ERR_END_OF_FILE No rows in range + # Error code +*/ + +int handler::read_range_next(bool eq_range) +{ + int result; + DBUG_ENTER("handler::read_range_next"); + + if (eq_range) + result= index_next_same(table->record[0], + end_range->key, + end_range->length); + else + result= index_next(table->record[0]); + if (result) + DBUG_RETURN(result); + DBUG_RETURN(compare_key(end_range) <= 0 ? 0 : HA_ERR_END_OF_FILE); +} + + +/* + Compare if found key is over max-value + + SYNOPSIS + compare_key + range key to compare to row + + NOTES + For this to work, the row must be stored in table->record[0] + + RETURN + 0 Key is equal to range or 'range' == 0 (no range) + -1 Key is less than range + 1 Key is larger than range +*/ + +int handler::compare_key(key_range *range) +{ + KEY_PART_INFO *key_part= range_key_part; + uint store_length; + + if (!range) + return 0; // No max range + + for (const char *key= (const char*) range->key, *end=key+range->length; + key < end; + key+= store_length, key_part++) + { + int cmp; + store_length= key_part->store_length; + if (key_part->null_bit) + { + if (*key) + { + if (!key_part->field->is_null()) + return 1; + continue; + } + else if (key_part->field->is_null()) + return 0; + key++; // Skip null byte + store_length--; + } + if ((cmp=key_part->field->key_cmp((byte*) key, key_part->length)) < 0) + return -1; + if (cmp > 0) + return 1; + } + return key_compare_result_on_equal; +} + + diff --git a/sql/handler.h b/sql/handler.h index 182f84e523f..15fc6a201d8 100644 --- a/sql/handler.h +++ b/sql/handler.h @@ -28,7 +28,8 @@ #define NO_HASH /* Not yet implemented */ #endif -#if defined(HAVE_BERKELEY_DB) || defined(HAVE_INNOBASE_DB) +#if defined(HAVE_BERKELEY_DB) || defined(HAVE_INNOBASE_DB) || \ + defined(HAVE_NDBCLUSTER_DB) #define USING_TRANSACTIONS #endif @@ -51,7 +52,7 @@ #define HA_REC_NOT_IN_SEQ 8 /* ha_info don't return recnumber; It returns a position to ha_r_rnd */ #define HA_HAS_GEOMETRY (1 << 4) -#define HA_NO_INDEX (1 << 5) /* No index needed for next/prev */ +#define HA_FAST_KEY_READ (1 << 5) /* no need for a record cache in filesort */ #define HA_KEY_READ_WRONG_STR (1 << 6) /* keyread returns converted strings */ #define HA_NULL_KEY (1 << 7) /* One can have keys with NULL */ #define HA_DUPP_POS (1 << 8) /* ha_position() gives dupp row */ @@ -80,7 +81,6 @@ #define HA_FILE_BASED (1 << 26) - /* bits in index_flags(index_number) for what you can do with index */ #define HA_WRONG_ASCII_ORDER 1 /* Can't use sorting through key */ #define HA_READ_NEXT 2 /* Read next record with same key */ @@ -90,6 +90,20 @@ #define HA_NOT_READ_PREFIX_LAST 32 /* No support for index_read_last() */ #define HA_KEY_READ_ONLY 64 /* Support HA_EXTRA_KEYREAD */ + +/* + Bits in index_ddl_flags(KEY *wanted_index) + for what ddl you can do with index + If none is set, the wanted type of index is not supported + by the handler at all. See WorkLog 1563. +*/ +#define HA_DDL_SUPPORT 1 /* Supported by handler */ +#define HA_DDL_WITH_LOCK 2 /* Can create/drop with locked table */ +#define HA_DDL_ONLINE 4 /* Can create/drop without lock */ + +/* Return value for ddl methods */ +#define HA_DDL_NOT_IMPLEMENTED -1 + /* Parameters for open() (in register form->filestat) HA_GET_INFO does an implicit HA_ABORT_IF_LOCKED @@ -124,15 +138,23 @@ /* Table caching type */ #define HA_CACHE_TBL_NONTRANSACT 0 -#define HA_CACHE_TBL_ASKTRANSACT 1 -#define HA_CACHE_TBL_TRANSACT 2 - -enum db_type { DB_TYPE_UNKNOWN=0,DB_TYPE_DIAB_ISAM=1, - DB_TYPE_HASH,DB_TYPE_MISAM,DB_TYPE_PISAM, - DB_TYPE_RMS_ISAM, DB_TYPE_HEAP, DB_TYPE_ISAM, - DB_TYPE_MRG_ISAM, DB_TYPE_MYISAM, DB_TYPE_MRG_MYISAM, - DB_TYPE_BERKELEY_DB, DB_TYPE_INNODB, DB_TYPE_GEMINI, - DB_TYPE_DEFAULT }; +#define HA_CACHE_TBL_NOCACHE 1 +#define HA_CACHE_TBL_ASKTRANSACT 2 +#define HA_CACHE_TBL_TRANSACT 4 + + +enum db_type +{ + DB_TYPE_UNKNOWN=0,DB_TYPE_DIAB_ISAM=1, + DB_TYPE_HASH,DB_TYPE_MISAM,DB_TYPE_PISAM, + DB_TYPE_RMS_ISAM, DB_TYPE_HEAP, DB_TYPE_ISAM, + DB_TYPE_MRG_ISAM, DB_TYPE_MYISAM, DB_TYPE_MRG_MYISAM, + DB_TYPE_BERKELEY_DB, DB_TYPE_INNODB, + DB_TYPE_GEMINI, DB_TYPE_NDBCLUSTER, + DB_TYPE_EXAMPLE_DB, + + DB_TYPE_DEFAULT // Must be last +}; struct show_table_type_st { const char *type; @@ -162,6 +184,7 @@ typedef struct st_thd_trans { void *bdb_tid; void *innobase_tid; bool innodb_active_trans; + void *ndb_tid; } THD_TRANS; enum enum_tx_isolation { ISO_READ_UNCOMMITTED, ISO_READ_COMMITTED, @@ -204,6 +227,14 @@ typedef struct st_ha_check_opt } HA_CHECK_OPT; +typedef struct st_key_range +{ + const byte *key; + uint length; + enum ha_rkey_function flag; +} key_range; + + class handler :public Sql_alloc { protected: @@ -225,6 +256,12 @@ public: time_t create_time; /* When table was created */ time_t check_time; time_t update_time; + + /* The following are for read_range() */ + key_range save_end_range, *end_range; + KEY_PART_INFO *range_key_part; + int key_compare_result_on_equal; + uint errkey; /* Last dup key */ uint sortkey, key_used_on_scan; uint active_index; @@ -236,6 +273,7 @@ public: bool auto_increment_column_changed; bool implicit_emptied; /* Can be !=0 only if HEAP */ + handler(TABLE *table_arg) :table(table_arg), ref(0), data_file_length(0), max_data_file_length(0), index_file_length(0), delete_length(0), auto_increment_value(0), @@ -256,7 +294,6 @@ public: { return ulonglong2double(data_file_length) / IO_SIZE + 2; } virtual double read_time(uint index, uint ranges, ha_rows rows) { return rows2double(ranges+rows); } - virtual bool fast_key_read() { return 0;} virtual const key_map *keys_to_use_for_scanning() { return &key_map_empty; } virtual bool has_transactions(){ return 0;} virtual uint extra_rec_buf_length() { return 0; } @@ -285,6 +322,11 @@ public: { return (my_errno=HA_ERR_WRONG_COMMAND); } + virtual int read_range_first(const key_range *start_key, + const key_range *end_key, + bool sorted); + virtual int read_range_next(bool eq_range); + int compare_key(key_range *range); virtual int ft_init() { return -1; } virtual FT_INFO *ft_init_ext(uint flags,uint inx,const byte *key, uint keylen) @@ -310,7 +352,7 @@ public: { return extra(operation); } - virtual int reset()=0; + virtual int reset() { return extra(HA_EXTRA_RESET); } virtual int external_lock(THD *thd, int lock_type)=0; virtual void unlock_row() {} virtual int start_stmt(THD *thd) {return 0;} @@ -331,8 +373,10 @@ public: */ virtual int restore(THD* thd, HA_CHECK_OPT* check_opt); virtual int dump(THD* thd, int fd = -1) { return ER_DUMP_NOT_IMPLEMENTED; } - virtual void deactivate_non_unique_index(ha_rows rows) {} - virtual bool activate_all_index(THD *thd) {return 0;} + virtual int disable_indexes(bool all, bool save) { return HA_ERR_WRONG_COMMAND; } + virtual int enable_indexes() { return HA_ERR_WRONG_COMMAND; } + virtual void start_bulk_insert(ha_rows rows) {} + virtual int end_bulk_insert() {return 0; } virtual int discard_or_import_tablespace(my_bool discard) {return -1;} // not implemented by default virtual int net_read_dump(NET* net) @@ -355,6 +399,20 @@ public: { return (HA_READ_NEXT | HA_READ_PREV | HA_READ_ORDER | HA_KEY_READ_ONLY); } + virtual ulong index_ddl_flags(KEY *wanted_index) const + { + return (HA_DDL_SUPPORT); + } + virtual int add_index(TABLE *table, KEY *key_info, uint num_of_keys) + { + my_error(ER_NOT_SUPPORTED_YET, MYF(0), "online add index"); + return (HA_DDL_NOT_IMPLEMENTED); + } + virtual int drop_index(TABLE *table, uint *key_num, uint num_of_keys) + { + my_error(ER_NOT_SUPPORTED_YET, MYF(0), "online drop index"); + return (HA_DDL_NOT_IMPLEMENTED); + } virtual uint max_record_length() const =0; virtual uint max_keys() const =0; virtual uint max_key_parts() const =0; @@ -437,3 +495,5 @@ bool ha_flush_logs(void); int ha_recovery_logging(THD *thd, bool on); int ha_change_key_cache(KEY_CACHE *old_key_cache, KEY_CACHE *new_key_cache); +int ha_discover(const char* dbname, const char* name, + const void** frmblob, uint* frmlen); diff --git a/sql/item.cc b/sql/item.cc index 5f31f3fa6ec..8c20d45714f 100644 --- a/sql/item.cc +++ b/sql/item.cc @@ -533,6 +533,22 @@ Item *Item_field::get_tmp_table_item(THD *thd) } +/* + Create an item from a string we KNOW points to a valid longlong/ulonglong + end \0 terminated number string +*/ + +Item_int::Item_int(const char *str_arg, uint length) +{ + char *end_ptr= (char*) str_arg + length; + int error; + value= my_strtoll10(str_arg, &end_ptr, &error); + max_length= (uint) (end_ptr - str_arg); + name= (char*) str_arg; + fixed= 1; +} + + String *Item_int::val_str(String *str) { // following assert is redundant, because fixed=1 assigned in constructor @@ -549,6 +565,13 @@ void Item_int::print(String *str) } +Item_uint::Item_uint(const char *str_arg, uint length): + Item_int(str_arg, length) +{ + unsigned_flag= 1; +} + + String *Item_uint::val_str(String *str) { // following assert is redundant, because fixed=1 assigned in constructor @@ -635,12 +658,19 @@ Item_param::Item_param(unsigned position) : set_param_func(default_set_param_func) { name= (char*) "?"; + /* + Since we can't say whenever this item can be NULL or cannot be NULL + before mysql_stmt_execute(), so we assuming that it can be NULL until + value is set. + */ + maybe_null= 1; } void Item_param::set_null() { DBUG_ENTER("Item_param::set_null"); - maybe_null= null_value= value_is_set= 1; + /* These are cleared after each execution by reset() method */ + null_value= value_is_set= 1; DBUG_VOID_RETURN; } @@ -650,6 +680,7 @@ void Item_param::set_int(longlong i) int_value= (longlong)i; item_type= INT_ITEM; value_is_set= 1; + maybe_null= 0; DBUG_PRINT("info", ("integer: %lld", int_value)); DBUG_VOID_RETURN; } @@ -660,6 +691,7 @@ void Item_param::set_double(double value) real_value=value; item_type= REAL_ITEM; value_is_set= 1; + maybe_null= 0; DBUG_PRINT("info", ("double: %lg", real_value)); DBUG_VOID_RETURN; } @@ -671,6 +703,7 @@ void Item_param::set_value(const char *str, uint length) str_value.copy(str,length,default_charset()); item_type= STRING_ITEM; value_is_set= 1; + maybe_null= 0; DBUG_PRINT("info", ("string: %s", str_value.ptr())); DBUG_VOID_RETURN; } @@ -695,6 +728,7 @@ void Item_param::set_time(TIME *tm, timestamp_type type) item_is_time= TRUE; item_type= STRING_ITEM; value_is_set= 1; + maybe_null= 0; } @@ -703,14 +737,33 @@ void Item_param::set_longdata(const char *str, ulong length) str_value.append(str,length); long_data_supplied= 1; value_is_set= 1; + maybe_null= 0; +} + + +/* + Resets parameter after execution. + + SYNOPSIS + Item_param::reset() + + NOTES + We clear null_value here instead of setting it in set_* methods, + because we want more easily handle case for long data. +*/ + +void Item_param::reset() +{ + str_value.set("", 0, &my_charset_bin); + value_is_set= long_data_supplied= 0; + maybe_null= 1; + null_value= 0; } int Item_param::save_in_field(Field *field, bool no_conversions) { - THD *thd= current_thd; - - DBUG_ASSERT(thd->command == COM_EXECUTE); + DBUG_ASSERT(current_thd->command == COM_EXECUTE); if (null_value) return (int) set_field_to_null(field); @@ -719,12 +772,12 @@ int Item_param::save_in_field(Field *field, bool no_conversions) if (item_result_type == INT_RESULT) { longlong nr=val_int(); - return (field->store(nr)) ? -1 : 0; + return field->store(nr); } if (item_result_type == REAL_RESULT) { double nr=val(); - return (field->store(nr)) ? -1 : 0; + return field->store(nr); } if (item_is_time) { @@ -745,6 +798,8 @@ double Item_param::val() { DBUG_ASSERT(value_is_set == 1); int err; + if (null_value) + return 0.0; switch (item_result_type) { case STRING_RESULT: return (double) my_strntod(str_value.charset(), (char*) str_value.ptr(), @@ -761,6 +816,8 @@ longlong Item_param::val_int() { DBUG_ASSERT(value_is_set == 1); int err; + if (null_value) + return 0; switch (item_result_type) { case STRING_RESULT: return my_strntoll(str_value.charset(), @@ -777,6 +834,8 @@ longlong Item_param::val_int() String *Item_param::val_str(String* str) { DBUG_ASSERT(value_is_set == 1); + if (null_value) + return NULL; switch (item_result_type) { case INT_RESULT: str->set(int_value, &my_charset_bin); @@ -1257,15 +1316,16 @@ Field *Item::tmp_table_field_from_field_type(TABLE *table) void Item_field::make_field(Send_field *tmp_field) { field->make_field(tmp_field); + DBUG_ASSERT(tmp_field->table_name); if (name) tmp_field->col_name=name; // Use user supplied name } + /* -** Set a field:s value from a item + Set a field:s value from a item */ - void Item_field::save_org_in_field(Field *to) { if (field->is_null()) @@ -1351,7 +1411,10 @@ int Item::save_in_field(Field *field, bool no_conversions) str_value.set_quick(buff, sizeof(buff), cs); result=val_str(&str_value); if (null_value) + { + str_value.set_quick(0, 0, cs); return set_field_to_null_with_conversions(field, no_conversions); + } field->set_notnull(); error=field->store(result->ptr(),result->length(),cs); str_value.set_quick(0, 0, cs); @@ -2001,7 +2064,7 @@ bool field_is_equal_to_item(Field *field,Item *item) item_result=item->val_str(&item_tmp); if (item->null_value) return 1; // This must be true - field->val_str(&field_tmp,&field_tmp); + field->val_str(&field_tmp); return !stringcmp(&field_tmp,item_result); } if (res_type == INT_RESULT) diff --git a/sql/item.h b/sql/item.h index 9f6aa5a9a32..90362e917d3 100644 --- a/sql/item.h +++ b/sql/item.h @@ -147,6 +147,7 @@ public: complete fix_fields() procedure. */ inline void quick_fix_field() { fixed= 1; } + /* Function returns 1 on overflow and -1 on fatal errors */ virtual int save_in_field(Field *field, bool no_conversions); virtual void save_org_in_field(Field *field) { (void) save_in_field(field, 1); } @@ -157,6 +158,7 @@ public: virtual Item_result result_type () const { return REAL_RESULT; } virtual enum_field_types field_type() const; virtual enum Type type() const =0; + /* valXXX methods must return NULL or 0 or 0.0 if null_value is set. */ virtual double val()=0; virtual longlong val_int()=0; virtual String *val_str(String*)=0; @@ -517,7 +519,7 @@ public: void set_long_end(); void set_time(TIME *tm, timestamp_type type); bool get_time(TIME *tm); - void reset() {} + void reset(); /* Assign placeholder value from bind data. Note, that 'len' has different semantics in embedded library (as we @@ -553,10 +555,7 @@ public: #endif Item_int(const char *str_arg,longlong i,uint length) :value(i) { max_length=length; name=(char*) str_arg; fixed= 1; } - Item_int(const char *str_arg) : - value(str_arg[0] == '-' ? strtoll(str_arg,(char**) 0,10) : - (longlong) strtoull(str_arg,(char**) 0,10)) - { max_length= (uint) strlen(str_arg); name=(char*) str_arg; fixed= 1; } + Item_int(const char *str_arg, uint length=64); enum Type type() const { return INT_ITEM; } enum Item_result result_type () const { return INT_RESULT; } enum_field_types field_type() const { return MYSQL_TYPE_LONGLONG; } @@ -576,9 +575,7 @@ public: class Item_uint :public Item_int { public: - Item_uint(const char *str_arg, uint length) : - Item_int(str_arg, (longlong) strtoull(str_arg, (char**) 0,10), length) - { unsigned_flag= 1; } + Item_uint(const char *str_arg, uint length); Item_uint(uint32 i) :Item_int((longlong) i, 10) { unsigned_flag= 1; } double val() diff --git a/sql/item_cmpfunc.cc b/sql/item_cmpfunc.cc index ae6658c8e35..afbf0b7163e 100644 --- a/sql/item_cmpfunc.cc +++ b/sql/item_cmpfunc.cc @@ -477,9 +477,10 @@ bool Item_in_optimizer::fix_left(THD *thd, struct st_table_list *tables, Item **ref) { - if (args[0]->fix_fields(thd, tables, ref) || - (!cache && !(cache= Item_cache::get_cache(args[0]->result_type())))) + if (!args[0]->fixed && args[0]->fix_fields(thd, tables, args) || + !cache && !(cache= Item_cache::get_cache(args[0]->result_type()))) return 1; + cache->setup(args[0]); cache->store(args[0]); if (cache->cols() == 1) @@ -512,12 +513,12 @@ bool Item_in_optimizer::fix_fields(THD *thd, struct st_table_list *tables, Item ** ref) { DBUG_ASSERT(fixed == 0); - if (!args[0]->fixed && fix_left(thd, tables, ref)) + if (fix_left(thd, tables, ref)) return 1; if (args[0]->maybe_null) maybe_null=1; - if (!args[1]->fixed && args[1]->fix_fields(thd, tables, args)) + if (!args[1]->fixed && args[1]->fix_fields(thd, tables, args+1)) return 1; Item_in_subselect * sub= (Item_in_subselect *)args[1]; if (args[0]->cols() != sub->engine->cols()) diff --git a/sql/item_cmpfunc.h b/sql/item_cmpfunc.h index d654bec4f4e..ef80c060c03 100644 --- a/sql/item_cmpfunc.h +++ b/sql/item_cmpfunc.h @@ -96,7 +96,8 @@ protected: bool save_cache; public: Item_in_optimizer(Item *a, Item_in_subselect *b): - Item_bool_func(a, (Item *)b), cache(0), save_cache(0) {} + Item_bool_func(a, my_reinterpret_cast(Item *)(b)), cache(0), save_cache(0) + {} bool fix_fields(THD *, struct st_table_list *, Item **); bool fix_left(THD *thd, struct st_table_list *tables, Item **ref); bool is_null(); diff --git a/sql/item_geofunc.cc b/sql/item_geofunc.cc index 555c1a74eaf..d95271a54bb 100644 --- a/sql/item_geofunc.cc +++ b/sql/item_geofunc.cc @@ -697,8 +697,7 @@ longlong Item_func_srid::val_int() DBUG_ASSERT(fixed == 1); String *swkb= args[0]->val_str(&value); Geometry_buffer buffer; - Geometry *geom; - + null_value= (!swkb || !Geometry::create_from_wkb(&buffer, swkb->ptr() + SRID_SIZE, diff --git a/sql/item_strfunc.cc b/sql/item_strfunc.cc index 933995c1d22..c05091cae1f 100644 --- a/sql/item_strfunc.cc +++ b/sql/item_strfunc.cc @@ -1579,7 +1579,7 @@ String *Item_func_soundex::val_str(String *str) char *from= (char *) res->ptr(), *end=from+res->length(); tmp_value.set_charset(cs); - while (from != end && my_isspace(cs,*from)) // Skip pre-space + while (from != end && !my_isalpha(cs,*from)) // Skip pre-space from++; /* purecov: inspected */ if (from == end) return &my_empty_string; // No alpha characters. @@ -2825,9 +2825,9 @@ String *Item_func_uuid::val_str(String *str) uuid_time=tv; pthread_mutex_unlock(&LOCK_uuid_generator); - uint32 time_low= tv & 0xFFFFFFFF; - uint16 time_mid= (tv >> 32) & 0xFFFF; - uint16 time_hi_and_version= (tv >> 48) | UUID_VERSION; + uint32 time_low= (uint32) (tv & 0xFFFFFFFF); + uint16 time_mid= (uint16) ((tv >> 32) & 0xFFFF); + uint16 time_hi_and_version= (uint16) ((tv >> 48) | UUID_VERSION); str->realloc(UUID_LENGTH+1); str->length(UUID_LENGTH); diff --git a/sql/item_subselect.cc b/sql/item_subselect.cc index 390c734a87a..a84d75a5895 100644 --- a/sql/item_subselect.cc +++ b/sql/item_subselect.cc @@ -1350,7 +1350,7 @@ void subselect_uniquesubquery_engine::exclude() table_map subselect_engine::calc_const_tables(TABLE_LIST *table) { table_map map= 0; - for(; table; table= table->next) + for (; table; table= table->next) { TABLE *tbl= table->table; if (tbl && tbl->const_table) diff --git a/sql/item_subselect.h b/sql/item_subselect.h index e68c882ba3e..6d8f5353695 100644 --- a/sql/item_subselect.h +++ b/sql/item_subselect.h @@ -140,7 +140,7 @@ public: void fix_length_and_dec(); uint cols(); - Item* el(uint i) { return (Item*)row[i]; } + Item* el(uint i) { return my_reinterpret_cast(Item*)(row[i]); } Item** addr(uint i) { return (Item**)row + i; } bool check_cols(uint c); bool null_inside(); diff --git a/sql/item_sum.cc b/sql/item_sum.cc index 7b2b00b18c6..c68754f6cae 100644 --- a/sql/item_sum.cc +++ b/sql/item_sum.cc @@ -1057,7 +1057,7 @@ Item_sum_hybrid::min_max_update_str_field() if (!args[0]->null_value) { res_str->strip_sp(); - result_field->val_str(&tmp_value,&tmp_value); + result_field->val_str(&tmp_value); if (result_field->is_null() || (cmp_sign * sortcmp(res_str,&tmp_value,cmp_charset)) < 0) @@ -1766,8 +1766,7 @@ int dump_leaf_key(byte* key, uint32 count __attribute__((unused)), Item_func_group_concat *item) { char buff[MAX_FIELD_WIDTH]; - String tmp((char*) &buff, sizeof(buff), default_charset_info); - String tmp2((char *) &buff, sizeof(buff), default_charset_info); + String tmp((char *)&buff,sizeof(buff),default_charset_info), tmp2; char *record= (char*) item->table->record[0]; tmp.length(0); diff --git a/sql/item_sum.h b/sql/item_sum.h index 170a142545e..953ff76b65d 100644 --- a/sql/item_sum.h +++ b/sql/item_sum.h @@ -331,6 +331,7 @@ class Item_sum_avg :public Item_sum_num void update_field(); Item *result_item(Field *field) { return new Item_avg_field(this); } + void no_rows_in_result() {} const char *func_name() const { return "avg"; } Item *copy_or_same(THD* thd); }; @@ -383,6 +384,7 @@ class Item_sum_variance : public Item_sum_num void update_field(); Item *result_item(Field *field) { return new Item_variance_field(this); } + void no_rows_in_result() {} const char *func_name() const { return "variance"; } Item *copy_or_same(THD* thd); }; @@ -766,8 +768,12 @@ class Item_func_group_concat : public Item_sum } longlong val_int() { - String *res; res=val_str(&str_value); - return res ? strtoll(res->c_ptr(),(char**) 0,10) : (longlong) 0; + String *res; + char *end_ptr; + int error; + res= val_str(&str_value); + end_ptr= (char*) res->ptr()+ res->length(); + return res ? my_strtoll10(res->ptr(), &end_ptr, &error) : (longlong) 0; } String* val_str(String* str); Item *copy_or_same(THD* thd); diff --git a/sql/item_timefunc.cc b/sql/item_timefunc.cc index ffa00832778..2cda5dde3aa 100644 --- a/sql/item_timefunc.cc +++ b/sql/item_timefunc.cc @@ -634,8 +634,9 @@ bool make_date_time(DATE_TIME_FORMAT *format, TIME *l_time, For example, '1.1' -> '1.100000' */ -bool get_interval_info(const char *str,uint length,CHARSET_INFO *cs, - uint count, long *values, bool transform_msec) +static bool get_interval_info(const char *str,uint length,CHARSET_INFO *cs, + uint count, ulonglong *values, + bool transform_msec) { const char *end=str+length; uint i; @@ -644,10 +645,10 @@ bool get_interval_info(const char *str,uint length,CHARSET_INFO *cs, for (i=0 ; i < count ; i++) { - long value; + longlong value; const char *start= str; for (value=0; str != end && my_isdigit(cs,*str) ; str++) - value=value*10L + (long) (*str - '0'); + value= value*LL(10) + (longlong) (*str - '0'); if (transform_msec && i == count - 1) // microseconds always last { long msec_length= 6 - (str - start); @@ -662,14 +663,15 @@ bool get_interval_info(const char *str,uint length,CHARSET_INFO *cs, i++; /* Change values[0...i-1] -> values[0...count-1] */ bmove_upp((char*) (values+count), (char*) (values+i), - sizeof(long)*i); - bzero((char*) values, sizeof(long)*(count-i)); + sizeof(*values)*i); + bzero((char*) values, sizeof(*values)*(count-i)); break; } } return (str != end); } + longlong Item_func_period_add::val_int() { DBUG_ASSERT(fixed == 1); @@ -929,7 +931,8 @@ longlong Item_func_time_to_sec::val_int() static bool get_interval_value(Item *args,interval_type int_type, String *str_value, INTERVAL *interval) { - long array[5],value; + ulonglong array[5]; + longlong value; const char *str; uint32 length; CHARSET_INFO *cs=str_value->charset(); @@ -941,7 +944,7 @@ static bool get_interval_value(Item *args,interval_type int_type, bzero((char*) interval,sizeof(*interval)); if ((int) int_type <= INTERVAL_MICROSECOND) { - value=(long) args->val_int(); + value= args->val_int(); if (args->null_value) return 1; if (value < 0) @@ -971,22 +974,22 @@ static bool get_interval_value(Item *args,interval_type int_type, switch (int_type) { case INTERVAL_YEAR: - interval->year=value; + interval->year= (ulong) value; break; case INTERVAL_QUARTER: interval->month=value*3; break; case INTERVAL_MONTH: - interval->month=value; + interval->month= (ulong) value; break; case INTERVAL_WEEK: interval->day=value*7; break; case INTERVAL_DAY: - interval->day=value; + interval->day= (ulong) value; break; case INTERVAL_HOUR: - interval->hour=value; + interval->hour= (ulong) value; break; case INTERVAL_MICROSECOND: interval->second_part=value; @@ -1000,78 +1003,78 @@ static bool get_interval_value(Item *args,interval_type int_type, case INTERVAL_YEAR_MONTH: // Allow YEAR-MONTH YYYYYMM if (get_interval_info(str,length,cs,2,array,0)) return (1); - interval->year=array[0]; - interval->month=array[1]; + interval->year= (ulong) array[0]; + interval->month= (ulong) array[1]; break; case INTERVAL_DAY_HOUR: if (get_interval_info(str,length,cs,2,array,0)) return (1); - interval->day=array[0]; - interval->hour=array[1]; + interval->day= (ulong) array[0]; + interval->hour= (ulong) array[1]; break; case INTERVAL_DAY_MICROSECOND: if (get_interval_info(str,length,cs,5,array,1)) return (1); - interval->day=array[0]; - interval->hour=array[1]; - interval->minute=array[2]; - interval->second=array[3]; - interval->second_part=array[4]; + interval->day= (ulong) array[0]; + interval->hour= (ulong) array[1]; + interval->minute= array[2]; + interval->second= array[3]; + interval->second_part= array[4]; break; case INTERVAL_DAY_MINUTE: if (get_interval_info(str,length,cs,3,array,0)) return (1); - interval->day=array[0]; - interval->hour=array[1]; - interval->minute=array[2]; + interval->day= (ulong) array[0]; + interval->hour= (ulong) array[1]; + interval->minute= array[2]; break; case INTERVAL_DAY_SECOND: if (get_interval_info(str,length,cs,4,array,0)) return (1); - interval->day=array[0]; - interval->hour=array[1]; - interval->minute=array[2]; - interval->second=array[3]; + interval->day= (ulong) array[0]; + interval->hour= (ulong) array[1]; + interval->minute= array[2]; + interval->second= array[3]; break; case INTERVAL_HOUR_MICROSECOND: if (get_interval_info(str,length,cs,4,array,1)) return (1); - interval->hour=array[0]; - interval->minute=array[1]; - interval->second=array[2]; - interval->second_part=array[3]; + interval->hour= (ulong) array[0]; + interval->minute= array[1]; + interval->second= array[2]; + interval->second_part= array[3]; break; case INTERVAL_HOUR_MINUTE: if (get_interval_info(str,length,cs,2,array,0)) return (1); - interval->hour=array[0]; - interval->minute=array[1]; + interval->hour= (ulong) array[0]; + interval->minute= array[1]; break; case INTERVAL_HOUR_SECOND: if (get_interval_info(str,length,cs,3,array,0)) return (1); - interval->hour=array[0]; - interval->minute=array[1]; - interval->second=array[2]; + interval->hour= (ulong) array[0]; + interval->minute= array[1]; + interval->second= array[2]; break; case INTERVAL_MINUTE_MICROSECOND: if (get_interval_info(str,length,cs,3,array,1)) return (1); - interval->minute=array[0]; - interval->second=array[1]; - interval->second_part=array[2]; + interval->minute= array[0]; + interval->second= array[1]; + interval->second_part= array[2]; break; case INTERVAL_MINUTE_SECOND: if (get_interval_info(str,length,cs,2,array,0)) return (1); - interval->minute=array[0]; - interval->second=array[1]; + interval->minute= array[0]; + interval->second= array[1]; break; case INTERVAL_SECOND_MICROSECOND: if (get_interval_info(str,length,cs,2,array,1)) return (1); - interval->second=array[0]; - interval->second_part=array[1]; + interval->second= array[0]; + interval->second_part= array[1]; break; } return 0; @@ -1645,7 +1648,8 @@ bool Item_date_add_interval::get_date(TIME *ltime, uint fuzzy_date) case INTERVAL_DAY_SECOND: case INTERVAL_DAY_MINUTE: case INTERVAL_DAY_HOUR: - long sec,days,daynr,microseconds,extra_sec; + { + longlong sec, days, daynr, microseconds, extra_sec; ltime->time_type=TIMESTAMP_DATETIME; // Return full date microseconds= ltime->second_part + sign*interval.second_part; extra_sec= microseconds/1000000L; @@ -1653,41 +1657,45 @@ bool Item_date_add_interval::get_date(TIME *ltime, uint fuzzy_date) sec=((ltime->day-1)*3600*24L+ltime->hour*3600+ltime->minute*60+ ltime->second + - sign*(interval.day*3600*24L + - interval.hour*3600+interval.minute*60+interval.second))+ - extra_sec; - + sign* (longlong) (interval.day*3600*24L + + interval.hour*LL(3600)+interval.minute*LL(60)+ + interval.second))+ extra_sec; if (microseconds < 0) { - microseconds+= 1000000L; + microseconds+= LL(1000000); sec--; } - days=sec/(3600*24L); sec=sec-days*3600*24L; + days= sec/(3600*LL(24)); + sec-= days*3600*LL(24); if (sec < 0) { days--; - sec+=3600*24L; + sec+= 3600*LL(24); } - ltime->second_part= microseconds; - ltime->second=sec % 60; - ltime->minute=sec/60 % 60; - ltime->hour=sec/3600; + ltime->second_part= (uint) microseconds; + ltime->second= (uint) (sec % 60); + ltime->minute= (uint) (sec/60 % 60); + ltime->hour= (uint) (sec/3600); daynr= calc_daynr(ltime->year,ltime->month,1) + days; - get_date_from_daynr(daynr,<ime->year,<ime->month,<ime->day); - if (daynr < 0 || daynr >= MAX_DAY_NUMBER) // Day number from year 0 to 9999-12-31 + /* Day number from year 0 to 9999-12-31 */ + if ((ulonglong) daynr >= MAX_DAY_NUMBER) goto null_date; + get_date_from_daynr((long) daynr, <ime->year, <ime->month, + <ime->day); break; + } case INTERVAL_DAY: case INTERVAL_WEEK: - period= calc_daynr(ltime->year,ltime->month,ltime->day) + - sign*interval.day; - if (period < 0 || period >= MAX_DAY_NUMBER) // Daynumber from year 0 to 9999-12-31 + period= (calc_daynr(ltime->year,ltime->month,ltime->day) + + sign * (long) interval.day); + /* Daynumber from year 0 to 9999-12-31 */ + if ((ulong) period >= MAX_DAY_NUMBER) goto null_date; get_date_from_daynr((long) period,<ime->year,<ime->month,<ime->day); break; case INTERVAL_YEAR: - ltime->year += sign*interval.year; - if ((int) ltime->year < 0 || ltime->year >= 10000L) + ltime->year+= sign * (long) interval.year; + if ((ulong) ltime->year >= 10000L) goto null_date; if (ltime->month == 2 && ltime->day == 29 && calc_days_in_year(ltime->year) != 366) @@ -1696,9 +1704,9 @@ bool Item_date_add_interval::get_date(TIME *ltime, uint fuzzy_date) case INTERVAL_YEAR_MONTH: case INTERVAL_QUARTER: case INTERVAL_MONTH: - period= (ltime->year*12 + sign*interval.year*12 + - ltime->month-1 + sign*interval.month); - if (period < 0 || period >= 120000L) + period= (ltime->year*12 + sign * (long) interval.year*12 + + ltime->month-1 + sign * (long) interval.month); + if ((ulong) period >= 120000L) goto null_date; ltime->year= (uint) (period / 12); ltime->month= (uint) (period % 12L)+1; diff --git a/sql/key.cc b/sql/key.cc index d4499573e8e..a2c3b2c8989 100644 --- a/sql/key.cc +++ b/sql/key.cc @@ -233,7 +233,7 @@ void key_unpack(String *to,TABLE *table,uint idx) } if ((field=key_part->field)) { - field->val_str(&tmp,&tmp); + field->val_str(&tmp); if (key_part->length < field->pack_length()) tmp.length(min(tmp.length(),key_part->length)); to->append(tmp); diff --git a/sql/lex.h b/sql/lex.h index 1102e070e6d..b72ba53ec7e 100644 --- a/sql/lex.h +++ b/sql/lex.h @@ -48,7 +48,7 @@ SYM_GROUP sym_group_rtree= {"RTree keys", "HAVE_RTREE_KEYS"}; */ static SYMBOL symbols[] = { - { "&&", SYM(AND)}, + { "&&", SYM(AND_SYM)}, { "<", SYM(LT)}, { "<=", SYM(LE)}, { "<>", SYM(NE)}, @@ -67,7 +67,7 @@ static SYMBOL symbols[] = { { "ALL", SYM(ALL)}, { "ALTER", SYM(ALTER)}, { "ANALYZE", SYM(ANALYZE_SYM)}, - { "AND", SYM(AND)}, + { "AND", SYM(AND_SYM)}, { "ANY", SYM(ANY_SYM)}, { "AS", SYM(AS)}, { "ASC", SYM(ASC)}, @@ -317,6 +317,8 @@ static SYMBOL symbols[] = { { "NAMES", SYM(NAMES_SYM)}, { "NATIONAL", SYM(NATIONAL_SYM)}, { "NATURAL", SYM(NATURAL)}, + { "NDB", SYM(NDBCLUSTER_SYM)}, + { "NDBCLUSTER", SYM(NDBCLUSTER_SYM)}, { "NCHAR", SYM(NCHAR_SYM)}, { "NEW", SYM(NEW_SYM)}, { "NEXT", SYM(NEXT_SYM)}, @@ -334,7 +336,7 @@ static SYMBOL symbols[] = { { "OPTIMIZE", SYM(OPTIMIZE)}, { "OPTION", SYM(OPTION)}, { "OPTIONALLY", SYM(OPTIONALLY)}, - { "OR", SYM(OR)}, + { "OR", SYM(OR_SYM)}, { "ORDER", SYM(ORDER_SYM)}, { "OUT", SYM(OUT_SYM)}, { "OUTER", SYM(OUTER)}, diff --git a/sql/log.cc b/sql/log.cc index b18a01b2e56..a673e72e26c 100644 --- a/sql/log.cc +++ b/sql/log.cc @@ -108,6 +108,7 @@ MYSQL_LOG::~MYSQL_LOG() void MYSQL_LOG::cleanup() { + DBUG_ENTER("cleanup"); if (inited) { inited= 0; @@ -118,6 +119,7 @@ void MYSQL_LOG::cleanup() (void) pthread_mutex_destroy(&LOCK_index); (void) pthread_cond_destroy(&update_cond); } + DBUG_VOID_RETURN; } diff --git a/sql/log_event.cc b/sql/log_event.cc index 5a56cc139b0..f0274405ba8 100644 --- a/sql/log_event.cc +++ b/sql/log_event.cc @@ -1244,15 +1244,15 @@ void Query_log_event::print(FILE* file, bool short_form, (ulong) thread_id, (ulong) exec_time, error_code); } - bool same_db = 0; + bool different_db= 1; if (db && last_event_info->db) { - if (!(same_db = !memcmp(last_event_info->db, db, db_len + 1))) + if ((different_db = memcmp(last_event_info->db, db, db_len + 1))) memcpy(last_event_info->db, db, db_len + 1); } - if (db && db[0] && !same_db) + if (db && db[0] && different_db) fprintf(file, "use %s;\n", db); end=int10_to_str((long) when, strmov(buff,"SET TIMESTAMP="),10); *end++=';'; @@ -1355,8 +1355,10 @@ int Query_log_event::exec_event(struct st_relay_log_info* rli) access it. */ #if MYSQL_VERSION_ID < 50000 - rli->future_group_master_log_pos= log_pos + get_event_len(); + rli->future_group_master_log_pos= log_pos + get_event_len() - + (rli->mi->old_format ? (LOG_EVENT_HEADER_LEN - OLD_HEADER_LEN) : 0); #else + /* In 5.0 we store the end_log_pos in the relay log so no problem */ rli->future_group_master_log_pos= log_pos; #endif clear_all_errors(thd, rli); @@ -1415,11 +1417,11 @@ int Query_log_event::exec_event(struct st_relay_log_info* rli) { slave_print_error(rli,expected_error, "\ -Query '%s' partially completed on the master (error on master: %d) \ +Query partially completed on the master (error on master: %d) \ and was aborted. There is a chance that your master is inconsistent at this \ point. If you are sure that your master is ok, run this query manually on the \ slave and then restart the slave with SET GLOBAL SQL_SLAVE_SKIP_COUNTER=1; \ -START SLAVE; .", thd->query, expected_error); +START SLAVE; . Query: '%s'", expected_error, thd->query); thd->query_error= 1; } goto end; @@ -1438,15 +1440,14 @@ START SLAVE; .", thd->query, expected_error); { slave_print_error(rli, 0, "\ -Query '%s' caused different errors on master and slave. \ +Query caused different errors on master and slave. \ Error on master: '%s' (%d), Error on slave: '%s' (%d). \ -Default database: '%s'", - query, +Default database: '%s'. Query: '%s'", ER_SAFE(expected_error), expected_error, actual_error ? thd->net.last_error: "no error", actual_error, - print_slave_db_safe(db)); + print_slave_db_safe(db), query); thd->query_error= 1; } /* @@ -1464,11 +1465,10 @@ Default database: '%s'", else if (thd->query_error || thd->is_fatal_error) { slave_print_error(rli,actual_error, - "Error '%s' on query '%s'. Default database: '%s'", + "Error '%s' on query. Default database: '%s'. Query: '%s'", (actual_error ? thd->net.last_error : "unexpected success or fatal error"), - query, - print_slave_db_safe(db)); + print_slave_db_safe(db), query); thd->query_error= 1; } @@ -1630,6 +1630,11 @@ int Start_log_event_v3::write_data(IO_CACHE* file) int Start_log_event_v3::exec_event(struct st_relay_log_info* rli) { DBUG_ENTER("Start_log_event_v3::exec_event"); + /* + If the I/O thread has not started, mi->old_format is BINLOG_FORMAT_CURRENT + (that's what the MASTER_INFO constructor does), so the test below is not + perfect at all. + */ switch (rli->relay_log.description_event_for_exec->binlog_version) { case 3: @@ -2267,14 +2272,21 @@ void Load_log_event::print(FILE* file, bool short_form, LAST_EVENT_INFO* last_ev thread_id, exec_time); } - bool same_db = 0; + bool different_db= 1; if (db && last_event_info->db) { - if (!(same_db = !memcmp(last_event_info->db, db, db_len + 1))) + /* + If the database is different from the one of the previous statement, we + need to print the "use" command, and we update the last_db. + But if commented, the "use" is going to be commented so we should not + update the last_db. + */ + if ((different_db= memcmp(last_event_info->db, db, db_len + 1)) && + !commented) memcpy(last_event_info->db, db, db_len + 1); } - if (db && db[0] && !same_db) + if (db && db[0] && different_db) fprintf(file, "%suse %s;\n", commented ? "# " : "", db); @@ -2386,14 +2398,15 @@ int Load_log_event::exec_event(NET* net, struct st_relay_log_info* rli, { thd->db= (char*) rewrite_db(db); DBUG_ASSERT(thd->query == 0); - thd->query = 0; // Should not be needed - thd->query_error = 0; + thd->query= 0; // Should not be needed + thd->query_error= 0; clear_all_errors(thd, rli); if (!use_rli_only_for_errors) { /* Saved for InnoDB, see comment in Query_log_event::exec_event() */ #if MYSQL_VERSION_ID < 50000 - rli->future_group_master_log_pos= log_pos + get_event_len(); + rli->future_group_master_log_pos= log_pos + get_event_len() - + (rli->mi->old_format ? (LOG_EVENT_HEADER_LEN - OLD_HEADER_LEN) : 0); #else rli->future_group_master_log_pos= log_pos; #endif @@ -3903,8 +3916,12 @@ int Execute_load_log_event::exec_event(struct st_relay_log_info* rli) mysql_load()). */ -#if MYSQL_VERSION_ID < 50000 - rli->future_group_master_log_pos= log_pos + get_event_len(); +#if MYSQL_VERSION_ID < 40100 + rli->future_master_log_pos= log_pos + get_event_len() - + (rli->mi->old_format ? (LOG_EVENT_HEADER_LEN - OLD_HEADER_LEN) : 0); +#elif MYSQL_VERSION_ID < 50000 + rli->future_group_master_log_pos= log_pos + get_event_len() - + (rli->mi->old_format ? (LOG_EVENT_HEADER_LEN - OLD_HEADER_LEN) : 0); #else rli->future_group_master_log_pos= log_pos; #endif diff --git a/sql/mysql_priv.h b/sql/mysql_priv.h index 697b5146b7d..42c3777e9df 100644 --- a/sql/mysql_priv.h +++ b/sql/mysql_priv.h @@ -386,6 +386,18 @@ void free_items(Item *item); void cleanup_items(Item *item); class THD; void close_thread_tables(THD *thd, bool locked=0, bool skip_derived=0); +int check_one_table_access(THD *thd, ulong privilege, + TABLE_LIST *tables); +bool check_merge_table_access(THD *thd, char *db, + TABLE_LIST *table_list); +int multi_update_precheck(THD *thd, TABLE_LIST *tables); +int multi_delete_precheck(THD *thd, TABLE_LIST *tables, uint *table_count); +int insert_select_precheck(THD *thd, TABLE_LIST *tables); +int update_precheck(THD *thd, TABLE_LIST *tables); +int delete_precheck(THD *thd, TABLE_LIST *tables); +int insert_precheck(THD *thd, TABLE_LIST *tables, bool update); +int create_table_precheck(THD *thd, TABLE_LIST *tables, + TABLE_LIST *create_table); #include "sql_class.h" #include "opt_range.h" @@ -527,6 +539,11 @@ int mysql_handle_derived(LEX *lex); Field *create_tmp_field(THD *thd, TABLE *table,Item *item, Item::Type type, Item ***copy_func, Field **from_field, bool group,bool modify_item); +int mysql_prepare_table(THD *thd, HA_CREATE_INFO *create_info, + List<create_field> &fields, + List<Key> &keys, uint &db_options, + handler *file, KEY *&key_info_buffer, + uint &key_count, int select_field_count); int mysql_create_table(THD *thd,const char *db, const char *table_name, HA_CREATE_INFO *create_info, List<create_field> &fields, List<Key> &keys, @@ -543,7 +560,7 @@ int mysql_alter_table(THD *thd, char *new_db, char *new_name, List<create_field> &fields, List<Key> &keys,List<Alter_drop> &drop_list, List<Alter_column> &alter_list, - uint order_num, ORDER *order, + uint order_num, ORDER *order, uint alter_flags, enum enum_duplicates handle_duplicates, enum enum_enable_or_disable keys_onoff=LEAVE_AS_IS, enum tablespace_op_type tablespace_op=NO_TABLESPACE_OP, @@ -559,6 +576,9 @@ bool mysql_rename_table(enum db_type base, int mysql_create_index(THD *thd, TABLE_LIST *table_list, List<Key> &keys); int mysql_drop_index(THD *thd, TABLE_LIST *table_list, List<Alter_drop> &drop_list); +int mysql_prepare_update(THD *thd, TABLE_LIST *table_list, + TABLE_LIST *update_table_list, + Item **conds, uint order_num, ORDER *order); int mysql_update(THD *thd,TABLE_LIST *tables,List<Item> &fields, List<Item> &values,COND *conds, uint order_num, ORDER *order, ha_rows limit, @@ -568,9 +588,15 @@ int mysql_multi_update(THD *thd, TABLE_LIST *table_list, COND *conds, ulong options, enum enum_duplicates handle_duplicates, SELECT_LEX_UNIT *unit, SELECT_LEX *select_lex); +int mysql_prepare_insert(THD *thd, TABLE_LIST *table_list, + TABLE_LIST *insert_table_list, TABLE *table, + List<Item> &fields, List_item *values, + List<Item> &update_fields, + List<Item> &update_values, enum_duplicates duplic); int mysql_insert(THD *thd,TABLE_LIST *table,List<Item> &fields, List<List_item> &values, List<Item> &update_fields, List<Item> &update_values, enum_duplicates flag); +int mysql_prepare_delete(THD *thd, TABLE_LIST *table_list, Item **conds); int mysql_delete(THD *thd, TABLE_LIST *table, COND *conds, SQL_LIST *order, ha_rows rows, ulong options); int mysql_truncate(THD *thd, TABLE_LIST *table_list, bool dont_send_ok=0); @@ -809,7 +835,7 @@ bool load_db_opt(THD *thd, const char *path, HA_CREATE_INFO *create); extern time_t start_time; extern char *mysql_data_home,server_version[SERVER_VERSION_LENGTH], mysql_real_data_home[], *opt_mysql_tmpdir, mysql_charsets_dir[], - opt_ft_boolean_syntax[sizeof(ft_boolean_syntax)]; + def_ft_boolean_syntax[sizeof(ft_boolean_syntax)]; #define mysql_tmpdir (my_tmpdir(&mysql_tmpdir_list)) extern MY_TMPDIR mysql_tmpdir_list; extern const char *command_name[]; @@ -850,7 +876,7 @@ extern ulong server_id, concurrency; extern ulong ha_read_count, ha_write_count, ha_delete_count, ha_update_count; extern ulong ha_read_key_count, ha_read_next_count, ha_read_prev_count; extern ulong ha_read_first_count, ha_read_last_count; -extern ulong ha_read_rnd_count, ha_read_rnd_next_count; +extern ulong ha_read_rnd_count, ha_read_rnd_next_count, ha_discover_count; extern ulong ha_commit_count, ha_rollback_count,table_cache_size; extern ulong max_connections,max_connect_errors, connect_timeout; extern ulong slave_net_timeout; @@ -904,6 +930,7 @@ extern SHOW_VAR init_vars[],status_vars[], internal_vars[]; extern SHOW_COMP_OPTION have_isam; extern SHOW_COMP_OPTION have_innodb; extern SHOW_COMP_OPTION have_berkeley_db; +extern SHOW_COMP_OPTION have_ndbcluster; extern struct system_variables global_system_variables; extern struct system_variables max_system_variables; extern struct rand_struct sql_rand; @@ -921,7 +948,7 @@ extern struct my_option my_long_options[]; /* optional things, have_* variables */ -extern SHOW_COMP_OPTION have_isam, have_innodb, have_berkeley_db; +extern SHOW_COMP_OPTION have_isam, have_innodb, have_berkeley_db, have_example_db; extern SHOW_COMP_OPTION have_raid, have_openssl, have_symlink; extern SHOW_COMP_OPTION have_query_cache, have_berkeley_db, have_innodb; extern SHOW_COMP_OPTION have_crypt; @@ -962,6 +989,10 @@ void unlock_table_names(THD *thd, TABLE_LIST *table_list, void unireg_init(ulong options); void unireg_end(void); +bool mysql_create_frm(THD *thd, my_string file_name, + HA_CREATE_INFO *create_info, + List<create_field> &create_field, + uint key_count,KEY *key_info,handler *db_type); int rea_create_table(THD *thd, my_string file_name,HA_CREATE_INFO *create_info, List<create_field> &create_field, uint key_count,KEY *key_info); @@ -969,6 +1000,10 @@ int format_number(uint inputflag,uint max_length,my_string pos,uint length, my_string *errpos); int openfrm(const char *name,const char *alias,uint filestat,uint prgflag, uint ha_open_flags, TABLE *outparam); +int readfrm(const char *name, const void** data, uint* length); +int writefrm(const char* name, const void* data, uint len); +int create_table_from_handler(const char *db, const char *name, + bool create_if_found); int closefrm(TABLE *table); db_type get_table_type(const char *name); int read_string(File file, gptr *to, uint length); @@ -1046,8 +1081,7 @@ void reset_host_errors(struct in_addr *in); bool hostname_cache_init(); void hostname_cache_free(); void hostname_cache_refresh(void); -bool get_interval_info(const char *str,uint length,uint count, - long *values); + /* sql_cache.cc */ extern bool sql_cache_init(); extern void sql_cache_free(); diff --git a/sql/mysqld.cc b/sql/mysqld.cc index d242a54f141..dc01f3e03bb 100644 --- a/sql/mysqld.cc +++ b/sql/mysqld.cc @@ -33,6 +33,9 @@ #ifdef HAVE_ISAM #include "ha_isam.h" #endif +#ifdef HAVE_NDBCLUSTER_DB +#include "ha_ndbcluster.h" +#endif #include <nisam.h> #include <thr_alarm.h> #include <ft_global.h> @@ -264,7 +267,7 @@ my_bool opt_local_infile, opt_external_locking, opt_slave_compressed_protocol; my_bool opt_safe_user_create = 0, opt_no_mix_types = 0; my_bool opt_show_slave_auth_info, opt_sql_bin_update = 0; my_bool opt_log_slave_updates= 0; -my_bool opt_console= 0, opt_bdb, opt_innodb, opt_isam; +my_bool opt_console= 0, opt_bdb, opt_innodb, opt_isam, opt_ndbcluster; my_bool opt_readonly, use_temp_pool, relay_log_purge; my_bool opt_sync_bdb_logs, opt_sync_frm; my_bool opt_secure_auth= 0; @@ -329,7 +332,7 @@ char mysql_real_data_home[FN_REFLEN], language[LIBLEN],reg_ext[FN_EXTLEN], mysql_charsets_dir[FN_REFLEN], max_sort_char,*mysqld_user,*mysqld_chroot, *opt_init_file, *opt_init_connect, *opt_init_slave, - opt_ft_boolean_syntax[sizeof(ft_boolean_syntax)]; + def_ft_boolean_syntax[sizeof(ft_boolean_syntax)]; const char *opt_date_time_formats[3]; @@ -373,7 +376,8 @@ KEY_CACHE *sql_key_cache; CHARSET_INFO *system_charset_info, *files_charset_info ; CHARSET_INFO *national_charset_info, *table_alias_charset; -SHOW_COMP_OPTION have_berkeley_db, have_innodb, have_isam; +SHOW_COMP_OPTION have_berkeley_db, have_innodb, have_isam, + have_ndbcluster, have_example_db; SHOW_COMP_OPTION have_raid, have_openssl, have_symlink, have_query_cache; SHOW_COMP_OPTION have_crypt, have_compress; @@ -2377,6 +2381,17 @@ Warning: you need to use --log-bin to make --log-slave-updates work. \ Now disabling --log-slave-updates."); } +#ifdef HAVE_REPLICATION + if (opt_log_slave_updates && replicate_same_server_id) + { + sql_print_error("\ +Error: using --replicate-same-server-id in conjunction with \ +--log-slave-updates is impossible, it would lead to infinite loops in this \ +server."); + unireg_abort(1); + } +#endif + if (opt_error_log) { if (!log_error_file_ptr[0]) @@ -3665,7 +3680,7 @@ enum options_mysqld OPT_SKIP_SLAVE_START, OPT_SKIP_INNOBASE, OPT_SAFEMALLOC_MEM_LIMIT, OPT_REPLICATE_DO_TABLE, OPT_REPLICATE_IGNORE_TABLE, OPT_REPLICATE_WILD_DO_TABLE, - OPT_REPLICATE_WILD_IGNORE_TABLE, + OPT_REPLICATE_WILD_IGNORE_TABLE, OPT_REPLICATE_SAME_SERVER_ID, OPT_DISCONNECT_SLAVE_EVENT_COUNT, OPT_ABORT_SLAVE_EVENT_COUNT, OPT_INNODB_DATA_HOME_DIR, @@ -3678,7 +3693,7 @@ enum options_mysqld OPT_INNODB_FAST_SHUTDOWN, OPT_INNODB_FILE_PER_TABLE, OPT_SAFE_SHOW_DB, - OPT_INNODB, OPT_ISAM, OPT_SKIP_SAFEMALLOC, + OPT_INNODB, OPT_ISAM, OPT_NDBCLUSTER, OPT_SKIP_SAFEMALLOC, OPT_TEMP_POOL, OPT_TX_ISOLATION, OPT_SKIP_STACK_TRACE, OPT_SKIP_SYMLINKS, OPT_MAX_BINLOG_DUMP_EVENTS, OPT_SPORADIC_BINLOG_DUMP_FAIL, @@ -3710,7 +3725,7 @@ enum options_mysqld OPT_MAX_SEEKS_FOR_KEY, OPT_MAX_TMP_TABLES, OPT_MAX_USER_CONNECTIONS, OPT_MAX_LENGTH_FOR_SORT_DATA, OPT_MAX_WRITE_LOCK_COUNT, OPT_BULK_INSERT_BUFFER_SIZE, - OPT_MAX_ERROR_COUNT, + OPT_MAX_ERROR_COUNT, OPT_MYISAM_DATA_POINTER_SIZE, OPT_MYISAM_BLOCK_SIZE, OPT_MYISAM_MAX_EXTRA_SORT_FILE_SIZE, OPT_MYISAM_MAX_SORT_FILE_SIZE, OPT_MYISAM_SORT_BUFFER_SIZE, OPT_NET_BUFFER_LENGTH, OPT_NET_RETRY_COUNT, @@ -4140,6 +4155,15 @@ master-ssl", {"replicate-rewrite-db", OPT_REPLICATE_REWRITE_DB, "Updates to a database with a different name than the original. Example: replicate-rewrite-db=master_db_name->slave_db_name.", 0, 0, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0}, +#ifdef HAVE_REPLICATION + {"replicate-same-server-id", OPT_REPLICATE_SAME_SERVER_ID, + "In replication, if set to 1, do not skip events having our server id. \ +Default value is 0 (to break infinite loops in circular replication). \ +Can't be set to 1 if --log-slave-updates is used.", + (gptr*) &replicate_same_server_id, + (gptr*) &replicate_same_server_id, + 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0}, +#endif // In replication, we may need to tell the other servers how to connect {"report-host", OPT_REPORT_HOST, "Hostname or IP of the slave to be reported to to the master during slave registration. Will appear in the output of SHOW SLAVE HOSTS. Leave unset if you do not want the slave to register itself with the master. Note that it is not sufficient for the master to simply read the IP of the slave off the socket once the slave connects. Due to NAT and other routing issues, that IP may not be valid for connecting to the slave from the master or other hosts.", @@ -4193,7 +4217,7 @@ relay logs.", 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0}, #endif {"show-slave-auth-info", OPT_SHOW_SLAVE_AUTH_INFO, - "Show user and password in SHOW SLAVE HOSTS.", + "Show user and password in SHOW SLAVE HOSTS on this master", (gptr*) &opt_show_slave_auth_info, (gptr*) &opt_show_slave_auth_info, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0}, {"concurrent-insert", OPT_CONCURRENT_INSERT, @@ -4212,6 +4236,10 @@ Disable with --skip-innodb (will save memory).", Disable with --skip-isam.", (gptr*) &opt_isam, (gptr*) &opt_isam, 0, GET_BOOL, NO_ARG, 1, 0, 0, 0, 0, 0}, + {"ndbcluster", OPT_NDBCLUSTER, "Enable NDB Cluster (if this version of MySQL supports it). \ +Disable with --skip-ndbcluster (will save memory).", + (gptr*) &opt_ndbcluster, (gptr*) &opt_ndbcluster, 0, GET_BOOL, NO_ARG, 1, 0, 0, + 0, 0, 0}, {"skip-locking", OPT_SKIP_LOCK, "Deprecated option, use --skip-external-locking instead.", 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}, @@ -4561,6 +4589,11 @@ The minimum value for this variable is 4096.", (gptr*) &opt_myisam_block_size, 0, GET_ULONG, REQUIRED_ARG, MI_KEY_BLOCK_LENGTH, MI_MIN_KEY_BLOCK_LENGTH, MI_MAX_KEY_BLOCK_LENGTH, 0, MI_MIN_KEY_BLOCK_LENGTH, 0}, + {"myisam_data_pointer_size", OPT_MYISAM_DATA_POINTER_SIZE, + "Default pointer size to be used for MyISAM tables.", + (gptr*) &myisam_data_pointer_size, + (gptr*) &myisam_data_pointer_size, 0, GET_ULONG, REQUIRED_ARG, + 4, 2, 7, 0, 1, 0}, {"myisam_max_extra_sort_file_size", OPT_MYISAM_MAX_EXTRA_SORT_FILE_SIZE, "Used to help MySQL to decide when to use the slow but safe key cache index create method.", (gptr*) &global_system_variables.myisam_max_extra_sort_file_size, @@ -4882,10 +4915,13 @@ struct show_var_st status_vars[]= { {"Handler_rollback", (char*) &ha_rollback_count, SHOW_LONG}, {"Handler_update", (char*) &ha_update_count, SHOW_LONG}, {"Handler_write", (char*) &ha_write_count, SHOW_LONG}, + {"Handler_discover", (char*) &ha_discover_count, SHOW_LONG}, {"Key_blocks_not_flushed", (char*) &dflt_key_cache_var.global_blocks_changed, SHOW_KEY_CACHE_LONG}, - {"Key_blocks_used", (char*) &dflt_key_cache_var.global_blocks_used, - SHOW_KEY_CACHE_LONG}, + {"Key_blocks_used", (char*) &dflt_key_cache_var.blocks_used, + SHOW_KEY_CACHE_CONST_LONG}, + {"Key_blocks_unused", (char*) &dflt_key_cache_var.blocks_unused, + SHOW_KEY_CACHE_CONST_LONG}, {"Key_read_requests", (char*) &dflt_key_cache_var.global_cache_r_requests, SHOW_KEY_CACHE_LONG}, {"Key_reads", (char*) &dflt_key_cache_var.global_cache_read, @@ -5175,6 +5211,16 @@ static void mysql_init_variables(void) #else have_isam=SHOW_OPTION_NO; #endif +#ifdef HAVE_EXAMPLE_DB + have_example_db= SHOW_OPTION_YES; +#else + have_example_db= SHOW_OPTION_NO; +#endif +#ifdef HAVE_NDBCLUSTER_DB + have_ndbcluster=SHOW_OPTION_DISABLED; +#else + have_ndbcluster=SHOW_OPTION_NO; +#endif #ifdef USE_RAID have_raid=SHOW_OPTION_YES; #else @@ -5645,6 +5691,14 @@ get_one_option(int optid, const struct my_option *opt __attribute__((unused)), have_isam= SHOW_OPTION_DISABLED; #endif break; + case OPT_NDBCLUSTER: +#ifdef HAVE_NDBCLUSTER_DB + if (opt_ndbcluster) + have_ndbcluster=SHOW_OPTION_YES; + else + have_ndbcluster=SHOW_OPTION_DISABLED; +#endif + break; case OPT_INNODB: #ifdef HAVE_INNOBASE_DB if (opt_innodb) @@ -5705,7 +5759,7 @@ get_one_option(int optid, const struct my_option *opt __attribute__((unused)), fprintf(stderr, "Invalid ft-boolean-syntax string: %s\n", argument); exit(1); } - strmake(opt_ft_boolean_syntax, argument, sizeof(ft_boolean_syntax)-1); + strmake(ft_boolean_syntax, argument, sizeof(ft_boolean_syntax)-1); break; case OPT_SKIP_SAFEMALLOC: #ifdef SAFEMALLOC @@ -5755,7 +5809,7 @@ static void get_options(int argc,char **argv) int ho_error; my_getopt_register_get_addr(mysql_getopt_value); - strmake(opt_ft_boolean_syntax, ft_boolean_syntax, + strmake(def_ft_boolean_syntax, ft_boolean_syntax, sizeof(ft_boolean_syntax)-1); if ((ho_error=handle_options(&argc, &argv, my_long_options, get_one_option))) exit(ho_error); @@ -5812,8 +5866,6 @@ static void get_options(int argc,char **argv) table_alias_charset= (lower_case_table_names ? files_charset_info : &my_charset_bin); - strmake(ft_boolean_syntax, opt_ft_boolean_syntax, - sizeof(ft_boolean_syntax)-1); if (opt_short_log_format) opt_specialflag|= SPECIAL_SHORT_LOG_FORMAT; diff --git a/sql/protocol.cc b/sql/protocol.cc index dafcabe9e3a..4d0859aed22 100644 --- a/sql/protocol.cc +++ b/sql/protocol.cc @@ -838,7 +838,7 @@ bool Protocol_simple::store(Field *field) String str(buff,sizeof(buff), &my_charset_bin); CHARSET_INFO *tocs= this->thd->variables.character_set_results; - field->val_str(&str,&str); + field->val_str(&str); if (tocs && !my_charset_same(field->charset(), tocs) && (field->charset() != &my_charset_bin) && (tocs != &my_charset_bin)) diff --git a/sql/records.cc b/sql/records.cc index cd1de5af6aa..264f7fe1241 100644 --- a/sql/records.cc +++ b/sql/records.cc @@ -80,7 +80,7 @@ void init_read_record(READ_RECORD *info,THD *thd, TABLE *table, if (!table->sort.addon_field && ! (specialflag & SPECIAL_SAFE_MODE) && thd->variables.read_rnd_buff_size && - !table->file->fast_key_read() && + !(table->file->table_flags() & HA_FAST_KEY_READ) && (table->db_stat & HA_READ_ONLY || table->reginfo.lock_type <= TL_READ_NO_INSERT) && (ulonglong) table->reclength*(table->file->records+ diff --git a/sql/set_var.cc b/sql/set_var.cc index 2af0a011b58..079a0eae3dc 100644 --- a/sql/set_var.cc +++ b/sql/set_var.cc @@ -59,6 +59,9 @@ #ifdef HAVE_INNOBASE_DB #include "ha_innodb.h" #endif +#ifdef HAVE_NDBCLUSTER_DB +#include "ha_ndbcluster.h" +#endif static HASH system_variable_hash; const char *bool_type_names[]= { "OFF", "ON", NullS }; @@ -102,6 +105,7 @@ static void fix_myisam_max_sort_file_size(THD *thd, enum_var_type type); static void fix_max_binlog_size(THD *thd, enum_var_type type); static void fix_max_relay_log_size(THD *thd, enum_var_type type); static void fix_max_connections(THD *thd, enum_var_type type); +static int check_max_delayed_threads(THD *thd, set_var *var); static void fix_thd_mem_root(THD *thd, enum_var_type type); static void fix_trans_mem_root(THD *thd, enum_var_type type); static KEY_CACHE *create_key_cache(const char *name, uint length); @@ -202,10 +206,13 @@ sys_var_long_ptr sys_max_connections("max_connections", sys_var_long_ptr sys_max_connect_errors("max_connect_errors", &max_connect_errors); sys_var_thd_ulong sys_max_insert_delayed_threads("max_insert_delayed_threads", - &SV::max_insert_delayed_threads); + &SV::max_insert_delayed_threads, + check_max_delayed_threads, + fix_max_connections); sys_var_thd_ulong sys_max_delayed_threads("max_delayed_threads", &SV::max_insert_delayed_threads, - 0, fix_max_connections); + check_max_delayed_threads, + fix_max_connections); sys_var_thd_ulong sys_max_error_count("max_error_count", &SV::max_error_count); sys_var_thd_ulong sys_max_heap_table_size("max_heap_table_size", @@ -236,6 +243,8 @@ sys_var_thd_ulong sys_max_tmp_tables("max_tmp_tables", &SV::max_tmp_tables); sys_var_long_ptr sys_max_write_lock_count("max_write_lock_count", &max_write_lock_count); +sys_var_long_ptr sys_myisam_data_pointer_size("myisam_data_pointer_size", + &myisam_data_pointer_size); sys_var_thd_ulonglong sys_myisam_max_extra_sort_file_size("myisam_max_extra_sort_file_size", &SV::myisam_max_extra_sort_file_size, fix_myisam_max_extra_sort_file_size, 1); sys_var_thd_ulonglong sys_myisam_max_sort_file_size("myisam_max_sort_file_size", &SV::myisam_max_sort_file_size, fix_myisam_max_sort_file_size, 1); sys_var_thd_ulong sys_myisam_repair_threads("myisam_repair_threads", &SV::myisam_repair_threads); @@ -515,6 +524,7 @@ sys_var *sys_variables[]= &sys_max_tmp_tables, &sys_max_user_connections, &sys_max_write_lock_count, + &sys_myisam_data_pointer_size, &sys_myisam_max_extra_sort_file_size, &sys_myisam_max_sort_file_size, &sys_myisam_repair_threads, @@ -635,6 +645,7 @@ struct show_var_st init_vars[]= { {"have_crypt", (char*) &have_crypt, SHOW_HAVE}, {"have_innodb", (char*) &have_innodb, SHOW_HAVE}, {"have_isam", (char*) &have_isam, SHOW_HAVE}, + {"have_ndbcluster", (char*) &have_ndbcluster, SHOW_HAVE}, {"have_openssl", (char*) &have_openssl, SHOW_HAVE}, {"have_query_cache", (char*) &have_query_cache, SHOW_HAVE}, {"have_raid", (char*) &have_raid, SHOW_HAVE}, @@ -714,6 +725,7 @@ struct show_var_st init_vars[]= { {sys_max_tmp_tables.name, (char*) &sys_max_tmp_tables, SHOW_SYS}, {sys_max_user_connections.name,(char*) &sys_max_user_connections, SHOW_SYS}, {sys_max_write_lock_count.name, (char*) &sys_max_write_lock_count,SHOW_SYS}, + {sys_myisam_data_pointer_size.name, (char*) &sys_myisam_data_pointer_size, SHOW_SYS}, {sys_myisam_max_extra_sort_file_size.name, (char*) &sys_myisam_max_extra_sort_file_size, SHOW_SYS}, @@ -842,7 +854,7 @@ bool update_sys_var_str(sys_var_str *var_str, rw_lock_t *var_mutex, uint new_length= (var ? var->value->str_value.length() : 0); if (!old_value) old_value= (char*) ""; - if (!(res= my_strdup_with_length(old_value, new_length, MYF(0)))) + if (!(res= my_strdup_with_length((byte*)old_value, new_length, MYF(0)))) return 1; /* Replace the old value in such a way that the any thread using @@ -902,7 +914,7 @@ static bool sys_update_ftb_syntax(THD *thd, set_var * var) static void sys_default_ftb_syntax(THD *thd, enum_var_type type) { - strmake(ft_boolean_syntax, opt_ft_boolean_syntax, + strmake(ft_boolean_syntax, def_ft_boolean_syntax, sizeof(ft_boolean_syntax)-1); } @@ -1085,6 +1097,20 @@ static void fix_max_relay_log_size(THD *thd, enum_var_type type) } +static int check_max_delayed_threads(THD *thd, set_var *var) +{ + longlong val= var->value->val_int(); + if (var->type != OPT_GLOBAL && val != 0 && + val != (longlong) global_system_variables.max_insert_delayed_threads) + { + char buf[64]; + my_error(ER_WRONG_VALUE_FOR_VAR, MYF(0), var->var->name, llstr(val, buf)); + return 1; + } + return 0; +} + + static void fix_max_connections(THD *thd, enum_var_type type) { resize_thr_alarm(max_connections + @@ -2554,6 +2580,36 @@ int set_var::check(THD *thd) } +/* + Check variable, but without assigning value (used by PS) + + SYNOPSIS + set_var::light_check() + thd thread handler + + RETURN VALUE + 0 ok + 1 ERROR, message sent (normally no variables was updated) + -1 ERROR, message not sent +*/ +int set_var::light_check(THD *thd) +{ + if (var->check_type(type)) + { + my_error(type == OPT_GLOBAL ? ER_LOCAL_VARIABLE : ER_GLOBAL_VARIABLE, + MYF(0), + var->name); + return -1; + } + if (type == OPT_GLOBAL && check_global_access(thd, SUPER_ACL)) + return 1; + + if (value && (value->fix_fields(thd, 0, &value) || value->check_cols(1))) + return -1; + return 0; +} + + int set_var::update(THD *thd) { if (!value) @@ -2581,6 +2637,28 @@ int set_var_user::check(THD *thd) } +/* + Check variable, but without assigning value (used by PS) + + SYNOPSIS + set_var_user::light_check() + thd thread handler + + RETURN VALUE + 0 ok + 1 ERROR, message sent (normally no variables was updated) + -1 ERROR, message not sent +*/ +int set_var_user::light_check(THD *thd) +{ + /* + Item_func_set_user_var can't substitute something else on its place => + 0 can be passed as last argument (reference on item) + */ + return (user_var_item->fix_fields(thd, 0, (Item**) 0)); +} + + int set_var_user::update(THD *thd) { if (user_var_item->update()) diff --git a/sql/set_var.h b/sql/set_var.h index 1cac2953a21..699f320bbd9 100644 --- a/sql/set_var.h +++ b/sql/set_var.h @@ -688,6 +688,8 @@ public: virtual ~set_var_base() {} virtual int check(THD *thd)=0; /* To check privileges etc. */ virtual int update(THD *thd)=0; /* To set the value */ + /* light check for PS */ + virtual int light_check(THD *thd) { return check(thd); } }; @@ -728,6 +730,7 @@ public: } int check(THD *thd); int update(THD *thd); + int light_check(THD *thd); }; @@ -742,6 +745,7 @@ public: {} int check(THD *thd); int update(THD *thd); + int light_check(THD *thd); }; /* For SET PASSWORD */ diff --git a/sql/share/charsets/Index.xml b/sql/share/charsets/Index.xml index bb4b6bd24af..44eb6f386d4 100644 --- a/sql/share/charsets/Index.xml +++ b/sql/share/charsets/Index.xml @@ -36,7 +36,7 @@ To make maintaining easier please: <alias>iso_8859-2</alias> <alias>iso_8859-2:1987</alias> <alias>l2</alias> - <collation name="latin2_czech_ci" id="2" order="Czech" flag="compiled"/> + <collation name="latin2_czech_cs" id="2" order="Czech" flag="compiled"/> <collation name="latin2_general_ci" id="9" flag="primary"> <order>Hungarian</order> <order>Polish</order> @@ -349,7 +349,7 @@ To make maintaining easier please: <order>Slovenian</order> <order>Sorbian</order> </collation> - <collation name="cp1250_czech_ci" id="34" order="Czech"> + <collation name="cp1250_czech_cs" id="34" order="Czech"> <flag>compiled</flag> </collation> <collation name="cp1250_bin" id="66" order="Binary" flag="binary"/> diff --git a/sql/share/czech/errmsg.txt b/sql/share/czech/errmsg.txt index 423c3090b8a..5c02d9b2aeb 100644 --- a/sql/share/czech/errmsg.txt +++ b/sql/share/czech/errmsg.txt @@ -307,6 +307,7 @@ character-set=latin2 "Truncated wrong %-.32s value: '%-.128s'" "Incorrect table definition; There can only be one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause" "Invalid ON UPDATE clause for '%-.64s' field", +"This command is not supported in the prepared statement protocol yet", "Can't create a %s from within another stored routine" "%s %s already exists" "%s %s does not exist" diff --git a/sql/share/danish/errmsg.txt b/sql/share/danish/errmsg.txt index ce444c8fad1..c61859645fe 100644 --- a/sql/share/danish/errmsg.txt +++ b/sql/share/danish/errmsg.txt @@ -301,6 +301,7 @@ character-set=latin1 "Truncated wrong %-.32s value: '%-.128s'" "Incorrect table definition; There can only be one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause" "Invalid ON UPDATE clause for '%-.64s' field", +"This command is not supported in the prepared statement protocol yet", "Can't create a %s from within another stored routine" "%s %s already exists" "%s %s does not exist" diff --git a/sql/share/dutch/errmsg.txt b/sql/share/dutch/errmsg.txt index 5ae00ef6421..61cc99478bd 100644 --- a/sql/share/dutch/errmsg.txt +++ b/sql/share/dutch/errmsg.txt @@ -309,6 +309,7 @@ character-set=latin1 "Truncated wrong %-.32s value: '%-.128s'" "Incorrect table definition; There can only be one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause" "Invalid ON UPDATE clause for '%-.64s' field", +"This command is not supported in the prepared statement protocol yet", "Can't create a %s from within another stored routine" "%s %s already exists" "%s %s does not exist" diff --git a/sql/share/english/errmsg.txt b/sql/share/english/errmsg.txt index 234d9cf3916..cceca3018f9 100644 --- a/sql/share/english/errmsg.txt +++ b/sql/share/english/errmsg.txt @@ -298,6 +298,7 @@ character-set=latin1 "Truncated wrong %-.32s value: '%-.128s'" "Incorrect table definition; There can only be one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause" "Invalid ON UPDATE clause for '%-.64s' field", +"This command is not supported in the prepared statement protocol yet", "Can't create a %s from within another stored routine" "%s %s already exists" "%s %s does not exist" diff --git a/sql/share/estonian/errmsg.txt b/sql/share/estonian/errmsg.txt index 275abf8690e..16789e2ffa0 100644 --- a/sql/share/estonian/errmsg.txt +++ b/sql/share/estonian/errmsg.txt @@ -303,6 +303,7 @@ character-set=latin7 "Truncated wrong %-.32s value: '%-.128s'" "Incorrect table definition; There can only be one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause" "Invalid ON UPDATE clause for '%-.64s' field", +"This command is not supported in the prepared statement protocol yet", "Can't create a %s from within another stored routine" "%s %s already exists" "%s %s does not exist" diff --git a/sql/share/french/errmsg.txt b/sql/share/french/errmsg.txt index 2fb86fbf917..86633b43b2e 100644 --- a/sql/share/french/errmsg.txt +++ b/sql/share/french/errmsg.txt @@ -298,6 +298,7 @@ character-set=latin1 "Truncated wrong %-.32s value: '%-.128s'" "Incorrect table definition; There can only be one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause" "Invalid ON UPDATE clause for '%-.64s' field", +"This command is not supported in the prepared statement protocol yet", "Can't create a %s from within another stored routine" "%s %s already exists" "%s %s does not exist" diff --git a/sql/share/german/errmsg.txt b/sql/share/german/errmsg.txt index 3e4eaa445f8..7b857a46612 100644 --- a/sql/share/german/errmsg.txt +++ b/sql/share/german/errmsg.txt @@ -310,6 +310,7 @@ character-set=latin1 "Truncated wrong %-.32s value: '%-.128s'" "Incorrect table definition; There can only be one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause" "Invalid ON UPDATE clause for '%-.64s' field", +"This command is not supported in the prepared statement protocol yet", "Can't create a %s from within another stored routine" "%s %s already exists" "%s %s does not exist" diff --git a/sql/share/greek/errmsg.txt b/sql/share/greek/errmsg.txt index c287c5fdc9d..b7ee97cfaca 100644 --- a/sql/share/greek/errmsg.txt +++ b/sql/share/greek/errmsg.txt @@ -298,6 +298,7 @@ character-set=greek "Truncated wrong %-.32s value: '%-.128s'" "Incorrect table definition; There can only be one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause" "Invalid ON UPDATE clause for '%-.64s' field", +"This command is not supported in the prepared statement protocol yet", "Can't create a %s from within another stored routine" "%s %s already exists" "%s %s does not exist" diff --git a/sql/share/hungarian/errmsg.txt b/sql/share/hungarian/errmsg.txt index 80e84c2da72..62355b11afb 100644 --- a/sql/share/hungarian/errmsg.txt +++ b/sql/share/hungarian/errmsg.txt @@ -300,6 +300,7 @@ character-set=latin2 "Truncated wrong %-.32s value: '%-.128s'" "Incorrect table definition; There can only be one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause" "Invalid ON UPDATE clause for '%-.64s' field", +"This command is not supported in the prepared statement protocol yet", "Can't create a %s from within another stored routine" "%s %s already exists" "%s %s does not exist" diff --git a/sql/share/italian/errmsg.txt b/sql/share/italian/errmsg.txt index ef41c493622..3c765793579 100644 --- a/sql/share/italian/errmsg.txt +++ b/sql/share/italian/errmsg.txt @@ -298,6 +298,7 @@ character-set=latin1 "Truncated wrong %-.32s value: '%-.128s'" "Incorrect table definition; There can only be one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause" "Invalid ON UPDATE clause for '%-.64s' field", +"This command is not supported in the prepared statement protocol yet", "Can't create a %s from within another stored routine" "%s %s already exists" "%s %s does not exist" diff --git a/sql/share/japanese/errmsg.txt b/sql/share/japanese/errmsg.txt index 60e390b02ff..6ce309c9f36 100644 --- a/sql/share/japanese/errmsg.txt +++ b/sql/share/japanese/errmsg.txt @@ -300,6 +300,7 @@ character-set=ujis "Truncated wrong %-.32s value: '%-.128s'" "Incorrect table definition; There can only be one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause" "Invalid ON UPDATE clause for '%-.64s' field", +"This command is not supported in the prepared statement protocol yet", "Can't create a %s from within another stored routine" "%s %s already exists" "%s %s does not exist" diff --git a/sql/share/korean/errmsg.txt b/sql/share/korean/errmsg.txt index 1797a7c8e2a..1cc4b01dc9b 100644 --- a/sql/share/korean/errmsg.txt +++ b/sql/share/korean/errmsg.txt @@ -298,6 +298,7 @@ character-set=euckr "Truncated wrong %-.32s value: '%-.128s'" "Incorrect table definition; There can only be one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause" "Invalid ON UPDATE clause for '%-.64s' field", +"This command is not supported in the prepared statement protocol yet", "Can't create a %s from within another stored routine" "%s %s already exists" "%s %s does not exist" diff --git a/sql/share/norwegian-ny/errmsg.txt b/sql/share/norwegian-ny/errmsg.txt index 498b65715b8..5a5b092b102 100644 --- a/sql/share/norwegian-ny/errmsg.txt +++ b/sql/share/norwegian-ny/errmsg.txt @@ -300,6 +300,7 @@ character-set=latin1 "Truncated wrong %-.32s value: '%-.128s'" "Incorrect table definition; There can only be one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause" "Invalid ON UPDATE clause for '%-.64s' field", +"This command is not supported in the prepared statement protocol yet", "Can't create a %s from within another stored routine" "%s %s already exists" "%s %s does not exist" diff --git a/sql/share/norwegian/errmsg.txt b/sql/share/norwegian/errmsg.txt index 74f4acd43f0..83ba716a3ac 100644 --- a/sql/share/norwegian/errmsg.txt +++ b/sql/share/norwegian/errmsg.txt @@ -300,6 +300,7 @@ character-set=latin1 "Truncated wrong %-.32s value: '%-.128s'" "Incorrect table definition; There can only be one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause" "Invalid ON UPDATE clause for '%-.64s' field", +"This command is not supported in the prepared statement protocol yet", "Can't create a %s from within another stored routine" "%s %s already exists" "%s %s does not exist" diff --git a/sql/share/polish/errmsg.txt b/sql/share/polish/errmsg.txt index e9f6e6a349e..ae89a3077e2 100644 --- a/sql/share/polish/errmsg.txt +++ b/sql/share/polish/errmsg.txt @@ -302,6 +302,7 @@ character-set=latin2 "Truncated wrong %-.32s value: '%-.128s'" "Incorrect table definition; There can only be one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause" "Invalid ON UPDATE clause for '%-.64s' field", +"This command is not supported in the prepared statement protocol yet", "Can't create a %s from within another stored routine" "%s %s already exists" "%s %s does not exist" diff --git a/sql/share/portuguese/errmsg.txt b/sql/share/portuguese/errmsg.txt index 7056a5bb169..c095f856c6a 100644 --- a/sql/share/portuguese/errmsg.txt +++ b/sql/share/portuguese/errmsg.txt @@ -299,6 +299,7 @@ character-set=latin1 "Truncado errado %-.32s valor: '%-.128s'" "Incorreta definição de tabela; Pode ter somente uma coluna TIMESTAMP com CURRENT_TIMESTAMP em DEFAULT ou ON UPDATE cláusula" "Inválida cláusula ON UPDATE para campo '%-.64s'", +"This command is not supported in the prepared statement protocol yet", "Can't create a %s from within another stored routine" "%s %s already exists" "%s %s does not exist" diff --git a/sql/share/romanian/errmsg.txt b/sql/share/romanian/errmsg.txt index c1f7cbf9914..e2b5642c004 100644 --- a/sql/share/romanian/errmsg.txt +++ b/sql/share/romanian/errmsg.txt @@ -302,6 +302,7 @@ character-set=latin2 "Truncated wrong %-.32s value: '%-.128s'" "Incorrect table definition; There can only be one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause" "Invalid ON UPDATE clause for '%-.64s' field", +"This command is not supported in the prepared statement protocol yet", "Can't create a %s from within another stored routine" "%s %s already exists" "%s %s does not exist" diff --git a/sql/share/russian/errmsg.txt b/sql/share/russian/errmsg.txt index 86a4424b8f1..11e7f099a76 100644 --- a/sql/share/russian/errmsg.txt +++ b/sql/share/russian/errmsg.txt @@ -300,6 +300,7 @@ character-set=koi8r "Truncated wrong %-.32s value: '%-.128s'" "Incorrect table definition; There can only be one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause" "Invalid ON UPDATE clause for '%-.64s' field", +"This command is not supported in the prepared statement protocol yet", "Can't create a %s from within another stored routine" "%s %s already exists" "%s %s does not exist" diff --git a/sql/share/serbian/errmsg.txt b/sql/share/serbian/errmsg.txt index 957c7650a87..f440323cafb 100644 --- a/sql/share/serbian/errmsg.txt +++ b/sql/share/serbian/errmsg.txt @@ -292,6 +292,7 @@ character-set=cp1250 "Truncated wrong %-.32s value: '%-.128s'" "Incorrect table definition; There can only be one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause" "Invalid ON UPDATE clause for '%-.64s' field", +"This command is not supported in the prepared statement protocol yet", "Can't create a %s from within another stored routine" "%s %s already exists" "%s %s does not exist" diff --git a/sql/share/slovak/errmsg.txt b/sql/share/slovak/errmsg.txt index feddd50b585..2a8df8db994 100644 --- a/sql/share/slovak/errmsg.txt +++ b/sql/share/slovak/errmsg.txt @@ -306,6 +306,7 @@ character-set=latin2 "Truncated wrong %-.32s value: '%-.128s'" "Incorrect table definition; There can only be one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause" "Invalid ON UPDATE clause for '%-.64s' field", +"This command is not supported in the prepared statement protocol yet", "Can't create a %s from within another stored routine" "%s %s already exists" "%s %s does not exist" diff --git a/sql/share/spanish/errmsg.txt b/sql/share/spanish/errmsg.txt index f309b8108bb..d4009920aa3 100644 --- a/sql/share/spanish/errmsg.txt +++ b/sql/share/spanish/errmsg.txt @@ -300,6 +300,7 @@ character-set=latin1 "Equivocado truncado %-.32s valor: '%-.128s'" "Incorrecta definición de tabla; Solamente debe haber una columna TIMESTAMP con CURRENT_TIMESTAMP en DEFAULT o ON UPDATE cláusula" "Inválido ON UPDATE cláusula para campo '%-.64s'", +"This command is not supported in the prepared statement protocol yet", "Can't create a %s from within another stored routine" "%s %s already exists" "%s %s does not exist" diff --git a/sql/share/swedish/errmsg.txt b/sql/share/swedish/errmsg.txt index 4761a6c15c1..32e5e9c58dc 100644 --- a/sql/share/swedish/errmsg.txt +++ b/sql/share/swedish/errmsg.txt @@ -298,6 +298,7 @@ character-set=latin1 "Truncated wrong %-.32s value: '%-.128s'" "Incorrect table definition; There can only be one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause" "Invalid ON UPDATE clause for '%-.64s' field", +"This command is not supported in the prepared statement protocol yet", "Can't create a %s from within another stored routine" "%s %s already exists" "%s %s does not exist" diff --git a/sql/share/ukrainian/errmsg.txt b/sql/share/ukrainian/errmsg.txt index 2495c2676be..1dba857894a 100644 --- a/sql/share/ukrainian/errmsg.txt +++ b/sql/share/ukrainian/errmsg.txt @@ -303,6 +303,7 @@ character-set=koi8u "Truncated wrong %-.32s value: '%-.128s'" "Incorrect table definition; There can only be one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause" "Invalid ON UPDATE clause for '%-.64s' field", +"This command is not supported in the prepared statement protocol yet", "Can't create a %s from within another stored routine" "%s %s already exists" "%s %s does not exist" diff --git a/sql/slave.cc b/sql/slave.cc index ad4cc820409..3fddc4dc783 100644 --- a/sql/slave.cc +++ b/sql/slave.cc @@ -39,7 +39,7 @@ HASH replicate_do_table, replicate_ignore_table; DYNAMIC_ARRAY replicate_wild_do_table, replicate_wild_ignore_table; bool do_table_inited = 0, ignore_table_inited = 0; bool wild_do_table_inited = 0, wild_ignore_table_inited = 0; -bool table_rules_on = 0; +bool table_rules_on= 0, replicate_same_server_id; ulonglong relay_log_space_limit = 0; /* @@ -463,6 +463,55 @@ void init_slave_skip_errors(const char* arg) } } + +void st_relay_log_info::inc_group_relay_log_pos(ulonglong val, + ulonglong log_pos, + bool skip_lock) +{ + if (!skip_lock) + pthread_mutex_lock(&data_lock); + inc_event_relay_log_pos(val); + group_relay_log_pos= event_relay_log_pos; + strmake(group_relay_log_name,event_relay_log_name, + sizeof(group_relay_log_name)-1); + + notify_group_relay_log_name_update(); + + /* + If the slave does not support transactions and replicates a transaction, + users should not trust group_master_log_pos (which they can display with + SHOW SLAVE STATUS or read from relay-log.info), because to compute + group_master_log_pos the slave relies on log_pos stored in the master's + binlog, but if we are in a master's transaction these positions are always + the BEGIN's one (excepted for the COMMIT), so group_master_log_pos does + not advance as it should on the non-transactional slave (it advances by + big leaps, whereas it should advance by small leaps). + */ + if (log_pos) // 3.23 binlogs don't have log_posx + { +#if MYSQL_VERSION_ID < 50000 + /* + If the event was converted from a 3.23 format, get_event_len() has + grown by 6 bytes (at least for most events, except LOAD DATA INFILE + which is already a big problem for 3.23->4.0 replication); 6 bytes is + the difference between the header's size in 4.0 (LOG_EVENT_HEADER_LEN) + and the header's size in 3.23 (OLD_HEADER_LEN). Note that using + mi->old_format will not help if the I/O thread has not started yet. + Yes this is a hack but it's just to make 3.23->4.x replication work; + 3.23->5.0 replication is working much better. + */ + group_master_log_pos= log_pos + val - + (mi->old_format ? (LOG_EVENT_HEADER_LEN - OLD_HEADER_LEN) : 0); +#else + group_master_log_pos= log_pos+ val; +#endif /* MYSQL_VERSION_ID < 5000 */ + } + pthread_cond_broadcast(&data_cond); + if (!skip_lock) + pthread_mutex_unlock(&data_lock); +} + + void st_relay_log_info::close_temporary_tables() { TABLE *table,*next; @@ -802,7 +851,17 @@ static TABLE_RULE_ENT* find_wild(DYNAMIC_ARRAY *a, const char* key, int len) Note that changing the order of the tables in the list can lead to different results. Note also the order of precedence of the do/ignore rules (see code below). For that reason, users should not set conflicting - rules because they may get unpredicted results. + rules because they may get unpredicted results (precedence order is + explained in the manual). + If no table of the list is marked "updating" (so far this can only happen + if the statement is a multi-delete (SQLCOM_DELETE_MULTI) and the "tables" + is the tables in the FROM): then we always return 0, because there is no + reason we play this statement on this slave if it updates nothing. In the + case of SQLCOM_DELETE_MULTI, there will be a second call to tables_ok(), + with tables having "updating==TRUE" (those after the DELETE), so this + second call will make the decision (because + all_tables_not_ok() = !tables_ok(1st_list) && !tables_ok(2nd_list)). + Thought which arose from a question of a big customer "I want to include all tables like "abc.%" except the "%.EFG"". This can't be done now. If we supported Perl regexps we could do it with this pattern: /^abc\.(?!EFG)/ @@ -815,6 +874,7 @@ static TABLE_RULE_ENT* find_wild(DYNAMIC_ARRAY *a, const char* key, int len) int tables_ok(THD* thd, TABLE_LIST* tables) { + bool some_tables_updating= 0; DBUG_ENTER("tables_ok"); for (; tables; tables = tables->next) @@ -825,6 +885,7 @@ int tables_ok(THD* thd, TABLE_LIST* tables) if (!tables->updating) continue; + some_tables_updating= 1; end= strmov(hash_key, tables->db ? tables->db : thd->db); *end++= '.'; len= (uint) (strmov(end, tables->real_name) - hash_key); @@ -847,10 +908,13 @@ int tables_ok(THD* thd, TABLE_LIST* tables) } /* + If no table was to be updated, ignore statement (no reason we play it on + slave, slave is supposed to replicate _changes_ only). If no explicit rule found and there was a do list, do not replicate. If there was no do list, go ahead */ - DBUG_RETURN(!do_table_inited && !wild_do_table_inited); + DBUG_RETURN(some_tables_updating && + !do_table_inited && !wild_do_table_inited); } @@ -1645,13 +1709,13 @@ Failed to open the existing relay log info file '%s' (errno %d)", rli->info_fd = info_fd; int relay_log_pos, master_log_pos; if (init_strvar_from_file(rli->group_relay_log_name, - sizeof(rli->group_relay_log_name), &rli->info_file, - "") || + sizeof(rli->group_relay_log_name), + &rli->info_file, "") || init_intvar_from_file(&relay_log_pos, &rli->info_file, BIN_LOG_HEADER_SIZE) || init_strvar_from_file(rli->group_master_log_name, - sizeof(rli->group_master_log_name), &rli->info_file, - "") || + sizeof(rli->group_master_log_name), + &rli->info_file, "") || init_intvar_from_file(&master_log_pos, &rli->info_file, 0)) { msg="Error reading slave log configuration"; @@ -2358,7 +2422,8 @@ st_relay_log_info::st_relay_log_info() inited(0), abort_slave(0), slave_running(0), until_condition(UNTIL_NONE), until_log_pos(0) { - group_relay_log_name[0]= event_relay_log_name[0]= group_master_log_name[0]= 0; + group_relay_log_name[0]= event_relay_log_name[0]= + group_master_log_name[0]= 0; last_slave_error[0]=0; until_log_name[0]= 0; bzero((char*) &info_file, sizeof(info_file)); @@ -2460,6 +2525,8 @@ int st_relay_log_info::wait_for_pos(THD* thd, String* log_name, error= -2; //means improper arguments goto err; } + // Convert 0-3 to 4 + log_pos= max(log_pos, BIN_LOG_HEADER_SIZE); /* p points to '.' */ log_name_extension= strtoul(++p, &p_end, 10); /* @@ -2480,14 +2547,24 @@ int st_relay_log_info::wait_for_pos(THD* thd, String* log_name, init_abort_pos_wait == abort_pos_wait && slave_running) { + /* - If we are after RESET SLAVE, and the SQL slave thread has not processed - any event yet, it could be that group_master_log_name is "". In that case, - just wait for more events (as there is no sensible comparison to do). + group_master_log_name can be "", if we are just after a fresh + replication start or after a CHANGE MASTER TO MASTER_HOST/PORT + (before we have executed one Rotate event from the master) or + (rare) if the user is doing a weird slave setup (see next + paragraph). If group_master_log_name is "", we assume we don't + have enough info to do the comparison yet, so we just wait until + more data. In this case master_log_pos is always 0 except if + somebody (wrongly) sets this slave to be a slave of itself + without using --replicate-same-server-id (an unsupported + configuration which does nothing), then group_master_log_pos + will grow and group_master_log_name will stay "". */ if (*group_master_log_name) { - char *basename= group_master_log_name + dirname_length(group_master_log_name); + char *basename= (group_master_log_name + + dirname_length(group_master_log_name)); /* First compare the parts before the extension. Find the dot in the master's log basename, @@ -2504,14 +2581,16 @@ int st_relay_log_info::wait_for_pos(THD* thd, String* log_name, char *q_end; ulong group_master_log_name_extension= strtoul(q, &q_end, 10); if (group_master_log_name_extension < log_name_extension) - cmp_result = -1 ; + cmp_result= -1 ; else - cmp_result= (group_master_log_name_extension > log_name_extension) ? - 1 : 0 ; - if (((!cmp_result && group_master_log_pos >= (ulonglong)log_pos) || - cmp_result > 0) || thd->killed) + cmp_result= (group_master_log_name_extension > log_name_extension) ? 1 : 0 ; + + pos_reached= ((!cmp_result && group_master_log_pos >= (ulonglong)log_pos) || + cmp_result > 0); + if (pos_reached || thd->killed) break; } + //wait for master update, with optional timeout. DBUG_PRINT("info",("Waiting for master update")); @@ -2945,6 +3024,7 @@ static int exec_relay_log_event(THD* thd, RELAY_LOG_INFO* rli) DBUG_PRINT("info",("type_code=%d, server_id=%d",type_code,ev->server_id)); if ((ev->server_id == (uint32) ::server_id && + !replicate_same_server_id && type_code!= FORMAT_DESCRIPTION_EVENT) || (rli->slave_skip_counter && type_code != ROTATE_EVENT && type_code != STOP_EVENT && @@ -3738,7 +3818,15 @@ static int queue_binlog_ver_1_event(MASTER_INFO *mi, const char *buf, DBUG_RETURN(1); } memcpy(tmp_buf,buf,event_len); - tmp_buf[event_len]=0; // Create_file constructor wants null-term buffer + /* + Create_file constructor wants a 0 as last char of buffer, this 0 will + serve as the string-termination char for the file's name (which is at the + end of the buffer) + We must increment event_len, otherwise the event constructor will not see + this end 0, which leads to segfault. + */ + tmp_buf[event_len++]=0; + int4store(tmp_buf+EVENT_LEN_OFFSET, event_len); buf = (const char*)tmp_buf; } /* @@ -4016,7 +4104,8 @@ int queue_event(MASTER_INFO* mi,const char* buf, ulong event_len) direct master (an unsupported, useless setup!). */ - if (uint4korr(buf + SERVER_ID_OFFSET) == ::server_id) + if ((uint4korr(buf + SERVER_ID_OFFSET) == ::server_id) && + !replicate_same_server_id) { /* Do not write it to the relay log. diff --git a/sql/slave.h b/sql/slave.h index f4080f3ca8d..157111e54f0 100644 --- a/sql/slave.h +++ b/sql/slave.h @@ -283,63 +283,12 @@ typedef struct st_relay_log_info until_log_names_cmp_result= UNTIL_LOG_NAMES_CMP_UNKNOWN; } - inline void inc_event_relay_log_pos() + inline void inc_event_relay_log_pos(ulonglong val) { - event_relay_log_pos= future_event_relay_log_pos; - } - - void inc_group_relay_log_pos(ulonglong log_pos, - bool skip_lock=0) - { - if (!skip_lock) - pthread_mutex_lock(&data_lock); - inc_event_relay_log_pos(); - group_relay_log_pos= event_relay_log_pos; - strmake(group_relay_log_name,event_relay_log_name, - sizeof(group_relay_log_name)-1); - - notify_group_relay_log_name_update(); - - /* - If the slave does not support transactions and replicates a transaction, - users should not trust group_master_log_pos (which they can display with - SHOW SLAVE STATUS or read from relay-log.info), because to compute - group_master_log_pos the slave relies on log_pos stored in the master's - binlog, but if we are in a master's transaction these positions are always - the BEGIN's one (excepted for the COMMIT), so group_master_log_pos does - not advance as it should on the non-transactional slave (it advances by - big leaps, whereas it should advance by small leaps). - */ - /* - In 4.x we used the event's len to compute the positions here. This is - wrong if the event was 3.23/4.0 and has been converted to 5.0, because - then the event's len is not what is was in the master's binlog, so this - will make a wrong group_master_log_pos (yes it's a bug in 3.23->4.0 - replication: Exec_master_log_pos is wrong). Only way to solve this is to - have the original offset of the end of the event the relay log. This is - what we do in 5.0: log_pos has become "end_log_pos" (because the real use - of log_pos in 4.0 was to compute the end_log_pos; so better to store - end_log_pos instead of begin_log_pos. - If we had not done this fix here, the problem would also have appeared - when the slave and master are 5.0 but with different event length (for - example the slave is more recent than the master and features the event - UID). It would give false MASTER_POS_WAIT, false Exec_master_log_pos in - SHOW SLAVE STATUS, and so the user would do some CHANGE MASTER using this - value which would lead to badly broken replication. - Even the relay_log_pos will be corrupted in this case, because the len is - the relay log is not "val". - With the end_log_pos solution, we avoid computations involving lengthes. - */ - DBUG_PRINT("info", ("log_pos=%lld group_master_log_pos=%lld", - log_pos,group_master_log_pos)); - if (log_pos) // some events (like fake Rotate) don't have log_pos - // when we are here, log_pos is the end of the event - group_master_log_pos= log_pos; - pthread_cond_broadcast(&data_cond); - if (!skip_lock) - pthread_mutex_unlock(&data_lock); + event_relay_log_pos+= val; } + void inc_group_relay_log_pos(ulonglong val, ulonglong log_pos, bool skip_lock=0); int wait_for_pos(THD* thd, String* log_name, longlong log_pos, longlong timeout); void close_temporary_tables(); @@ -578,7 +527,7 @@ extern HASH replicate_do_table, replicate_ignore_table; extern DYNAMIC_ARRAY replicate_wild_do_table, replicate_wild_ignore_table; extern bool do_table_inited, ignore_table_inited, wild_do_table_inited, wild_ignore_table_inited; -extern bool table_rules_on; +extern bool table_rules_on, replicate_same_server_id; extern int disconnect_slave_event_count, abort_slave_event_count ; diff --git a/sql/spatial.h b/sql/spatial.h index cf07b364bb3..cc1cc70f1bc 100644 --- a/sql/spatial.h +++ b/sql/spatial.h @@ -217,7 +217,7 @@ public: if (!(ci= find_class((int) type_id))) return NULL; (*ci->m_create_func)((void *)buffer); - return (Geometry *)buffer; + return my_reinterpret_cast(Geometry *)(buffer); } static Geometry *create_from_wkb(Geometry_buffer *buffer, diff --git a/sql/sql_acl.cc b/sql/sql_acl.cc index ef55eaf8ec6..b532da9006e 100644 --- a/sql/sql_acl.cc +++ b/sql/sql_acl.cc @@ -505,7 +505,7 @@ static ulong get_access(TABLE *form, uint fieldnr) ((Field_enum*) (*pos))->typelib->count == 2 ; pos++ , bit<<=1) { - (*pos)->val_str(&res,&res); + (*pos)->val_str(&res); if (my_toupper(&my_charset_latin1, res[0]) == 'Y') access_bits|= bit; } @@ -930,11 +930,11 @@ static void acl_insert_user(const char *user, const char *host, { ACL_USER acl_user; acl_user.user=*user ? strdup_root(&mem,user) : 0; - update_hostname(&acl_user.host,strdup_root(&mem,host)); + update_hostname(&acl_user.host, *host ? strdup_root(&mem, host): 0); acl_user.access=privileges; acl_user.user_resource = *mqh; acl_user.sort=get_sort(2,acl_user.host.hostname,acl_user.user); - acl_user.hostname_length=(uint) strlen(acl_user.host.hostname); + acl_user.hostname_length=(uint) strlen(host); acl_user.ssl_type= (ssl_type != SSL_TYPE_NOT_SPECIFIED ? ssl_type : SSL_TYPE_NONE); acl_user.ssl_cipher= ssl_cipher ? strdup_root(&mem,ssl_cipher) : 0; @@ -1467,7 +1467,7 @@ static bool test_if_create_new_users(THD *thd) thd->priv_user, tl.db, 0); if (!(db_access & INSERT_ACL)) { - if (check_grant(thd,INSERT_ACL,&tl,0,1)) + if (check_grant(thd, INSERT_ACL, &tl, 0, UINT_MAX, 1)) create_new_users=0; } } @@ -1639,7 +1639,8 @@ end: { acl_cache->clear(1); // Clear privilege cache if (old_row_exists) - acl_update_user(combo.user.str, combo.host.str, password, password_len, + acl_update_user(combo.user.str, combo.host.str, + combo.password.str, password_len, thd->lex->ssl_type, thd->lex->ssl_cipher, thd->lex->x509_issuer, @@ -1783,107 +1784,124 @@ static byte* get_key_column(GRANT_COLUMN *buff,uint *length, class GRANT_TABLE :public Sql_alloc { public: - char *host,*db,*user,*tname, *hash_key; + char *host,*db, *user, *tname, *hash_key, *orig_host; ulong privs, cols; ulong sort; uint key_length; HASH hash_columns; - GRANT_TABLE (const char *h, const char *d,const char *u, const char *t, - ulong p, ulong c) - : privs(p), cols(c) - { - host = strdup_root(&memex,h); - db = strdup_root(&memex,d); - user = strdup_root(&memex,u); - sort= get_sort(3,host,db,user); - tname= strdup_root(&memex,t); - if (lower_case_table_names) - { - my_casedn_str(&my_charset_latin1, db); - my_casedn_str(&my_charset_latin1, tname); - } - key_length =(uint) strlen(d)+(uint) strlen(u)+(uint) strlen(t)+3; - hash_key = (char*) alloc_root(&memex,key_length); - strmov(strmov(strmov(hash_key,user)+1,db)+1,tname); - (void) hash_init(&hash_columns,&my_charset_latin1, - 0,0,0, (hash_get_key) get_key_column,0,0); - } - - GRANT_TABLE (TABLE *form, TABLE *col_privs) - { - byte key[MAX_KEY_LENGTH]; - - host = get_field(&memex,form->field[0]); - db = get_field(&memex,form->field[1]); - user = get_field(&memex,form->field[2]); - if (!user) - user=(char*) ""; - sort= get_sort(3,host,db,user); - tname= get_field(&memex,form->field[3]); - if (!host || !db || !tname) - { - /* Wrong table row; Ignore it */ - privs = cols = 0; /* purecov: inspected */ - return; /* purecov: inspected */ - } - if (lower_case_table_names) - { - my_casedn_str(&my_charset_latin1, db); - my_casedn_str(&my_charset_latin1, tname); - } - key_length = ((uint) strlen(db) + (uint) strlen(user) + - (uint) strlen(tname) + 3); - hash_key = (char*) alloc_root(&memex,key_length); - strmov(strmov(strmov(hash_key,user)+1,db)+1,tname); - privs = (ulong) form->field[6]->val_int(); - cols = (ulong) form->field[7]->val_int(); - privs = fix_rights_for_table(privs); - cols = fix_rights_for_column(cols); - - (void) hash_init(&hash_columns,&my_charset_latin1, - 0,0,0, (hash_get_key) get_key_column,0,0); - if (cols) - { - int key_len; - col_privs->field[0]->store(host,(uint) strlen(host), &my_charset_latin1); - col_privs->field[1]->store(db,(uint) strlen(db), &my_charset_latin1); - col_privs->field[2]->store(user,(uint) strlen(user), &my_charset_latin1); - col_privs->field[3]->store(tname,(uint) strlen(tname), &my_charset_latin1); - key_len=(col_privs->field[0]->pack_length()+ - col_privs->field[1]->pack_length()+ - col_privs->field[2]->pack_length()+ - col_privs->field[3]->pack_length()); - key_copy(key,col_privs,0,key_len); - col_privs->field[4]->store("",0, &my_charset_latin1); - col_privs->file->index_init(0); - if (col_privs->file->index_read(col_privs->record[0], - (byte*) col_privs->field[0]->ptr, - key_len, HA_READ_KEY_EXACT)) + + GRANT_TABLE(const char *h, const char *d,const char *u, + const char *t, ulong p, ulong c); + GRANT_TABLE (TABLE *form, TABLE *col_privs); + bool ok() { return privs != 0 || cols != 0; } +}; + + + +GRANT_TABLE::GRANT_TABLE(const char *h, const char *d,const char *u, + const char *t, ulong p, ulong c) + :privs(p), cols(c) +{ + /* Host given by user */ + orig_host= strdup_root(&memex,h); + /* Convert empty hostname to '%' for easy comparision */ + host= orig_host[0] ? orig_host : (char*) "%"; + db = strdup_root(&memex,d); + user = strdup_root(&memex,u); + sort= get_sort(3,host,db,user); + tname= strdup_root(&memex,t); + if (lower_case_table_names) + { + my_casedn_str(&my_charset_latin1, db); + my_casedn_str(&my_charset_latin1, tname); + } + key_length =(uint) strlen(d)+(uint) strlen(u)+(uint) strlen(t)+3; + hash_key = (char*) alloc_root(&memex,key_length); + strmov(strmov(strmov(hash_key,user)+1,db)+1,tname); + (void) hash_init(&hash_columns,&my_charset_latin1, + 0,0,0, (hash_get_key) get_key_column,0,0); +} + + +GRANT_TABLE::GRANT_TABLE(TABLE *form, TABLE *col_privs) +{ + byte key[MAX_KEY_LENGTH]; + + orig_host= host= get_field(&memex, form->field[0]); + db= get_field(&memex,form->field[1]); + user= get_field(&memex,form->field[2]); + if (!user) + user= (char*) ""; + if (!orig_host) + { + orig_host= (char*) ""; + host= (char*) "%"; + } + sort= get_sort(3, orig_host, db, user); + tname= get_field(&memex,form->field[3]); + if (!db || !tname) + { + /* Wrong table row; Ignore it */ + privs = cols = 0; /* purecov: inspected */ + return; /* purecov: inspected */ + } + if (lower_case_table_names) + { + my_casedn_str(&my_charset_latin1, db); + my_casedn_str(&my_charset_latin1, tname); + } + key_length = ((uint) strlen(db) + (uint) strlen(user) + + (uint) strlen(tname) + 3); + hash_key = (char*) alloc_root(&memex,key_length); + strmov(strmov(strmov(hash_key,user)+1,db)+1,tname); + privs = (ulong) form->field[6]->val_int(); + cols = (ulong) form->field[7]->val_int(); + privs = fix_rights_for_table(privs); + cols = fix_rights_for_column(cols); + + (void) hash_init(&hash_columns,&my_charset_latin1, + 0,0,0, (hash_get_key) get_key_column,0,0); + if (cols) + { + int key_len; + col_privs->field[0]->store(orig_host,(uint) strlen(orig_host), + &my_charset_latin1); + col_privs->field[1]->store(db,(uint) strlen(db), &my_charset_latin1); + col_privs->field[2]->store(user,(uint) strlen(user), &my_charset_latin1); + col_privs->field[3]->store(tname,(uint) strlen(tname), &my_charset_latin1); + key_len=(col_privs->field[0]->pack_length()+ + col_privs->field[1]->pack_length()+ + col_privs->field[2]->pack_length()+ + col_privs->field[3]->pack_length()); + key_copy(key,col_privs,0,key_len); + col_privs->field[4]->store("",0, &my_charset_latin1); + col_privs->file->index_init(0); + if (col_privs->file->index_read(col_privs->record[0], + (byte*) col_privs->field[0]->ptr, + key_len, HA_READ_KEY_EXACT)) + { + cols = 0; /* purecov: deadcode */ + return; + } + do + { + String *res,column_name; + GRANT_COLUMN *mem_check; + /* As column name is a string, we don't have to supply a buffer */ + res=col_privs->field[4]->val_str(&column_name); + ulong priv= (ulong) col_privs->field[6]->val_int(); + if (!(mem_check = new GRANT_COLUMN(*res, + fix_rights_for_column(priv)))) { - cols = 0; /* purecov: deadcode */ - return; + /* Don't use this entry */ + privs = cols = 0; /* purecov: deadcode */ + return; /* purecov: deadcode */ } - do - { - String *res,column_name; - GRANT_COLUMN *mem_check; - /* As column name is a string, we don't have to supply a buffer */ - res=col_privs->field[4]->val_str(&column_name,&column_name); - ulong priv= (ulong) col_privs->field[6]->val_int(); - if (!(mem_check = new GRANT_COLUMN(*res, - fix_rights_for_column(priv)))) - { - /* Don't use this entry */ - privs = cols = 0; /* purecov: deadcode */ - return; /* purecov: deadcode */ - } - my_hash_insert(&hash_columns, (byte *) mem_check); - } while (!col_privs->file->index_next(col_privs->record[0]) && - !key_cmp(col_privs,key,0,key_len)); - } + my_hash_insert(&hash_columns, (byte *) mem_check); + } while (!col_privs->file->index_next(col_privs->record[0]) && + !key_cmp(col_privs,key,0,key_len)); } - bool ok() { return privs != 0 || cols != 0; } -}; +} static byte* get_grant_table(GRANT_TABLE *buff,uint *length, @@ -2071,7 +2089,7 @@ static int replace_column_table(GRANT_TABLE *g_t, privileges&= ~rights; table->field[6]->store((longlong) get_rights_for_column(privileges)); - table->field[4]->val_str(&column_name,&column_name); + table->field[4]->val_str(&column_name); grant_column = column_hash_search(g_t, column_name.ptr(), column_name.length()); @@ -2724,12 +2742,12 @@ void grant_reload(THD *thd) /**************************************************************************** - Check grants + Check table level grants All errors are written directly to the client if no_errors is given ! ****************************************************************************/ bool check_grant(THD *thd, ulong want_access, TABLE_LIST *tables, - uint show_table, bool no_errors) + uint show_table, uint number, bool no_errors) { TABLE_LIST *table; char *user = thd->priv_user; @@ -2739,7 +2757,7 @@ bool check_grant(THD *thd, ulong want_access, TABLE_LIST *tables, return 0; // ok rw_rdlock(&LOCK_grant); - for (table=tables; table ;table=table->next) + for (table= tables; table && number--; table= table->next) { if (!(~table->grant.privilege & want_access) || table->derived) { @@ -2825,9 +2843,9 @@ bool check_grant_column(THD *thd,TABLE *table, const char *name, if (table->grant.version != grant_version) { table->grant.grant_table= - table_hash_search(thd->host,thd->ip,thd->db, + table_hash_search(thd->host, thd->ip, table->table_cache_key, thd->priv_user, - table->real_name,0); /* purecov: inspected */ + table->real_name, 0); /* purecov: inspected */ table->grant.version=grant_version; /* purecov: inspected */ } if (!(grant_table=table->grant.grant_table)) @@ -3076,6 +3094,12 @@ int mysql_show_grants(THD *thd,LEX_USER *lex_user) my_error(ER_OPTION_PREVENTS_STATEMENT, MYF(0), "--skip-grant-tables"); DBUG_RETURN(-1); } + + if (!lex_user->host.str) + { + lex_user->host.str= (char*) "%"; + lex_user->host.length=1; + } if (lex_user->host.length > HOSTNAME_LENGTH || lex_user->user.length > USERNAME_LENGTH) { @@ -3088,9 +3112,9 @@ int mysql_show_grants(THD *thd,LEX_USER *lex_user) const char *user,*host; acl_user=dynamic_element(&acl_users,counter,ACL_USER*); if (!(user=acl_user->user)) - user=""; + user= ""; if (!(host=acl_user->host.hostname)) - host="%"; + host= ""; if (!strcmp(lex_user->user.str,user) && !my_strcasecmp(&my_charset_latin1, lex_user->host.str, host)) break; @@ -3216,13 +3240,13 @@ int mysql_show_grants(THD *thd,LEX_USER *lex_user) /* Add database access */ for (counter=0 ; counter < acl_dbs.elements ; counter++) { - const char *user,*host; + const char *user, *host; acl_db=dynamic_element(&acl_dbs,counter,ACL_DB*); if (!(user=acl_db->user)) - user=""; + user= ""; if (!(host=acl_db->host.hostname)) - host=""; + host= ""; if (!strcmp(lex_user->user.str,user) && !my_strcasecmp(&my_charset_latin1, lex_user->host.str, host)) @@ -3273,20 +3297,19 @@ int mysql_show_grants(THD *thd,LEX_USER *lex_user) } } - /* Add column access */ + /* Add table & column access */ for (index=0 ; index < column_priv_hash.records ; index++) { - const char *user,*host; + const char *user; GRANT_TABLE *grant_table= (GRANT_TABLE*) hash_element(&column_priv_hash, index); if (!(user=grant_table->user)) - user=""; - if (!(host=grant_table->host)) - host=""; + user= ""; if (!strcmp(lex_user->user.str,user) && - !my_strcasecmp(&my_charset_latin1, lex_user->host.str, host)) + !my_strcasecmp(&my_charset_latin1, lex_user->host.str, + grant_table->orig_host)) { ulong table_access= grant_table->privs; if ((table_access | grant_table->cols) != 0) @@ -3303,6 +3326,7 @@ int mysql_show_grants(THD *thd,LEX_USER *lex_user) global.append("USAGE",5); else { + /* Add specific column access */ int found= 0; ulong j; @@ -3483,9 +3507,9 @@ ACL_USER *check_acl_user(LEX_USER *user_name, const char *user,*host; acl_user= dynamic_element(&acl_users, counter, ACL_USER*); if (!(user=acl_user->user)) - user=""; + user= ""; if (!(host=acl_user->host.hostname)) - host="%"; + host= "%"; if (!strcmp(user_name->user.str,user) && !my_strcasecmp(system_charset_info, user_name->host.str, host)) break; @@ -3541,9 +3565,9 @@ int mysql_drop_user(THD *thd, List <LEX_USER> &list) const char *user,*host; acl_db=dynamic_element(&acl_dbs,counter,ACL_DB*); if (!(user= acl_db->user)) - user=""; + user= ""; if (!(host= acl_db->host.hostname)) - host=""; + host= ""; if (!strcmp(user_name->user.str,user) && !my_strcasecmp(system_charset_info, user_name->host.str, host)) @@ -3564,9 +3588,9 @@ int mysql_drop_user(THD *thd, List <LEX_USER> &list) GRANT_TABLE *grant_table= (GRANT_TABLE*) hash_element(&column_priv_hash, counter); if (!(user=grant_table->user)) - user=""; + user= ""; if (!(host=grant_table->host)) - host=""; + host= ""; if (!strcmp(user_name->user.str,user) && !my_strcasecmp(system_charset_info, user_name->host.str, host)) @@ -3654,9 +3678,9 @@ int mysql_revoke_all(THD *thd, List <LEX_USER> &list) acl_db=dynamic_element(&acl_dbs,counter,ACL_DB*); if (!(user=acl_db->user)) - user=""; + user= ""; if (!(host=acl_db->host.hostname)) - host=""; + host= ""; if (!strcmp(lex_user->user.str,user) && !my_strcasecmp(system_charset_info, lex_user->host.str, host)) @@ -3673,9 +3697,9 @@ int mysql_revoke_all(THD *thd, List <LEX_USER> &list) GRANT_TABLE *grant_table= (GRANT_TABLE*) hash_element(&column_priv_hash, counter); if (!(user=grant_table->user)) - user=""; + user= ""; if (!(host=grant_table->host)) - host=""; + host= ""; if (!strcmp(lex_user->user.str,user) && !my_strcasecmp(system_charset_info, lex_user->host.str, host)) diff --git a/sql/sql_acl.h b/sql/sql_acl.h index d2dd5b23f0d..2ea3b8f5628 100644 --- a/sql/sql_acl.h +++ b/sql/sql_acl.h @@ -155,7 +155,7 @@ my_bool grant_init(THD *thd); void grant_free(void); void grant_reload(THD *thd); bool check_grant(THD *thd, ulong want_access, TABLE_LIST *tables, - uint show_command, bool dont_print_error); + uint show_command, uint number, bool dont_print_error); bool check_grant_column (THD *thd,TABLE *table, const char *name, uint length, uint show_command=0); bool check_grant_all_columns(THD *thd, ulong want_access, TABLE *table); @@ -169,6 +169,6 @@ int mysql_drop_user(THD *thd, List <LEX_USER> &list); int mysql_revoke_all(THD *thd, List <LEX_USER> &list); #ifdef NO_EMBEDDED_ACCESS_CHECKS -#define check_grant(A,B,C,D,E) 0 +#define check_grant(A,B,C,D,E,F) 0 #define check_grant_db(A,B) 0 #endif diff --git a/sql/sql_base.cc b/sql/sql_base.cc index 8e96ccad586..190e930deed 100644 --- a/sql/sql_base.cc +++ b/sql/sql_base.cc @@ -444,7 +444,7 @@ bool close_thread_table(THD *thd, TABLE **table_ptr) else { // Free memory and reset for next loop - table->file->extra(HA_EXTRA_RESET); + table->file->reset(); } table->in_use=0; if (unused_tables) @@ -1029,14 +1029,15 @@ bool reopen_table(TABLE *table,bool locked) *table=tmp; table->file->change_table_ptr(table); + DBUG_ASSERT(table->table_name); for (field=table->field ; *field ; field++) { - (*field)->table=table; + (*field)->table= (*field)->orig_table= table; (*field)->table_name=table->table_name; } for (key=0 ; key < table->keys ; key++) for (part=0 ; part < table->key_info[key].usable_key_parts ; part++) - table->key_info[key].key_part[part].field->table=table; + table->key_info[key].key_part[part].field->table= table; VOID(pthread_cond_broadcast(&COND_refresh)); error=0; @@ -1317,18 +1318,34 @@ static int open_unireg_entry(THD *thd, TABLE *entry, const char *db, { char path[FN_REFLEN]; int error; + uint discover_retry_count= 0; DBUG_ENTER("open_unireg_entry"); strxmov(path, mysql_data_home, "/", db, "/", name, NullS); - if (openfrm(path,alias, + while (openfrm(path,alias, (uint) (HA_OPEN_KEYFILE | HA_OPEN_RNDFILE | HA_GET_INDEX | HA_TRY_READ_ONLY), READ_KEYINFO | COMPUTE_TYPES | EXTRA_RECORD, thd->open_options, entry)) { if (!entry->crashed) - goto err; // Can't repair the table + { + /* + Frm file could not be found on disk + Since it does not exist, no one can be using it + LOCK_open has been locked to protect from someone else + trying to discover the table at the same time. + */ + if (discover_retry_count++ != 0) + goto err; + if (create_table_from_handler(db, name, true) != 0) + goto err; + thd->clear_error(); // Clear error message + continue; + } + + // Code below is for repairing a crashed file TABLE_LIST table_list; bzero((char*) &table_list, sizeof(table_list)); // just for safe table_list.db=(char*) db; @@ -1374,6 +1391,7 @@ static int open_unireg_entry(THD *thd, TABLE *entry, const char *db, if (error) goto err; + break; } /* If we are here, there was no fatal error (but error may be still @@ -2589,9 +2607,15 @@ static void mysql_rm_tmp_tables(void) /* Remove all SQLxxx tables from directory */ - for (idx=2 ; idx < (uint) dirp->number_off_files ; idx++) + for (idx=0 ; idx < (uint) dirp->number_off_files ; idx++) { file=dirp->dir_entry+idx; + + /* skiping . and .. */ + if (file->name[0] == '.' && (!file->name[1] || + (file->name[1] == '.' && !file->name[2]))) + continue; + if (!bcmp(file->name,tmp_file_prefix,tmp_file_prefix_length)) { sprintf(filePath,"%s%s",tmpdir,file->name); @@ -2604,45 +2628,6 @@ static void mysql_rm_tmp_tables(void) } -/* - CREATE INDEX and DROP INDEX are implemented by calling ALTER TABLE with - the proper arguments. This isn't very fast but it should work for most - cases. - One should normally create all indexes with CREATE TABLE or ALTER TABLE. -*/ - -int mysql_create_index(THD *thd, TABLE_LIST *table_list, List<Key> &keys) -{ - List<create_field> fields; - List<Alter_drop> drop; - List<Alter_column> alter; - HA_CREATE_INFO create_info; - DBUG_ENTER("mysql_create_index"); - bzero((char*) &create_info,sizeof(create_info)); - create_info.db_type=DB_TYPE_DEFAULT; - create_info.default_table_charset= thd->variables.collation_database; - DBUG_RETURN(mysql_alter_table(thd,table_list->db,table_list->real_name, - &create_info, table_list, - fields, keys, drop, alter, 0, (ORDER*)0, - DUP_ERROR)); -} - - -int mysql_drop_index(THD *thd, TABLE_LIST *table_list, List<Alter_drop> &drop) -{ - List<create_field> fields; - List<Key> keys; - List<Alter_column> alter; - HA_CREATE_INFO create_info; - DBUG_ENTER("mysql_drop_index"); - bzero((char*) &create_info,sizeof(create_info)); - create_info.db_type=DB_TYPE_DEFAULT; - create_info.default_table_charset= thd->variables.collation_database; - DBUG_RETURN(mysql_alter_table(thd,table_list->db,table_list->real_name, - &create_info, table_list, - fields, keys, drop, alter, 0, (ORDER*)0, - DUP_ERROR)); -} /***************************************************************************** unireg support functions diff --git a/sql/sql_cache.cc b/sql/sql_cache.cc index cc3d6fec9ae..2cbc1072ebd 100644 --- a/sql/sql_cache.cc +++ b/sql/sql_cache.cc @@ -661,7 +661,7 @@ void query_cache_end_of_result(THD *thd) if (thd->net.query_cache_query != 0) // Quick check on unlocked structure { #ifdef EMBEDDED_LIBRARY - query_cache_insert(&thd->net, (byte*)thd, + query_cache_insert(&thd->net, (char*)thd, emb_count_querycache_size(thd)); #endif STRUCT_LOCK(&query_cache.structure_guard_mutex); @@ -2617,14 +2617,15 @@ TABLE_COUNTER_TYPE Query_cache::is_cacheable(THD *thd, uint32 query_len, table_alias_charset used here because it depends of lower_case_table_names variable */ - if (tables_used->table->db_type == DB_TYPE_MRG_ISAM || - tables_used->table->tmp_table != NO_TMP_TABLE || + if (tables_used->table->tmp_table != NO_TMP_TABLE || + (*tables_type & HA_CACHE_TBL_NOCACHE) || (tables_used->db_length == 5 && my_strnncoll(table_alias_charset, (uchar*)tables_used->db, 6, (uchar*)"mysql",6) == 0)) { DBUG_PRINT("qcache", - ("select not cacheable: used MRG_ISAM, temporary or system table(s)")); + ("select not cacheable: temporary, system or \ +other non-cacheable table(s)")); DBUG_RETURN(0); } if (tables_used->table->db_type == DB_TYPE_MRG_MYISAM) diff --git a/sql/sql_class.h b/sql/sql_class.h index 3f1236851b3..17397c72708 100644 --- a/sql/sql_class.h +++ b/sql/sql_class.h @@ -719,7 +719,10 @@ public: THD_TRANS all; // Trans since BEGIN WORK THD_TRANS stmt; // Trans for current statement uint bdb_lock_count; - + uint ndb_lock_count; +#ifdef HAVE_NDBCLUSTER_DB + void* ndb; +#endif /* Tables changed in transaction (that must be invalidated in query cache). List contain only transactional tables, that not invalidated in query @@ -925,7 +928,8 @@ public: { #ifdef USING_TRANSACTIONS return (transaction.all.bdb_tid != 0 || - transaction.all.innodb_active_trans != 0); + transaction.all.innodb_active_trans != 0 || + transaction.all.ndb_tid != 0); #else return 0; #endif diff --git a/sql/sql_db.cc b/sql/sql_db.cc index ce92fa7ff90..300a2a455a4 100644 --- a/sql/sql_db.cc +++ b/sql/sql_db.cc @@ -461,7 +461,7 @@ static long mysql_rm_known_files(THD *thd, MY_DIR *dirp, const char *db, tot_list_next= &tot_list; - for (uint idx=2 ; + for (uint idx=0 ; idx < (uint) dirp->number_off_files && !thd->killed ; idx++) { @@ -469,6 +469,11 @@ static long mysql_rm_known_files(THD *thd, MY_DIR *dirp, const char *db, char *extension; DBUG_PRINT("info",("Examining: %s", file->name)); + /* skiping . and .. */ + if (file->name[0] == '.' && (!file->name[1] || + (file->name[1] == '.' && !file->name[2]))) + continue; + /* Check if file is a raid directory */ if ((my_isdigit(&my_charset_latin1, file->name[0]) || (file->name[0] >= 'a' && file->name[0] <= 'f')) && @@ -550,7 +555,12 @@ static long mysql_rm_known_files(THD *thd, MY_DIR *dirp, const char *db, If the directory is a symbolic link, remove the link first, then remove the directory the symbolic link pointed at */ - if (!found_other_files) + if (found_other_files) + { + my_error(ER_DB_DROP_RMDIR, MYF(0), org_path, EEXIST); + DBUG_RETURN(-1); + } + else { char tmp_path[FN_REFLEN], *pos; char *path= tmp_path; diff --git a/sql/sql_delete.cc b/sql/sql_delete.cc index 3307f11a917..9f725c7e17f 100644 --- a/sql/sql_delete.cc +++ b/sql/sql_delete.cc @@ -37,8 +37,6 @@ int mysql_delete(THD *thd, TABLE_LIST *table_list, COND *conds, SQL_LIST *order, bool using_limit=limit != HA_POS_ERROR; bool transactional_table, log_delayed, safe_update, const_cond; ha_rows deleted; - TABLE_LIST *delete_table_list= (TABLE_LIST*) - thd->lex->select_lex.table_list.first; DBUG_ENTER("mysql_delete"); if ((open_and_lock_tables(thd, table_list))) @@ -47,15 +45,9 @@ int mysql_delete(THD *thd, TABLE_LIST *table_list, COND *conds, SQL_LIST *order, table->file->info(HA_STATUS_VARIABLE | HA_STATUS_NO_LOCK); thd->proc_info="init"; table->map=1; - if (setup_conds(thd, delete_table_list, &conds) || - setup_ftfuncs(&thd->lex->select_lex)) - DBUG_RETURN(-1); - if (find_real_table_in_list(table_list->next, - table_list->db, table_list->real_name)) - { - my_error(ER_UPDATE_TABLE_USED, MYF(0), table_list->real_name); - DBUG_RETURN(-1); - } + + if ((error= mysql_prepare_delete(thd, table_list, &conds))) + DBUG_RETURN(error); const_cond= (!conds || conds->const_item()); safe_update=test(thd->options & OPTION_SAFE_UPDATES); @@ -250,6 +242,39 @@ cleanup: send_ok(thd,deleted); DBUG_PRINT("info",("%d records deleted",deleted)); } + DBUG_RETURN(0); +} + + +/* + Prepare items in DELETE statement + + SYNOPSIS + mysql_prepare_delete() + thd - thread handler + table_list - global table list + conds - conditions + + RETURN VALUE + 0 - OK + 1 - error (message is sent to user) + -1 - error (message is not sent to user) +*/ +int mysql_prepare_delete(THD *thd, TABLE_LIST *table_list, Item **conds) +{ + TABLE_LIST *delete_table_list= ((TABLE_LIST*) thd->lex-> + select_lex.table_list.first); + DBUG_ENTER("mysql_prepare_delete"); + + if (setup_conds(thd, delete_table_list, conds) || + setup_ftfuncs(&thd->lex->select_lex)) + DBUG_RETURN(-1); + if (find_real_table_in_list(table_list->next, + table_list->db, table_list->real_name)) + { + my_error(ER_UPDATE_TABLE_USED, MYF(0), table_list->real_name); + DBUG_RETURN(-1); + } DBUG_RETURN(0); } diff --git a/sql/sql_derived.cc b/sql/sql_derived.cc index 4a9bf3f1348..2e2ad6786fc 100644 --- a/sql/sql_derived.cc +++ b/sql/sql_derived.cc @@ -148,30 +148,42 @@ static int mysql_derived(THD *thd, LEX *lex, SELECT_LEX_UNIT *unit, } derived_result->set_table(table); - - if (is_union) - { - // execute union without clean up - if (!(res= unit->prepare(thd, derived_result, SELECT_NO_UNLOCK))) - res= unit->exec(); - } - else + /* + if it is preparation PS only then we do not need real data and we + can skip execution (and parameters is not defined, too) + */ + if (!thd->current_statement) { - unit->set_limit(first_select, first_select); - - lex->current_select= first_select; - res= mysql_select(thd, &first_select->ref_pointer_array, - (TABLE_LIST*) first_select->table_list.first, - first_select->with_wild, - first_select->item_list, first_select->where, - (first_select->order_list.elements+ - first_select->group_list.elements), - (ORDER *) first_select->order_list.first, - (ORDER *) first_select->group_list.first, - first_select->having, (ORDER*) NULL, - (first_select->options | thd->options | - SELECT_NO_UNLOCK), - derived_result, unit, first_select); + if (is_union) + { + // execute union without clean up + if (!(res= unit->prepare(thd, derived_result, SELECT_NO_UNLOCK))) + res= unit->exec(); + } + else + { + unit->offset_limit_cnt= first_select->offset_limit; + unit->select_limit_cnt= first_select->select_limit+ + first_select->offset_limit; + if (unit->select_limit_cnt < first_select->select_limit) + unit->select_limit_cnt= HA_POS_ERROR; + if (unit->select_limit_cnt == HA_POS_ERROR) + first_select->options&= ~OPTION_FOUND_ROWS; + + lex->current_select= first_select; + res= mysql_select(thd, &first_select->ref_pointer_array, + (TABLE_LIST*) first_select->table_list.first, + first_select->with_wild, + first_select->item_list, first_select->where, + (first_select->order_list.elements+ + first_select->group_list.elements), + (ORDER *) first_select->order_list.first, + (ORDER *) first_select->group_list.first, + first_select->having, (ORDER*) NULL, + (first_select->options | thd->options | + SELECT_NO_UNLOCK), + derived_result, unit, first_select); + } } if (!res) diff --git a/sql/sql_insert.cc b/sql/sql_insert.cc index ced85159a53..5fb525aa9a9 100644 --- a/sql/sql_insert.cc +++ b/sql/sql_insert.cc @@ -118,7 +118,7 @@ int mysql_insert(THD *thd,TABLE_LIST *table_list, runs without --log-update or --log-bin). */ bool log_on= (thd->options & OPTION_BIN_LOG) || (!(thd->master_access & SUPER_ACL)); - bool transactional_table, log_delayed, bulk_insert; + bool transactional_table, log_delayed; uint value_count; ulong counter = 1; ulonglong id; @@ -200,19 +200,10 @@ int mysql_insert(THD *thd,TABLE_LIST *table_list, goto abort; } - if (check_insert_fields(thd,table,fields,*values,1) || - setup_tables(insert_table_list) || - setup_fields(thd, 0, insert_table_list, *values, 0, 0, 0) || - (duplic == DUP_UPDATE && - (setup_fields(thd, 0, insert_table_list, update_fields, 0, 0, 0) || - setup_fields(thd, 0, insert_table_list, update_values, 0, 0, 0)))) + if (mysql_prepare_insert(thd, table_list, insert_table_list, table, + fields, values, update_fields, + update_values, duplic)) goto abort; - if (find_real_table_in_list(table_list->next, - table_list->db, table_list->real_name)) - { - my_error(ER_UPDATE_TABLE_USED, MYF(0), table_list->real_name); - goto abort; - } value_count= values->elements; while ((values= its++)) @@ -253,17 +244,16 @@ int mysql_insert(THD *thd,TABLE_LIST *table_list, thd->proc_info="update"; if (duplic != DUP_ERROR) table->file->extra(HA_EXTRA_IGNORE_DUP_KEY); - if ((lock_type != TL_WRITE_DELAYED && !(specialflag & SPECIAL_SAFE_MODE)) && - values_list.elements >= MIN_ROWS_TO_USE_BULK_INSERT) - { - table->file->extra_opt(HA_EXTRA_WRITE_CACHE, - min(thd->variables.read_buff_size, - table->avg_row_length*values_list.elements)); - table->file->deactivate_non_unique_index(values_list.elements); - bulk_insert=1; - } - else - bulk_insert=0; + /* + let's *try* to start bulk inserts. It won't necessary + start them as values_list.elements should be greater than + some - handler dependent - threshold. + So we call start_bulk_insert to perform nesessary checks on + values_list.elements, and - if nothing else - to initialize + the code to make the call of end_bulk_insert() below safe. + */ + if (lock_type != TL_WRITE_DELAYED) + table->file->start_bulk_insert(values_list.elements); while ((values= its++)) { @@ -341,24 +331,10 @@ int mysql_insert(THD *thd,TABLE_LIST *table_list, else #endif { - if (bulk_insert) + if (table->file->end_bulk_insert() && !error) { - if (table->file->extra(HA_EXTRA_NO_CACHE)) - { - if (!error) - { - table->file->print_error(my_errno,MYF(0)); - error=1; - } - } - if (table->file->activate_all_index(thd)) - { - if (!error) - { - table->file->print_error(my_errno,MYF(0)); - error=1; - } - } + table->file->print_error(my_errno,MYF(0)); + error=1; } if (id && values_list.elements != 1) thd->insert_id(id); // For update log @@ -378,7 +354,7 @@ int mysql_insert(THD *thd,TABLE_LIST *table_list, transactional_table= table->file->has_transactions(); log_delayed= (transactional_table || table->tmp_table); - if ((info.copied || info.deleted || info.updated) && + if ((info.copied || info.deleted || info.updated) && (error <= 0 || !transactional_table)) { if (mysql_bin_log.is_open()) @@ -451,6 +427,43 @@ abort: } +/* + Prepare items in INSERT statement + + SYNOPSIS + mysql_prepare_update() + thd - thread handler + table_list - global table list + insert_table_list - local table list of INSERT SELECT_LEX + + RETURN VALUE + 0 - OK + -1 - error (message is not sent to user) +*/ +int mysql_prepare_insert(THD *thd, TABLE_LIST *table_list, + TABLE_LIST *insert_table_list, TABLE *table, + List<Item> &fields, List_item *values, + List<Item> &update_fields, List<Item> &update_values, + enum_duplicates duplic) +{ + DBUG_ENTER("mysql_prepare_insert"); + if (check_insert_fields(thd, table, fields, *values, 1) || + setup_tables(insert_table_list) || + setup_fields(thd, 0, insert_table_list, *values, 0, 0, 0) || + (duplic == DUP_UPDATE && + (setup_fields(thd, 0, insert_table_list, update_fields, 0, 0, 0) || + setup_fields(thd, 0, insert_table_list, update_values, 0, 0, 0)))) + DBUG_RETURN(-1); + if (find_real_table_in_list(table_list->next, + table_list->db, table_list->real_name)) + { + my_error(ER_UPDATE_TABLE_USED, MYF(0), table_list->real_name); + DBUG_RETURN(-1); + } + DBUG_RETURN(0); +} + + /* Check if there is more uniq keys after field */ static int last_uniq_key(TABLE *table,uint keynr) @@ -471,6 +484,7 @@ int write_record(TABLE *table,COPY_INFO *info) { int error; char *key=0; + DBUG_ENTER("write_record"); info->records++; if (info->handle_duplicates == DUP_REPLACE || @@ -578,14 +592,14 @@ int write_record(TABLE *table,COPY_INFO *info) info->copied++; if (key) my_safe_afree(key,table->max_unique_length,MAX_KEY_LENGTH); - return 0; + DBUG_RETURN(0); err: if (key) my_afree(key); info->last_errno= error; table->file->print_error(error,MYF(0)); - return 1; + DBUG_RETURN(1); } @@ -1432,12 +1446,10 @@ select_insert::prepare(List<Item> &values, SELECT_LEX_UNIT *u) table->next_number_field=table->found_next_number_field; thd->count_cuted_fields= CHECK_FIELD_WARN; // calc cuted fields thd->cuted_fields=0; - if (info.handle_duplicates != DUP_REPLACE) - table->file->extra(HA_EXTRA_WRITE_CACHE); if (info.handle_duplicates == DUP_IGNORE || info.handle_duplicates == DUP_REPLACE) table->file->extra(HA_EXTRA_IGNORE_DUP_KEY); - table->file->deactivate_non_unique_index((ha_rows) 0); + table->file->start_bulk_insert((ha_rows) 0); DBUG_RETURN(0); } @@ -1446,7 +1458,7 @@ select_insert::~select_insert() if (table) { table->next_number_field=0; - table->file->extra(HA_EXTRA_RESET); + table->file->reset(); } thd->count_cuted_fields= CHECK_FIELD_IGNORE; } @@ -1454,24 +1466,25 @@ select_insert::~select_insert() bool select_insert::send_data(List<Item> &values) { + DBUG_ENTER("select_insert::send_data"); if (unit->offset_limit_cnt) { // using limit offset,count unit->offset_limit_cnt--; - return 0; + DBUG_RETURN(0); } if (fields->elements) fill_record(*fields, values, 1); else fill_record(table->field, values, 1); if (thd->net.report_error || write_record(table,&info)) - return 1; + DBUG_RETURN(1); if (table->next_number_field) // Clear for next record { table->next_number_field->reset(); if (! last_insert_id && thd->insert_id_used) last_insert_id=thd->insert_id(); } - return 0; + DBUG_RETURN(0); } @@ -1490,15 +1503,14 @@ void select_insert::send_error(uint errcode,const char *err) */ DBUG_VOID_RETURN; } - table->file->extra(HA_EXTRA_NO_CACHE); - table->file->activate_all_index(thd); + table->file->end_bulk_insert(); /* If at least one row has been inserted/modified and will stay in the table (the table doesn't have transactions) (example: we got a duplicate key error while inserting into a MyISAM table) we must write to the binlog (and the error code will make the slave stop). */ - if ((info.copied || info.deleted || info.updated) && + if ((info.copied || info.deleted || info.updated) && !table->file->has_transactions()) { if (last_insert_id) @@ -1510,7 +1522,7 @@ void select_insert::send_error(uint errcode,const char *err) mysql_bin_log.write(&qinfo); } if (!table->tmp_table) - thd->options|=OPTION_STATUS_NO_TRANS_UPDATE; + thd->options|=OPTION_STATUS_NO_TRANS_UPDATE; } if (info.copied || info.deleted || info.updated) { @@ -1526,8 +1538,7 @@ bool select_insert::send_eof() int error,error2; DBUG_ENTER("select_insert::send_eof"); - if (!(error=table->file->extra(HA_EXTRA_NO_CACHE))) - error=table->file->activate_all_index(thd); + error=table->file->end_bulk_insert(); table->file->extra(HA_EXTRA_NO_IGNORE_DUP_KEY); /* @@ -1603,7 +1614,7 @@ select_create::prepare(List<Item> &values, SELECT_LEX_UNIT *u) /* Don't set timestamp if used */ table->timestamp_default_now= table->timestamp_on_update_now= 0; - + table->next_number_field=table->found_next_number_field; restore_record(table,default_values); // Get empty record @@ -1612,7 +1623,7 @@ select_create::prepare(List<Item> &values, SELECT_LEX_UNIT *u) if (info.handle_duplicates == DUP_IGNORE || info.handle_duplicates == DUP_REPLACE) table->file->extra(HA_EXTRA_IGNORE_DUP_KEY); - table->file->deactivate_non_unique_index((ha_rows) 0); + table->file->start_bulk_insert((ha_rows) 0); DBUG_RETURN(0); } diff --git a/sql/sql_lex.cc b/sql/sql_lex.cc index 071e41b1247..7e38941f344 100644 --- a/sql/sql_lex.cc +++ b/sql/sql_lex.cc @@ -869,13 +869,15 @@ int yylex(void *arg, void *yythd) } yySkip(); return (SET_VAR); - case MY_LEX_COLON: // optional line terminator + case MY_LEX_SEMICOLON: // optional line terminator if (yyPeek()) { - if (((THD *)yythd)->client_capabilities & CLIENT_MULTI_STATEMENTS) + THD* thd= (THD*)yythd; + if ((thd->client_capabilities & CLIENT_MULTI_STATEMENTS) && + (thd->command != COM_PREPARE)) { lex->found_colon=(char*)lex->ptr; - ((THD *)yythd)->server_status |= SERVER_MORE_RESULTS_EXISTS; + thd->server_status |= SERVER_MORE_RESULTS_EXISTS; lex->next_state=MY_LEX_END; return(END_OF_INPUT); } @@ -1012,6 +1014,7 @@ void st_select_lex::init_query() ref_pointer_array= 0; select_n_having_items= 0; prep_where= 0; + explicit_limit= 0; } void st_select_lex::init_select() @@ -1520,7 +1523,7 @@ bool st_select_lex::setup_ref_array(THD *thd, uint order_group_num) */ bool st_select_lex_unit::check_updateable(char *db, char *table) { - for(SELECT_LEX *sl= first_select(); sl; sl= sl->next_select()) + for (SELECT_LEX *sl= first_select(); sl; sl= sl->next_select()) if (sl->check_updateable(db, table)) return 1; return 0; @@ -1605,10 +1608,7 @@ void st_select_lex::print_limit(THD *thd, String *str) if (!thd) thd= current_thd; - if ((select_limit != thd->variables.select_limit && - this == &thd->lex->select_lex) || - (select_limit != HA_POS_ERROR && this != &thd->lex->select_lex) || - offset_limit != 0L) + if (explicit_limit) { str->append(" limit ", 7); char buff[20]; @@ -1644,6 +1644,75 @@ void st_select_lex_unit::set_limit(SELECT_LEX *values, sl->options&= ~OPTION_FOUND_ROWS; } + +/* + Unlink first table from global table list and first table from outer select + list (lex->select_lex) + + SYNOPSIS + unlink_first_table() + tables Global table list + global_first Save first global table here + local_first Save first local table here + + NORES + global_first & local_first are used to save result for link_first_table_back + + RETURN + global list without first table + +*/ +TABLE_LIST *st_lex::unlink_first_table(TABLE_LIST *tables, + TABLE_LIST **global_first, + TABLE_LIST **local_first) +{ + *global_first= tables; + *local_first= (TABLE_LIST*)select_lex.table_list.first; + /* + Exclude from global table list + */ + tables= tables->next; + /* + and from local list if it is not the same + */ + select_lex.table_list.first= ((&select_lex != all_selects_list) ? + (byte*) (*local_first)->next : + (byte*) tables); + (*global_first)->next= 0; + return tables; +} + + +/* + Link table back that was unlinked with unlink_first_table() + + SYNOPSIS + link_first_table_back() + tables Global table list + global_first Saved first global table + local_first Saved first local table + + RETURN + global list +*/ +TABLE_LIST *st_lex::link_first_table_back(TABLE_LIST *tables, + TABLE_LIST *global_first, + TABLE_LIST *local_first) +{ + global_first->next= tables; + if (&select_lex != all_selects_list) + { + /* + we do not touch local table 'next' field => we need just + put the table in the list + */ + select_lex.table_list.first= (byte*) local_first; + } + else + select_lex.table_list.first= (byte*) global_first; + return global_first; +} + /* There are st_select_lex::add_table_to_list & st_select_lex::set_lock_for_tables are in sql_parse.cc diff --git a/sql/sql_lex.h b/sql/sql_lex.h index 7a5a1c13f80..60965209cc9 100644 --- a/sql/sql_lex.h +++ b/sql/sql_lex.h @@ -344,7 +344,10 @@ public: Item_subselect *item; /* thread handler */ THD *thd; - /* fake SELECT_LEX for union processing */ + /* + SELECT_LEX for hidden SELECT in onion which process global + ORDER BY and LIMIT + */ st_select_lex *fake_select_lex; st_select_lex *union_distinct; /* pointer to the last UNION DISTINCT */ @@ -353,12 +356,18 @@ public: bool create_total_list(THD *thd, st_lex *lex, TABLE_LIST **result); st_select_lex_unit* master_unit(); st_select_lex* outer_select(); - st_select_lex* first_select() { return (st_select_lex*) slave; } + st_select_lex* first_select() + { + return my_reinterpret_cast(st_select_lex*)(slave); + } st_select_lex* first_select_in_union() { - return (st_select_lex*) slave; + return my_reinterpret_cast(st_select_lex*)(slave); + } + st_select_lex_unit* next_unit() + { + return my_reinterpret_cast(st_select_lex_unit*)(next); } - st_select_lex_unit* next_unit() { return (st_select_lex_unit*) next; } st_select_lex* return_after_parsing() { return return_to; } void exclude_level(); void exclude_tree(); @@ -372,7 +381,8 @@ public: bool check_updateable(char *db, char *table); void print(String *str); - + + ulong init_prepare_fake_select_lex(THD *thd); void set_limit(st_select_lex *values, st_select_lex *sl); friend void mysql_init_query(THD *thd, bool lexonly); @@ -433,6 +443,8 @@ public: bool braces; /* SELECT ... UNION (SELECT ... ) <- this braces */ /* TRUE when having fix field called in processing of this SELECT */ bool having_fix_field; + /* explicit LIMIT clause was used */ + bool explicit_limit; /* SELECT for SELECT command st_select_lex. Used to privent scaning @@ -524,6 +536,14 @@ public: }; typedef class st_select_lex SELECT_LEX; +#define ALTER_ADD_COLUMN 1 +#define ALTER_DROP_COLUMN 2 +#define ALTER_CHANGE_COLUMN 4 +#define ALTER_ADD_INDEX 8 +#define ALTER_DROP_INDEX 16 +#define ALTER_RENAME 32 +#define ALTER_ORDER 64 +#define ALTER_OPTIONS 128 struct st_sp_chistics { @@ -601,6 +621,7 @@ typedef struct st_lex uint grant, grant_tot_col, which_columns; uint fk_delete_opt, fk_update_opt, fk_match_option; uint slave_thd_opt; + uint alter_flags; uint8 describe; bool drop_if_exists, drop_temporary, local_file; bool in_comment, ignore_space, verbose, simple_alter, no_write_to_binlog; @@ -643,6 +664,12 @@ typedef struct st_lex un->uncacheable|= cause; } } + TABLE_LIST *unlink_first_table(TABLE_LIST *tables, + TABLE_LIST **global_first, + TABLE_LIST **local_first); + TABLE_LIST *link_first_table_back(TABLE_LIST *tables, + TABLE_LIST *global_first, + TABLE_LIST *local_first); } LEX; diff --git a/sql/sql_load.cc b/sql/sql_load.cc index 377f41958c5..3313ff913dc 100644 --- a/sql/sql_load.cc +++ b/sql/sql_load.cc @@ -276,14 +276,12 @@ int mysql_load(THD *thd,sql_exchange *ex,TABLE_LIST *table_list, { if (use_timestamp) table->timestamp_default_now= table->timestamp_on_update_now= 0; - + table->next_number_field=table->found_next_number_field; - VOID(table->file->extra_opt(HA_EXTRA_WRITE_CACHE, - thd->variables.read_buff_size)); if (handle_duplicates == DUP_IGNORE || handle_duplicates == DUP_REPLACE) table->file->extra(HA_EXTRA_IGNORE_DUP_KEY); - table->file->deactivate_non_unique_index((ha_rows) 0); + table->file->start_bulk_insert((ha_rows) 0); table->copy_blobs=1; if (!field_term->length() && !enclosed->length()) error=read_fixed_length(thd,info,table,fields,read_info, @@ -291,9 +289,7 @@ int mysql_load(THD *thd,sql_exchange *ex,TABLE_LIST *table_list, else error=read_sep_field(thd,info,table,fields,read_info,*enclosed, skip_lines); - if (table->file->extra(HA_EXTRA_NO_CACHE)) - error=1; /* purecov: inspected */ - if (table->file->activate_all_index(thd)) + if (table->file->end_bulk_insert()) error=1; /* purecov: inspected */ table->file->extra(HA_EXTRA_NO_IGNORE_DUP_KEY); table->next_number_field=0; @@ -530,8 +526,8 @@ read_sep_field(THD *thd,COPY_INFO &info,TABLE *table, if (field->type() == FIELD_TYPE_TIMESTAMP) ((Field_timestamp*) field)->set_time(); else if (field != table->next_number_field) - field->set_warning((uint)MYSQL_ERROR::WARN_LEVEL_WARN, - ER_WARN_NULL_TO_NOTNULL); + field->set_warning((uint) MYSQL_ERROR::WARN_LEVEL_WARN, + ER_WARN_NULL_TO_NOTNULL); } continue; } @@ -1015,7 +1011,7 @@ bool READ_INFO::find_start_of_fields() { // Can't be line_start PUSH(chr); while (--ptr != line_start_ptr) - { // Restart with next char + { // Restart with next char PUSH((uchar) *ptr); } goto try_again; diff --git a/sql/sql_parse.cc b/sql/sql_parse.cc index 4ea1fcf3301..057ea6e8bd5 100644 --- a/sql/sql_parse.cc +++ b/sql/sql_parse.cc @@ -65,13 +65,10 @@ static int check_for_max_user_connections(THD *thd, USER_CONN *uc); #endif static void decrease_user_connections(USER_CONN *uc); static bool check_db_used(THD *thd,TABLE_LIST *tables); -static bool check_merge_table_access(THD *thd, char *db, TABLE_LIST *tables); static void remove_escape(char *name); static void refresh_status(void); static bool append_file_to_dir(THD *thd, const char **filename_ptr, const char *table_name); -static int check_one_table_access(THD *thd, ulong privilege, - TABLE_LIST *tables, bool no_errors); const char *any_db="*any*"; // Special symbol for check_access @@ -1109,6 +1106,12 @@ extern "C" pthread_handler_decl(handle_bootstrap,arg) while (fgets(buff, thd->net.max_packet, file)) { uint length=(uint) strlen(buff); + if (buff[length-1]!='\n' && !feof(file)) + { + send_error(thd,ER_NET_PACKET_TOO_LARGE, NullS); + thd->is_fatal_error= 1; + break; + } while (length && (my_isspace(thd->charset(), buff[length-1]) || buff[length-1] == ';')) length--; @@ -1191,7 +1194,7 @@ int mysql_table_dump(THD* thd, char* db, char* tbl_name, int fd) if (!(table=open_ltable(thd, table_list, TL_READ_NO_INSERT))) DBUG_RETURN(1); - if (check_one_table_access(thd, SELECT_ACL, table_list, 0)) + if (check_one_table_access(thd, SELECT_ACL, table_list)) goto err; thd->free_list = 0; thd->query_length=(uint) strlen(tbl_name); @@ -1439,6 +1442,7 @@ bool dispatch_command(enum enum_server_command command, THD *thd, } case COM_EXECUTE: { + thd->free_list= NULL; mysql_stmt_execute(thd, packet, packet_length); break; } @@ -1551,7 +1555,8 @@ bool dispatch_command(enum enum_server_command command, THD *thd, if (check_access(thd,SELECT_ACL,table_list.db,&table_list.grant.privilege, 0, 0)) break; - if (grant_option && check_grant(thd,SELECT_ACL,&table_list,2,0)) + if (grant_option && + check_grant(thd, SELECT_ACL, &table_list, 2, UINT_MAX, 0)) break; mysqld_list_fields(thd,&table_list,fields); free_items(thd->free_list); @@ -1894,7 +1899,7 @@ mysql_execute_command(THD *thd) if (&lex->select_lex != lex->all_selects_list && lex->unit.create_total_list(thd, lex, &tables)) DBUG_RETURN(0); - + /* When option readonly is set deny operations which change tables. Except for the replication thread and the 'super' users. @@ -1911,6 +1916,13 @@ mysql_execute_command(THD *thd) switch (lex->sql_command) { case SQLCOM_SELECT: { + /* assign global limit variable if limit is not given */ + { + SELECT_LEX *param= lex->unit.global_parameters; + if (!param->explicit_limit) + param->select_limit= thd->variables.select_limit; + } + select_result *result=lex->result; if (tables) { @@ -2150,11 +2162,7 @@ mysql_execute_command(THD *thd) if (grant_option) { /* Check that the first table has CREATE privilege */ - TABLE_LIST *tmp_table_list=tables->next; - tables->next=0; - bool error=check_grant(thd,CREATE_ACL,tables,0,0); - tables->next=tmp_table_list; - if (error) + if (check_grant(thd, CREATE_ACL, tables, 0, 1, 0)) goto error; } if (strlen(tables->real_name) > NAME_LEN) @@ -2180,30 +2188,13 @@ mysql_execute_command(THD *thd) case SQLCOM_CREATE_TABLE: { /* Skip first table, which is the table we are creating */ - TABLE_LIST *create_table= tables; - TABLE_LIST *create_table_local= - (TABLE_LIST*)lex->select_lex.table_list.first; - // exclude from global table list - tables= tables->next; - // and from local list if it is not the same - if (&lex->select_lex != lex->all_selects_list) - lex->select_lex.table_list.first= (gptr)create_table_local->next; - else - lex->select_lex.table_list.first= (gptr)tables; - create_table->next= 0; - - ulong want_priv= ((lex->create_info.options & HA_LEX_CREATE_TMP_TABLE) ? - CREATE_TMP_ACL : CREATE_ACL); - lex->create_info.alias= create_table->alias; - if (check_access(thd, want_priv, create_table->db, - &create_table->grant.privilege, 0, 0) || - check_merge_table_access(thd, create_table->db, - (TABLE_LIST *) - lex->create_info.merge_list.first)) - goto create_eror; /* purecov: inspected */ - if (grant_option && want_priv != CREATE_TMP_ACL && - check_grant(thd, want_priv, create_table,0,0)) - goto create_eror; + TABLE_LIST *create_table, *create_table_local; + tables= lex->unlink_first_table(tables, &create_table, + &create_table_local); + + if ((res= create_table_precheck(thd, tables, create_table))) + goto unsent_create_error; + #ifndef HAVE_READLINK lex->create_info.data_file_name=lex->create_info.index_file_name=0; #else @@ -2239,10 +2230,10 @@ mysql_execute_command(THD *thd) create_table->real_name)) { net_printf(thd,ER_UPDATE_TABLE_USED, create_table->real_name); - goto create_eror; + goto create_error; } if (tables && check_table_access(thd, SELECT_ACL, tables,0)) - goto create_eror; // Error message is given + goto create_error; // Error message is given select_lex->options|= SELECT_NO_UNLOCK; unit->set_limit(select_lex, select_lex); @@ -2256,6 +2247,9 @@ mysql_execute_command(THD *thd) lex->key_list, select_lex->item_list,lex->duplicates))) res=handle_select(thd, lex, result); + //reset for PS + lex->create_list.empty(); + lex->key_list.empty(); } } else // regular create @@ -2273,39 +2267,22 @@ mysql_execute_command(THD *thd) if (!res) send_ok(thd); } + // put tables back for PS rexecuting - create_table->next= tables; - tables= create_table; - if (&lex->select_lex != lex->all_selects_list) - { - /* - we do not touch local table 'next' field => we need just - put the table in the list - */ - lex->select_lex.table_list.first= (gptr) create_table_local; - } - else - lex->select_lex.table_list.first= (gptr) tables; + tables= lex->link_first_table_back(tables, create_table, + create_table_local); break; -create_eror: +create_error: res= 1; //error reported unsent_create_error: // put tables back for PS rexecuting - create_table->next= tables; - tables= create_table; - if (&lex->select_lex != lex->all_selects_list) - { - /* - we do not touch local table 'next' field => we need just - put the table in the list - */ - lex->select_lex.table_list.first= (gptr) create_table_local; - } + tables= lex->link_first_table_back(tables, create_table, + create_table_local); break; } case SQLCOM_CREATE_INDEX: - if (check_one_table_access(thd, INDEX_ACL, tables, 0)) + if (check_one_table_access(thd, INDEX_ACL, tables)) goto error; /* purecov: inspected */ thd->slow_command=TRUE; if (end_active_trans(thd)) @@ -2372,7 +2349,7 @@ unsent_create_error: goto error; /* purecov: inspected */ if (grant_option) { - if (check_grant(thd,ALTER_ACL,tables,0,0)) + if (check_grant(thd, ALTER_ACL, tables, 0, UINT_MAX, 0)) goto error; if (lex->name && !test_all_bits(priv,INSERT_ACL | CREATE_ACL)) { // Rename of table @@ -2381,7 +2358,8 @@ unsent_create_error: tmp_table.real_name=lex->name; tmp_table.db=select_lex->db; tmp_table.grant.privilege=priv; - if (check_grant(thd, INSERT_ACL | CREATE_ACL, &tmp_table, 0, 0)) + if (check_grant(thd, INSERT_ACL | CREATE_ACL, &tmp_table, 0, + UINT_MAX, 0)) goto error; } } @@ -2399,6 +2377,7 @@ unsent_create_error: lex->key_list, lex->drop_list, lex->alter_list, select_lex->order_list.elements, (ORDER *) select_lex->order_list.first, + lex->alter_flags, lex->duplicates, lex->alter_keys_onoff, lex->tablespace_op, @@ -2429,10 +2408,11 @@ unsent_create_error: old_list=table[0]; new_list=table->next[0]; old_list.next=new_list.next=0; - if (check_grant(thd,ALTER_ACL,&old_list,0,0) || + if (check_grant(thd, ALTER_ACL, &old_list, 0, UINT_MAX, 0) || (!test_all_bits(table->next->grant.privilege, INSERT_ACL | CREATE_ACL) && - check_grant(thd,INSERT_ACL | CREATE_ACL, &new_list,0,0))) + check_grant(thd, INSERT_ACL | CREATE_ACL, &new_list, 0, + UINT_MAX, 0))) goto error; } } @@ -2547,7 +2527,7 @@ unsent_create_error: res= mysql_alter_table(thd, NullS, NullS, &create_info, tables, lex->create_list, lex->key_list, lex->drop_list, lex->alter_list, - 0, (ORDER *) 0, + 0, (ORDER *) 0, 0, DUP_ERROR); } else @@ -2564,15 +2544,8 @@ unsent_create_error: break; } case SQLCOM_UPDATE: - if (select_lex->item_list.elements != lex->value_list.elements) - { - send_error(thd,ER_WRONG_VALUE_COUNT); - goto error; - } - if (check_db_used(thd,tables)) - goto error; - if (check_one_table_access(thd, UPDATE_ACL, tables, 0)) - goto error; + if (update_precheck(thd, tables)) + break; res= mysql_update(thd,tables, select_lex->item_list, lex->value_list, @@ -2586,39 +2559,8 @@ unsent_create_error: break; case SQLCOM_UPDATE_MULTI: { - const char *msg= 0; - TABLE_LIST *table; - - if (select_lex->item_list.elements != lex->value_list.elements) - { - send_error(thd,ER_WRONG_VALUE_COUNT); - goto error; - } - /* - Ensure that we have UPDATE or SELECT privilege for each table - The exact privilege is checked in mysql_multi_update() - */ - for (table= tables ; table ; table= table->next) - { - TABLE_LIST *save= table->next; - table->next= 0; - if (check_one_table_access(thd, UPDATE_ACL, table, 1) && - check_one_table_access(thd, SELECT_ACL, table, 0)) - goto error; - table->next= save; - } - - if (select_lex->order_list.elements) - msg= "ORDER BY"; - else if (select_lex->select_limit && select_lex->select_limit != - HA_POS_ERROR) - msg= "LIMIT"; - if (msg) - { - net_printf(thd, ER_WRONG_USAGE, "UPDATE", msg); - res= 1; + if ((res= multi_update_precheck(thd, tables))) break; - } res= mysql_multi_update(thd,tables, &select_lex->item_list, &lex->value_list, @@ -2630,17 +2572,9 @@ unsent_create_error: case SQLCOM_REPLACE: case SQLCOM_INSERT: { - my_bool update=(lex->value_list.elements ? UPDATE_ACL : 0); - ulong privilege= (lex->duplicates == DUP_REPLACE ? - INSERT_ACL | DELETE_ACL : INSERT_ACL | update); - - if (check_one_table_access(thd, privilege, tables, 0)) - goto error; - if (select_lex->item_list.elements != lex->value_list.elements) - { - send_error(thd,ER_WRONG_VALUE_COUNT); - goto error; - } + my_bool update= (lex->value_list.elements ? UPDATE_ACL : 0); + if ((res= insert_precheck(thd, tables, update))) + break; res = mysql_insert(thd,tables,lex->field_list,lex->many_values, select_lex->item_list, lex->value_list, (update ? DUP_UPDATE : lex->duplicates)); @@ -2651,16 +2585,9 @@ unsent_create_error: case SQLCOM_REPLACE_SELECT: case SQLCOM_INSERT_SELECT: { - /* - Check that we have modify privileges for the first table and - select privileges for the rest - */ - { - ulong privilege= (lex->duplicates == DUP_REPLACE ? - INSERT_ACL | DELETE_ACL : INSERT_ACL); - if (check_one_table_access(thd, privilege, tables, 0)) - goto error; - } + TABLE_LIST *first_local_table= (TABLE_LIST *) select_lex->table_list.first; + if ((res= insert_select_precheck(thd, tables))) + break; /* Fix lock for first table */ if (tables->lock_type == TL_WRITE_DELAYED) @@ -2678,16 +2605,18 @@ unsent_create_error: select_lex->options |= OPTION_BUFFER_RESULT; } - /* Skip first table, which is the table we are inserting in */ - lex->select_lex.table_list.first= - (byte*) (((TABLE_LIST *) lex->select_lex.table_list.first)->next); - lex->select_lex.resolve_mode= SELECT_LEX::NOMATTER_MODE; if (!(res=open_and_lock_tables(thd, tables))) { if ((result=new select_insert(tables->table,&lex->field_list, lex->duplicates))) + /* Skip first table, which is the table we are inserting in */ + lex->select_lex.table_list.first= (byte*) first_local_table->next; + lex->select_lex.resolve_mode= SELECT_LEX::NOMATTER_MODE; res=handle_select(thd,lex,result); + /* revert changes for SP */ + lex->select_lex.table_list.first= (byte*) first_local_table; + lex->select_lex.resolve_mode= SELECT_LEX::INSERT_MODE; if (thd->net.report_error) res= -1; } @@ -2696,7 +2625,7 @@ unsent_create_error: break; } case SQLCOM_TRUNCATE: - if (check_one_table_access(thd, DELETE_ACL, tables, 0)) + if (check_one_table_access(thd, DELETE_ACL, tables)) goto error; /* Don't allow this within a transaction because we want to use @@ -2711,10 +2640,8 @@ unsent_create_error: break; case SQLCOM_DELETE: { - if (check_one_table_access(thd, DELETE_ACL, tables, 0)) - goto error; - // Set privilege for the WHERE clause - tables->grant.want_privilege=(SELECT_ACL & ~tables->grant.privilege); + if ((res= delete_precheck(thd, tables))) + break; res = mysql_delete(thd,tables, select_lex->where, &select_lex->order_list, select_lex->select_limit, select_lex->options); @@ -2724,58 +2651,33 @@ unsent_create_error: } case SQLCOM_DELETE_MULTI: { - TABLE_LIST *aux_tables= (TABLE_LIST *)thd->lex->auxilliary_table_list.first; - TABLE_LIST *auxi; - uint table_count=0; + TABLE_LIST *aux_tables= + (TABLE_LIST *)thd->lex->auxilliary_table_list.first; + TABLE_LIST *target_tbl; + uint table_count; multi_delete *result; - /* sql_yacc guarantees that tables and aux_tables are not zero */ - if (check_db_used(thd, tables) || check_db_used(thd,aux_tables) || - check_table_access(thd,SELECT_ACL, tables,0) || - check_table_access(thd,DELETE_ACL, aux_tables,0)) - goto error; - if ((thd->options & OPTION_SAFE_UPDATES) && !select_lex->where) - { - send_error(thd,ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE); - goto error; - } - for (auxi=(TABLE_LIST*) aux_tables ; auxi ; auxi=auxi->next) - { - table_count++; - /* All tables in aux_tables must be found in FROM PART */ - TABLE_LIST *walk; - for (walk= (TABLE_LIST*) tables; walk; walk= walk->next) - { - if (!my_strcasecmp(table_alias_charset, auxi->alias, walk->alias) && - !strcmp(walk->db, auxi->db)) - break; - } - if (!walk) - { - net_printf(thd, ER_NONUNIQ_TABLE, auxi->real_name); - goto error; - } - if (walk->derived) - { - net_printf(thd, ER_NON_UPDATABLE_TABLE, - auxi->real_name, "DELETE"); - goto error; - } - walk->lock_type= auxi->lock_type; - auxi->table_list= walk; // Remember corresponding table - } + if ((res= multi_delete_precheck(thd, tables, &table_count))) + break; + + /* condition will be TRUE on SP re-excuting */ + if (select_lex->item_list.elements != 0) + select_lex->item_list.empty(); if (add_item_to_list(thd, new Item_null())) { res= -1; break; } + thd->proc_info="init"; if ((res=open_and_lock_tables(thd,tables))) break; /* Fix tables-to-be-deleted-from list to point at opened tables */ - for (auxi=(TABLE_LIST*) aux_tables ; auxi ; auxi=auxi->next) + for (target_tbl= (TABLE_LIST*) aux_tables; + target_tbl; + target_tbl= target_tbl->next) { - auxi->table= auxi->table_list->table; + target_tbl->table= target_tbl->table_list->table; /* Multi-delete can't be constructed over-union => we always have single SELECT on top and have to check underlaying SELECTs of it @@ -2785,10 +2687,11 @@ unsent_create_error: un= un->next_unit()) { if (un->first_select()->linkage != DERIVED_TABLE_TYPE && - un->check_updateable(auxi->table_list->db, - auxi->table_list->real_name)) + un->check_updateable(target_tbl->table_list->db, + target_tbl->table_list->real_name)) { - my_error(ER_UPDATE_TABLE_USED, MYF(0), auxi->table_list->real_name); + my_error(ER_UPDATE_TABLE_USED, MYF(0), + target_tbl->table_list->real_name); res= -1; break; } @@ -2846,7 +2749,7 @@ unsent_create_error: } break; case SQLCOM_DROP_INDEX: - if (check_one_table_access(thd, INDEX_ACL, tables, 0)) + if (check_one_table_access(thd, INDEX_ACL, tables)) goto error; /* purecov: inspected */ if (end_active_trans(thd)) res= -1; @@ -2961,7 +2864,7 @@ unsent_create_error: if (check_access(thd,SELECT_ACL | EXTRA_ACL,db, &tables->grant.privilege, 0, 0)) goto error; /* purecov: inspected */ - if (grant_option && check_grant(thd,SELECT_ACL,tables,2,0)) + if (grant_option && check_grant(thd, SELECT_ACL, tables, 2, UINT_MAX, 0)) goto error; res= mysqld_show_fields(thd,tables, (lex->wild ? lex->wild->ptr() : NullS), @@ -2981,7 +2884,7 @@ unsent_create_error: if (check_access(thd,SELECT_ACL | EXTRA_ACL,db, &tables->grant.privilege, 0, 0)) goto error; /* purecov: inspected */ - if (grant_option && check_grant(thd,SELECT_ACL,tables,2,0)) + if (grant_option && check_grant(thd, SELECT_ACL, tables, 2, UINT_MAX, 0)) goto error; res= mysqld_show_keys(thd,tables); break; @@ -3009,7 +2912,7 @@ unsent_create_error: send_error(thd,ER_NOT_ALLOWED_COMMAND); goto error; } - if (check_one_table_access(thd, privilege, tables, 0)) + if (check_one_table_access(thd, privilege, tables)) goto error; } res=mysql_load(thd, lex->exchange, tables, lex->field_list, @@ -3273,7 +3176,7 @@ unsent_create_error: if (grant_option && check_grant(thd, (lex->grant | lex->grant_tot_col | GRANT_ACL), - tables,0,0)) + tables, 0, UINT_MAX, 0)) goto error; if (!(res = mysql_table_grant(thd,tables,lex->users_list, lex->columns, lex->grant, @@ -3768,33 +3671,26 @@ error: thd Thread handler privilege requested privelage tables table list of command - no_errors Don't send error to client RETURN 0 - OK 1 - access denied, error is sent to client */ -static int check_one_table_access(THD *thd, ulong privilege, - TABLE_LIST *tables, bool no_errors) +int check_one_table_access(THD *thd, ulong privilege, TABLE_LIST *tables) { if (check_access(thd, privilege, tables->db, &tables->grant.privilege,0,0)) return 1; - // Show only 1 table for check_grant - TABLE_LIST *subselects_tables= tables->next; - tables->next= 0; - if (grant_option && check_grant(thd, privilege, tables, 0, 0)) - { - tables->next= subselects_tables; + /* Show only 1 table for check_grant */ + if (grant_option && check_grant(thd, privilege, tables, 0, 1, 0)) return 1; - } - // check rights on tables of subselect (if exists) - if (subselects_tables) + /* Check rights on tables of subselect (if exists) */ + TABLE_LIST *subselects_tables; + if ((subselects_tables= tables->next)) { - tables->next= subselects_tables; if ((check_table_access(thd, SELECT_ACL, subselects_tables,0))) return 1; } @@ -3973,12 +3869,12 @@ check_table_access(THD *thd, ulong want_access,TABLE_LIST *tables, } if (grant_option) return check_grant(thd,want_access & ~EXTRA_ACL,org_tables, - test(want_access & EXTRA_ACL), no_errors); + test(want_access & EXTRA_ACL), UINT_MAX, no_errors); return FALSE; } -static bool check_merge_table_access(THD *thd, char *db, - TABLE_LIST *table_list) +bool check_merge_table_access(THD *thd, char *db, + TABLE_LIST *table_list) { int error=0; if (table_list) @@ -4135,9 +4031,7 @@ mysql_init_select(LEX *lex) { SELECT_LEX *select_lex= lex->current_select; select_lex->init_select(); - select_lex->select_limit= (&lex->select_lex == select_lex) ? - lex->thd->variables.select_limit : /* Primry UNION */ - HA_POS_ERROR; /* subquery */ + select_lex->select_limit= HA_POS_ERROR; if (select_lex == &lex->select_lex) { lex->exchange= 0; @@ -4189,9 +4083,7 @@ mysql_new_select(LEX *lex, bool move_down) fake->select_number= INT_MAX; fake->make_empty_select(); fake->linkage= GLOBAL_OPTIONS_TYPE; - fake->select_limit= (&lex->unit == unit) ? - lex->thd->variables.select_limit : /* Primry UNION */ - HA_POS_ERROR; /* subquery */ + fake->select_limit= HA_POS_ERROR; } } @@ -4249,6 +4141,7 @@ void mysql_init_multi_delete(LEX *lex) When you modify mysql_parse(), you may need to mofify mysql_test_parse_for_slave() in this same file. */ + void mysql_parse(THD *thd, char *inBuf, uint length) { DBUG_ENTER("mysql_parse"); @@ -4771,7 +4664,6 @@ static void remove_escape(char *name) bool add_to_list(THD *thd, SQL_LIST &list,Item *item,bool asc) { ORDER *order; - Item **item_ptr; DBUG_ENTER("add_to_list"); if (!(order = (ORDER *) thd->alloc(sizeof(ORDER)))) DBUG_RETURN(1); @@ -5289,3 +5181,353 @@ Item * all_any_subquery_creator(Item *left_expr, return it; /* ANY/SOME */ } + + +/* + CREATE INDEX and DROP INDEX are implemented by calling ALTER TABLE with + the proper arguments. This isn't very fast but it should work for most + cases. + + In the future ALTER TABLE will notice that only added indexes + and create these one by one for the existing table without having to do + a full rebuild. + + One should normally create all indexes with CREATE TABLE or ALTER TABLE. +*/ + +int mysql_create_index(THD *thd, TABLE_LIST *table_list, List<Key> &keys) +{ + List<create_field> fields; + List<Alter_drop> drop; + List<Alter_column> alter; + HA_CREATE_INFO create_info; + DBUG_ENTER("mysql_create_index"); + bzero((char*) &create_info,sizeof(create_info)); + create_info.db_type=DB_TYPE_DEFAULT; + create_info.default_table_charset= thd->variables.collation_database; + DBUG_RETURN(mysql_alter_table(thd,table_list->db,table_list->real_name, + &create_info, table_list, + fields, keys, drop, alter, 0, (ORDER*)0, + ALTER_ADD_INDEX, DUP_ERROR)); +} + + +int mysql_drop_index(THD *thd, TABLE_LIST *table_list, List<Alter_drop> &drop) +{ + List<create_field> fields; + List<Key> keys; + List<Alter_column> alter; + HA_CREATE_INFO create_info; + DBUG_ENTER("mysql_drop_index"); + bzero((char*) &create_info,sizeof(create_info)); + create_info.db_type=DB_TYPE_DEFAULT; + create_info.default_table_charset= thd->variables.collation_database; + DBUG_RETURN(mysql_alter_table(thd,table_list->db,table_list->real_name, + &create_info, table_list, + fields, keys, drop, alter, 0, (ORDER*)0, + ALTER_DROP_INDEX, DUP_ERROR)); +} + + +/* + Multi update query pre-check + + SYNOPSIS + multi_update_precheck() + thd Thread handler + tables Global table list + + RETURN VALUE + 0 OK + 1 Error (message is sent to user) + -1 Error (message is not sent to user) +*/ + +int multi_update_precheck(THD *thd, TABLE_LIST *tables) +{ + DBUG_ENTER("multi_update_precheck"); + const char *msg= 0; + TABLE_LIST *table; + LEX *lex= thd->lex; + SELECT_LEX *select_lex= &lex->select_lex; + TABLE_LIST *update_list= (TABLE_LIST*)select_lex->table_list.first; + + if (select_lex->item_list.elements != lex->value_list.elements) + { + my_error(ER_WRONG_VALUE_COUNT, MYF(0)); + DBUG_RETURN(-1); + } + /* + Ensure that we have UPDATE or SELECT privilege for each table + The exact privilege is checked in mysql_multi_update() + */ + for (table= update_list; table; table= table->next) + { + if ((check_access(thd, UPDATE_ACL, table->db, + &table->grant.privilege, 0, 1) || + grant_option && check_grant(thd, UPDATE_ACL, table, 0, 1, 1)) && + (check_access(thd, SELECT_ACL, table->db, + &table->grant.privilege, 0, 0) || + grant_option && check_grant(thd, SELECT_ACL, table, 0, 1, 0))) + DBUG_RETURN(1); + + /* + We assign following flag only to copy of table, because it will + be checked only if query contains subqueries i.e. only if copy exists + */ + if (table->table_list) + table->table_list->table_in_update_from_clause= 1; + } + /* + Is there tables of subqueries? + */ + if (&lex->select_lex != lex->all_selects_list) + { + for (table= tables; table; table= table->next) + { + if (table->table_in_update_from_clause) + { + /* + If we check table by local TABLE_LIST copy then we should copy + grants to global table list, because it will be used for table + opening. + */ + if (table->table_list) + table->grant= table->table_list->grant; + } + else + { + if (check_access(thd, SELECT_ACL, table->db, + &table->grant.privilege, 0, 0) || + grant_option && check_grant(thd, SELECT_ACL, table, 0, 1, 0)) + DBUG_RETURN(1); + } + } + } + + if (select_lex->order_list.elements) + msg= "ORDER BY"; + else if (select_lex->select_limit && select_lex->select_limit != + HA_POS_ERROR) + msg= "LIMIT"; + if (msg) + { + my_error(ER_WRONG_USAGE, MYF(0), "UPDATE", msg); + DBUG_RETURN(-1); + } + DBUG_RETURN(0); +} + +/* + Multi delete query pre-check + + SYNOPSIS + multi_delete_precheck() + thd Thread handler + tables Global table list + table_count Pointer to table counter + + RETURN VALUE + 0 OK + 1 error (message is sent to user) + -1 error (message is not sent to user) +*/ +int multi_delete_precheck(THD *thd, TABLE_LIST *tables, uint *table_count) +{ + DBUG_ENTER("multi_delete_precheck"); + SELECT_LEX *select_lex= &thd->lex->select_lex; + TABLE_LIST *aux_tables= + (TABLE_LIST *)thd->lex->auxilliary_table_list.first; + TABLE_LIST *delete_tables= (TABLE_LIST *)select_lex->table_list.first; + TABLE_LIST *target_tbl; + + *table_count= 0; + + /* sql_yacc guarantees that tables and aux_tables are not zero */ + DBUG_ASSERT(aux_tables != 0); + if (check_db_used(thd, tables) || check_db_used(thd,aux_tables) || + check_table_access(thd,SELECT_ACL, tables,0) || + check_table_access(thd,DELETE_ACL, aux_tables,0)) + DBUG_RETURN(1); + if ((thd->options & OPTION_SAFE_UPDATES) && !select_lex->where) + { + my_error(ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE, MYF(0)); + DBUG_RETURN(-1); + } + for (target_tbl= aux_tables; target_tbl; target_tbl= target_tbl->next) + { + (*table_count)++; + /* All tables in aux_tables must be found in FROM PART */ + TABLE_LIST *walk; + for (walk= delete_tables; walk; walk= walk->next) + { + if (!my_strcasecmp(table_alias_charset, + target_tbl->alias, walk->alias) && + !strcmp(walk->db, target_tbl->db)) + break; + } + if (!walk) + { + my_error(ER_UNKNOWN_TABLE, MYF(0), target_tbl->real_name, + "MULTI DELETE"); + DBUG_RETURN(-1); + } + if (walk->derived) + { + my_error(ER_NON_UPDATABLE_TABLE, MYF(0), target_tbl->real_name, + "DELETE"); + DBUG_RETURN(-1); + } + walk->lock_type= target_tbl->lock_type; + target_tbl->table_list= walk; // Remember corresponding table + } + DBUG_RETURN(0); +} + + +/* + INSERT ... SELECT query pre-check + + SYNOPSIS + multi_delete_precheck() + thd Thread handler + tables Global table list + + RETURN VALUE + 0 OK + 1 Error (message is sent to user) + -1 Error (message is not sent to user) +*/ + +int insert_select_precheck(THD *thd, TABLE_LIST *tables) +{ + DBUG_ENTER("insert_select_precheck"); + /* + Check that we have modify privileges for the first table and + select privileges for the rest + */ + ulong privilege= (thd->lex->duplicates == DUP_REPLACE ? + INSERT_ACL | DELETE_ACL : INSERT_ACL); + DBUG_RETURN(check_one_table_access(thd, privilege, tables) ? 1 : 0); +} + + +/* + simple UPDATE query pre-check + + SYNOPSIS + update_precheck() + thd Thread handler + tables Global table list + + RETURN VALUE + 0 OK + 1 Error (message is sent to user) + -1 Error (message is not sent to user) +*/ + +int update_precheck(THD *thd, TABLE_LIST *tables) +{ + DBUG_ENTER("update_precheck"); + if (thd->lex->select_lex.item_list.elements != thd->lex->value_list.elements) + { + my_error(ER_WRONG_VALUE_COUNT, MYF(0)); + DBUG_RETURN(-1); + } + DBUG_RETURN((check_db_used(thd, tables) || + check_one_table_access(thd, UPDATE_ACL, tables)) ? 1 : 0); +} + + +/* + simple DELETE query pre-check + + SYNOPSIS + delete_precheck() + thd Thread handler + tables Global table list + + RETURN VALUE + 0 OK + 1 error (message is sent to user) + -1 error (message is not sent to user) +*/ + +int delete_precheck(THD *thd, TABLE_LIST *tables) +{ + DBUG_ENTER("delete_precheck"); + if (check_one_table_access(thd, DELETE_ACL, tables)) + DBUG_RETURN(1); + /* Set privilege for the WHERE clause */ + tables->grant.want_privilege=(SELECT_ACL & ~tables->grant.privilege); + DBUG_RETURN(0); +} + + +/* + simple INSERT query pre-check + + SYNOPSIS + insert_precheck() + thd Thread handler + tables Global table list + + RETURN VALUE + 0 OK + 1 error (message is sent to user) + -1 error (message is not sent to user) +*/ + +int insert_precheck(THD *thd, TABLE_LIST *tables, bool update) +{ + LEX *lex= thd->lex; + DBUG_ENTER("insert_precheck"); + + ulong privilege= (lex->duplicates == DUP_REPLACE ? + INSERT_ACL | DELETE_ACL : INSERT_ACL | update); + + if (check_one_table_access(thd, privilege, tables)) + DBUG_RETURN(1); + + if (lex->select_lex.item_list.elements != lex->value_list.elements) + { + my_error(ER_WRONG_VALUE_COUNT, MYF(0)); + DBUG_RETURN(-1); + } + DBUG_RETURN(0); +} + + +/* + CREATE TABLE query pre-check + + SYNOPSIS + create_table_precheck() + thd Thread handler + tables Global table list + create_table Table which will be created + + RETURN VALUE + 0 OK + 1 Error (message is sent to user) + -1 Error (message is not sent to user) +*/ + +int create_table_precheck(THD *thd, TABLE_LIST *tables, + TABLE_LIST *create_table) +{ + LEX *lex= thd->lex; + DBUG_ENTER("create_table_precheck"); + ulong want_priv= ((lex->create_info.options & HA_LEX_CREATE_TMP_TABLE) ? + CREATE_TMP_ACL : CREATE_ACL); + lex->create_info.alias= create_table->alias; + if (check_access(thd, want_priv, create_table->db, + &create_table->grant.privilege, 0, 0) || + check_merge_table_access(thd, create_table->db, + (TABLE_LIST *) + lex->create_info.merge_list.first)) + DBUG_RETURN(1); + DBUG_RETURN((grant_option && want_priv != CREATE_TMP_ACL && + check_grant(thd, want_priv, create_table, 0, UINT_MAX, 0)) ? + 1 : 0); +} diff --git a/sql/sql_prepare.cc b/sql/sql_prepare.cc index af1b85ae8a7..f3ae4052bb4 100644 --- a/sql/sql_prepare.cc +++ b/sql/sql_prepare.cc @@ -92,7 +92,6 @@ public: uint last_errno; char last_error[MYSQL_ERRMSG_SIZE]; bool get_longdata_error; - bool long_data_used; bool log_full_query; #ifndef EMBEDDED_LIBRARY bool (*set_params)(Prepared_statement *st, uchar *data, uchar *data_end, @@ -247,7 +246,9 @@ void set_param_tiny(Item_param *param, uchar **pos, ulong len) if (len < 1) return; #endif - param->set_int((longlong)(**pos)); + int8 value= (int8) **pos; + param->set_int(param->unsigned_flag ? (longlong) ((uint8) value) : + (longlong) value); *pos+= 1; } @@ -257,7 +258,9 @@ void set_param_short(Item_param *param, uchar **pos, ulong len) if (len < 2) return; #endif - param->set_int((longlong)sint2korr(*pos)); + int16 value= sint2korr(*pos); + param->set_int(param->unsigned_flag ? (longlong) ((uint16) value) : + (longlong) value); *pos+= 2; } @@ -267,7 +270,9 @@ void set_param_int32(Item_param *param, uchar **pos, ulong len) if (len < 4) return; #endif - param->set_int((longlong)sint4korr(*pos)); + int32 value= sint4korr(*pos); + param->set_int(param->unsigned_flag ? (longlong) ((uint32) value) : + (longlong) value); *pos+= 4; } @@ -466,12 +471,11 @@ static bool insert_params_withlog(Prepared_statement *stmt, uchar *null_array, { if (is_param_null(null_array, it - begin)) { - param->maybe_null= param->null_value= 1; + param->set_null(); res= &my_null_string; } else { - param->maybe_null= param->null_value= 0; if (read_pos >= data_end) DBUG_RETURN(1); param->set_param_func(param, &read_pos, data_end - read_pos); @@ -504,10 +508,9 @@ static bool insert_params(Prepared_statement *stmt, uchar *null_array, if (!param->long_data_supplied) { if (is_param_null(null_array, it - begin)) - param->maybe_null= param->null_value= 1; + param->set_null(); else { - param->maybe_null= param->null_value= 0; if (read_pos >= data_end) DBUG_RETURN(1); param->set_param_func(param, &read_pos, data_end - read_pos); @@ -536,10 +539,16 @@ static bool setup_conversion_functions(Prepared_statement *stmt, Item_param **end= it + stmt->param_count; for (; it < end; ++it) { + ushort typecode; + const uint signed_bit= 1 << 15; + if (read_pos >= data_end) DBUG_RETURN(1); - setup_one_conversion_function(*it, *read_pos); + + typecode= sint2korr(read_pos); read_pos+= 2; + (**it).unsigned_flag= test(typecode & signed_bit); + setup_one_conversion_function(*it, (uchar) (typecode & ~signed_bit)); } } *data= read_pos; @@ -563,11 +572,10 @@ static bool emb_insert_params(Prepared_statement *stmt) if (!param->long_data_supplied) { if (*client_param->is_null) - param->maybe_null= param->null_value= 1; + param->set_null(); else { uchar *buff= (uchar*)client_param->buffer; - param->maybe_null= param->null_value= 0; param->set_param_func(param, &buff, client_param->length ? *client_param->length : @@ -605,13 +613,12 @@ static bool emb_insert_params_withlog(Prepared_statement *stmt) { if (*client_param->is_null) { - param->maybe_null= param->null_value= 1; + param->set_null(); res= &my_null_string; } else { uchar *buff= (uchar*)client_param->buffer; - param->maybe_null= param->null_value= 0; param->set_param_func(param, &buff, client_param->length ? *client_param->length : @@ -632,41 +639,42 @@ static bool emb_insert_params_withlog(Prepared_statement *stmt) #endif /*!EMBEDDED_LIBRARY*/ + /* - Validate the following information for INSERT statement: - - field existence - - fields count + Validate INSERT statement: + SYNOPSIS - mysql_test_insert_fields() + mysql_test_insert() + stmt prepared statemen handler + tables list of tables queries + RETURN VALUE 0 ok 1 error, sent to the client -1 error, not sent to client */ - -static int mysql_test_insert_fields(Prepared_statement *stmt, - TABLE_LIST *table_list, - List<Item> &fields, - List<List_item> &values_list) +static int mysql_test_insert(Prepared_statement *stmt, + TABLE_LIST *table_list, + List<Item> &fields, + List<List_item> &values_list, + List<Item> &update_fields, + List<Item> &update_values, + enum_duplicates duplic) { THD *thd= stmt->thd; - TABLE *table; + LEX *lex= stmt->lex; List_iterator_fast<List_item> its(values_list); List_item *values; - + int res= -1; + TABLE_LIST *insert_table_list= + (TABLE_LIST*) lex->select_lex.table_list.first; + my_bool update= (lex->value_list.elements ? UPDATE_ACL : 0); DBUG_ENTER("mysql_test_insert_fields"); -#ifndef NO_EMBEDDED_ACCESS_CHECKS - my_bool update=(stmt->lex->value_list.elements ? UPDATE_ACL : 0); - ulong privilege= (stmt->lex->duplicates == DUP_REPLACE ? - INSERT_ACL | DELETE_ACL : INSERT_ACL | update); - if (check_access(thd,privilege,table_list->db, - &table_list->grant.privilege,0,0) || - (grant_option && check_grant(thd,privilege,table_list,0,0))) - DBUG_RETURN(1); -#endif + if ((res= insert_precheck(thd, table_list, update))) + DBUG_RETURN(res); - /* + /* open temporary memory pool for temporary data allocated by derived tables & preparation procedure */ @@ -677,20 +685,16 @@ static int mysql_test_insert_fields(Prepared_statement *stmt, DBUG_RETURN(-1); } - table= table_list->table; - if ((values= its++)) { uint value_count; ulong counter= 0; - - if (check_insert_fields(thd,table,fields,*values,1)) - { - thd->free_temporary_memory_pool_for_ps_preparing(); - DBUG_RETURN(-1); - } - thd->free_temporary_memory_pool_for_ps_preparing(); + if ((res= mysql_prepare_insert(thd, table_list, insert_table_list, + table_list->table, fields, values, + update_fields, update_values, duplic))) + goto error; + value_count= values->elements; its.rewind(); @@ -702,99 +706,143 @@ static int mysql_test_insert_fields(Prepared_statement *stmt, my_printf_error(ER_WRONG_VALUE_COUNT_ON_ROW, ER(ER_WRONG_VALUE_COUNT_ON_ROW), MYF(0), counter); - DBUG_RETURN(-1); + goto error; } + if (setup_fields(thd, 0, insert_table_list, *values, 0, 0, 0)) + goto error; } } + + res= 0; +error: + lex->unit.cleanup(); + thd->free_temporary_memory_pool_for_ps_preparing(); + DBUG_RETURN(res); +} + + +/* + Validate UPDATE statement + + SYNOPSIS + mysql_test_delete() + stmt prepared statemen handler + tables list of tables queries + + RETURN VALUE + 0 success + 1 error, sent to client + -1 error, not sent to client +*/ +static int mysql_test_update(Prepared_statement *stmt, + TABLE_LIST *table_list) +{ + int res; + THD *thd= stmt->thd; + SELECT_LEX *select= &stmt->lex->select_lex; + DBUG_ENTER("mysql_test_update"); + + if ((res= update_precheck(thd, table_list))) + DBUG_RETURN(res); + + /* + open temporary memory pool for temporary data allocated by derived + tables & preparation procedure + */ + thd->allocate_temporary_memory_pool_for_ps_preparing(); + + if (open_and_lock_tables(thd, table_list)) + res= -1; else { - thd->free_temporary_memory_pool_for_ps_preparing(); + TABLE_LIST *update_table_list= (TABLE_LIST *)select->table_list.first; + if (!(res= mysql_prepare_update(thd, table_list, + update_table_list, + &select->where, + select->order_list.elements, + (ORDER *) select->order_list.first))) + { + if (setup_fields(thd, 0, update_table_list, + select->item_list, 1, 0, 0) || + setup_fields(thd, 0, update_table_list, + stmt->lex->value_list, 0, 0, 0)) + res= -1; + } + stmt->lex->unit.cleanup(); } - DBUG_RETURN(0); + thd->free_temporary_memory_pool_for_ps_preparing(); + /* TODO: here we should send types of placeholders to the client. */ + DBUG_RETURN(res); } /* - Validate the following information: - UPDATE - set and where clause - DELETE - where clause + Validate DELETE statement + SYNOPSIS - mysql_test_upd_fields() + mysql_test_delete() + stmt prepared statemen handler + tables list of tables queries + RETURN VALUE 0 success 1 error, sent to client -1 error, not sent to client */ - -static int mysql_test_upd_fields(Prepared_statement *stmt, - TABLE_LIST *table_list, - List<Item> &fields, List<Item> &values, - COND *conds) +static int mysql_test_delete(Prepared_statement *stmt, + TABLE_LIST *table_list) { + int res; THD *thd= stmt->thd; + LEX *lex= stmt->lex; + DBUG_ENTER("mysql_test_delete"); - DBUG_ENTER("mysql_test_upd_fields"); -#ifndef NO_EMBEDDED_ACCESS_CHECKS - if (check_access(thd,UPDATE_ACL,table_list->db, - &table_list->grant.privilege,0,0) || - (grant_option && check_grant(thd,UPDATE_ACL,table_list,0,0))) - DBUG_RETURN(1); -#endif + if ((res= delete_precheck(thd, table_list))) + DBUG_RETURN(res); - /* + /* open temporary memory pool for temporary data allocated by derived tables & preparation procedure */ thd->allocate_temporary_memory_pool_for_ps_preparing(); if (open_and_lock_tables(thd, table_list)) - goto err; - if (setup_tables(table_list) || - setup_fields(thd, 0, table_list, fields, 1, 0, 0) || - setup_conds(thd, table_list, &conds) || thd->net.report_error) - goto err; - - thd->free_temporary_memory_pool_for_ps_preparing(); - - /* TODO: here we should send types of placeholders to the client. */ - DBUG_RETURN(0); -err: + res= -1; + else + { + res= mysql_prepare_delete(thd, table_list, &lex->select_lex.where); + lex->unit.cleanup(); + } thd->free_temporary_memory_pool_for_ps_preparing(); - DBUG_RETURN(-1); + /* TODO: here we should send types of placeholders to the client. */ + DBUG_RETURN(res); } + /* - Validate the following information: - SELECT - column list - - where clause - - order clause - - having clause - - group by clause - - if no column spec i.e. '*', then setup all fields + Validate SELECT statement. In case of success, if this query is not EXPLAIN, send column list info back to client. + SYNOPSIS - mysql_test_select_fields() + mysql_test_select() + stmt prepared statemen handler + tables list of tables queries + RETURN VALUE 0 success 1 error, sent to client -1 error, not sent to client */ -static int mysql_test_select_fields(Prepared_statement *stmt, - TABLE_LIST *tables, - uint wild_num, - List<Item> &fields, COND *conds, - uint og_num, ORDER *order, ORDER *group, - Item *having, ORDER *proc, - ulong select_options, - SELECT_LEX_UNIT *unit, - SELECT_LEX *select_lex) +static int mysql_test_select(Prepared_statement *stmt, + TABLE_LIST *tables) { THD *thd= stmt->thd; LEX *lex= stmt->lex; + SELECT_LEX_UNIT *unit= &lex->unit; - DBUG_ENTER("mysql_test_select_fields"); + DBUG_ENTER("mysql_test_select"); #ifndef NO_EMBEDDED_ACCESS_CHECKS ulong privilege= lex->exchange ? SELECT_ACL | FILE_ACL : SELECT_ACL; @@ -806,11 +854,8 @@ static int mysql_test_select_fields(Prepared_statement *stmt, else if (check_access(thd, privilege, any_db,0,0,0)) DBUG_RETURN(1); #endif - if ((&lex->select_lex != lex->all_selects_list && - lex->unit.create_total_list(thd, lex, &tables))) - DBUG_RETURN(1); - /* + /* open temporary memory pool for temporary data allocated by derived tables & preparation procedure */ @@ -826,25 +871,19 @@ static int mysql_test_select_fields(Prepared_statement *stmt, if (send_prep_stmt(stmt, 0)) goto err; } - else + else { - select_result *result= lex->result; - if (!result && !(result= new select_send())) - { - send_error(thd, ER_OUT_OF_RESOURCES); - goto err; - } - thd->used_tables= 0; // Updated by setup_fields - if (unit->prepare(thd, result, 0)) + // JOIN::prepare calls + if (unit->prepare(thd, 0, 0)) { send_error(thd); goto err_prep; } - if (send_prep_stmt(stmt, fields.elements) || - thd->protocol_simple.send_fields(&fields, 0) + if (send_prep_stmt(stmt, lex->select_lex.item_list.elements) || + thd->protocol_simple.send_fields(&lex->select_lex.item_list, 0) #ifndef EMBEDDED_LIBRARY || net_flush(&thd->net) #endif @@ -865,6 +904,261 @@ err: /* + Validate and prepare for execution DO statement expressions + + SYNOPSIS + mysql_test_do_fields() + stmt prepared statemen handler + tables list of tables queries + values list of expressions + + RETURN VALUE + 0 success + 1 error, sent to client + -1 error, not sent to client +*/ + +static int mysql_test_do_fields(Prepared_statement *stmt, + TABLE_LIST *tables, + List<Item> *values) +{ + DBUG_ENTER("mysql_test_do_fields"); + THD *thd= stmt->thd; + int res= 0; + if (tables && (res= check_table_access(thd, SELECT_ACL, tables, 0))) + DBUG_RETURN(res); + /* + open temporary memory pool for temporary data allocated by derived + tables & preparation procedure + */ + thd->allocate_temporary_memory_pool_for_ps_preparing(); + if (tables && (res= open_and_lock_tables(thd, tables))) + { + thd->free_temporary_memory_pool_for_ps_preparing(); + DBUG_RETURN(res); + } + res= setup_fields(thd, 0, 0, *values, 0, 0, 0); + stmt->lex->unit.cleanup(); + thd->free_temporary_memory_pool_for_ps_preparing(); + if (res) + DBUG_RETURN(-1); + DBUG_RETURN(0); +} + + +/* + Validate and prepare for execution SET statement expressions + + SYNOPSIS + mysql_test_set_fields() + stmt prepared statemen handler + tables list of tables queries + values list of expressions + + RETURN VALUE + 0 success + 1 error, sent to client + -1 error, not sent to client +*/ +static int mysql_test_set_fields(Prepared_statement *stmt, + TABLE_LIST *tables, + List<set_var_base> *var_list) +{ + DBUG_ENTER("mysql_test_set_fields"); + List_iterator_fast<set_var_base> it(*var_list); + THD *thd= stmt->thd; + set_var_base *var; + int res= 0; + + if (tables && (res= check_table_access(thd, SELECT_ACL, tables, 0))) + DBUG_RETURN(res); + /* + open temporary memory pool for temporary data allocated by derived + tables & preparation procedure + */ + thd->allocate_temporary_memory_pool_for_ps_preparing(); + if (tables && (res= open_and_lock_tables(thd, tables))) + goto error; + while ((var= it++)) + { + if (var->light_check(thd)) + { + stmt->lex->unit.cleanup(); + res= -1; + goto error; + } + } +error: + stmt->lex->unit.cleanup(); + thd->free_temporary_memory_pool_for_ps_preparing(); + DBUG_RETURN(res); +} + + +/* + Check internal SELECT of the prepared command + + SYNOPSIS + select_like_statement_test() + stmt - prepared table handler + tables - global list of tables + + RETURN VALUE + 0 success + 1 error, sent to client + -1 error, not sent to client +*/ +static int select_like_statement_test(Prepared_statement *stmt, + TABLE_LIST *tables) +{ + DBUG_ENTER("select_like_statement_test"); + THD *thd= stmt->thd; + LEX *lex= stmt->lex; + int res= 0; + /* + open temporary memory pool for temporary data allocated by derived + tables & preparation procedure + */ + thd->allocate_temporary_memory_pool_for_ps_preparing(); + if (tables && (res= open_and_lock_tables(thd, tables))) + goto end; + + thd->used_tables= 0; // Updated by setup_fields + + // JOIN::prepare calls + if (lex->unit.prepare(thd, 0, 0)) + { + res= thd->net.report_error ? -1 : 1; + } +end: + lex->unit.cleanup(); + thd->free_temporary_memory_pool_for_ps_preparing(); + DBUG_RETURN(res); +} + + +/* + Validate and prepare for execution CRETE TABLE statement + + SYNOPSIS + mysql_test_create_table() + stmt prepared statemen handler + tables list of tables queries + + RETURN VALUE + 0 success + 1 error, sent to client + -1 error, not sent to client +*/ +static int mysql_test_create_table(Prepared_statement *stmt, + TABLE_LIST *tables) +{ + DBUG_ENTER("mysql_test_create_table"); + THD *thd= stmt->thd; + LEX *lex= stmt->lex; + int res= 0; + + /* Skip first table, which is the table we are creating */ + TABLE_LIST *create_table, *create_table_local; + tables= lex->unlink_first_table(tables, &create_table, + &create_table_local); + + if (!(res= create_table_precheck(thd, tables, create_table)) && + lex->select_lex.item_list.elements) + res= select_like_statement_test(stmt, tables); + + /* put tables back for PS rexecuting */ + tables= lex->link_first_table_back(tables, create_table, + create_table_local); + DBUG_RETURN(res); +} + + +/* + Validate and prepare for execution multy update statement + + SYNOPSIS + mysql_test_multiupdate() + stmt prepared statemen handler + tables list of tables queries + + RETURN VALUE + 0 success + 1 error, sent to client + -1 error, not sent to client +*/ +static int mysql_test_multiupdate(Prepared_statement *stmt, + TABLE_LIST *tables) +{ + int res; + if ((res= multi_update_precheck(stmt->thd, tables))) + return res; + return select_like_statement_test(stmt, tables); +} + + +/* + Validate and prepare for execution multy delete statement + + SYNOPSIS + mysql_test_multidelete() + stmt prepared statemen handler + tables list of tables queries + + RETURN VALUE + 0 success + 1 error, sent to client + -1 error, not sent to client +*/ +static int mysql_test_multidelete(Prepared_statement *stmt, + TABLE_LIST *tables) +{ + int res; + stmt->thd->lex->current_select= &stmt->thd->lex->select_lex; + if (add_item_to_list(stmt->thd, new Item_null())) + return -1; + + uint fake_counter; + if ((res= multi_delete_precheck(stmt->thd, tables, &fake_counter))) + return res; + return select_like_statement_test(stmt, tables); +} + + +/* + Validate and prepare for execution INSERT ... SELECT statement + + SYNOPSIS + mysql_test_insert_select() + stmt prepared statemen handler + tables list of tables queries + + RETURN VALUE + 0 success + 1 error, sent to client + -1 error, not sent to client +*/ +static int mysql_test_insert_select(Prepared_statement *stmt, + TABLE_LIST *tables) +{ + int res; + LEX *lex= stmt->lex; + if ((res= insert_select_precheck(stmt->thd, tables))) + return res; + TABLE_LIST *first_local_table= + (TABLE_LIST *)lex->select_lex.table_list.first; + /* Skip first table, which is the table we are inserting in */ + lex->select_lex.table_list.first= (byte*) first_local_table->next; + lex->select_lex.resolve_mode= SELECT_LEX::NOMATTER_MODE; + res= select_like_statement_test(stmt, tables); + /* revert changes*/ + lex->select_lex.table_list.first= (byte*) first_local_table; + lex->select_lex.resolve_mode= SELECT_LEX::INSERT_MODE; + return res; +} + + +/* Send the prepare query results back to client SYNOPSIS send_prepare_results() @@ -873,7 +1167,6 @@ err: 0 success 1 error, sent to client */ - static int send_prepare_results(Prepared_statement *stmt) { THD *thd= stmt->thd; @@ -881,53 +1174,95 @@ static int send_prepare_results(Prepared_statement *stmt) SELECT_LEX *select_lex= &lex->select_lex; TABLE_LIST *tables=(TABLE_LIST*) select_lex->table_list.first; enum enum_sql_command sql_command= lex->sql_command; - int res; - + int res= 0; DBUG_ENTER("send_prepare_results"); + DBUG_PRINT("enter",("command: %d, param_count: %ld", sql_command, stmt->param_count)); + + if (select_lex != lex->all_selects_list && + lex->unit.create_total_list(thd, lex, &tables)) + DBUG_RETURN(1); + switch (sql_command) { - + case SQLCOM_REPLACE: case SQLCOM_INSERT: - if ((res= mysql_test_insert_fields(stmt, tables, lex->field_list, - lex->many_values))) - goto error; + res= mysql_test_insert(stmt, tables, lex->field_list, + lex->many_values, + select_lex->item_list, lex->value_list, + (lex->value_list.elements ? + DUP_UPDATE : lex->duplicates)); break; case SQLCOM_UPDATE: - /* XXX: fallthrough */ + res= mysql_test_update(stmt, tables); + break; + case SQLCOM_DELETE: - if ((res= mysql_test_upd_fields(stmt, tables, select_lex->item_list, - lex->value_list, select_lex->where))) - goto error; + res= mysql_test_delete(stmt, tables); break; case SQLCOM_SELECT: - if ((res= mysql_test_select_fields(stmt, tables, select_lex->with_wild, - select_lex->item_list, - select_lex->where, - select_lex->order_list.elements + - select_lex->group_list.elements, - (ORDER*) select_lex->order_list.first, - (ORDER*) select_lex->group_list.first, - select_lex->having, - (ORDER*)lex->proc_list.first, - select_lex->options | thd->options, - &(lex->unit), select_lex))) + if ((res= mysql_test_select(stmt, tables))) goto error; /* Statement and field info has already been sent */ DBUG_RETURN(0); + case SQLCOM_CREATE_TABLE: + res= mysql_test_create_table(stmt, tables); + break; + + case SQLCOM_DO: + res= mysql_test_do_fields(stmt, tables, lex->insert_list); + break; + + case SQLCOM_SET_OPTION: + res= mysql_test_set_fields(stmt, tables, &lex->var_list); + break; + + case SQLCOM_DELETE_MULTI: + res= mysql_test_multidelete(stmt, tables); + break; + + case SQLCOM_UPDATE_MULTI: + res= mysql_test_multiupdate(stmt, tables); + break; + + case SQLCOM_INSERT_SELECT: + res= mysql_test_insert_select(stmt, tables); + break; + + case SQLCOM_SHOW_DATABASES: + case SQLCOM_SHOW_PROCESSLIST: + case SQLCOM_SHOW_STORAGE_ENGINES: + case SQLCOM_SHOW_PRIVILEGES: + case SQLCOM_SHOW_COLUMN_TYPES: + case SQLCOM_SHOW_STATUS: + case SQLCOM_SHOW_VARIABLES: + case SQLCOM_SHOW_LOGS: + case SQLCOM_SHOW_TABLES: + case SQLCOM_SHOW_OPEN_TABLES: + case SQLCOM_SHOW_CHARSETS: + case SQLCOM_SHOW_COLLATIONS: + case SQLCOM_SHOW_FIELDS: + case SQLCOM_SHOW_KEYS: + case SQLCOM_SHOW_CREATE_DB: + case SQLCOM_SHOW_GRANTS: + case SQLCOM_DROP_TABLE: + case SQLCOM_RENAME_TABLE: + break; + default: - /* - Rest fall through to default category, no parsing - for non-DML statements + /* + All other is not supported yet */ - break; + res= -1; + my_error(ER_UNSUPPORTED_PS, MYF(0)); + goto error; } - DBUG_RETURN(send_prep_stmt(stmt, 0)); - + if (res == 0) + DBUG_RETURN(send_prep_stmt(stmt, 0)); error: if (res < 0) send_error(thd,thd->killed_errno()); @@ -1119,6 +1454,24 @@ static void reset_stmt_for_execute(Prepared_statement *stmt) } } + +/* + Clears parameters from data left from previous execution or long data + + SYNOPSIS + reset_stmt_params() + stmt - prepared statement for which parameters should be reset +*/ + +static void reset_stmt_params(Prepared_statement *stmt) +{ + Item_param **item= stmt->param_array; + Item_param **end= item + stmt->param_count; + for (;item < end ; ++item) + (**item).reset(); +} + + /* Executes previously prepared query. If there is any parameters, then replace markers with the data supplied @@ -1153,9 +1506,7 @@ void mysql_stmt_execute(THD *thd, char *packet, uint packet_length) thd->stmt_backup.set_statement(thd); thd->set_statement(stmt); - reset_stmt_for_execute(stmt); - #ifndef EMBEDDED_LIBRARY if (stmt->param_count) { @@ -1185,17 +1536,25 @@ void mysql_stmt_execute(THD *thd, char *packet, uint packet_length) */ thd->protocol= &thd->protocol_prep; // Switch to binary protocol mysql_execute_command(thd); + thd->lex->unit.cleanup(); thd->protocol= &thd->protocol_simple; // Use normal protocol if (!(specialflag & SPECIAL_NO_PRIOR)) my_pthread_setprio(pthread_self(), WAIT_PRIOR); cleanup_items(stmt->free_list); + reset_stmt_params(stmt); close_thread_tables(thd); // to close derived tables thd->set_statement(&thd->stmt_backup); + /* + Free Items that were created during this execution of the PS by query + optimizer. + */ + free_items(thd->free_list); DBUG_VOID_RETURN; set_params_data_err: + reset_stmt_params(stmt); thd->set_statement(&thd->stmt_backup); my_error(ER_WRONG_ARGUMENTS, MYF(0), "mysql_execute"); send_error(thd); @@ -1204,18 +1563,20 @@ set_params_data_err: /* - Reset a prepared statement, in case there was an error in send_longdata. - Note: we don't send any reply to that command. + Reset a prepared statement in case there was a recoverable error. SYNOPSIS mysql_stmt_reset() thd Thread handle packet Packet with stmt id DESCRIPTION - This function is useful when one gets an error after calling - mysql_stmt_getlongdata() and wants to reset the handle - so that one can call execute again. - See also bug #1664 + This function resets statement to the state it was right after prepare. + It can be used to: + - clear an error happened during mysql_stmt_send_long_data + - cancel long data stream for all placeholders without + having to call mysql_stmt_execute. + Sends 'OK' packet in case of success (statement was reset) + or 'ERROR' packet (unrecoverable error/statement not found/etc). */ void mysql_stmt_reset(THD *thd, char *packet) @@ -1226,20 +1587,19 @@ void mysql_stmt_reset(THD *thd, char *packet) DBUG_ENTER("mysql_stmt_reset"); - if (!(stmt= find_prepared_statement(thd, stmt_id, "reset", DONT_SEND_ERROR))) + if (!(stmt= find_prepared_statement(thd, stmt_id, "reset", SEND_ERROR))) DBUG_VOID_RETURN; stmt->get_longdata_error= 0; - /* Free long data if used */ - if (stmt->long_data_used) - { - Item_param **item= stmt->param_array; - Item_param **end= item + stmt->param_count; - stmt->long_data_used= 0; - for (; item < end ; item++) - (**item).reset(); - } + /* + Clear parameters from data which could be set by + mysql_stmt_send_long_data() call. + */ + reset_stmt_params(stmt); + + send_ok(thd); + DBUG_VOID_RETURN; } @@ -1325,7 +1685,6 @@ void mysql_stmt_get_longdata(THD *thd, char *pos, ulong packet_length) #else param->set_longdata(thd->extra_data, thd->extra_length); #endif - stmt->long_data_used= 1; DBUG_VOID_RETURN; } @@ -1337,7 +1696,6 @@ Prepared_statement::Prepared_statement(THD *thd_arg) param_count(0), last_errno(0), get_longdata_error(0), - long_data_used(0), log_full_query(0) { *last_error= '\0'; diff --git a/sql/sql_select.cc b/sql/sql_select.cc index 6004778781d..1de8d4daf9a 100644 --- a/sql/sql_select.cc +++ b/sql/sql_select.cc @@ -262,15 +262,19 @@ inline int setup_without_group(THD *thd, Item **ref_pointer_array, ORDER *order, ORDER *group, bool *hidden_group_fields) { - bool save_allow_sum_func= thd->allow_sum_func; + bool save_allow_sum_func; + int res; + DBUG_ENTER("setup_without_group"); + + save_allow_sum_func= thd->allow_sum_func; thd->allow_sum_func= 0; - int res= (setup_conds(thd, tables, conds) || - setup_order(thd, ref_pointer_array, tables, fields, all_fields, - order) || - setup_group(thd, ref_pointer_array, tables, fields, all_fields, - group, hidden_group_fields)); + res= (setup_conds(thd, tables, conds) || + setup_order(thd, ref_pointer_array, tables, fields, all_fields, + order) || + setup_group(thd, ref_pointer_array, tables, fields, all_fields, + group, hidden_group_fields)); thd->allow_sum_func= save_allow_sum_func; - return res; + DBUG_RETURN(res); } /***************************************************************************** @@ -279,7 +283,7 @@ inline int setup_without_group(THD *thd, Item **ref_pointer_array, *****************************************************************************/ /* - Prepare of whole select (including subselect in future). + Prepare of whole select (including sub queries in future). return -1 on error 0 on success */ @@ -440,7 +444,7 @@ JOIN::prepare(Item ***rref_pointer_array, goto err; } #endif - if (!procedure && result->prepare(fields_list, unit_arg)) + if (!procedure && result && result->prepare(fields_list, unit_arg)) goto err; /* purecov: inspected */ if (select_lex->olap == ROLLUP_TYPE && rollup_init()) @@ -1052,6 +1056,13 @@ JOIN::reinit() if (tmp_join) restore_tmp(); + if (sum_funcs) + { + Item_sum *func, **func_ptr= sum_funcs; + while ((func= *(func_ptr++))) + func->clear(); + } + DBUG_RETURN(0); } @@ -1116,8 +1127,7 @@ JOIN::exec() if (zero_result_cause) { (void) return_zero_rows(this, result, tables_list, fields_list, - tmp_table_param.sum_func_count != 0 && - !group_list, + send_row_on_empty_set(), select_options, zero_result_cause, having, procedure, @@ -2654,8 +2664,6 @@ find_best(JOIN *join,table_map rest_tables,uint idx,double record_count, ha_rows rec; double tmp; THD *thd= join->thd; - if (thd->killed) // Abort - return; if (!rest_tables) { @@ -3631,6 +3639,12 @@ make_join_readinfo(JOIN *join, uint options) table->status=STATUS_NO_RECORD; tab->read_first_record= join_read_const; tab->read_record.read_record= join_no_more_records; + if (table->used_keys.is_set(tab->ref.key) && + !table->no_keyread) + { + table->key_read=1; + table->file->extra(HA_EXTRA_KEYREAD); + } break; case JT_EQ_REF: table->status=STATUS_NO_RECORD; @@ -3747,7 +3761,8 @@ make_join_readinfo(JOIN *join, uint options) table->key_read=1; table->file->extra(HA_EXTRA_KEYREAD); } - else if (!table->used_keys.is_clear_all() && ! (tab->select && tab->select->quick)) + else if (!table->used_keys.is_clear_all() && + !(tab->select && tab->select->quick)) { // Only read index tree tab->index=find_shortest_key(table, & table->used_keys); tab->table->file->index_init(tab->index); @@ -4116,11 +4131,6 @@ return_zero_rows(JOIN *join, select_result *result,TABLE_LIST *tables, DBUG_RETURN(0); } - if (procedure) - { - if (result->prepare(fields, unit)) // This hasn't been done yet - DBUG_RETURN(-1); - } if (send_row) { for (TABLE_LIST *table=tables; table ; table=table->next) @@ -5668,6 +5678,8 @@ do_select(JOIN *join,List<Item> *fields,TABLE *table,Procedure *procedure) if (!(error=(*end_select)(join,join_tab,0)) || error == -3) error=(*end_select)(join,join_tab,1); } + else if (join->send_row_on_empty_set()) + error= join->result->send_data(*join->fields); } else { @@ -5956,6 +5968,12 @@ join_read_const_table(JOIN_TAB *tab, POSITION *pos) } else { + if (!table->key_read && table->used_keys.is_set(tab->ref.key) && + !table->no_keyread) + { + table->key_read=1; + table->file->extra(HA_EXTRA_KEYREAD); + } if ((error=join_read_const(tab))) { tab->info="unique row not found"; @@ -6908,6 +6926,7 @@ static int test_if_order_by_key(ORDER *order, TABLE *table, uint idx, key_part_end=key_part+table->key_info[idx].key_parts; key_part_map const_key_parts=table->const_key_parts[idx]; int reverse=0; + DBUG_ENTER("test_if_order_by_key"); for (; order ; order=order->next, const_key_parts>>=1) { @@ -6918,25 +6937,24 @@ static int test_if_order_by_key(ORDER *order, TABLE *table, uint idx, Skip key parts that are constants in the WHERE clause. These are already skipped in the ORDER BY by const_expression_in_where() */ - while (const_key_parts & 1) - { - key_part++; const_key_parts>>=1; - } + for (; const_key_parts & 1 ; const_key_parts>>= 1) + key_part++; + if (key_part == key_part_end || key_part->field != field) - return 0; + DBUG_RETURN(0); /* set flag to 1 if we can use read-next on key, else to -1 */ - flag=(order->asc == !(key_part->key_part_flag & HA_REVERSE_SORT)) - ? 1 : -1; + flag= ((order->asc == !(key_part->key_part_flag & HA_REVERSE_SORT)) ? 1 : -1); if (reverse && flag != reverse) - return 0; + DBUG_RETURN(0); reverse=flag; // Remember if reverse key_part++; } *used_key_parts= (uint) (key_part - table->key_info[idx].key_part); - return reverse; + DBUG_RETURN(reverse); } + uint find_shortest_key(TABLE *table, const key_map *usable_keys) { uint min_length= (uint) ~0; @@ -6959,18 +6977,20 @@ uint find_shortest_key(TABLE *table, const key_map *usable_keys) } /* + Test if a second key is the subkey of the first one. + SYNOPSIS is_subkey() - key_part - first key parts - ref_key_part - second key parts - ref_key_part_end - last+1 part of the second key - DESCRIPTION - Test if a second key is the subkey of the first one. + key_part First key parts + ref_key_part Second key parts + ref_key_part_end Last+1 part of the second key + NOTE Second key MUST be shorter than the first one. + RETURN - 1 - is the subkey - 0 - otherwise + 1 is a subkey + 0 no sub key */ inline bool @@ -6984,20 +7004,21 @@ is_subkey(KEY_PART_INFO *key_part, KEY_PART_INFO *ref_key_part, } /* + Test if we can use one of the 'usable_keys' instead of 'ref' key for sorting + SYNOPSIS test_if_subkey() - ref - number of key, used for WHERE clause - usable_keys - keys for testing - DESCRIPTION - Test if we can use one of the 'usable_keys' instead of 'ref' key. + ref Number of key, used for WHERE clause + usable_keys Keys for testing + RETURN - MAX_KEY - if we can't use other key - the number of found key - otherwise + MAX_KEY If we can't use other key + the number of found key Otherwise */ static uint test_if_subkey(ORDER *order, TABLE *table, uint ref, uint ref_key_parts, - const key_map& usable_keys) + const key_map *usable_keys) { uint nr; uint min_length= (uint) ~0; @@ -7008,7 +7029,7 @@ test_if_subkey(ORDER *order, TABLE *table, uint ref, uint ref_key_parts, for (nr= 0 ; nr < table->keys ; nr++) { - if (usable_keys.is_set(nr) && + if (usable_keys->is_set(nr) && table->key_info[nr].key_length < min_length && table->key_info[nr].key_parts >= ref_key_parts && is_subkey(table->key_info[nr].key_part, ref_key_part, @@ -7052,12 +7073,12 @@ test_if_skip_sort_order(JOIN_TAB *tab,ORDER *order,ha_rows select_limit, if ((*tmp_order->item)->type() != Item::FIELD_ITEM) { usable_keys.clear_all(); - break; + DBUG_RETURN(0); } - usable_keys.intersect( - ((Item_field*) (*tmp_order->item))->field->part_of_sortkey); + usable_keys.intersect(((Item_field*) (*tmp_order->item))-> + field->part_of_sortkey); if (usable_keys.is_clear_all()) - break; // No usable keys + DBUG_RETURN(0); // No usable keys } ref_key= -1; @@ -7096,9 +7117,9 @@ test_if_skip_sort_order(JOIN_TAB *tab,ORDER *order,ha_rows select_limit, keys */ if (table->used_keys.is_set(ref_key)) - usable_keys.merge(table->used_keys); + usable_keys.intersect(table->used_keys); if ((new_ref_key= test_if_subkey(order, table, ref_key, ref_key_parts, - usable_keys)) < MAX_KEY) + &usable_keys)) < MAX_KEY) { /* Found key that can be used to retrieve data in sorted order */ if (tab->ref.key >= 0) @@ -7165,6 +7186,8 @@ test_if_skip_sort_order(JOIN_TAB *tab,ORDER *order,ha_rows select_limit, /* fall through */ } } + else if (select && select->quick) + select->quick->sorted= 1; DBUG_RETURN(1); /* No need to sort */ } } @@ -7306,9 +7329,9 @@ create_sort_index(THD *thd, JOIN *join, ORDER *order, For impossible ranges (like when doing a lookup on NULL on a NOT NULL field, quick will contain an empty record set. */ - if (!(select->quick= tab->type == JT_FT ? - new FT_SELECT(thd, table, tab->ref.key) : - get_quick_select_for_ref(thd, table, &tab->ref))) + if (!(select->quick= (tab->type == JT_FT ? + new FT_SELECT(thd, table, tab->ref.key) : + get_quick_select_for_ref(thd, table, &tab->ref)))) goto err; } } @@ -9284,7 +9307,8 @@ static void select_describe(JOIN *join, bool need_tmp_table, bool need_order, join->best_positions[i]. records_read, 21)); my_bool key_read=table->key_read; - if (tab->type == JT_NEXT && table->used_keys.is_set(tab->index)) + if ((tab->type == JT_NEXT || tab->type == JT_CONST) && + table->used_keys.is_set(tab->index)) key_read=1; if (tab->info) diff --git a/sql/sql_select.h b/sql/sql_select.h index 07b94b0afed..8c58401a7f6 100644 --- a/sql/sql_select.h +++ b/sql/sql_select.h @@ -299,6 +299,11 @@ class JOIN :public Sql_alloc void join_free(bool full); void clear(); bool save_join_tab(); + bool send_row_on_empty_set() + { + return (do_send_rows && tmp_table_param.sum_func_count != 0 && + !group_list); + } }; diff --git a/sql/sql_show.cc b/sql/sql_show.cc index 2e755c419ff..d78a52134dc 100644 --- a/sql/sql_show.cc +++ b/sql/sql_show.cc @@ -181,7 +181,7 @@ int mysqld_show_storage_engines(THD *thd) Protocol *protocol= thd->protocol; DBUG_ENTER("mysqld_show_storage_engines"); - field_list.push_back(new Item_empty_string("Type",10)); + field_list.push_back(new Item_empty_string("Engine",10)); field_list.push_back(new Item_empty_string("Support",10)); field_list.push_back(new Item_empty_string("Comment",80)); @@ -436,7 +436,7 @@ mysql_find_files(THD *thd,List<char> *files, const char *db,const char *path, table_list.db= (char*) db; table_list.real_name=file->name; table_list.grant.privilege=col_access; - if (check_grant(thd,TABLE_ACLS,&table_list,1,1)) + if (check_grant(thd, TABLE_ACLS, &table_list, 1, UINT_MAX, 1)) continue; } #endif @@ -471,7 +471,7 @@ int mysqld_extend_show_tables(THD *thd,const char *db,const char *wild) (void) sprintf(path,"%s/%s",mysql_data_home,db); (void) unpack_dirname(path,path); field_list.push_back(item=new Item_empty_string("Name",NAME_LEN)); - field_list.push_back(item=new Item_empty_string("Type",10)); + field_list.push_back(item=new Item_empty_string("Engine",10)); item->maybe_null=1; field_list.push_back(item=new Item_empty_string("Row_format",10)); item->maybe_null=1; @@ -735,11 +735,11 @@ mysqld_show_fields(THD *thd, TABLE_LIST *table_list,const char *wild, */ protocol->store("CURRENT_TIMESTAMP", system_charset_info); } - else if (field->unireg_check != Field::NEXT_NUMBER && + else if (field->unireg_check != Field::NEXT_NUMBER && !field->is_null()) { // Not null by default type.set(tmp, sizeof(tmp), field->charset()); - field->val_str(&type,&type); + field->val_str(&type); protocol->store(type.ptr(),type.length(),type.charset()); } else if (field->unireg_check == Field::NEXT_NUMBER || @@ -1298,10 +1298,10 @@ store_create_info(THD *thd, TABLE *table, String *packet) else if (!field->is_null()) { // Not null by default type.set(tmp, sizeof(tmp), field->charset()); - field->val_str(&type,&type); + field->val_str(&type); if (type.length()) { - String def_val; + String def_val; /* convert to system_charset_info == utf8 */ def_val.copy(type.ptr(), type.length(), field->charset(), system_charset_info); @@ -2021,6 +2021,7 @@ int mysqld_show(THD *thd, const char *wild, show_var_st *variables, #endif /* HAVE_OPENSSL */ case SHOW_KEY_CACHE_LONG: + case SHOW_KEY_CACHE_CONST_LONG: value= (value-(char*) &dflt_key_cache_var)+ (char*) sql_key_cache; end= int10_to_str(*(long*) value, buff, 10); break; diff --git a/sql/sql_string.cc b/sql/sql_string.cc index 4c30b14cfe5..7c3f9bc5cde 100644 --- a/sql/sql_string.cc +++ b/sql/sql_string.cc @@ -843,7 +843,7 @@ outp: void String::print(String *str) { char *st= (char*)Ptr, *end= st+str_length; - for(; st < end; st++) + for (; st < end; st++) { uchar c= *st; switch (c) diff --git a/sql/sql_table.cc b/sql/sql_table.cc index 54cf052643f..f6087392dc1 100644 --- a/sql/sql_table.cc +++ b/sql/sql_table.cc @@ -36,7 +36,7 @@ static char *make_unique_key_name(const char *field_name,KEY *start,KEY *end); static int copy_data_between_tables(TABLE *from,TABLE *to, List<create_field> &create, enum enum_duplicates handle_duplicates, - uint order_num, ORDER *order, + uint order_num, ORDER *order, ha_rows *copied,ha_rows *deleted); /* @@ -223,7 +223,7 @@ int mysql_rm_table_part2(THD *thd, TABLE_LIST *tables, bool if_exists, ER_BAD_TABLE_ERROR, ER(ER_BAD_TABLE_ERROR), table->real_name); else - error= 1; + error= 1; } else { @@ -236,7 +236,7 @@ int mysql_rm_table_part2(THD *thd, TABLE_LIST *tables, bool if_exists, if (error == HA_ERR_ROW_IS_REFERENCED) { /* the table is referenced by a foreign key constraint */ - foreign_key_error=1; + foreign_key_error=1; } if (!error || error == ENOENT) { @@ -285,11 +285,12 @@ int quick_rm_table(enum db_type base,const char *db, { char path[FN_REFLEN]; int error=0; - (void) sprintf(path,"%s/%s/%s%s",mysql_data_home,db,table_name,reg_ext); + my_snprintf(path, sizeof(path), "%s/%s/%s%s", + mysql_data_home, db, table_name, reg_ext); unpack_filename(path,path); if (my_delete(path,MYF(0))) error=1; /* purecov: inspected */ - sprintf(path,"%s/%s/%s",mysql_data_home,db,table_name); + my_snprintf(path, sizeof(path), "%s/%s/%s", mysql_data_home, db, table_name); unpack_filename(path,path); return ha_delete_table(base,path) || error; } @@ -330,7 +331,7 @@ static int sort_keys(KEY *a, KEY *b) return (a->flags & HA_FULLTEXT) ? 1 : -1; } /* - Prefer original key order. usable_key_parts contains here + Prefer original key order. usable_key_parts contains here the original key position. */ return ((a->usable_key_parts < b->usable_key_parts) ? -1 : @@ -340,15 +341,15 @@ static int sort_keys(KEY *a, KEY *b) /* Check TYPELIB (set or enum) for duplicates - + SYNOPSIS check_duplicates_in_interval() set_or_name "SET" or "ENUM" string for warning message - name name of the checked column - typelib list of values for the column + name name of the checked column + typelib list of values for the column DESCRIPTION - This function prints an warning for each value in list + This function prints an warning for each value in list which has some duplicates on its right RETURN VALUES @@ -381,85 +382,43 @@ void check_duplicates_in_interval(const char *set_or_name, } /* - Create a table + Preparation for table creation SYNOPSIS - mysql_create_table() + mysql_prepare_table() thd Thread object - db Database - table_name Table name create_info Create information (like MAX_ROWS) fields List of fields to create keys List of keys to create - tmp_table Set to 1 if this is an internal temporary table - (From ALTER TABLE) - no_log Don't log the query to binary log. DESCRIPTION - If one creates a temporary table, this is automaticly opened - - no_log is needed for the case of CREATE ... SELECT, - as the logging will be done later in sql_insert.cc - select_field_count is also used for CREATE ... SELECT, - and must be zero for standard create of table. + Prepares the table and key structures for table creation. RETURN VALUES 0 ok -1 error */ -int mysql_create_table(THD *thd,const char *db, const char *table_name, - HA_CREATE_INFO *create_info, - List<create_field> &fields, - List<Key> &keys,bool tmp_table,bool no_log, - uint select_field_count) +int mysql_prepare_table(THD *thd, HA_CREATE_INFO *create_info, + List<create_field> &fields, + List<Key> &keys, bool tmp_table, uint &db_options, + handler *file, KEY *&key_info_buffer, + uint *key_count, int select_field_count) { - char path[FN_REFLEN]; - const char *key_name, *alias; + const char *key_name; create_field *sql_field,*dup_field; - int error= -1; - uint db_options,field,null_fields,blob_columns; + uint field,null_fields,blob_columns; ulong pos; - KEY *key_info,*key_info_buffer; + KEY *key_info; KEY_PART_INFO *key_part_info; - int auto_increment=0; - int timestamps= 0, timestamps_with_niladic= 0; - handler *file; - int field_no,dup_no; - enum db_type new_db_type; - DBUG_ENTER("mysql_create_table"); + int timestamps= 0, timestamps_with_niladic= 0; + int field_no,dup_no; + int select_field_pos,auto_increment=0; + DBUG_ENTER("mysql_prepare_table"); - /* Check for duplicate fields and check type of table to create */ - if (!fields.elements) - { - my_error(ER_TABLE_MUST_HAVE_COLUMNS,MYF(0)); - DBUG_RETURN(-1); - } List_iterator<create_field> it(fields),it2(fields); - int select_field_pos=fields.elements - select_field_count; + select_field_pos=fields.elements - select_field_count; null_fields=blob_columns=0; - if ((new_db_type= ha_checktype(create_info->db_type)) != - create_info->db_type) - { - create_info->db_type= new_db_type; - push_warning_printf(thd, MYSQL_ERROR::WARN_LEVEL_WARN, - ER_WARN_USING_OTHER_HANDLER, - ER(ER_WARN_USING_OTHER_HANDLER), - ha_get_storage_engine(new_db_type), - table_name); - } - db_options=create_info->table_options; - if (create_info->row_type == ROW_TYPE_DYNAMIC) - db_options|=HA_OPTION_PACK_RECORD; - alias= table_case_name(create_info, table_name); - file=get_new_handler((TABLE*) 0, create_info->db_type); - - if ((create_info->options & HA_LEX_CREATE_TMP_TABLE) && - (file->table_flags() & HA_NO_TEMP_TABLES)) - { - my_error(ER_ILLEGAL_HA,MYF(0),table_name); - DBUG_RETURN(-1); - } for (field_no=0; (sql_field=it++) ; field_no++) { @@ -484,7 +443,7 @@ int mysql_create_table(THD *thd,const char *db, const char *table_name, my_error(ER_UNKNOWN_COLLATION, MYF(0), tmp); DBUG_RETURN(-1); } - + sql_field->create_length_to_internal_length(); /* Don't pack keys in old tables if the user has requested this */ @@ -507,20 +466,20 @@ int mysql_create_table(THD *thd,const char *db, const char *table_name, for (dup_no=0; (dup_field=it2++) != sql_field; dup_no++) { if (my_strcasecmp(system_charset_info, - sql_field->field_name, - dup_field->field_name) == 0) + sql_field->field_name, + dup_field->field_name) == 0) { /* If this was a CREATE ... SELECT statement, accept a field redefinition if we are changing a field in the SELECT part */ - if (field_no < select_field_pos || dup_no >= select_field_pos) - { - my_error(ER_DUP_FIELDNAME,MYF(0),sql_field->field_name); - DBUG_RETURN(-1); - } - else - { + if (field_no < select_field_pos || dup_no >= select_field_pos) + { + my_error(ER_DUP_FIELDNAME,MYF(0),sql_field->field_name); + DBUG_RETURN(-1); + } + else + { /* Field redefined */ sql_field->sql_type= dup_field->sql_type; sql_field->charset= (dup_field->charset ? @@ -535,7 +494,7 @@ int mysql_create_table(THD *thd,const char *db, const char *table_name, it2.remove(); // Remove first (create) definition select_field_pos--; break; - } + } } } it2.rewind(); @@ -621,17 +580,17 @@ int mysql_create_table(THD *thd,const char *db, const char *table_name, /* We should replace old TIMESTAMP fields with their newer analogs */ if (sql_field->unireg_check == Field::TIMESTAMP_OLD_FIELD) { - if (!timestamps) - { - sql_field->unireg_check= Field::TIMESTAMP_DNUN_FIELD; - timestamps_with_niladic++; - } - else - sql_field->unireg_check= Field::NONE; + if (!timestamps) + { + sql_field->unireg_check= Field::TIMESTAMP_DNUN_FIELD; + timestamps_with_niladic++; + } + else + sql_field->unireg_check= Field::NONE; } else if (sql_field->unireg_check != Field::NONE) - timestamps_with_niladic++; - + timestamps_with_niladic++; + timestamps++; /* fall-through */ default: @@ -677,13 +636,14 @@ int mysql_create_table(THD *thd,const char *db, const char *table_name, /* Create keys */ List_iterator<Key> key_iterator(keys); - uint key_parts=0, key_count=0, fk_key_count=0; + uint key_parts=0, fk_key_count=0; List<Key> keys_in_order; // Add new keys here bool primary_key=0,unique_key=0; Key *key; uint tmp, key_number; /* Calculate number of key segements */ + *key_count= 0; while ((key=key_iterator++)) { @@ -701,7 +661,7 @@ int mysql_create_table(THD *thd,const char *db, const char *table_name, } continue; } - key_count++; + (*key_count)++; tmp=max(file->max_key_parts(),MAX_REF_PARTS); if (key->columns.elements > tmp) { @@ -722,13 +682,13 @@ int mysql_create_table(THD *thd,const char *db, const char *table_name, } } tmp=min(file->max_keys(), MAX_KEY); - if (key_count > tmp) + if (*key_count > tmp) { my_error(ER_TOO_MANY_KEYS,MYF(0),tmp); DBUG_RETURN(-1); } - key_info_buffer=key_info=(KEY*) sql_calloc(sizeof(KEY)*key_count); + key_info_buffer=key_info=(KEY*) sql_calloc(sizeof(KEY)* *key_count); key_part_info=(KEY_PART_INFO*) sql_calloc(sizeof(KEY_PART_INFO)*key_parts); if (!key_info_buffer || ! key_part_info) DBUG_RETURN(-1); // Out of memory @@ -742,15 +702,15 @@ int mysql_create_table(THD *thd,const char *db, const char *table_name, switch(key->type){ case Key::MULTIPLE: - key_info->flags = 0; - break; + key_info->flags = 0; + break; case Key::FULLTEXT: - key_info->flags = HA_FULLTEXT; - break; + key_info->flags = HA_FULLTEXT; + break; case Key::SPATIAL: #ifdef HAVE_SPATIAL - key_info->flags = HA_SPATIAL; - break; + key_info->flags = HA_SPATIAL; + break; #else my_printf_error(ER_FEATURE_DISABLED,ER(ER_FEATURE_DISABLED),MYF(0), sym_group_geom.name, sym_group_geom.needed_define); @@ -760,7 +720,7 @@ int mysql_create_table(THD *thd,const char *db, const char *table_name, key_number--; // Skip this key continue; default: - key_info->flags = HA_NOSAME; + key_info->flags = HA_NOSAME; } key_info->key_parts=(uint8) key->columns.elements; @@ -772,8 +732,8 @@ int mysql_create_table(THD *thd,const char *db, const char *table_name, { if (!(file->table_flags() & HA_CAN_FULLTEXT)) { - my_error(ER_TABLE_CANT_HANDLE_FT, MYF(0)); - DBUG_RETURN(-1); + my_error(ER_TABLE_CANT_HANDLE_FT, MYF(0)); + DBUG_RETURN(-1); } } /* @@ -789,9 +749,9 @@ int mysql_create_table(THD *thd,const char *db, const char *table_name, { if (key_info->key_parts != 1) { - my_printf_error(ER_WRONG_ARGUMENTS, - ER(ER_WRONG_ARGUMENTS),MYF(0),"SPATIAL INDEX"); - DBUG_RETURN(-1); + my_printf_error(ER_WRONG_ARGUMENTS, + ER(ER_WRONG_ARGUMENTS),MYF(0),"SPATIAL INDEX"); + DBUG_RETURN(-1); } } else @@ -823,8 +783,8 @@ int mysql_create_table(THD *thd,const char *db, const char *table_name, field=0; while ((sql_field=it++) && my_strcasecmp(system_charset_info, - column->field_name, - sql_field->field_name)) + column->field_name, + sql_field->field_name)) field++; if (!sql_field) { @@ -834,94 +794,94 @@ int mysql_create_table(THD *thd,const char *db, const char *table_name, DBUG_RETURN(-1); } /* for fulltext keys keyseg length is 1 for blobs (it's ignored in - ft code anyway, and 0 (set to column width later) for char's. - it has to be correct col width for char's, as char data are not - prefixed with length (unlike blobs, where ft code takes data length - from a data prefix, ignoring column->length). + ft code anyway, and 0 (set to column width later) for char's. + it has to be correct col width for char's, as char data are not + prefixed with length (unlike blobs, where ft code takes data length + from a data prefix, ignoring column->length). */ if (key->type == Key::FULLTEXT) { - if ((sql_field->sql_type != FIELD_TYPE_STRING && - sql_field->sql_type != FIELD_TYPE_VAR_STRING && - !f_is_blob(sql_field->pack_flag)) || - sql_field->charset == &my_charset_bin || - sql_field->charset->state & MY_CS_NONTEXT || // ucs2 doesn't work yet - (ft_key_charset && sql_field->charset != ft_key_charset)) - { - my_printf_error(ER_BAD_FT_COLUMN,ER(ER_BAD_FT_COLUMN),MYF(0), - column->field_name); - DBUG_RETURN(-1); - } - ft_key_charset=sql_field->charset; - /* - for fulltext keys keyseg length is 1 for blobs (it's ignored in ft - code anyway, and 0 (set to column width later) for char's. it has - to be correct col width for char's, as char data are not prefixed - with length (unlike blobs, where ft code takes data length from a - data prefix, ignoring column->length). - */ - column->length=test(f_is_blob(sql_field->pack_flag)); + if ((sql_field->sql_type != FIELD_TYPE_STRING && + sql_field->sql_type != FIELD_TYPE_VAR_STRING && + !f_is_blob(sql_field->pack_flag)) || + sql_field->charset == &my_charset_bin || + sql_field->charset->state & MY_CS_NONTEXT || // ucs2 doesn't work yet + (ft_key_charset && sql_field->charset != ft_key_charset)) + { + my_printf_error(ER_BAD_FT_COLUMN,ER(ER_BAD_FT_COLUMN),MYF(0), + column->field_name); + DBUG_RETURN(-1); + } + ft_key_charset=sql_field->charset; + /* + for fulltext keys keyseg length is 1 for blobs (it's ignored in ft + code anyway, and 0 (set to column width later) for char's. it has + to be correct col width for char's, as char data are not prefixed + with length (unlike blobs, where ft code takes data length from a + data prefix, ignoring column->length). + */ + column->length=test(f_is_blob(sql_field->pack_flag)); } else { - column->length*= sql_field->charset->mbmaxlen; - - if (f_is_blob(sql_field->pack_flag)) - { - if (!(file->table_flags() & HA_BLOB_KEY)) - { - my_printf_error(ER_BLOB_USED_AS_KEY,ER(ER_BLOB_USED_AS_KEY),MYF(0), - column->field_name); - DBUG_RETURN(-1); - } - if (!column->length) - { - my_printf_error(ER_BLOB_KEY_WITHOUT_LENGTH, - ER(ER_BLOB_KEY_WITHOUT_LENGTH),MYF(0), - column->field_name); - DBUG_RETURN(-1); - } - } + column->length*= sql_field->charset->mbmaxlen; + + if (f_is_blob(sql_field->pack_flag)) + { + if (!(file->table_flags() & HA_BLOB_KEY)) + { + my_printf_error(ER_BLOB_USED_AS_KEY,ER(ER_BLOB_USED_AS_KEY),MYF(0), + column->field_name); + DBUG_RETURN(-1); + } + if (!column->length) + { + my_printf_error(ER_BLOB_KEY_WITHOUT_LENGTH, + ER(ER_BLOB_KEY_WITHOUT_LENGTH),MYF(0), + column->field_name); + DBUG_RETURN(-1); + } + } #ifdef HAVE_SPATIAL - if (key->type == Key::SPATIAL) - { - if (!column->length ) - { - /* - BAR: 4 is: (Xmin,Xmax,Ymin,Ymax), this is for 2D case - Lately we'll extend this code to support more dimensions - */ - column->length=4*sizeof(double); - } - } + if (key->type == Key::SPATIAL) + { + if (!column->length ) + { + /* + BAR: 4 is: (Xmin,Xmax,Ymin,Ymax), this is for 2D case + Lately we'll extend this code to support more dimensions + */ + column->length=4*sizeof(double); + } + } #endif - if (!(sql_field->flags & NOT_NULL_FLAG)) - { - if (key->type == Key::PRIMARY) - { - /* Implicitly set primary key fields to NOT NULL for ISO conf. */ - sql_field->flags|= NOT_NULL_FLAG; - sql_field->pack_flag&= ~FIELDFLAG_MAYBE_NULL; - } - else - key_info->flags|= HA_NULL_PART_KEY; - if (!(file->table_flags() & HA_NULL_KEY)) - { - my_printf_error(ER_NULL_COLUMN_IN_INDEX,ER(ER_NULL_COLUMN_IN_INDEX), - MYF(0),column->field_name); - DBUG_RETURN(-1); - } - if (key->type == Key::SPATIAL) - { - my_error(ER_SPATIAL_CANT_HAVE_NULL, MYF(0)); - DBUG_RETURN(-1); - } - } - if (MTYP_TYPENR(sql_field->unireg_check) == Field::NEXT_NUMBER) - { - if (column_nr == 0 || (file->table_flags() & HA_AUTO_PART_KEY)) - auto_increment--; // Field is used - } + if (!(sql_field->flags & NOT_NULL_FLAG)) + { + if (key->type == Key::PRIMARY) + { + /* Implicitly set primary key fields to NOT NULL for ISO conf. */ + sql_field->flags|= NOT_NULL_FLAG; + sql_field->pack_flag&= ~FIELDFLAG_MAYBE_NULL; + } + else + key_info->flags|= HA_NULL_PART_KEY; + if (!(file->table_flags() & HA_NULL_KEY)) + { + my_printf_error(ER_NULL_COLUMN_IN_INDEX,ER(ER_NULL_COLUMN_IN_INDEX), + MYF(0),column->field_name); + DBUG_RETURN(-1); + } + if (key->type == Key::SPATIAL) + { + my_error(ER_SPATIAL_CANT_HAVE_NULL, MYF(0)); + DBUG_RETURN(-1); + } + } + if (MTYP_TYPENR(sql_field->unireg_check) == Field::NEXT_NUMBER) + { + if (column_nr == 0 || (file->table_flags() & HA_AUTO_PART_KEY)) + auto_increment--; // Field is used + } } key_part_info->fieldnr= field; @@ -934,35 +894,36 @@ int mysql_create_table(THD *thd,const char *db, const char *table_name, { if ((length=column->length) > file->max_key_length() || length > file->max_key_part_length()) - { - length=min(file->max_key_length(), file->max_key_part_length()); - if (key->type == Key::MULTIPLE) - { - /* not a critical problem */ - char warn_buff[MYSQL_ERRMSG_SIZE]; - sprintf(warn_buff,ER(ER_TOO_LONG_KEY),length); - push_warning(thd, MYSQL_ERROR::WARN_LEVEL_WARN, - ER_TOO_LONG_KEY, warn_buff); - } - else - { - my_error(ER_TOO_LONG_KEY,MYF(0),length); - DBUG_RETURN(-1); - } - } + { + length=min(file->max_key_length(), file->max_key_part_length()); + if (key->type == Key::MULTIPLE) + { + /* not a critical problem */ + char warn_buff[MYSQL_ERRMSG_SIZE]; + my_snprintf(warn_buff, sizeof(warn_buff), ER(ER_TOO_LONG_KEY), + length); + push_warning(thd, MYSQL_ERROR::WARN_LEVEL_WARN, + ER_TOO_LONG_KEY, warn_buff); + } + else + { + my_error(ER_TOO_LONG_KEY,MYF(0),length); + DBUG_RETURN(-1); + } + } } else if (!f_is_geom(sql_field->pack_flag) && - (column->length > length || - ((f_is_packed(sql_field->pack_flag) || - ((file->table_flags() & HA_NO_PREFIX_CHAR_KEYS) && - (key_info->flags & HA_NOSAME))) && - column->length != length))) - { - my_error(ER_WRONG_SUB_KEY,MYF(0)); - DBUG_RETURN(-1); - } - else if (!(file->table_flags() & HA_NO_PREFIX_CHAR_KEYS)) - length=column->length; + (column->length > length || + ((f_is_packed(sql_field->pack_flag) || + ((file->table_flags() & HA_NO_PREFIX_CHAR_KEYS) && + (key_info->flags & HA_NOSAME))) && + column->length != length))) + { + my_error(ER_WRONG_SUB_KEY,MYF(0)); + DBUG_RETURN(-1); + } + else if (!(file->table_flags() & HA_NO_PREFIX_CHAR_KEYS)) + length=column->length; } else if (length == 0) { @@ -972,20 +933,21 @@ int mysql_create_table(THD *thd,const char *db, const char *table_name, } if (length > file->max_key_part_length()) { - length=file->max_key_part_length(); - if (key->type == Key::MULTIPLE) - { - /* not a critical problem */ - char warn_buff[MYSQL_ERRMSG_SIZE]; - sprintf(warn_buff,ER(ER_TOO_LONG_KEY),length); - push_warning(thd, MYSQL_ERROR::WARN_LEVEL_WARN, - ER_TOO_LONG_KEY, warn_buff); - } - else - { - my_error(ER_TOO_LONG_KEY,MYF(0),length); - DBUG_RETURN(-1); - } + length=file->max_key_part_length(); + if (key->type == Key::MULTIPLE) + { + /* not a critical problem */ + char warn_buff[MYSQL_ERRMSG_SIZE]; + my_snprintf(warn_buff, sizeof(warn_buff), ER(ER_TOO_LONG_KEY), + length); + push_warning(thd, MYSQL_ERROR::WARN_LEVEL_WARN, + ER_TOO_LONG_KEY, warn_buff); + } + else + { + my_error(ER_TOO_LONG_KEY,MYF(0),length); + DBUG_RETURN(-1); + } } key_part_info->length=(uint16) length; /* Use packed keys for long strings on the first column */ @@ -1055,17 +1017,102 @@ int mysql_create_table(THD *thd,const char *db, const char *table_name, DBUG_RETURN(-1); } /* Sort keys in optimized order */ - qsort((gptr) key_info_buffer, key_count, sizeof(KEY), (qsort_cmp) sort_keys); + qsort((gptr) key_info_buffer, *key_count, sizeof(KEY), + (qsort_cmp) sort_keys); + + DBUG_RETURN(0); +} + + +/* + Create a table + + SYNOPSIS + mysql_create_table() + thd Thread object + db Database + table_name Table name + create_info Create information (like MAX_ROWS) + fields List of fields to create + keys List of keys to create + tmp_table Set to 1 if this is an internal temporary table + (From ALTER TABLE) + no_log Don't log the query to binary log. + + DESCRIPTION + If one creates a temporary table, this is automaticly opened + + no_log is needed for the case of CREATE ... SELECT, + as the logging will be done later in sql_insert.cc + select_field_count is also used for CREATE ... SELECT, + and must be zero for standard create of table. + + RETURN VALUES + 0 ok + -1 error +*/ + +int mysql_create_table(THD *thd,const char *db, const char *table_name, + HA_CREATE_INFO *create_info, + List<create_field> &fields, + List<Key> &keys,bool tmp_table,bool no_log, + uint select_field_count) +{ + char path[FN_REFLEN]; + const char *alias; + int error= -1; + uint db_options, key_count; + KEY *key_info_buffer; + handler *file; + enum db_type new_db_type; + DBUG_ENTER("mysql_create_table"); + + /* Check for duplicate fields and check type of table to create */ + if (!fields.elements) + { + my_error(ER_TABLE_MUST_HAVE_COLUMNS,MYF(0)); + DBUG_RETURN(-1); + } + if ((new_db_type= ha_checktype(create_info->db_type)) != + create_info->db_type) + { + create_info->db_type= new_db_type; + push_warning_printf(thd, MYSQL_ERROR::WARN_LEVEL_WARN, + ER_WARN_USING_OTHER_HANDLER, + ER(ER_WARN_USING_OTHER_HANDLER), + ha_get_storage_engine(new_db_type), + table_name); + } + db_options=create_info->table_options; + if (create_info->row_type == ROW_TYPE_DYNAMIC) + db_options|=HA_OPTION_PACK_RECORD; + alias= table_case_name(create_info, table_name); + file=get_new_handler((TABLE*) 0, create_info->db_type); + + if ((create_info->options & HA_LEX_CREATE_TMP_TABLE) && + (file->table_flags() & HA_NO_TEMP_TABLES)) + { + my_error(ER_ILLEGAL_HA,MYF(0),table_name); + DBUG_RETURN(-1); + } + + if (mysql_prepare_table(thd, create_info, fields, + keys, tmp_table, db_options, file, + key_info_buffer, &key_count, + select_field_count)) + DBUG_RETURN(-1); /* Check if table exists */ if (create_info->options & HA_LEX_CREATE_TMP_TABLE) { - sprintf(path,"%s%s%lx_%lx_%x%s",mysql_tmpdir,tmp_file_prefix, - current_pid, thd->thread_id, thd->tmp_table++,reg_ext); + my_snprintf(path, sizeof(path), "%s%s%lx_%lx_%x%s", + mysql_tmpdir, tmp_file_prefix, current_pid, thd->thread_id, + thd->tmp_table++, reg_ext); create_info->table_options|=HA_CREATE_DELAY_KEY_WRITE; } else - (void) sprintf(path,"%s/%s/%s%s",mysql_data_home,db,alias,reg_ext); + my_snprintf(path, sizeof(path), "%s/%s/%s%s", mysql_data_home, db, + alias, reg_ext); unpack_filename(path,path); /* Check if table already exists */ if ((create_info->options & HA_LEX_CREATE_TMP_TABLE) @@ -1090,9 +1137,38 @@ int mysql_create_table(THD *thd,const char *db, const char *table_name, { create_info->table_existed= 1; // Mark that table existed error= 0; - } + } + else + my_error(ER_TABLE_EXISTS_ERROR,MYF(0),table_name); + goto end; + } + } + + /* + Check that table with given name does not already + exist in any storage engine. In such a case it should + be discovered and the error ER_TABLE_EXISTS_ERROR be returned + unless user specified CREATE TABLE IF EXISTS + The LOCK_open mutex has been locked to make sure no + one else is attempting to discover the table. Since + it's not on disk as a frm file, no one could be using it! + */ + if (!(create_info->options & HA_LEX_CREATE_TMP_TABLE)) + { + bool create_if_not_exists = + create_info->options & HA_LEX_CREATE_IF_NOT_EXISTS; + if (!create_table_from_handler(db, table_name, + create_if_not_exists)) + { + DBUG_PRINT("info", ("Table already existed in handler")); + + if (create_if_not_exists) + { + create_info->table_existed= 1; // Mark that table existed + error= 0; + } else - my_error(ER_TABLE_EXISTS_ERROR,MYF(0),table_name); + my_error(ER_TABLE_EXISTS_ERROR,MYF(0),table_name); goto end; } } @@ -1158,15 +1234,23 @@ make_unique_key_name(const char *field_name,KEY *start,KEY *end) if (!check_if_keyname_exists(field_name,start,end) && my_strcasecmp(system_charset_info,field_name,primary_key_name)) return (char*) field_name; // Use fieldname - buff_end=strmake(buff,field_name,MAX_FIELD_NAME-4); - for (uint i=2 ; ; i++) + buff_end=strmake(buff,field_name, sizeof(buff)-4); + + /* + Only 3 chars + '\0' left, so need to limit to 2 digit + This is ok as we can't have more than 100 keys anyway + */ + for (uint i=2 ; i< 100; i++) { - sprintf(buff_end,"_%d",i); + *buff_end= '_'; + int10_to_str(i, buff_end+1, 10); if (!check_if_keyname_exists(buff,start,end)) return sql_strdup(buff); } + return (char*) "not_specified"; // Should never happen } + /**************************************************************************** ** Create table from a list of fields and items ****************************************************************************/ @@ -1262,8 +1346,10 @@ mysql_rename_table(enum db_type base, my_casedn_str(system_charset_info, tmp_to); new_name= tmp_to; } - (void) sprintf(from,"%s/%s/%s",mysql_data_home,old_db,old_name); - (void) sprintf(to,"%s/%s/%s",mysql_data_home,new_db,new_name); + my_snprintf(from, sizeof(from), "%s/%s/%s", + mysql_data_home, old_db, old_name); + my_snprintf(to, sizeof(to), "%s/%s/%s", + mysql_data_home, new_db, new_name); fn_format(from,from,"","",4); fn_format(to,to, "","",4); @@ -1291,7 +1377,7 @@ mysql_rename_table(enum db_type base, thd Thread handler table Table to remove from cache function HA_EXTRA_PREPARE_FOR_DELETE if table is to be deleted - HA_EXTRA_FORCE_REOPEN if table is not be used + HA_EXTRA_FORCE_REOPEN if table is not be used NOTES When returning, the table will be unusable for other threads until the table is closed. @@ -1339,11 +1425,11 @@ static void wait_while_table_is_used(THD *thd,TABLE *table, Lock on LOCK_open Win32 clients must also have a WRITE LOCK on the table ! */ - + static bool close_cached_table(THD *thd, TABLE *table) { DBUG_ENTER("close_cached_table"); - + wait_while_table_is_used(thd, table, HA_EXTRA_PREPARE_FOR_DELETE); /* Close lock if this is not got with LOCK TABLES */ if (thd->lock) @@ -1398,7 +1484,8 @@ static int prepare_for_restore(THD* thd, TABLE_LIST* table, reg_ext)) DBUG_RETURN(-1); // protect buffer overflow - sprintf(dst_path, "%s/%s/%s", mysql_real_data_home, db, table_name); + my_snprintf(dst_path, sizeof(dst_path), "%s/%s/%s", + mysql_real_data_home, db, table_name); if (lock_and_wait_for_table_name(thd,table)) DBUG_RETURN(-1); @@ -1482,7 +1569,8 @@ static int prepare_for_repair(THD* thd, TABLE_LIST *table_list, if (!my_stat(from, &stat_info, MYF(0))) goto end; // Can't use USE_FRM flag - sprintf(tmp,"%s-%lx_%lx", from, current_pid, thd->thread_id); + my_snprintf(tmp, sizeof(tmp), "%s-%lx_%lx", + from, current_pid, thd->thread_id); /* If we could open the table, close it */ if (table_list->table) @@ -1588,9 +1676,9 @@ static int mysql_admin_table(THD* thd, TABLE_LIST* tables, if (prepare_func) { switch ((*prepare_func)(thd, table, check_opt)) { - case 1: continue; // error, message written to net - case -1: goto err; // error, message could be written to net - default: ; // should be 0 otherwise + case 1: continue; // error, message written to net + case -1: goto err; // error, message could be written to net + default: ; // should be 0 otherwise } } @@ -1617,7 +1705,7 @@ static int mysql_admin_table(THD* thd, TABLE_LIST* tables, protocol->store(table_name, system_charset_info); protocol->store(operator_name, system_charset_info); protocol->store("error", 5, system_charset_info); - sprintf(buff, ER(ER_OPEN_AS_READONLY), table_name); + my_snprintf(buff, sizeof(buff), ER(ER_OPEN_AS_READONLY), table_name); protocol->store(buff, system_charset_info); close_thread_tables(thd); table->table=0; // For query cache @@ -1659,11 +1747,11 @@ static int mysql_admin_table(THD* thd, TABLE_LIST* tables, switch (result_code) { case HA_ADMIN_NOT_IMPLEMENTED: { - char buf[ERRMSGSIZE+20]; - uint length=my_snprintf(buf, ERRMSGSIZE, + char buf[ERRMSGSIZE+20]; + uint length=my_snprintf(buf, ERRMSGSIZE, ER(ER_CHECK_NOT_IMPLEMENTED), operator_name); - protocol->store("error", 5, system_charset_info); - protocol->store(buf, length, system_charset_info); + protocol->store("note", 4, system_charset_info); + protocol->store(buf, length, system_charset_info); } break; @@ -1747,7 +1835,7 @@ int mysql_restore_table(THD* thd, TABLE_LIST* table_list) DBUG_ENTER("mysql_restore_table"); DBUG_RETURN(mysql_admin_table(thd, table_list, 0, "restore", TL_WRITE, 1, 0, - &prepare_for_restore, + &prepare_for_restore, &handler::restore)); } @@ -1757,7 +1845,7 @@ int mysql_repair_table(THD* thd, TABLE_LIST* tables, HA_CHECK_OPT* check_opt) DBUG_ENTER("mysql_repair_table"); DBUG_RETURN(mysql_admin_table(thd, tables, check_opt, "repair", TL_WRITE, 1, HA_OPEN_FOR_REPAIR, - &prepare_for_repair, + &prepare_for_repair, &handler::repair)); } @@ -1776,8 +1864,8 @@ int mysql_optimize_table(THD* thd, TABLE_LIST* tables, HA_CHECK_OPT* check_opt) SYNOPSIS mysql_assign_to_keycache() - thd Thread object - tables Table list (one table only) + thd Thread object + tables Table list (one table only) RETURN VALUES 0 ok @@ -1802,8 +1890,8 @@ int mysql_assign_to_keycache(THD* thd, TABLE_LIST* tables, pthread_mutex_unlock(&LOCK_global_system_variables); check_opt.key_cache= key_cache; DBUG_RETURN(mysql_admin_table(thd, tables, &check_opt, - "assign_to_keycache", TL_READ_NO_INSERT, 0, - 0, 0, &handler::assign_to_keycache)); + "assign_to_keycache", TL_READ_NO_INSERT, 0, + 0, 0, &handler::assign_to_keycache)); } @@ -1812,9 +1900,9 @@ int mysql_assign_to_keycache(THD* thd, TABLE_LIST* tables, SYNOPSIS reassign_keycache_tables() - thd Thread object - src_cache Reference to the key cache to clean up - dest_cache New key cache + thd Thread object + src_cache Reference to the key cache to clean up + dest_cache New key cache NOTES This is called when one sets a key cache size to zero, in which @@ -1832,8 +1920,8 @@ int mysql_assign_to_keycache(THD* thd, TABLE_LIST* tables, 0 ok */ -int reassign_keycache_tables(THD *thd, KEY_CACHE *src_cache, - KEY_CACHE *dst_cache) +int reassign_keycache_tables(THD *thd, KEY_CACHE *src_cache, + KEY_CACHE *dst_cache) { DBUG_ENTER("reassign_keycache_tables"); @@ -1842,7 +1930,7 @@ int reassign_keycache_tables(THD *thd, KEY_CACHE *src_cache, src_cache->param_buff_size= 0; // Free key cache ha_resize_key_cache(src_cache); ha_change_key_cache(src_cache, dst_cache); - DBUG_RETURN(0); + DBUG_RETURN(0); } @@ -1851,8 +1939,8 @@ int reassign_keycache_tables(THD *thd, KEY_CACHE *src_cache, SYNOPSIS mysql_preload_keys() - thd Thread object - tables Table list (one table only) + thd Thread object + tables Table list (one table only) RETURN VALUES 0 ok @@ -1873,8 +1961,8 @@ int mysql_preload_keys(THD* thd, TABLE_LIST* tables) SYNOPSIS mysql_create_like_table() - thd Thread object - table Table list (one table only) + thd Thread object + table Table list (one table only) create_info Create info table_ident Src table_ident @@ -1883,9 +1971,9 @@ int mysql_preload_keys(THD* thd, TABLE_LIST* tables) -1 error */ -int mysql_create_like_table(THD* thd, TABLE_LIST* table, - HA_CREATE_INFO *create_info, - Table_ident *table_ident) +int mysql_create_like_table(THD* thd, TABLE_LIST* table, + HA_CREATE_INFO *create_info, + Table_ident *table_ident) { TABLE **tmp_table; char src_path[FN_REFLEN], dst_path[FN_REFLEN]; @@ -1908,11 +1996,11 @@ int mysql_create_like_table(THD* thd, TABLE_LIST* table, my_error(ER_WRONG_TABLE_NAME, MYF(0), src_table); DBUG_RETURN(-1); } - + src_tables_list.db= table_ident->db.str ? table_ident->db.str : thd->db; src_tables_list.real_name= table_ident->table.str; src_tables_list.next= 0; - + if (lock_and_wait_for_table_name(thd, &src_tables_list)) goto err; @@ -1920,8 +2008,8 @@ int mysql_create_like_table(THD* thd, TABLE_LIST* table, strxmov(src_path, (*tmp_table)->path, reg_ext, NullS); else { - strxmov(src_path, mysql_data_home, "/", src_db, "/", src_table, - reg_ext, NullS); + strxmov(src_path, mysql_data_home, "/", src_db, "/", src_table, + reg_ext, NullS); if (access(src_path, F_OK)) { my_error(ER_BAD_TABLE_ERROR, MYF(0), src_table); @@ -1932,53 +2020,54 @@ int mysql_create_like_table(THD* thd, TABLE_LIST* table, /* Validate the destination table - skip the destination table name checking as this is already + skip the destination table name checking as this is already validated. */ if (create_info->options & HA_LEX_CREATE_TMP_TABLE) { if (find_temporary_table(thd, db, table_name)) goto table_exists; - sprintf(dst_path,"%s%s%lx_%lx_%x%s",mysql_tmpdir,tmp_file_prefix, - current_pid, thd->thread_id, thd->tmp_table++,reg_ext); + my_snprintf(dst_path, sizeof(dst_path), "%s%s%lx_%lx_%x%s", + mysql_tmpdir, tmp_file_prefix, current_pid, + thd->thread_id, thd->tmp_table++, reg_ext); create_info->table_options|= HA_CREATE_DELAY_KEY_WRITE; } else { - strxmov(dst_path, mysql_data_home, "/", db, "/", table_name, - reg_ext, NullS); + strxmov(dst_path, mysql_data_home, "/", db, "/", table_name, + reg_ext, NullS); if (!access(dst_path, F_OK)) goto table_exists; } - /* + /* Create a new table by copying from source table - */ + */ if (my_copy(src_path, dst_path, MYF(MY_WME|MY_DONT_OVERWRITE_FILE))) goto err; /* - As mysql_truncate don't work on a new table at this stage of - creation, instead create the table directly (for both normal + As mysql_truncate don't work on a new table at this stage of + creation, instead create the table directly (for both normal and temporary tables). */ - *fn_ext(dst_path)= 0; + *fn_ext(dst_path)= 0; err= ha_create_table(dst_path, create_info, 1); - + if (create_info->options & HA_LEX_CREATE_TMP_TABLE) { if (err || !open_temporary_table(thd, dst_path, db, table_name, 1)) { - (void) rm_temporary_table(create_info->db_type, - dst_path); /* purecov: inspected */ + (void) rm_temporary_table(create_info->db_type, + dst_path); /* purecov: inspected */ goto err; /* purecov: inspected */ } } else if (err) { - (void) quick_rm_table(create_info->db_type, db, - table_name); /* purecov: inspected */ - goto err; /* purecov: inspected */ + (void) quick_rm_table(create_info->db_type, db, + table_name); /* purecov: inspected */ + goto err; /* purecov: inspected */ } // Must be written before unlock @@ -1992,14 +2081,15 @@ int mysql_create_like_table(THD* thd, TABLE_LIST* table, } res= 0; goto err; - + table_exists: if (create_info->options & HA_LEX_CREATE_IF_NOT_EXISTS) { char warn_buff[MYSQL_ERRMSG_SIZE]; - sprintf(warn_buff,ER(ER_TABLE_EXISTS_ERROR),table_name); - push_warning(thd, MYSQL_ERROR::WARN_LEVEL_WARN, - ER_TABLE_EXISTS_ERROR,warn_buff); + my_snprintf(warn_buff, sizeof(warn_buff), + ER(ER_TABLE_EXISTS_ERROR), table_name); + push_warning(thd, MYSQL_ERROR::WARN_LEVEL_WARN, + ER_TABLE_EXISTS_ERROR,warn_buff); res= 0; } else @@ -2073,7 +2163,7 @@ int mysql_discard_or_import_tablespace(THD *thd, thd->tablespace_op=FALSE; DBUG_RETURN(-1); } - + error=table->file->discard_or_import_tablespace(discard); thd->proc_info="end"; @@ -2107,17 +2197,226 @@ err: DBUG_RETURN(error); } + +#ifdef NOT_USED +/* + CREATE INDEX and DROP INDEX are implemented by calling ALTER TABLE with + the proper arguments. This isn't very fast but it should work for most + cases. + One should normally create all indexes with CREATE TABLE or ALTER TABLE. +*/ + +int mysql_create_indexes(THD *thd, TABLE_LIST *table_list, List<Key> &keys) +{ + List<create_field> fields; + List<Alter_drop> drop; + List<Alter_column> alter; + HA_CREATE_INFO create_info; + int rc; + uint idx; + uint db_options; + uint key_count; + TABLE *table; + Field **f_ptr; + KEY *key_info_buffer; + char path[FN_REFLEN+1]; + DBUG_ENTER("mysql_create_index"); + + /* + Try to use online generation of index. + This requires that all indexes can be created online. + Otherwise, the old alter table procedure is executed. + + Open the table to have access to the correct table handler. + */ + if (!(table=open_ltable(thd,table_list,TL_WRITE_ALLOW_READ))) + DBUG_RETURN(-1); + + /* + The add_index method takes an array of KEY structs for the new indexes. + Preparing a new table structure generates this array. + It needs a list with all fields of the table, which does not need to + be correct in every respect. The field names are important. + */ + for (f_ptr= table->field; *f_ptr; f_ptr++) + { + create_field *c_fld= new create_field(*f_ptr, *f_ptr); + c_fld->unireg_check= Field::NONE; /*avoid multiple auto_increments*/ + fields.push_back(c_fld); + } + bzero((char*) &create_info,sizeof(create_info)); + create_info.db_type=DB_TYPE_DEFAULT; + create_info.default_table_charset= thd->variables.collation_database; + db_options= 0; + if (mysql_prepare_table(thd, &create_info, fields, + keys, /*tmp_table*/ 0, db_options, table->file, + key_info_buffer, key_count, + /*select_field_count*/ 0)) + DBUG_RETURN(-1); + + /* + Check if all keys can be generated with the add_index method. + If anyone cannot, then take the old way. + */ + for (idx=0; idx< key_count; idx++) + { + DBUG_PRINT("info", ("creating index %s", key_info_buffer[idx].name)); + if (!(table->file->index_ddl_flags(key_info_buffer+idx)& + (HA_DDL_ONLINE| HA_DDL_WITH_LOCK))) + break ; + } + if ((idx < key_count)|| !key_count) + { + /* Re-initialize the create_info, which was changed by prepare table. */ + bzero((char*) &create_info,sizeof(create_info)); + create_info.db_type=DB_TYPE_DEFAULT; + create_info.default_table_charset= thd->variables.collation_database; + /* Cleanup the fields list. We do not want to create existing fields. */ + fields.delete_elements(); + if (real_alter_table(thd, table_list->db, table_list->real_name, + &create_info, table_list, table, + fields, keys, drop, alter, 0, (ORDER*)0, + ALTER_ADD_INDEX, DUP_ERROR)) + /* Don't need to free((gptr) key_info_buffer);*/ + DBUG_RETURN(-1); + } + else + { + if (table->file->add_index(table, key_info_buffer, key_count)|| + (my_snprintf(path, sizeof(path), "%s/%s/%s%s", mysql_data_home, + table_list->db, (lower_case_table_names == 2) ? + table_list->alias: table_list->real_name, reg_ext) >= + (int) sizeof(path)) || + ! unpack_filename(path, path) || + mysql_create_frm(thd, path, &create_info, + fields, key_count, key_info_buffer, table->file)) + /* don't need to free((gptr) key_info_buffer);*/ + DBUG_RETURN(-1); + } + /* don't need to free((gptr) key_info_buffer);*/ + DBUG_RETURN(0); +} + + +int mysql_drop_indexes(THD *thd, TABLE_LIST *table_list, + List<Alter_drop> &drop) +{ + List<create_field> fields; + List<Key> keys; + List<Alter_column> alter; + HA_CREATE_INFO create_info; + uint idx; + uint db_options; + uint key_count; + uint *key_numbers; + TABLE *table; + Field **f_ptr; + KEY *key_info; + KEY *key_info_buffer; + char path[FN_REFLEN]; + DBUG_ENTER("mysql_drop_index"); + + /* + Try to use online generation of index. + This requires that all indexes can be created online. + Otherwise, the old alter table procedure is executed. + + Open the table to have access to the correct table handler. + */ + if (!(table=open_ltable(thd,table_list,TL_WRITE_ALLOW_READ))) + DBUG_RETURN(-1); + + /* + The drop_index method takes an array of key numbers. + It cannot get more entries than keys in the table. + */ + key_numbers= (uint*) thd->alloc(sizeof(uint*)*table->keys); + key_count= 0; + + /* + Get the number of each key and check if it can be created online. + */ + List_iterator<Alter_drop> drop_it(drop); + Alter_drop *drop_key; + while ((drop_key= drop_it++)) + { + /* Find the key in the table. */ + key_info=table->key_info; + for (idx=0; idx< table->keys; idx++, key_info++) + { + if (!my_strcasecmp(system_charset_info, key_info->name, drop_key->name)) + break; + } + if (idx>= table->keys) + { + my_error(ER_CANT_DROP_FIELD_OR_KEY, MYF(0), drop_key->name); + /*don't need to free((gptr) key_numbers);*/ + DBUG_RETURN(-1); + } + /* + Check if the key can be generated with the add_index method. + If anyone cannot, then take the old way. + */ + DBUG_PRINT("info", ("dropping index %s", table->key_info[idx].name)); + if (!(table->file->index_ddl_flags(table->key_info+idx)& + (HA_DDL_ONLINE| HA_DDL_WITH_LOCK))) + break ; + key_numbers[key_count++]= idx; + } + + bzero((char*) &create_info,sizeof(create_info)); + create_info.db_type=DB_TYPE_DEFAULT; + create_info.default_table_charset= thd->variables.collation_database; + + if ((drop_key)|| (drop.elements<= 0)) + { + if (real_alter_table(thd, table_list->db, table_list->real_name, + &create_info, table_list, table, + fields, keys, drop, alter, 0, (ORDER*)0, + ALTER_DROP_INDEX, DUP_ERROR)) + /*don't need to free((gptr) key_numbers);*/ + DBUG_RETURN(-1); + } + else + { + db_options= 0; + if (table->file->drop_index(table, key_numbers, key_count)|| + mysql_prepare_table(thd, &create_info, fields, + keys, /*tmp_table*/ 0, db_options, table->file, + key_info_buffer, key_count, + /*select_field_count*/ 0)|| + (snprintf(path, sizeof(path), "%s/%s/%s%s", mysql_data_home, + table_list->db, (lower_case_table_names == 2)? + table_list->alias: table_list->real_name, reg_ext)>= + (int)sizeof(path))|| + ! unpack_filename(path, path)|| + mysql_create_frm(thd, path, &create_info, + fields, key_count, key_info_buffer, table->file)) + /*don't need to free((gptr) key_numbers);*/ + DBUG_RETURN(-1); + } + + /*don't need to free((gptr) key_numbers);*/ + DBUG_RETURN(0); +} +#endif /* NOT_USED */ + + +/* + Alter table +*/ + int mysql_alter_table(THD *thd,char *new_db, char *new_name, HA_CREATE_INFO *create_info, TABLE_LIST *table_list, List<create_field> &fields, List<Key> &keys,List<Alter_drop> &drop_list, List<Alter_column> &alter_list, - uint order_num, ORDER *order, + uint order_num, ORDER *order, uint alter_flags, enum enum_duplicates handle_duplicates, - enum enum_enable_or_disable keys_onoff, + enum enum_enable_or_disable keys_onoff, enum tablespace_op_type tablespace_op, - bool simple_alter) + bool simple_alter) { TABLE *table,*new_table; int error; @@ -2136,9 +2435,7 @@ int mysql_alter_table(THD *thd,char *new_db, char *new_name, db=table_list->db; if (!new_db || !my_strcasecmp(table_alias_charset, new_db, db)) - { new_db= db; - } used_fields=create_info->used_fields; mysql_ha_closeall(thd, table_list); @@ -2168,8 +2465,8 @@ int mysql_alter_table(THD *thd,char *new_db, char *new_name, !my_strcasecmp(table_alias_charset, new_name_buff, table_name)) { /* - Source and destination table names are equal: make later check - easier. + Source and destination table names are equal: make later check + easier. */ new_alias= new_name= table_name; } @@ -2226,14 +2523,14 @@ int mysql_alter_table(THD *thd,char *new_db, char *new_name, error=0; if (!access(new_name_buff,F_OK)) { - my_error(ER_TABLE_EXISTS_ERROR,MYF(0),new_name); - error= -1; + my_error(ER_TABLE_EXISTS_ERROR,MYF(0),new_name); + error= -1; } else { - *fn_ext(new_name)=0; - close_cached_table(thd, table); - if (mysql_rename_table(old_db_type,db,table_name,new_db,new_alias)) + *fn_ext(new_name)=0; + close_cached_table(thd, table); + if (mysql_rename_table(old_db_type,db,table_name,new_db,new_alias)) error= -1; } VOID(pthread_mutex_unlock(&LOCK_open)); @@ -2248,35 +2545,40 @@ int mysql_alter_table(THD *thd,char *new_db, char *new_name, VOID(pthread_mutex_lock(&LOCK_open)); wait_while_table_is_used(thd, table, HA_EXTRA_FORCE_REOPEN); VOID(pthread_mutex_unlock(&LOCK_open)); - error= table->file->activate_all_index(thd); + error= table->file->enable_indexes(); /* COND_refresh will be signaled in close_thread_tables() */ break; case DISABLE: - if (table->db_type == DB_TYPE_MYISAM) - { - VOID(pthread_mutex_lock(&LOCK_open)); - wait_while_table_is_used(thd, table, HA_EXTRA_FORCE_REOPEN); - VOID(pthread_mutex_unlock(&LOCK_open)); - table->file->deactivate_non_unique_index(HA_POS_ERROR); + VOID(pthread_mutex_lock(&LOCK_open)); + wait_while_table_is_used(thd, table, HA_EXTRA_FORCE_REOPEN); + VOID(pthread_mutex_unlock(&LOCK_open)); + error=table->file->disable_indexes(0, 1); /* COND_refresh will be signaled in close_thread_tables() */ - } - else - push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN, - ER_ILLEGAL_HA, - ER(ER_ILLEGAL_HA), table->table_name); break; } } + if (error == HA_ERR_WRONG_COMMAND) + { + push_warning_printf(thd, MYSQL_ERROR::WARN_LEVEL_NOTE, + ER_ILLEGAL_HA, ER(ER_ILLEGAL_HA), + table->table_name); + error=0; + } if (!error) { if (mysql_bin_log.is_open()) { - thd->clear_error(); + thd->clear_error(); Query_log_event qinfo(thd, thd->query, thd->query_length, 0); mysql_bin_log.write(&qinfo); } send_ok(thd); } + else + { + table->file->print_error(error, MYF(0)); + error= -1; + } table_list->table=0; // For query cache query_cache_invalidate3(thd, table_list, 0); DBUG_RETURN(error); @@ -2337,7 +2639,7 @@ int mysql_alter_table(THD *thd,char *new_db, char *new_name, while ((def=def_it++)) { if (def->change && - !my_strcasecmp(system_charset_info,field->field_name, def->change)) + !my_strcasecmp(system_charset_info,field->field_name, def->change)) break; } if (def) @@ -2361,11 +2663,11 @@ int mysql_alter_table(THD *thd,char *new_db, char *new_name, } if (alter) { - if (def->sql_type == FIELD_TYPE_BLOB) - { - my_error(ER_BLOB_CANT_HAVE_DEFAULT,MYF(0),def->change); - DBUG_RETURN(-1); - } + if (def->sql_type == FIELD_TYPE_BLOB) + { + my_error(ER_BLOB_CANT_HAVE_DEFAULT,MYF(0),def->change); + DBUG_RETURN(-1); + } def->def=alter->def; // Use new default alter_it.remove(); } @@ -2457,7 +2759,7 @@ int mysql_alter_table(THD *thd,char *new_db, char *new_name, break; } else if (!my_strcasecmp(system_charset_info, - key_part_name, cfield->field_name)) + key_part_name, cfield->field_name)) break; } if (!cfield) @@ -2477,14 +2779,14 @@ int mysql_alter_table(THD *thd,char *new_db, char *new_name, } if (key_parts.elements) key_list.push_back(new Key(key_info->flags & HA_SPATIAL ? Key::SPATIAL : - (key_info->flags & HA_NOSAME ? + (key_info->flags & HA_NOSAME ? (!my_strcasecmp(system_charset_info, - key_name, primary_key_name) ? - Key::PRIMARY : Key::UNIQUE) : + key_name, primary_key_name) ? + Key::PRIMARY : Key::UNIQUE) : (key_info->flags & HA_FULLTEXT ? Key::FULLTEXT : Key::MULTIPLE)), key_name, - key_info->algorithm, + key_info->algorithm, key_parts)); } { @@ -2514,8 +2816,11 @@ int mysql_alter_table(THD *thd,char *new_db, char *new_name, } db_create_options=table->db_create_options & ~(HA_OPTION_PACK_RECORD); - (void) sprintf(tmp_name,"%s-%lx_%lx", tmp_file_prefix, current_pid, - thd->thread_id); + my_snprintf(tmp_name, sizeof(tmp_name), "%s-%lx_%lx", tmp_file_prefix, + current_pid, thd->thread_id); + /* Safety fix for innodb */ + if (lower_case_table_names) + my_casedn_str(system_charset_info, tmp_name); create_info->db_type=new_db_type; if (!create_info->comment) create_info->comment=table->comment; @@ -2594,7 +2899,8 @@ int mysql_alter_table(THD *thd,char *new_db, char *new_name, else { char path[FN_REFLEN]; - (void) sprintf(path,"%s/%s/%s",mysql_data_home,new_db,tmp_name); + my_snprintf(path, sizeof(path), "%s/%s/%s", mysql_data_home, + new_db, tmp_name); fn_format(path,path,"","",4); new_table=open_temporary_table(thd, path, new_db, tmp_name,0); } @@ -2604,9 +2910,9 @@ int mysql_alter_table(THD *thd,char *new_db, char *new_name, goto err; } - - /* - We don't want update TIMESTAMP fields during ALTER TABLE + + /* + We don't want update TIMESTAMP fields during ALTER TABLE and copy_data_between_tables uses only write_row() for new_table so don't need to set up timestamp_on_update_now member. */ @@ -2676,8 +2982,8 @@ int mysql_alter_table(THD *thd,char *new_db, char *new_name, */ thd->proc_info="rename result table"; - sprintf(old_name,"%s2-%lx-%lx", tmp_file_prefix, current_pid, - thd->thread_id); + my_snprintf(old_name, sizeof(old_name), "%s2-%lx-%lx", tmp_file_prefix, + current_pid, thd->thread_id); if (new_name != table_name || new_db != db) { if (!access(new_name_buff,F_OK)) @@ -2799,7 +3105,8 @@ int mysql_alter_table(THD *thd,char *new_db, char *new_name, shutdown. */ char path[FN_REFLEN]; - (void) sprintf(path,"%s/%s/%s",mysql_data_home,new_db,table_name); + my_snprintf(path, sizeof(path), "%s/%s/%s", mysql_data_home, + new_db, table_name); fn_format(path,path,"","",4); table=open_temporary_table(thd, path, new_db, tmp_name,0); if (table) @@ -2817,8 +3124,9 @@ int mysql_alter_table(THD *thd,char *new_db, char *new_name, query_cache_invalidate3(thd, table_list, 0); end_temporary: - sprintf(tmp_name, ER(ER_INSERT_INFO), (ulong) (copied + deleted), - (ulong) deleted, (ulong) thd->cuted_fields); + my_snprintf(tmp_name, sizeof(tmp_name), ER(ER_INSERT_INFO), + (ulong) (copied + deleted), (ulong) deleted, + (ulong) thd->cuted_fields); send_ok(thd,copied+deleted,0L,tmp_name); thd->some_tables_deleted=0; DBUG_RETURN(0); @@ -2830,11 +3138,11 @@ end_temporary: static int copy_data_between_tables(TABLE *from,TABLE *to, - List<create_field> &create, + List<create_field> &create, enum enum_duplicates handle_duplicates, - uint order_num, ORDER *order, + uint order_num, ORDER *order, ha_rows *copied, - ha_rows *deleted) + ha_rows *deleted) { int error; Copy_field *copy,*copy_end; @@ -2854,9 +3162,8 @@ copy_data_between_tables(TABLE *from,TABLE *to, DBUG_RETURN(-1); /* purecov: inspected */ to->file->external_lock(thd,F_WRLCK); - to->file->extra(HA_EXTRA_WRITE_CACHE); from->file->info(HA_STATUS_VARIABLE); - to->file->deactivate_non_unique_index(from->file->records); + to->file->start_bulk_insert(from->file->records); List_iterator<create_field> it(create); create_field *def; @@ -2873,7 +3180,7 @@ copy_data_between_tables(TABLE *from,TABLE *to, if (order) { from->sort.io_cache=(IO_CACHE*) my_malloc(sizeof(IO_CACHE), - MYF(MY_FAE | MY_ZEROFILL)); + MYF(MY_FAE | MY_ZEROFILL)); bzero((char*) &tables,sizeof(tables)); tables.table = from; tables.alias = tables.real_name= from->real_name; @@ -2883,11 +3190,11 @@ copy_data_between_tables(TABLE *from,TABLE *to, if (thd->lex->select_lex.setup_ref_array(thd, order_num) || setup_order(thd, thd->lex->select_lex.ref_pointer_array, &tables, fields, all_fields, order) || - !(sortorder=make_unireg_sortorder(order, &length)) || - (from->sort.found_records = filesort(thd, from, sortorder, length, - (SQL_SELECT *) 0, HA_POS_ERROR, - &examined_rows)) - == HA_POS_ERROR) + !(sortorder=make_unireg_sortorder(order, &length)) || + (from->sort.found_records = filesort(thd, from, sortorder, length, + (SQL_SELECT *) 0, HA_POS_ERROR, + &examined_rows)) + == HA_POS_ERROR) goto err; }; @@ -2939,17 +3246,15 @@ copy_data_between_tables(TABLE *from,TABLE *to, end_read_record(&info); free_io_cache(from); delete [] copy; // This is never 0 - uint tmp_error; - if ((tmp_error=to->file->extra(HA_EXTRA_NO_CACHE))) + + if (to->file->end_bulk_insert() && !error) { - to->file->print_error(tmp_error,MYF(0)); + to->file->print_error(my_errno,MYF(0)); error=1; } to->file->extra(HA_EXTRA_NO_IGNORE_DUP_KEY); - if (to->file->activate_all_index(thd)) - error=1; - tmp_error = ha_recovery_logging(thd,TRUE); + ha_recovery_logging(thd,TRUE); /* Ensure that the new table is saved properly to disk so that we can do a rename @@ -3007,50 +3312,50 @@ int mysql_checksum_table(THD *thd, TABLE_LIST *tables, HA_CHECK_OPT *check_opt) t->pos_in_table_list= table; if (t->file->table_flags() & HA_HAS_CHECKSUM && - !(check_opt->flags & T_EXTEND)) - protocol->store((ulonglong)t->file->checksum()); + !(check_opt->flags & T_EXTEND)) + protocol->store((ulonglong)t->file->checksum()); else if (!(t->file->table_flags() & HA_HAS_CHECKSUM) && (check_opt->flags & T_QUICK)) - protocol->store_null(); + protocol->store_null(); else { - /* calculating table's checksum */ - ha_checksum crc= 0; - - /* InnoDB must be told explicitly to retrieve all columns, because - this function does not set field->query_id in the columns to the - current query id */ - t->file->extra(HA_EXTRA_RETRIEVE_ALL_COLS); - - if (t->file->rnd_init(1)) - protocol->store_null(); - else - { - while (!t->file->rnd_next(t->record[0])) - { - ha_checksum row_crc= 0; - if (t->record[0] != (byte*) t->field[0]->ptr) - row_crc= my_checksum(row_crc, t->record[0], + /* calculating table's checksum */ + ha_checksum crc= 0; + + /* InnoDB must be told explicitly to retrieve all columns, because + this function does not set field->query_id in the columns to the + current query id */ + t->file->extra(HA_EXTRA_RETRIEVE_ALL_COLS); + + if (t->file->rnd_init(1)) + protocol->store_null(); + else + { + while (!t->file->rnd_next(t->record[0])) + { + ha_checksum row_crc= 0; + if (t->record[0] != (byte*) t->field[0]->ptr) + row_crc= my_checksum(row_crc, t->record[0], ((byte*) t->field[0]->ptr) - t->record[0]); - for (uint i= 0; i < t->fields; i++ ) - { - Field *f= t->field[i]; - if (f->type() == FIELD_TYPE_BLOB) - { - String tmp; - f->val_str(&tmp,&tmp); - row_crc= my_checksum(row_crc, (byte*) tmp.ptr(), tmp.length()); - } - else - row_crc= my_checksum(row_crc, (byte*) f->ptr, + for (uint i= 0; i < t->fields; i++ ) + { + Field *f= t->field[i]; + if (f->type() == FIELD_TYPE_BLOB) + { + String tmp; + f->val_str(&tmp); + row_crc= my_checksum(row_crc, (byte*) tmp.ptr(), tmp.length()); + } + else + row_crc= my_checksum(row_crc, (byte*) f->ptr, f->pack_length()); - } + } - crc+= row_crc; - } - protocol->store((ulonglong)crc); - } + crc+= row_crc; + } + protocol->store((ulonglong)crc); + } } thd->clear_error(); close_thread_tables(thd); diff --git a/sql/sql_test.cc b/sql/sql_test.cc index f2909b93a2a..348b5347c0d 100644 --- a/sql/sql_test.cc +++ b/sql/sql_test.cc @@ -359,7 +359,7 @@ reads: %10lu\n\n", name, (ulong) key_cache->param_buff_size, key_cache->param_block_size, key_cache->param_division_limit, key_cache->param_age_threshold, - key_cache->global_blocks_used,key_cache->global_blocks_changed, + key_cache->blocks_used,key_cache->global_blocks_changed, key_cache->global_cache_w_requests,key_cache->global_cache_write, key_cache->global_cache_r_requests,key_cache->global_cache_read); } diff --git a/sql/sql_union.cc b/sql/sql_union.cc index 7ac5c23a54d..8136ce44d1d 100644 --- a/sql/sql_union.cc +++ b/sql/sql_union.cc @@ -106,13 +106,47 @@ bool select_union::flush() } +/* + initialization procedures before fake_select_lex preparation() + + SYNOPSIS + st_select_lex_unit::init_prepare_fake_select_lex() + thd - thread handler + + RETURN + options of SELECT +*/ + +ulong +st_select_lex_unit::init_prepare_fake_select_lex(THD *thd) +{ + ulong options_tmp= thd->options; + thd->lex->current_select= fake_select_lex; + offset_limit_cnt= global_parameters->offset_limit; + select_limit_cnt= global_parameters->select_limit + + global_parameters->offset_limit; + + if (select_limit_cnt < global_parameters->select_limit) + select_limit_cnt= HA_POS_ERROR; // no limit + if (select_limit_cnt == HA_POS_ERROR) + options_tmp&= ~OPTION_FOUND_ROWS; + else if (found_rows_for_union && !thd->lex->describe) + options_tmp|= OPTION_FOUND_ROWS; + fake_select_lex->ftfunc_list_alloc.empty(); + fake_select_lex->ftfunc_list= &fake_select_lex->ftfunc_list_alloc; + fake_select_lex->table_list.link_in_list((byte *)&result_table_list, + (byte **) + &result_table_list.next); + return options_tmp; +} + + int st_select_lex_unit::prepare(THD *thd_arg, select_result *sel_result, ulong additional_options) { SELECT_LEX *lex_select_save= thd_arg->lex->current_select; SELECT_LEX *sl, *first_select; select_result *tmp_result; - ORDER *tmp_order; DBUG_ENTER("st_select_lex_unit::prepare"); /* @@ -205,14 +239,13 @@ int st_select_lex_unit::prepare(THD *thd_arg, select_result *sel_result, } } - item_list.empty(); // it is not single select if (first_select->next_select()) { union_result->tmp_table_param.field_count= types.elements; if (!(table= create_tmp_table(thd_arg, &union_result->tmp_table_param, types, - (ORDER*) 0, union_distinct, 1, + (ORDER*) 0, (bool) union_distinct, 1, (first_select_in_union()->options | thd_arg->options | TMP_TABLE_ALL_COLUMNS), @@ -227,6 +260,7 @@ int st_select_lex_unit::prepare(THD *thd_arg, select_result *sel_result, union_result->set_table(table); thd_arg->lex->current_select= lex_select_save; + if (!item_list.elements) { Statement *stmt= thd->current_statement; Statement backup; @@ -244,7 +278,30 @@ int st_select_lex_unit::prepare(THD *thd_arg, select_result *sel_result, } } if (stmt) + { thd->restore_backup_item_arena(stmt, &backup); + + /* prepare fake select to initialize it correctly */ + ulong options_tmp= init_prepare_fake_select_lex(thd); + if (!(fake_select_lex->join= new JOIN(thd, item_list, thd->options, + result))) + { + fake_select_lex->table_list.empty(); + DBUG_RETURN(-1); + } + fake_select_lex->item_list= item_list; + + thd_arg->lex->current_select= fake_select_lex; + res= fake_select_lex->join-> + prepare(&fake_select_lex->ref_pointer_array, + (TABLE_LIST*) fake_select_lex->table_list.first, + 0, 0, + fake_select_lex->order_list.elements, + (ORDER*) fake_select_lex->order_list.first, + (ORDER*) NULL, NULL, (ORDER*) NULL, + fake_select_lex, this); + fake_select_lex->table_list.empty(); + } } } else @@ -364,8 +421,6 @@ int st_select_lex_unit::exec() optimized= 1; /* Send result to 'result' */ - - res= -1; { List<Item_func_match> empty_list; @@ -373,27 +428,20 @@ int st_select_lex_unit::exec() if (!thd->is_fatal_error) // Check if EOM { - thd->lex->current_select= fake_select_lex; - fake_select_lex->options= thd->options; - set_limit(global_parameters, fake_select_lex); - - if (found_rows_for_union && !thd->lex->describe && - select_limit_cnt != HA_POS_ERROR) - fake_select_lex->options|= OPTION_FOUND_ROWS; - fake_select_lex->ftfunc_list= &empty_list; - fake_select_lex->table_list.link_in_list((byte *)&result_table_list, - (byte **) - &result_table_list.next); + ulong options_tmp= init_prepare_fake_select_lex(thd); JOIN *join= fake_select_lex->join; if (!join) { /* - allocate JOIN for fake select only once (privent + allocate JOIN for fake select only once (prevent mysql_select automatic allocation) */ if (!(fake_select_lex->join= new JOIN(thd, item_list, fake_select_lex->options, result))) + { + fake_select_lex->table_list.empty(); DBUG_RETURN(-1); + } /* Fake st_select_lex should have item list for correctref_array @@ -419,6 +467,8 @@ int st_select_lex_unit::exec() (ORDER*) NULL, NULL, (ORDER*) NULL, fake_select_lex->options | SELECT_NO_UNLOCK, result, this, fake_select_lex); + + fake_select_lex->table_list.empty(); if (!res) thd->limit_found_rows = (ulonglong)table->file->records + add_rows; /* @@ -452,13 +502,24 @@ int st_select_lex_unit::cleanup() table= 0; // Safety } JOIN *join; - for (SELECT_LEX *sl= first_select_in_union(); sl; sl= sl->next_select()) + SELECT_LEX *sl= first_select_in_union(); + for (; sl; sl= sl->next_select()) { if ((join= sl->join)) { error|= sl->join->cleanup(); delete join; } + else + { + // it can be DO/SET with subqueries + for (SELECT_LEX_UNIT *lex_unit= sl->first_inner_unit(); + lex_unit != 0; + lex_unit= lex_unit->next_unit()) + { + error|= lex_unit->cleanup(); + } + } } if (fake_select_lex && (join= fake_select_lex->join)) { diff --git a/sql/sql_update.cc b/sql/sql_update.cc index 3e9bb0b753b..918a4c769b4 100644 --- a/sql/sql_update.cc +++ b/sql/sql_update.cc @@ -70,8 +70,6 @@ int mysql_update(THD *thd, READ_RECORD info; TABLE_LIST *update_table_list= ((TABLE_LIST*) thd->lex->select_lex.table_list.first); - TABLE_LIST tables; - List<Item> all_fields; DBUG_ENTER("mysql_update"); LINT_INIT(used_index); @@ -86,30 +84,13 @@ int mysql_update(THD *thd, /* Calculate "table->used_keys" based on the WHERE */ table->used_keys=table->keys_in_use; table->quick_keys.clear_all(); + #ifndef NO_EMBEDDED_ACCESS_CHECKS - want_privilege=table->grant.want_privilege; - table->grant.want_privilege=(SELECT_ACL & ~table->grant.privilege); + want_privilege= table->grant.want_privilege; #endif - - bzero((char*) &tables,sizeof(tables)); // For ORDER BY - tables.table= table; - tables.alias= table_list->alias; - - if (setup_tables(update_table_list) || - setup_conds(thd,update_table_list,&conds) || - thd->lex->select_lex.setup_ref_array(thd, order_num) || - setup_order(thd, thd->lex->select_lex.ref_pointer_array, - &tables, all_fields, all_fields, order) || - setup_ftfuncs(&thd->lex->select_lex)) - DBUG_RETURN(-1); /* purecov: inspected */ - - /* Check that we are not using table that we are updating in a sub select */ - if (find_real_table_in_list(table_list->next, - table_list->db, table_list->real_name)) - { - my_error(ER_UPDATE_TABLE_USED, MYF(0), table_list->real_name); - DBUG_RETURN(-1); - } + if ((error= mysql_prepare_update(thd, table_list, update_table_list, + &conds, order_num, order))) + DBUG_RETURN(error); old_used_keys= table->used_keys; // Keys used in WHERE /* @@ -420,6 +401,59 @@ err: DBUG_RETURN(-1); } +/* + Prepare items in UPDATE statement + + SYNOPSIS + mysql_prepare_update() + thd - thread handler + table_list - global table list + update_table_list - local table list of UPDATE SELECT_LEX + conds - conditions + order_num - number of ORDER BY list entries + order - ORDER BY clause list + + RETURN VALUE + 0 - OK + 1 - error (message is sent to user) + -1 - error (message is not sent to user) +*/ +int mysql_prepare_update(THD *thd, TABLE_LIST *table_list, + TABLE_LIST *update_table_list, + Item **conds, uint order_num, ORDER *order) +{ + TABLE *table= table_list->table; + TABLE_LIST tables; + List<Item> all_fields; + DBUG_ENTER("mysql_prepare_update"); + +#ifndef NO_EMBEDDED_ACCESS_CHECKS + table->grant.want_privilege= (SELECT_ACL & ~table->grant.privilege); +#endif + + bzero((char*) &tables,sizeof(tables)); // For ORDER BY + tables.table= table; + tables.alias= table_list->alias; + + if (setup_tables(update_table_list) || + setup_conds(thd, update_table_list, conds) || + thd->lex->select_lex.setup_ref_array(thd, order_num) || + setup_order(thd, thd->lex->select_lex.ref_pointer_array, + update_table_list, all_fields, all_fields, order) || + setup_ftfuncs(&thd->lex->select_lex)) + DBUG_RETURN(-1); + + /* Check that we are not using table that we are updating in a sub select */ + if (find_real_table_in_list(table_list->next, + table_list->db, table_list->real_name)) + { + my_error(ER_UPDATE_TABLE_USED, MYF(0), table_list->real_name); + DBUG_RETURN(-1); + } + + DBUG_RETURN(0); +} + /*************************************************************************** Update multiple tables from join @@ -441,6 +475,7 @@ int mysql_multi_update(THD *thd, int res; multi_update *result; TABLE_LIST *tl; + TABLE_LIST *update_list= (TABLE_LIST*) thd->lex->select_lex.table_list.first; table_map item_tables= 0, derived_tables= 0; DBUG_ENTER("mysql_multi_update"); @@ -453,7 +488,7 @@ int mysql_multi_update(THD *thd, Ensure that we have update privilege for all tables and columns in the SET part */ - for (tl= table_list ; tl ; tl=tl->next) + for (tl= update_list; tl; tl= tl->next) { TABLE *table= tl->table; /* @@ -470,14 +505,14 @@ int mysql_multi_update(THD *thd, { // Assign table map values to check updatability of derived tables uint tablenr=0; - for (TABLE_LIST *table_list= (TABLE_LIST*) select_lex->table_list.first; + for (TABLE_LIST *table_list= update_list; table_list; table_list= table_list->next, tablenr++) { table_list->table->map= (table_map) 1 << tablenr; } } - if (setup_fields(thd, 0, table_list, *fields, 1, 0, 0)) + if (setup_fields(thd, 0, update_list, *fields, 1, 0, 0)) DBUG_RETURN(-1); if (thd->lex->derived_tables) { @@ -493,7 +528,7 @@ int mysql_multi_update(THD *thd, /* Count tables and setup timestamp handling */ - for (tl= select_lex->get_table_list() ; tl ; tl= tl->next) + for (tl= update_list; tl; tl= tl->next) { TABLE *table= tl->table; @@ -510,7 +545,7 @@ int mysql_multi_update(THD *thd, if (thd->lex->derived_tables && (item_tables & derived_tables)) { // find derived table which cause error - for (tl= select_lex->get_table_list() ; tl ; tl= tl->next) + for (tl= update_list; tl; tl= tl->next) { if (tl->derived && (item_tables & tl->table->map)) { @@ -521,7 +556,7 @@ int mysql_multi_update(THD *thd, } } - if (!(result=new multi_update(thd, table_list, fields, values, + if (!(result=new multi_update(thd, update_list, fields, values, handle_duplicates))) DBUG_RETURN(-1); diff --git a/sql/sql_yacc.yy b/sql/sql_yacc.yy index f72d43ad298..0874abaca9c 100644 --- a/sql/sql_yacc.yy +++ b/sql/sql_yacc.yy @@ -193,7 +193,7 @@ bool my_yyoverflow(short **a, YYSTYPE **b,int *yystacksize); %token ACTION %token AGGREGATE_SYM %token ALL -%token AND +%token AND_SYM %token AS %token ASC %token AUTO_INC @@ -329,6 +329,7 @@ bool my_yyoverflow(short **a, YYSTYPE **b,int *yystacksize); %token NAME_SYM %token NATIONAL_SYM %token NATURAL +%token NDBCLUSTER_SYM %token NEW_SYM %token NCHAR_SYM %token NCHAR_STRING @@ -342,7 +343,7 @@ bool my_yyoverflow(short **a, YYSTYPE **b,int *yystacksize); %token OPEN_SYM %token OPTION %token OPTIONALLY -%token OR +%token OR_SYM %token OR_OR_CONCAT %token ORDER_SYM %token OUT_SYM @@ -617,8 +618,8 @@ bool my_yyoverflow(short **a, YYSTYPE **b,int *yystacksize); %token BEFORE_SYM %left SET_VAR -%left OR_OR_CONCAT OR XOR -%left AND +%left OR_OR_CONCAT OR_SYM XOR +%left AND_SYM %left BETWEEN_SYM CASE_SYM WHEN_SYM THEN_SYM ELSE %left EQ EQUAL_SYM GE GT_SYM LE LT NE IS LIKE REGEXP IN_SYM %left '|' @@ -646,7 +647,7 @@ bool my_yyoverflow(short **a, YYSTYPE **b,int *yystacksize); %type <simple_string> remember_name remember_end opt_ident opt_db text_or_password - opt_escape opt_constraint + opt_escape opt_constraint constraint %type <string> text_string opt_gconcat_separator @@ -781,7 +782,7 @@ END_OF_INPUT %type <NONE> '-' '+' '*' '/' '%' '(' ')' - ',' '!' '{' '}' '&' '|' AND OR OR_OR_CONCAT BETWEEN_SYM CASE_SYM + ',' '!' '{' '}' '&' '|' AND_SYM OR_SYM OR_OR_CONCAT BETWEEN_SYM CASE_SYM THEN_SYM WHEN_SYM DIV_SYM MOD_SYM %% @@ -2240,7 +2241,7 @@ field_list_item: ; column_def: - field_spec check_constraint + field_spec opt_check_constraint | field_spec references { Lex->col_list.empty(); /* Alloced by sql_alloc */ @@ -2272,20 +2273,33 @@ key_def: lex->fk_match_option)); lex->col_list.empty(); /* Alloced by sql_alloc */ } + | constraint opt_check_constraint + { + Lex->col_list.empty(); /* Alloced by sql_alloc */ + } | opt_constraint check_constraint { Lex->col_list.empty(); /* Alloced by sql_alloc */ } ; -check_constraint: +opt_check_constraint: /* empty */ - | CHECK_SYM expr + | check_constraint + ; + +check_constraint: + CHECK_SYM expr ; opt_constraint: /* empty */ { $$=(char*) 0; } - | CONSTRAINT opt_ident { $$=$2; }; + | constraint { $$= $1; } + ; + +constraint: + CONSTRAINT opt_ident { $$=$2; } + ; field_spec: field_ident @@ -2504,10 +2518,29 @@ attribute: { Lex->on_update_value= new Item_func_now_local(); } | AUTO_INC { Lex->type|= AUTO_INCREMENT_FLAG | NOT_NULL_FLAG; } | SERIAL_SYM DEFAULT VALUE_SYM - { Lex->type|= AUTO_INCREMENT_FLAG | NOT_NULL_FLAG | UNIQUE_FLAG; } - | opt_primary KEY_SYM { Lex->type|= PRI_KEY_FLAG | NOT_NULL_FLAG; } - | UNIQUE_SYM { Lex->type|= UNIQUE_FLAG; } - | UNIQUE_SYM KEY_SYM { Lex->type|= UNIQUE_KEY_FLAG; } + { + LEX *lex=Lex; + lex->type|= AUTO_INCREMENT_FLAG | NOT_NULL_FLAG | UNIQUE_FLAG; + lex->alter_flags|= ALTER_ADD_INDEX; + } + | opt_primary KEY_SYM + { + LEX *lex=Lex; + lex->type|= PRI_KEY_FLAG | NOT_NULL_FLAG; + lex->alter_flags|= ALTER_ADD_INDEX; + } + | UNIQUE_SYM + { + LEX *lex=Lex; + lex->type|= UNIQUE_FLAG; + lex->alter_flags|= ALTER_ADD_INDEX; + } + | UNIQUE_SYM KEY_SYM + { + LEX *lex=Lex; + lex->type|= UNIQUE_KEY_FLAG; + lex->alter_flags|= ALTER_ADD_INDEX; + } | COMMENT_SYM TEXT_STRING_sys { Lex->comment= &$2; } | BINARY { Lex->type|= BINCMP_FLAG; } | COLLATE_SYM collation_name @@ -2765,6 +2798,7 @@ alter: lex->alter_keys_onoff=LEAVE_AS_IS; lex->tablespace_op=NO_TABLESPACE_OP; lex->simple_alter=1; + lex->alter_flags=0; } alter_list {} @@ -2813,16 +2847,28 @@ alter_list: | alter_list ',' alter_list_item; add_column: - ADD opt_column { Lex->change=0; }; + ADD opt_column + { + LEX *lex=Lex; + lex->change=0; + lex->alter_flags|= ALTER_ADD_COLUMN; + }; alter_list_item: add_column column_def opt_place { Lex->simple_alter=0; } - | ADD key_def { Lex->simple_alter=0; } + | ADD key_def + { + LEX *lex=Lex; + lex->simple_alter=0; + lex->alter_flags|= ALTER_ADD_INDEX; + } | add_column '(' field_list ')' { Lex->simple_alter=0; } | CHANGE opt_column field_ident { LEX *lex=Lex; - lex->change= $3.str; lex->simple_alter=0; + lex->change= $3.str; + lex->simple_alter=0; + lex->alter_flags|= ALTER_CHANGE_COLUMN; } field_spec opt_place | MODIFY_SYM opt_column field_ident @@ -2833,6 +2879,7 @@ alter_list_item: lex->comment=0; lex->charset= NULL; lex->simple_alter=0; + lex->alter_flags|= ALTER_CHANGE_COLUMN; } type opt_attribute { @@ -2851,7 +2898,9 @@ alter_list_item: { LEX *lex=Lex; lex->drop_list.push_back(new Alter_drop(Alter_drop::COLUMN, - $3.str)); lex->simple_alter=0; + $3.str)); + lex->simple_alter=0; + lex->alter_flags|= ALTER_DROP_COLUMN; } | DROP FOREIGN KEY_SYM opt_ident { Lex->simple_alter=0; } | DROP PRIMARY_SYM KEY_SYM @@ -2860,6 +2909,7 @@ alter_list_item: lex->drop_list.push_back(new Alter_drop(Alter_drop::KEY, primary_key_name)); lex->simple_alter=0; + lex->alter_flags|= ALTER_DROP_INDEX; } | DROP key_or_index field_ident { @@ -2867,6 +2917,7 @@ alter_list_item: lex->drop_list.push_back(new Alter_drop(Alter_drop::KEY, $3.str)); lex->simple_alter=0; + lex->alter_flags|= ALTER_DROP_INDEX; } | DISABLE_SYM KEYS { Lex->alter_keys_onoff=DISABLE; } | ENABLE_SYM KEYS { Lex->alter_keys_onoff=ENABLE; } @@ -2875,18 +2926,21 @@ alter_list_item: LEX *lex=Lex; lex->alter_list.push_back(new Alter_column($3.str,$6)); lex->simple_alter=0; + lex->alter_flags|= ALTER_CHANGE_COLUMN; } | ALTER opt_column field_ident DROP DEFAULT { LEX *lex=Lex; lex->alter_list.push_back(new Alter_column($3.str,(Item*) 0)); lex->simple_alter=0; + lex->alter_flags|= ALTER_CHANGE_COLUMN; } | RENAME opt_to table_ident { LEX *lex=Lex; lex->select_lex.db=$3->db.str; lex->name= $3->table.str; + lex->alter_flags|= ALTER_RENAME; } | CONVERT_SYM TO_SYM charset charset_name_or_default opt_collate { @@ -2909,8 +2963,18 @@ alter_list_item: HA_CREATE_USED_DEFAULT_CHARSET); lex->simple_alter= 0; } - | create_table_options_space_separated { Lex->simple_alter=0; } - | order_clause { Lex->simple_alter=0; }; + | create_table_options_space_separated + { + LEX *lex=Lex; + lex->simple_alter=0; + lex->alter_flags|= ALTER_OPTIONS; + } + | order_clause + { + LEX *lex=Lex; + lex->simple_alter=0; + lex->alter_flags|= ALTER_ORDER; + }; opt_column: /* empty */ {} @@ -3466,14 +3530,14 @@ expr_expr: { $$= new Item_func_not(new Item_in_subselect($1, $4)); } - | expr BETWEEN_SYM no_and_expr AND expr + | expr BETWEEN_SYM no_and_expr AND_SYM expr { $$= new Item_func_between($1,$3,$5); } - | expr NOT BETWEEN_SYM no_and_expr AND expr + | expr NOT BETWEEN_SYM no_and_expr AND_SYM expr { $$= new Item_func_not(new Item_func_between($1,$4,$6)); } | expr OR_OR_CONCAT expr { $$= or_or_concat(YYTHD, $1,$3); } - | expr OR expr { $$= new Item_cond_or($1,$3); } + | expr OR_SYM expr { $$= new Item_cond_or($1,$3); } | expr XOR expr { $$= new Item_cond_xor($1,$3); } - | expr AND expr { $$= new Item_cond_and($1,$3); } + | expr AND_SYM expr { $$= new Item_cond_and($1,$3); } | expr SOUNDS_SYM LIKE expr { $$= new Item_func_eq(new Item_func_soundex($1), @@ -3514,14 +3578,14 @@ expr_expr: /* expressions that begin with 'expr' that do NOT follow IN_SYM */ no_in_expr: - no_in_expr BETWEEN_SYM no_and_expr AND expr + no_in_expr BETWEEN_SYM no_and_expr AND_SYM expr { $$= new Item_func_between($1,$3,$5); } - | no_in_expr NOT BETWEEN_SYM no_and_expr AND expr + | no_in_expr NOT BETWEEN_SYM no_and_expr AND_SYM expr { $$= new Item_func_not(new Item_func_between($1,$4,$6)); } | no_in_expr OR_OR_CONCAT expr { $$= or_or_concat(YYTHD, $1,$3); } - | no_in_expr OR expr { $$= new Item_cond_or($1,$3); } + | no_in_expr OR_SYM expr { $$= new Item_cond_or($1,$3); } | no_in_expr XOR expr { $$= new Item_cond_xor($1,$3); } - | no_in_expr AND expr { $$= new Item_cond_and($1,$3); } + | no_in_expr AND_SYM expr { $$= new Item_cond_and($1,$3); } | no_in_expr SOUNDS_SYM LIKE expr { $$= new Item_func_eq(new Item_func_soundex($1), @@ -3572,12 +3636,12 @@ no_and_expr: { $$= new Item_func_not(new Item_in_subselect($1, $4)); } - | no_and_expr BETWEEN_SYM no_and_expr AND expr + | no_and_expr BETWEEN_SYM no_and_expr AND_SYM expr { $$= new Item_func_between($1,$3,$5); } - | no_and_expr NOT BETWEEN_SYM no_and_expr AND expr + | no_and_expr NOT BETWEEN_SYM no_and_expr AND_SYM expr { $$= new Item_func_not(new Item_func_between($1,$4,$6)); } | no_and_expr OR_OR_CONCAT expr { $$= or_or_concat(YYTHD, $1,$3); } - | no_and_expr OR expr { $$= new Item_cond_or($1,$3); } + | no_and_expr OR_SYM expr { $$= new Item_cond_or($1,$3); } | no_and_expr XOR expr { $$= new Item_cond_xor($1,$3); } | no_and_expr SOUNDS_SYM LIKE expr { @@ -4711,9 +4775,7 @@ opt_limit_clause_init: LEX *lex= Lex; SELECT_LEX *sel= lex->current_select; sel->offset_limit= 0L; - sel->select_limit= (&lex->select_lex == sel) ? - Lex->thd->variables.select_limit : /* primary SELECT */ - HA_POS_ERROR; /* subquery */ + sel->select_limit= HA_POS_ERROR; } | limit_clause {} ; @@ -4733,18 +4795,21 @@ limit_options: SELECT_LEX *sel= Select; sel->select_limit= $1; sel->offset_limit= 0L; + sel->explicit_limit= 1; } | ULONG_NUM ',' ULONG_NUM { SELECT_LEX *sel= Select; sel->select_limit= $3; sel->offset_limit= $1; + sel->explicit_limit= 1; } | ULONG_NUM OFFSET_SYM ULONG_NUM { SELECT_LEX *sel= Select; sel->select_limit= $1; sel->offset_limit= $3; + sel->explicit_limit= 1; } ; @@ -4756,7 +4821,11 @@ delete_limit_clause: lex->current_select->select_limit= HA_POS_ERROR; } | LIMIT ulonglong_num - { Select->select_limit= (ha_rows) $2; }; + { + SELECT_LEX *sel= Select; + sel->select_limit= (ha_rows) $2; + sel->explicit_limit= 1; + }; ULONG_NUM: NUM { $$= strtoul($1.str,NULL,10); } @@ -5320,8 +5389,8 @@ show_param: YYABORT; } | NEW_SYM MASTER_SYM FOR_SYM SLAVE WITH MASTER_LOG_FILE_SYM EQ - TEXT_STRING_sys AND MASTER_LOG_POS_SYM EQ ulonglong_num - AND MASTER_SERVER_ID_SYM EQ + TEXT_STRING_sys AND_SYM MASTER_LOG_POS_SYM EQ ulonglong_num + AND_SYM MASTER_SERVER_ID_SYM EQ ULONG_NUM { Lex->sql_command = SQLCOM_SHOW_NEW_MASTER; @@ -6220,6 +6289,7 @@ keyword: | NAMES_SYM {} | NATIONAL_SYM {} | NCHAR_SYM {} + | NDBCLUSTER_SYM {} | NEXT_SYM {} | NEW_SYM {} | NO_SYM {} @@ -6718,7 +6788,7 @@ grant_privilege: opt_and: /* empty */ {} - | AND {} + | AND_SYM {} ; require_list: diff --git a/sql/structs.h b/sql/structs.h index 86d754f00d6..ee231186e1a 100644 --- a/sql/structs.h +++ b/sql/structs.h @@ -150,7 +150,8 @@ typedef struct st_time { typedef struct { - long year,month,day,hour,minute,second,second_part; + ulong year,month,day,hour; + ulonglong minute,second,second_part; bool neg; } INTERVAL; @@ -183,7 +184,8 @@ enum SHOW_TYPE SHOW_SSL_CTX_SESS_TIMEOUTS, SHOW_SSL_CTX_SESS_CACHE_FULL, SHOW_SSL_GET_CIPHER_LIST, #endif /* HAVE_OPENSSL */ - SHOW_RPL_STATUS, SHOW_SLAVE_RUNNING, SHOW_KEY_CACHE_LONG + SHOW_RPL_STATUS, SHOW_SLAVE_RUNNING, + SHOW_KEY_CACHE_LONG, SHOW_KEY_CACHE_CONST_LONG }; enum SHOW_COMP_OPTION { SHOW_OPTION_YES, SHOW_OPTION_NO, SHOW_OPTION_DISABLED}; diff --git a/sql/table.cc b/sql/table.cc index 23d99466a83..73f036aed87 100644 --- a/sql/table.cc +++ b/sql/table.cc @@ -553,7 +553,6 @@ int openfrm(const char *name, const char *alias, uint db_stat, uint prgflag, keyinfo->key_length+= HA_KEY_NULL_LENGTH; } if (field->type() == FIELD_TYPE_BLOB || - field->type() == FIELD_TYPE_GEOMETRY || field->real_type() == FIELD_TYPE_VAR_STRING) { if (field->type() == FIELD_TYPE_BLOB) @@ -945,7 +944,8 @@ static void frm_error(int error, TABLE *form, const char *name, myf errortype) break; case 2: { - datext=form->file ? *form->file->bas_ext() : ""; + datext= form->file ? *form->file->bas_ext() : ""; + datext= datext==NullS ? "" : datext; err_no= (my_errno == ENOENT) ? ER_FILE_NOT_FOUND : (my_errno == EAGAIN) ? ER_FILE_USED : ER_CANT_OPEN_FILE; my_error(err_no,errortype, @@ -1140,6 +1140,11 @@ File create_frm(register my_string name, uint reclength, uchar *fileinfo, if (create_info->min_rows > ~(ulong) 0) create_info->min_rows= ~(ulong) 0; #endif + /* + Ensure that raid_chunks can't be larger than 255, as this would cause + problems with drop database + */ + set_if_smaller(create_info->raid_chunks, 255); if ((file=my_create(name,CREATE_MODE,O_RDWR | O_TRUNC,MYF(MY_WME))) >= 0) { @@ -1228,7 +1233,7 @@ bool get_field(MEM_ROOT *mem, Field *field, String *res) String str(buff,sizeof(buff),&my_charset_bin); uint length; - field->val_str(&str,&str); + field->val_str(&str); if (!(length= str.length())) return 1; to= strmake_root(mem, str.ptr(), length); @@ -1256,7 +1261,7 @@ char *get_field(MEM_ROOT *mem, Field *field) String str(buff,sizeof(buff),&my_charset_bin); uint length; - field->val_str(&str,&str); + field->val_str(&str); length= str.length(); if (!length || !(to= (char*) alloc_root(mem,length+1))) return NullS; diff --git a/sql/table.h b/sql/table.h index 18155e1568d..03b33cd564d 100644 --- a/sql/table.h +++ b/sql/table.h @@ -195,6 +195,8 @@ typedef struct st_table_list bool force_index; /* Prefer index over table scan */ bool ignore_leaves; /* Preload only non-leaf nodes */ bool cacheable_table; /* stop PS caching */ + /* used in multi-upd privelege check */ + bool table_in_update_from_clause; } TABLE_LIST; typedef struct st_changed_table_list diff --git a/sql/unireg.cc b/sql/unireg.cc index 0e4b449c6a3..bab021aed59 100644 --- a/sql/unireg.cc +++ b/sql/unireg.cc @@ -46,11 +46,29 @@ static bool make_empty_rec(int file, enum db_type table_type, List<create_field> &create_fields, uint reclength,uint null_fields); +/* + Create a frm (table definition) file + + SYNOPSIS + mysql_create_frm() + thd Thread handler + file_name Name of file (including database and .frm) + create_info create info parameters + create_fields Fields to create + keys number of keys to create + key_info Keys to create + db_file Handler to use. May be zero, in which case we use + create_info->db_type + RETURN + 0 ok + 1 error +*/ -int rea_create_table(THD *thd, my_string file_name, - HA_CREATE_INFO *create_info, - List<create_field> &create_fields, - uint keys, KEY *key_info) +bool mysql_create_frm(THD *thd, my_string file_name, + HA_CREATE_INFO *create_info, + List<create_field> &create_fields, + uint keys, KEY *key_info, + handler *db_file) { uint reclength,info_length,screens,key_info_length,maxlength,null_fields; File file; @@ -58,13 +76,13 @@ int rea_create_table(THD *thd, my_string file_name, uchar fileinfo[64],forminfo[288],*keybuff; TYPELIB formnames; uchar *screen_buff; - handler *db_file; DBUG_ENTER("rea_create_table"); formnames.type_names=0; if (!(screen_buff=pack_screens(create_fields,&info_length,&screens,0))) DBUG_RETURN(1); - db_file=get_new_handler((TABLE*) 0, create_info->db_type); + if (db_file == NULL) + db_file=get_new_handler((TABLE*) 0, create_info->db_type); if (pack_header(forminfo, create_info->db_type,create_fields,info_length, screens, create_info->table_options, db_file)) { @@ -155,8 +173,7 @@ int rea_create_table(THD *thd, my_string file_name, if (opt_sync_frm && !(create_info->options & HA_LEX_CREATE_TMP_TABLE) && my_sync(file, MYF(MY_WME))) goto err2; - if (my_close(file,MYF(MY_WME)) || - ha_create_table(file_name,create_info,0)) + if (my_close(file,MYF(MY_WME))) goto err3; DBUG_RETURN(0); @@ -168,6 +185,43 @@ err2: err3: my_delete(file_name,MYF(0)); DBUG_RETURN(1); +} /* mysql_create_frm */ + + +/* + Create a frm (table definition) file and the tables + + SYNOPSIS + mysql_create_frm() + thd Thread handler + file_name Name of file (including database and .frm) + create_info create info parameters + create_fields Fields to create + keys number of keys to create + key_info Keys to create + db_file Handler to use. May be zero, in which case we use + create_info->db_type + RETURN + 0 ok + 1 error +*/ + +int rea_create_table(THD *thd, my_string file_name, + HA_CREATE_INFO *create_info, + List<create_field> &create_fields, + uint keys, KEY *key_info) +{ + DBUG_ENTER("rea_create_table"); + + if (mysql_create_frm(thd, file_name, create_info, + create_fields, keys, key_info, NULL)) + DBUG_RETURN(1); + if (ha_create_table(file_name,create_info,0)) + { + my_delete(file_name,MYF(0)); + DBUG_RETURN(1); + } + DBUG_RETURN(0); } /* rea_create_table */ diff --git a/sql/unireg.h b/sql/unireg.h index 442809a9e08..4ab2ba26b15 100644 --- a/sql/unireg.h +++ b/sql/unireg.h @@ -48,7 +48,7 @@ #define MAX_ALIAS_NAME 256 #define MAX_FIELD_NAME 34 /* Max colum name length +2 */ #define MAX_SYS_VAR_LENGTH 32 -#define MAX_KEY 32 /* Max used keys */ +#define MAX_KEY 64 /* Max used keys */ #define MAX_REF_PARTS 16 /* Max parts used as ref */ #define MAX_KEY_LENGTH 1024 /* max possible key */ #if SIZEOF_OFF_T > 4 |