~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/innobase/handler/ha_innodb.h

  • Committer: Stewart Smith
  • Date: 2009-05-15 06:57:12 UTC
  • mto: (991.1.5 for-brian)
  • mto: This revision was merged to the branch mainline in revision 1022.
  • Revision ID: stewart@flamingspork.com-20090515065712-bmionylacjmexmmm
Make sql_mode=NO_AUTO_VALUE_ON_ZERO default for Drizzle.

Also fix DEFAULT keyword handling for auto-increment so that it defaults to
NULL and not 0 so that the following is valid and generates two auto-inc
values:

create table t1 (a int auto_increment primary key)
insert into t1 (a) values (default);
insert into t1 (a) values (default);

Important to note that 0 is no longer magic. So this gives you duplicate
primary key error:

insert into t1 (a) values(0);
insert into t1 (a) values(0);

as you've inserted the explicit value of 0 twice.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*****************************************************************************
2
2
 
3
 
Copyright (C) 2000, 2010, MySQL AB & Innobase Oy. All Rights Reserved.
 
3
Copyright (c) 2000, 2009, MySQL AB & Innobase Oy. All Rights Reserved.
4
4
 
5
5
This program is free software; you can redistribute it and/or modify it under
6
6
the terms of the GNU General Public License as published by the Free Software
11
11
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12
12
 
13
13
You should have received a copy of the GNU General Public License along with
14
 
this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
15
 
St, Fifth Floor, Boston, MA 02110-1301 USA
 
14
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 
15
Place, Suite 330, Boston, MA 02111-1307 USA
16
16
 
17
17
*****************************************************************************/
18
18
 
19
19
/*
20
20
  This file is based on ha_berkeley.h of MySQL distribution
21
21
 
22
 
  This file defines the Innodb Cursor: the interface between MySQL and
 
22
  This file defines the Innodb handler: the interface between MySQL and
23
23
  Innodb
24
24
*/
25
25
 
26
26
#ifndef INNODB_HANDLER_HA_INNODB_H
27
27
#define INNODB_HANDLER_HA_INNODB_H
28
28
 
29
 
#include <drizzled/cursor.h>
30
 
#include <drizzled/thr_lock.h>
31
 
#include <drizzled/plugin/transactional_storage_engine.h>
32
 
 
33
 
using namespace drizzled;
34
 
 
35
 
/* Structure defines translation table between mysql index and innodb
36
 
index structures */
37
 
typedef struct innodb_idx_translate_struct {
38
 
        ulint           index_count;    /*!< number of valid index entries
39
 
                                        in the index_mapping array */
40
 
        ulint           array_size;     /*!< array size of index_mapping */
41
 
        dict_index_t**  index_mapping;  /*!< index pointer array directly
42
 
                                        maps to index in Innodb from MySQL
43
 
                                        array index */
44
 
} innodb_idx_translate_t;
45
 
 
46
 
/** InnoDB table share */
 
29
#include <drizzled/handler.h>
 
30
#include <mysys/thr_lock.h>
 
31
 
 
32
#ifdef USE_PRAGMA_INTERFACE
 
33
#pragma interface                       /* gcc class implementation */
 
34
#endif
 
35
 
47
36
typedef struct st_innobase_share {
48
 
        THR_LOCK        lock;           /*!< MySQL lock protecting
49
 
                                        this structure */
50
 
        char    table_name[FN_REFLEN];  /*!< InnoDB table name */
51
 
        uint            use_count;      /*!< reference count,
52
 
                                        incremented in get_share()
53
 
                                        and decremented in free_share() */
54
 
        void*           table_name_hash;/*!< hash table chain node */
55
 
        innodb_idx_translate_t  idx_trans_tbl;  /*!< index translation
56
 
                                                table between MySQL and
57
 
                                                Innodb */
58
 
 
59
 
        st_innobase_share(const char *arg) :
60
 
          use_count(0)
61
 
        {
62
 
          strncpy(table_name, arg, FN_REFLEN);
63
 
        }
64
 
 
 
37
  THR_LOCK lock;
 
38
  pthread_mutex_t mutex;
 
39
  const char* table_name;
 
40
  uint use_count;
 
41
  void* table_name_hash;
65
42
} INNOBASE_SHARE;
66
43
 
