~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/cursor.h

  • Committer: Brian Aker
  • Date: 2009-11-24 02:06:37 UTC
  • mfrom: (1223.1.7 push)
  • Revision ID: brian@gaz-20091124020637-9gb65vj98x1arydm
Merge for staging.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
3
 *
4
 
 *  Copyright (C) 2008 Sun Microsystems, Inc.
 
4
 *  Copyright (C) 2008 Sun Microsystems
5
5
 *
6
6
 *  This program is free software; you can redistribute it and/or modify
7
7
 *  it under the terms of the GNU General Public License as published by
22
22
 
23
23
#include <drizzled/xid.h>
24
24
#include <drizzled/discrete_interval.h>
25
 
#include <drizzled/identifier.h>
26
 
#include <drizzled/definitions.h>
27
 
#include <drizzled/key_map.h>
28
 
#include <drizzled/error_t.h>
29
25
 
30
26
/* Definitions for parameters to do with Cursor-routines */
31
27
 
32
 
#include <drizzled/thr_lock.h>
 
28
#include <mysys/thr_lock.h>
 
29
#include <mysys/hash.h>
 
30
#include <drizzled/sql_string.h>
33
31
#include <drizzled/sql_list.h>
 
32
#include <drizzled/plugin/storage_engine.h>
34
33
#include <drizzled/handler_structs.h>
35
34
#include <drizzled/ha_statistics.h>
36
35
#include <drizzled/atomics.h>
37
36
 
38
 
#include <drizzled/message/table.h>
 
37
#include <drizzled/message/table.pb.h>
 
38
 
 
39
/* Bits to show what an alter table will do */
 
40
#include <drizzled/sql_bitmap.h>
 
41
 
 
42
#include <drizzled/cursor.h>
39
43
 
40
44
#include <bitset>
41
45
#include <algorithm>
42
46
 
43
 
#include "drizzled/visibility.h"
44
 
 
45
 
namespace drizzled
46
 
