2006-12-31 01:02:27 +01:00
|
|
|
/* Copyright (C) 2000-2006 MySQL AB
|
2001-12-06 13:10:51 +01:00
|
|
|
|
2000-07-31 21:29:14 +02:00
|
|
|
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
|
2006-12-23 20:17:15 +01:00
|
|
|
the Free Software Foundation; version 2 of the License.
|
2001-12-06 13:10:51 +01:00
|
|
|
|
2000-07-31 21:29:14 +02:00
|
|
|
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.
|
2001-12-06 13:10:51 +01:00
|
|
|
|
2000-07-31 21:29:14 +02:00
|
|
|
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 */
|
|
|
|
|
|
|
|
|
|
|
|
/* Copy data from a textfile to table */
|
|
|
|
|
|
|
|
#include "mysql_priv.h"
|
|
|
|
#include <my_dir.h>
|
|
|
|
#include <m_ctype.h>
|
2001-08-03 23:57:53 +02:00
|
|
|
#include "sql_repl.h"
|
2005-05-24 20:19:33 +02:00
|
|
|
#include "sp_head.h"
|
|
|
|
#include "sql_trigger.h"
|
2000-07-31 21:29:14 +02:00
|
|
|
|
|
|
|
class READ_INFO {
|
|
|
|
File file;
|
|
|
|
byte *buffer, /* Buffer for read text */
|
|
|
|
*end_of_buff; /* Data in bufferts ends here */
|
|
|
|
uint buff_length, /* Length of buffert */
|
|
|
|
max_length; /* Max length of row */
|
|
|
|
char *field_term_ptr,*line_term_ptr,*line_start_ptr,*line_start_end;
|
|
|
|
uint field_term_length,line_term_length,enclosed_length;
|
|
|
|
int field_term_char,line_term_char,enclosed_char,escape_char;
|
|
|
|
int *stack,*stack_pos;
|
|
|
|
bool found_end_of_line,start_of_line,eof;
|
2001-08-03 23:57:53 +02:00
|
|
|
bool need_end_io_cache;
|
2000-07-31 21:29:14 +02:00
|
|
|
IO_CACHE cache;
|
|
|
|
NET *io_net;
|
|
|
|
|
|
|
|
public:
|
|
|
|
bool error,line_cuted,found_null,enclosed;
|
|
|
|
byte *row_start, /* Found row starts here */
|
|
|
|
*row_end; /* Found row ends here */
|
2003-03-07 09:55:55 +01:00
|
|
|
CHARSET_INFO *read_charset;
|
2000-07-31 21:29:14 +02:00
|
|
|
|
2003-03-07 09:55:55 +01:00
|
|
|
READ_INFO(File file,uint tot_length,CHARSET_INFO *cs,
|
2000-07-31 21:29:14 +02:00
|
|
|
String &field_term,String &line_start,String &line_term,
|
2000-09-25 23:33:25 +02:00
|
|
|
String &enclosed,int escape,bool get_it_from_net, bool is_fifo);
|
2000-07-31 21:29:14 +02:00
|
|
|
~READ_INFO();
|
|
|
|
int read_field();
|
|
|
|
int read_fixed_length(void);
|
|
|
|
int next_line(void);
|
|
|
|
char unescape(char chr);
|
|
|
|
int terminator(char *ptr,uint length);
|
|
|
|
bool find_start_of_fields();
|
2002-07-23 17:31:22 +02:00
|
|
|
/*
|
|
|
|
We need to force cache close before destructor is invoked to log
|
|
|
|
the last read block
|
|
|
|
*/
|
2001-08-03 23:57:53 +02:00
|
|
|
void end_io_cache()
|
|
|
|
{
|
|
|
|
::end_io_cache(&cache);
|
|
|
|
need_end_io_cache = 0;
|
|
|
|
}
|
|
|
|
|
2002-07-23 17:31:22 +02:00
|
|
|
/*
|
|
|
|
Either this method, or we need to make cache public
|
|
|
|
Arg must be set from mysql_load() since constructor does not see
|
|
|
|
either the table or THD value
|
|
|
|
*/
|
2001-08-03 23:57:53 +02:00
|
|
|
void set_io_cache_arg(void* arg) { cache.arg = arg; }
|
2000-07-31 21:29:14 +02:00
|
|
|
};
|
|
|
|
|
2004-10-21 20:53:27 +02:00
|
|
|
static int read_fixed_length(THD *thd, COPY_INFO &info, TABLE_LIST *table_list,
|
2005-03-16 02:32:47 +01:00
|
|
|
List<Item> &fields_vars, List<Item> &set_fields,
|
|
|
|
List<Item> &set_values, READ_INFO &read_info,
|
2004-10-21 20:53:27 +02:00
|
|
|
ulong skip_lines,
|
|
|
|
bool ignore_check_option_errors);
|
|
|
|
static int read_sep_field(THD *thd, COPY_INFO &info, TABLE_LIST *table_list,
|
2005-03-16 02:32:47 +01:00
|
|
|
List<Item> &fields_vars, List<Item> &set_fields,
|
|
|
|
List<Item> &set_values, READ_INFO &read_info,
|
2004-10-21 20:53:27 +02:00
|
|
|
String &enclosed, ulong skip_lines,
|
|
|
|
bool ignore_check_option_errors);
|
2006-12-14 23:51:37 +01:00
|
|
|
#ifndef EMBEDDED_LIBRARY
|
2005-03-25 14:51:17 +01:00
|
|
|
static bool write_execute_load_query_log_event(THD *thd,
|
|
|
|
bool duplicates, bool ignore,
|
|
|
|
bool transactional_table);
|
2006-12-14 23:51:37 +01:00
|
|
|
#endif /* EMBEDDED_LIBRARY */
|
2005-03-16 02:32:47 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
Execute LOAD DATA query
|
|
|
|
|
|
|
|
SYNOPSYS
|
|
|
|
mysql_load()
|
|
|
|
thd - current thread
|
|
|
|
ex - sql_exchange object representing source file and its parsing rules
|
|
|
|
table_list - list of tables to which we are loading data
|
|
|
|
fields_vars - list of fields and variables to which we read
|
|
|
|
data from file
|
|
|
|
set_fields - list of fields mentioned in set clause
|
|
|
|
set_values - expressions to assign to fields in previous list
|
|
|
|
handle_duplicates - indicates whenever we should emit error or
|
|
|
|
replace row if we will meet duplicates.
|
|
|
|
ignore - - indicates whenever we should ignore duplicates
|
|
|
|
read_file_from_client - is this LOAD DATA LOCAL ?
|
|
|
|
|
|
|
|
RETURN VALUES
|
|
|
|
TRUE - error / FALSE - success
|
|
|
|
*/
|
|
|
|
|
2004-10-20 03:04:37 +02:00
|
|
|
bool mysql_load(THD *thd,sql_exchange *ex,TABLE_LIST *table_list,
|
2005-03-16 02:32:47 +01:00
|
|
|
List<Item> &fields_vars, List<Item> &set_fields,
|
|
|
|
List<Item> &set_values,
|
|
|
|
enum enum_duplicates handle_duplicates, bool ignore,
|
2005-03-16 10:13:35 +01:00
|
|
|
bool read_file_from_client)
|
2000-07-31 21:29:14 +02:00
|
|
|
{
|
|
|
|
char name[FN_REFLEN];
|
|
|
|
File file;
|
2006-06-02 22:21:32 +02:00
|
|
|
TABLE *table= NULL;
|
2000-07-31 21:29:14 +02:00
|
|
|
int error;
|
2003-12-14 05:39:52 +01:00
|
|
|
String *field_term=ex->field_term,*escaped=ex->escaped;
|
|
|
|
String *enclosed=ex->enclosed;
|
2000-09-25 23:33:25 +02:00
|
|
|
bool is_fifo=0;
|
2003-09-06 15:50:30 +02:00
|
|
|
#ifndef EMBEDDED_LIBRARY
|
2001-08-03 23:57:53 +02:00
|
|
|
LOAD_FILE_INFO lf_info;
|
2003-09-06 15:50:30 +02:00
|
|
|
#endif
|
2003-02-26 23:06:09 +01:00
|
|
|
char *db = table_list->db; // This is never null
|
2004-02-16 17:58:21 +01:00
|
|
|
/*
|
|
|
|
If path for file is not defined, we will use the current database.
|
|
|
|
If this is not set, we will use the directory where the table to be
|
|
|
|
loaded is located
|
|
|
|
*/
|
2004-02-16 08:41:13 +01:00
|
|
|
char *tdb= thd->db ? thd->db : db; // Result is never null
|
2003-12-14 05:39:52 +01:00
|
|
|
ulong skip_lines= ex->skip_lines;
|
2005-01-27 22:38:56 +01:00
|
|
|
bool transactional_table;
|
2000-07-31 21:29:14 +02:00
|
|
|
DBUG_ENTER("mysql_load");
|
|
|
|
|
2002-07-23 17:31:22 +02:00
|
|
|
#ifdef EMBEDDED_LIBRARY
|
|
|
|
read_file_from_client = 0; //server is always in the same process
|
|
|
|
#endif
|
|
|
|
|
2000-07-31 21:29:14 +02:00
|
|
|
if (escaped->length() > 1 || enclosed->length() > 1)
|
|
|
|
{
|
|
|
|
my_message(ER_WRONG_FIELD_TERMINATORS,ER(ER_WRONG_FIELD_TERMINATORS),
|
|
|
|
MYF(0));
|
2004-10-20 03:04:37 +02:00
|
|
|
DBUG_RETURN(TRUE);
|
2000-07-31 21:29:14 +02:00
|
|
|
}
|
2004-10-20 03:04:37 +02:00
|
|
|
if (open_and_lock_tables(thd, table_list))
|
|
|
|
DBUG_RETURN(TRUE);
|
2006-05-26 10:47:53 +02:00
|
|
|
if (setup_tables_and_check_access(thd, &thd->lex->select_lex.context,
|
|
|
|
&thd->lex->select_lex.top_join_list,
|
2006-06-04 20:05:22 +02:00
|
|
|
table_list,
|
2006-05-26 10:47:53 +02:00
|
|
|
&thd->lex->select_lex.leaf_tables, FALSE,
|
2006-08-15 19:45:24 +02:00
|
|
|
INSERT_ACL | UPDATE_ACL,
|
2006-05-26 10:47:53 +02:00
|
|
|
INSERT_ACL | UPDATE_ACL))
|
2004-10-21 20:53:27 +02:00
|
|
|
DBUG_RETURN(-1);
|
2004-11-21 18:33:49 +01:00
|
|
|
if (!table_list->table || // do not suport join view
|
|
|
|
!table_list->updatable || // and derived tables
|
|
|
|
check_key_in_view(thd, table_list))
|
2004-07-16 00:15:55 +02:00
|
|
|
{
|
2004-11-13 18:35:51 +01:00
|
|
|
my_error(ER_NON_UPDATABLE_TABLE, MYF(0), table_list->alias, "LOAD");
|
2004-10-20 03:04:37 +02:00
|
|
|
DBUG_RETURN(TRUE);
|
2004-07-16 00:15:55 +02:00
|
|
|
}
|
2005-07-01 06:05:42 +02:00
|
|
|
if (table_list->prepare_where(thd, 0, TRUE) ||
|
|
|
|
table_list->prepare_check_option(thd))
|
|
|
|
{
|
|
|
|
DBUG_RETURN(TRUE);
|
|
|
|
}
|
2005-03-16 02:32:47 +01:00
|
|
|
/*
|
|
|
|
Let us emit an error if we are loading data to table which is used
|
|
|
|
in subselect in SET clause like we do it for INSERT.
|
|
|
|
|
|
|
|
The main thing to fix to remove this restriction is to ensure that the
|
|
|
|
table is marked to be 'used for insert' in which case we should never
|
2005-05-02 15:45:33 +02:00
|
|
|
mark this table as 'const table' (ie, one that has only one row).
|
2005-03-16 02:32:47 +01:00
|
|
|
*/
|
2007-03-01 22:09:22 +01:00
|
|
|
if (unique_table(thd, table_list, table_list->next_global, 0))
|
2005-03-16 02:32:47 +01:00
|
|
|
{
|
|
|
|
my_error(ER_UPDATE_TABLE_USED, MYF(0), table_list->table_name);
|
|
|
|
DBUG_RETURN(TRUE);
|
|
|
|
}
|
|
|
|
|
2004-07-16 00:15:55 +02:00
|
|
|
table= table_list->table;
|
2002-11-07 03:02:37 +01:00
|
|
|
transactional_table= table->file->has_transactions();
|
|
|
|
|
2005-03-16 02:32:47 +01:00
|
|
|
if (!fields_vars.elements)
|
2000-07-31 21:29:14 +02:00
|
|
|
{
|
|
|
|
Field **field;
|
|
|
|
for (field=table->field; *field ; field++)
|
2005-03-16 02:32:47 +01:00
|
|
|
fields_vars.push_back(new Item_field(*field));
|
This changeset is largely a handler cleanup changeset (WL#3281), but includes fixes and cleanups that was found necessary while testing the handler changes
Changes that requires code changes in other code of other storage engines.
(Note that all changes are very straightforward and one should find all issues
by compiling a --debug build and fixing all compiler errors and all
asserts in field.cc while running the test suite),
- New optional handler function introduced: reset()
This is called after every DML statement to make it easy for a handler to
statement specific cleanups.
(The only case it's not called is if force the file to be closed)
- handler::extra(HA_EXTRA_RESET) is removed. Code that was there before
should be moved to handler::reset()
- table->read_set contains a bitmap over all columns that are needed
in the query. read_row() and similar functions only needs to read these
columns
- table->write_set contains a bitmap over all columns that will be updated
in the query. write_row() and update_row() only needs to update these
columns.
The above bitmaps should now be up to date in all context
(including ALTER TABLE, filesort()).
The handler is informed of any changes to the bitmap after
fix_fields() by calling the virtual function
handler::column_bitmaps_signal(). If the handler does caching of
these bitmaps (instead of using table->read_set, table->write_set),
it should redo the caching in this code. as the signal() may be sent
several times, it's probably best to set a variable in the signal
and redo the caching on read_row() / write_row() if the variable was
set.
- Removed the read_set and write_set bitmap objects from the handler class
- Removed all column bit handling functions from the handler class.
(Now one instead uses the normal bitmap functions in my_bitmap.c instead
of handler dedicated bitmap functions)
- field->query_id is removed. One should instead instead check
table->read_set and table->write_set if a field is used in the query.
- handler::extra(HA_EXTRA_RETRIVE_ALL_COLS) and
handler::extra(HA_EXTRA_RETRIEVE_PRIMARY_KEY) are removed. One should now
instead use table->read_set to check for which columns to retrieve.
- If a handler needs to call Field->val() or Field->store() on columns
that are not used in the query, one should install a temporary
all-columns-used map while doing so. For this, we provide the following
functions:
my_bitmap_map *old_map= dbug_tmp_use_all_columns(table, table->read_set);
field->val();
dbug_tmp_restore_column_map(table->read_set, old_map);
and similar for the write map:
my_bitmap_map *old_map= dbug_tmp_use_all_columns(table, table->write_set);
field->val();
dbug_tmp_restore_column_map(table->write_set, old_map);
If this is not done, you will sooner or later hit a DBUG_ASSERT
in the field store() / val() functions.
(For not DBUG binaries, the dbug_tmp_restore_column_map() and
dbug_tmp_restore_column_map() are inline dummy functions and should
be optimized away be the compiler).
- If one needs to temporary set the column map for all binaries (and not
just to avoid the DBUG_ASSERT() in the Field::store() / Field::val()
methods) one should use the functions tmp_use_all_columns() and
tmp_restore_column_map() instead of the above dbug_ variants.
- All 'status' fields in the handler base class (like records,
data_file_length etc) are now stored in a 'stats' struct. This makes
it easier to know what status variables are provided by the base
handler. This requires some trivial variable names in the extra()
function.
- New virtual function handler::records(). This is called to optimize
COUNT(*) if (handler::table_flags() & HA_HAS_RECORDS()) is true.
(stats.records is not supposed to be an exact value. It's only has to
be 'reasonable enough' for the optimizer to be able to choose a good
optimization path).
- Non virtual handler::init() function added for caching of virtual
constants from engine.
- Removed has_transactions() virtual method. Now one should instead return
HA_NO_TRANSACTIONS in table_flags() if the table handler DOES NOT support
transactions.
- The 'xxxx_create_handler()' function now has a MEM_ROOT_root argument
that is to be used with 'new handler_name()' to allocate the handler
in the right area. The xxxx_create_handler() function is also
responsible for any initialization of the object before returning.
For example, one should change:
static handler *myisam_create_handler(TABLE_SHARE *table)
{
return new ha_myisam(table);
}
->
static handler *myisam_create_handler(TABLE_SHARE *table, MEM_ROOT *mem_root)
{
return new (mem_root) ha_myisam(table);
}
- New optional virtual function: use_hidden_primary_key().
This is called in case of an update/delete when
(table_flags() and HA_PRIMARY_KEY_REQUIRED_FOR_DELETE) is defined
but we don't have a primary key. This allows the handler to take precisions
in remembering any hidden primary key to able to update/delete any
found row. The default handler marks all columns to be read.
- handler::table_flags() now returns a ulonglong (to allow for more flags).
- New/changed table_flags()
- HA_HAS_RECORDS Set if ::records() is supported
- HA_NO_TRANSACTIONS Set if engine doesn't support transactions
- HA_PRIMARY_KEY_REQUIRED_FOR_DELETE
Set if we should mark all primary key columns for
read when reading rows as part of a DELETE
statement. If there is no primary key,
all columns are marked for read.
- HA_PARTIAL_COLUMN_READ Set if engine will not read all columns in some
cases (based on table->read_set)
- HA_PRIMARY_KEY_ALLOW_RANDOM_ACCESS
Renamed to HA_PRIMARY_KEY_REQUIRED_FOR_POSITION.
- HA_DUPP_POS Renamed to HA_DUPLICATE_POS
- HA_REQUIRES_KEY_COLUMNS_FOR_DELETE
Set this if we should mark ALL key columns for
read when when reading rows as part of a DELETE
statement. In case of an update we will mark
all keys for read for which key part changed
value.
- HA_STATS_RECORDS_IS_EXACT
Set this if stats.records is exact.
(This saves us some extra records() calls
when optimizing COUNT(*))
- Removed table_flags()
- HA_NOT_EXACT_COUNT Now one should instead use HA_HAS_RECORDS if
handler::records() gives an exact count() and
HA_STATS_RECORDS_IS_EXACT if stats.records is exact.
- HA_READ_RND_SAME Removed (no one supported this one)
- Removed not needed functions ha_retrieve_all_cols() and ha_retrieve_all_pk()
- Renamed handler::dupp_pos to handler::dup_pos
- Removed not used variable handler::sortkey
Upper level handler changes:
- ha_reset() now does some overall checks and calls ::reset()
- ha_table_flags() added. This is a cached version of table_flags(). The
cache is updated on engine creation time and updated on open.
MySQL level changes (not obvious from the above):
- DBUG_ASSERT() added to check that column usage matches what is set
in the column usage bit maps. (This found a LOT of bugs in current
column marking code).
- In 5.1 before, all used columns was marked in read_set and only updated
columns was marked in write_set. Now we only mark columns for which we
need a value in read_set.
- Column bitmaps are created in open_binary_frm() and open_table_from_share().
(Before this was in table.cc)
- handler::table_flags() calls are replaced with handler::ha_table_flags()
- For calling field->val() you must have the corresponding bit set in
table->read_set. For calling field->store() you must have the
corresponding bit set in table->write_set. (There are asserts in
all store()/val() functions to catch wrong usage)
- thd->set_query_id is renamed to thd->mark_used_columns and instead
of setting this to an integer value, this has now the values:
MARK_COLUMNS_NONE, MARK_COLUMNS_READ, MARK_COLUMNS_WRITE
Changed also all variables named 'set_query_id' to mark_used_columns.
- In filesort() we now inform the handler of exactly which columns are needed
doing the sort and choosing the rows.
- The TABLE_SHARE object has a 'all_set' column bitmap one can use
when one needs a column bitmap with all columns set.
(This is used for table->use_all_columns() and other places)
- The TABLE object has 3 column bitmaps:
- def_read_set Default bitmap for columns to be read
- def_write_set Default bitmap for columns to be written
- tmp_set Can be used as a temporary bitmap when needed.
The table object has also two pointer to bitmaps read_set and write_set
that the handler should use to find out which columns are used in which way.
- count() optimization now calls handler::records() instead of using
handler->stats.records (if (table_flags() & HA_HAS_RECORDS) is true).
- Added extra argument to Item::walk() to indicate if we should also
traverse sub queries.
- Added TABLE parameter to cp_buffer_from_ref()
- Don't close tables created with CREATE ... SELECT but keep them in
the table cache. (Faster usage of newly created tables).
New interfaces:
- table->clear_column_bitmaps() to initialize the bitmaps for tables
at start of new statements.
- table->column_bitmaps_set() to set up new column bitmaps and signal
the handler about this.
- table->column_bitmaps_set_no_signal() for some few cases where we need
to setup new column bitmaps but don't signal the handler (as the handler
has already been signaled about these before). Used for the momement
only in opt_range.cc when doing ROR scans.
- table->use_all_columns() to install a bitmap where all columns are marked
as use in the read and the write set.
- table->default_column_bitmaps() to install the normal read and write
column bitmaps, but not signaling the handler about this.
This is mainly used when creating TABLE instances.
- table->mark_columns_needed_for_delete(),
table->mark_columns_needed_for_delete() and
table->mark_columns_needed_for_insert() to allow us to put additional
columns in column usage maps if handler so requires.
(The handler indicates what it neads in handler->table_flags())
- table->prepare_for_position() to allow us to tell handler that it
needs to read primary key parts to be able to store them in
future table->position() calls.
(This replaces the table->file->ha_retrieve_all_pk function)
- table->mark_auto_increment_column() to tell handler are going to update
columns part of any auto_increment key.
- table->mark_columns_used_by_index() to mark all columns that is part of
an index. It will also send extra(HA_EXTRA_KEYREAD) to handler to allow
it to quickly know that it only needs to read colums that are part
of the key. (The handler can also use the column map for detecting this,
but simpler/faster handler can just monitor the extra() call).
- table->mark_columns_used_by_index_no_reset() to in addition to other columns,
also mark all columns that is used by the given key.
- table->restore_column_maps_after_mark_index() to restore to default
column maps after a call to table->mark_columns_used_by_index().
- New item function register_field_in_read_map(), for marking used columns
in table->read_map. Used by filesort() to mark all used columns
- Maintain in TABLE->merge_keys set of all keys that are used in query.
(Simplices some optimization loops)
- Maintain Field->part_of_key_not_clustered which is like Field->part_of_key
but the field in the clustered key is not assumed to be part of all index.
(used in opt_range.cc for faster loops)
- dbug_tmp_use_all_columns(), dbug_tmp_restore_column_map()
tmp_use_all_columns() and tmp_restore_column_map() functions to temporally
mark all columns as usable. The 'dbug_' version is primarily intended
inside a handler when it wants to just call Field:store() & Field::val()
functions, but don't need the column maps set for any other usage.
(ie:: bitmap_is_set() is never called)
- We can't use compare_records() to skip updates for handlers that returns
a partial column set and the read_set doesn't cover all columns in the
write set. The reason for this is that if we have a column marked only for
write we can't in the MySQL level know if the value changed or not.
The reason this worked before was that MySQL marked all to be written
columns as also to be read. The new 'optimal' bitmaps exposed this 'hidden
bug'.
- open_table_from_share() does not anymore setup temporary MEM_ROOT
object as a thread specific variable for the handler. Instead we
send the to-be-used MEMROOT to get_new_handler().
(Simpler, faster code)
Bugs fixed:
- Column marking was not done correctly in a lot of cases.
(ALTER TABLE, when using triggers, auto_increment fields etc)
(Could potentially result in wrong values inserted in table handlers
relying on that the old column maps or field->set_query_id was correct)
Especially when it comes to triggers, there may be cases where the
old code would cause lost/wrong values for NDB and/or InnoDB tables.
- Split thd->options flag OPTION_STATUS_NO_TRANS_UPDATE to two flags:
OPTION_STATUS_NO_TRANS_UPDATE and OPTION_KEEP_LOG.
This allowed me to remove some wrong warnings about:
"Some non-transactional changed tables couldn't be rolled back"
- Fixed handling of INSERT .. SELECT and CREATE ... SELECT that wrongly reset
(thd->options & OPTION_STATUS_NO_TRANS_UPDATE) which caused us to loose
some warnings about
"Some non-transactional changed tables couldn't be rolled back")
- Fixed use of uninitialized memory in ha_ndbcluster.cc::delete_table()
which could cause delete_table to report random failures.
- Fixed core dumps for some tests when running with --debug
- Added missing FN_LIBCHAR in mysql_rm_tmp_tables()
(This has probably caused us to not properly remove temporary files after
crash)
- slow_logs was not properly initialized, which could maybe cause
extra/lost entries in slow log.
- If we get an duplicate row on insert, change column map to read and
write all columns while retrying the operation. This is required by
the definition of REPLACE and also ensures that fields that are only
part of UPDATE are properly handled. This fixed a bug in NDB and
REPLACE where REPLACE wrongly copied some column values from the replaced
row.
- For table handler that doesn't support NULL in keys, we would give an error
when creating a primary key with NULL fields, even after the fields has been
automaticly converted to NOT NULL.
- Creating a primary key on a SPATIAL key, would fail if field was not
declared as NOT NULL.
Cleanups:
- Removed not used condition argument to setup_tables
- Removed not needed item function reset_query_id_processor().
- Field->add_index is removed. Now this is instead maintained in
(field->flags & FIELD_IN_ADD_INDEX)
- Field->fieldnr is removed (use field->field_index instead)
- New argument to filesort() to indicate that it should return a set of
row pointers (not used columns). This allowed me to remove some references
to sql_command in filesort and should also enable us to return column
results in some cases where we couldn't before.
- Changed column bitmap handling in opt_range.cc to be aligned with TABLE
bitmap, which allowed me to use bitmap functions instead of looping over
all fields to create some needed bitmaps. (Faster and smaller code)
- Broke up found too long lines
- Moved some variable declaration at start of function for better code
readability.
- Removed some not used arguments from functions.
(setup_fields(), mysql_prepare_insert_check_table())
- setup_fields() now takes an enum instead of an int for marking columns
usage.
- For internal temporary tables, use handler::write_row(),
handler::delete_row() and handler::update_row() instead of
handler::ha_xxxx() for faster execution.
- Changed some constants to enum's and define's.
- Using separate column read and write sets allows for easier checking
of timestamp field was set by statement.
- Remove calls to free_io_cache() as this is now done automaticly in ha_reset()
- Don't build table->normalized_path as this is now identical to table->path
(after bar's fixes to convert filenames)
- Fixed some missed DBUG_PRINT(.."%lx") to use "0x%lx" to make it easier to
do comparision with the 'convert-dbug-for-diff' tool.
Things left to do in 5.1:
- We wrongly log failed CREATE TABLE ... SELECT in some cases when using
row based logging (as shown by testcase binlog_row_mix_innodb_myisam.result)
Mats has promised to look into this.
- Test that my fix for CREATE TABLE ... SELECT is indeed correct.
(I added several test cases for this, but in this case it's better that
someone else also tests this throughly).
Lars has promosed to do this.
2006-06-04 17:52:22 +02:00
|
|
|
bitmap_set_all(table->write_set);
|
2005-03-16 02:32:47 +01:00
|
|
|
table->timestamp_field_type= TIMESTAMP_NO_AUTO_SET;
|
|
|
|
/*
|
|
|
|
Let us also prepare SET clause, altough it is probably empty
|
|
|
|
in this case.
|
|
|
|
*/
|
This changeset is largely a handler cleanup changeset (WL#3281), but includes fixes and cleanups that was found necessary while testing the handler changes
Changes that requires code changes in other code of other storage engines.
(Note that all changes are very straightforward and one should find all issues
by compiling a --debug build and fixing all compiler errors and all
asserts in field.cc while running the test suite),
- New optional handler function introduced: reset()
This is called after every DML statement to make it easy for a handler to
statement specific cleanups.
(The only case it's not called is if force the file to be closed)
- handler::extra(HA_EXTRA_RESET) is removed. Code that was there before
should be moved to handler::reset()
- table->read_set contains a bitmap over all columns that are needed
in the query. read_row() and similar functions only needs to read these
columns
- table->write_set contains a bitmap over all columns that will be updated
in the query. write_row() and update_row() only needs to update these
columns.
The above bitmaps should now be up to date in all context
(including ALTER TABLE, filesort()).
The handler is informed of any changes to the bitmap after
fix_fields() by calling the virtual function
handler::column_bitmaps_signal(). If the handler does caching of
these bitmaps (instead of using table->read_set, table->write_set),
it should redo the caching in this code. as the signal() may be sent
several times, it's probably best to set a variable in the signal
and redo the caching on read_row() / write_row() if the variable was
set.
- Removed the read_set and write_set bitmap objects from the handler class
- Removed all column bit handling functions from the handler class.
(Now one instead uses the normal bitmap functions in my_bitmap.c instead
of handler dedicated bitmap functions)
- field->query_id is removed. One should instead instead check
table->read_set and table->write_set if a field is used in the query.
- handler::extra(HA_EXTRA_RETRIVE_ALL_COLS) and
handler::extra(HA_EXTRA_RETRIEVE_PRIMARY_KEY) are removed. One should now
instead use table->read_set to check for which columns to retrieve.
- If a handler needs to call Field->val() or Field->store() on columns
that are not used in the query, one should install a temporary
all-columns-used map while doing so. For this, we provide the following
functions:
my_bitmap_map *old_map= dbug_tmp_use_all_columns(table, table->read_set);
field->val();
dbug_tmp_restore_column_map(table->read_set, old_map);
and similar for the write map:
my_bitmap_map *old_map= dbug_tmp_use_all_columns(table, table->write_set);
field->val();
dbug_tmp_restore_column_map(table->write_set, old_map);
If this is not done, you will sooner or later hit a DBUG_ASSERT
in the field store() / val() functions.
(For not DBUG binaries, the dbug_tmp_restore_column_map() and
dbug_tmp_restore_column_map() are inline dummy functions and should
be optimized away be the compiler).
- If one needs to temporary set the column map for all binaries (and not
just to avoid the DBUG_ASSERT() in the Field::store() / Field::val()
methods) one should use the functions tmp_use_all_columns() and
tmp_restore_column_map() instead of the above dbug_ variants.
- All 'status' fields in the handler base class (like records,
data_file_length etc) are now stored in a 'stats' struct. This makes
it easier to know what status variables are provided by the base
handler. This requires some trivial variable names in the extra()
function.
- New virtual function handler::records(). This is called to optimize
COUNT(*) if (handler::table_flags() & HA_HAS_RECORDS()) is true.
(stats.records is not supposed to be an exact value. It's only has to
be 'reasonable enough' for the optimizer to be able to choose a good
optimization path).
- Non virtual handler::init() function added for caching of virtual
constants from engine.
- Removed has_transactions() virtual method. Now one should instead return
HA_NO_TRANSACTIONS in table_flags() if the table handler DOES NOT support
transactions.
- The 'xxxx_create_handler()' function now has a MEM_ROOT_root argument
that is to be used with 'new handler_name()' to allocate the handler
in the right area. The xxxx_create_handler() function is also
responsible for any initialization of the object before returning.
For example, one should change:
static handler *myisam_create_handler(TABLE_SHARE *table)
{
return new ha_myisam(table);
}
->
static handler *myisam_create_handler(TABLE_SHARE *table, MEM_ROOT *mem_root)
{
return new (mem_root) ha_myisam(table);
}
- New optional virtual function: use_hidden_primary_key().
This is called in case of an update/delete when
(table_flags() and HA_PRIMARY_KEY_REQUIRED_FOR_DELETE) is defined
but we don't have a primary key. This allows the handler to take precisions
in remembering any hidden primary key to able to update/delete any
found row. The default handler marks all columns to be read.
- handler::table_flags() now returns a ulonglong (to allow for more flags).
- New/changed table_flags()
- HA_HAS_RECORDS Set if ::records() is supported
- HA_NO_TRANSACTIONS Set if engine doesn't support transactions
- HA_PRIMARY_KEY_REQUIRED_FOR_DELETE
Set if we should mark all primary key columns for
read when reading rows as part of a DELETE
statement. If there is no primary key,
all columns are marked for read.
- HA_PARTIAL_COLUMN_READ Set if engine will not read all columns in some
cases (based on table->read_set)
- HA_PRIMARY_KEY_ALLOW_RANDOM_ACCESS
Renamed to HA_PRIMARY_KEY_REQUIRED_FOR_POSITION.
- HA_DUPP_POS Renamed to HA_DUPLICATE_POS
- HA_REQUIRES_KEY_COLUMNS_FOR_DELETE
Set this if we should mark ALL key columns for
read when when reading rows as part of a DELETE
statement. In case of an update we will mark
all keys for read for which key part changed
value.
- HA_STATS_RECORDS_IS_EXACT
Set this if stats.records is exact.
(This saves us some extra records() calls
when optimizing COUNT(*))
- Removed table_flags()
- HA_NOT_EXACT_COUNT Now one should instead use HA_HAS_RECORDS if
handler::records() gives an exact count() and
HA_STATS_RECORDS_IS_EXACT if stats.records is exact.
- HA_READ_RND_SAME Removed (no one supported this one)
- Removed not needed functions ha_retrieve_all_cols() and ha_retrieve_all_pk()
- Renamed handler::dupp_pos to handler::dup_pos
- Removed not used variable handler::sortkey
Upper level handler changes:
- ha_reset() now does some overall checks and calls ::reset()
- ha_table_flags() added. This is a cached version of table_flags(). The
cache is updated on engine creation time and updated on open.
MySQL level changes (not obvious from the above):
- DBUG_ASSERT() added to check that column usage matches what is set
in the column usage bit maps. (This found a LOT of bugs in current
column marking code).
- In 5.1 before, all used columns was marked in read_set and only updated
columns was marked in write_set. Now we only mark columns for which we
need a value in read_set.
- Column bitmaps are created in open_binary_frm() and open_table_from_share().
(Before this was in table.cc)
- handler::table_flags() calls are replaced with handler::ha_table_flags()
- For calling field->val() you must have the corresponding bit set in
table->read_set. For calling field->store() you must have the
corresponding bit set in table->write_set. (There are asserts in
all store()/val() functions to catch wrong usage)
- thd->set_query_id is renamed to thd->mark_used_columns and instead
of setting this to an integer value, this has now the values:
MARK_COLUMNS_NONE, MARK_COLUMNS_READ, MARK_COLUMNS_WRITE
Changed also all variables named 'set_query_id' to mark_used_columns.
- In filesort() we now inform the handler of exactly which columns are needed
doing the sort and choosing the rows.
- The TABLE_SHARE object has a 'all_set' column bitmap one can use
when one needs a column bitmap with all columns set.
(This is used for table->use_all_columns() and other places)
- The TABLE object has 3 column bitmaps:
- def_read_set Default bitmap for columns to be read
- def_write_set Default bitmap for columns to be written
- tmp_set Can be used as a temporary bitmap when needed.
The table object has also two pointer to bitmaps read_set and write_set
that the handler should use to find out which columns are used in which way.
- count() optimization now calls handler::records() instead of using
handler->stats.records (if (table_flags() & HA_HAS_RECORDS) is true).
- Added extra argument to Item::walk() to indicate if we should also
traverse sub queries.
- Added TABLE parameter to cp_buffer_from_ref()
- Don't close tables created with CREATE ... SELECT but keep them in
the table cache. (Faster usage of newly created tables).
New interfaces:
- table->clear_column_bitmaps() to initialize the bitmaps for tables
at start of new statements.
- table->column_bitmaps_set() to set up new column bitmaps and signal
the handler about this.
- table->column_bitmaps_set_no_signal() for some few cases where we need
to setup new column bitmaps but don't signal the handler (as the handler
has already been signaled about these before). Used for the momement
only in opt_range.cc when doing ROR scans.
- table->use_all_columns() to install a bitmap where all columns are marked
as use in the read and the write set.
- table->default_column_bitmaps() to install the normal read and write
column bitmaps, but not signaling the handler about this.
This is mainly used when creating TABLE instances.
- table->mark_columns_needed_for_delete(),
table->mark_columns_needed_for_delete() and
table->mark_columns_needed_for_insert() to allow us to put additional
columns in column usage maps if handler so requires.
(The handler indicates what it neads in handler->table_flags())
- table->prepare_for_position() to allow us to tell handler that it
needs to read primary key parts to be able to store them in
future table->position() calls.
(This replaces the table->file->ha_retrieve_all_pk function)
- table->mark_auto_increment_column() to tell handler are going to update
columns part of any auto_increment key.
- table->mark_columns_used_by_index() to mark all columns that is part of
an index. It will also send extra(HA_EXTRA_KEYREAD) to handler to allow
it to quickly know that it only needs to read colums that are part
of the key. (The handler can also use the column map for detecting this,
but simpler/faster handler can just monitor the extra() call).
- table->mark_columns_used_by_index_no_reset() to in addition to other columns,
also mark all columns that is used by the given key.
- table->restore_column_maps_after_mark_index() to restore to default
column maps after a call to table->mark_columns_used_by_index().
- New item function register_field_in_read_map(), for marking used columns
in table->read_map. Used by filesort() to mark all used columns
- Maintain in TABLE->merge_keys set of all keys that are used in query.
(Simplices some optimization loops)
- Maintain Field->part_of_key_not_clustered which is like Field->part_of_key
but the field in the clustered key is not assumed to be part of all index.
(used in opt_range.cc for faster loops)
- dbug_tmp_use_all_columns(), dbug_tmp_restore_column_map()
tmp_use_all_columns() and tmp_restore_column_map() functions to temporally
mark all columns as usable. The 'dbug_' version is primarily intended
inside a handler when it wants to just call Field:store() & Field::val()
functions, but don't need the column maps set for any other usage.
(ie:: bitmap_is_set() is never called)
- We can't use compare_records() to skip updates for handlers that returns
a partial column set and the read_set doesn't cover all columns in the
write set. The reason for this is that if we have a column marked only for
write we can't in the MySQL level know if the value changed or not.
The reason this worked before was that MySQL marked all to be written
columns as also to be read. The new 'optimal' bitmaps exposed this 'hidden
bug'.
- open_table_from_share() does not anymore setup temporary MEM_ROOT
object as a thread specific variable for the handler. Instead we
send the to-be-used MEMROOT to get_new_handler().
(Simpler, faster code)
Bugs fixed:
- Column marking was not done correctly in a lot of cases.
(ALTER TABLE, when using triggers, auto_increment fields etc)
(Could potentially result in wrong values inserted in table handlers
relying on that the old column maps or field->set_query_id was correct)
Especially when it comes to triggers, there may be cases where the
old code would cause lost/wrong values for NDB and/or InnoDB tables.
- Split thd->options flag OPTION_STATUS_NO_TRANS_UPDATE to two flags:
OPTION_STATUS_NO_TRANS_UPDATE and OPTION_KEEP_LOG.
This allowed me to remove some wrong warnings about:
"Some non-transactional changed tables couldn't be rolled back"
- Fixed handling of INSERT .. SELECT and CREATE ... SELECT that wrongly reset
(thd->options & OPTION_STATUS_NO_TRANS_UPDATE) which caused us to loose
some warnings about
"Some non-transactional changed tables couldn't be rolled back")
- Fixed use of uninitialized memory in ha_ndbcluster.cc::delete_table()
which could cause delete_table to report random failures.
- Fixed core dumps for some tests when running with --debug
- Added missing FN_LIBCHAR in mysql_rm_tmp_tables()
(This has probably caused us to not properly remove temporary files after
crash)
- slow_logs was not properly initialized, which could maybe cause
extra/lost entries in slow log.
- If we get an duplicate row on insert, change column map to read and
write all columns while retrying the operation. This is required by
the definition of REPLACE and also ensures that fields that are only
part of UPDATE are properly handled. This fixed a bug in NDB and
REPLACE where REPLACE wrongly copied some column values from the replaced
row.
- For table handler that doesn't support NULL in keys, we would give an error
when creating a primary key with NULL fields, even after the fields has been
automaticly converted to NOT NULL.
- Creating a primary key on a SPATIAL key, would fail if field was not
declared as NOT NULL.
Cleanups:
- Removed not used condition argument to setup_tables
- Removed not needed item function reset_query_id_processor().
- Field->add_index is removed. Now this is instead maintained in
(field->flags & FIELD_IN_ADD_INDEX)
- Field->fieldnr is removed (use field->field_index instead)
- New argument to filesort() to indicate that it should return a set of
row pointers (not used columns). This allowed me to remove some references
to sql_command in filesort and should also enable us to return column
results in some cases where we couldn't before.
- Changed column bitmap handling in opt_range.cc to be aligned with TABLE
bitmap, which allowed me to use bitmap functions instead of looping over
all fields to create some needed bitmaps. (Faster and smaller code)
- Broke up found too long lines
- Moved some variable declaration at start of function for better code
readability.
- Removed some not used arguments from functions.
(setup_fields(), mysql_prepare_insert_check_table())
- setup_fields() now takes an enum instead of an int for marking columns
usage.
- For internal temporary tables, use handler::write_row(),
handler::delete_row() and handler::update_row() instead of
handler::ha_xxxx() for faster execution.
- Changed some constants to enum's and define's.
- Using separate column read and write sets allows for easier checking
of timestamp field was set by statement.
- Remove calls to free_io_cache() as this is now done automaticly in ha_reset()
- Don't build table->normalized_path as this is now identical to table->path
(after bar's fixes to convert filenames)
- Fixed some missed DBUG_PRINT(.."%lx") to use "0x%lx" to make it easier to
do comparision with the 'convert-dbug-for-diff' tool.
Things left to do in 5.1:
- We wrongly log failed CREATE TABLE ... SELECT in some cases when using
row based logging (as shown by testcase binlog_row_mix_innodb_myisam.result)
Mats has promised to look into this.
- Test that my fix for CREATE TABLE ... SELECT is indeed correct.
(I added several test cases for this, but in this case it's better that
someone else also tests this throughly).
Lars has promosed to do this.
2006-06-04 17:52:22 +02:00
|
|
|
if (setup_fields(thd, 0, set_fields, MARK_COLUMNS_WRITE, 0, 0) ||
|
|
|
|
setup_fields(thd, 0, set_values, MARK_COLUMNS_READ, 0, 0))
|
2005-03-16 02:32:47 +01:00
|
|
|
DBUG_RETURN(TRUE);
|
2000-07-31 21:29:14 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{ // Part field list
|
2004-07-16 00:15:55 +02:00
|
|
|
/* TODO: use this conds for 'WITH CHECK OPTIONS' */
|
This changeset is largely a handler cleanup changeset (WL#3281), but includes fixes and cleanups that was found necessary while testing the handler changes
Changes that requires code changes in other code of other storage engines.
(Note that all changes are very straightforward and one should find all issues
by compiling a --debug build and fixing all compiler errors and all
asserts in field.cc while running the test suite),
- New optional handler function introduced: reset()
This is called after every DML statement to make it easy for a handler to
statement specific cleanups.
(The only case it's not called is if force the file to be closed)
- handler::extra(HA_EXTRA_RESET) is removed. Code that was there before
should be moved to handler::reset()
- table->read_set contains a bitmap over all columns that are needed
in the query. read_row() and similar functions only needs to read these
columns
- table->write_set contains a bitmap over all columns that will be updated
in the query. write_row() and update_row() only needs to update these
columns.
The above bitmaps should now be up to date in all context
(including ALTER TABLE, filesort()).
The handler is informed of any changes to the bitmap after
fix_fields() by calling the virtual function
handler::column_bitmaps_signal(). If the handler does caching of
these bitmaps (instead of using table->read_set, table->write_set),
it should redo the caching in this code. as the signal() may be sent
several times, it's probably best to set a variable in the signal
and redo the caching on read_row() / write_row() if the variable was
set.
- Removed the read_set and write_set bitmap objects from the handler class
- Removed all column bit handling functions from the handler class.
(Now one instead uses the normal bitmap functions in my_bitmap.c instead
of handler dedicated bitmap functions)
- field->query_id is removed. One should instead instead check
table->read_set and table->write_set if a field is used in the query.
- handler::extra(HA_EXTRA_RETRIVE_ALL_COLS) and
handler::extra(HA_EXTRA_RETRIEVE_PRIMARY_KEY) are removed. One should now
instead use table->read_set to check for which columns to retrieve.
- If a handler needs to call Field->val() or Field->store() on columns
that are not used in the query, one should install a temporary
all-columns-used map while doing so. For this, we provide the following
functions:
my_bitmap_map *old_map= dbug_tmp_use_all_columns(table, table->read_set);
field->val();
dbug_tmp_restore_column_map(table->read_set, old_map);
and similar for the write map:
my_bitmap_map *old_map= dbug_tmp_use_all_columns(table, table->write_set);
field->val();
dbug_tmp_restore_column_map(table->write_set, old_map);
If this is not done, you will sooner or later hit a DBUG_ASSERT
in the field store() / val() functions.
(For not DBUG binaries, the dbug_tmp_restore_column_map() and
dbug_tmp_restore_column_map() are inline dummy functions and should
be optimized away be the compiler).
- If one needs to temporary set the column map for all binaries (and not
just to avoid the DBUG_ASSERT() in the Field::store() / Field::val()
methods) one should use the functions tmp_use_all_columns() and
tmp_restore_column_map() instead of the above dbug_ variants.
- All 'status' fields in the handler base class (like records,
data_file_length etc) are now stored in a 'stats' struct. This makes
it easier to know what status variables are provided by the base
handler. This requires some trivial variable names in the extra()
function.
- New virtual function handler::records(). This is called to optimize
COUNT(*) if (handler::table_flags() & HA_HAS_RECORDS()) is true.
(stats.records is not supposed to be an exact value. It's only has to
be 'reasonable enough' for the optimizer to be able to choose a good
optimization path).
- Non virtual handler::init() function added for caching of virtual
constants from engine.
- Removed has_transactions() virtual method. Now one should instead return
HA_NO_TRANSACTIONS in table_flags() if the table handler DOES NOT support
transactions.
- The 'xxxx_create_handler()' function now has a MEM_ROOT_root argument
that is to be used with 'new handler_name()' to allocate the handler
in the right area. The xxxx_create_handler() function is also
responsible for any initialization of the object before returning.
For example, one should change:
static handler *myisam_create_handler(TABLE_SHARE *table)
{
return new ha_myisam(table);
}
->
static handler *myisam_create_handler(TABLE_SHARE *table, MEM_ROOT *mem_root)
{
return new (mem_root) ha_myisam(table);
}
- New optional virtual function: use_hidden_primary_key().
This is called in case of an update/delete when
(table_flags() and HA_PRIMARY_KEY_REQUIRED_FOR_DELETE) is defined
but we don't have a primary key. This allows the handler to take precisions
in remembering any hidden primary key to able to update/delete any
found row. The default handler marks all columns to be read.
- handler::table_flags() now returns a ulonglong (to allow for more flags).
- New/changed table_flags()
- HA_HAS_RECORDS Set if ::records() is supported
- HA_NO_TRANSACTIONS Set if engine doesn't support transactions
- HA_PRIMARY_KEY_REQUIRED_FOR_DELETE
Set if we should mark all primary key columns for
read when reading rows as part of a DELETE
statement. If there is no primary key,
all columns are marked for read.
- HA_PARTIAL_COLUMN_READ Set if engine will not read all columns in some
cases (based on table->read_set)
- HA_PRIMARY_KEY_ALLOW_RANDOM_ACCESS
Renamed to HA_PRIMARY_KEY_REQUIRED_FOR_POSITION.
- HA_DUPP_POS Renamed to HA_DUPLICATE_POS
- HA_REQUIRES_KEY_COLUMNS_FOR_DELETE
Set this if we should mark ALL key columns for
read when when reading rows as part of a DELETE
statement. In case of an update we will mark
all keys for read for which key part changed
value.
- HA_STATS_RECORDS_IS_EXACT
Set this if stats.records is exact.
(This saves us some extra records() calls
when optimizing COUNT(*))
- Removed table_flags()
- HA_NOT_EXACT_COUNT Now one should instead use HA_HAS_RECORDS if
handler::records() gives an exact count() and
HA_STATS_RECORDS_IS_EXACT if stats.records is exact.
- HA_READ_RND_SAME Removed (no one supported this one)
- Removed not needed functions ha_retrieve_all_cols() and ha_retrieve_all_pk()
- Renamed handler::dupp_pos to handler::dup_pos
- Removed not used variable handler::sortkey
Upper level handler changes:
- ha_reset() now does some overall checks and calls ::reset()
- ha_table_flags() added. This is a cached version of table_flags(). The
cache is updated on engine creation time and updated on open.
MySQL level changes (not obvious from the above):
- DBUG_ASSERT() added to check that column usage matches what is set
in the column usage bit maps. (This found a LOT of bugs in current
column marking code).
- In 5.1 before, all used columns was marked in read_set and only updated
columns was marked in write_set. Now we only mark columns for which we
need a value in read_set.
- Column bitmaps are created in open_binary_frm() and open_table_from_share().
(Before this was in table.cc)
- handler::table_flags() calls are replaced with handler::ha_table_flags()
- For calling field->val() you must have the corresponding bit set in
table->read_set. For calling field->store() you must have the
corresponding bit set in table->write_set. (There are asserts in
all store()/val() functions to catch wrong usage)
- thd->set_query_id is renamed to thd->mark_used_columns and instead
of setting this to an integer value, this has now the values:
MARK_COLUMNS_NONE, MARK_COLUMNS_READ, MARK_COLUMNS_WRITE
Changed also all variables named 'set_query_id' to mark_used_columns.
- In filesort() we now inform the handler of exactly which columns are needed
doing the sort and choosing the rows.
- The TABLE_SHARE object has a 'all_set' column bitmap one can use
when one needs a column bitmap with all columns set.
(This is used for table->use_all_columns() and other places)
- The TABLE object has 3 column bitmaps:
- def_read_set Default bitmap for columns to be read
- def_write_set Default bitmap for columns to be written
- tmp_set Can be used as a temporary bitmap when needed.
The table object has also two pointer to bitmaps read_set and write_set
that the handler should use to find out which columns are used in which way.
- count() optimization now calls handler::records() instead of using
handler->stats.records (if (table_flags() & HA_HAS_RECORDS) is true).
- Added extra argument to Item::walk() to indicate if we should also
traverse sub queries.
- Added TABLE parameter to cp_buffer_from_ref()
- Don't close tables created with CREATE ... SELECT but keep them in
the table cache. (Faster usage of newly created tables).
New interfaces:
- table->clear_column_bitmaps() to initialize the bitmaps for tables
at start of new statements.
- table->column_bitmaps_set() to set up new column bitmaps and signal
the handler about this.
- table->column_bitmaps_set_no_signal() for some few cases where we need
to setup new column bitmaps but don't signal the handler (as the handler
has already been signaled about these before). Used for the momement
only in opt_range.cc when doing ROR scans.
- table->use_all_columns() to install a bitmap where all columns are marked
as use in the read and the write set.
- table->default_column_bitmaps() to install the normal read and write
column bitmaps, but not signaling the handler about this.
This is mainly used when creating TABLE instances.
- table->mark_columns_needed_for_delete(),
table->mark_columns_needed_for_delete() and
table->mark_columns_needed_for_insert() to allow us to put additional
columns in column usage maps if handler so requires.
(The handler indicates what it neads in handler->table_flags())
- table->prepare_for_position() to allow us to tell handler that it
needs to read primary key parts to be able to store them in
future table->position() calls.
(This replaces the table->file->ha_retrieve_all_pk function)
- table->mark_auto_increment_column() to tell handler are going to update
columns part of any auto_increment key.
- table->mark_columns_used_by_index() to mark all columns that is part of
an index. It will also send extra(HA_EXTRA_KEYREAD) to handler to allow
it to quickly know that it only needs to read colums that are part
of the key. (The handler can also use the column map for detecting this,
but simpler/faster handler can just monitor the extra() call).
- table->mark_columns_used_by_index_no_reset() to in addition to other columns,
also mark all columns that is used by the given key.
- table->restore_column_maps_after_mark_index() to restore to default
column maps after a call to table->mark_columns_used_by_index().
- New item function register_field_in_read_map(), for marking used columns
in table->read_map. Used by filesort() to mark all used columns
- Maintain in TABLE->merge_keys set of all keys that are used in query.
(Simplices some optimization loops)
- Maintain Field->part_of_key_not_clustered which is like Field->part_of_key
but the field in the clustered key is not assumed to be part of all index.
(used in opt_range.cc for faster loops)
- dbug_tmp_use_all_columns(), dbug_tmp_restore_column_map()
tmp_use_all_columns() and tmp_restore_column_map() functions to temporally
mark all columns as usable. The 'dbug_' version is primarily intended
inside a handler when it wants to just call Field:store() & Field::val()
functions, but don't need the column maps set for any other usage.
(ie:: bitmap_is_set() is never called)
- We can't use compare_records() to skip updates for handlers that returns
a partial column set and the read_set doesn't cover all columns in the
write set. The reason for this is that if we have a column marked only for
write we can't in the MySQL level know if the value changed or not.
The reason this worked before was that MySQL marked all to be written
columns as also to be read. The new 'optimal' bitmaps exposed this 'hidden
bug'.
- open_table_from_share() does not anymore setup temporary MEM_ROOT
object as a thread specific variable for the handler. Instead we
send the to-be-used MEMROOT to get_new_handler().
(Simpler, faster code)
Bugs fixed:
- Column marking was not done correctly in a lot of cases.
(ALTER TABLE, when using triggers, auto_increment fields etc)
(Could potentially result in wrong values inserted in table handlers
relying on that the old column maps or field->set_query_id was correct)
Especially when it comes to triggers, there may be cases where the
old code would cause lost/wrong values for NDB and/or InnoDB tables.
- Split thd->options flag OPTION_STATUS_NO_TRANS_UPDATE to two flags:
OPTION_STATUS_NO_TRANS_UPDATE and OPTION_KEEP_LOG.
This allowed me to remove some wrong warnings about:
"Some non-transactional changed tables couldn't be rolled back"
- Fixed handling of INSERT .. SELECT and CREATE ... SELECT that wrongly reset
(thd->options & OPTION_STATUS_NO_TRANS_UPDATE) which caused us to loose
some warnings about
"Some non-transactional changed tables couldn't be rolled back")
- Fixed use of uninitialized memory in ha_ndbcluster.cc::delete_table()
which could cause delete_table to report random failures.
- Fixed core dumps for some tests when running with --debug
- Added missing FN_LIBCHAR in mysql_rm_tmp_tables()
(This has probably caused us to not properly remove temporary files after
crash)
- slow_logs was not properly initialized, which could maybe cause
extra/lost entries in slow log.
- If we get an duplicate row on insert, change column map to read and
write all columns while retrying the operation. This is required by
the definition of REPLACE and also ensures that fields that are only
part of UPDATE are properly handled. This fixed a bug in NDB and
REPLACE where REPLACE wrongly copied some column values from the replaced
row.
- For table handler that doesn't support NULL in keys, we would give an error
when creating a primary key with NULL fields, even after the fields has been
automaticly converted to NOT NULL.
- Creating a primary key on a SPATIAL key, would fail if field was not
declared as NOT NULL.
Cleanups:
- Removed not used condition argument to setup_tables
- Removed not needed item function reset_query_id_processor().
- Field->add_index is removed. Now this is instead maintained in
(field->flags & FIELD_IN_ADD_INDEX)
- Field->fieldnr is removed (use field->field_index instead)
- New argument to filesort() to indicate that it should return a set of
row pointers (not used columns). This allowed me to remove some references
to sql_command in filesort and should also enable us to return column
results in some cases where we couldn't before.
- Changed column bitmap handling in opt_range.cc to be aligned with TABLE
bitmap, which allowed me to use bitmap functions instead of looping over
all fields to create some needed bitmaps. (Faster and smaller code)
- Broke up found too long lines
- Moved some variable declaration at start of function for better code
readability.
- Removed some not used arguments from functions.
(setup_fields(), mysql_prepare_insert_check_table())
- setup_fields() now takes an enum instead of an int for marking columns
usage.
- For internal temporary tables, use handler::write_row(),
handler::delete_row() and handler::update_row() instead of
handler::ha_xxxx() for faster execution.
- Changed some constants to enum's and define's.
- Using separate column read and write sets allows for easier checking
of timestamp field was set by statement.
- Remove calls to free_io_cache() as this is now done automaticly in ha_reset()
- Don't build table->normalized_path as this is now identical to table->path
(after bar's fixes to convert filenames)
- Fixed some missed DBUG_PRINT(.."%lx") to use "0x%lx" to make it easier to
do comparision with the 'convert-dbug-for-diff' tool.
Things left to do in 5.1:
- We wrongly log failed CREATE TABLE ... SELECT in some cases when using
row based logging (as shown by testcase binlog_row_mix_innodb_myisam.result)
Mats has promised to look into this.
- Test that my fix for CREATE TABLE ... SELECT is indeed correct.
(I added several test cases for this, but in this case it's better that
someone else also tests this throughly).
Lars has promosed to do this.
2006-06-04 17:52:22 +02:00
|
|
|
if (setup_fields(thd, 0, fields_vars, MARK_COLUMNS_WRITE, 0, 0) ||
|
|
|
|
setup_fields(thd, 0, set_fields, MARK_COLUMNS_WRITE, 0, 0) ||
|
2005-07-01 06:05:42 +02:00
|
|
|
check_that_all_fields_are_given_values(thd, table, table_list))
|
2004-10-20 03:04:37 +02:00
|
|
|
DBUG_RETURN(TRUE);
|
2005-03-16 02:32:47 +01:00
|
|
|
/*
|
|
|
|
Check whenever TIMESTAMP field with auto-set feature specified
|
|
|
|
explicitly.
|
|
|
|
*/
|
This changeset is largely a handler cleanup changeset (WL#3281), but includes fixes and cleanups that was found necessary while testing the handler changes
Changes that requires code changes in other code of other storage engines.
(Note that all changes are very straightforward and one should find all issues
by compiling a --debug build and fixing all compiler errors and all
asserts in field.cc while running the test suite),
- New optional handler function introduced: reset()
This is called after every DML statement to make it easy for a handler to
statement specific cleanups.
(The only case it's not called is if force the file to be closed)
- handler::extra(HA_EXTRA_RESET) is removed. Code that was there before
should be moved to handler::reset()
- table->read_set contains a bitmap over all columns that are needed
in the query. read_row() and similar functions only needs to read these
columns
- table->write_set contains a bitmap over all columns that will be updated
in the query. write_row() and update_row() only needs to update these
columns.
The above bitmaps should now be up to date in all context
(including ALTER TABLE, filesort()).
The handler is informed of any changes to the bitmap after
fix_fields() by calling the virtual function
handler::column_bitmaps_signal(). If the handler does caching of
these bitmaps (instead of using table->read_set, table->write_set),
it should redo the caching in this code. as the signal() may be sent
several times, it's probably best to set a variable in the signal
and redo the caching on read_row() / write_row() if the variable was
set.
- Removed the read_set and write_set bitmap objects from the handler class
- Removed all column bit handling functions from the handler class.
(Now one instead uses the normal bitmap functions in my_bitmap.c instead
of handler dedicated bitmap functions)
- field->query_id is removed. One should instead instead check
table->read_set and table->write_set if a field is used in the query.
- handler::extra(HA_EXTRA_RETRIVE_ALL_COLS) and
handler::extra(HA_EXTRA_RETRIEVE_PRIMARY_KEY) are removed. One should now
instead use table->read_set to check for which columns to retrieve.
- If a handler needs to call Field->val() or Field->store() on columns
that are not used in the query, one should install a temporary
all-columns-used map while doing so. For this, we provide the following
functions:
my_bitmap_map *old_map= dbug_tmp_use_all_columns(table, table->read_set);
field->val();
dbug_tmp_restore_column_map(table->read_set, old_map);
and similar for the write map:
my_bitmap_map *old_map= dbug_tmp_use_all_columns(table, table->write_set);
field->val();
dbug_tmp_restore_column_map(table->write_set, old_map);
If this is not done, you will sooner or later hit a DBUG_ASSERT
in the field store() / val() functions.
(For not DBUG binaries, the dbug_tmp_restore_column_map() and
dbug_tmp_restore_column_map() are inline dummy functions and should
be optimized away be the compiler).
- If one needs to temporary set the column map for all binaries (and not
just to avoid the DBUG_ASSERT() in the Field::store() / Field::val()
methods) one should use the functions tmp_use_all_columns() and
tmp_restore_column_map() instead of the above dbug_ variants.
- All 'status' fields in the handler base class (like records,
data_file_length etc) are now stored in a 'stats' struct. This makes
it easier to know what status variables are provided by the base
handler. This requires some trivial variable names in the extra()
function.
- New virtual function handler::records(). This is called to optimize
COUNT(*) if (handler::table_flags() & HA_HAS_RECORDS()) is true.
(stats.records is not supposed to be an exact value. It's only has to
be 'reasonable enough' for the optimizer to be able to choose a good
optimization path).
- Non virtual handler::init() function added for caching of virtual
constants from engine.
- Removed has_transactions() virtual method. Now one should instead return
HA_NO_TRANSACTIONS in table_flags() if the table handler DOES NOT support
transactions.
- The 'xxxx_create_handler()' function now has a MEM_ROOT_root argument
that is to be used with 'new handler_name()' to allocate the handler
in the right area. The xxxx_create_handler() function is also
responsible for any initialization of the object before returning.
For example, one should change:
static handler *myisam_create_handler(TABLE_SHARE *table)
{
return new ha_myisam(table);
}
->
static handler *myisam_create_handler(TABLE_SHARE *table, MEM_ROOT *mem_root)
{
return new (mem_root) ha_myisam(table);
}
- New optional virtual function: use_hidden_primary_key().
This is called in case of an update/delete when
(table_flags() and HA_PRIMARY_KEY_REQUIRED_FOR_DELETE) is defined
but we don't have a primary key. This allows the handler to take precisions
in remembering any hidden primary key to able to update/delete any
found row. The default handler marks all columns to be read.
- handler::table_flags() now returns a ulonglong (to allow for more flags).
- New/changed table_flags()
- HA_HAS_RECORDS Set if ::records() is supported
- HA_NO_TRANSACTIONS Set if engine doesn't support transactions
- HA_PRIMARY_KEY_REQUIRED_FOR_DELETE
Set if we should mark all primary key columns for
read when reading rows as part of a DELETE
statement. If there is no primary key,
all columns are marked for read.
- HA_PARTIAL_COLUMN_READ Set if engine will not read all columns in some
cases (based on table->read_set)
- HA_PRIMARY_KEY_ALLOW_RANDOM_ACCESS
Renamed to HA_PRIMARY_KEY_REQUIRED_FOR_POSITION.
- HA_DUPP_POS Renamed to HA_DUPLICATE_POS
- HA_REQUIRES_KEY_COLUMNS_FOR_DELETE
Set this if we should mark ALL key columns for
read when when reading rows as part of a DELETE
statement. In case of an update we will mark
all keys for read for which key part changed
value.
- HA_STATS_RECORDS_IS_EXACT
Set this if stats.records is exact.
(This saves us some extra records() calls
when optimizing COUNT(*))
- Removed table_flags()
- HA_NOT_EXACT_COUNT Now one should instead use HA_HAS_RECORDS if
handler::records() gives an exact count() and
HA_STATS_RECORDS_IS_EXACT if stats.records is exact.
- HA_READ_RND_SAME Removed (no one supported this one)
- Removed not needed functions ha_retrieve_all_cols() and ha_retrieve_all_pk()
- Renamed handler::dupp_pos to handler::dup_pos
- Removed not used variable handler::sortkey
Upper level handler changes:
- ha_reset() now does some overall checks and calls ::reset()
- ha_table_flags() added. This is a cached version of table_flags(). The
cache is updated on engine creation time and updated on open.
MySQL level changes (not obvious from the above):
- DBUG_ASSERT() added to check that column usage matches what is set
in the column usage bit maps. (This found a LOT of bugs in current
column marking code).
- In 5.1 before, all used columns was marked in read_set and only updated
columns was marked in write_set. Now we only mark columns for which we
need a value in read_set.
- Column bitmaps are created in open_binary_frm() and open_table_from_share().
(Before this was in table.cc)
- handler::table_flags() calls are replaced with handler::ha_table_flags()
- For calling field->val() you must have the corresponding bit set in
table->read_set. For calling field->store() you must have the
corresponding bit set in table->write_set. (There are asserts in
all store()/val() functions to catch wrong usage)
- thd->set_query_id is renamed to thd->mark_used_columns and instead
of setting this to an integer value, this has now the values:
MARK_COLUMNS_NONE, MARK_COLUMNS_READ, MARK_COLUMNS_WRITE
Changed also all variables named 'set_query_id' to mark_used_columns.
- In filesort() we now inform the handler of exactly which columns are needed
doing the sort and choosing the rows.
- The TABLE_SHARE object has a 'all_set' column bitmap one can use
when one needs a column bitmap with all columns set.
(This is used for table->use_all_columns() and other places)
- The TABLE object has 3 column bitmaps:
- def_read_set Default bitmap for columns to be read
- def_write_set Default bitmap for columns to be written
- tmp_set Can be used as a temporary bitmap when needed.
The table object has also two pointer to bitmaps read_set and write_set
that the handler should use to find out which columns are used in which way.
- count() optimization now calls handler::records() instead of using
handler->stats.records (if (table_flags() & HA_HAS_RECORDS) is true).
- Added extra argument to Item::walk() to indicate if we should also
traverse sub queries.
- Added TABLE parameter to cp_buffer_from_ref()
- Don't close tables created with CREATE ... SELECT but keep them in
the table cache. (Faster usage of newly created tables).
New interfaces:
- table->clear_column_bitmaps() to initialize the bitmaps for tables
at start of new statements.
- table->column_bitmaps_set() to set up new column bitmaps and signal
the handler about this.
- table->column_bitmaps_set_no_signal() for some few cases where we need
to setup new column bitmaps but don't signal the handler (as the handler
has already been signaled about these before). Used for the momement
only in opt_range.cc when doing ROR scans.
- table->use_all_columns() to install a bitmap where all columns are marked
as use in the read and the write set.
- table->default_column_bitmaps() to install the normal read and write
column bitmaps, but not signaling the handler about this.
This is mainly used when creating TABLE instances.
- table->mark_columns_needed_for_delete(),
table->mark_columns_needed_for_delete() and
table->mark_columns_needed_for_insert() to allow us to put additional
columns in column usage maps if handler so requires.
(The handler indicates what it neads in handler->table_flags())
- table->prepare_for_position() to allow us to tell handler that it
needs to read primary key parts to be able to store them in
future table->position() calls.
(This replaces the table->file->ha_retrieve_all_pk function)
- table->mark_auto_increment_column() to tell handler are going to update
columns part of any auto_increment key.
- table->mark_columns_used_by_index() to mark all columns that is part of
an index. It will also send extra(HA_EXTRA_KEYREAD) to handler to allow
it to quickly know that it only needs to read colums that are part
of the key. (The handler can also use the column map for detecting this,
but simpler/faster handler can just monitor the extra() call).
- table->mark_columns_used_by_index_no_reset() to in addition to other columns,
also mark all columns that is used by the given key.
- table->restore_column_maps_after_mark_index() to restore to default
column maps after a call to table->mark_columns_used_by_index().
- New item function register_field_in_read_map(), for marking used columns
in table->read_map. Used by filesort() to mark all used columns
- Maintain in TABLE->merge_keys set of all keys that are used in query.
(Simplices some optimization loops)
- Maintain Field->part_of_key_not_clustered which is like Field->part_of_key
but the field in the clustered key is not assumed to be part of all index.
(used in opt_range.cc for faster loops)
- dbug_tmp_use_all_columns(), dbug_tmp_restore_column_map()
tmp_use_all_columns() and tmp_restore_column_map() functions to temporally
mark all columns as usable. The 'dbug_' version is primarily intended
inside a handler when it wants to just call Field:store() & Field::val()
functions, but don't need the column maps set for any other usage.
(ie:: bitmap_is_set() is never called)
- We can't use compare_records() to skip updates for handlers that returns
a partial column set and the read_set doesn't cover all columns in the
write set. The reason for this is that if we have a column marked only for
write we can't in the MySQL level know if the value changed or not.
The reason this worked before was that MySQL marked all to be written
columns as also to be read. The new 'optimal' bitmaps exposed this 'hidden
bug'.
- open_table_from_share() does not anymore setup temporary MEM_ROOT
object as a thread specific variable for the handler. Instead we
send the to-be-used MEMROOT to get_new_handler().
(Simpler, faster code)
Bugs fixed:
- Column marking was not done correctly in a lot of cases.
(ALTER TABLE, when using triggers, auto_increment fields etc)
(Could potentially result in wrong values inserted in table handlers
relying on that the old column maps or field->set_query_id was correct)
Especially when it comes to triggers, there may be cases where the
old code would cause lost/wrong values for NDB and/or InnoDB tables.
- Split thd->options flag OPTION_STATUS_NO_TRANS_UPDATE to two flags:
OPTION_STATUS_NO_TRANS_UPDATE and OPTION_KEEP_LOG.
This allowed me to remove some wrong warnings about:
"Some non-transactional changed tables couldn't be rolled back"
- Fixed handling of INSERT .. SELECT and CREATE ... SELECT that wrongly reset
(thd->options & OPTION_STATUS_NO_TRANS_UPDATE) which caused us to loose
some warnings about
"Some non-transactional changed tables couldn't be rolled back")
- Fixed use of uninitialized memory in ha_ndbcluster.cc::delete_table()
which could cause delete_table to report random failures.
- Fixed core dumps for some tests when running with --debug
- Added missing FN_LIBCHAR in mysql_rm_tmp_tables()
(This has probably caused us to not properly remove temporary files after
crash)
- slow_logs was not properly initialized, which could maybe cause
extra/lost entries in slow log.
- If we get an duplicate row on insert, change column map to read and
write all columns while retrying the operation. This is required by
the definition of REPLACE and also ensures that fields that are only
part of UPDATE are properly handled. This fixed a bug in NDB and
REPLACE where REPLACE wrongly copied some column values from the replaced
row.
- For table handler that doesn't support NULL in keys, we would give an error
when creating a primary key with NULL fields, even after the fields has been
automaticly converted to NOT NULL.
- Creating a primary key on a SPATIAL key, would fail if field was not
declared as NOT NULL.
Cleanups:
- Removed not used condition argument to setup_tables
- Removed not needed item function reset_query_id_processor().
- Field->add_index is removed. Now this is instead maintained in
(field->flags & FIELD_IN_ADD_INDEX)
- Field->fieldnr is removed (use field->field_index instead)
- New argument to filesort() to indicate that it should return a set of
row pointers (not used columns). This allowed me to remove some references
to sql_command in filesort and should also enable us to return column
results in some cases where we couldn't before.
- Changed column bitmap handling in opt_range.cc to be aligned with TABLE
bitmap, which allowed me to use bitmap functions instead of looping over
all fields to create some needed bitmaps. (Faster and smaller code)
- Broke up found too long lines
- Moved some variable declaration at start of function for better code
readability.
- Removed some not used arguments from functions.
(setup_fields(), mysql_prepare_insert_check_table())
- setup_fields() now takes an enum instead of an int for marking columns
usage.
- For internal temporary tables, use handler::write_row(),
handler::delete_row() and handler::update_row() instead of
handler::ha_xxxx() for faster execution.
- Changed some constants to enum's and define's.
- Using separate column read and write sets allows for easier checking
of timestamp field was set by statement.
- Remove calls to free_io_cache() as this is now done automaticly in ha_reset()
- Don't build table->normalized_path as this is now identical to table->path
(after bar's fixes to convert filenames)
- Fixed some missed DBUG_PRINT(.."%lx") to use "0x%lx" to make it easier to
do comparision with the 'convert-dbug-for-diff' tool.
Things left to do in 5.1:
- We wrongly log failed CREATE TABLE ... SELECT in some cases when using
row based logging (as shown by testcase binlog_row_mix_innodb_myisam.result)
Mats has promised to look into this.
- Test that my fix for CREATE TABLE ... SELECT is indeed correct.
(I added several test cases for this, but in this case it's better that
someone else also tests this throughly).
Lars has promosed to do this.
2006-06-04 17:52:22 +02:00
|
|
|
if (table->timestamp_field)
|
|
|
|
{
|
|
|
|
if (bitmap_is_set(table->write_set,
|
|
|
|
table->timestamp_field->field_index))
|
|
|
|
table->timestamp_field_type= TIMESTAMP_NO_AUTO_SET;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
bitmap_set_bit(table->write_set,
|
|
|
|
table->timestamp_field->field_index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Fix the expressions in SET clause */
|
|
|
|
if (setup_fields(thd, 0, set_values, MARK_COLUMNS_READ, 0, 0))
|
2004-10-20 03:04:37 +02:00
|
|
|
DBUG_RETURN(TRUE);
|
2000-07-31 21:29:14 +02:00
|
|
|
}
|
|
|
|
|
2006-07-06 11:33:23 +02:00
|
|
|
table->mark_columns_needed_for_insert();
|
2006-07-01 23:51:10 +02:00
|
|
|
|
2000-07-31 21:29:14 +02:00
|
|
|
uint tot_length=0;
|
2005-03-16 02:32:47 +01:00
|
|
|
bool use_blobs= 0, use_vars= 0;
|
|
|
|
List_iterator_fast<Item> it(fields_vars);
|
|
|
|
Item *item;
|
2000-07-31 21:29:14 +02:00
|
|
|
|
2005-03-16 02:32:47 +01:00
|
|
|
while ((item= it++))
|
2000-07-31 21:29:14 +02:00
|
|
|
{
|
2005-03-16 02:32:47 +01:00
|
|
|
if (item->type() == Item::FIELD_ITEM)
|
2000-07-31 21:29:14 +02:00
|
|
|
{
|
2005-03-16 02:32:47 +01:00
|
|
|
Field *field= ((Item_field*)item)->field;
|
|
|
|
if (field->flags & BLOB_FLAG)
|
|
|
|
{
|
|
|
|
use_blobs= 1;
|
|
|
|
tot_length+= 256; // Will be extended if needed
|
|
|
|
}
|
|
|
|
else
|
|
|
|
tot_length+= field->field_length;
|
2000-07-31 21:29:14 +02:00
|
|
|
}
|
|
|
|
else
|
2005-03-16 02:32:47 +01:00
|
|
|
use_vars= 1;
|
2000-07-31 21:29:14 +02:00
|
|
|
}
|
|
|
|
if (use_blobs && !ex->line_term->length() && !field_term->length())
|
|
|
|
{
|
|
|
|
my_message(ER_BLOBS_AND_NO_TERMINATED,ER(ER_BLOBS_AND_NO_TERMINATED),
|
|
|
|
MYF(0));
|
2004-10-20 03:04:37 +02:00
|
|
|
DBUG_RETURN(TRUE);
|
2000-07-31 21:29:14 +02:00
|
|
|
}
|
2005-03-16 02:32:47 +01:00
|
|
|
if (use_vars && !field_term->length() && !enclosed->length())
|
|
|
|
{
|
|
|
|
my_error(ER_LOAD_FROM_FIXED_SIZE_ROWS_TO_VAR, MYF(0));
|
|
|
|
DBUG_RETURN(TRUE);
|
|
|
|
}
|
2000-07-31 21:29:14 +02:00
|
|
|
|
|
|
|
/* We can't give an error in the middle when using LOCAL files */
|
|
|
|
if (read_file_from_client && handle_duplicates == DUP_ERROR)
|
2004-12-31 11:04:35 +01:00
|
|
|
ignore= 1;
|
2000-07-31 21:29:14 +02:00
|
|
|
|
2002-12-16 14:33:29 +01:00
|
|
|
#ifndef EMBEDDED_LIBRARY
|
2002-02-13 20:53:26 +01:00
|
|
|
if (read_file_from_client)
|
2000-07-31 21:29:14 +02:00
|
|
|
{
|
2002-02-07 23:29:46 +01:00
|
|
|
(void)net_request_file(&thd->net,ex->file_name);
|
2000-07-31 21:29:14 +02:00
|
|
|
file = -1;
|
|
|
|
}
|
|
|
|
else
|
2002-12-16 14:33:29 +01:00
|
|
|
#endif
|
2000-07-31 21:29:14 +02:00
|
|
|
{
|
|
|
|
#ifdef DONT_ALLOW_FULL_LOAD_DATA_PATHS
|
|
|
|
ex->file_name+=dirname_length(ex->file_name);
|
|
|
|
#endif
|
2004-09-07 08:55:34 +02:00
|
|
|
if (!dirname_length(ex->file_name))
|
2000-07-31 21:29:14 +02:00
|
|
|
{
|
2005-11-23 21:45:02 +01:00
|
|
|
strxnmov(name, FN_REFLEN-1, mysql_real_data_home, tdb, NullS);
|
2004-09-07 14:28:26 +02:00
|
|
|
(void) fn_format(name, ex->file_name, name, "",
|
|
|
|
MY_RELATIVE_PATH | MY_UNPACK_FILENAME);
|
2000-07-31 21:29:14 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2004-09-07 14:28:26 +02:00
|
|
|
(void) fn_format(name, ex->file_name, mysql_real_data_home, "",
|
|
|
|
MY_RELATIVE_PATH | MY_UNPACK_FILENAME);
|
2006-04-16 03:17:32 +02:00
|
|
|
#if !defined(__WIN__) && ! defined(__NETWARE__)
|
2000-07-31 21:29:14 +02:00
|
|
|
MY_STAT stat_info;
|
2000-11-13 22:55:10 +01:00
|
|
|
if (!my_stat(name,&stat_info,MYF(MY_WME)))
|
2004-10-20 03:04:37 +02:00
|
|
|
DBUG_RETURN(TRUE);
|
2001-12-06 13:10:51 +01:00
|
|
|
|
2001-08-03 23:57:53 +02:00
|
|
|
// if we are not in slave thread, the file must be:
|
|
|
|
if (!thd->slave_thread &&
|
|
|
|
!((stat_info.st_mode & S_IROTH) == S_IROTH && // readable by others
|
2000-09-25 23:33:25 +02:00
|
|
|
(stat_info.st_mode & S_IFLNK) != S_IFLNK && // and not a symlink
|
|
|
|
((stat_info.st_mode & S_IFREG) == S_IFREG ||
|
|
|
|
(stat_info.st_mode & S_IFIFO) == S_IFIFO)))
|
2000-07-31 21:29:14 +02:00
|
|
|
{
|
2004-11-13 18:35:51 +01:00
|
|
|
my_error(ER_TEXTFILE_NOT_READABLE, MYF(0), name);
|
2004-10-20 03:04:37 +02:00
|
|
|
DBUG_RETURN(TRUE);
|
2000-07-31 21:29:14 +02:00
|
|
|
}
|
2000-09-25 23:33:25 +02:00
|
|
|
if ((stat_info.st_mode & S_IFIFO) == S_IFIFO)
|
|
|
|
is_fifo = 1;
|
2000-07-31 21:29:14 +02:00
|
|
|
#endif
|
2007-02-14 14:44:34 +01:00
|
|
|
|
|
|
|
if (opt_secure_file_priv &&
|
|
|
|
strncmp(opt_secure_file_priv, name, strlen(opt_secure_file_priv)))
|
|
|
|
{
|
|
|
|
/* Read only allowed from within dir specified by secure_file_priv */
|
|
|
|
my_error(ER_OPTION_PREVENTS_STATEMENT, MYF(0), "--secure-file-priv");
|
|
|
|
DBUG_RETURN(TRUE);
|
|
|
|
}
|
|
|
|
|
2000-07-31 21:29:14 +02:00
|
|
|
}
|
|
|
|
if ((file=my_open(name,O_RDONLY,MYF(MY_WME))) < 0)
|
2004-10-20 03:04:37 +02:00
|
|
|
DBUG_RETURN(TRUE);
|
2000-07-31 21:29:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
COPY_INFO info;
|
|
|
|
bzero((char*) &info,sizeof(info));
|
2004-12-31 11:04:35 +01:00
|
|
|
info.ignore= ignore;
|
2000-07-31 21:29:14 +02:00
|
|
|
info.handle_duplicates=handle_duplicates;
|
|
|
|
info.escape_char=escaped->length() ? (*escaped)[0] : INT_MAX;
|
|
|
|
|
2007-02-28 14:06:57 +01:00
|
|
|
READ_INFO read_info(file,tot_length,
|
|
|
|
ex->cs ? ex->cs : thd->variables.collation_database,
|
2003-03-07 09:55:55 +01:00
|
|
|
*field_term,*ex->line_start, *ex->line_term, *enclosed,
|
2000-09-25 23:33:25 +02:00
|
|
|
info.escape_char, read_file_from_client, is_fifo);
|
2000-07-31 21:29:14 +02:00
|
|
|
if (read_info.error)
|
|
|
|
{
|
|
|
|
if (file >= 0)
|
|
|
|
my_close(file,MYF(0)); // no files in net reading
|
2004-10-20 03:04:37 +02:00
|
|
|
DBUG_RETURN(TRUE); // Can't allocate buffers
|
2000-07-31 21:29:14 +02:00
|
|
|
}
|
|
|
|
|
2003-09-06 15:50:30 +02:00
|
|
|
#ifndef EMBEDDED_LIBRARY
|
2004-01-18 17:51:20 +01:00
|
|
|
if (mysql_bin_log.is_open())
|
2001-08-03 23:57:53 +02:00
|
|
|
{
|
|
|
|
lf_info.thd = thd;
|
|
|
|
lf_info.wrote_create_file = 0;
|
|
|
|
lf_info.last_pos_in_file = HA_POS_ERROR;
|
2005-01-27 22:38:56 +01:00
|
|
|
lf_info.log_delayed= transactional_table;
|
2002-07-23 17:31:22 +02:00
|
|
|
read_info.set_io_cache_arg((void*) &lf_info);
|
2001-08-03 23:57:53 +02:00
|
|
|
}
|
2003-09-06 15:50:30 +02:00
|
|
|
#endif /*!EMBEDDED_LIBRARY*/
|
|
|
|
|
2003-10-11 22:26:39 +02:00
|
|
|
thd->count_cuted_fields= CHECK_FIELD_WARN; /* calc cuted fields */
|
2000-07-31 21:29:14 +02:00
|
|
|
thd->cuted_fields=0L;
|
2003-12-14 05:39:52 +01:00
|
|
|
/* Skip lines if there is a line terminator */
|
|
|
|
if (ex->line_term->length())
|
2000-07-31 21:29:14 +02:00
|
|
|
{
|
2003-12-14 05:39:52 +01:00
|
|
|
/* ex->skip_lines needs to be preserved for logging */
|
|
|
|
while (skip_lines > 0)
|
2000-07-31 21:29:14 +02:00
|
|
|
{
|
2003-12-14 05:39:52 +01:00
|
|
|
skip_lines--;
|
2000-07-31 21:29:14 +02:00
|
|
|
if (read_info.next_line())
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2003-12-14 05:39:52 +01:00
|
|
|
|
2000-07-31 21:29:14 +02:00
|
|
|
if (!(error=test(read_info.error)))
|
|
|
|
{
|
2004-04-07 16:04:28 +02:00
|
|
|
|
2000-07-31 21:29:14 +02:00
|
|
|
table->next_number_field=table->found_next_number_field;
|
2004-12-31 11:04:35 +01:00
|
|
|
if (ignore ||
|
2000-12-24 14:19:00 +01:00
|
|
|
handle_duplicates == DUP_REPLACE)
|
|
|
|
table->file->extra(HA_EXTRA_IGNORE_DUP_KEY);
|
2006-07-06 11:33:23 +02:00
|
|
|
if (handle_duplicates == DUP_REPLACE &&
|
|
|
|
(!table->triggers ||
|
|
|
|
!table->triggers->has_delete_triggers()))
|
2006-07-01 23:51:10 +02:00
|
|
|
table->file->extra(HA_EXTRA_WRITE_CAN_REPLACE);
|
2006-03-29 12:53:00 +02:00
|
|
|
if (!thd->prelocked_mode)
|
2006-06-02 22:21:32 +02:00
|
|
|
table->file->ha_start_bulk_insert((ha_rows) 0);
|
2000-07-31 21:29:14 +02:00
|
|
|
table->copy_blobs=1;
|
2004-09-28 19:08:00 +02:00
|
|
|
|
|
|
|
thd->no_trans_update= 0;
|
2005-01-04 12:46:53 +01:00
|
|
|
thd->abort_on_warning= (!ignore &&
|
2004-09-28 19:08:00 +02:00
|
|
|
(thd->variables.sql_mode &
|
|
|
|
(MODE_STRICT_TRANS_TABLES |
|
|
|
|
MODE_STRICT_ALL_TABLES)));
|
|
|
|
|
2000-07-31 21:29:14 +02:00
|
|
|
if (!field_term->length() && !enclosed->length())
|
2005-03-16 02:32:47 +01:00
|
|
|
error= read_fixed_length(thd, info, table_list, fields_vars,
|
|
|
|
set_fields, set_values, read_info,
|
2005-01-03 22:04:52 +01:00
|
|
|
skip_lines, ignore);
|
2000-07-31 21:29:14 +02:00
|
|
|
else
|
2005-03-16 02:32:47 +01:00
|
|
|
error= read_sep_field(thd, info, table_list, fields_vars,
|
|
|
|
set_fields, set_values, read_info,
|
|
|
|
*enclosed, skip_lines, ignore);
|
2006-06-02 22:21:32 +02:00
|
|
|
if (!thd->prelocked_mode && table->file->ha_end_bulk_insert() && !error)
|
2006-02-07 11:47:04 +01:00
|
|
|
{
|
|
|
|
table->file->print_error(my_errno, MYF(0));
|
|
|
|
error= 1;
|
|
|
|
}
|
2000-12-24 14:19:00 +01:00
|
|
|
table->file->extra(HA_EXTRA_NO_IGNORE_DUP_KEY);
|
2006-07-01 23:51:10 +02:00
|
|
|
table->file->extra(HA_EXTRA_WRITE_CANNOT_REPLACE);
|
2000-07-31 21:29:14 +02:00
|
|
|
table->next_number_field=0;
|
|
|
|
}
|
2003-12-14 05:39:52 +01:00
|
|
|
if (file >= 0)
|
|
|
|
my_close(file,MYF(0));
|
2000-07-31 21:29:14 +02:00
|
|
|
free_blobs(table); /* if pack_blob was used */
|
|
|
|
table->copy_blobs=0;
|
2003-10-11 22:26:39 +02:00
|
|
|
thd->count_cuted_fields= CHECK_FIELD_IGNORE;
|
2002-11-07 03:02:37 +01:00
|
|
|
|
2003-05-26 19:48:40 +02:00
|
|
|
/*
|
|
|
|
We must invalidate the table in query cache before binlog writing and
|
|
|
|
ha_autocommit_...
|
|
|
|
*/
|
2003-05-26 18:10:43 +02:00
|
|
|
query_cache_invalidate3(thd, table_list, 0);
|
|
|
|
|
2000-07-31 21:29:14 +02:00
|
|
|
if (error)
|
2001-08-03 23:57:53 +02:00
|
|
|
{
|
2002-11-07 03:02:37 +01:00
|
|
|
if (transactional_table)
|
2001-08-28 05:43:55 +02:00
|
|
|
ha_autocommit_or_rollback(thd,error);
|
2004-07-20 00:53:24 +02:00
|
|
|
|
2004-07-13 12:54:20 +02:00
|
|
|
if (read_file_from_client)
|
|
|
|
while (!read_info.next_line())
|
|
|
|
;
|
2004-07-20 00:53:24 +02:00
|
|
|
|
2003-09-06 15:50:30 +02:00
|
|
|
#ifndef EMBEDDED_LIBRARY
|
2004-01-18 17:51:20 +01:00
|
|
|
if (mysql_bin_log.is_open())
|
2001-08-03 23:57:53 +02:00
|
|
|
{
|
2001-10-23 21:28:03 +02:00
|
|
|
{
|
2005-12-22 06:39:02 +01:00
|
|
|
/*
|
|
|
|
Make sure last block (the one which caused the error) gets
|
|
|
|
logged. This is needed because otherwise after write of (to
|
|
|
|
the binlog, not to read_info (which is a cache))
|
|
|
|
Delete_file_log_event the bad block will remain in read_info
|
|
|
|
(because pre_read is not called at the end of the last
|
|
|
|
block; remember pre_read is called whenever a new block is
|
|
|
|
read from disk). At the end of mysql_load(), the destructor
|
|
|
|
of read_info will call end_io_cache() which will flush
|
|
|
|
read_info, so we will finally have this in the binlog:
|
|
|
|
|
|
|
|
Append_block # The last successfull block
|
|
|
|
Delete_file
|
|
|
|
Append_block # The failing block
|
|
|
|
which is nonsense.
|
|
|
|
Or could also be (for a small file)
|
|
|
|
Create_file # The failing block
|
|
|
|
which is nonsense (Delete_file is not written in this case, because:
|
|
|
|
Create_file has not been written, so Delete_file is not written, then
|
|
|
|
when read_info is destroyed end_io_cache() is called which writes
|
|
|
|
Create_file.
|
|
|
|
*/
|
|
|
|
read_info.end_io_cache();
|
|
|
|
/* If the file was not empty, wrote_create_file is true */
|
|
|
|
if (lf_info.wrote_create_file)
|
2005-03-25 14:51:17 +01:00
|
|
|
{
|
2005-12-22 06:39:02 +01:00
|
|
|
if ((info.copied || info.deleted) && !transactional_table)
|
|
|
|
write_execute_load_query_log_event(thd, handle_duplicates,
|
|
|
|
ignore, transactional_table);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Delete_file_log_event d(thd, db, transactional_table);
|
|
|
|
d.flags|= LOG_EVENT_UPDATE_TABLE_MAP_VERSION_F;
|
|
|
|
mysql_bin_log.write(&d);
|
|
|
|
}
|
2005-03-25 14:51:17 +01:00
|
|
|
}
|
2001-10-23 21:28:03 +02:00
|
|
|
}
|
2001-08-03 23:57:53 +02:00
|
|
|
}
|
2003-09-06 15:50:30 +02:00
|
|
|
#endif /*!EMBEDDED_LIBRARY*/
|
2002-12-13 15:28:15 +01:00
|
|
|
error= -1; // Error on read
|
|
|
|
goto err;
|
2001-08-03 23:57:53 +02:00
|
|
|
}
|
2003-07-14 12:32:31 +02:00
|
|
|
sprintf(name, ER(ER_LOAD_INFO), (ulong) info.records, (ulong) info.deleted,
|
|
|
|
(ulong) (info.records - info.copied), (ulong) thd->cuted_fields);
|
2001-12-06 13:10:51 +01:00
|
|
|
|
2005-01-27 22:38:56 +01:00
|
|
|
if (!transactional_table)
|
2000-11-24 00:51:18 +01:00
|
|
|
thd->options|=OPTION_STATUS_NO_TRANS_UPDATE;
|
2003-09-06 15:50:30 +02:00
|
|
|
#ifndef EMBEDDED_LIBRARY
|
2001-08-03 23:57:53 +02:00
|
|
|
if (mysql_bin_log.is_open())
|
2000-09-16 03:27:21 +02:00
|
|
|
{
|
2004-01-18 17:51:20 +01:00
|
|
|
/*
|
2005-12-22 06:39:02 +01:00
|
|
|
We need to do the job that is normally done inside
|
|
|
|
binlog_query() here, which is to ensure that the pending event
|
|
|
|
is written before tables are unlocked and before any other
|
|
|
|
events are written. We also need to update the table map
|
|
|
|
version for the binary log to mark that table maps are invalid
|
|
|
|
after this point.
|
|
|
|
*/
|
WL#2977 and WL#2712 global and session-level variable to set the binlog format (row/statement),
and new binlog format called "mixed" (which is statement-based except if only row-based is correct,
in this cset it means if UDF or UUID is used; more cases could be added in later 5.1 release):
SET GLOBAL|SESSION BINLOG_FORMAT=row|statement|mixed|default;
the global default is statement unless cluster is enabled (then it's row) as in 5.1-alpha.
It's not possible to use SET on this variable if a session is currently in row-based mode and has open temporary tables (because CREATE
TEMPORARY TABLE was not binlogged so temp table is not known on slave), or if NDB is enabled (because
NDB does not support such change on-the-fly, though it will later), of if in a stored function (see below).
The added tests test the possibility or impossibility to SET, their effects, and the mixed mode,
including in prepared statements and in stored procedures and functions.
Caveats:
a) The mixed mode will not work for stored functions: in mixed mode, a stored function will
always be binlogged as one call and in a statement-based way (e.g. INSERT VALUES(myfunc()) or SELECT myfunc()).
b) for the same reason, changing the thread's binlog format inside a stored function is
refused with an error message.
c) the same problems apply to triggers; implementing b) for triggers will be done later (will ask
Dmitri).
Additionally, as the binlog format is now changeable by each user for his session, I remove the implication
which was done at startup, where row-based automatically set log-bin-trust-routine-creators to 1
(not possible anymore as a user can now switch to stmt-based and do nasty things again), and automatically
set --innodb-locks-unsafe-for-binlog to 1 (was anyway theoretically incorrect as it disabled
phantom protection).
Plus fixes for compiler warnings.
2006-02-25 22:21:03 +01:00
|
|
|
if (thd->current_stmt_binlog_row_based)
|
2005-12-22 06:39:02 +01:00
|
|
|
thd->binlog_flush_pending_rows_event(true);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
As already explained above, we need to call end_io_cache() or the last
|
|
|
|
block will be logged only after Execute_load_query_log_event (which is
|
|
|
|
wrong), when read_info is destroyed.
|
|
|
|
*/
|
|
|
|
read_info.end_io_cache();
|
|
|
|
if (lf_info.wrote_create_file)
|
|
|
|
{
|
|
|
|
write_execute_load_query_log_event(thd, handle_duplicates,
|
|
|
|
ignore, transactional_table);
|
|
|
|
}
|
|
|
|
}
|
2000-09-16 03:27:21 +02:00
|
|
|
}
|
2003-09-06 15:50:30 +02:00
|
|
|
#endif /*!EMBEDDED_LIBRARY*/
|
2002-11-07 03:02:37 +01:00
|
|
|
if (transactional_table)
|
2005-01-27 22:38:56 +01:00
|
|
|
error=ha_autocommit_or_rollback(thd,error);
|
2004-09-28 19:08:00 +02:00
|
|
|
|
2007-02-26 20:35:28 +01:00
|
|
|
/* ok to client sent only after binlog write and engine commit */
|
|
|
|
send_ok(thd, info.copied + info.deleted, 0L, name);
|
2002-12-13 15:28:15 +01:00
|
|
|
err:
|
WL#3146 "less locking in auto_increment":
this is a cleanup patch for our current auto_increment handling:
new names for auto_increment variables in THD, new methods to manipulate them
(see sql_class.h), some move into handler::, causing less backup/restore
work when executing substatements.
This makes the logic hopefully clearer, less work is is needed in
mysql_insert().
By cleaning up, using different variables for different purposes (instead
of one for 3 things...), we fix those bugs, which someone may want to fix
in 5.0 too:
BUG#20339 "stored procedure using LAST_INSERT_ID() does not replicate
statement-based"
BUG#20341 "stored function inserting into one auto_increment puts bad
data in slave"
BUG#19243 "wrong LAST_INSERT_ID() after ON DUPLICATE KEY UPDATE"
(now if a row is updated, LAST_INSERT_ID() will return its id)
and re-fixes:
BUG#6880 "LAST_INSERT_ID() value changes during multi-row INSERT"
(already fixed differently by Ramil in 4.1)
Test of documented behaviour of mysql_insert_id() (there was no test).
The behaviour changes introduced are:
- LAST_INSERT_ID() now returns "the first autogenerated auto_increment value
successfully inserted", instead of "the first autogenerated auto_increment
value if any row was successfully inserted", see auto_increment.test.
Same for mysql_insert_id(), see mysql_client_test.c.
- LAST_INSERT_ID() returns the id of the updated row if ON DUPLICATE KEY
UPDATE, see auto_increment.test. Same for mysql_insert_id(), see
mysql_client_test.c.
- LAST_INSERT_ID() does not change if no autogenerated value was successfully
inserted (it used to then be 0), see auto_increment.test.
- if in INSERT SELECT no autogenerated value was successfully inserted,
mysql_insert_id() now returns the id of the last inserted row (it already
did this for INSERT VALUES), see mysql_client_test.c.
- if INSERT SELECT uses LAST_INSERT_ID(X), mysql_insert_id() now returns X
(it already did this for INSERT VALUES), see mysql_client_test.c.
- NDB now behaves like other engines wrt SET INSERT_ID: with INSERT IGNORE,
the id passed in SET INSERT_ID is re-used until a row succeeds; SET INSERT_ID
influences not only the first row now.
Additionally, when unlocking a table we check that the thread is not keeping
a next_insert_id (as the table is unlocked that id is potentially out-of-date);
forgetting about this next_insert_id is done in a new
handler::ha_release_auto_increment().
Finally we prepare for engines capable of reserving finite-length intervals
of auto_increment values: we store such intervals in THD. The next step
(to be done by the replication team in 5.1) is to read those intervals from
THD and actually store them in the statement-based binary log. NDB
will be a good engine to test that.
2006-07-09 17:52:19 +02:00
|
|
|
table->file->ha_release_auto_increment();
|
2002-12-13 15:28:15 +01:00
|
|
|
if (thd->lock)
|
|
|
|
{
|
|
|
|
mysql_unlock_tables(thd, thd->lock);
|
|
|
|
thd->lock=0;
|
|
|
|
}
|
2005-01-27 22:38:56 +01:00
|
|
|
thd->abort_on_warning= 0;
|
2001-08-28 05:43:55 +02:00
|
|
|
DBUG_RETURN(error);
|
2000-07-31 21:29:14 +02:00
|
|
|
}
|
|
|
|
|
2005-03-25 14:51:17 +01:00
|
|
|
|
2006-12-14 23:51:37 +01:00
|
|
|
#ifndef EMBEDDED_LIBRARY
|
|
|
|
|
2005-03-25 14:51:17 +01:00
|
|
|
/* Not a very useful function; just to avoid duplication of code */
|
|
|
|
static bool write_execute_load_query_log_event(THD *thd,
|
|
|
|
bool duplicates, bool ignore,
|
|
|
|
bool transactional_table)
|
|
|
|
{
|
|
|
|
Execute_load_query_log_event
|
|
|
|
e(thd, thd->query, thd->query_length,
|
|
|
|
(char*)thd->lex->fname_start - (char*)thd->query,
|
|
|
|
(char*)thd->lex->fname_end - (char*)thd->query,
|
|
|
|
(duplicates == DUP_REPLACE) ? LOAD_DUP_REPLACE :
|
|
|
|
(ignore ? LOAD_DUP_IGNORE : LOAD_DUP_ERROR),
|
|
|
|
transactional_table, FALSE);
|
2005-12-22 06:39:02 +01:00
|
|
|
e.flags|= LOG_EVENT_UPDATE_TABLE_MAP_VERSION_F;
|
2005-03-25 14:51:17 +01:00
|
|
|
return mysql_bin_log.write(&e);
|
|
|
|
}
|
|
|
|
|
2006-12-14 23:51:37 +01:00
|
|
|
#endif
|
2005-03-25 14:51:17 +01:00
|
|
|
|
2000-07-31 21:29:14 +02:00
|
|
|
/****************************************************************************
|
|
|
|
** Read of rows of fixed size + optional garage + optonal newline
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int
|
2004-10-21 20:53:27 +02:00
|
|
|
read_fixed_length(THD *thd, COPY_INFO &info, TABLE_LIST *table_list,
|
2005-03-16 02:32:47 +01:00
|
|
|
List<Item> &fields_vars, List<Item> &set_fields,
|
|
|
|
List<Item> &set_values, READ_INFO &read_info,
|
|
|
|
ulong skip_lines, bool ignore_check_option_errors)
|
2000-07-31 21:29:14 +02:00
|
|
|
{
|
2005-03-16 02:32:47 +01:00
|
|
|
List_iterator_fast<Item> it(fields_vars);
|
2000-07-31 21:29:14 +02:00
|
|
|
Item_field *sql_field;
|
2004-10-21 20:53:27 +02:00
|
|
|
TABLE *table= table_list->table;
|
2003-01-14 10:27:26 +01:00
|
|
|
ulonglong id;
|
2004-09-28 19:08:00 +02:00
|
|
|
bool no_trans_update;
|
2000-07-31 21:29:14 +02:00
|
|
|
DBUG_ENTER("read_fixed_length");
|
|
|
|
|
2003-04-22 09:53:07 +02:00
|
|
|
id= 0;
|
2005-03-16 02:32:47 +01:00
|
|
|
|
2000-07-31 21:29:14 +02:00
|
|
|
while (!read_info.read_fixed_length())
|
|
|
|
{
|
|
|
|
if (thd->killed)
|
|
|
|
{
|
2003-04-08 16:18:33 +02:00
|
|
|
thd->send_kill_message();
|
2000-07-31 21:29:14 +02:00
|
|
|
DBUG_RETURN(1);
|
|
|
|
}
|
2003-12-14 05:39:52 +01:00
|
|
|
if (skip_lines)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
We could implement this with a simple seek if:
|
|
|
|
- We are not using DATA INFILE LOCAL
|
|
|
|
- escape character is ""
|
|
|
|
- line starting prefix is ""
|
|
|
|
*/
|
|
|
|
skip_lines--;
|
|
|
|
continue;
|
|
|
|
}
|
2000-07-31 21:29:14 +02:00
|
|
|
it.rewind();
|
|
|
|
byte *pos=read_info.row_start;
|
|
|
|
#ifdef HAVE_purify
|
|
|
|
read_info.row_end[0]=0;
|
|
|
|
#endif
|
2004-09-28 19:08:00 +02:00
|
|
|
no_trans_update= !table->file->has_transactions();
|
2005-03-16 02:32:47 +01:00
|
|
|
|
|
|
|
restore_record(table, s->default_values);
|
|
|
|
/*
|
|
|
|
There is no variables in fields_vars list in this format so
|
|
|
|
this conversion is safe.
|
|
|
|
*/
|
2000-07-31 21:29:14 +02:00
|
|
|
while ((sql_field= (Item_field*) it++))
|
|
|
|
{
|
2003-04-30 09:02:28 +02:00
|
|
|
Field *field= sql_field->field;
|
2005-09-22 09:46:01 +02:00
|
|
|
if (field == table->next_number_field)
|
|
|
|
table->auto_increment_field_not_null= TRUE;
|
2005-03-16 02:32:47 +01:00
|
|
|
/*
|
|
|
|
No fields specified in fields_vars list can be null in this format.
|
|
|
|
Mark field as not null, we should do this for each row because of
|
|
|
|
restore_record...
|
|
|
|
*/
|
|
|
|
field->set_notnull();
|
|
|
|
|
2000-07-31 21:29:14 +02:00
|
|
|
if (pos == read_info.row_end)
|
|
|
|
{
|
2003-04-22 09:53:07 +02:00
|
|
|
thd->cuted_fields++; /* Not enough fields */
|
|
|
|
push_warning_printf(thd, MYSQL_ERROR::WARN_LEVEL_WARN,
|
|
|
|
ER_WARN_TOO_FEW_RECORDS,
|
2003-04-30 09:02:28 +02:00
|
|
|
ER(ER_WARN_TOO_FEW_RECORDS), thd->row_count);
|
2000-07-31 21:29:14 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
uint length;
|
|
|
|
byte save_chr;
|
2005-09-25 20:22:23 +02:00
|
|
|
if (field == table->next_number_field)
|
|
|
|
table->auto_increment_field_not_null= TRUE;
|
2000-07-31 21:29:14 +02:00
|
|
|
if ((length=(uint) (read_info.row_end-pos)) >
|
|
|
|
field->field_length)
|
|
|
|
length=field->field_length;
|
|
|
|
save_chr=pos[length]; pos[length]='\0'; // Safeguard aganst malloc
|
2004-09-28 19:08:00 +02:00
|
|
|
field->store((char*) pos,length,read_info.read_charset);
|
2000-07-31 21:29:14 +02:00
|
|
|
pos[length]=save_chr;
|
|
|
|
if ((pos+=length) > read_info.row_end)
|
|
|
|
pos= read_info.row_end; /* Fills rest with space */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (pos != read_info.row_end)
|
2003-04-22 09:53:07 +02:00
|
|
|
{
|
2000-07-31 21:29:14 +02:00
|
|
|
thd->cuted_fields++; /* To long row */
|
2003-04-22 09:53:07 +02:00
|
|
|
push_warning_printf(thd, MYSQL_ERROR::WARN_LEVEL_WARN,
|
|
|
|
ER_WARN_TOO_MANY_RECORDS,
|
2003-04-30 09:02:28 +02:00
|
|
|
ER(ER_WARN_TOO_MANY_RECORDS), thd->row_count);
|
2003-04-22 09:53:07 +02:00
|
|
|
}
|
2004-10-21 20:53:27 +02:00
|
|
|
|
2005-05-24 20:19:33 +02:00
|
|
|
if (thd->killed ||
|
|
|
|
fill_record_n_invoke_before_triggers(thd, set_fields, set_values,
|
|
|
|
ignore_check_option_errors,
|
|
|
|
table->triggers,
|
|
|
|
TRG_EVENT_INSERT))
|
2005-03-16 02:32:47 +01:00
|
|
|
DBUG_RETURN(1);
|
|
|
|
|
2005-01-03 22:04:52 +01:00
|
|
|
switch (table_list->view_check_option(thd,
|
|
|
|
ignore_check_option_errors)) {
|
2004-10-21 20:53:27 +02:00
|
|
|
case VIEW_CHECK_SKIP:
|
|
|
|
read_info.next_line();
|
|
|
|
goto continue_loop;
|
|
|
|
case VIEW_CHECK_ERROR:
|
|
|
|
DBUG_RETURN(-1);
|
|
|
|
}
|
|
|
|
|
2005-05-24 20:19:33 +02:00
|
|
|
if (write_record(thd, table, &info))
|
2000-07-31 21:29:14 +02:00
|
|
|
DBUG_RETURN(1);
|
2004-09-28 19:08:00 +02:00
|
|
|
thd->no_trans_update= no_trans_update;
|
|
|
|
|
2005-05-24 20:19:33 +02:00
|
|
|
/*
|
|
|
|
We don't need to reset auto-increment field since we are restoring
|
|
|
|
its default value at the beginning of each loop iteration.
|
|
|
|
*/
|
2001-11-06 23:13:29 +01:00
|
|
|
if (read_info.next_line()) // Skip to next line
|
2000-07-31 21:29:14 +02:00
|
|
|
break;
|
|
|
|
if (read_info.line_cuted)
|
2003-04-22 09:53:07 +02:00
|
|
|
{
|
2000-07-31 21:29:14 +02:00
|
|
|
thd->cuted_fields++; /* To long row */
|
2003-04-22 09:53:07 +02:00
|
|
|
push_warning_printf(thd, MYSQL_ERROR::WARN_LEVEL_WARN,
|
|
|
|
ER_WARN_TOO_MANY_RECORDS,
|
2003-04-30 09:02:28 +02:00
|
|
|
ER(ER_WARN_TOO_MANY_RECORDS), thd->row_count);
|
2003-04-22 09:53:07 +02:00
|
|
|
}
|
2003-04-30 09:02:28 +02:00
|
|
|
thd->row_count++;
|
2004-10-21 20:53:27 +02:00
|
|
|
continue_loop:;
|
2000-07-31 21:29:14 +02:00
|
|
|
}
|
|
|
|
DBUG_RETURN(test(read_info.error));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
2004-10-21 20:53:27 +02:00
|
|
|
read_sep_field(THD *thd, COPY_INFO &info, TABLE_LIST *table_list,
|
2005-03-16 02:32:47 +01:00
|
|
|
List<Item> &fields_vars, List<Item> &set_fields,
|
|
|
|
List<Item> &set_values, READ_INFO &read_info,
|
2004-10-21 20:53:27 +02:00
|
|
|
String &enclosed, ulong skip_lines,
|
|
|
|
bool ignore_check_option_errors)
|
2000-07-31 21:29:14 +02:00
|
|
|
{
|
2005-03-16 02:32:47 +01:00
|
|
|
List_iterator_fast<Item> it(fields_vars);
|
|
|
|
Item *item;
|
2004-10-21 20:53:27 +02:00
|
|
|
TABLE *table= table_list->table;
|
2000-07-31 21:29:14 +02:00
|
|
|
uint enclosed_length;
|
2003-01-14 10:27:26 +01:00
|
|
|
ulonglong id;
|
2004-09-28 19:08:00 +02:00
|
|
|
bool no_trans_update;
|
2000-07-31 21:29:14 +02:00
|
|
|
DBUG_ENTER("read_sep_field");
|
|
|
|
|
|
|
|
enclosed_length=enclosed.length();
|
2003-04-22 09:53:07 +02:00
|
|
|
id= 0;
|
2004-09-28 19:08:00 +02:00
|
|
|
no_trans_update= !table->file->has_transactions();
|
2003-04-22 09:53:07 +02:00
|
|
|
|
2000-07-31 21:29:14 +02:00
|
|
|
for (;;it.rewind())
|
|
|
|
{
|
|
|
|
if (thd->killed)
|
|
|
|
{
|
2003-04-08 16:18:33 +02:00
|
|
|
thd->send_kill_message();
|
2000-07-31 21:29:14 +02:00
|
|
|
DBUG_RETURN(1);
|
|
|
|
}
|
2005-03-16 02:32:47 +01:00
|
|
|
|
|
|
|
restore_record(table, s->default_values);
|
|
|
|
|
|
|
|
while ((item= it++))
|
2000-07-31 21:29:14 +02:00
|
|
|
{
|
|
|
|
uint length;
|
|
|
|
byte *pos;
|
|
|
|
|
|
|
|
if (read_info.read_field())
|
|
|
|
break;
|
2005-03-16 02:32:47 +01:00
|
|
|
|
|
|
|
/* If this line is to be skipped we don't want to fill field or var */
|
|
|
|
if (skip_lines)
|
|
|
|
continue;
|
|
|
|
|
2000-07-31 21:29:14 +02:00
|
|
|
pos=read_info.row_start;
|
|
|
|
length=(uint) (read_info.row_end-pos);
|
|
|
|
|
|
|
|
if (!read_info.enclosed &&
|
2005-11-20 19:47:07 +01:00
|
|
|
(enclosed_length && length == 4 &&
|
|
|
|
!memcmp(pos, STRING_WITH_LEN("NULL"))) ||
|
2000-07-31 21:29:14 +02:00
|
|
|
(length == 1 && read_info.found_null))
|
|
|
|
{
|
2005-03-16 02:32:47 +01:00
|
|
|
if (item->type() == Item::FIELD_ITEM)
|
|
|
|
{
|
|
|
|
Field *field= ((Item_field *)item)->field;
|
2006-12-06 19:02:39 +01:00
|
|
|
if (field->reset())
|
|
|
|
{
|
|
|
|
my_error(ER_WARN_NULL_TO_NOTNULL, MYF(0), field->field_name,
|
|
|
|
thd->row_count);
|
|
|
|
DBUG_RETURN(1);
|
|
|
|
}
|
2005-03-16 02:32:47 +01:00
|
|
|
field->set_null();
|
2005-09-23 14:37:22 +02:00
|
|
|
if (field == table->next_number_field)
|
|
|
|
table->auto_increment_field_not_null= TRUE;
|
2005-03-16 02:32:47 +01:00
|
|
|
if (!field->maybe_null())
|
|
|
|
{
|
2006-12-02 02:26:52 +01:00
|
|
|
if (field->type() == MYSQL_TYPE_TIMESTAMP)
|
2005-03-16 02:32:47 +01:00
|
|
|
((Field_timestamp*) field)->set_time();
|
|
|
|
else if (field != table->next_number_field)
|
2005-04-01 14:04:50 +02:00
|
|
|
field->set_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
|
2005-03-16 02:32:47 +01:00
|
|
|
ER_WARN_NULL_TO_NOTNULL, 1);
|
|
|
|
}
|
2000-07-31 21:29:14 +02:00
|
|
|
}
|
2005-03-16 02:32:47 +01:00
|
|
|
else
|
|
|
|
((Item_user_var_as_out_param *)item)->set_null_value(
|
|
|
|
read_info.read_charset);
|
2000-07-31 21:29:14 +02:00
|
|
|
continue;
|
|
|
|
}
|
2005-03-16 02:32:47 +01:00
|
|
|
|
|
|
|
if (item->type() == Item::FIELD_ITEM)
|
|
|
|
{
|
2005-09-23 14:37:22 +02:00
|
|
|
|
2005-03-16 02:32:47 +01:00
|
|
|
Field *field= ((Item_field *)item)->field;
|
|
|
|
field->set_notnull();
|
|
|
|
read_info.row_end[0]=0; // Safe to change end marker
|
2005-09-23 14:37:22 +02:00
|
|
|
if (field == table->next_number_field)
|
|
|
|
table->auto_increment_field_not_null= TRUE;
|
2005-03-16 02:32:47 +01:00
|
|
|
field->store((char*) pos, length, read_info.read_charset);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
((Item_user_var_as_out_param *)item)->set_value((char*) pos, length,
|
|
|
|
read_info.read_charset);
|
2000-07-31 21:29:14 +02:00
|
|
|
}
|
|
|
|
if (read_info.error)
|
|
|
|
break;
|
2003-12-14 05:39:52 +01:00
|
|
|
if (skip_lines)
|
|
|
|
{
|
2005-03-16 02:32:47 +01:00
|
|
|
skip_lines--;
|
2003-12-14 05:39:52 +01:00
|
|
|
continue;
|
|
|
|
}
|
2005-03-16 02:32:47 +01:00
|
|
|
if (item)
|
|
|
|
{
|
|
|
|
/* Have not read any field, thus input file is simply ended */
|
|
|
|
if (item == fields_vars.head())
|
2000-07-31 21:29:14 +02:00
|
|
|
break;
|
2005-03-16 02:32:47 +01:00
|
|
|
for (; item ; item= it++)
|
2000-07-31 21:29:14 +02:00
|
|
|
{
|
2005-03-16 02:32:47 +01:00
|
|
|
if (item->type() == Item::FIELD_ITEM)
|
|
|
|
{
|
2006-12-06 19:02:39 +01:00
|
|
|
Field *field= ((Item_field *)item)->field;
|
|
|
|
if (field->reset())
|
|
|
|
{
|
2006-12-07 06:11:56 +01:00
|
|
|
my_error(ER_WARN_NULL_TO_NOTNULL, MYF(0),field->field_name,
|
2006-12-06 19:02:39 +01:00
|
|
|
thd->row_count);
|
|
|
|
DBUG_RETURN(1);
|
|
|
|
}
|
2005-03-16 02:32:47 +01:00
|
|
|
/*
|
|
|
|
QQ: We probably should not throw warning for each field.
|
|
|
|
But how about intention to always have the same number
|
|
|
|
of warnings in THD::cuted_fields (and get rid of cuted_fields
|
|
|
|
in the end ?)
|
|
|
|
*/
|
|
|
|
thd->cuted_fields++;
|
|
|
|
push_warning_printf(thd, MYSQL_ERROR::WARN_LEVEL_WARN,
|
|
|
|
ER_WARN_TOO_FEW_RECORDS,
|
|
|
|
ER(ER_WARN_TOO_FEW_RECORDS), thd->row_count);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
((Item_user_var_as_out_param *)item)->set_null_value(
|
|
|
|
read_info.read_charset);
|
2000-07-31 21:29:14 +02:00
|
|
|
}
|
|
|
|
}
|
2004-10-21 20:53:27 +02:00
|
|
|
|
2005-05-24 20:19:33 +02:00
|
|
|
if (thd->killed ||
|
|
|
|
fill_record_n_invoke_before_triggers(thd, set_fields, set_values,
|
|
|
|
ignore_check_option_errors,
|
|
|
|
table->triggers,
|
|
|
|
TRG_EVENT_INSERT))
|
2005-03-16 02:32:47 +01:00
|
|
|
DBUG_RETURN(1);
|
|
|
|
|
2005-01-03 22:04:52 +01:00
|
|
|
switch (table_list->view_check_option(thd,
|
|
|
|
ignore_check_option_errors)) {
|
2004-10-21 20:53:27 +02:00
|
|
|
case VIEW_CHECK_SKIP:
|
|
|
|
read_info.next_line();
|
|
|
|
goto continue_loop;
|
|
|
|
case VIEW_CHECK_ERROR:
|
|
|
|
DBUG_RETURN(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-05-24 20:19:33 +02:00
|
|
|
if (write_record(thd, table, &info))
|
2000-07-31 21:29:14 +02:00
|
|
|
DBUG_RETURN(1);
|
2005-05-24 20:19:33 +02:00
|
|
|
/*
|
|
|
|
We don't need to reset auto-increment field since we are restoring
|
|
|
|
its default value at the beginning of each loop iteration.
|
|
|
|
*/
|
2004-09-28 19:08:00 +02:00
|
|
|
thd->no_trans_update= no_trans_update;
|
2001-11-06 23:13:29 +01:00
|
|
|
if (read_info.next_line()) // Skip to next line
|
2000-07-31 21:29:14 +02:00
|
|
|
break;
|
|
|
|
if (read_info.line_cuted)
|
2003-04-22 09:53:07 +02:00
|
|
|
{
|
2000-07-31 21:29:14 +02:00
|
|
|
thd->cuted_fields++; /* To long row */
|
2003-04-22 09:53:07 +02:00
|
|
|
push_warning_printf(thd, MYSQL_ERROR::WARN_LEVEL_WARN,
|
2003-04-30 09:02:28 +02:00
|
|
|
ER_WARN_TOO_MANY_RECORDS, ER(ER_WARN_TOO_MANY_RECORDS),
|
|
|
|
thd->row_count);
|
2004-09-28 19:08:00 +02:00
|
|
|
if (thd->killed)
|
|
|
|
DBUG_RETURN(1);
|
2003-04-22 09:53:07 +02:00
|
|
|
}
|
2003-04-30 09:02:28 +02:00
|
|
|
thd->row_count++;
|
2004-10-21 20:53:27 +02:00
|
|
|
continue_loop:;
|
2000-07-31 21:29:14 +02:00
|
|
|
}
|
|
|
|
DBUG_RETURN(test(read_info.error));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Unescape all escape characters, mark \N as null */
|
|
|
|
|
|
|
|
char
|
|
|
|
READ_INFO::unescape(char chr)
|
|
|
|
{
|
|
|
|
switch(chr) {
|
|
|
|
case 'n': return '\n';
|
|
|
|
case 't': return '\t';
|
|
|
|
case 'r': return '\r';
|
|
|
|
case 'b': return '\b';
|
|
|
|
case '0': return 0; // Ascii null
|
|
|
|
case 'Z': return '\032'; // Win32 end of file
|
|
|
|
case 'N': found_null=1;
|
|
|
|
|
|
|
|
/* fall through */
|
|
|
|
default: return chr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-06-11 10:20:31 +02:00
|
|
|
/*
|
|
|
|
Read a line using buffering
|
|
|
|
If last line is empty (in line mode) then it isn't outputed
|
|
|
|
*/
|
2000-07-31 21:29:14 +02:00
|
|
|
|
|
|
|
|
2003-03-07 09:55:55 +01:00
|
|
|
READ_INFO::READ_INFO(File file_par, uint tot_length, CHARSET_INFO *cs,
|
|
|
|
String &field_term, String &line_start, String &line_term,
|
2000-09-25 23:33:25 +02:00
|
|
|
String &enclosed_par, int escape, bool get_it_from_net,
|
|
|
|
bool is_fifo)
|
2000-07-31 21:29:14 +02:00
|
|
|
:file(file_par),escape_char(escape)
|
|
|
|
{
|
2003-03-07 09:55:55 +01:00
|
|
|
read_charset= cs;
|
2000-07-31 21:29:14 +02:00
|
|
|
field_term_ptr=(char*) field_term.ptr();
|
|
|
|
field_term_length= field_term.length();
|
|
|
|
line_term_ptr=(char*) line_term.ptr();
|
|
|
|
line_term_length= line_term.length();
|
|
|
|
if (line_start.length() == 0)
|
|
|
|
{
|
|
|
|
line_start_ptr=0;
|
|
|
|
start_of_line= 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
line_start_ptr=(char*) line_start.ptr();
|
|
|
|
line_start_end=line_start_ptr+line_start.length();
|
|
|
|
start_of_line= 1;
|
|
|
|
}
|
|
|
|
/* If field_terminator == line_terminator, don't use line_terminator */
|
|
|
|
if (field_term_length == line_term_length &&
|
|
|
|
!memcmp(field_term_ptr,line_term_ptr,field_term_length))
|
|
|
|
{
|
|
|
|
line_term_length=0;
|
|
|
|
line_term_ptr=(char*) "";
|
|
|
|
}
|
|
|
|
enclosed_char= (enclosed_length=enclosed_par.length()) ?
|
|
|
|
(uchar) enclosed_par[0] : INT_MAX;
|
|
|
|
field_term_char= field_term_length ? (uchar) field_term_ptr[0] : INT_MAX;
|
|
|
|
line_term_char= line_term_length ? (uchar) line_term_ptr[0] : INT_MAX;
|
|
|
|
error=eof=found_end_of_line=found_null=line_cuted=0;
|
|
|
|
buff_length=tot_length;
|
|
|
|
|
|
|
|
|
|
|
|
/* Set of a stack for unget if long terminators */
|
|
|
|
uint length=max(field_term_length,line_term_length)+1;
|
|
|
|
set_if_bigger(length,line_start.length());
|
|
|
|
stack=stack_pos=(int*) sql_alloc(sizeof(int)*length);
|
|
|
|
|
|
|
|
if (!(buffer=(byte*) my_malloc(buff_length+1,MYF(0))))
|
|
|
|
error=1; /* purecov: inspected */
|
|
|
|
else
|
|
|
|
{
|
|
|
|
end_of_buff=buffer+buff_length;
|
|
|
|
if (init_io_cache(&cache,(get_it_from_net) ? -1 : file, 0,
|
2000-09-25 23:33:25 +02:00
|
|
|
(get_it_from_net) ? READ_NET :
|
|
|
|
(is_fifo ? READ_FIFO : READ_CACHE),0L,1,
|
2000-07-31 21:29:14 +02:00
|
|
|
MYF(MY_WME)))
|
|
|
|
{
|
|
|
|
my_free((gptr) buffer,MYF(0)); /* purecov: inspected */
|
|
|
|
error=1;
|
|
|
|
}
|
2005-01-27 22:38:56 +01:00
|
|
|
else
|
2001-08-03 23:57:53 +02:00
|
|
|
{
|
2002-06-11 10:20:31 +02:00
|
|
|
/*
|
|
|
|
init_io_cache() will not initialize read_function member
|
2005-01-27 22:38:56 +01:00
|
|
|
if the cache is READ_NET. So we work around the problem with a
|
2002-06-11 10:20:31 +02:00
|
|
|
manual assignment
|
2001-10-23 21:28:03 +02:00
|
|
|
*/
|
2003-09-06 15:50:30 +02:00
|
|
|
need_end_io_cache = 1;
|
|
|
|
|
|
|
|
#ifndef EMBEDDED_LIBRARY
|
2001-10-23 21:28:03 +02:00
|
|
|
if (get_it_from_net)
|
|
|
|
cache.read_function = _my_b_net_read;
|
2001-12-06 13:10:51 +01:00
|
|
|
|
2006-02-26 00:03:52 +01:00
|
|
|
if (mysql_bin_log.is_open())
|
2001-08-03 23:57:53 +02:00
|
|
|
cache.pre_read = cache.pre_close =
|
2001-11-28 01:55:52 +01:00
|
|
|
(IO_CACHE_CALLBACK) log_loaded_block;
|
2002-12-16 14:33:29 +01:00
|
|
|
#endif
|
2001-08-03 23:57:53 +02:00
|
|
|
}
|
2000-07-31 21:29:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
READ_INFO::~READ_INFO()
|
|
|
|
{
|
|
|
|
if (!error)
|
|
|
|
{
|
2001-08-03 23:57:53 +02:00
|
|
|
if (need_end_io_cache)
|
|
|
|
::end_io_cache(&cache);
|
2000-07-31 21:29:14 +02:00
|
|
|
my_free((gptr) buffer,MYF(0));
|
|
|
|
error=1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#define GET (stack_pos != stack ? *--stack_pos : my_b_get(&cache))
|
|
|
|
#define PUSH(A) *(stack_pos++)=(A)
|
|
|
|
|
|
|
|
|
|
|
|
inline int READ_INFO::terminator(char *ptr,uint length)
|
|
|
|
{
|
|
|
|
int chr=0; // Keep gcc happy
|
|
|
|
uint i;
|
|
|
|
for (i=1 ; i < length ; i++)
|
|
|
|
{
|
|
|
|
if ((chr=GET) != *++ptr)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (i == length)
|
|
|
|
return 1;
|
|
|
|
PUSH(chr);
|
|
|
|
while (i-- > 1)
|
|
|
|
PUSH((uchar) *--ptr);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int READ_INFO::read_field()
|
|
|
|
{
|
|
|
|
int chr,found_enclosed_char;
|
|
|
|
byte *to,*new_buffer;
|
|
|
|
|
|
|
|
found_null=0;
|
|
|
|
if (found_end_of_line)
|
|
|
|
return 1; // One have to call next_line
|
|
|
|
|
2001-11-06 23:13:29 +01:00
|
|
|
/* Skip until we find 'line_start' */
|
2000-07-31 21:29:14 +02:00
|
|
|
|
|
|
|
if (start_of_line)
|
2001-11-06 23:13:29 +01:00
|
|
|
{ // Skip until line_start
|
2000-07-31 21:29:14 +02:00
|
|
|
start_of_line=0;
|
|
|
|
if (find_start_of_fields())
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if ((chr=GET) == my_b_EOF)
|
|
|
|
{
|
|
|
|
found_end_of_line=eof=1;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
to=buffer;
|
|
|
|
if (chr == enclosed_char)
|
|
|
|
{
|
|
|
|
found_enclosed_char=enclosed_char;
|
|
|
|
*to++=(byte) chr; // If error
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
found_enclosed_char= INT_MAX;
|
|
|
|
PUSH(chr);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
while ( to < end_of_buff)
|
|
|
|
{
|
|
|
|
chr = GET;
|
|
|
|
#ifdef USE_MB
|
2003-12-06 19:05:26 +01:00
|
|
|
if ((my_mbcharlen(read_charset, chr) > 1) &&
|
2003-03-07 09:55:55 +01:00
|
|
|
to+my_mbcharlen(read_charset, chr) <= end_of_buff)
|
2000-07-31 21:29:14 +02:00
|
|
|
{
|
|
|
|
uchar* p = (uchar*)to;
|
|
|
|
*to++ = chr;
|
2003-03-07 09:55:55 +01:00
|
|
|
int ml = my_mbcharlen(read_charset, chr);
|
2000-07-31 21:29:14 +02:00
|
|
|
int i;
|
|
|
|
for (i=1; i<ml; i++) {
|
|
|
|
chr = GET;
|
|
|
|
if (chr == my_b_EOF)
|
|
|
|
goto found_eof;
|
|
|
|
*to++ = chr;
|
|
|
|
}
|
2003-03-07 09:55:55 +01:00
|
|
|
if (my_ismbchar(read_charset,
|
2000-07-31 21:29:14 +02:00
|
|
|
(const char *)p,
|
|
|
|
(const char *)to))
|
|
|
|
continue;
|
|
|
|
for (i=0; i<ml; i++)
|
|
|
|
PUSH((uchar) *--to);
|
|
|
|
chr = GET;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if (chr == my_b_EOF)
|
|
|
|
goto found_eof;
|
|
|
|
if (chr == escape_char)
|
|
|
|
{
|
|
|
|
if ((chr=GET) == my_b_EOF)
|
|
|
|
{
|
|
|
|
*to++= (byte) escape_char;
|
|
|
|
goto found_eof;
|
|
|
|
}
|
2005-06-23 01:14:14 +02:00
|
|
|
/*
|
|
|
|
When escape_char == enclosed_char, we treat it like we do for
|
|
|
|
handling quotes in SQL parsing -- you can double-up the
|
|
|
|
escape_char to include it literally, but it doesn't do escapes
|
|
|
|
like \n. This allows: LOAD DATA ... ENCLOSED BY '"' ESCAPED BY '"'
|
|
|
|
with data like: "fie""ld1", "field2"
|
|
|
|
*/
|
|
|
|
if (escape_char != enclosed_char || chr == escape_char)
|
|
|
|
{
|
|
|
|
*to++ = (byte) unescape((char) chr);
|
|
|
|
continue;
|
|
|
|
}
|
2005-10-25 01:27:40 +02:00
|
|
|
PUSH(chr);
|
|
|
|
chr= escape_char;
|
2000-07-31 21:29:14 +02:00
|
|
|
}
|
|
|
|
#ifdef ALLOW_LINESEPARATOR_IN_STRINGS
|
|
|
|
if (chr == line_term_char)
|
|
|
|
#else
|
|
|
|
if (chr == line_term_char && found_enclosed_char == INT_MAX)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
if (terminator(line_term_ptr,line_term_length))
|
|
|
|
{ // Maybe unexpected linefeed
|
|
|
|
enclosed=0;
|
|
|
|
found_end_of_line=1;
|
|
|
|
row_start=buffer;
|
|
|
|
row_end= to;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (chr == found_enclosed_char)
|
|
|
|
{
|
|
|
|
if ((chr=GET) == found_enclosed_char)
|
|
|
|
{ // Remove dupplicated
|
|
|
|
*to++ = (byte) chr;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// End of enclosed field if followed by field_term or line_term
|
|
|
|
if (chr == my_b_EOF ||
|
|
|
|
chr == line_term_char && terminator(line_term_ptr,
|
|
|
|
line_term_length))
|
|
|
|
{ // Maybe unexpected linefeed
|
|
|
|
enclosed=1;
|
|
|
|
found_end_of_line=1;
|
|
|
|
row_start=buffer+1;
|
|
|
|
row_end= to;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (chr == field_term_char &&
|
|
|
|
terminator(field_term_ptr,field_term_length))
|
|
|
|
{
|
|
|
|
enclosed=1;
|
|
|
|
row_start=buffer+1;
|
|
|
|
row_end= to;
|
|
|
|
return 0;
|
|
|
|
}
|
2003-08-22 03:07:40 +02:00
|
|
|
/*
|
|
|
|
The string didn't terminate yet.
|
|
|
|
Store back next character for the loop
|
|
|
|
*/
|
2000-07-31 21:29:14 +02:00
|
|
|
PUSH(chr);
|
2003-08-22 03:07:40 +02:00
|
|
|
/* copy the found term character to 'to' */
|
|
|
|
chr= found_enclosed_char;
|
2000-07-31 21:29:14 +02:00
|
|
|
}
|
|
|
|
else if (chr == field_term_char && found_enclosed_char == INT_MAX)
|
|
|
|
{
|
|
|
|
if (terminator(field_term_ptr,field_term_length))
|
|
|
|
{
|
|
|
|
enclosed=0;
|
|
|
|
row_start=buffer;
|
|
|
|
row_end= to;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*to++ = (byte) chr;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
** We come here if buffer is too small. Enlarge it and continue
|
|
|
|
*/
|
|
|
|
if (!(new_buffer=(byte*) my_realloc((char*) buffer,buff_length+1+IO_SIZE,
|
|
|
|
MYF(MY_WME))))
|
|
|
|
return (error=1);
|
|
|
|
to=new_buffer + (to-buffer);
|
|
|
|
buffer=new_buffer;
|
|
|
|
buff_length+=IO_SIZE;
|
|
|
|
end_of_buff=buffer+buff_length;
|
|
|
|
}
|
|
|
|
|
|
|
|
found_eof:
|
|
|
|
enclosed=0;
|
|
|
|
found_end_of_line=eof=1;
|
|
|
|
row_start=buffer;
|
|
|
|
row_end=to;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2003-12-14 05:39:52 +01:00
|
|
|
Read a row with fixed length.
|
|
|
|
|
|
|
|
NOTES
|
|
|
|
The row may not be fixed size on disk if there are escape
|
|
|
|
characters in the file.
|
|
|
|
|
|
|
|
IMPLEMENTATION NOTE
|
|
|
|
One can't use fixed length with multi-byte charset **
|
|
|
|
|
|
|
|
RETURN
|
|
|
|
0 ok
|
|
|
|
1 error
|
2000-07-31 21:29:14 +02:00
|
|
|
*/
|
2001-12-06 13:10:51 +01:00
|
|
|
|
2000-07-31 21:29:14 +02:00
|
|
|
int READ_INFO::read_fixed_length()
|
|
|
|
{
|
|
|
|
int chr;
|
|
|
|
byte *to;
|
|
|
|
if (found_end_of_line)
|
|
|
|
return 1; // One have to call next_line
|
|
|
|
|
|
|
|
if (start_of_line)
|
2001-11-06 23:13:29 +01:00
|
|
|
{ // Skip until line_start
|
2000-07-31 21:29:14 +02:00
|
|
|
start_of_line=0;
|
|
|
|
if (find_start_of_fields())
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
to=row_start=buffer;
|
|
|
|
while (to < end_of_buff)
|
|
|
|
{
|
|
|
|
if ((chr=GET) == my_b_EOF)
|
|
|
|
goto found_eof;
|
|
|
|
if (chr == escape_char)
|
|
|
|
{
|
|
|
|
if ((chr=GET) == my_b_EOF)
|
|
|
|
{
|
|
|
|
*to++= (byte) escape_char;
|
|
|
|
goto found_eof;
|
|
|
|
}
|
|
|
|
*to++ =(byte) unescape((char) chr);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (chr == line_term_char)
|
|
|
|
{
|
|
|
|
if (terminator(line_term_ptr,line_term_length))
|
|
|
|
{ // Maybe unexpected linefeed
|
|
|
|
found_end_of_line=1;
|
|
|
|
row_end= to;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*to++ = (byte) chr;
|
|
|
|
}
|
|
|
|
row_end=to; // Found full line
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
found_eof:
|
|
|
|
found_end_of_line=eof=1;
|
|
|
|
row_start=buffer;
|
|
|
|
row_end=to;
|
|
|
|
return to == buffer ? 1 : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int READ_INFO::next_line()
|
|
|
|
{
|
|
|
|
line_cuted=0;
|
|
|
|
start_of_line= line_start_ptr != 0;
|
|
|
|
if (found_end_of_line || eof)
|
|
|
|
{
|
|
|
|
found_end_of_line=0;
|
|
|
|
return eof;
|
|
|
|
}
|
|
|
|
found_end_of_line=0;
|
|
|
|
if (!line_term_length)
|
|
|
|
return 0; // No lines
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
int chr = GET;
|
|
|
|
#ifdef USE_MB
|
2003-12-06 19:05:26 +01:00
|
|
|
if (my_mbcharlen(read_charset, chr) > 1)
|
2000-07-31 21:29:14 +02:00
|
|
|
{
|
|
|
|
for (int i=1;
|
2003-03-07 09:55:55 +01:00
|
|
|
chr != my_b_EOF && i<my_mbcharlen(read_charset, chr);
|
2000-07-31 21:29:14 +02:00
|
|
|
i++)
|
|
|
|
chr = GET;
|
|
|
|
if (chr == escape_char)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if (chr == my_b_EOF)
|
|
|
|
{
|
|
|
|
eof=1;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (chr == escape_char)
|
|
|
|
{
|
|
|
|
line_cuted=1;
|
|
|
|
if (GET == my_b_EOF)
|
|
|
|
return 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (chr == line_term_char && terminator(line_term_ptr,line_term_length))
|
|
|
|
return 0;
|
|
|
|
line_cuted=1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool READ_INFO::find_start_of_fields()
|
|
|
|
{
|
|
|
|
int chr;
|
|
|
|
try_again:
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if ((chr=GET) == my_b_EOF)
|
|
|
|
{
|
|
|
|
found_end_of_line=eof=1;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
} while ((char) chr != line_start_ptr[0]);
|
|
|
|
for (char *ptr=line_start_ptr+1 ; ptr != line_start_end ; ptr++)
|
|
|
|
{
|
|
|
|
chr=GET; // Eof will be checked later
|
|
|
|
if ((char) chr != *ptr)
|
|
|
|
{ // Can't be line_start
|
|
|
|
PUSH(chr);
|
|
|
|
while (--ptr != line_start_ptr)
|
2004-05-05 11:31:17 +02:00
|
|
|
{ // Restart with next char
|
2000-07-31 21:29:14 +02:00
|
|
|
PUSH((uchar) *ptr);
|
|
|
|
}
|
|
|
|
goto try_again;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|