drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
drizzled/cursor.h
1/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3 *
4 * Copyright (C) 2008 Sun Microsystems, Inc.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20#pragma once
21
22#include <drizzled/atomics.h>
24#include <drizzled/discrete_interval.h>
25#include <drizzled/error_t.h>
26#include <drizzled/ha_statistics.h>
27#include <drizzled/handler_structs.h>
28#include <drizzled/identifier.h>
29#include <drizzled/key_map.h>
30#include <drizzled/message/table.h>
31#include <drizzled/sql_list.h>
32#include <drizzled/thr_lock.h>
33
34#include <bitset>
35#include <algorithm>
36
37#include <drizzled/visibility.h>
38
39namespace drizzled {
40
41class KeyPartInfo;
42
43#define HA_MAX_ALTER_FLAGS 40
44
45typedef std::bitset<HA_MAX_ALTER_FLAGS> HA_ALTER_FLAGS;
46
47typedef List<Item> List_item;
48extern KEY_CREATE_INFO default_key_create_info;
49
50/*
51 bitmap with first N+1 bits set
52 (keypart_map for a key prefix of [0..N] keyparts)
53*/
54inline key_part_map make_keypart_map(int a)
55{
56 return (((key_part_map)2 << a) - 1);
57}
58
59/*
60 bitmap with first N bits set
61 (keypart_map for a key prefix of [0..N-1] keyparts)
62*/
63inline key_part_map make_prev_keypart_map(int a)
64{
65 return (((key_part_map)1 << a) - 1);
66}
67
111class DRIZZLED_API Cursor
112{
113 friend class SEAPITesterCursor;
114 Table &table; /* The current open table */
115 plugin::StorageEngine &engine; /* storage engine of this Cursor */
116
117protected:
118 ha_rows estimation_rows_to_insert;
119
120public:
121 inline plugin::StorageEngine *getEngine() const /* table_type for handler */
122 {
123 return &engine;
124 }
125 unsigned char *ref; /* Pointer to current row */
126 unsigned char *dup_ref; /* Pointer to duplicate row */
127
128 TableShare *getShare();
129
130 Table *getTable() const
131 {
132 return &table;
133 }
134
135 ha_statistics stats;
137 range_seq_t mrr_iter; /* Interator to traverse the range sequence */
138 RANGE_SEQ_IF mrr_funcs; /* Range sequence traversal functions */
139
140 uint32_t ranges_in_seq; /* Total number of ranges in the traversed sequence */
141 /* true <=> source MRR ranges and the output are ordered */
142 bool mrr_is_output_sorted;
143
146
147 bool eq_range;
148
151
154 KeyPartInfo *range_key_part;
155 int key_compare_result_on_equal;
156
157 uint32_t errkey; /* Last dup key */
158 uint32_t key_used_on_scan;
159 uint32_t active_index;
161 uint32_t ref_length;
162 enum {NONE=0, INDEX, RND} inited;
163 bool locked;
164
175 uint64_t getNextInsertId() const
176 {
177 return next_insert_id;
178 }
179
183 uint64_t getAutoIncrement() const
184 {
185 return stats.auto_increment_value;
186 }
187
200
201 Cursor(plugin::StorageEngine &engine_arg, Table &share_arg);
202 virtual ~Cursor(void);
203 virtual Cursor *clone(memory::Root *mem_root);
204
205 /* ha_ methods: pubilc wrappers for private virtual API */
206
207 int ha_open(const identifier::Table &identifier, int mode, int test_if_locked);
208 int startIndexScan(uint32_t idx, bool sorted) __attribute__ ((warn_unused_result));
209 int endIndexScan();
210 int startTableScan(bool scan) __attribute__ ((warn_unused_result));
211 int endTableScan();
212 int ha_reset();
213
214 /* this is necessary in many places, e.g. in HANDLER command */
215 int ha_index_or_rnd_end();
216
223 int ha_external_lock(Session *session, int lock_type);
224 int insertRecord(unsigned char * buf) __attribute__ ((warn_unused_result));
225 int updateRecord(const unsigned char * old_data, unsigned char * new_data) __attribute__ ((warn_unused_result));
226 int deleteRecord(const unsigned char * buf) __attribute__ ((warn_unused_result));
228 int ha_check(Session*);
229 void ha_start_bulk_insert(ha_rows rows);
230 int ha_end_bulk_insert();
231 int ha_delete_all_rows();
232 int ha_reset_auto_increment(uint64_t value);
233 int ha_analyze(Session*);
234
235 int ha_disable_indexes(uint32_t mode);
236 int ha_enable_indexes(uint32_t mode);
237 int ha_discard_or_import_tablespace(bool discard);
238 void closeMarkForDelete();
239
240 void adjust_next_insert_id_after_explicit_value(uint64_t nr);
241 int update_auto_increment();
242
243 /* Estimates calculation */
244 virtual double scan_time(void)
245 { return static_cast<double>(stats.data_file_length) / IO_SIZE + 2; }
246 virtual double read_time(uint32_t, uint32_t ranges, ha_rows rows)
247 { return ranges + rows; }
248
249 virtual double index_only_read_time(uint32_t keynr, double records);
250
251 virtual ha_rows multi_range_read_info_const(uint32_t keyno, RANGE_SEQ_IF *seq,
252 void *seq_init_param,
253 uint32_t n_ranges, uint32_t *bufsz,
254 uint32_t *flags, optimizer::CostVector *cost);
255 virtual int multi_range_read_info(uint32_t keyno, uint32_t n_ranges, uint32_t keys,
256 uint32_t *bufsz, uint32_t *flags, optimizer::CostVector *cost);
257 virtual int multi_range_read_init(RANGE_SEQ_IF *seq, void *seq_init_param,
258 uint32_t n_ranges, uint32_t mode);
259 virtual int multi_range_read_next(char **range_info);
260
261
262 virtual const key_map *keys_to_use_for_scanning();
263 bool has_transactions();
264
275 virtual bool is_fatal_error(int error, uint32_t flags);
276
281 virtual ha_rows records();
282 virtual uint64_t tableSize();
283 virtual uint64_t rowSize();
291 { return stats.records+EXTRA_RECORDS; }
292
293 virtual const char *index_type(uint32_t)
294 { assert(0); return "";}
295
296
297 uint32_t get_index(void) const { return active_index; }
298 virtual int close(void)=0;
299
306 virtual int index_read_map(unsigned char * buf, const unsigned char *key,
307 key_part_map keypart_map,
308 enum ha_rkey_function find_flag)
309 {
310 uint32_t key_len= calculate_key_len(active_index, keypart_map);
311 return index_read(buf, key, key_len, find_flag);
312 }
313
319 virtual int index_read_idx_map(unsigned char * buf, uint32_t index,
320 const unsigned char * key,
321 key_part_map keypart_map,
322 enum ha_rkey_function find_flag);
323 virtual int index_next(unsigned char *) __attribute__ ((warn_unused_result))
324 { return HA_ERR_WRONG_COMMAND; }
325 virtual int index_prev(unsigned char *)
326 { return HA_ERR_WRONG_COMMAND; }
327 virtual int index_first(unsigned char *)
328 { return HA_ERR_WRONG_COMMAND; }
329 virtual int index_last(unsigned char *)
330 { return HA_ERR_WRONG_COMMAND; }
331 virtual int index_next_same(unsigned char *, const unsigned char *, uint32_t);
332
333private:
334 uint32_t calculate_key_len(uint32_t key_position, key_part_map keypart_map_arg);
335public:
336
342 virtual int index_read_last_map(unsigned char * buf, const unsigned char * key,
343 key_part_map keypart_map)
344 {
345 uint32_t key_len= calculate_key_len(active_index, keypart_map);
346 return index_read_last(buf, key, key_len);
347 }
348 virtual int read_range_first(const key_range *start_key,
349 const key_range *end_key,
350 bool eq_range, bool sorted);
351 virtual int read_range_next();
352 int compare_key(key_range *range);
353 virtual int rnd_next(unsigned char *)=0;
354 virtual int rnd_pos(unsigned char *, unsigned char *)=0;
355 virtual int read_first_row(unsigned char *buf, uint32_t primary_key);
356 virtual int rnd_same(unsigned char *, uint32_t)
357 { return HA_ERR_WRONG_COMMAND; }
358 virtual ha_rows records_in_range(uint32_t, key_range *, key_range *)
359 { return (ha_rows) 10; }
360 virtual void position(const unsigned char *record)=0;
361 virtual int info(uint32_t)=0; // see my_base.h for full description
362 virtual uint32_t calculate_key_hash_value(Field **)
363 { assert(0); return 0; }
364 virtual int extra(enum ha_extra_function)
365 { return 0; }
366 virtual int extra_opt(enum ha_extra_function operation, uint32_t)
367 { return extra(operation); }
368
381 virtual bool was_semi_consistent_read() { return 0; }
388 virtual void try_semi_consistent_read(bool) {}
389 virtual void unlock_row(void) {}
390 virtual void get_auto_increment(uint64_t offset, uint64_t increment,
391 uint64_t nb_desired_values,
392 uint64_t *first_value,
393 uint64_t *nb_reserved_values)= 0;
394
395 void set_next_insert_id(uint64_t id)
396 {
397 next_insert_id= id;
398 }
399 void restore_auto_increment(uint64_t prev_insert_id)
400 {
401 /*
402 Insertion of a row failed, re-use the lastly generated auto_increment
403 id, for the next row. This is achieved by resetting next_insert_id to
404 what it was before the failed insertion (that old value is provided by
405 the caller). If that value was 0, it was the first row of the INSERT;
406 then if insert_id_for_cur_row contains 0 it means no id was generated
407 for this first row, so no id was generated since the INSERT started, so
408 we should set next_insert_id to 0; if insert_id_for_cur_row is not 0, it
409 is the generated id of the first and failed row, so we use it.
410 */
411 next_insert_id= (prev_insert_id > 0) ? prev_insert_id :
412 insert_id_for_cur_row;
413 }
414
415 /* end of the list of admin commands */
416
417 virtual int indexes_are_disabled(void) {return 0;}
418 virtual void append_create_info(String *)
419 {}
430 virtual char* get_foreign_key_create_info(void)
431 { return NULL;} /* gets foreign key create string from InnoDB */
432
435 virtual bool can_switch_engines(void) { return true; }
438 { return 0; }
439 virtual uint32_t referenced_by_foreign_key() { return 0;}
440 virtual void free_foreign_key_create_info(char *) {}
441
456 THR_LOCK_DATA **to,
457 enum thr_lock_type)
458 {
459 assert(0); // Impossible programming situation
460
461 return(to);
462 }
463
464 /*
465 @retval true Primary key (if there is one) is clustered
466 key covering all fields
467 @retval false otherwise
468 */
469 virtual bool primary_key_is_clustered() { return false; }
470 virtual int cmp_ref(const unsigned char *ref1, const unsigned char *ref2)
471 {
472 return memcmp(ref1, ref2, ref_length);
473 }
474
475 virtual bool isOrdered(void)
476 {
477 return false;
478 }
479
480
481protected:
482 /* Service methods for use by storage engines. */
483 void ha_statistic_increment(uint64_t system_status_var::*offset) const;
484 void **ha_data(Session *) const;
485
486private:
487 /* Private helpers */
488 inline void setTransactionReadWrite();
489private:
490 /*
491 Low-level primitives for storage engines. These should be
492 overridden by the storage engine class. To call these methods, use
493 the corresponding 'ha_*' method above.
494 */
495
496 virtual int open(const char *, int , uint32_t ) { assert(0); return -1; }
497 virtual int doOpen(const identifier::Table &identifier, int mode, uint32_t test_if_locked);
498 virtual int doStartIndexScan(uint32_t idx, bool)
499 { active_index= idx; return 0; }
500 virtual int doEndIndexScan() { active_index= MAX_KEY; return 0; }
508 virtual int doStartTableScan(bool scan) __attribute__ ((warn_unused_result)) = 0;
509 virtual int doEndTableScan() { return 0; }
510 virtual int doInsertRecord(unsigned char *)
511 {
512 return HA_ERR_WRONG_COMMAND;
513 }
514
515 virtual int doUpdateRecord(const unsigned char *, unsigned char *)
516 {
517 return HA_ERR_WRONG_COMMAND;
518 }
519
520 virtual int doDeleteRecord(const unsigned char *)
521 {
522 return HA_ERR_WRONG_COMMAND;
523 }
529 virtual int reset() { return 0; }
530
553 virtual int external_lock(Session *, int)
554 {
555 return 0;
556 }
557 virtual void release_auto_increment(void) { return; }
559 virtual int check(Session *)
560 { return HA_ADMIN_NOT_IMPLEMENTED; }
561
562 virtual void start_bulk_insert(ha_rows)
563 {}
564 virtual int end_bulk_insert(void) { return 0; }
565 virtual int index_read(unsigned char *, const unsigned char *,
566 uint32_t, enum ha_rkey_function)
567 { return HA_ERR_WRONG_COMMAND; }
568 virtual int index_read_last(unsigned char *, const unsigned char *, uint32_t)
569 { return (errno= HA_ERR_WRONG_COMMAND); }
576 virtual int delete_all_rows(void)
577 { return (errno=HA_ERR_WRONG_COMMAND); }
578
584 virtual int reset_auto_increment(uint64_t)
585 { return HA_ERR_WRONG_COMMAND; }
586
587 virtual int analyze(Session *)
588 { return HA_ADMIN_NOT_IMPLEMENTED; }
589
590 virtual int disable_indexes(uint32_t)
591 { return HA_ERR_WRONG_COMMAND; }
592
593 virtual int enable_indexes(uint32_t)
594 { return HA_ERR_WRONG_COMMAND; }
595
596 virtual int discard_or_import_tablespace(bool)
597 { return (errno=HA_ERR_WRONG_COMMAND); }
598
599 /*
600 @todo this is just for the HEAP engine, it should
601 be removed at some point in the future (and
602 no new engine should ever use it). Right
603 now HEAP does rely on it, so we cannot remove it.
604 */
605 virtual void drop_table();
606};
607
608/* basic stuff */
609void ha_init_errors(void);
610
611SortField *make_unireg_sortorder(Order *order, uint32_t *length,
612 SortField *sortorder);
613int setup_order(Session *session, Item **ref_pointer_array, TableList *tables,
614 List<Item> &fields, List <Item> &all_fields, Order *order);
615int setup_group(Session *session, Item **ref_pointer_array, TableList *tables,
616 List<Item> &fields, List<Item> &all_fields, Order *order,
617 bool *hidden_group_fields);
618bool fix_inner_refs(Session *session, List<Item> &all_fields, Select_Lex *select,
619 Item **ref_pointer_array);
620
621bool handle_select(Session *session, LEX *lex, select_result *result,
622 uint64_t setup_tables_done_option);
623void free_underlaid_joins(Session *session, Select_Lex *select);
624
625bool handle_derived(LEX *lex, bool (*processor)(Session *session,
626 LEX *lex,
627 TableList *table));
628bool derived_prepare(Session *session, LEX *lex, TableList *t);
629bool derived_filling(Session *session, LEX *lex, TableList *t);
630int prepare_create_field(CreateField *sql_field,
631 uint32_t *blob_columns,
632 int *timestamps, int *timestamps_with_niladic);
633
634bool create_table(Session *session,
635 const identifier::Table &identifier,
636 HA_CREATE_INFO *create_info,
637 message::Table &table_proto,
638 AlterInfo *alter_info,
639 bool tmp_table, uint32_t select_field_count,
640 bool is_if_not_exists);
641
642bool create_table_no_lock(Session *session,
643 const identifier::Table &identifier,
644 HA_CREATE_INFO *create_info,
645 message::Table &table_proto,
646 AlterInfo *alter_info,
647 bool tmp_table, uint32_t select_field_count,
648 bool is_if_not_exists);
649
650bool create_like_table(Session* session,
651 const identifier::Table& destination_identifier,
652 const identifier::Table& source_identifier,
653 message::Table &create_table_proto,
654 bool is_if_not_exists,
655 bool is_engine_set);
656
657bool rename_table(Session &session,
659 const identifier::Table &old_identifier,
660 const identifier::Table &new_identifier);
661
662bool prepare_update(Session *session, TableList *table_list,
663 Item **conds, uint32_t order_num, Order *order);
664int update_query(Session *session,TableList *tables,List<Item> &fields,
665 List<Item> &values,COND *conds,
666 uint32_t order_num, Order *order, ha_rows limit,
667 enum enum_duplicates handle_duplicates, bool ignore);
668bool prepare_insert(Session *session, TableList *table_list, Table *table,
669 List<Item> &fields, List_item *values,
670 List<Item> &update_fields,
671 List<Item> &update_values, enum_duplicates duplic,
672 COND **where, bool select_insert,
673 bool check_fields, bool abort_on_warning);
674bool insert_query(Session *session,TableList *table,List<Item> &fields,
675 List<List_item> &values, List<Item> &update_fields,
676 List<Item> &update_values, enum_duplicates flag,
677 bool ignore);
678int check_that_all_fields_are_given_values(Session *session, Table *entry,
679 TableList *table_list);
680int prepare_delete(Session *session, TableList *table_list, Item **conds);
681bool delete_query(Session *session, TableList *table_list, COND *conds,
682 SQL_LIST *order, ha_rows rows, uint64_t options,
683 bool reset_auto_increment);
684bool truncate(Session& session, TableList *table_list);
685TableShare *get_table_share(Session *session, TableList *table_list, char *key,
686 uint32_t key_length, uint32_t db_flags, int *error);
687TableShare *get_cached_table_share(const char *db, const char *table_name);
688bool reopen_name_locked_table(Session* session, TableList* table_list, bool link_in);
689bool reopen_tables(Session *session,bool get_locks,bool in_refresh);
690void close_handle_and_leave_table_as_lock(Table *table);
691bool wait_for_tables(Session *session);
692bool table_is_used(Table *table, bool wait_for_name_lock);
693Table *drop_locked_tables(Session *session, const drizzled::identifier::Table &identifier);
694void abort_locked_tables(Session *session, const drizzled::identifier::Table &identifier);
695extern Field *not_found_field;
696extern Field *view_ref_found;
697
698Field *
699find_field_in_tables(Session *session, Item_ident *item,
700 TableList *first_table, TableList *last_table,
701 Item **ref, find_item_error_report_type report_error,
702 bool register_tree_change);
703Field *
704find_field_in_table_ref(Session *session, TableList *table_list,
705 const char *name, uint32_t length,
706 const char *item_name, const char *db_name,
707 const char *table_name, Item **ref,
708 bool allow_rowid,
709 uint32_t *cached_field_index_ptr,
710 bool register_tree_change, TableList **actual_table);
711Field *
712find_field_in_table(Session *session, Table *table, const char *name, uint32_t length,
713 bool allow_rowid, uint32_t *cached_field_index_ptr);
714
715} /* namespace drizzled */
716
KEY_MULTI_RANGE mrr_cur_range
virtual THR_LOCK_DATA ** store_lock(Session *, THR_LOCK_DATA **to, enum thr_lock_type)
uint64_t insert_id_for_cur_row
int ha_enable_indexes(uint32_t mode)
int ha_open(const identifier::Table &identifier, int mode, int test_if_locked)
virtual void try_semi_consistent_read(bool)
virtual int delete_all_rows(void)
virtual int index_read_map(unsigned char *buf, const unsigned char *key, key_part_map keypart_map, enum ha_rkey_function find_flag)
Positions an index cursor to the index specified in the handle. Fetches the row if available....
virtual int doStartTableScan(bool scan) __attribute__((warn_unused_result))=0
virtual bool was_semi_consistent_read()
int ha_reset_auto_increment(uint64_t value)
int ha_external_lock(Session *session, int lock_type)
int ha_discard_or_import_tablespace(bool discard)
virtual int reset()
int ha_disable_indexes(uint32_t mode)
virtual char * get_foreign_key_create_info(void)
virtual int reset_auto_increment(uint64_t)
virtual bool can_switch_engines(void)
virtual int index_read_last_map(unsigned char *buf, const unsigned char *key, key_part_map keypart_map)
The following functions works like index_read, but it find the last row with the current key value or...
virtual int external_lock(Session *, int)
virtual int get_foreign_key_list(Session *, List< ForeignKeyInfo > *)
uint64_t getAutoIncrement() const
Discrete_interval auto_inc_interval_for_cur_row
int ha_analyze(Session *)
virtual int check(Session *)
virtual ha_rows estimate_rows_upper_bound()
bool handle_select(Session *session, LEX *lex, select_result *result, uint64_t setup_tables_done_option)
void free_underlaid_joins(Session *session, Select_Lex *select)
int setup_order(Session *session, Item **ref_pointer_array, TableList *tables, List< Item > &fields, List< Item > &all_fields, Order *order)
int setup_group(Session *session, Item **ref_pointer_array, TableList *tables, List< Item > &fields, List< Item > &all_fields, Order *order, bool *hidden_group_fields)
TODO: Rename this file - func.h is stupid.
uint64_t prev_insert_id(uint64_t nr, drizzle_system_variables *variables)
bool delete_query(Session *session, TableList *table_list, COND *conds, SQL_LIST *order, ha_rows rows, uint64_t options, bool reset_auto_increment)
Definition sql_delete.cc:49
void ha_init_errors(void)
bool insert_query(Session *session, TableList *table, List< Item > &fields, List< List_item > &values, List< Item > &update_fields, List< Item > &update_values, enum_duplicates flag, bool ignore)
int update_query(Session *session, TableList *tables, List< Item > &fields, List< Item > &values, COND *conds, uint32_t order_num, Order *order, ha_rows limit, enum enum_duplicates handle_duplicates, bool ignore)
Visibility Control Macros.
#define DRIZZLED_API
Definition visibility.h:38