{
47
 
 
48
47
#define HA_MAX_ALTER_FLAGS 40
49
48
 
 
49
 
50
50
typedef std::bitset<HA_MAX_ALTER_FLAGS> HA_ALTER_FLAGS;
51
51
 
52
 
class AlterInfo;
53
 
class CreateField;
54
 
class ForeignKeyInfo;
 
52
extern uint64_t refresh_version;  /* Increments on each reload */
 
53
 
 
54
 
 
55
typedef bool (*qc_engine_callback)(Session *session, char *table_key,
 
56
                                      uint32_t key_length,
 
57
                                      uint64_t *engine_data);
 
58
 
 
59
 
 
60
/* The Cursor for a table type.  Will be included in the Table structure */
 
61
 
 
62
class Table;
 
63
class TableList;
 
64
class TableShare;
 
65
class Select_Lex_Unit;
 
66
struct st_foreign_key_info;
 
67
typedef struct st_foreign_key_info FOREIGN_KEY_INFO;
 
68
struct order_st;
 
69
 
55
70
class Item;
56
 
class Item_ident;
 
71
struct st_table_log_memory_entry;
 
72
 
57
73
class LEX;
58
74
class Select_Lex;
59
 
class Select_Lex_Unit;
60
 
class String;
61
 
class Table;
62
 
class TableList;
63
 
class TableShare;
 
75
class AlterInfo;
64
76
class select_result;
 
77
class CreateField;
65
78
class sys_var_str;
66
 
struct Order;
 
79
class Item_ident;
 
80
typedef struct st_sort_field SORT_FIELD;
67
81
 
68
82
typedef List<Item> List_item;
 
83
 
 
84
typedef struct st_savepoint SAVEPOINT;
 
85
extern uint32_t savepoint_alloc_size;
69
86
extern KEY_CREATE_INFO default_key_create_info;
70
87
 
71
88
/* Forward declaration for condition pushdown to storage engine */
72
89
typedef class Item COND;
73
90
 
74
 
typedef struct system_status_var system_status_var;
75
 
 
76
 
namespace optimizer { class CostVector; }
77
 
namespace plugin { class StorageEngine; }
78
 
 
 
91
typedef struct system_status_var SSV;
 
92
 
 
93
class COST_VECT;
 
94
 
 
95
uint32_t calculate_key_len(Table *, uint, const unsigned char *, key_part_map);
79
96
/*
80
97
  bitmap with first N+1 bits set
81
98
  (keypart_map for a key prefix of [0..N] keyparts)
107
124
     storage engine
108
125
 
109
126
  2. KeyTupleFormat - used to pass index search tuples (aka "keys") to
110
 
     storage engine. See optimizer/range.cc for description of this format.
 
127
     storage engine. See opt_range.cc for description of this format.
111
128
 
112
129
  TableRecordFormat
113
130
  =================
132
149
  present, its length is one byte <not-sure> which must be set to 0xFF
133
150
  at all times. </not-sure>
134
151
 
 
152
  If the table has columns of type BIT, then certain bits from those columns
 
153
  may be stored in null_bytes as well. Grep around for Field_bit for
 
154
  details.
 
155
 
135
156
  For blob columns (see Field_blob), the record buffer stores length of the
136
157
  data, following by memory pointer to the blob data. The pointer is owned
137
158
  by the storage engine and is valid until the next operation.
139
160
  If a blob column has NULL value, then its length and blob data pointer
140
161
  must be set to 0.
141
162
*/
142
 
class DRIZZLED_API Cursor
 
163
 
 
164
class Cursor :public Sql_alloc
143
165
{
144
 
  friend class SEAPITesterCursor;
145
 
  Table &table;               /* The current open table */
146
 
  plugin::StorageEngine &engine;      /* storage engine of this Cursor */
147
 
 
148
166
protected:
 
167
  TableShare *table_share;   /* The table definition */
 
168
  Table *table;               /* The current open table */
 
169
 
149
170
  ha_rows estimation_rows_to_insert;
150
 
 
151
171
public:
152
 
  inline plugin::StorageEngine *getEngine() const       /* table_type for handler */
153
 
  {
154
 
    return &engine;
155
 
  }
156
 
  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 */
157
174
  unsigned char *dup_ref;                       /* Pointer to duplicate row */
158
175
 
159
 
  TableShare *getShare();
160
 
 
161
 
  Table *getTable() const
162
 
  {
163
 
    return &table;
164
 
  }
165
 
 
166
176
  ha_statistics stats;
167
177
  /** MultiRangeRead-related members: */
168
178
  range_seq_t mrr_iter;    /* Interator to traverse the range sequence */
169
179
  RANGE_SEQ_IF mrr_funcs;  /* Range sequence traversal functions */
170
 
 
 
180
  HANDLER_BUFFER *multi_range_buffer; /* MRR buffer info */
171
181
  uint32_t ranges_in_seq; /* Total number of ranges in the traversed sequence */
172
182
  /* true <=> source MRR ranges and the output are ordered */
173
183
  bool mrr_is_output_sorted;
176
186
  bool mrr_have_range;
177
187
 
178
188
  bool eq_range;
 
189
  /*
 
190
    true <=> the engine guarantees that returned records are within the range
 
191
    being scanned.
 
192
  */
 
193
  bool in_range_check_pushed_down;
179
194
 
180
195
  /** Current range (the one we're now returning rows from) */
181
196
  KEY_MULTI_RANGE mrr_cur_range;
182
197
 
183
198
  /** The following are for read_range() */
184
199
  key_range save_end_range, *end_range;
185
 
  KeyPartInfo *range_key_part;
 
200
  KEY_PART_INFO *range_key_part;
186
201
  int key_compare_result_on_equal;
187
202
 
188
203
  uint32_t errkey;                              /* Last dup key */
192
207
  uint32_t ref_length;
193
208
  enum {NONE=0, INDEX, RND} inited;
194
209
  bool locked;
 
210
  bool implicit_emptied;                /* Can be !=0 only if HEAP */
195
211
 
196
212
  /**
197
213
    next_insert_id is the next value which should be inserted into the
203
219
    get_auto_increment().
204
220
  */
205
221
  uint64_t next_insert_id;
206
 
  uint64_t getNextInsertId()
207
 
  {
208
 
    return next_insert_id;
209
 
  }
210
 
 
211
 
  /**
212
 
    Used by SHOW TABLE STATUS to get the current auto_inc from the engine
213
 
  */
214
 
  uint64_t getAutoIncrement()
215
 
  {
216
 
    return stats.auto_increment_value;
217
 
  }
218
 
 
219
222
  /**
220
223
    insert id for the current row (*autogenerated*; if not
221
224
    autogenerated, it's 0).
229
232
  */
230
233
  Discrete_interval auto_inc_interval_for_cur_row;
231
234
 
232
 
  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)),
 
