39
37
#include <drizzled/message/table.pb.h>
39
/* Bits to show what an alter table will do */
40
#include <drizzled/sql_bitmap.h>
42
#include <drizzled/cursor.h>
42
45
#include <algorithm>
47
47
#define HA_MAX_ALTER_FLAGS 40
49
50
typedef std::bitset<HA_MAX_ALTER_FLAGS> HA_ALTER_FLAGS;
51
52
extern uint64_t refresh_version; /* Increments on each reload */
55
typedef bool (*qc_engine_callback)(Session *session, char *table_key,
57
uint64_t *engine_data);
60
/* The Cursor for a table type. Will be included in the Table structure */
56
65
class Select_Lex_Unit;
66
struct st_foreign_key_info;
67
typedef struct st_foreign_key_info FOREIGN_KEY_INFO;
71
struct st_table_log_memory_entry;
143
160
If a blob column has NULL value, then its length and blob data pointer
144
161
must be set to 0.
164
class Cursor :public Sql_alloc
148
Table &table; /* The current open table */
149
plugin::StorageEngine &engine; /* storage engine of this Cursor */
167
TableShare *table_share; /* The table definition */
168
Table *table; /* The current open table */
152
170
ha_rows estimation_rows_to_insert;
155
inline plugin::StorageEngine *getEngine() const /* table_type for handler */
159
unsigned char *ref; /* Pointer to current row */
172
drizzled::plugin::StorageEngine *engine; /* storage engine of this Cursor */
173
unsigned char *ref; /* Pointer to current row */
160
174
unsigned char *dup_ref; /* Pointer to duplicate row */
162
TableShare *getShare();
164
Table *getTable() const
169
176
ha_statistics stats;
170
177
/** MultiRangeRead-related members: */
171
178
range_seq_t mrr_iter; /* Interator to traverse the range sequence */
172
179
RANGE_SEQ_IF mrr_funcs; /* Range sequence traversal functions */
180
HANDLER_BUFFER *multi_range_buffer; /* MRR buffer info */
174
181
uint32_t ranges_in_seq; /* Total number of ranges in the traversed sequence */
175
182
/* true <=> source MRR ranges and the output are ordered */
176
183
bool mrr_is_output_sorted;
225
233
Discrete_interval auto_inc_interval_for_cur_row;
227
Cursor(plugin::StorageEngine &engine_arg, Table &share_arg);
235
Cursor(drizzled::plugin::StorageEngine &engine_arg, TableShare &share_arg)
236
:table_share(&share_arg), table(0),
237
estimation_rows_to_insert(0), engine(&engine_arg),
238
ref(0), in_range_check_pushed_down(false),
239
key_used_on_scan(MAX_KEY), active_index(MAX_KEY),
240
ref_length(sizeof(my_off_t)),
242
locked(false), implicit_emptied(0),
243
next_insert_id(0), insert_id_for_cur_row(0)
228
245
virtual ~Cursor(void);
229
virtual Cursor *clone(memory::Root *mem_root);
246
virtual Cursor *clone(MEM_ROOT *mem_root);
231
248
/* ha_ methods: pubilc wrappers for private virtual API */
233
int ha_open(const TableIdentifier &identifier, int mode, int test_if_locked);
234
int startIndexScan(uint32_t idx, bool sorted);
236
int startTableScan(bool scan);
250
int ha_open(Table *table, const char *name, int mode, int test_if_locked);
251
int ha_index_init(uint32_t idx, bool sorted);
253
int ha_rnd_init(bool scan);
240
257
/* this is necessary in many places, e.g. in HANDLER command */
241
258
int ha_index_or_rnd_end();
259
drizzled::plugin::StorageEngine::Table_flags ha_table_flags() const;
244
262
These functions represent the public interface to *users* of the
245
263
Cursor class, hence they are *not* virtual. For the inheritance
246
interface, see the (private) functions doInsertRecord(), doUpdateRecord(),
247
and doDeleteRecord() below.
264
interface, see the (private) functions write_row(), update_row(),
265
and delete_row() below.
249
267
int ha_external_lock(Session *session, int lock_type);
250
int insertRecord(unsigned char * buf);
251
int updateRecord(const unsigned char * old_data, unsigned char * new_data);
252
int deleteRecord(const unsigned char * buf);
268
int ha_write_row(unsigned char * buf);
269
int ha_update_row(const unsigned char * old_data, unsigned char * new_data);
270
int ha_delete_row(const unsigned char * buf);
253
271
void ha_release_auto_increment();
255
273
/** to be actually called to get 'check()' functionality*/
280
302
virtual ha_rows multi_range_read_info_const(uint32_t keyno, RANGE_SEQ_IF *seq,
281
303
void *seq_init_param,
282
304
uint32_t n_ranges, uint32_t *bufsz,
283
uint32_t *flags, optimizer::CostVector *cost);
305
uint32_t *flags, COST_VECT *cost);
284
306
virtual int multi_range_read_info(uint32_t keyno, uint32_t n_ranges, uint32_t keys,
285
uint32_t *bufsz, uint32_t *flags, optimizer::CostVector *cost);
307
uint32_t *bufsz, uint32_t *flags, COST_VECT *cost);
286
308
virtual int multi_range_read_init(RANGE_SEQ_IF *seq, void *seq_init_param,
287
uint32_t n_ranges, uint32_t mode);
309
uint32_t n_ranges, uint32_t mode,
310
HANDLER_BUFFER *buf);
288
311
virtual int multi_range_read_next(char **range_info);
327
354
virtual int close(void)=0;
357
@retval 0 Bulk update used by Cursor
358
@retval 1 Bulk update not used, normal operation used
360
virtual bool start_bulk_update() { return 1; }
362
@retval 0 Bulk delete used by Cursor
363
@retval 1 Bulk delete not used, normal operation used
365
virtual bool start_bulk_delete() { return 1; }
367
After this call all outstanding updates must be performed. The number
368
of duplicate key errors are reported in the duplicate key parameter.
369
It is allowed to continue to the batched update after this call, the
370
Cursor has to wait until end_bulk_update with changing state.
372
@param dup_key_found Number of duplicate keys found
375
@retval >0 Error code
377
virtual int exec_bulk_update(uint32_t *)
380
return HA_ERR_WRONG_COMMAND;
383
Perform any needed clean-up, no outstanding updates are there at the
386
virtual void end_bulk_update() { return; }
388
Execute all outstanding deletes and close down the bulk delete.
391
@retval >0 Error code
393
virtual int end_bulk_delete()
396
return HA_ERR_WRONG_COMMAND;
331
400
Positions an index cursor to the index specified in the handle. Fetches the
332
401
row if available. If the key value is null, begin at the first key of the
335
virtual int index_read_map(unsigned char * buf, const unsigned char *key,
404
virtual int index_read_map(unsigned char * buf, const unsigned char * key,
336
405
key_part_map keypart_map,
337
406
enum ha_rkey_function find_flag)
339
uint32_t key_len= calculate_key_len(active_index, keypart_map);
408
uint32_t key_len= calculate_key_len(table, active_index, key, keypart_map);
340
409
return index_read(buf, key, key_len, find_flag);
379
443
bool eq_range, bool sorted);
380
444
virtual int read_range_next();
381
445
int compare_key(key_range *range);
446
int compare_key2(key_range *range);
382
447
virtual int rnd_next(unsigned char *)=0;
383
448
virtual int rnd_pos(unsigned char *, unsigned char *)=0;
450
One has to use this method when to find
451
random position by record as the plain
452
position() call doesn't work for some
453
handlers for random position.
455
virtual int rnd_pos_by_record(unsigned char *record);
384
456
virtual int read_first_row(unsigned char *buf, uint32_t primary_key);
458
The following function is only needed for tables that may be temporary
461
virtual int restart_rnd_next(unsigned char *, unsigned char *)
462
{ return HA_ERR_WRONG_COMMAND; }
385
463
virtual int rnd_same(unsigned char *, uint32_t)
386
464
{ return HA_ERR_WRONG_COMMAND; }
387
465
virtual ha_rows records_in_range(uint32_t, key_range *, key_range *)
464
545
virtual bool can_switch_engines(void) { return true; }
465
546
/** used in REPLACE; is > 0 if table is referred by a FOREIGN KEY */
466
virtual int get_foreign_key_list(Session *, List<ForeignKeyInfo> *)
547
virtual int get_foreign_key_list(Session *, List<FOREIGN_KEY_INFO> *)
468
549
virtual uint32_t referenced_by_foreign_key() { return 0;}
469
550
virtual void free_foreign_key_create_info(char *) {}
551
/** The following can be called without an open Cursor */
553
virtual uint32_t index_flags(uint32_t idx, uint32_t part, bool all_parts) const =0;
555
virtual int add_index(Table *, KEY *, uint32_t)
556
{ return (HA_ERR_WRONG_COMMAND); }
557
virtual int prepare_drop_index(Table *, uint32_t *, uint32_t)
558
{ return (HA_ERR_WRONG_COMMAND); }
559
virtual int final_drop_index(Table *)
560
{ return (HA_ERR_WRONG_COMMAND); }
562
uint32_t max_record_length() const
563
{ return std::min((unsigned int)HA_MAX_REC_LENGTH, max_supported_record_length()); }
564
uint32_t max_keys() const
565
{ return std::min((unsigned int)MAX_KEY, max_supported_keys()); }
566
uint32_t max_key_parts() const
567
{ return std::min((unsigned int)MAX_REF_PARTS, max_supported_key_parts()); }
568
uint32_t max_key_length() const
569
{ return std::min((unsigned int)MAX_KEY_LENGTH, max_supported_key_length()); }
570
uint32_t max_key_part_length(void) const
571
{ return std::min((unsigned int)MAX_KEY_LENGTH, max_supported_key_part_length()); }
573
virtual uint32_t max_supported_record_length(void) const
574
{ return HA_MAX_REC_LENGTH; }
575
virtual uint32_t max_supported_keys(void) const { return 0; }
576
virtual uint32_t max_supported_key_parts(void) const { return MAX_REF_PARTS; }
577
virtual uint32_t max_supported_key_length(void) const { return MAX_KEY_LENGTH; }
578
virtual uint32_t max_supported_key_part_length(void) const { return 255; }
580
virtual bool low_byte_first(void) const { return true; }
581
virtual uint32_t checksum(void) const { return 0; }
582
virtual bool is_crashed(void) const { return false; }
583
virtual bool auto_repair(void) const { return false; }
472
586
Is not invoked for non-transactional temporary tables.
522
637
the corresponding 'ha_*' method above.
525
virtual int open(const char *, int , uint32_t ) { assert(0); return -1; }
526
virtual int doOpen(const TableIdentifier &identifier, int mode, uint32_t test_if_locked);
527
virtual int doStartIndexScan(uint32_t idx, bool)
640
virtual int open(const char *name, int mode, uint32_t test_if_locked)=0;
641
virtual int index_init(uint32_t idx, bool)
528
642
{ active_index= idx; return 0; }
529
virtual int doEndIndexScan() { active_index= MAX_KEY; return 0; }
643
virtual int index_end() { active_index= MAX_KEY; return 0; }
531
doStartTableScan() can be called two times without doEndTableScan() in between
645
rnd_init() can be called two times without rnd_end() in between
532
646
(it only makes sense if scan=1).
533
647
then the second call should prepare for the new table scan (e.g
534
648
if rnd_init allocates the cursor, second call should position it
535
649
to the start of the table, no need to deallocate and allocate it again
537
virtual int doStartTableScan(bool scan)= 0;
538
virtual int doEndTableScan() { return 0; }
539
virtual int doInsertRecord(unsigned char *)
541
return HA_ERR_WRONG_COMMAND;
544
virtual int doUpdateRecord(const unsigned char *, unsigned char *)
546
return HA_ERR_WRONG_COMMAND;
549
virtual int doDeleteRecord(const unsigned char *)
651
virtual int rnd_init(bool scan)= 0;
652
virtual int rnd_end() { return 0; }
653
virtual int write_row(unsigned char *)
655
return HA_ERR_WRONG_COMMAND;
658
virtual int update_row(const unsigned char *, unsigned char *)
660
return HA_ERR_WRONG_COMMAND;
663
virtual int delete_row(const unsigned char *)
551
665
return HA_ERR_WRONG_COMMAND;
595
709
uint32_t, enum ha_rkey_function)
596
710
{ return HA_ERR_WRONG_COMMAND; }
597
711
virtual int index_read_last(unsigned char *, const unsigned char *, uint32_t)
598
{ return (errno= HA_ERR_WRONG_COMMAND); }
712
{ return (my_errno= HA_ERR_WRONG_COMMAND); }
714
This method is similar to update_row, however the Cursor doesn't need
715
to execute the updates at this point in time. The Cursor can be certain
716
that another call to bulk_update_row will occur OR a call to
717
exec_bulk_update before the set of updates in this query is concluded.
719
@param old_data Old record
720
@param new_data New record
721
@param dup_key_found Number of duplicate keys found
723
@retval 0 Bulk delete used by Cursor
724
@retval 1 Bulk delete not used, normal operation used
726
virtual int bulk_update_row(const unsigned char *, unsigned char *, uint32_t *)
729
return HA_ERR_WRONG_COMMAND;
600
732
This is called to delete all rows in a table
601
733
If the Cursor don't support this, then this function will
637
772
extern const char *ha_row_type[];
773
extern const char *tx_isolation_names[];
774
extern const char *binlog_format_names[];
775
extern TYPELIB tx_isolation_typelib;
776
extern uint32_t total_ha, total_ha_2pc;
778
/* Wrapper functions */
779
#define ha_commit(session) (ha_commit_trans((session), true))
780
#define ha_rollback(session) (ha_rollback_trans((session), true))
639
782
/* basic stuff */
640
void ha_init_errors(void);
643
SortField *make_unireg_sortorder(Order *order, uint32_t *length,
644
SortField *sortorder);
783
int ha_init_errors(void);
787
/* transactions: interface to plugin::StorageEngine functions */
788
int ha_commit_one_phase(Session *session, bool all);
789
int ha_rollback_trans(Session *session, bool all);
791
/* transactions: these functions never call plugin::StorageEngine functions directly */
792
int ha_commit_trans(Session *session, bool all);
793
int ha_autocommit_or_rollback(Session *session, int error);
794
int ha_enable_transaction(Session *session, bool on);
797
int ha_rollback_to_savepoint(Session *session, SAVEPOINT *sv);
798
int ha_savepoint(Session *session, SAVEPOINT *sv);
799
int ha_release_savepoint(Session *session, SAVEPOINT *sv);
801
/* these are called by storage engines */
802
void trans_register_ha(Session *session, bool all, drizzled::plugin::StorageEngine *engine);
804
uint32_t filename_to_tablename(const char *from, char *to, uint32_t to_length);
805
bool tablename_to_filename(const char *from, char *to, size_t to_length);
809
Storage engine has to assume the transaction will end up with 2pc if
810
- there is more than one 2pc-capable storage engine available
811
- in the current transaction 2pc was not disabled yet
813
#define trans_need_2pc(session, all) ((total_ha_2pc > 1) && \
814
!((all ? &session->transaction.all : &session->transaction.stmt)->no_2pc))
817
bool mysql_xa_recover(Session *session);
819
SORT_FIELD * make_unireg_sortorder(order_st *order, uint32_t *length,
820
SORT_FIELD *sortorder);
645
821
int setup_order(Session *session, Item **ref_pointer_array, TableList *tables,
646
List<Item> &fields, List <Item> &all_fields, Order *order);
822
List<Item> &fields, List <Item> &all_fields, order_st *order);
647
823
int setup_group(Session *session, Item **ref_pointer_array, TableList *tables,
648
List<Item> &fields, List<Item> &all_fields, Order *order,
824
List<Item> &fields, List<Item> &all_fields, order_st *order,
649
825
bool *hidden_group_fields);
650
826
bool fix_inner_refs(Session *session, List<Item> &all_fields, Select_Lex *select,
651
827
Item **ref_pointer_array);
653
829
bool handle_select(Session *session, LEX *lex, select_result *result,
654
830
uint64_t setup_tables_done_option);
831
bool mysql_select(Session *session, Item ***rref_pointer_array,
832
TableList *tables, uint32_t wild_num, List<Item> &list,
833
COND *conds, uint32_t og_num, order_st *order, order_st *group,
834
Item *having, uint64_t select_type,
835
select_result *result, Select_Lex_Unit *unit,
836
Select_Lex *select_lex);
655
837
void free_underlaid_joins(Session *session, Select_Lex *select);
838
bool mysql_explain_union(Session *session, Select_Lex_Unit *unit,
839
select_result *result);
840
int mysql_explain_select(Session *session, Select_Lex *sl, char const *type,
841
select_result *result);
657
843
bool mysql_handle_derived(LEX *lex, bool (*processor)(Session *session,
661
847
bool mysql_derived_filling(Session *session, LEX *lex, TableList *t);
662
848
int prepare_create_field(CreateField *sql_field,
663
849
uint32_t *blob_columns,
664
int *timestamps, int *timestamps_with_niladic);
850
int *timestamps, int *timestamps_with_niladic,
851
int64_t table_flags);
666
bool mysql_create_table(Session *session,
667
const TableIdentifier &identifier,
853
bool mysql_create_table(Session *session,const char *db, const char *table_name,
668
854
HA_CREATE_INFO *create_info,
669
message::Table &table_proto,
855
drizzled::message::Table *table_proto,
670
856
AlterInfo *alter_info,
671
857
bool tmp_table, uint32_t select_field_count,
672
858
bool is_if_not_exists);
674
bool mysql_create_table_no_lock(Session *session,
675
const TableIdentifier &identifier,
860
bool mysql_create_table_no_lock(Session *session, const char *db,
861
const char *table_name,
676
862
HA_CREATE_INFO *create_info,
677
message::Table &table_proto,
863
drizzled::message::Table *table_proto,
678
864
AlterInfo *alter_info,
679
bool tmp_table, uint32_t select_field_count,
865
bool tmp_table, uint32_t select_field_count,
680
866
bool is_if_not_exists);
682
bool mysql_create_like_table(Session* session,
683
const TableIdentifier &destination_identifier,
684
TableList* table, TableList* src_table,
685
message::Table &create_table_proto,
868
bool mysql_recreate_table(Session *session, TableList *table_list);
870
bool mysql_create_like_table(Session* session, TableList* table, TableList* src_table,
871
drizzled::message::Table& create_table_proto,
872
drizzled::plugin::StorageEngine*,
686
873
bool is_if_not_exists,
687
874
bool is_engine_set);
689
bool mysql_rename_table(Session &session,
690
plugin::StorageEngine *base,
691
const TableIdentifier &old_identifier,
692
const TableIdentifier &new_identifier);
876
bool mysql_rename_table(drizzled::plugin::StorageEngine *base, const char *old_db,
877
const char * old_name, const char *new_db,
878
const char * new_name, uint32_t flags);
694
880
bool mysql_prepare_update(Session *session, TableList *table_list,
695
Item **conds, uint32_t order_num, Order *order);
881
Item **conds, uint32_t order_num, order_st *order);
696
882
int mysql_update(Session *session,TableList *tables,List<Item> &fields,
697
883
List<Item> &values,COND *conds,
698
uint32_t order_num, Order *order, ha_rows limit,
884
uint32_t order_num, order_st *order, ha_rows limit,
699
885
enum enum_duplicates handle_duplicates, bool ignore);
700
886
bool mysql_prepare_insert(Session *session, TableList *table_list, Table *table,
701
887
List<Item> &fields, List_item *values,
718
904
uint32_t key_length, uint32_t db_flags, int *error);
719
905
TableShare *get_cached_table_share(const char *db, const char *table_name);
720
906
bool reopen_name_locked_table(Session* session, TableList* table_list, bool link_in);
907
Table *table_cache_insert_placeholder(Session *session, const char *key,
908
uint32_t key_length);
909
bool lock_table_name_if_not_cached(Session *session, const char *db,
910
const char *table_name, Table **table);
911
bool reopen_table(Table *table);
721
912
bool reopen_tables(Session *session,bool get_locks,bool in_refresh);
913
void close_data_files_and_morph_locks(Session *session, const char *db,
914
const char *table_name);
722
915
void close_handle_and_leave_table_as_lock(Table *table);
723
916
bool wait_for_tables(Session *session);
724
917
bool table_is_used(Table *table, bool wait_for_name_lock);
725
Table *drop_locked_tables(Session *session, const drizzled::TableIdentifier &identifier);
726
void abort_locked_tables(Session *session, const drizzled::TableIdentifier &identifier);
918
Table *drop_locked_tables(Session *session,const char *db, const char *table_name);
919
void abort_locked_tables(Session *session,const char *db, const char *table_name);
727
920
extern Field *not_found_field;
728
921
extern Field *view_ref_found;