67
44
 
68
 
/** InnoDB B-tree index */
69
45
struct dict_index_struct;
70
 
/** Prebuilt structures in an Innobase table handle used within MySQL */
71
46
struct row_prebuilt_struct;
72
47
 
73
 
/** InnoDB B-tree index */
74
48
typedef struct dict_index_struct dict_index_t;
75
 
/** Prebuilt structures in an Innobase table handle used within MySQL */
76
49
typedef struct row_prebuilt_struct row_prebuilt_t;
77
50
 
78
 
/** The class defining a handle to an Innodb table */
79
 
class ha_innobase: public Cursor
 
51
/* The class defining a handle to an Innodb table */
 
52
class ha_innobase: public handler
80
53
{
81
 
        row_prebuilt_t* prebuilt;       /*!< prebuilt struct in InnoDB, used
 
54
        row_prebuilt_t* prebuilt;       /* prebuilt struct in InnoDB, used
82
55
                                        to save CPU time with prebuilt data
83
56
                                        structures*/
84
 
        Session*        user_session;   /*!< the thread handle of the user
 
57
        Session*                user_session;   /* the thread handle of the user
85
58
                                        currently using the handle; this is
86
59
                                        set in external_lock function */
87
60
        THR_LOCK_DATA   lock;
88
 
        INNOBASE_SHARE* share;          /*!< information for MySQL
89
 
                                        table locking */
 
61
        INNOBASE_SHARE  *share;
90
62
 
91
 
        std::vector<unsigned char> upd_buff; /*!< buffer used in updates */
92
 
        std::vector<unsigned char> key_val_buff; /*!< buffer used in converting
93
 
                                                     search key values from MySQL format
94
 
                                                     to Innodb format */
 
63
        unsigned char*          upd_buff;       /* buffer used in updates */
 
64
        unsigned char*          key_val_buff;   /* buffer used in converting
 
65
                                        search key values from MySQL format
 
66
                                        to Innodb format */
95
67
        ulong           upd_and_key_val_buff_len;
96
68
                                        /* the length of each of the previous
97
69
                                        two buffers */
 
70
        Table_flags     int_table_flags;
98
71
        uint            primary_key;
99
 
        ulong           start_of_scan;  /*!< this is set to 1 when we are
 
72
        ulong           start_of_scan;  /* this is set to 1 when we are
100
73
                                        starting a table scan but have not
101
74
                                        yet fetched any row, else 0 */
102
75
        uint            last_match_mode;/* match mode of the latest search:
103
76
                                        ROW_SEL_EXACT, ROW_SEL_EXACT_PREFIX,
104
77
                                        or undefined */
105
 
        uint            num_write_row;  /*!< number of doInsertRecord() calls */
 
78
        uint            num_write_row;  /* number of write_row() calls */
106
79
 
107
 
        UNIV_INTERN uint store_key_val_for_row(uint keynr, char* buff, 
108
 
                                   uint buff_len, const unsigned char* record);
109
 
        UNIV_INTERN void update_session(Session* session);
110
 
        UNIV_INTERN int change_active_index(uint32_t keynr);
111
 
        UNIV_INTERN int general_fetch(unsigned char* buf, uint32_t direction, uint32_t match_mode);
112
 
        UNIV_INTERN ulint innobase_lock_autoinc();
113
 
        UNIV_INTERN uint64_t innobase_peek_autoinc();
114
 
        UNIV_INTERN ulint innobase_set_max_autoinc(uint64_t auto_inc);
115
 
        UNIV_INTERN ulint innobase_reset_autoinc(uint64_t auto_inc);
116
 
        UNIV_INTERN ulint innobase_get_autoinc(uint64_t* value);
 
80
        uint store_key_val_for_row(uint keynr, char* buff, uint buff_len,
 
81
                                   const unsigned char* record);
 
82
        void update_session(Session* session);
 
83
        void update_session();
 
84
        int change_active_index(uint32_t keynr);
 
85
        int general_fetch(unsigned char* buf, uint32_t direction, uint32_t match_mode);
 
86
        ulint innobase_lock_autoinc();
 
87
        uint64_t innobase_peek_autoinc();
 
88
        ulint innobase_set_max_autoinc(uint64_t auto_inc);
 
89
        ulint innobase_reset_autoinc(uint64_t auto_inc);
 
90
        ulint innobase_get_autoinc(uint64_t* value);
117
91
        ulint innobase_update_autoinc(uint64_t  auto_inc);
118
 
        UNIV_INTERN void innobase_initialize_autoinc();
119
 
        UNIV_INTERN dict_index_t* innobase_get_index(uint keynr);
 
92
        ulint innobase_initialize_autoinc();
 
93
        dict_index_t* innobase_get_index(uint keynr);
 
94
        uint64_t innobase_get_int_col_max_value(const Field* field);
120
95
 
121
96
        /* Init values for the class: */
122
97
 public:
123
 
        UNIV_INTERN ha_innobase(plugin::StorageEngine &engine,
124
 
                                Table &table_arg);
125
 
        UNIV_INTERN ~ha_innobase();
126
 
  /**
127
 
   * Returns the plugin::TransactionStorageEngine pointer
128
 
   * of the cursor's underlying engine.
129
 
   *
130
 
   * @todo
131
 
   *
132
 
   * Have a TransactionalCursor subclass...
133
 
   */
134
 
  UNIV_INTERN plugin::TransactionalStorageEngine *getTransactionalEngine()
135
 
  {
136
 
    return static_cast<plugin::TransactionalStorageEngine *>(getEngine());
137
 
  }
138
 
 
139
 
        UNIV_INTERN const char* index_type(uint key_number);
140
 
        UNIV_INTERN const key_map* keys_to_use_for_scanning();
141
 
 
142
 
        UNIV_INTERN int doOpen(const drizzled::identifier::Table &identifier, int mode, uint test_if_locked);
143
 
        UNIV_INTERN int close(void);
144
 
        UNIV_INTERN double scan_time();
145
 
        UNIV_INTERN double read_time(uint index, uint ranges, ha_rows rows);
146
 
 
147
 
        UNIV_INTERN int doInsertRecord(unsigned char * buf);
148
 
        UNIV_INTERN int doUpdateRecord(const unsigned char * old_data, unsigned char * new_data);
149
 
        UNIV_INTERN int doDeleteRecord(const unsigned char * buf);
150
 
        UNIV_INTERN bool was_semi_consistent_read();
151
 
        UNIV_INTERN void try_semi_consistent_read(bool yes);
152
 
        UNIV_INTERN void unlock_row();
153
 
 
154
 
        UNIV_INTERN int doStartIndexScan(uint index, bool sorted);
155
 
        UNIV_INTERN int doEndIndexScan();
156
 
        UNIV_INTERN int index_read(unsigned char * buf, const unsigned char * key,
 
98
        ha_innobase(StorageEngine *engine, TableShare *table_arg);
 
99
        ~ha_innobase();
 
100
        /*
 
101
          Get the row type from the storage engine.  If this method returns
 
102
          ROW_TYPE_NOT_USED, the information in HA_CREATE_INFO should be used.
 
103
        */
 
104
        enum row_type get_row_type() const;
 
105
 
 
106
        const char* table_type() const;
 
107
        const char* index_type(uint key_number);
 
108
        const char** bas_ext() const;
 
109
        Table_flags table_flags() const;
 
110
        uint32_t index_flags(uint idx, uint part, bool all_parts) const;
 
111
        uint32_t max_supported_keys() const;
 
112
        uint32_t max_supported_key_length() const;
 
113
        uint32_t max_supported_key_part_length() const;
 
114
        const key_map* keys_to_use_for_scanning();
 
115
 
 
116
        int open(const char *name, int mode, uint test_if_locked);
 
117
        int close(void);
 
118
        double scan_time();
 
119
        double read_time(uint index, uint ranges, ha_rows rows);
 
120
 
 
121
        int write_row(unsigned char * buf);
 
122
        int update_row(const unsigned char * old_data, unsigned char * new_data);
 
123
        int delete_row(const unsigned char * buf);
 
124
        bool was_semi_consistent_read();
 
125
        void try_semi_consistent_read(bool yes);
 
126
        void unlock_row();
 
127
 
 
128
#ifdef ROW_MERGE_IS_INDEX_USABLE
 
129
        /** Check if an index can be used by this transaction.
 
130
        * @param keynr  key number to check
 
131
        * @return       true if available, false if the index
 
132
        *               does not contain old records that exist
 
133
        *               in the read view of this transaction */
 
134
        bool is_index_available(uint keynr);
 
135
#endif /* ROW_MERGE_IS_INDEX_USABLE */
 
136
        int index_init(uint index, bool sorted);
 
137
        int index_end();
 
138
        int index_read(unsigned char * buf, const unsigned char * key,
157
139
                uint key_len, enum ha_rkey_function find_flag);
158
 
        UNIV_INTERN int index_read_idx(unsigned char * buf, uint index, const unsigned char * key,
 
140
        int index_read_idx(unsigned char * buf, uint index, const unsigned char * key,
159
141
                           uint key_len, enum ha_rkey_function find_flag);
160
 
        UNIV_INTERN int index_read_last(unsigned char * buf, const unsigned char * key, uint key_len);
161
 
        UNIV_INTERN int index_next(unsigned char * buf);
162
 
        UNIV_INTERN int index_next_same(unsigned char * buf, const unsigned char *key, uint keylen);
163
 
        UNIV_INTERN int index_prev(unsigned char * buf);
164
 
        UNIV_INTERN int index_first(unsigned char * buf);
165
 
        UNIV_INTERN int index_last(unsigned char * buf);
166
 
 
167
 
        UNIV_INTERN int doStartTableScan(bool scan);
168
 
        UNIV_INTERN int doEndTableScan();
169
 
        UNIV_INTERN int rnd_next(unsigned char *buf);
170
 
        UNIV_INTERN int rnd_pos(unsigned char * buf, unsigned char *pos);
171
 
 
172
 
        UNIV_INTERN void position(const unsigned char *record);
173
 
        UNIV_INTERN int info(uint);
174
 
        UNIV_INTERN int analyze(Session* session);
175
 
        UNIV_INTERN int discard_or_import_tablespace(bool discard);
176
 
        UNIV_INTERN int extra(enum ha_extra_function operation);
177
 
        UNIV_INTERN int reset();
178
 
        UNIV_INTERN int external_lock(Session *session, int lock_type);
 
142
        int index_read_last(unsigned char * buf, const unsigned char * key, uint key_len);
 
143
        int index_next(unsigned char * buf);
 
144
        int index_next_same(unsigned char * buf, const unsigned char *key, uint keylen);
 
145
        int index_prev(unsigned char * buf);
 
146
        int index_first(unsigned char * buf);
 
147
        int index_last(unsigned char * buf);
 
148
 
 
149
        int rnd_init(bool scan);
 
150
        int rnd_end();
 
151
        int rnd_next(unsigned char *buf);
 
152
        int rnd_pos(unsigned char * buf, unsigned char *pos);
 
153
 
 
154
        void position(const unsigned char *record);
 
155
        int info(uint);
 
156
        int analyze(Session* session,HA_CHECK_OPT* check_opt);
 
157
        int optimize(Session* session,HA_CHECK_OPT* check_opt);
 
158
        int discard_or_import_tablespace(bool discard);
 
159
        int extra(enum ha_extra_function operation);
 
160
        int reset();
 
161
        int external_lock(Session *session, int lock_type);
 
162
        int transactional_table_lock(Session *session, int lock_type);
 
163
        int start_stmt(Session *session, thr_lock_type lock_type);
179
164
        void position(unsigned char *record);
180
 
        UNIV_INTERN ha_rows records_in_range(uint inx, key_range *min_key, key_range
 
165
        ha_rows records_in_range(uint inx, key_range *min_key, key_range
181
166
                                                                *max_key);
182
 
        UNIV_INTERN ha_rows estimate_rows_upper_bound();
 
167
        ha_rows estimate_rows_upper_bound();
183
168
 
184
 
        UNIV_INTERN int delete_all_rows();
185
 
        UNIV_INTERN int check(Session* session);
186
 
        UNIV_INTERN char* update_table_comment(const char* comment);
187
 
        UNIV_INTERN char* get_foreign_key_create_info();
188
 
        UNIV_INTERN int get_foreign_key_list(Session *session, List<ForeignKeyInfo> *f_key_list);
189
 
        UNIV_INTERN bool can_switch_engines();
190
 
        UNIV_INTERN uint referenced_by_foreign_key();
191
 
        UNIV_INTERN void free_foreign_key_create_info(char* str);
192
 
        UNIV_INTERN THR_LOCK_DATA **store_lock(Session *session, THR_LOCK_DATA **to,
 
169
        void update_create_info(HA_CREATE_INFO* create_info);
 
170
        int create(const char *name, Table *form,
 
171
                   HA_CREATE_INFO *create_info);
 
172
        int delete_all_rows();
 
173
        int delete_table(const char *name);
 
174
        int rename_table(const char* from, const char* to);
 
175
        int check(Session* session, HA_CHECK_OPT* check_opt);
 
176
        char* update_table_comment(const char* comment);
 
177
        char* get_foreign_key_create_info();
 
178
        int get_foreign_key_list(Session *session, List<FOREIGN_KEY_INFO> *f_key_list);
 
179
        bool can_switch_engines();
 
180
        uint referenced_by_foreign_key();
 
181
        void free_foreign_key_create_info(char* str);
 
182
        THR_LOCK_DATA **store_lock(Session *session, THR_LOCK_DATA **to,
193
183
                                        enum thr_lock_type lock_type);
194
 
        UNIV_INTERN virtual void get_auto_increment(uint64_t offset, 
195
 
                                                    uint64_t increment,
196
 
                                                    uint64_t nb_desired_values,
197
 
                                                    uint64_t *first_value,
198
 
                                                    uint64_t *nb_reserved_values);
199
 
        UNIV_INTERN int reset_auto_increment(uint64_t value);
200
 
 
201
 
        UNIV_INTERN bool primary_key_is_clustered();
202
 
        UNIV_INTERN int cmp_ref(const unsigned char *ref1, const unsigned char *ref2);
 
184
        void init_table_handle_for_HANDLER();
 
185
        virtual void get_auto_increment(uint64_t offset, uint64_t increment,
 
186
                                        uint64_t nb_desired_values,
 
187
                                        uint64_t *first_value,
 
188
                                        uint64_t *nb_reserved_values);
 
189
        int reset_auto_increment(uint64_t value);
 
190
 
 
191
        virtual bool get_error_message(int error, String *buf);
 
192
 
 
193
        bool primary_key_is_clustered();
 
194
        int cmp_ref(const unsigned char *ref1, const unsigned char *ref2);
203
195
        /** Fast index creation (smart ALTER TABLE) @see handler0alter.cc @{ */
204
 
        // Don't use these, I have just left them in here as reference for
205
 
        // the future. -Brian 
206
 
#if 0
207
 
        UNIV_INTERN int add_index(Session *session, TABLE *table_arg, KeyInfo *key_info, uint num_of_keys);
208
 
        UNIV_INTERN int prepare_drop_index(Session *session,
209
 
                                           TABLE *table_arg,
210
 
                                           uint *key_num,
211
 
                                           uint num_of_keys);
212
 
        UNIV_INTERN int final_drop_index(Session *session, TABLE *table_arg);
213
 
#endif
 
196
        int add_index(TABLE *table_arg, KEY *key_info, uint num_of_keys);
 
197
        int prepare_drop_index(TABLE *table_arg, uint *key_num,
 
198
                               uint num_of_keys);
 
199
        int final_drop_index(TABLE *table_arg);
214
200
        /** @} */
215
201
public:
 
202
  /**
 
203
   * Multi Range Read interface
 
204
   */
 
205
  int multi_range_read_init(RANGE_SEQ_IF *seq, void *seq_init_param,
 
206
                            uint32_t n_ranges, uint32_t mode,
 
207
                            HANDLER_BUFFER *buf);
 
208
  int multi_range_read_next(char **range_info);
 
209
  ha_rows multi_range_read_info_const(uint32_t keyno, RANGE_SEQ_IF *seq,
 
210
                                      void *seq_init_param,
 
211
                                      uint32_t n_ranges, uint32_t *bufsz,
 
212
                                      uint32_t *flags, COST_VECT *cost);
 
213
  int multi_range_read_info(uint32_t keyno, uint32_t n_ranges, uint32_t keys,
 
214
                            uint32_t *bufsz, uint32_t *flags, COST_VECT *cost);
 
215
  DsMrr_impl ds_mrr;
 
216
 
216
217
  int read_range_first(const key_range *start_key, const key_range *end_key,
217
218
                       bool eq_range_arg, bool sorted);
218
219
  int read_range_next();
 
220
  Item *idx_cond_push(uint32_t keyno, Item* idx_cond);
 
221
 
219
222
};
220
223
 
221
224
 
 
225
extern "C" {
 
226
struct charset_info_st *session_charset(Session *session);
 
227
char **session_query(Session *session);
 
228
 
222
229
/** Get the file name of the MySQL binlog.
223
230
 * @return the name of the binlog file
224
231
 */
237
244
*/
238
245
int session_slave_thread(const Session *session);
239
246
 
 
247
/**
 
248
  Check if a user thread is running a non-transactional update
 
249
  @param session  user thread
 
250
  @retval 0 the user thread is not running a non-transactional update
 
251
  @retval 1 the user thread is running a non-transactional update
 
252
*/
 
253
int session_non_transactional_update(const Session *session);
 
254
 
 
255
/**
 
256
  Get the user thread's binary logging format
 
257
  @param session  user thread
 
258
  @return Value to be used as index into the binlog_format_names array
 
259
*/
 
260
int session_binlog_format(const Session *session);
 
261
 
 
262
/**
 
263
  Mark transaction to rollback and mark error as fatal to a sub-statement.
 
264
  @param  session   Thread handle
 
265
  @param  all   TRUE <=> rollback main transaction.
 
266
*/
 
267
void session_mark_transaction_to_rollback(Session *session, bool all);
 
268
}
 
269
 
240
270
typedef struct trx_struct trx_t;
241
 
/********************************************************************//**
242
 
@file Cursor/ha_innodb.h
 
271
/************************************************************************
243
272
Converts an InnoDB error code to a MySQL error code and also tells to MySQL
244
273
about a possible transaction rollback inside InnoDB caused by a lock wait
245
 
timeout or a deadlock.
246
 
@return MySQL error code */
247
 
UNIV_INTERN
 
274
timeout or a deadlock. */
 
275
extern "C"
248
276
int
249
277
convert_error_code_to_mysql(
250
278
/*========================*/
251
 
        int             error,          /*!< in: InnoDB error code */
252
 
        ulint           flags,          /*!< in: InnoDB table flags, or 0 */
253
 
        Session         *session);      /*!< in: user thread handle or NULL */
 
279
                                        /* out: MySQL error code */
 
280
        int             error,          /* in: InnoDB error code */
 
281
        ulint           flags,          /* in: InnoDB table flags, or 0 */
 
282
        Session         *session);      /* in: user thread handle or NULL */
254
283
 
255
 
/*********************************************************************//**
256
 
Allocates an InnoDB transaction for a MySQL Cursor object.
257
 
@return InnoDB transaction handle */
258
 
UNIV_INTERN
 
284
/*************************************************************************
 
285
Allocates an InnoDB transaction for a MySQL handler object. */
 
286
extern "C"
259
287
trx_t*
260
288
innobase_trx_allocate(
261
289
/*==================*/
262
 
        Session         *session);      /*!< in: user thread handle */
263
 
 
264
 
/***********************************************************************
265
 
This function checks each index name for a table against reserved
266
 
system default primary index name 'GEN_CLUST_INDEX'. If a name matches,
267
 
this function pushes an error message to the client, and returns true. */
268
 
bool
269
 
innobase_index_name_is_reserved(
270
 
/*============================*/
271
 
                                        /* out: true if index name matches a
272
 
                                        reserved name */
273
 
        const trx_t*    trx,            /* in: InnoDB transaction handle */
274
 
        const drizzled::KeyInfo*        key_info,/* in: Indexes to be created */
275
 
        ulint           num_of_keys);   /* in: Number of indexes to
276
 
                                        be created. */
277
 
 
 
290
                                        /* out: InnoDB transaction handle */
 
291
        Session         *session);      /* in: user thread handle */
278
292
#endif /* INNODB_HANDLER_HA_INNODB_H */