241
    inited(NONE),
 
242
    locked(false), implicit_emptied(0),
 
243
    next_insert_id(0), insert_id_for_cur_row(0)
 
244
    {}
233
245
  virtual ~Cursor(void);
234
 
  virtual Cursor *clone(memory::Root *mem_root);
 
246
  virtual Cursor *clone(MEM_ROOT *mem_root);
235
247
 
236
248
  /* ha_ methods: pubilc wrappers for private virtual API */
237
249
 
238
 
  int ha_open(const identifier::Table &identifier, int mode, int test_if_locked);
239
 
  int startIndexScan(uint32_t idx, bool sorted) __attribute__ ((warn_unused_result));
240
 
  int endIndexScan();
241
 
  int startTableScan(bool scan) __attribute__ ((warn_unused_result));
242
 
  int endTableScan();
 
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);
 
252
  int ha_index_end();
 
253
  int ha_rnd_init(bool scan);
 
254
  int ha_rnd_end();
243
255
  int ha_reset();
244
256
 
245
257
  /* this is necessary in many places, e.g. in HANDLER command */
246
258
  int ha_index_or_rnd_end();
 
259
  drizzled::plugin::StorageEngine::Table_flags ha_table_flags() const;
247
260
 
248
261
  /**
249
262
    These functions represent the public interface to *users* of the
250
263
    Cursor class, hence they are *not* virtual. For the inheritance
251
 
    interface, see the (private) functions doInsertRecord(), doUpdateRecord(),
252
 
    and doDeleteRecord() below.
 
264
    interface, see the (private) functions write_row(), update_row(),
 
265
    and delete_row() below.
253
266
  */
254
267
  int ha_external_lock(Session *session, int lock_type);
255
 
  int insertRecord(unsigned char * buf) __attribute__ ((warn_unused_result));
256
 
  int updateRecord(const unsigned char * old_data, unsigned char * new_data) __attribute__ ((warn_unused_result));
257
 
  int deleteRecord(const unsigned char * buf) __attribute__ ((warn_unused_result));
 
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);
258
271
  void ha_release_auto_increment();
259
272
 
260
273
  /** to be actually called to get 'check()' functionality*/
262
275
 
263
276
  void ha_start_bulk_insert(ha_rows rows);
264
277
  int ha_end_bulk_insert();
 
278
  int ha_bulk_update_row(const unsigned char *old_data, unsigned char *new_data,
 
279
                         uint32_t *dup_key_found);
265
280
  int ha_delete_all_rows();
266
281
  int ha_reset_auto_increment(uint64_t value);
 
282
  int ha_optimize(Session* session, HA_CHECK_OPT* check_opt);
267
283
  int ha_analyze(Session* session, HA_CHECK_OPT* check_opt);
268
284
 
269
285
  int ha_disable_indexes(uint32_t mode);
273
289
 
274
290
  void adjust_next_insert_id_after_explicit_value(uint64_t nr);
275
291
  int update_auto_increment();
 
292
  virtual void change_table_ptr(Table *table_arg, TableShare *share);
276
293
 
277
294
  /* Estimates calculation */
278
295
  virtual double scan_time(void)
285
302
  virtual ha_rows multi_range_read_info_const(uint32_t keyno, RANGE_SEQ_IF *seq,
286
303
                                              void *seq_init_param,
287
304
                                              uint32_t n_ranges, uint32_t *bufsz,
288
 
                                              uint32_t *flags, optimizer::CostVector *cost);
 
305
                                              uint32_t *flags, COST_VECT *cost);
289
306
  virtual int multi_range_read_info(uint32_t keyno, uint32_t n_ranges, uint32_t keys,
290
 
                                    uint32_t *bufsz, uint32_t *flags, optimizer::CostVector *cost);
 
307
                                    uint32_t *bufsz, uint32_t *flags, COST_VECT *cost);
291
308
  virtual int multi_range_read_init(RANGE_SEQ_IF *seq, void *seq_init_param,
292
 
                                    uint32_t n_ranges, uint32_t mode);
 
309
                                    uint32_t n_ranges, uint32_t mode,
 
310
                                    HANDLER_BUFFER *buf);
293
311
  virtual int multi_range_read_next(char **range_info);
294
312
 
295
313
 
313
331
    (table_flags() & (HA_HAS_RECORDS | HA_STATS_RECORDS_IS_EXACT)) != 0
314
332
  */
315
333
  virtual ha_rows records();
316
 
  virtual uint64_t tableSize();
317
 
  virtual uint64_t rowSize();
318
334
  /**
319
335
    Return upper bound of current number of records in the table
320
336
    (max. of how many records one will retrieve when doing a full table scan)
324
340
  virtual ha_rows estimate_rows_upper_bound()
325
341
  { return stats.records+EXTRA_RECORDS; }
326
342
 
 
343
  /**
 
344
    Get the row type from the storage engine.  If this method returns
 
345
    ROW_TYPE_NOT_USED, the information in HA_CREATE_INFO should be used.
 
346
  */
 
347
  virtual enum row_type get_row_type() const { return ROW_TYPE_NOT_USED; }
 
348
 
327
349
  virtual const char *index_type(uint32_t)
328
350
  { assert(0); return "";}
329
351
 
332
354
  virtual int close(void)=0;
333
355
 
334
356
  /**
 
357
    @retval  0   Bulk update used by Cursor
 
358
    @retval  1   Bulk update not used, normal operation used
 
359
  */
 
360
  virtual bool start_bulk_update() { return 1; }
 
361
  /**
 
362
    @retval  0   Bulk delete used by Cursor
 
363
    @retval  1   Bulk delete not used, normal operation used
 
364
  */
 
365
  virtual bool start_bulk_delete() { return 1; }
 
366
  /**
 
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.
 
371
 
 
372
    @param    dup_key_found       Number of duplicate keys found
 
373
 
 
374
    @retval  0           Success
 
375
    @retval  >0          Error code
 
376
  */
 
377
  virtual int exec_bulk_update(uint32_t *)
 
378
  {
 
379
    assert(false);
 
380
    return HA_ERR_WRONG_COMMAND;
 
381
  }
 
382
  /**
 
383
    Perform any needed clean-up, no outstanding updates are there at the
 
384
    moment.
 
385
  */
 
386
  virtual void end_bulk_update() { return; }
 
387
  /**
 
388
    Execute all outstanding deletes and close down the bulk delete.
 
389
 
 
390
    @retval 0             Success
 
391
    @retval >0            Error code
 
392
  */
 
393
  virtual int end_bulk_delete()
 
394
  {
 
395
    assert(false);
 
396
    return HA_ERR_WRONG_COMMAND;
 
397
  }
 
398
  /**
335
399
     @brief
336
400
     Positions an index cursor to the index specified in the handle. Fetches the
337
401
     row if available. If the key value is null, begin at the first key of the
338
402
     index.
339
403
  */
340
 
  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,
341
405
                             key_part_map keypart_map,
342
406
                             enum ha_rkey_function find_flag)
343
407
  {
344
 
    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);
345
409
    return  index_read(buf, key, key_len, find_flag);
346
410
  }
347
411
  /**
354
418
                                 const unsigned char * key,
355
419
                                 key_part_map keypart_map,
356
420
                                 enum ha_rkey_function find_flag);
357
 
  virtual int index_next(unsigned char *) __attribute__ ((warn_unused_result))
 
421
  virtual int index_next(unsigned char *)
358
422
   { return  HA_ERR_WRONG_COMMAND; }
359
423
  virtual int index_prev(unsigned char *)
360
424
   { return  HA_ERR_WRONG_COMMAND; }
363
427
  virtual int index_last(unsigned char *)
364
428
   { return  HA_ERR_WRONG_COMMAND; }
365
429
  virtual int index_next_same(unsigned char *, const unsigned char *, uint32_t);
366
 
 
367
 
private:
368
 
  uint32_t calculate_key_len(uint32_t key_position, key_part_map keypart_map_arg);
369
 
public:
370
 
 
371
430
  /**
372
431
     @brief
373
432
     The following functions works like index_read, but it find the last
376
435
  virtual int index_read_last_map(unsigned char * buf, const unsigned char * key,
377
436
                                  key_part_map keypart_map)
378
437
  {
379
 
    uint32_t key_len= calculate_key_len(active_index, keypart_map);
 
438
    uint32_t key_len= calculate_key_len(table, active_index, key, keypart_map);
380
439
    return index_read_last(buf, key, key_len);
381
440
  }
382
441
  virtual int read_range_first(const key_range *start_key,
384
443
                               bool eq_range, bool sorted);
385
444
  virtual int read_range_next();
386
445
  int compare_key(key_range *range);
 
446
  int compare_key2(key_range *range);
387
447
  virtual int rnd_next(unsigned char *)=0;
388
448
  virtual int rnd_pos(unsigned char *, unsigned char *)=0;
 
449
  /**
 
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.
 
454
  */
 
455
  virtual int rnd_pos_by_record(unsigned char *record);
389
456
  virtual int read_first_row(unsigned char *buf, uint32_t primary_key);
 
457
  /**
 
458
    The following function is only needed for tables that may be temporary
 
459
    tables during joins.
 
460
  */
 
461
  virtual int restart_rnd_next(unsigned char *, unsigned char *)
 
462
    { return HA_ERR_WRONG_COMMAND; }
390
463
  virtual int rnd_same(unsigned char *, uint32_t)
391
464
    { return HA_ERR_WRONG_COMMAND; }
392
465
  virtual ha_rows records_in_range(uint32_t, key_range *, key_range *)
421
494
  */
422
495
  virtual void try_semi_consistent_read(bool) {}
423
496
  virtual void unlock_row(void) {}
 
497
  virtual int start_stmt(Session *, thr_lock_type)
 
498
  {return 0;}
424
499
  virtual void get_auto_increment(uint64_t offset, uint64_t increment,
425
500
                                  uint64_t nb_desired_values,
426
501
                                  uint64_t *first_value,
427
 
                                  uint64_t *nb_reserved_values)= 0;
428
 
 
 
502
                                  uint64_t *nb_reserved_values);
429
503
  void set_next_insert_id(uint64_t id)
430
504
  {
431
505
    next_insert_id= id;
446
520
      insert_id_for_cur_row;
447
521
  }
448
522
 
 
523
  virtual void update_create_info(HA_CREATE_INFO *) {}
 
524
  int check_old_types(void);
449
525
  /* end of the list of admin commands */
450
526
 
451
527
  virtual int indexes_are_disabled(void) {return 0;}
468
544
   */
469
545
  virtual bool can_switch_engines(void) { return true; }
470
546
  /** used in REPLACE; is > 0 if table is referred by a FOREIGN KEY */
471
 
  virtual int get_foreign_key_list(Session *, List<ForeignKeyInfo> *)
 
547
  virtual int get_foreign_key_list(Session *, List<FOREIGN_KEY_INFO> *)
472
548
  { return 0; }
473
549
  virtual uint32_t referenced_by_foreign_key() { return 0;}
474
550
  virtual void free_foreign_key_create_info(char *) {}
 
551
  /** The following can be called without an open Cursor */
 
552
 
 
553
  virtual uint32_t index_flags(uint32_t idx, uint32_t part, bool all_parts) const =0;
 
554
 
 
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); }
 
561
 
 
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()); }
 
572
 
 
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; }
 
579
 
 
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; }
475
584
 
476
585
  /**
477
586
    Is not invoked for non-transactional temporary tables.
480
589
    or partitioned.
481
590
 
482
591
    @note that one can NOT rely on table->in_use in store_lock().  It may
483
 
    refer to a different thread if called from abortLockForThread().
 
592
    refer to a different thread if called from mysql_lock_abort_for_thread().
484
593
 
485
594
    @note If the table is MERGE, store_lock() can return less locks
486
595
    than lock_count() claimed. This can happen when the MERGE children
514
623
 
515
624
protected:
516
625
  /* Service methods for use by storage engines. */
517
 
  void ha_statistic_increment(uint64_t system_status_var::*offset) const;
 
626
  void ha_statistic_increment(ulong SSV::*offset) const;
518
627
  void **ha_data(Session *) const;
 
628
  Session *ha_session(void) const;
519
629
 
520
630
private:
521
631
  /* Private helpers */
522
 
  inline void setTransactionReadWrite();
 
632
  inline void mark_trx_read_write();
523
633
private:
524
634
  /*
525
635
    Low-level primitives for storage engines.  These should be
527
637
    the corresponding 'ha_*' method above.
528
638
  */
529
639
 
530
 
  virtual int open(const char *, int , uint32_t ) { assert(0); return -1; }
531
 
  virtual int doOpen(const identifier::Table &identifier, int mode, uint32_t test_if_locked);
532
 
  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)
533
642
  { active_index= idx; return 0; }
534
 
  virtual int doEndIndexScan() { active_index= MAX_KEY; return 0; }
 
643
  virtual int index_end() { active_index= MAX_KEY; return 0; }
535
644
  /**
536
 
    doStartTableScan() can be called two times without doEndTableScan() in between
 
645
    rnd_init() can be called two times without rnd_end() in between
537
646
    (it only makes sense if scan=1).
538
647
    then the second call should prepare for the new table scan (e.g
539
648
    if rnd_init allocates the cursor, second call should position it
540
649
    to the start of the table, no need to deallocate and allocate it again
541
650
  */
542
 
  virtual int doStartTableScan(bool scan) __attribute__ ((warn_unused_result)) = 0;
543
 
  virtual int doEndTableScan() { return 0; }
544
 
  virtual int doInsertRecord(unsigned char *)
545
 
  {
546
 
    return HA_ERR_WRONG_COMMAND;
547
 
  }
548
 
 
549
 
  virtual int doUpdateRecord(const unsigned char *, unsigned char *)
550
 
  {
551
 
    return HA_ERR_WRONG_COMMAND;
552
 
  }
553
 
 
554
 
  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 *)
 
654
  {
 
655
    return HA_ERR_WRONG_COMMAND;
 
656
  }
 
657
 
 
658
  virtual int update_row(const unsigned char *, unsigned char *)
 
659
  {
 
660
    return HA_ERR_WRONG_COMMAND;
 
661
  }
 
662
 
 
663
  virtual int delete_row(const unsigned char *)
555
664
  {
556
665
    return HA_ERR_WRONG_COMMAND;
557
666
  }
588
697
  {
589
698
    return 0;
590
699
  }
591
 
  virtual void release_auto_increment(void) { return; }
 
700
  virtual void release_auto_increment(void) { return; };
592
701
  /** admin commands - called from mysql_admin_table */
593
702
  virtual int check(Session *)
594
703
  { return HA_ADMIN_NOT_IMPLEMENTED; }
600
709
                         uint32_t, enum ha_rkey_function)
601
710
   { return  HA_ERR_WRONG_COMMAND; }
602
711
  virtual int index_read_last(unsigned char *, const unsigned char *, uint32_t)
603
 
   { return (errno= HA_ERR_WRONG_COMMAND); }
 
712
   { return (my_errno= HA_ERR_WRONG_COMMAND); }
 
713
  /**
 
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.
 
718
 
 
719
    @param    old_data       Old record
 
720
    @param    new_data       New record
 
721
    @param    dup_key_found  Number of duplicate keys found
 
722
 
 
723
    @retval  0   Bulk delete used by Cursor
 
724
    @retval  1   Bulk delete not used, normal operation used
 
725
  */
 
726
  virtual int bulk_update_row(const unsigned char *, unsigned char *, uint32_t *)
 
727
  {
 
728
    assert(false);
 
729
    return HA_ERR_WRONG_COMMAND;
 
730
  }
604
731
  /**
605
732
    This is called to delete all rows in a table
606
733
    If the Cursor don't support this, then this function will
608
735
    by one.
609
736
  */
610
737
  virtual int delete_all_rows(void)
611
 
  { return (errno=HA_ERR_WRONG_COMMAND); }
 
738
  { return (my_errno=HA_ERR_WRONG_COMMAND); }
612
739
  /**
613
740
    Reset the auto-increment counter to the given value, i.e. the next row
614
741
    inserted will get the given value. This is called e.g. after TRUNCATE
618
745
  virtual int reset_auto_increment(uint64_t)
619
746
  { return HA_ERR_WRONG_COMMAND; }
620
747
 
 
748
  virtual int optimize(Session *)
 
749
  { return HA_ADMIN_NOT_IMPLEMENTED; }
 
750
 
621
751
  virtual int analyze(Session *)
622
752
  { return HA_ADMIN_NOT_IMPLEMENTED; }
623
753
 
628
758
  { return HA_ERR_WRONG_COMMAND; }
629
759
 
630
760
  virtual int discard_or_import_tablespace(bool)
631
 
  { return (errno=HA_ERR_WRONG_COMMAND); }
 
761
  { return (my_errno=HA_ERR_WRONG_COMMAND); }
632
762
 
633
763
  /* 
634
764
    @todo this is just for the HEAP engine, it should
640
770
};
641
771
 
642
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;
 
777
 
 
778
       /* Wrapper functions */
 
779
#define ha_commit(session) (ha_commit_trans((session), true))
 
780
#define ha_rollback(session) (ha_rollback_trans((session), true))
643
781
 
644
782
/* basic stuff */
645
 
void ha_init_errors(void);
646
 
 
647
 
class SortField;
648
 
SortField *make_unireg_sortorder(Order *order, uint32_t *length,
649
 
                                 SortField *sortorder);
 
783
int ha_init_errors(void);
 
784
int ha_init(void);
 
785
int ha_end(void);
 
786
 
 
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);
 
790
 
 
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);
 
795
 
 
796
/* savepoints */
 
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);
 
800
 
 
801
/* these are called by storage engines */
 
802
void trans_register_ha(Session *session, bool all, drizzled::plugin::StorageEngine *engine);
 
803
 
 
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);
 
806
 
 
807
 
 
808
/*
 
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
 
812
*/
 
813
#define trans_need_2pc(session, all)                   ((total_ha_2pc > 1) && \
 
814
        !((all ? &session->transaction.all : &session->transaction.stmt)->no_2pc))
 
815
 
 
816
 
 
817
bool mysql_xa_recover(Session *session);
 
818
 
 
819
SORT_FIELD * make_unireg_sortorder(order_st *order, uint32_t *length,
 
820
                                   SORT_FIELD *sortorder);
650
821
int setup_order(Session *session, Item **ref_pointer_array, TableList *tables,
651
 
                List<Item> &fields, List <Item> &all_fields, Order *order);
 
822
                List<Item> &fields, List <Item> &all_fields, order_st *order);
652
823
int setup_group(Session *session, Item **ref_pointer_array, TableList *tables,
653
 
                List<Item> &fields, List<Item> &all_fields, Order *order,
 
824
                List<Item> &fields, List<Item> &all_fields, order_st *order,
654
825
                bool *hidden_group_fields);
655
826
bool fix_inner_refs(Session *session, List<Item> &all_fields, Select_Lex *select,
656
827
                    Item **ref_pointer_array);
657
828
 
658
829
bool handle_select(Session *session, LEX *lex, select_result *result,
659
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);
660
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);
661
842
 
662
 
bool handle_derived(LEX *lex, bool (*processor)(Session *session,
 
843
bool mysql_handle_derived(LEX *lex, bool (*processor)(Session *session,
663
844
                                                      LEX *lex,
664
845
                                                      TableList *table));
665
 
bool derived_prepare(Session *session, LEX *lex, TableList *t);
666
 
bool derived_filling(Session *session, LEX *lex, TableList *t);
 
846
bool mysql_derived_prepare(Session *session, LEX *lex, TableList *t);
 
847
bool mysql_derived_filling(Session *session, LEX *lex, TableList *t);
667
848
int prepare_create_field(CreateField *sql_field,
668
849
                         uint32_t *blob_columns,
669
 
                         int *timestamps, int *timestamps_with_niladic);
670
 
 
671
 
bool create_table(Session *session,
672
 
                  const identifier::Table &identifier,
673
 
                  HA_CREATE_INFO *create_info,
674
 
                  message::Table &table_proto,
675
 
                  AlterInfo *alter_info,
676
 
                  bool tmp_table, uint32_t select_field_count,
677
 
                  bool is_if_not_exists);
678
 
 
679
 
bool create_table_no_lock(Session *session,
680
 
                          const identifier::Table &identifier,
681
 
                          HA_CREATE_INFO *create_info,
682
 
                          message::Table &table_proto,
683
 
                          AlterInfo *alter_info,
684
 
                          bool tmp_table, uint32_t select_field_count,
685
 
                          bool is_if_not_exists);
686
 
 
687
 
bool create_like_table(Session* session,
688
 
                       identifier::Table::const_reference destination_identifier,
689
 
                       identifier::Table::const_reference source_identifier,
690
 
                       message::Table &create_table_proto,
691
 
                       bool is_if_not_exists,
692
 
                       bool is_engine_set);
693
 
 
694
 
bool rename_table(Session &session,
695
 
                        plugin::StorageEngine *base,
696
 
                        const identifier::Table &old_identifier,
697
 
                        const identifier::Table &new_identifier);
698
 
 
699
 
bool prepare_update(Session *session, TableList *table_list,
700
 
                          Item **conds, uint32_t order_num, Order *order);
701
 
int update_query(Session *session,TableList *tables,List<Item> &fields,
 
850
                         int *timestamps, int *timestamps_with_niladic,
 
851
                         int64_t table_flags);
 
852
 
 
853
bool mysql_create_table(Session *session,const char *db, const char *table_name,
 
854
                        HA_CREATE_INFO *create_info,
 
855
                        drizzled::message::Table *table_proto,
 
856
                        AlterInfo *alter_info,
 
857
                        bool tmp_table, uint32_t select_field_count,
 
858
                        bool is_if_not_exists);
 
859
 
 
860
bool mysql_create_table_no_lock(Session *session, const char *db,
 
861
                                const char *table_name,
 
862
                                HA_CREATE_INFO *create_info,
 
863
                                drizzled::message::Table *table_proto,
 
864
                                AlterInfo *alter_info,
 
865
                                bool tmp_table, uint32_t select_field_count, 
 
866
                                bool is_if_not_exists);
 
867
 
 
868
bool mysql_recreate_table(Session *session, TableList *table_list);
 
869
 
 
870
bool mysql_create_like_table(Session* session, TableList* table, TableList* src_table,
 
871
                             drizzled::message::Table& create_table_proto,
 
872
                             drizzled::plugin::StorageEngine*,
 
873
                             bool is_if_not_exists,
 
874
                             bool is_engine_set);
 
875
 
 
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);
 
879
 
 
880
bool mysql_prepare_update(Session *session, TableList *table_list,
 
881
                          Item **conds, uint32_t order_num, order_st *order);
 
882
int mysql_update(Session *session,TableList *tables,List<Item> &fields,
702
883
                 List<Item> &values,COND *conds,
703
 
                 uint32_t order_num, Order *order, ha_rows limit,
 
884
                 uint32_t order_num, order_st *order, ha_rows limit,
704
885
                 enum enum_duplicates handle_duplicates, bool ignore);
705
 
bool prepare_insert(Session *session, TableList *table_list, Table *table,
 
886
bool mysql_prepare_insert(Session *session, TableList *table_list, Table *table,
706
887
                          List<Item> &fields, List_item *values,
707
888
                          List<Item> &update_fields,
708
889
                          List<Item> &update_values, enum_duplicates duplic,
709
890
                          COND **where, bool select_insert,
710
891
                          bool check_fields, bool abort_on_warning);
711
 
bool insert_query(Session *session,TableList *table,List<Item> &fields,
 
892
bool mysql_insert(Session *session,TableList *table,List<Item> &fields,
712
893
                  List<List_item> &values, List<Item> &update_fields,
713
894
                  List<Item> &update_values, enum_duplicates flag,
714
895
                  bool ignore);
715
896
int check_that_all_fields_are_given_values(Session *session, Table *entry,
716
897
                                           TableList *table_list);
717
 
int prepare_delete(Session *session, TableList *table_list, Item **conds);
718
 
bool delete_query(Session *session, TableList *table_list, COND *conds,
 
898
int mysql_prepare_delete(Session *session, TableList *table_list, Item **conds);
 
899
bool mysql_delete(Session *session, TableList *table_list, COND *conds,
719
900
                  SQL_LIST *order, ha_rows rows, uint64_t options,
720
901
                  bool reset_auto_increment);
721
 
bool truncate(Session& session, TableList *table_list);
 
902
bool mysql_truncate(Session& session, TableList *table_list);
722
903
TableShare *get_table_share(Session *session, TableList *table_list, char *key,
723
904
                             uint32_t key_length, uint32_t db_flags, int *error);
724
905
TableShare *get_cached_table_share(const char *db, const char *table_name);
725
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);
726
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);
727
915
void close_handle_and_leave_table_as_lock(Table *table);
728
916
bool wait_for_tables(Session *session);
729
917
bool table_is_used(Table *table, bool wait_for_name_lock);
730
 
Table *drop_locked_tables(Session *session, const drizzled::identifier::Table &identifier);
731
 
void abort_locked_tables(Session *session, const drizzled::identifier::Table &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);
732
920
extern Field *not_found_field;
733
921
extern Field *view_ref_found;
734
922
 
748
936
Field *
749
937
find_field_in_table(Session *session, Table *table, const char *name, uint32_t length,
750
938
                    bool allow_rowid, uint32_t *cached_field_index_ptr);
 
939
Field *
 
940
find_field_in_table_sef(Table *table, const char *name);
751
941
 
752
 
} /* namespace drizzled */
753
942
 
754
943
#endif /* DRIZZLED_CURSOR_H */