~drizzle-trunk/drizzle/development

575.1.5 by Monty Taylor
Moved stuff to handlerton.cc
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
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
 */
1 by brian
clean slate
19
20
/**
21
  @file handler.cc
22
23
  Handler-calling-functions
24
*/
25
873.1.17 by Jay Pipes
Fix for incomplete class definition in handler.cc. Forgot to commit it
26
#include "drizzled/server_includes.h"
27
#include "mysys/hash.h"
28
#include "drizzled/error.h"
29
#include "drizzled/gettext.h"
30
#include "drizzled/probes.h"
31
#include "drizzled/sql_parse.h"
32
#include "drizzled/cost_vect.h"
33
#include "drizzled/session.h"
34
#include "drizzled/sql_base.h"
1039.5.31 by Jay Pipes
This patch does a few things:
35
#include "drizzled/replication_services.h"
873.1.17 by Jay Pipes
Fix for incomplete class definition in handler.cc. Forgot to commit it
36
#include "drizzled/lock.h"
37
#include "drizzled/item/int.h"
38
#include "drizzled/item/empty_string.h"
39
#include "drizzled/unireg.h" // for mysql_frm_type
40
#include "drizzled/field/timestamp.h"
988.1.1 by Jay Pipes
Changes libserialize to libdrizzledmessage per ML discussion. All GPB messages are now in the drizzled::message namespace.
41
#include "drizzled/message/table.pb.h"
572.1.4 by Monty Taylor
Removed a bunch of unusued tests and defines from autoconf.
42
919.2.14 by Monty Taylor
Use math.h instead of cmath... one of these days...
43
using namespace std;
1 by brian
clean slate
44
1039.5.31 by Jay Pipes
This patch does a few things:
45
extern drizzled::ReplicationServices replication_services;
988.1.6 by Jay Pipes
Removed old protobuf_replicator plugin, fixed up db.cc and other files to use new
46
461 by Monty Taylor
Removed NullS. bu-bye.
47
KEY_CREATE_INFO default_key_create_info= { HA_KEY_ALG_UNDEF, 0, {NULL,0}, {NULL,0} };
1 by brian
clean slate
48
960.2.39 by Monty Taylor
Removed more handlerton naming references.
49
/* number of entries in storage_engines[] */
61 by Brian Aker
Conversion of handler type.
50
uint32_t total_ha= 0;
960.2.39 by Monty Taylor
Removed more handlerton naming references.
51
/* number of storage engines (from storage_engines[]) that support 2pc */
61 by Brian Aker
Conversion of handler type.
52
uint32_t total_ha_2pc= 0;
1 by brian
clean slate
53
/* size of savepoint storage area (see ha_init) */
61 by Brian Aker
Conversion of handler type.
54
uint32_t savepoint_alloc_size= 0;
1 by brian
clean slate
55
56
const char *ha_row_type[] = {
57
  "", "FIXED", "DYNAMIC", "COMPRESSED", "REDUNDANT", "COMPACT", "PAGE", "?","?","?"
58
};
59
60
const char *tx_isolation_names[] =
61
{ "READ-UNCOMMITTED", "READ-COMMITTED", "REPEATABLE-READ", "SERIALIZABLE",
461 by Monty Taylor
Removed NullS. bu-bye.
62
  NULL};
575.1.3 by Monty Taylor
Moved some stuff out of handler.h.
63
1 by brian
clean slate
64
TYPELIB tx_isolation_typelib= {array_elements(tx_isolation_names)-1,"",
575.1.3 by Monty Taylor
Moved some stuff out of handler.h.
65
                               tx_isolation_names, NULL};
1 by brian
clean slate
66
67
68
/**
69
  Register handler error messages for use with my_error().
70
71
  @retval
72
    0           OK
73
  @retval
74
    !=0         Error
75
*/
76
77
int ha_init_errors(void)
78
{
79
#define SETMSG(nr, msg) errmsgs[(nr) - HA_ERR_FIRST]= (msg)
80
  const char    **errmsgs;
81
82
  /* Allocate a pointer array for the error message strings. */
83
  /* Zerofill it to avoid uninitialized gaps. */
641.3.8 by Monty Taylor
Removed my_malloc from drizzled.
84
  if (! (errmsgs= (const char**) malloc(HA_ERR_ERRORS * sizeof(char*))))
1 by brian
clean slate
85
    return 1;
641.3.9 by Monty Taylor
More removal of my_malloc.
86
  memset(errmsgs, 0, HA_ERR_ERRORS * sizeof(char *));
1 by brian
clean slate
87
88
  /* Set the dedicated error messages. */
89
  SETMSG(HA_ERR_KEY_NOT_FOUND,          ER(ER_KEY_NOT_FOUND));
90
  SETMSG(HA_ERR_FOUND_DUPP_KEY,         ER(ER_DUP_KEY));
91
  SETMSG(HA_ERR_RECORD_CHANGED,         "Update wich is recoverable");
92
  SETMSG(HA_ERR_WRONG_INDEX,            "Wrong index given to function");
93
  SETMSG(HA_ERR_CRASHED,                ER(ER_NOT_KEYFILE));
94
  SETMSG(HA_ERR_WRONG_IN_RECORD,        ER(ER_CRASHED_ON_USAGE));
95
  SETMSG(HA_ERR_OUT_OF_MEM,             "Table handler out of memory");
96
  SETMSG(HA_ERR_NOT_A_TABLE,            "Incorrect file format '%.64s'");
97
  SETMSG(HA_ERR_WRONG_COMMAND,          "Command not supported");
98
  SETMSG(HA_ERR_OLD_FILE,               ER(ER_OLD_KEYFILE));
99
  SETMSG(HA_ERR_NO_ACTIVE_RECORD,       "No record read in update");
100
  SETMSG(HA_ERR_RECORD_DELETED,         "Intern record deleted");
101
  SETMSG(HA_ERR_RECORD_FILE_FULL,       ER(ER_RECORD_FILE_FULL));
102
  SETMSG(HA_ERR_INDEX_FILE_FULL,        "No more room in index file '%.64s'");
103
  SETMSG(HA_ERR_END_OF_FILE,            "End in next/prev/first/last");
104
  SETMSG(HA_ERR_UNSUPPORTED,            ER(ER_ILLEGAL_HA));
105
  SETMSG(HA_ERR_TO_BIG_ROW,             "Too big row");
106
  SETMSG(HA_WRONG_CREATE_OPTION,        "Wrong create option");
107
  SETMSG(HA_ERR_FOUND_DUPP_UNIQUE,      ER(ER_DUP_UNIQUE));
108
  SETMSG(HA_ERR_UNKNOWN_CHARSET,        "Can't open charset");
109
  SETMSG(HA_ERR_WRONG_MRG_TABLE_DEF,    ER(ER_WRONG_MRG_TABLE));
110
  SETMSG(HA_ERR_CRASHED_ON_REPAIR,      ER(ER_CRASHED_ON_REPAIR));
111
  SETMSG(HA_ERR_CRASHED_ON_USAGE,       ER(ER_CRASHED_ON_USAGE));
112
  SETMSG(HA_ERR_LOCK_WAIT_TIMEOUT,      ER(ER_LOCK_WAIT_TIMEOUT));
113
  SETMSG(HA_ERR_LOCK_TABLE_FULL,        ER(ER_LOCK_TABLE_FULL));
114
  SETMSG(HA_ERR_READ_ONLY_TRANSACTION,  ER(ER_READ_ONLY_TRANSACTION));
115
  SETMSG(HA_ERR_LOCK_DEADLOCK,          ER(ER_LOCK_DEADLOCK));
116
  SETMSG(HA_ERR_CANNOT_ADD_FOREIGN,     ER(ER_CANNOT_ADD_FOREIGN));
117
  SETMSG(HA_ERR_NO_REFERENCED_ROW,      ER(ER_NO_REFERENCED_ROW_2));
118
  SETMSG(HA_ERR_ROW_IS_REFERENCED,      ER(ER_ROW_IS_REFERENCED_2));
119
  SETMSG(HA_ERR_NO_SAVEPOINT,           "No savepoint with that name");
120
  SETMSG(HA_ERR_NON_UNIQUE_BLOCK_SIZE,  "Non unique key block size");
121
  SETMSG(HA_ERR_NO_SUCH_TABLE,          "No such table: '%.64s'");
122
  SETMSG(HA_ERR_TABLE_EXIST,            ER(ER_TABLE_EXISTS_ERROR));
123
  SETMSG(HA_ERR_NO_CONNECTION,          "Could not connect to storage engine");
124
  SETMSG(HA_ERR_TABLE_DEF_CHANGED,      ER(ER_TABLE_DEF_CHANGED));
125
  SETMSG(HA_ERR_FOREIGN_DUPLICATE_KEY,  "FK constraint would lead to duplicate key");
126
  SETMSG(HA_ERR_TABLE_NEEDS_UPGRADE,    ER(ER_TABLE_NEEDS_UPGRADE));
127
  SETMSG(HA_ERR_TABLE_READONLY,         ER(ER_OPEN_AS_READONLY));
128
  SETMSG(HA_ERR_AUTOINC_READ_FAILED,    ER(ER_AUTOINC_READ_FAILED));
129
  SETMSG(HA_ERR_AUTOINC_ERANGE,         ER(ER_WARN_DATA_OUT_OF_RANGE));
130
131
  /* Register the error messages for use with my_error(). */
132
  return my_error_register(errmsgs, HA_ERR_FIRST, HA_ERR_LAST);
133
}
134
135
136
/**
137
  Unregister handler error messages.
138
139
  @retval
140
    0           OK
141
  @retval
142
    !=0         Error
143
*/
144
static int ha_finish_errors(void)
145
{
146
  const char    **errmsgs;
147
148
  /* Allocate a pointer array for the error message strings. */
149
  if (! (errmsgs= my_error_unregister(HA_ERR_FIRST, HA_ERR_LAST)))
150
    return 1;
481 by Brian Aker
Remove all of uchar.
151
  free((unsigned char*) errmsgs);
1 by brian
clean slate
152
  return 0;
153
}
154
155
int ha_init()
156
{
157
  int error= 0;
158
51.1.77 by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols
159
  assert(total_ha < MAX_HA);
1 by brian
clean slate
160
  /*
161
    Check if there is a transaction-capable storage engine besides the
162
    binary log (which is considered a transaction-capable storage engine in
163
    counting total_ha)
164
  */
165
  savepoint_alloc_size+= sizeof(SAVEPOINT);
1046.1.9 by Brian Aker
Remove caller that wasn't correctly locking, and reverted code to 5.1
166
  return error;
1 by brian
clean slate
167
}
168
169
int ha_end()
170
{
171
  int error= 0;
172
575.1.3 by Monty Taylor
Moved some stuff out of handler.h.
173
  /*
1 by brian
clean slate
174
    This should be eventualy based  on the graceful shutdown flag.
175
    So if flag is equal to HA_PANIC_CLOSE, the deallocate
176
    the errors.
177
  */
178
  if (ha_finish_errors())
179
    error= 1;
180
1046.1.9 by Brian Aker
Remove caller that wasn't correctly locking, and reverted code to 5.1
181
  return error;
1 by brian
clean slate
182
}
183
184
185
186
/* ========================================================================
187
 ======================= TRANSACTIONS ===================================*/
188
189
/**
190
  Transaction handling in the server
191
  ==================================
192
193
  In each client connection, MySQL maintains two transactional
194
  states:
195
  - a statement transaction,
196
  - a standard, also called normal transaction.
197
198
  Historical note
199
  ---------------
200
  "Statement transaction" is a non-standard term that comes
201
  from the times when MySQL supported BerkeleyDB storage engine.
202
203
  First of all, it should be said that in BerkeleyDB auto-commit
204
  mode auto-commits operations that are atomic to the storage
205
  engine itself, such as a write of a record, and are too
206
  high-granular to be atomic from the application perspective
207
  (MySQL). One SQL statement could involve many BerkeleyDB
208
  auto-committed operations and thus BerkeleyDB auto-commit was of
209
  little use to MySQL.
210
211
  Secondly, instead of SQL standard savepoints, BerkeleyDB
212
  provided the concept of "nested transactions". In a nutshell,
213
  transactions could be arbitrarily nested, but when the parent
214
  transaction was committed or aborted, all its child (nested)
215
  transactions were handled committed or aborted as well.
216
  Commit of a nested transaction, in turn, made its changes
217
  visible, but not durable: it destroyed the nested transaction,
218
  all its changes would become available to the parent and
219
  currently active nested transactions of this parent.
220
221
  So the mechanism of nested transactions was employed to
222
  provide "all or nothing" guarantee of SQL statements
223
  required by the standard.
224
  A nested transaction would be created at start of each SQL
225
  statement, and destroyed (committed or aborted) at statement
226
  end. Such nested transaction was internally referred to as
227
  a "statement transaction" and gave birth to the term.
228
229
  <Historical note ends>
230
231
  Since then a statement transaction is started for each statement
232
  that accesses transactional tables or uses the binary log.  If
233
  the statement succeeds, the statement transaction is committed.
234
  If the statement fails, the transaction is rolled back. Commits
235
  of statement transactions are not durable -- each such
236
  transaction is nested in the normal transaction, and if the
237
  normal transaction is rolled back, the effects of all enclosed
238
  statement transactions are undone as well.  Technically,
239
  a statement transaction can be viewed as a savepoint which is
240
  maintained automatically in order to make effects of one
241
  statement atomic.
242
243
  The normal transaction is started by the user and is ended
244
  usually upon a user request as well. The normal transaction
245
  encloses transactions of all statements issued between
246
  its beginning and its end.
247
  In autocommit mode, the normal transaction is equivalent
248
  to the statement transaction.
249
250
  Since MySQL supports PSEA (pluggable storage engine
251
  architecture), more than one transactional engine can be
252
  active at a time. Hence transactions, from the server
253
  point of view, are always distributed. In particular,
254
  transactional state is maintained independently for each
255
  engine. In order to commit a transaction the two phase
256
  commit protocol is employed.
257
258
  Not all statements are executed in context of a transaction.
259
  Administrative and status information statements do not modify
260
  engine data, and thus do not start a statement transaction and
261
  also have no effect on the normal transaction. Examples of such
262
  statements are SHOW STATUS and RESET SLAVE.
263
264
  Similarly DDL statements are not transactional,
265
  and therefore a transaction is [almost] never started for a DDL
266
  statement. The difference between a DDL statement and a purely
267
  administrative statement though is that a DDL statement always
268
  commits the current transaction before proceeding, if there is
269
  any.
270
271
  At last, SQL statements that work with non-transactional
272
  engines also have no effect on the transaction state of the
273
  connection. Even though they are written to the binary log,
274
  and the binary log is, overall, transactional, the writes
275
  are done in "write-through" mode, directly to the binlog
276
  file, followed with a OS cache sync, in other words,
277
  bypassing the binlog undo log (translog).
278
  They do not commit the current normal transaction.
279
  A failure of a statement that uses non-transactional tables
280
  would cause a rollback of the statement transaction, but
281
  in case there no non-transactional tables are used,
282
  no statement transaction is started.
283
284
  Data layout
285
  -----------
286
287
  The server stores its transaction-related data in
520.1.22 by Brian Aker
Second pass of thd cleanup
288
  session->transaction. This structure has two members of type
520.1.21 by Brian Aker
THD -> Session rename
289
  Session_TRANS. These members correspond to the statement and
1 by brian
clean slate
290
  normal transactions respectively:
291
520.1.22 by Brian Aker
Second pass of thd cleanup
292
  - session->transaction.stmt contains a list of engines
1 by brian
clean slate
293
  that are participating in the given statement
520.1.22 by Brian Aker
Second pass of thd cleanup
294
  - session->transaction.all contains a list of engines that
1 by brian
clean slate
295
  have participated in any of the statement transactions started
296
  within the context of the normal transaction.
297
  Each element of the list contains a pointer to the storage
298
  engine, engine-specific transactional data, and engine-specific
299
  transaction flags.
300
520.1.22 by Brian Aker
Second pass of thd cleanup
301
  In autocommit mode session->transaction.all is empty.
302
  Instead, data of session->transaction.stmt is
1 by brian
clean slate
303
  used to commit/rollback the normal transaction.
304
305
  The list of registered engines has a few important properties:
306
  - no engine is registered in the list twice
307
  - engines are present in the list a reverse temporal order --
308
  new participants are always added to the beginning of the list.
309
310
  Transaction life cycle
311
  ----------------------
312
520.1.22 by Brian Aker
Second pass of thd cleanup
313
  When a new connection is established, session->transaction
1 by brian
clean slate
314
  members are initialized to an empty state.
315
  If a statement uses any tables, all affected engines
316
  are registered in the statement engine list. In
317
  non-autocommit mode, the same engines are registered in
318
  the normal transaction list.
319
  At the end of the statement, the server issues a commit
320
  or a roll back for all engines in the statement list.
321
  At this point transaction flags of an engine, if any, are
322
  propagated from the statement list to the list of the normal
323
  transaction.
324
  When commit/rollback is finished, the statement list is
325
  cleared. It will be filled in again by the next statement,
326
  and emptied again at the next statement's end.
327
328
  The normal transaction is committed in a similar way
520.1.22 by Brian Aker
Second pass of thd cleanup
329
  (by going over all engines in session->transaction.all list)
1 by brian
clean slate
330
  but at different times:
331
  - upon COMMIT SQL statement is issued by the user
332
  - implicitly, by the server, at the beginning of a DDL statement
333
  or SET AUTOCOMMIT={0|1} statement.
334
335
  The normal transaction can be rolled back as well:
336
  - if the user has requested so, by issuing ROLLBACK SQL
337
  statement
338
  - if one of the storage engines requested a rollback
520.1.22 by Brian Aker
Second pass of thd cleanup
339
  by setting session->transaction_rollback_request. This may
1 by brian
clean slate
340
  happen in case, e.g., when the transaction in the engine was
341
  chosen a victim of the internal deadlock resolution algorithm
342
  and rolled back internally. When such a situation happens, there
343
  is little the server can do and the only option is to rollback
344
  transactions in all other participating engines.  In this case
345
  the rollback is accompanied by an error sent to the user.
346
347
  As follows from the use cases above, the normal transaction
348
  is never committed when there is an outstanding statement
349
  transaction. In most cases there is no conflict, since
350
  commits of the normal transaction are issued by a stand-alone
351
  administrative or DDL statement, thus no outstanding statement
352
  transaction of the previous statement exists. Besides,
353
  all statements that manipulate with the normal transaction
354
  are prohibited in stored functions and triggers, therefore
355
  no conflicting situation can occur in a sub-statement either.
356
  The remaining rare cases when the server explicitly has
357
  to commit the statement transaction prior to committing the normal
358
  one cover error-handling scenarios (see for example
1054.1.2 by Brian Aker
Remove SQLCOM_LOCK_TABLES
359
  ?).
1 by brian
clean slate
360
361
  When committing a statement or a normal transaction, the server
362
  either uses the two-phase commit protocol, or issues a commit
363
  in each engine independently. The two-phase commit protocol
364
  is used only if:
365
  - all participating engines support two-phase commit (provide
960.2.24 by Monty Taylor
Changed handlerton to StorageEngine.
366
    StorageEngine::prepare PSEA API call) and
1 by brian
clean slate
367
  - transactions in at least two engines modify data (i.e. are
368
  not read-only).
369
370
  Note that the two phase commit is used for
371
  statement transactions, even though they are not durable anyway.
372
  This is done to ensure logical consistency of data in a multiple-
373
  engine transaction.
374
  For example, imagine that some day MySQL supports unique
375
  constraint checks deferred till the end of statement. In such
376
  case a commit in one of the engines may yield ER_DUP_KEY,
377
  and MySQL should be able to gracefully abort statement
378
  transactions of other participants.
379
380
  After the normal transaction has been committed,
520.1.22 by Brian Aker
Second pass of thd cleanup
381
  session->transaction.all list is cleared.
1 by brian
clean slate
382
383
  When a connection is closed, the current normal transaction, if
384
  any, is rolled back.
385
386
  Roles and responsibilities
387
  --------------------------
388
389
  The server has no way to know that an engine participates in
390
  the statement and a transaction has been started
391
  in it unless the engine says so. Thus, in order to be
392
  a part of a transaction, the engine must "register" itself.
393
  This is done by invoking trans_register_ha() server call.
394
  Normally the engine registers itself whenever handler::external_lock()
395
  is called. trans_register_ha() can be invoked many times: if
396
  an engine is already registered, the call does nothing.
397
  In case autocommit is not set, the engine must register itself
398
  twice -- both in the statement list and in the normal transaction
399
  list.
400
  In which list to register is a parameter of trans_register_ha().
401
402
  Note, that although the registration interface in itself is
403
  fairly clear, the current usage practice often leads to undesired
404
  effects. E.g. since a call to trans_register_ha() in most engines
405
  is embedded into implementation of handler::external_lock(), some
406
  DDL statements start a transaction (at least from the server
407
  point of view) even though they are not expected to. E.g.
408
  CREATE TABLE does not start a transaction, since
409
  handler::external_lock() is never called during CREATE TABLE. But
410
  CREATE TABLE ... SELECT does, since handler::external_lock() is
411
  called for the table that is being selected from. This has no
412
  practical effects currently, but must be kept in mind
413
  nevertheless.
414
415
  Once an engine is registered, the server will do the rest
416
  of the work.
417
418
  During statement execution, whenever any of data-modifying
419
  PSEA API methods is used, e.g. handler::write_row() or
420
  handler::update_row(), the read-write flag is raised in the
421
  statement transaction for the involved engine.
422
  Currently All PSEA calls are "traced", and the data can not be
423
  changed in a way other than issuing a PSEA call. Important:
424
  unless this invariant is preserved the server will not know that
425
  a transaction in a given engine is read-write and will not
426
  involve the two-phase commit protocol!
427
428
  At the end of a statement, server call
429
  ha_autocommit_or_rollback() is invoked. This call in turn
960.2.24 by Monty Taylor
Changed handlerton to StorageEngine.
430
  invokes StorageEngine::prepare() for every involved engine.
431
  Prepare is followed by a call to StorageEngine::commit_one_phase()
432
  If a one-phase commit will suffice, StorageEngine::prepare() is not
433
  invoked and the server only calls StorageEngine::commit_one_phase().
1 by brian
clean slate
434
  At statement commit, the statement-related read-write engine
435
  flag is propagated to the corresponding flag in the normal
436
  transaction.  When the commit is complete, the list of registered
437
  engines is cleared.
438
439
  Rollback is handled in a similar fashion.
440
441
  Additional notes on DDL and the normal transaction.
442
  ---------------------------------------------------
443
444
  DDLs and operations with non-transactional engines
520.1.22 by Brian Aker
Second pass of thd cleanup
445
  do not "register" in session->transaction lists, and thus do not
1 by brian
clean slate
446
  modify the transaction state. Besides, each DDL in
447
  MySQL is prefixed with an implicit normal transaction commit
934.2.11 by Jay Pipes
Moves end_trans(), begin_trans(), end_active_trans() out of the parser module and adds startTransaction(), endTransaction(), and endActiveTransaction() member methods of Session object.
448
  (a call to Session::endActiveTransaction()), and thus leaves nothing
1 by brian
clean slate
449
  to modify.
450
  However, as it has been pointed out with CREATE TABLE .. SELECT,
451
  some DDL statements can start a *new* transaction.
452
453
  Behaviour of the server in this case is currently badly
454
  defined.
455
  DDL statements use a form of "semantic" logging
456
  to maintain atomicity: if CREATE TABLE .. SELECT failed,
457
  the newly created table is deleted.
458
  In addition, some DDL statements issue interim transaction
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
459
  commits: e.g. ALTER Table issues a commit after data is copied
1 by brian
clean slate
460
  from the original table to the internal temporary table. Other
461
  statements, e.g. CREATE TABLE ... SELECT do not always commit
462
  after itself.
463
  And finally there is a group of DDL statements such as
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
464
  RENAME/DROP Table that doesn't start a new transaction
1 by brian
clean slate
465
  and doesn't commit.
466
467
  This diversity makes it hard to say what will happen if
468
  by chance a stored function is invoked during a DDL --
469
  whether any modifications it makes will be committed or not
470
  is not clear. Fortunately, SQL grammar of few DDLs allows
471
  invocation of a stored function.
472
473
  A consistent behaviour is perhaps to always commit the normal
474
  transaction after all DDLs, just like the statement transaction
475
  is always committed at the end of all statements.
476
*/
477
478
/**
479
  Register a storage engine for a transaction.
480
481
  Every storage engine MUST call this function when it starts
482
  a transaction or a statement (that is it must be called both for the
483
  "beginning of transaction" and "beginning of statement").
484
  Only storage engines registered for the transaction/statement
485
  will know when to commit/rollback it.
486
487
  @note
488
    trans_register_ha is idempotent - storage engine may register many
489
    times per transaction.
490
491
*/
960.2.27 by Monty Taylor
Reworked transformed handlerton into class StorageEngine.
492
void trans_register_ha(Session *session, bool all, StorageEngine *engine)
1 by brian
clean slate
493
{
520.1.21 by Brian Aker
THD -> Session rename
494
  Session_TRANS *trans;
1 by brian
clean slate
495
  Ha_trx_info *ha_info;
496
497
  if (all)
498
  {
520.1.22 by Brian Aker
Second pass of thd cleanup
499
    trans= &session->transaction.all;
500
    session->server_status|= SERVER_STATUS_IN_TRANS;
1 by brian
clean slate
501
  }
502
  else
520.1.22 by Brian Aker
Second pass of thd cleanup
503
    trans= &session->transaction.stmt;
1 by brian
clean slate
504
1030.1.1 by Brian Aker
Straighten out structures (remove some some dead bits).
505
  ha_info= session->ha_data[engine->getSlot()].ha_info + static_cast<unsigned>(all);
1 by brian
clean slate
506
507
  if (ha_info->is_started())
51.1.77 by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols
508
    return; /* already registered, return */
1 by brian
clean slate
509
960.2.27 by Monty Taylor
Reworked transformed handlerton into class StorageEngine.
510
  ha_info->register_ha(trans, engine);
1 by brian
clean slate
511
960.2.27 by Monty Taylor
Reworked transformed handlerton into class StorageEngine.
512
  trans->no_2pc|= not engine->has_2pc();
520.1.22 by Brian Aker
Second pass of thd cleanup
513
  if (session->transaction.xid_state.xid.is_null())
514
    session->transaction.xid_state.xid.set(session->query_id);
1 by brian
clean slate
515
}
516
517
/**
518
  Check if we can skip the two-phase commit.
519
520
  A helper function to evaluate if two-phase commit is mandatory.
521
  As a side effect, propagates the read-only/read-write flags
522
  of the statement transaction to its enclosing normal transaction.
523
56 by brian
Next pass of true/false update.
524
  @retval true   we must run a two-phase commit. Returned
1 by brian
clean slate
525
                 if we have at least two engines with read-write changes.
56 by brian
Next pass of true/false update.
526
  @retval false  Don't need two-phase commit. Even if we have two
1 by brian
clean slate
527
                 transactional engines, we can run two independent
528
                 commits if changes in one of the engines are read-only.
529
*/
530
531
static
532
bool
520.1.22 by Brian Aker
Second pass of thd cleanup
533
ha_check_and_coalesce_trx_read_only(Session *session, Ha_trx_info *ha_list,
1 by brian
clean slate
534
                                    bool all)
535
{
536
  /* The number of storage engines that have actual changes. */
537
  unsigned rw_ha_count= 0;
538
  Ha_trx_info *ha_info;
539
540
  for (ha_info= ha_list; ha_info; ha_info= ha_info->next())
541
  {
542
    if (ha_info->is_trx_read_write())
543
      ++rw_ha_count;
544
545
    if (! all)
546
    {
1030.1.1 by Brian Aker
Straighten out structures (remove some some dead bits).
547
      Ha_trx_info *ha_info_all= &session->ha_data[ha_info->engine()->getSlot()].ha_info[1];
51.1.77 by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols
548
      assert(ha_info != ha_info_all);
1 by brian
clean slate
549
      /*
550
        Merge read-only/read-write information about statement
551
        transaction to its enclosing normal transaction. Do this
552
        only if in a real transaction -- that is, if we know
520.1.22 by Brian Aker
Second pass of thd cleanup
553
        that ha_info_all is registered in session->transaction.all.
1 by brian
clean slate
554
        Since otherwise we only clutter the normal transaction flags.
555
      */
56 by brian
Next pass of true/false update.
556
      if (ha_info_all->is_started()) /* false if autocommit. */
1 by brian
clean slate
557
        ha_info_all->coalesce_trx_with(ha_info);
558
    }
559
    else if (rw_ha_count > 1)
560
    {
561
      /*
562
        It is a normal transaction, so we don't need to merge read/write
563
        information up, and the need for two-phase commit has been
564
        already established. Break the loop prematurely.
565
      */
566
      break;
567
    }
568
  }
569
  return rw_ha_count > 1;
570
}
571
572
573
/**
574
  @retval
575
    0   ok
576
  @retval
577
    1   transaction was rolled back
578
  @retval
579
    2   error during commit, data may be inconsistent
580
581
  @todo
582
    Since we don't support nested statement transactions in 5.0,
583
    we can't commit or rollback stmt transactions while we are inside
584
    stored functions or triggers. So we simply do nothing now.
585
    TODO: This should be fixed in later ( >= 5.1) releases.
586
*/
520.1.22 by Brian Aker
Second pass of thd cleanup
587
int ha_commit_trans(Session *session, bool all)
1 by brian
clean slate
588
{
589
  int error= 0, cookie= 0;
590
  /*
591
    'all' means that this is either an explicit commit issued by
592
    user, or an implicit commit issued by a DDL.
593
  */
520.1.22 by Brian Aker
Second pass of thd cleanup
594
  Session_TRANS *trans= all ? &session->transaction.all : &session->transaction.stmt;
595
  bool is_real_trans= all || session->transaction.all.ha_list == 0;
1 by brian
clean slate
596
  Ha_trx_info *ha_info= trans->ha_list;
597
598
  /*
599
    We must not commit the normal transaction if a statement
600
    transaction is pending. Otherwise statement transaction
601
    flags will not get propagated to its normal transaction's
602
    counterpart.
603
  */
520.1.22 by Brian Aker
Second pass of thd cleanup
604
  assert(session->transaction.stmt.ha_list == NULL ||
605
              trans == &session->transaction.stmt);
1 by brian
clean slate
606
607
  if (ha_info)
608
  {
609
    bool must_2pc;
610
520.1.22 by Brian Aker
Second pass of thd cleanup
611
    if (is_real_trans && wait_if_global_read_lock(session, 0, 0))
1 by brian
clean slate
612
    {
520.1.22 by Brian Aker
Second pass of thd cleanup
613
      ha_rollback_trans(session, all);
1046.1.9 by Brian Aker
Remove caller that wasn't correctly locking, and reverted code to 5.1
614
      return 1;
1 by brian
clean slate
615
    }
616
520.1.22 by Brian Aker
Second pass of thd cleanup
617
    must_2pc= ha_check_and_coalesce_trx_read_only(session, ha_info, all);
1 by brian
clean slate
618
619
    if (!trans->no_2pc && must_2pc)
620
    {
621
      for (; ha_info && !error; ha_info= ha_info->next())
622
      {
623
        int err;
960.2.37 by Monty Taylor
More naming fixes.
624
        StorageEngine *engine= ha_info->engine();
1 by brian
clean slate
625
        /*
626
          Do not call two-phase commit if this particular
627
          transaction is read-only. This allows for simpler
628
          implementation in engines that are always read-only.
629
        */
630
        if (! ha_info->is_trx_read_write())
631
          continue;
632
        /*
633
          Sic: we know that prepare() is not NULL since otherwise
634
          trans->no_2pc would have been set.
635
        */
960.2.38 by Monty Taylor
Removed extraneous send myself to myself argument.
636
        if ((err= engine->prepare(session, all)))
1 by brian
clean slate
637
        {
638
          my_error(ER_ERROR_DURING_COMMIT, MYF(0), err);
639
          error= 1;
640
        }
520.1.22 by Brian Aker
Second pass of thd cleanup
641
        status_var_increment(session->status_var.ha_prepare_count);
1 by brian
clean slate
642
      }
798.2.30 by Brian Aker
Removed dependency on log.h
643
      if (error)
1 by brian
clean slate
644
      {
520.1.22 by Brian Aker
Second pass of thd cleanup
645
        ha_rollback_trans(session, all);
1 by brian
clean slate
646
        error= 1;
647
        goto end;
648
      }
649
    }
520.1.22 by Brian Aker
Second pass of thd cleanup
650
    error=ha_commit_one_phase(session, all) ? (cookie ? 2 : 1) : 0;
1 by brian
clean slate
651
end:
652
    if (is_real_trans)
520.1.22 by Brian Aker
Second pass of thd cleanup
653
      start_waiting_global_read_lock(session);
1 by brian
clean slate
654
  }
1046.1.9 by Brian Aker
Remove caller that wasn't correctly locking, and reverted code to 5.1
655
  return error;
1 by brian
clean slate
656
}
657
658
/**
659
  @note
660
  This function does not care about global read lock. A caller should.
661
*/
520.1.22 by Brian Aker
Second pass of thd cleanup
662
int ha_commit_one_phase(Session *session, bool all)
1 by brian
clean slate
663
{
664
  int error=0;
520.1.22 by Brian Aker
Second pass of thd cleanup
665
  Session_TRANS *trans=all ? &session->transaction.all : &session->transaction.stmt;
666
  bool is_real_trans=all || session->transaction.all.ha_list == 0;
1 by brian
clean slate
667
  Ha_trx_info *ha_info= trans->ha_list, *ha_info_next;
668
  if (ha_info)
669
  {
670
    for (; ha_info; ha_info= ha_info_next)
671
    {
672
      int err;
960.2.37 by Monty Taylor
More naming fixes.
673
      StorageEngine *engine= ha_info->engine();
960.2.38 by Monty Taylor
Removed extraneous send myself to myself argument.
674
      if ((err= engine->commit(session, all)))
1 by brian
clean slate
675
      {
676
        my_error(ER_ERROR_DURING_COMMIT, MYF(0), err);
677
        error=1;
678
      }
520.1.22 by Brian Aker
Second pass of thd cleanup
679
      status_var_increment(session->status_var.ha_commit_count);
1 by brian
clean slate
680
      ha_info_next= ha_info->next();
681
      ha_info->reset(); /* keep it conveniently zero-filled */
682
    }
683
    trans->ha_list= 0;
684
    trans->no_2pc=0;
685
    if (is_real_trans)
520.1.22 by Brian Aker
Second pass of thd cleanup
686
      session->transaction.xid_state.xid.null();
1 by brian
clean slate
687
    if (all)
688
    {
520.1.22 by Brian Aker
Second pass of thd cleanup
689
      session->variables.tx_isolation=session->session_tx_isolation;
690
      session->transaction.cleanup();
1 by brian
clean slate
691
    }
692
  }
1046.1.9 by Brian Aker
Remove caller that wasn't correctly locking, and reverted code to 5.1
693
  return error;
1 by brian
clean slate
694
}
695
696
520.1.22 by Brian Aker
Second pass of thd cleanup
697
int ha_rollback_trans(Session *session, bool all)
1 by brian
clean slate
698
{
699
  int error=0;
520.1.22 by Brian Aker
Second pass of thd cleanup
700
  Session_TRANS *trans=all ? &session->transaction.all : &session->transaction.stmt;
1 by brian
clean slate
701
  Ha_trx_info *ha_info= trans->ha_list, *ha_info_next;
520.1.22 by Brian Aker
Second pass of thd cleanup
702
  bool is_real_trans=all || session->transaction.all.ha_list == 0;
1 by brian
clean slate
703
704
  /*
705
    We must not rollback the normal transaction if a statement
706
    transaction is pending.
707
  */
520.1.22 by Brian Aker
Second pass of thd cleanup
708
  assert(session->transaction.stmt.ha_list == NULL ||
709
              trans == &session->transaction.stmt);
1 by brian
clean slate
710
711
  if (ha_info)
712
  {
713
    for (; ha_info; ha_info= ha_info_next)
714
    {
715
      int err;
960.2.37 by Monty Taylor
More naming fixes.
716
      StorageEngine *engine= ha_info->engine();
960.2.38 by Monty Taylor
Removed extraneous send myself to myself argument.
717
      if ((err= engine->rollback(session, all)))
1 by brian
clean slate
718
      { // cannot happen
719
        my_error(ER_ERROR_DURING_ROLLBACK, MYF(0), err);
720
        error=1;
721
      }
520.1.22 by Brian Aker
Second pass of thd cleanup
722
      status_var_increment(session->status_var.ha_rollback_count);
1 by brian
clean slate
723
      ha_info_next= ha_info->next();
724
      ha_info->reset(); /* keep it conveniently zero-filled */
725
    }
726
    trans->ha_list= 0;
727
    trans->no_2pc=0;
728
    if (is_real_trans)
520.1.22 by Brian Aker
Second pass of thd cleanup
729
      session->transaction.xid_state.xid.null();
1 by brian
clean slate
730
    if (all)
731
    {
520.1.22 by Brian Aker
Second pass of thd cleanup
732
      session->variables.tx_isolation=session->session_tx_isolation;
733
      session->transaction.cleanup();
1 by brian
clean slate
734
    }
735
  }
736
  if (all)
520.1.22 by Brian Aker
Second pass of thd cleanup
737
    session->transaction_rollback_request= false;
1 by brian
clean slate
738
739
  /*
740
    If a non-transactional table was updated, warn; don't warn if this is a
741
    slave thread (because when a slave thread executes a ROLLBACK, it has
742
    been read from the binary log, so it's 100% sure and normal to produce
743
    error ER_WARNING_NOT_COMPLETE_ROLLBACK. If we sent the warning to the
744
    slave SQL thread, it would not stop the thread but just be printed in
745
    the error log; but we don't want users to wonder why they have this
746
    message in the error log, so we don't send it.
747
  */
812 by Brian Aker
Merge and fix for scheduler.h
748
  if (is_real_trans && session->transaction.all.modified_non_trans_table && session->killed != Session::KILL_CONNECTION)
520.1.22 by Brian Aker
Second pass of thd cleanup
749
    push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
1 by brian
clean slate
750
                 ER_WARNING_NOT_COMPLETE_ROLLBACK,
751
                 ER(ER_WARNING_NOT_COMPLETE_ROLLBACK));
1046.1.9 by Brian Aker
Remove caller that wasn't correctly locking, and reverted code to 5.1
752
  return error;
1 by brian
clean slate
753
}
754
755
/**
756
  This is used to commit or rollback a single statement depending on
757
  the value of error.
758
759
  @note
760
    Note that if the autocommit is on, then the following call inside
761
    InnoDB will commit or rollback the whole transaction (= the statement). The
762
    autocommit mechanism built into InnoDB is based on counting locks, but if
763
    the user has used LOCK TABLES then that mechanism does not know to do the
764
    commit.
765
*/
520.1.22 by Brian Aker
Second pass of thd cleanup
766
int ha_autocommit_or_rollback(Session *session, int error)
1 by brian
clean slate
767
{
520.1.22 by Brian Aker
Second pass of thd cleanup
768
  if (session->transaction.stmt.ha_list)
1 by brian
clean slate
769
  {
770
    if (!error)
771
    {
520.1.22 by Brian Aker
Second pass of thd cleanup
772
      if (ha_commit_trans(session, 0))
1046.1.9 by Brian Aker
Remove caller that wasn't correctly locking, and reverted code to 5.1
773
        error= 1;
1 by brian
clean slate
774
    }
575.1.3 by Monty Taylor
Moved some stuff out of handler.h.
775
    else
1 by brian
clean slate
776
    {
520.1.22 by Brian Aker
Second pass of thd cleanup
777
      (void) ha_rollback_trans(session, 0);
778
      if (session->transaction_rollback_request)
779
        (void) ha_rollback(session);
1 by brian
clean slate
780
    }
781
520.1.22 by Brian Aker
Second pass of thd cleanup
782
    session->variables.tx_isolation=session->session_tx_isolation;
1 by brian
clean slate
783
  }
1046.1.9 by Brian Aker
Remove caller that wasn't correctly locking, and reverted code to 5.1
784
785
  return error;
1 by brian
clean slate
786
}
787
788
971.1.35 by Monty Taylor
One more plugin_foreach down.
789
1 by brian
clean slate
790
791
/**
792
  return the list of XID's to a client, the same way SHOW commands do.
793
794
  @note
795
    I didn't find in XA specs that an RM cannot return the same XID twice,
796
    so mysql_xa_recover does not filter XID's to ensure uniqueness.
797
    It can be easily fixed later, if necessary.
798
*/
520.1.22 by Brian Aker
Second pass of thd cleanup
799
bool mysql_xa_recover(Session *session)
1 by brian
clean slate
800
{
801
  List<Item> field_list;
520.1.22 by Brian Aker
Second pass of thd cleanup
802
  Protocol *protocol= session->protocol;
1 by brian
clean slate
803
  int i=0;
804
  XID_STATE *xs;
805
806
  field_list.push_back(new Item_int("formatID", 0, MY_INT32_NUM_DECIMAL_DIGITS));
807
  field_list.push_back(new Item_int("gtrid_length", 0, MY_INT32_NUM_DECIMAL_DIGITS));
808
  field_list.push_back(new Item_int("bqual_length", 0, MY_INT32_NUM_DECIMAL_DIGITS));
809
  field_list.push_back(new Item_empty_string("data",XIDDATASIZE));
810
971.3.19 by Eric Day
Finished first pass at Protocol cleanup, still some things to remove but they are a bit more involved.
811
  if (protocol->sendFields(&field_list,
812
                           Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF))
1046.1.9 by Brian Aker
Remove caller that wasn't correctly locking, and reverted code to 5.1
813
    return 1;
1 by brian
clean slate
814
815
  pthread_mutex_lock(&LOCK_xid_cache);
816
  while ((xs= (XID_STATE*)hash_element(&xid_cache, i++)))
817
  {
818
    if (xs->xa_state==XA_PREPARED)
819
    {
971.3.19 by Eric Day
Finished first pass at Protocol cleanup, still some things to remove but they are a bit more involved.
820
      protocol->prepareForResend();
971.3.17 by Eric Day
Cleaned up int/date related store functions.
821
      protocol->store((int64_t)xs->xid.formatID);
822
      protocol->store((int64_t)xs->xid.gtrid_length);
823
      protocol->store((int64_t)xs->xid.bqual_length);
1054.2.9 by Monty Taylor
Removed CHARSET_INFO stuff from protocol plugin interface - it makes no sense.
824
      protocol->store(xs->xid.data, xs->xid.gtrid_length+xs->xid.bqual_length);
1 by brian
clean slate
825
      if (protocol->write())
826
      {
827
        pthread_mutex_unlock(&LOCK_xid_cache);
1046.1.9 by Brian Aker
Remove caller that wasn't correctly locking, and reverted code to 5.1
828
        return 1;
1 by brian
clean slate
829
      }
830
    }
831
  }
832
833
  pthread_mutex_unlock(&LOCK_xid_cache);
836 by Brian Aker
Fixed session call from function to method.
834
  session->my_eof();
1046.1.9 by Brian Aker
Remove caller that wasn't correctly locking, and reverted code to 5.1
835
  return 0;
1 by brian
clean slate
836
}
837
838
520.1.22 by Brian Aker
Second pass of thd cleanup
839
int ha_rollback_to_savepoint(Session *session, SAVEPOINT *sv)
1 by brian
clean slate
840
{
1046.1.9 by Brian Aker
Remove caller that wasn't correctly locking, and reverted code to 5.1
841
  int error= 0;
520.1.22 by Brian Aker
Second pass of thd cleanup
842
  Session_TRANS *trans= &session->transaction.all;
1 by brian
clean slate
843
  Ha_trx_info *ha_info, *ha_info_next;
844
845
  trans->no_2pc=0;
846
  /*
847
    rolling back to savepoint in all storage engines that were part of the
848
    transaction when the savepoint was set
849
  */
850
  for (ha_info= sv->ha_list; ha_info; ha_info= ha_info->next())
851
  {
852
    int err;
960.2.37 by Monty Taylor
More naming fixes.
853
    StorageEngine *engine= ha_info->engine();
960.2.27 by Monty Taylor
Reworked transformed handlerton into class StorageEngine.
854
    assert(engine);
960.2.38 by Monty Taylor
Removed extraneous send myself to myself argument.
855
    if ((err= engine->savepoint_rollback(session,
964.1.6 by Monty Taylor
Moved savepoint stuff to protected virtual methods, moved the offset to private, and moved the logic about adding the offset to the passed-in pointer and adding the offset to the global offset inside of the class.
856
                                         (void *)(sv+1))))
1 by brian
clean slate
857
    { // cannot happen
858
      my_error(ER_ERROR_DURING_ROLLBACK, MYF(0), err);
1046.1.9 by Brian Aker
Remove caller that wasn't correctly locking, and reverted code to 5.1
859
      error= 1;
1 by brian
clean slate
860
    }
520.1.22 by Brian Aker
Second pass of thd cleanup
861
    status_var_increment(session->status_var.ha_savepoint_rollback_count);
960.2.27 by Monty Taylor
Reworked transformed handlerton into class StorageEngine.
862
    trans->no_2pc|= not engine->has_2pc();
1 by brian
clean slate
863
  }
864
  /*
865
    rolling back the transaction in all storage engines that were not part of
866
    the transaction when the savepoint was set
867
  */
868
  for (ha_info= trans->ha_list; ha_info != sv->ha_list;
869
       ha_info= ha_info_next)
870
  {
871
    int err;
960.2.37 by Monty Taylor
More naming fixes.
872
    StorageEngine *engine= ha_info->engine();
960.2.38 by Monty Taylor
Removed extraneous send myself to myself argument.
873
    if ((err= engine->rollback(session, !(0))))
1 by brian
clean slate
874
    { // cannot happen
875
      my_error(ER_ERROR_DURING_ROLLBACK, MYF(0), err);
1046.1.9 by Brian Aker
Remove caller that wasn't correctly locking, and reverted code to 5.1
876
      error= 1;
1 by brian
clean slate
877
    }
520.1.22 by Brian Aker
Second pass of thd cleanup
878
    status_var_increment(session->status_var.ha_rollback_count);
1 by brian
clean slate
879
    ha_info_next= ha_info->next();
880
    ha_info->reset(); /* keep it conveniently zero-filled */
881
  }
882
  trans->ha_list= sv->ha_list;
1046.1.9 by Brian Aker
Remove caller that wasn't correctly locking, and reverted code to 5.1
883
  return error;
1 by brian
clean slate
884
}
885
886
/**
887
  @note
888
  according to the sql standard (ISO/IEC 9075-2:2003)
889
  section "4.33.4 SQL-statements and transaction states",
890
  SAVEPOINT is *not* transaction-initiating SQL-statement
891
*/
520.1.22 by Brian Aker
Second pass of thd cleanup
892
int ha_savepoint(Session *session, SAVEPOINT *sv)
1 by brian
clean slate
893
{
1046.1.9 by Brian Aker
Remove caller that wasn't correctly locking, and reverted code to 5.1
894
  int error= 0;
520.1.22 by Brian Aker
Second pass of thd cleanup
895
  Session_TRANS *trans= &session->transaction.all;
1 by brian
clean slate
896
  Ha_trx_info *ha_info= trans->ha_list;
897
  for (; ha_info; ha_info= ha_info->next())
898
  {
899
    int err;
960.2.37 by Monty Taylor
More naming fixes.
900
    StorageEngine *engine= ha_info->engine();
960.2.27 by Monty Taylor
Reworked transformed handlerton into class StorageEngine.
901
    assert(engine);
1046.1.9 by Brian Aker
Remove caller that wasn't correctly locking, and reverted code to 5.1
902
#ifdef NOT_IMPLEMENTED /*- TODO (examine this againt the original code base) */
903
    if (! engine->savepoint_set)
1 by brian
clean slate
904
    {
905
      my_error(ER_CHECK_NOT_IMPLEMENTED, MYF(0), "SAVEPOINT");
1046.1.9 by Brian Aker
Remove caller that wasn't correctly locking, and reverted code to 5.1
906
      error= 1;
1 by brian
clean slate
907
      break;
1046.1.9 by Brian Aker
Remove caller that wasn't correctly locking, and reverted code to 5.1
908
    } 
909
#endif
964.1.6 by Monty Taylor
Moved savepoint stuff to protected virtual methods, moved the offset to private, and moved the logic about adding the offset to the passed-in pointer and adding the offset to the global offset inside of the class.
910
    if ((err= engine->savepoint_set(session, (void *)(sv+1))))
1 by brian
clean slate
911
    { // cannot happen
912
      my_error(ER_GET_ERRNO, MYF(0), err);
1046.1.9 by Brian Aker
Remove caller that wasn't correctly locking, and reverted code to 5.1
913
      error= 1;
1 by brian
clean slate
914
    }
520.1.22 by Brian Aker
Second pass of thd cleanup
915
    status_var_increment(session->status_var.ha_savepoint_count);
1 by brian
clean slate
916
  }
917
  /*
918
    Remember the list of registered storage engines. All new
919
    engines are prepended to the beginning of the list.
920
  */
921
  sv->ha_list= trans->ha_list;
1046.1.9 by Brian Aker
Remove caller that wasn't correctly locking, and reverted code to 5.1
922
  return error;
1 by brian
clean slate
923
}
924
520.1.22 by Brian Aker
Second pass of thd cleanup
925
int ha_release_savepoint(Session *session, SAVEPOINT *sv)
1 by brian
clean slate
926
{
1046.1.9 by Brian Aker
Remove caller that wasn't correctly locking, and reverted code to 5.1
927
  int error= 0;
1 by brian
clean slate
928
  Ha_trx_info *ha_info= sv->ha_list;
929
930
  for (; ha_info; ha_info= ha_info->next())
931
  {
932
    int err;
960.2.37 by Monty Taylor
More naming fixes.
933
    StorageEngine *engine= ha_info->engine();
1 by brian
clean slate
934
    /* Savepoint life time is enclosed into transaction life time. */
960.2.27 by Monty Taylor
Reworked transformed handlerton into class StorageEngine.
935
    assert(engine);
960.2.38 by Monty Taylor
Removed extraneous send myself to myself argument.
936
    if ((err= engine->savepoint_release(session,
964.1.6 by Monty Taylor
Moved savepoint stuff to protected virtual methods, moved the offset to private, and moved the logic about adding the offset to the passed-in pointer and adding the offset to the global offset inside of the class.
937
                                        (void *)(sv+1))))
1 by brian
clean slate
938
    { // cannot happen
939
      my_error(ER_GET_ERRNO, MYF(0), err);
1046.1.9 by Brian Aker
Remove caller that wasn't correctly locking, and reverted code to 5.1
940
      error= 1;
1 by brian
clean slate
941
    }
942
  }
1046.1.9 by Brian Aker
Remove caller that wasn't correctly locking, and reverted code to 5.1
943
  return error;
1 by brian
clean slate
944
}
945
946
947
948
949
950
/****************************************************************************
951
** General handler functions
952
****************************************************************************/
1022.2.29 by Monty Taylor
Fixed some no-inline warnings.
953
handler::~handler(void)
954
{
955
  assert(locked == false);
956
  /* TODO: assert(inited == NONE); */
957
}
958
959
1 by brian
clean slate
960
handler *handler::clone(MEM_ROOT *mem_root)
961
{
962
  handler *new_handler= get_new_handler(table->s, mem_root, table->s->db_type());
963
  /*
964
    Allocate handler->ref here because otherwise ha_open will allocate it
575.2.2 by Monty Taylor
Moved vio stuff into libdrizzle.
965
    on this->table->mem_root and we will not be able to reclaim that memory
1 by brian
clean slate
966
    when the clone handler object is destroyed.
967
  */
481 by Brian Aker
Remove all of uchar.
968
  if (!(new_handler->ref= (unsigned char*) alloc_root(mem_root, ALIGN_SIZE(ref_length)*2)))
1 by brian
clean slate
969
    return NULL;
970
  if (new_handler && !new_handler->ha_open(table,
971
                                           table->s->normalized_path.str,
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
972
                                           table->getDBStat(),
1 by brian
clean slate
973
                                           HA_OPEN_IGNORE_IF_LOCKED))
974
    return new_handler;
975
  return NULL;
976
}
977
575.1.4 by Monty Taylor
Moved implementation of some methods into handler.cc from handler.h.
978
int handler::ha_index_init(uint32_t idx, bool sorted)
979
{
980
  int result;
1046.1.9 by Brian Aker
Remove caller that wasn't correctly locking, and reverted code to 5.1
981
  assert(inited == NONE);
575.1.4 by Monty Taylor
Moved implementation of some methods into handler.cc from handler.h.
982
  if (!(result= index_init(idx, sorted)))
983
    inited=INDEX;
984
  end_range= NULL;
1046.1.9 by Brian Aker
Remove caller that wasn't correctly locking, and reverted code to 5.1
985
  return result;
575.1.4 by Monty Taylor
Moved implementation of some methods into handler.cc from handler.h.
986
}
987
988
int handler::ha_index_end()
989
{
990
  assert(inited==INDEX);
991
  inited=NONE;
992
  end_range= NULL;
993
  return(index_end());
994
}
995
996
int handler::ha_rnd_init(bool scan)
997
{
998
  int result;
999
  assert(inited==NONE || (inited==RND && scan));
1000
  inited= (result= rnd_init(scan)) ? NONE: RND;
1046.1.9 by Brian Aker
Remove caller that wasn't correctly locking, and reverted code to 5.1
1001
1002
  return result;
575.1.4 by Monty Taylor
Moved implementation of some methods into handler.cc from handler.h.
1003
}
1004
1005
int handler::ha_rnd_end()
1006
{
1007
  assert(inited==RND);
1008
  inited=NONE;
1009
  return(rnd_end());
1010
}
1011
1012
int handler::ha_index_or_rnd_end()
1013
{
1014
  return inited == INDEX ? ha_index_end() : inited == RND ? ha_rnd_end() : 0;
1015
}
1016
1017
handler::Table_flags handler::ha_table_flags() const
1018
{
1019
  return cached_table_flags;
1020
}
1021
1022
void handler::ha_start_bulk_insert(ha_rows rows)
1023
{
1024
  estimation_rows_to_insert= rows;
1025
  start_bulk_insert(rows);
1026
}
1027
1028
int handler::ha_end_bulk_insert()
1029
{
1030
  estimation_rows_to_insert= 0;
1031
  return end_bulk_insert();
1032
}
1033
1000.1.3 by Brian Aker
Renamed TABLE_SHARE to TableShare
1034
void handler::change_table_ptr(Table *table_arg, TableShare *share)
575.1.4 by Monty Taylor
Moved implementation of some methods into handler.cc from handler.h.
1035
{
1036
  table= table_arg;
1037
  table_share= share;
1038
}
1039
1040
const key_map *handler::keys_to_use_for_scanning()
1041
{
1042
  return &key_map_empty;
1043
}
1044
1045
bool handler::has_transactions()
1046
{
1047
  return (ha_table_flags() & HA_NO_TRANSACTIONS) == 0;
1048
}
1 by brian
clean slate
1049
1050
void handler::ha_statistic_increment(ulong SSV::*offset) const
1051
{
1052
  status_var_increment(table->in_use->status_var.*offset);
1053
}
1054
520.1.22 by Brian Aker
Second pass of thd cleanup
1055
void **handler::ha_data(Session *session) const
1 by brian
clean slate
1056
{
960.2.37 by Monty Taylor
More naming fixes.
1057
  return session_ha_data(session, engine);
1 by brian
clean slate
1058
}
1059
520.1.22 by Brian Aker
Second pass of thd cleanup
1060
Session *handler::ha_session(void) const
1 by brian
clean slate
1061
{
520.1.22 by Brian Aker
Second pass of thd cleanup
1062
  assert(!table || !table->in_use || table->in_use == current_session);
1063
  return (table && table->in_use) ? table->in_use : current_session;
1 by brian
clean slate
1064
}
1065
575.1.4 by Monty Taylor
Moved implementation of some methods into handler.cc from handler.h.
1066
1067
bool handler::is_fatal_error(int error, uint32_t flags)
1068
{
1069
  if (!error ||
1070
      ((flags & HA_CHECK_DUP_KEY) &&
1071
       (error == HA_ERR_FOUND_DUPP_KEY ||
1072
        error == HA_ERR_FOUND_DUPP_UNIQUE)))
1073
    return false;
1074
  return true;
1075
}
1076
575.1.5 by Monty Taylor
Moved stuff to handlerton.cc
1077
1078
ha_rows handler::records() { return stats.records; }
1079
1 by brian
clean slate
1080
/**
1081
  Open database-handler.
1082
1083
  Try O_RDONLY if cannot open as O_RDWR
1084
  Don't wait for locks if not HA_OPEN_WAIT_IF_LOCKED is set
1085
*/
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
1086
int handler::ha_open(Table *table_arg, const char *name, int mode,
1 by brian
clean slate
1087
                     int test_if_locked)
1088
{
1089
  int error;
1090
1091
  table= table_arg;
51.1.77 by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols
1092
  assert(table->s == table_share);
1093
  assert(alloc_root_inited(&table->mem_root));
1 by brian
clean slate
1094
1095
  if ((error=open(name,mode,test_if_locked)))
1096
  {
1097
    if ((error == EACCES || error == EROFS) && mode == O_RDWR &&
1098
	(table->db_stat & HA_TRY_READ_ONLY))
1099
    {
1100
      table->db_stat|=HA_READ_ONLY;
1101
      error=open(name,O_RDONLY,test_if_locked);
1102
    }
1103
  }
1104
  if (error)
1105
  {
1106
    my_errno= error;                            /* Safeguard */
1107
  }
1108
  else
1109
  {
1110
    if (table->s->db_options_in_use & HA_OPTION_READ_ONLY_DATA)
1111
      table->db_stat|=HA_READ_ONLY;
1112
    (void) extra(HA_EXTRA_NO_READCHECK);	// Not needed in SQL
1113
1114
    /* ref is already allocated for us if we're called from handler::clone() */
575.2.2 by Monty Taylor
Moved vio stuff into libdrizzle.
1115
    if (!ref && !(ref= (unsigned char*) alloc_root(&table->mem_root,
1 by brian
clean slate
1116
                                          ALIGN_SIZE(ref_length)*2)))
1117
    {
1118
      close();
1119
      error=HA_ERR_OUT_OF_MEM;
1120
    }
1121
    else
1122
      dup_ref=ref+ALIGN_SIZE(ref_length);
1123
    cached_table_flags= table_flags();
1124
  }
1046.1.9 by Brian Aker
Remove caller that wasn't correctly locking, and reverted code to 5.1
1125
  return error;
1 by brian
clean slate
1126
}
1127
1128
/**
1129
  one has to use this method when to find
1130
  random position by record as the plain
1131
  position() call doesn't work for some
1132
  handlers for random position
1133
*/
1134
481 by Brian Aker
Remove all of uchar.
1135
int handler::rnd_pos_by_record(unsigned char *record)
1 by brian
clean slate
1136
{
1137
  register int error;
1138
1139
  position(record);
1140
  if (inited && (error= ha_index_end()))
1046.1.9 by Brian Aker
Remove caller that wasn't correctly locking, and reverted code to 5.1
1141
    return error;
56 by brian
Next pass of true/false update.
1142
  if ((error= ha_rnd_init(false)))
1046.1.9 by Brian Aker
Remove caller that wasn't correctly locking, and reverted code to 5.1
1143
    return error;
1 by brian
clean slate
1144
1046.1.9 by Brian Aker
Remove caller that wasn't correctly locking, and reverted code to 5.1
1145
  return rnd_pos(record, ref);
1 by brian
clean slate
1146
}
1147
1148
/**
1149
  Read first row (only) from a table.
1150
1151
  This is never called for InnoDB tables, as these table types
1152
  has the HA_STATS_RECORDS_IS_EXACT set.
1153
*/
482 by Brian Aker
Remove uint.
1154
int handler::read_first_row(unsigned char * buf, uint32_t primary_key)
1 by brian
clean slate
1155
{
1156
  register int error;
1157
1158
  ha_statistic_increment(&SSV::ha_read_first_count);
1159
1160
  /*
1161
    If there is very few deleted rows in the table, find the first row by
1162
    scanning the table.
1163
    TODO remove the test for HA_READ_ORDER
1164
  */
1165
  if (stats.deleted < 10 || primary_key >= MAX_KEY ||
1166
      !(index_flags(primary_key, 0, 0) & HA_READ_ORDER))
1167
  {
1168
    (void) ha_rnd_init(1);
1169
    while ((error= rnd_next(buf)) == HA_ERR_RECORD_DELETED) ;
1170
    (void) ha_rnd_end();
1171
  }
1172
  else
1173
  {
1174
    /* Find the first row through the primary key */
1175
    (void) ha_index_init(primary_key, 0);
1176
    error=index_first(buf);
1177
    (void) ha_index_end();
1178
  }
1046.1.9 by Brian Aker
Remove caller that wasn't correctly locking, and reverted code to 5.1
1179
  return error;
1 by brian
clean slate
1180
}
1181
1182
/**
1183
  Generate the next auto-increment number based on increment and offset.
1184
  computes the lowest number
1185
  - strictly greater than "nr"
1186
  - of the form: auto_increment_offset + N * auto_increment_increment
1187
1188
  In most cases increment= offset= 1, in which case we get:
1189
  @verbatim 1,2,3,4,5,... @endverbatim
1190
    If increment=10 and offset=5 and previous number is 1, we get:
1191
  @verbatim 1,5,15,25,35,... @endverbatim
1192
*/
1193
inline uint64_t
1194
compute_next_insert_id(uint64_t nr,struct system_variables *variables)
1195
{
1196
  if (variables->auto_increment_increment == 1)
1197
    return (nr+1); // optimization of the formula below
1198
  nr= (((nr+ variables->auto_increment_increment -
1199
         variables->auto_increment_offset)) /
1200
       (uint64_t) variables->auto_increment_increment);
1201
  return (nr* (uint64_t) variables->auto_increment_increment +
1202
          variables->auto_increment_offset);
1203
}
1204
1205
1206
void handler::adjust_next_insert_id_after_explicit_value(uint64_t nr)
1207
{
1208
  /*
520.1.21 by Brian Aker
THD -> Session rename
1209
    If we have set Session::next_insert_id previously and plan to insert an
1 by brian
clean slate
1210
    explicitely-specified value larger than this, we need to increase
520.1.21 by Brian Aker
THD -> Session rename
1211
    Session::next_insert_id to be greater than the explicit value.
1 by brian
clean slate
1212
  */
1213
  if ((next_insert_id > 0) && (nr >= next_insert_id))
1214
    set_next_insert_id(compute_next_insert_id(nr, &table->in_use->variables));
1215
}
1216
1217
1218
/**
1219
  Compute a previous insert id
1220
1221
  Computes the largest number X:
1222
  - smaller than or equal to "nr"
1223
  - of the form: auto_increment_offset + N * auto_increment_increment
1224
    where N>=0.
1225
1226
  @param nr            Number to "round down"
1227
  @param variables     variables struct containing auto_increment_increment and
1228
                       auto_increment_offset
1229
1230
  @return
1231
    The number X if it exists, "nr" otherwise.
1232
*/
1233
inline uint64_t
1234
prev_insert_id(uint64_t nr, struct system_variables *variables)
1235
{
1236
  if (unlikely(nr < variables->auto_increment_offset))
1237
  {
1238
    /*
1239
      There's nothing good we can do here. That is a pathological case, where
1240
      the offset is larger than the column's max possible value, i.e. not even
1241
      the first sequence value may be inserted. User will receive warning.
1242
    */
1243
    return nr;
1244
  }
1245
  if (variables->auto_increment_increment == 1)
1246
    return nr; // optimization of the formula below
1247
  nr= (((nr - variables->auto_increment_offset)) /
1248
       (uint64_t) variables->auto_increment_increment);
1249
  return (nr * (uint64_t) variables->auto_increment_increment +
1250
          variables->auto_increment_offset);
1251
}
1252
1253
1254
/**
1255
  Update the auto_increment field if necessary.
1256
1257
  Updates columns with type NEXT_NUMBER if:
1258
1055.2.2 by Jay Pipes
Cleanup of style, indentation, and documentation of Table class members. Removed 5 or 6 dead Table member variables.
1259
  - If column value is set to NULL (in which case auto_increment_field_not_null is false)
1 by brian
clean slate
1260
  - If column is set to 0 and (sql_mode & MODE_NO_AUTO_VALUE_ON_ZERO) is not
1261
    set. In the future we will only set NEXT_NUMBER fields if one sets them
1262
    to NULL (or they are not included in the insert list).
1263
1264
    In those cases, we check if the currently reserved interval still has
1265
    values we have not used. If yes, we pick the smallest one and use it.
1266
    Otherwise:
1267
1268
  - If a list of intervals has been provided to the statement via SET
1269
    INSERT_ID or via an Intvar_log_event (in a replication slave), we pick the
1270
    first unused interval from this list, consider it as reserved.
1271
1272
  - Otherwise we set the column for the first row to the value
1273
    next_insert_id(get_auto_increment(column))) which is usually
1274
    max-used-column-value+1.
1275
    We call get_auto_increment() for the first row in a multi-row
1276
    statement. get_auto_increment() will tell us the interval of values it
1277
    reserved for us.
1278
1279
  - In both cases, for the following rows we use those reserved values without
1280
    calling the handler again (we just progress in the interval, computing
1281
    each new value from the previous one). Until we have exhausted them, then
1282
    we either take the next provided interval or call get_auto_increment()
1283
    again to reserve a new interval.
1284
1285
  - In both cases, the reserved intervals are remembered in
520.1.22 by Brian Aker
Second pass of thd cleanup
1286
    session->auto_inc_intervals_in_cur_stmt_for_binlog if statement-based
1 by brian
clean slate
1287
    binlogging; the last reserved interval is remembered in
1288
    auto_inc_interval_for_cur_row.
1289
1290
    The idea is that generated auto_increment values are predictable and
1291
    independent of the column values in the table.  This is needed to be
1292
    able to replicate into a table that already has rows with a higher
1293
    auto-increment value than the one that is inserted.
1294
1295
    After we have already generated an auto-increment number and the user
1296
    inserts a column with a higher value than the last used one, we will
1297
    start counting from the inserted value.
1298
1299
    This function's "outputs" are: the table's auto_increment field is filled
520.1.22 by Brian Aker
Second pass of thd cleanup
1300
    with a value, session->next_insert_id is filled with the value to use for the
1 by brian
clean slate
1301
    next row, if a value was autogenerated for the current row it is stored in
520.1.22 by Brian Aker
Second pass of thd cleanup
1302
    session->insert_id_for_cur_row, if get_auto_increment() was called
1303
    session->auto_inc_interval_for_cur_row is modified, if that interval is not
1304
    present in session->auto_inc_intervals_in_cur_stmt_for_binlog it is added to
1 by brian
clean slate
1305
    this list.
1306
1307
  @todo
1308
    Replace all references to "next number" or NEXT_NUMBER to
1309
    "auto_increment", everywhere (see below: there is
1310
    table->auto_increment_field_not_null, and there also exists
1311
    table->next_number_field, it's not consistent).
1312
1313
  @retval
1314
    0	ok
1315
  @retval
1316
    HA_ERR_AUTOINC_READ_FAILED  get_auto_increment() was called and
1317
    returned ~(uint64_t) 0
1318
  @retval
1319
    HA_ERR_AUTOINC_ERANGE storing value in field caused strict mode
1320
    failure.
1321
*/
1322
1323
#define AUTO_INC_DEFAULT_NB_ROWS 1 // Some prefer 1024 here
1324
#define AUTO_INC_DEFAULT_NB_MAX_BITS 16
1325
#define AUTO_INC_DEFAULT_NB_MAX ((1 << AUTO_INC_DEFAULT_NB_MAX_BITS) - 1)
1326
1327
int handler::update_auto_increment()
1328
{
1329
  uint64_t nr, nb_reserved_values;
56 by brian
Next pass of true/false update.
1330
  bool append= false;
520.1.22 by Brian Aker
Second pass of thd cleanup
1331
  Session *session= table->in_use;
1332
  struct system_variables *variables= &session->variables;
1 by brian
clean slate
1333
1334
  /*
1335
    next_insert_id is a "cursor" into the reserved interval, it may go greater
1336
    than the interval, but not smaller.
1337
  */
51.1.77 by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols
1338
  assert(next_insert_id >= auto_inc_interval_for_cur_row.minimum());
1 by brian
clean slate
1339
1055.2.2 by Jay Pipes
Cleanup of style, indentation, and documentation of Table class members. Removed 5 or 6 dead Table member variables.
1340
  /* We check if auto_increment_field_not_null is false
1008.3.2 by Stewart Smith
Make sql_mode=NO_AUTO_VALUE_ON_ZERO default for Drizzle.
1341
     for an auto increment column, not a magic value like NULL is.
1342
     same as sql_mode=NO_AUTO_VALUE_ON_ZERO */
1343
1344
  if ((nr= table->next_number_field->val_int()) != 0
1345
      || table->auto_increment_field_not_null)
1 by brian
clean slate
1346
  {
1347
    /*
1348
      Update next_insert_id if we had already generated a value in this
1349
      statement (case of INSERT VALUES(null),(3763),(null):
1350
      the last NULL needs to insert 3764, not the value of the first NULL plus
1351
      1).
1352
    */
1353
    adjust_next_insert_id_after_explicit_value(nr);
1354
    insert_id_for_cur_row= 0; // didn't generate anything
1046.1.9 by Brian Aker
Remove caller that wasn't correctly locking, and reverted code to 5.1
1355
1356
    return 0;
1 by brian
clean slate
1357
  }
1358
1359
  if ((nr= next_insert_id) >= auto_inc_interval_for_cur_row.maximum())
1360
  {
1361
    /* next_insert_id is beyond what is reserved, so we reserve more. */
1362
    const Discrete_interval *forced=
520.1.22 by Brian Aker
Second pass of thd cleanup
1363
      session->auto_inc_intervals_forced.get_next();
1 by brian
clean slate
1364
    if (forced != NULL)
1365
    {
1366
      nr= forced->minimum();
1367
      nb_reserved_values= forced->values();
1368
    }
1369
    else
1370
    {
1371
      /*
1372
        handler::estimation_rows_to_insert was set by
1373
        handler::ha_start_bulk_insert(); if 0 it means "unknown".
1374
      */
482 by Brian Aker
Remove uint.
1375
      uint32_t nb_already_reserved_intervals=
520.1.22 by Brian Aker
Second pass of thd cleanup
1376
        session->auto_inc_intervals_in_cur_stmt_for_binlog.nb_elements();
1 by brian
clean slate
1377
      uint64_t nb_desired_values;
1378
      /*
1379
        If an estimation was given to the engine:
1380
        - use it.
1381
        - if we already reserved numbers, it means the estimation was
1382
        not accurate, then we'll reserve 2*AUTO_INC_DEFAULT_NB_ROWS the 2nd
1383
        time, twice that the 3rd time etc.
1384
        If no estimation was given, use those increasing defaults from the
1385
        start, starting from AUTO_INC_DEFAULT_NB_ROWS.
1386
        Don't go beyond a max to not reserve "way too much" (because
1387
        reservation means potentially losing unused values).
1388
      */
1389
      if (nb_already_reserved_intervals == 0 &&
1390
          (estimation_rows_to_insert > 0))
1391
        nb_desired_values= estimation_rows_to_insert;
1392
      else /* go with the increasing defaults */
1393
      {
1394
        /* avoid overflow in formula, with this if() */
1395
        if (nb_already_reserved_intervals <= AUTO_INC_DEFAULT_NB_MAX_BITS)
1396
        {
575.2.2 by Monty Taylor
Moved vio stuff into libdrizzle.
1397
          nb_desired_values= AUTO_INC_DEFAULT_NB_ROWS *
1 by brian
clean slate
1398
            (1 << nb_already_reserved_intervals);
937.2.6 by Stewart Smith
make set_if_bigger typesafe for C and C++. Fix up everywhere.
1399
          set_if_smaller(nb_desired_values, (uint64_t)AUTO_INC_DEFAULT_NB_MAX);
1 by brian
clean slate
1400
        }
1401
        else
1402
          nb_desired_values= AUTO_INC_DEFAULT_NB_MAX;
1403
      }
1404
      /* This call ignores all its parameters but nr, currently */
1405
      get_auto_increment(variables->auto_increment_offset,
1406
                         variables->auto_increment_increment,
1407
                         nb_desired_values, &nr,
1408
                         &nb_reserved_values);
1409
      if (nr == ~(uint64_t) 0)
1046.1.9 by Brian Aker
Remove caller that wasn't correctly locking, and reverted code to 5.1
1410
        return HA_ERR_AUTOINC_READ_FAILED;  // Mark failure
575.2.2 by Monty Taylor
Moved vio stuff into libdrizzle.
1411
1 by brian
clean slate
1412
      /*
1413
        That rounding below should not be needed when all engines actually
1414
        respect offset and increment in get_auto_increment(). But they don't
1415
        so we still do it. Wonder if for the not-first-in-index we should do
1416
        it. Hope that this rounding didn't push us out of the interval; even
1417
        if it did we cannot do anything about it (calling the engine again
1418
        will not help as we inserted no row).
1419
      */
1420
      nr= compute_next_insert_id(nr-1, variables);
1421
    }
575.2.2 by Monty Taylor
Moved vio stuff into libdrizzle.
1422
1 by brian
clean slate
1423
    if (table->s->next_number_keypart == 0)
1424
    {
1425
      /* We must defer the appending until "nr" has been possibly truncated */
56 by brian
Next pass of true/false update.
1426
      append= true;
1 by brian
clean slate
1427
    }
1428
  }
1429
152 by Brian Aker
longlong replacement
1430
  if (unlikely(table->next_number_field->store((int64_t) nr, true)))
1 by brian
clean slate
1431
  {
1432
    /*
1433
      first test if the query was aborted due to strict mode constraints
1434
    */
520.1.22 by Brian Aker
Second pass of thd cleanup
1435
    if (session->killed == Session::KILL_BAD_DATA)
1046.1.9 by Brian Aker
Remove caller that wasn't correctly locking, and reverted code to 5.1
1436
      return HA_ERR_AUTOINC_ERANGE;
1 by brian
clean slate
1437
1438
    /*
1439
      field refused this value (overflow) and truncated it, use the result of
1440
      the truncation (which is going to be inserted); however we try to
1441
      decrease it to honour auto_increment_* variables.
1442
      That will shift the left bound of the reserved interval, we don't
1443
      bother shifting the right bound (anyway any other value from this
1444
      interval will cause a duplicate key).
1445
    */
1446
    nr= prev_insert_id(table->next_number_field->val_int(), variables);
152 by Brian Aker
longlong replacement
1447
    if (unlikely(table->next_number_field->store((int64_t) nr, true)))
1 by brian
clean slate
1448
      nr= table->next_number_field->val_int();
1449
  }
1450
  if (append)
1451
  {
1452
    auto_inc_interval_for_cur_row.replace(nr, nb_reserved_values,
1453
                                          variables->auto_increment_increment);
1454
  }
1455
1456
  /*
1457
    Record this autogenerated value. If the caller then
1458
    succeeds to insert this value, it will call
1459
    record_first_successful_insert_id_in_cur_stmt()
1460
    which will set first_successful_insert_id_in_cur_stmt if it's not
1461
    already set.
1462
  */
1463
  insert_id_for_cur_row= nr;
1464
  /*
1465
    Set next insert id to point to next auto-increment value to be able to
1466
    handle multi-row statements.
1467
  */
1468
  set_next_insert_id(compute_next_insert_id(nr, variables));
1469
1046.1.9 by Brian Aker
Remove caller that wasn't correctly locking, and reverted code to 5.1
1470
  return 0;
1 by brian
clean slate
1471
}
1472
1473
1474
/**
1475
  Reserves an interval of auto_increment values from the handler.
1476
1477
  offset and increment means that we want values to be of the form
1478
  offset + N * increment, where N>=0 is integer.
1479
  If the function sets *first_value to ~(uint64_t)0 it means an error.
163 by Brian Aker
Merge Monty's code.
1480
  If the function sets *nb_reserved_values to UINT64_MAX it means it has
1 by brian
clean slate
1481
  reserved to "positive infinite".
1482
1483
  @param offset
1484
  @param increment
1485
  @param nb_desired_values   how many values we want
1486
  @param first_value         (OUT) the first value reserved by the handler
1487
  @param nb_reserved_values  (OUT) how many values the handler reserved
1488
*/
779.1.27 by Monty Taylor
Got rid of __attribute__((unused)) and the like from the .cc files.
1489
void handler::get_auto_increment(uint64_t ,
1490
                                 uint64_t ,
1491
                                 uint64_t ,
1 by brian
clean slate
1492
                                 uint64_t *first_value,
1493
                                 uint64_t *nb_reserved_values)
1494
{
1495
  uint64_t nr;
1496
  int error;
1497
1498
  (void) extra(HA_EXTRA_KEYREAD);
1003.1.7 by Brian Aker
Add mark_columns_used_by_index_no_reset() with just index (assumes read_set
1499
  table->mark_columns_used_by_index_no_reset(table->s->next_number_index);
1 by brian
clean slate
1500
  index_init(table->s->next_number_index, 1);
1501
  if (table->s->next_number_keypart == 0)
1502
  {						// Autoincrement at key-start
1503
    error=index_last(table->record[1]);
1504
    /*
1505
      MySQL implicitely assumes such method does locking (as MySQL decides to
1506
      use nr+increment without checking again with the handler, in
1507
      handler::update_auto_increment()), so reserves to infinite.
1508
    */
163 by Brian Aker
Merge Monty's code.
1509
    *nb_reserved_values= UINT64_MAX;
1 by brian
clean slate
1510
  }
1511
  else
1512
  {
481 by Brian Aker
Remove all of uchar.
1513
    unsigned char key[MAX_KEY_LENGTH];
1 by brian
clean slate
1514
    key_copy(key, table->record[0],
1515
             table->key_info + table->s->next_number_index,
1516
             table->s->next_number_key_offset);
1517
    error= index_read_map(table->record[1], key,
1518
                          make_prev_keypart_map(table->s->next_number_keypart),
1519
                          HA_READ_PREFIX_LAST);
1520
    /*
1521
      MySQL needs to call us for next row: assume we are inserting ("a",null)
1522
      here, we return 3, and next this statement will want to insert
1523
      ("b",null): there is no reason why ("b",3+1) would be the good row to
1524
      insert: maybe it already exists, maybe 3+1 is too large...
1525
    */
1526
    *nb_reserved_values= 1;
1527
  }
1528
1529
  if (error)
1530
    nr=1;
1531
  else
1532
    nr= ((uint64_t) table->next_number_field->
1533
         val_int_offset(table->s->rec_buff_length)+1);
1534
  index_end();
1535
  (void) extra(HA_EXTRA_NO_KEYREAD);
1536
  *first_value= nr;
1537
}
1538
1539
1540
void handler::ha_release_auto_increment()
1541
{
1542
  release_auto_increment();
1543
  insert_id_for_cur_row= 0;
1544
  auto_inc_interval_for_cur_row.replace(0, 0, 0);
1545
  if (next_insert_id > 0)
1546
  {
1547
    next_insert_id= 0;
1548
    /*
1549
      this statement used forced auto_increment values if there were some,
1550
      wipe them away for other statements.
1551
    */
1552
    table->in_use->auto_inc_intervals_forced.empty();
1553
  }
1554
}
1555
1556
482 by Brian Aker
Remove uint.
1557
void handler::print_keydup_error(uint32_t key_nr, const char *msg)
1 by brian
clean slate
1558
{
1559
  /* Write the duplicated key in the error message */
1560
  char key[MAX_KEY_LENGTH];
1561
  String str(key,sizeof(key),system_charset_info);
1562
1563
  if (key_nr == MAX_KEY)
1564
  {
1565
    /* Key is unknown */
1566
    str.copy("", 0, system_charset_info);
1567
    my_printf_error(ER_DUP_ENTRY, msg, MYF(0), str.c_ptr(), "*UNKNOWN*");
1568
  }
1569
  else
1570
  {
1571
    /* Table is opened and defined at this point */
895 by Brian Aker
Completion (?) of uint conversion.
1572
    key_unpack(&str,table,(uint32_t) key_nr);
1573
    uint32_t max_length=DRIZZLE_ERRMSG_SIZE-(uint32_t) strlen(msg);
1 by brian
clean slate
1574
    if (str.length() >= max_length)
1575
    {
1576
      str.length(max_length-4);
1577
      str.append(STRING_WITH_LEN("..."));
1578
    }
1579
    my_printf_error(ER_DUP_ENTRY, msg,
1580
		    MYF(0), str.c_ptr(), table->key_info[key_nr].name);
1581
  }
1582
}
1583
1584
1585
/**
1586
  Print error that we got from handler function.
1587
1588
  @note
1589
    In case of delete table it's only safe to use the following parts of
1590
    the 'table' structure:
1591
    - table->s->path
1592
    - table->alias
1593
*/
1594
void handler::print_error(int error, myf errflag)
1595
{
1596
  int textno=ER_GET_ERRNO;
1597
  switch (error) {
1598
  case EACCES:
1599
    textno=ER_OPEN_AS_READONLY;
1600
    break;
1601
  case EAGAIN:
1602
    textno=ER_FILE_USED;
1603
    break;
1604
  case ENOENT:
1605
    textno=ER_FILE_NOT_FOUND;
1606
    break;
1607
  case HA_ERR_KEY_NOT_FOUND:
1608
  case HA_ERR_NO_ACTIVE_RECORD:
1609
  case HA_ERR_END_OF_FILE:
1610
    textno=ER_KEY_NOT_FOUND;
1611
    break;
1612
  case HA_ERR_WRONG_MRG_TABLE_DEF:
1613
    textno=ER_WRONG_MRG_TABLE;
1614
    break;
1615
  case HA_ERR_FOUND_DUPP_KEY:
1616
  {
482 by Brian Aker
Remove uint.
1617
    uint32_t key_nr=get_dup_key(error);
1 by brian
clean slate
1618
    if ((int) key_nr >= 0)
1619
    {
1620
      print_keydup_error(key_nr, ER(ER_DUP_ENTRY_WITH_KEY_NAME));
51.1.77 by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols
1621
      return;
1 by brian
clean slate
1622
    }
1623
    textno=ER_DUP_KEY;
1624
    break;
1625
  }
1626
  case HA_ERR_FOREIGN_DUPLICATE_KEY:
1627
  {
482 by Brian Aker
Remove uint.
1628
    uint32_t key_nr= get_dup_key(error);
1 by brian
clean slate
1629
    if ((int) key_nr >= 0)
1630
    {
482 by Brian Aker
Remove uint.
1631
      uint32_t max_length;
1 by brian
clean slate
1632
      /* Write the key in the error message */
1633
      char key[MAX_KEY_LENGTH];
1634
      String str(key,sizeof(key),system_charset_info);
1635
      /* Table is opened and defined at this point */
895 by Brian Aker
Completion (?) of uint conversion.
1636
      key_unpack(&str,table,(uint32_t) key_nr);
261.4.1 by Felipe
- Renamed MYSQL_ERROR to DRIZZLE_ERROR.
1637
      max_length= (DRIZZLE_ERRMSG_SIZE-
895 by Brian Aker
Completion (?) of uint conversion.
1638
                   (uint32_t) strlen(ER(ER_FOREIGN_DUPLICATE_KEY)));
1 by brian
clean slate
1639
      if (str.length() >= max_length)
1640
      {
1641
        str.length(max_length-4);
1642
        str.append(STRING_WITH_LEN("..."));
1643
      }
1644
      my_error(ER_FOREIGN_DUPLICATE_KEY, MYF(0), table_share->table_name.str,
1645
        str.c_ptr(), key_nr+1);
51.1.77 by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols
1646
      return;
1 by brian
clean slate
1647
    }
1648
    textno= ER_DUP_KEY;
1649
    break;
1650
  }
1651
  case HA_ERR_FOUND_DUPP_UNIQUE:
1652
    textno=ER_DUP_UNIQUE;
1653
    break;
1654
  case HA_ERR_RECORD_CHANGED:
1655
    textno=ER_CHECKREAD;
1656
    break;
1657
  case HA_ERR_CRASHED:
1658
    textno=ER_NOT_KEYFILE;
1659
    break;
1660
  case HA_ERR_WRONG_IN_RECORD:
1661
    textno= ER_CRASHED_ON_USAGE;
1662
    break;
1663
  case HA_ERR_CRASHED_ON_USAGE:
1664
    textno=ER_CRASHED_ON_USAGE;
1665
    break;
1666
  case HA_ERR_NOT_A_TABLE:
1001.1.3 by Andrew Ettinger
Revert bad spacing
1667
    textno= error;
1 by brian
clean slate
1668
    break;
1669
  case HA_ERR_CRASHED_ON_REPAIR:
1670
    textno=ER_CRASHED_ON_REPAIR;
1671
    break;
1672
  case HA_ERR_OUT_OF_MEM:
1673
    textno=ER_OUT_OF_RESOURCES;
1674
    break;
1675
  case HA_ERR_WRONG_COMMAND:
1676
    textno=ER_ILLEGAL_HA;
1677
    break;
1678
  case HA_ERR_OLD_FILE:
1679
    textno=ER_OLD_KEYFILE;
1680
    break;
1681
  case HA_ERR_UNSUPPORTED:
1682
    textno=ER_UNSUPPORTED_EXTENSION;
1683
    break;
1684
  case HA_ERR_RECORD_FILE_FULL:
1685
  case HA_ERR_INDEX_FILE_FULL:
1686
    textno=ER_RECORD_FILE_FULL;
1687
    break;
1688
  case HA_ERR_LOCK_WAIT_TIMEOUT:
1689
    textno=ER_LOCK_WAIT_TIMEOUT;
1690
    break;
1691
  case HA_ERR_LOCK_TABLE_FULL:
1692
    textno=ER_LOCK_TABLE_FULL;
1693
    break;
1694
  case HA_ERR_LOCK_DEADLOCK:
1695
    textno=ER_LOCK_DEADLOCK;
1696
    break;
1697
  case HA_ERR_READ_ONLY_TRANSACTION:
1698
    textno=ER_READ_ONLY_TRANSACTION;
1699
    break;
1700
  case HA_ERR_CANNOT_ADD_FOREIGN:
1701
    textno=ER_CANNOT_ADD_FOREIGN;
1702
    break;
1703
  case HA_ERR_ROW_IS_REFERENCED:
1704
  {
1705
    String str;
1706
    get_error_message(error, &str);
1707
    my_error(ER_ROW_IS_REFERENCED_2, MYF(0), str.c_ptr_safe());
51.1.77 by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols
1708
    return;
1 by brian
clean slate
1709
  }
1710
  case HA_ERR_NO_REFERENCED_ROW:
1711
  {
1712
    String str;
1713
    get_error_message(error, &str);
1714
    my_error(ER_NO_REFERENCED_ROW_2, MYF(0), str.c_ptr_safe());
51.1.77 by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols
1715
    return;
1 by brian
clean slate
1716
  }
1717
  case HA_ERR_TABLE_DEF_CHANGED:
1718
    textno=ER_TABLE_DEF_CHANGED;
1719
    break;
1720
  case HA_ERR_NO_SUCH_TABLE:
1721
    my_error(ER_NO_SUCH_TABLE, MYF(0), table_share->db.str,
1722
             table_share->table_name.str);
51.1.77 by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols
1723
    return;
1 by brian
clean slate
1724
  case HA_ERR_RBR_LOGGING_FAILED:
1725
    textno= ER_BINLOG_ROW_LOGGING_FAILED;
1726
    break;
1727
  case HA_ERR_DROP_INDEX_FK:
1728
  {
1729
    const char *ptr= "???";
482 by Brian Aker
Remove uint.
1730
    uint32_t key_nr= get_dup_key(error);
1 by brian
clean slate
1731
    if ((int) key_nr >= 0)
1732
      ptr= table->key_info[key_nr].name;
1733
    my_error(ER_DROP_INDEX_FK, MYF(0), ptr);
51.1.77 by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols
1734
    return;
1 by brian
clean slate
1735
  }
1736
  case HA_ERR_TABLE_NEEDS_UPGRADE:
1737
    textno=ER_TABLE_NEEDS_UPGRADE;
1738
    break;
1739
  case HA_ERR_TABLE_READONLY:
1740
    textno= ER_OPEN_AS_READONLY;
1741
    break;
1742
  case HA_ERR_AUTOINC_READ_FAILED:
1743
    textno= ER_AUTOINC_READ_FAILED;
1744
    break;
1745
  case HA_ERR_AUTOINC_ERANGE:
1746
    textno= ER_WARN_DATA_OUT_OF_RANGE;
1747
    break;
1748
  case HA_ERR_LOCK_OR_ACTIVE_TRANSACTION:
1749
    my_message(ER_LOCK_OR_ACTIVE_TRANSACTION,
1750
               ER(ER_LOCK_OR_ACTIVE_TRANSACTION), MYF(0));
51.1.77 by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols
1751
    return;
1 by brian
clean slate
1752
  default:
1753
    {
1754
      /* The error was "unknown" to this function.
1755
	 Ask handler if it has got a message for this error */
56 by brian
Next pass of true/false update.
1756
      bool temporary= false;
1 by brian
clean slate
1757
      String str;
1758
      temporary= get_error_message(error, &str);
1759
      if (!str.is_empty())
1760
      {
1008.3.26 by Stewart Smith
remove handler::table_type() as same information can be retrieved from handler::engine->getName()
1761
        const char* engine_name= engine->getName().c_str();
1762
        if (temporary)
1763
          my_error(ER_GET_TEMPORARY_ERRMSG, MYF(0), error, str.ptr(),
960.2.37 by Monty Taylor
More naming fixes.
1764
                   engine_name);
1008.3.26 by Stewart Smith
remove handler::table_type() as same information can be retrieved from handler::engine->getName()
1765
        else
1766
          my_error(ER_GET_ERRMSG, MYF(0), error, str.ptr(), engine_name);
1 by brian
clean slate
1767
      }
1768
      else
960.2.37 by Monty Taylor
More naming fixes.
1769
      {
1770
	      my_error(ER_GET_ERRNO,errflag,error);
1771
      }
51.1.77 by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols
1772
      return;
1 by brian
clean slate
1773
    }
1774
  }
1775
  my_error(textno, errflag, table_share->table_name.str, error);
1776
}
1777
1778
1779
/**
1780
  Return an error message specific to this handler.
1781
1782
  @param error  error code previously returned by handler
1783
  @param buf    pointer to String where to add error message
1784
1785
  @return
1786
    Returns true if this is a temporary error
1787
*/
1046.1.9 by Brian Aker
Remove caller that wasn't correctly locking, and reverted code to 5.1
1788
bool handler::get_error_message(int , String* )
1 by brian
clean slate
1789
{
56 by brian
Next pass of true/false update.
1790
  return false;
1 by brian
clean slate
1791
}
1792
1793
1794
/* Code left, but Drizzle has no legacy yet (while MySQL did) */
1795
int handler::check_old_types()
1796
{
1797
  return 0;
1798
}
1799
1800
/**
1801
  @return
1802
    key if error because of duplicated keys
1803
*/
482 by Brian Aker
Remove uint.
1804
uint32_t handler::get_dup_key(int error)
1 by brian
clean slate
1805
{
895 by Brian Aker
Completion (?) of uint conversion.
1806
  table->file->errkey  = (uint32_t) -1;
1 by brian
clean slate
1807
  if (error == HA_ERR_FOUND_DUPP_KEY || error == HA_ERR_FOREIGN_DUPLICATE_KEY ||
1808
      error == HA_ERR_FOUND_DUPP_UNIQUE ||
1809
      error == HA_ERR_DROP_INDEX_FK)
1810
    info(HA_STATUS_ERRKEY | HA_STATUS_NO_LOCK);
51.1.77 by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols
1811
  return(table->file->errkey);
1 by brian
clean slate
1812
}
1813
1814
void handler::drop_table(const char *name)
1815
{
1816
  close();
1039.3.12 by Stewart Smith
remove handler::ha_delete_table and have StorageEngine::deleteTable instead
1817
  engine->deleteTable(ha_session(), name);
1 by brian
clean slate
1818
}
1819
1820
1821
/**
1822
  Performs checks upon the table.
1823
520.1.22 by Brian Aker
Second pass of thd cleanup
1824
  @param session                thread doing CHECK Table operation
1 by brian
clean slate
1825
  @param check_opt          options from the parser
1826
1827
  @retval
1828
    HA_ADMIN_OK               Successful upgrade
1829
  @retval
1830
    HA_ADMIN_NEEDS_UPGRADE    Table has structures requiring upgrade
1831
  @retval
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
1832
    HA_ADMIN_NEEDS_ALTER      Table has structures requiring ALTER Table
1 by brian
clean slate
1833
  @retval
1834
    HA_ADMIN_NOT_IMPLEMENTED
1835
*/
1039.1.16 by Brian Aker
A lot of little cleanups (most based off lcov)
1836
int handler::ha_check(Session *, HA_CHECK_OPT *)
1 by brian
clean slate
1837
{
590.1.4 by Stewart Smith
remove frm_version from TABLE_SHARE
1838
  return HA_ADMIN_OK;
1 by brian
clean slate
1839
}
1840
1841
/**
1842
  A helper function to mark a transaction read-write,
1843
  if it is started.
1844
*/
1845
1846
inline
1847
void
1848
handler::mark_trx_read_write()
1849
{
1030.1.1 by Brian Aker
Straighten out structures (remove some some dead bits).
1850
  Ha_trx_info *ha_info= &ha_session()->ha_data[engine->getSlot()].ha_info[0];
1 by brian
clean slate
1851
  /*
1852
    When a storage engine method is called, the transaction must
1853
    have been started, unless it's a DDL call, for which the
1854
    storage engine starts the transaction internally, and commits
1855
    it internally, without registering in the ha_list.
1856
    Unfortunately here we can't know know for sure if the engine
1857
    has registered the transaction or not, so we must check.
1858
  */
1859
  if (ha_info->is_started())
1860
  {
1861
    /*
1862
      table_share can be NULL in ha_delete_table(). See implementation
1863
      of standalone function ha_delete_table() in sql_base.cc.
1864
    */
1039.3.8 by Stewart Smith
Re-introduce marking transaction as read-write for engine on create_table:
1865
//    if (table_share == NULL || table_share->tmp_table == NO_TMP_TABLE)
1 by brian
clean slate
1866
      ha_info->set_trx_read_write();
1867
  }
1868
}
1869
1870
/**
1871
  Bulk update row: public interface.
1872
1873
  @sa handler::bulk_update_row()
1874
*/
1875
1876
int
481 by Brian Aker
Remove all of uchar.
1877
handler::ha_bulk_update_row(const unsigned char *old_data, unsigned char *new_data,
482 by Brian Aker
Remove uint.
1878
                            uint32_t *dup_key_found)
1 by brian
clean slate
1879
{
1880
  mark_trx_read_write();
1881
1882
  return bulk_update_row(old_data, new_data, dup_key_found);
1883
}
1884
1885
1886
/**
1887
  Delete all rows: public interface.
1888
1889
  @sa handler::delete_all_rows()
1890
*/
1891
1892
int
1893
handler::ha_delete_all_rows()
1894
{
1895
  mark_trx_read_write();
1896
1897
  return delete_all_rows();
1898
}
1899
1900
1901
/**
1902
  Reset auto increment: public interface.
1903
1904
  @sa handler::reset_auto_increment()
1905
*/
1906
1907
int
1908
handler::ha_reset_auto_increment(uint64_t value)
1909
{
1910
  mark_trx_read_write();
1911
1912
  return reset_auto_increment(value);
1913
}
1914
1915
1916
/**
1917
  Optimize table: public interface.
1918
1919
  @sa handler::optimize()
1920
*/
1921
1922
int
520.1.22 by Brian Aker
Second pass of thd cleanup
1923
handler::ha_optimize(Session* session, HA_CHECK_OPT* check_opt)
1 by brian
clean slate
1924
{
1925
  mark_trx_read_write();
1926
520.1.22 by Brian Aker
Second pass of thd cleanup
1927
  return optimize(session, check_opt);
1 by brian
clean slate
1928
}
1929
1930
1931
/**
1932
  Analyze table: public interface.
1933
1934
  @sa handler::analyze()
1935
*/
1936
1937
int
520.1.22 by Brian Aker
Second pass of thd cleanup
1938
handler::ha_analyze(Session* session, HA_CHECK_OPT* check_opt)
1 by brian
clean slate
1939
{
1940
  mark_trx_read_write();
1941
520.1.22 by Brian Aker
Second pass of thd cleanup
1942
  return analyze(session, check_opt);
1 by brian
clean slate
1943
}
1944
1945
/**
1946
  Disable indexes: public interface.
1947
1948
  @sa handler::disable_indexes()
1949
*/
1950
1951
int
482 by Brian Aker
Remove uint.
1952
handler::ha_disable_indexes(uint32_t mode)
1 by brian
clean slate
1953
{
1954
  mark_trx_read_write();
1955
1956
  return disable_indexes(mode);
1957
}
1958
1959
1960
/**
1961
  Enable indexes: public interface.
1962
1963
  @sa handler::enable_indexes()
1964
*/
1965
1966
int
482 by Brian Aker
Remove uint.
1967
handler::ha_enable_indexes(uint32_t mode)
1 by brian
clean slate
1968
{
1969
  mark_trx_read_write();
1970
1971
  return enable_indexes(mode);
1972
}
1973
1974
1975
/**
1976
  Discard or import tablespace: public interface.
1977
1978
  @sa handler::discard_or_import_tablespace()
1979
*/
1980
1981
int
200 by Brian Aker
my_bool from handler and set_var
1982
handler::ha_discard_or_import_tablespace(bool discard)
1 by brian
clean slate
1983
{
1984
  mark_trx_read_write();
1985
1986
  return discard_or_import_tablespace(discard);
1987
}
1988
1989
1990
/**
1991
  Prepare for alter: public interface.
1992
1993
  Called to prepare an *online* ALTER.
1994
1995
  @sa handler::prepare_for_alter()
1996
*/
1997
1998
void
1999
handler::ha_prepare_for_alter()
2000
{
2001
  mark_trx_read_write();
2002
2003
  prepare_for_alter();
2004
}
2005
2006
/**
2007
  Drop table in the engine: public interface.
2008
2009
  @sa handler::drop_table()
2010
*/
2011
2012
void
2013
handler::ha_drop_table(const char *name)
2014
{
2015
  mark_trx_read_write();
2016
2017
  return drop_table(name);
2018
}
2019
2020
/**
2021
  Tell the storage engine that it is allowed to "disable transaction" in the
2022
  handler. It is a hint that ACID is not required - it is used in NDB for
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
2023
  ALTER Table, for example, when data are copied to temporary table.
1 by brian
clean slate
2024
  A storage engine may treat this hint any way it likes. NDB for example
2025
  starts to commit every now and then automatically.
2026
  This hint can be safely ignored.
2027
*/
520.1.22 by Brian Aker
Second pass of thd cleanup
2028
int ha_enable_transaction(Session *session, bool on)
1 by brian
clean slate
2029
{
1046.1.9 by Brian Aker
Remove caller that wasn't correctly locking, and reverted code to 5.1
2030
  int error= 0;
1 by brian
clean slate
2031
520.1.22 by Brian Aker
Second pass of thd cleanup
2032
  if ((session->transaction.on= on))
1 by brian
clean slate
2033
  {
2034
    /*
2035
      Now all storage engines should have transaction handling enabled.
2036
      But some may have it enabled all the time - "disabling" transactions
2037
      is an optimization hint that storage engine is free to ignore.
2038
      So, let's commit an open transaction (if any) now.
2039
    */
520.1.22 by Brian Aker
Second pass of thd cleanup
2040
    if (!(error= ha_commit_trans(session, 0)))
934.2.11 by Jay Pipes
Moves end_trans(), begin_trans(), end_active_trans() out of the parser module and adds startTransaction(), endTransaction(), and endActiveTransaction() member methods of Session object.
2041
      if (! session->endTransaction(COMMIT))
2042
        error= 1;
2043
1 by brian
clean slate
2044
  }
1046.1.9 by Brian Aker
Remove caller that wasn't correctly locking, and reverted code to 5.1
2045
  return error;
1 by brian
clean slate
2046
}
2047
482 by Brian Aker
Remove uint.
2048
int handler::index_next_same(unsigned char *buf, const unsigned char *key, uint32_t keylen)
1 by brian
clean slate
2049
{
2050
  int error;
2051
  if (!(error=index_next(buf)))
2052
  {
2053
    my_ptrdiff_t ptrdiff= buf - table->record[0];
481 by Brian Aker
Remove all of uchar.
2054
    unsigned char *save_record_0= NULL;
1 by brian
clean slate
2055
    KEY *key_info= NULL;
2056
    KEY_PART_INFO *key_part;
2057
    KEY_PART_INFO *key_part_end= NULL;
2058
2059
    /*
2060
      key_cmp_if_same() compares table->record[0] against 'key'.
2061
      In parts it uses table->record[0] directly, in parts it uses
2062
      field objects with their local pointers into table->record[0].
2063
      If 'buf' is distinct from table->record[0], we need to move
2064
      all record references. This is table->record[0] itself and
2065
      the field pointers of the fields used in this key.
2066
    */
2067
    if (ptrdiff)
2068
    {
2069
      save_record_0= table->record[0];
2070
      table->record[0]= buf;
2071
      key_info= table->key_info + active_index;
2072
      key_part= key_info->key_part;
2073
      key_part_end= key_part + key_info->key_parts;
2074
      for (; key_part < key_part_end; key_part++)
2075
      {
51.1.77 by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols
2076
        assert(key_part->field);
1 by brian
clean slate
2077
        key_part->field->move_field_offset(ptrdiff);
2078
      }
2079
    }
2080
2081
    if (key_cmp_if_same(table, key, active_index, keylen))
2082
    {
2083
      table->status=STATUS_NOT_FOUND;
2084
      error=HA_ERR_END_OF_FILE;
2085
    }
2086
2087
    /* Move back if necessary. */
2088
    if (ptrdiff)
2089
    {
2090
      table->record[0]= save_record_0;
2091
      for (key_part= key_info->key_part; key_part < key_part_end; key_part++)
2092
        key_part->field->move_field_offset(-ptrdiff);
2093
    }
2094
  }
1046.1.9 by Brian Aker
Remove caller that wasn't correctly locking, and reverted code to 5.1
2095
  return error;
1 by brian
clean slate
2096
}
2097
2098
2099
/****************************************************************************
2100
** Some general functions that isn't in the handler class
2101
****************************************************************************/
2102
2103
2104
void st_ha_check_opt::init()
2105
{
792 by Brian Aker
Refactor for myisam specifics in handler
2106
  flags= 0; 
2107
  use_frm= false;
1 by brian
clean slate
2108
}
2109
2110
/**
2111
  Calculate cost of 'index only' scan for given index and number of records
2112
2113
  @param keynr    Index number
2114
  @param records  Estimated number of records to be retrieved
2115
2116
  @note
2117
    It is assumed that we will read trough the whole key range and that all
2118
    key blocks are half full (normally things are much better). It is also
2119
    assumed that each time we read the next key from the index, the handler
2120
    performs a random seek, thus the cost is proportional to the number of
2121
    blocks read.
2122
2123
  @todo
2124
    Consider joining this function and handler::read_time() into one
2125
    handler::read_time(keynr, records, ranges, bool index_only) function.
2126
2127
  @return
2128
    Estimated cost of 'index only' scan
2129
*/
2130
779.3.10 by Monty Taylor
Turned on -Wshadow.
2131
double handler::index_only_read_time(uint32_t keynr, double key_records)
1 by brian
clean slate
2132
{
482 by Brian Aker
Remove uint.
2133
  uint32_t keys_per_block= (stats.block_size/2/
1 by brian
clean slate
2134
			(table->key_info[keynr].key_length + ref_length) + 1);
779.3.10 by Monty Taylor
Turned on -Wshadow.
2135
  return ((double) (key_records + keys_per_block-1) /
2136
          (double) keys_per_block);
1 by brian
clean slate
2137
}
2138
2139
2140
/****************************************************************************
2141
 * Default MRR implementation (MRR to non-MRR converter)
2142
 ***************************************************************************/
2143
2144
/**
2145
  Get cost and other information about MRR scan over a known list of ranges
2146
2147
  Calculate estimated cost and other information about an MRR scan for given
2148
  sequence of ranges.
2149
2150
  @param keyno           Index number
2151
  @param seq             Range sequence to be traversed
2152
  @param seq_init_param  First parameter for seq->init()
2153
  @param n_ranges_arg    Number of ranges in the sequence, or 0 if the caller
2154
                         can't efficiently determine it
2155
  @param bufsz    INOUT  IN:  Size of the buffer available for use
2156
                         OUT: Size of the buffer that is expected to be actually
2157
                              used, or 0 if buffer is not needed.
2158
  @param flags    INOUT  A combination of HA_MRR_* flags
2159
  @param cost     OUT    Estimated cost of MRR access
2160
2161
  @note
2162
    This method (or an overriding one in a derived class) must check for
520.1.22 by Brian Aker
Second pass of thd cleanup
2163
    session->killed and return HA_POS_ERROR if it is not zero. This is required
1 by brian
clean slate
2164
    for a user to be able to interrupt the calculation by killing the
2165
    connection/query.
2166
2167
  @retval
2168
    HA_POS_ERROR  Error or the engine is unable to perform the requested
2169
                  scan. Values of OUT parameters are undefined.
2170
  @retval
2171
    other         OK, *cost contains cost of the scan, *bufsz and *flags
2172
                  contain scan parameters.
2173
*/
2174
77.1.15 by Monty Taylor
Bunch of warning cleanups.
2175
ha_rows
482 by Brian Aker
Remove uint.
2176
handler::multi_range_read_info_const(uint32_t keyno, RANGE_SEQ_IF *seq,
77.1.15 by Monty Taylor
Bunch of warning cleanups.
2177
                                     void *seq_init_param,
779.1.27 by Monty Taylor
Got rid of __attribute__((unused)) and the like from the .cc files.
2178
                                     uint32_t ,
482 by Brian Aker
Remove uint.
2179
                                     uint32_t *bufsz, uint32_t *flags, COST_VECT *cost)
1 by brian
clean slate
2180
{
2181
  KEY_MULTI_RANGE range;
2182
  range_seq_t seq_it;
2183
  ha_rows rows, total_rows= 0;
482 by Brian Aker
Remove uint.
2184
  uint32_t n_ranges=0;
520.1.22 by Brian Aker
Second pass of thd cleanup
2185
  Session *session= current_session;
575.2.2 by Monty Taylor
Moved vio stuff into libdrizzle.
2186
1 by brian
clean slate
2187
  /* Default MRR implementation doesn't need buffer */
2188
  *bufsz= 0;
2189
2190
  seq_it= seq->init(seq_init_param, n_ranges, *flags);
2191
  while (!seq->next(seq_it, &range))
2192
  {
520.1.22 by Brian Aker
Second pass of thd cleanup
2193
    if (unlikely(session->killed != 0))
1 by brian
clean slate
2194
      return HA_POS_ERROR;
575.2.2 by Monty Taylor
Moved vio stuff into libdrizzle.
2195
1 by brian
clean slate
2196
    n_ranges++;
2197
    key_range *min_endp, *max_endp;
2198
    {
2199
      min_endp= range.start_key.length? &range.start_key : NULL;
2200
      max_endp= range.end_key.length? &range.end_key : NULL;
2201
    }
2202
    if ((range.range_flag & UNIQUE_RANGE) && !(range.range_flag & NULL_RANGE))
2203
      rows= 1; /* there can be at most one row */
2204
    else
2205
    {
575.2.2 by Monty Taylor
Moved vio stuff into libdrizzle.
2206
      if (HA_POS_ERROR == (rows= this->records_in_range(keyno, min_endp,
1 by brian
clean slate
2207
                                                        max_endp)))
2208
      {
2209
        /* Can't scan one range => can't do MRR scan at all */
2210
        total_rows= HA_POS_ERROR;
2211
        break;
2212
      }
2213
    }
2214
    total_rows += rows;
2215
  }
575.2.2 by Monty Taylor
Moved vio stuff into libdrizzle.
2216
1 by brian
clean slate
2217
  if (total_rows != HA_POS_ERROR)
2218
  {
2219
    /* The following calculation is the same as in multi_range_read_info(): */
2220
    *flags |= HA_MRR_USE_DEFAULT_IMPL;
2221
    cost->zero();
2222
    cost->avg_io_cost= 1; /* assume random seeks */
2223
    if ((*flags & HA_MRR_INDEX_ONLY) && total_rows > 2)
895 by Brian Aker
Completion (?) of uint conversion.
2224
      cost->io_count= index_only_read_time(keyno, (uint32_t)total_rows);
1 by brian
clean slate
2225
    else
2226
      cost->io_count= read_time(keyno, n_ranges, total_rows);
2227
    cost->cpu_cost= (double) total_rows / TIME_FOR_COMPARE + 0.01;
2228
  }
2229
  return total_rows;
2230
}
2231
2232
2233
/**
2234
  Get cost and other information about MRR scan over some sequence of ranges
2235
2236
  Calculate estimated cost and other information about an MRR scan for some
2237
  sequence of ranges.
2238
2239
  The ranges themselves will be known only at execution phase. When this
2240
  function is called we only know number of ranges and a (rough) E(#records)
2241
  within those ranges.
2242
2243
  Currently this function is only called for "n-keypart singlepoint" ranges,
2244
  i.e. each range is "keypart1=someconst1 AND ... AND keypartN=someconstN"
2245
2246
  The flags parameter is a combination of those flags: HA_MRR_SORTED,
2247
  HA_MRR_INDEX_ONLY, HA_MRR_NO_ASSOCIATION, HA_MRR_LIMITS.
2248
2249
  @param keyno           Index number
2250
  @param n_ranges        Estimated number of ranges (i.e. intervals) in the
2251
                         range sequence.
2252
  @param n_rows          Estimated total number of records contained within all
2253
                         of the ranges
2254
  @param bufsz    INOUT  IN:  Size of the buffer available for use
2255
                         OUT: Size of the buffer that will be actually used, or
2256
                              0 if buffer is not needed.
2257
  @param flags    INOUT  A combination of HA_MRR_* flags
2258
  @param cost     OUT    Estimated cost of MRR access
2259
2260
  @retval
2261
    0     OK, *cost contains cost of the scan, *bufsz and *flags contain scan
2262
          parameters.
2263
  @retval
2264
    other Error or can't perform the requested scan
2265
*/
2266
482 by Brian Aker
Remove uint.
2267
int handler::multi_range_read_info(uint32_t keyno, uint32_t n_ranges, uint32_t n_rows,
2268
                                   uint32_t *bufsz, uint32_t *flags, COST_VECT *cost)
1 by brian
clean slate
2269
{
2270
  *bufsz= 0; /* Default implementation doesn't need a buffer */
2271
2272
  *flags |= HA_MRR_USE_DEFAULT_IMPL;
2273
2274
  cost->zero();
2275
  cost->avg_io_cost= 1; /* assume random seeks */
2276
2277
  /* Produce the same cost as non-MRR code does */
2278
  if (*flags & HA_MRR_INDEX_ONLY)
2279
    cost->io_count= index_only_read_time(keyno, n_rows);
2280
  else
2281
    cost->io_count= read_time(keyno, n_ranges, n_rows);
2282
  return 0;
2283
}
2284
2285
2286
/**
2287
  Initialize the MRR scan
2288
575.2.2 by Monty Taylor
Moved vio stuff into libdrizzle.
2289
  Initialize the MRR scan. This function may do heavyweight scan
1 by brian
clean slate
2290
  initialization like row prefetching/sorting/etc (NOTE: but better not do
2291
  it here as we may not need it, e.g. if we never satisfy WHERE clause on
2292
  previous tables. For many implementations it would be natural to do such
2293
  initializations in the first multi_read_range_next() call)
2294
2295
  mode is a combination of the following flags: HA_MRR_SORTED,
575.2.2 by Monty Taylor
Moved vio stuff into libdrizzle.
2296
  HA_MRR_INDEX_ONLY, HA_MRR_NO_ASSOCIATION
1 by brian
clean slate
2297
2298
  @param seq             Range sequence to be traversed
2299
  @param seq_init_param  First parameter for seq->init()
2300
  @param n_ranges        Number of ranges in the sequence
2301
  @param mode            Flags, see the description section for the details
2302
  @param buf             INOUT: memory buffer to be used
2303
2304
  @note
2305
    One must have called index_init() before calling this function. Several
2306
    multi_range_read_init() calls may be made in course of one query.
2307
575.2.2 by Monty Taylor
Moved vio stuff into libdrizzle.
2308
    Until WL#2623 is done (see its text, section 3.2), the following will
1 by brian
clean slate
2309
    also hold:
2310
    The caller will guarantee that if "seq->init == mrr_ranges_array_init"
2311
    then seq_init_param is an array of n_ranges KEY_MULTI_RANGE structures.
2312
    This property will only be used by NDB handler until WL#2623 is done.
575.2.2 by Monty Taylor
Moved vio stuff into libdrizzle.
2313
1 by brian
clean slate
2314
    Buffer memory management is done according to the following scenario:
2315
    The caller allocates the buffer and provides it to the callee by filling
2316
    the members of HANDLER_BUFFER structure.
2317
    The callee consumes all or some fraction of the provided buffer space, and
2318
    sets the HANDLER_BUFFER members accordingly.
2319
    The callee may use the buffer memory until the next multi_range_read_init()
2320
    call is made, all records have been read, or until index_end() call is
2321
    made, whichever comes first.
2322
2323
  @retval 0  OK
2324
  @retval 1  Error
2325
*/
2326
2327
int
2328
handler::multi_range_read_init(RANGE_SEQ_IF *seq_funcs, void *seq_init_param,
482 by Brian Aker
Remove uint.
2329
                               uint32_t n_ranges, uint32_t mode,
779.1.27 by Monty Taylor
Got rid of __attribute__((unused)) and the like from the .cc files.
2330
                               HANDLER_BUFFER *)
1 by brian
clean slate
2331
{
2332
  mrr_iter= seq_funcs->init(seq_init_param, n_ranges, mode);
2333
  mrr_funcs= *seq_funcs;
2334
  mrr_is_output_sorted= test(mode & HA_MRR_SORTED);
56 by brian
Next pass of true/false update.
2335
  mrr_have_range= false;
1046.1.9 by Brian Aker
Remove caller that wasn't correctly locking, and reverted code to 5.1
2336
  return 0;
1 by brian
clean slate
2337
}
2338
2339
2340
/**
2341
  Get next record in MRR scan
2342
2343
  Default MRR implementation: read the next record
2344
2345
  @param range_info  OUT  Undefined if HA_MRR_NO_ASSOCIATION flag is in effect
2346
                          Otherwise, the opaque value associated with the range
2347
                          that contains the returned record.
2348
2349
  @retval 0      OK
2350
  @retval other  Error code
2351
*/
2352
2353
int handler::multi_range_read_next(char **range_info)
2354
{
2355
  int result= 0;
236.2.2 by rbradfor
Using correct coding standards for variable initialization
2356
  int range_res= 0;
1 by brian
clean slate
2357
2358
  if (!mrr_have_range)
2359
  {
56 by brian
Next pass of true/false update.
2360
    mrr_have_range= true;
1 by brian
clean slate
2361
    goto start;
2362
  }
2363
2364
  do
2365
  {
2366
    /* Save a call if there can be only one row in range. */
2367
    if (mrr_cur_range.range_flag != (UNIQUE_RANGE | EQ_RANGE))
2368
    {
2369
      result= read_range_next();
2370
      /* On success or non-EOF errors jump to the end. */
2371
      if (result != HA_ERR_END_OF_FILE)
2372
        break;
2373
    }
2374
    else
2375
    {
2376
      if (was_semi_consistent_read())
2377
        goto scan_it_again;
2378
      /*
2379
        We need to set this for the last range only, but checking this
2380
        condition is more expensive than just setting the result code.
2381
      */
2382
      result= HA_ERR_END_OF_FILE;
2383
    }
2384
2385
start:
2386
    /* Try the next range(s) until one matches a record. */
2387
    while (!(range_res= mrr_funcs.next(mrr_iter, &mrr_cur_range)))
2388
    {
2389
scan_it_again:
2390
      result= read_range_first(mrr_cur_range.start_key.keypart_map ?
2391
                                 &mrr_cur_range.start_key : 0,
2392
                               mrr_cur_range.end_key.keypart_map ?
2393
                                 &mrr_cur_range.end_key : 0,
2394
                               test(mrr_cur_range.range_flag & EQ_RANGE),
2395
                               mrr_is_output_sorted);
2396
      if (result != HA_ERR_END_OF_FILE)
2397
        break;
2398
    }
2399
  }
2400
  while ((result == HA_ERR_END_OF_FILE) && !range_res);
2401
2402
  *range_info= mrr_cur_range.ptr;
1046.1.9 by Brian Aker
Remove caller that wasn't correctly locking, and reverted code to 5.1
2403
  return result;
1 by brian
clean slate
2404
}
2405
2406
2407
/**
2408
  Get cost of reading nrows table records in a "disk sweep"
2409
2410
  A disk sweep read is a sequence of handler->rnd_pos(rowid) calls that made
2411
  for an ordered sequence of rowids.
2412
2413
  We assume hard disk IO. The read is performed as follows:
2414
2415
   1. The disk head is moved to the needed cylinder
2416
   2. The controller waits for the plate to rotate
2417
   3. The data is transferred
2418
2419
  Time to do #3 is insignificant compared to #2+#1.
2420
2421
  Time to move the disk head is proportional to head travel distance.
2422
2423
  Time to wait for the plate to rotate depends on whether the disk head
575.2.2 by Monty Taylor
Moved vio stuff into libdrizzle.
2424
  was moved or not.
1 by brian
clean slate
2425
2426
  If disk head wasn't moved, the wait time is proportional to distance
2427
  between the previous block and the block we're reading.
2428
2429
  If the head was moved, we don't know how much we'll need to wait for the
2430
  plate to rotate. We assume the wait time to be a variate with a mean of
2431
  0.5 of full rotation time.
2432
2433
  Our cost units are "random disk seeks". The cost of random disk seek is
2434
  actually not a constant, it depends one range of cylinders we're going
575.2.2 by Monty Taylor
Moved vio stuff into libdrizzle.
2435
  to access. We make it constant by introducing a fuzzy concept of "typical
1 by brian
clean slate
2436
  datafile length" (it's fuzzy as it's hard to tell whether it should
2437
  include index file, temp.tables etc). Then random seek cost is:
2438
2439
    1 = half_rotation_cost + move_cost * 1/3 * typical_data_file_length
2440
2441
  We define half_rotation_cost as DISK_SEEK_BASE_COST=0.9.
2442
2443
  @param table             Table to be accessed
2444
  @param nrows             Number of rows to retrieve
56 by brian
Next pass of true/false update.
2445
  @param interrupted       true <=> Assume that the disk sweep will be
2446
                           interrupted by other disk IO. false - otherwise.
1 by brian
clean slate
2447
  @param cost         OUT  The cost.
2448
*/
2449
575.2.2 by Monty Taylor
Moved vio stuff into libdrizzle.
2450
void get_sweep_read_cost(Table *table, ha_rows nrows, bool interrupted,
1 by brian
clean slate
2451
                         COST_VECT *cost)
2452
{
2453
  cost->zero();
2454
  if (table->file->primary_key_is_clustered())
2455
  {
2456
    cost->io_count= table->file->read_time(table->s->primary_key,
895 by Brian Aker
Completion (?) of uint conversion.
2457
                                           (uint32_t) nrows, nrows);
1 by brian
clean slate
2458
  }
2459
  else
2460
  {
2461
    double n_blocks=
151 by Brian Aker
Ulonglong to uint64_t
2462
      ceil(uint64_t2double(table->file->stats.data_file_length) / IO_SIZE);
1 by brian
clean slate
2463
    double busy_blocks=
2464
      n_blocks * (1.0 - pow(1.0 - 1.0/n_blocks, rows2double(nrows)));
2465
    if (busy_blocks < 1.0)
2466
      busy_blocks= 1.0;
2467
2468
    cost->io_count= busy_blocks;
2469
2470
    if (!interrupted)
2471
    {
2472
      /* Assume reading is done in one 'sweep' */
2473
      cost->avg_io_cost= (DISK_SEEK_BASE_COST +
2474
                          DISK_SEEK_PROP_COST*n_blocks/busy_blocks);
2475
    }
2476
  }
2477
}
2478
2479
2480
/* **************************************************************************
2481
 * DS-MRR implementation ends
2482
 ***************************************************************************/
2483
2484
/**
2485
  Read first row between two ranges.
2486
2487
  @param start_key		Start key. Is 0 if no min range
2488
  @param end_key		End key.  Is 0 if no max range
2489
  @param eq_range_arg	        Set to 1 if start_key == end_key
2490
  @param sorted		Set to 1 if result should be sorted per key
2491
2492
  @note
2493
    Record is read into table->record[0]
2494
2495
  @retval
2496
    0			Found row
2497
  @retval
2498
    HA_ERR_END_OF_FILE	No rows in range
2499
  @retval
2500
    \#			Error code
2501
*/
2502
int handler::read_range_first(const key_range *start_key,
2503
			      const key_range *end_key,
2504
			      bool eq_range_arg,
779.1.27 by Monty Taylor
Got rid of __attribute__((unused)) and the like from the .cc files.
2505
                              bool )
1 by brian
clean slate
2506
{
2507
  int result;
2508
2509
  eq_range= eq_range_arg;
2510
  end_range= 0;
2511
  if (end_key)
2512
  {
2513
    end_range= &save_end_range;
2514
    save_end_range= *end_key;
2515
    key_compare_result_on_equal= ((end_key->flag == HA_READ_BEFORE_KEY) ? 1 :
2516
				  (end_key->flag == HA_READ_AFTER_KEY) ? -1 : 0);
2517
  }
2518
  range_key_part= table->key_info[active_index].key_part;
2519
2520
  if (!start_key)			// Read first record
2521
    result= index_first(table->record[0]);
2522
  else
2523
    result= index_read_map(table->record[0],
2524
                           start_key->key,
2525
                           start_key->keypart_map,
2526
                           start_key->flag);
2527
  if (result)
575.2.2 by Monty Taylor
Moved vio stuff into libdrizzle.
2528
    return((result == HA_ERR_KEY_NOT_FOUND)
1 by brian
clean slate
2529
		? HA_ERR_END_OF_FILE
2530
		: result);
2531
51.1.77 by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols
2532
  return (compare_key(end_range) <= 0 ? 0 : HA_ERR_END_OF_FILE);
1 by brian
clean slate
2533
}
2534
2535
2536
/**
2537
  Read next row between two endpoints.
2538
2539
  @note
2540
    Record is read into table->record[0]
2541
2542
  @retval
2543
    0			Found row
2544
  @retval
2545
    HA_ERR_END_OF_FILE	No rows in range
2546
  @retval
2547
    \#			Error code
2548
*/
2549
int handler::read_range_next()
2550
{
2551
  int result;
2552
2553
  if (eq_range)
2554
  {
2555
    /* We trust that index_next_same always gives a row in range */
51.1.77 by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols
2556
    return(index_next_same(table->record[0],
1 by brian
clean slate
2557
                                end_range->key,
2558
                                end_range->length));
2559
  }
2560
  result= index_next(table->record[0]);
2561
  if (result)
1046.1.9 by Brian Aker
Remove caller that wasn't correctly locking, and reverted code to 5.1
2562
    return result;
51.1.77 by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols
2563
  return(compare_key(end_range) <= 0 ? 0 : HA_ERR_END_OF_FILE);
1 by brian
clean slate
2564
}
2565
2566
2567
/**
2568
  Compare if found key (in row) is over max-value.
2569
2570
  @param range		range to compare to row. May be 0 for no range
2571
2572
  @seealso
2573
    key.cc::key_cmp()
2574
2575
  @return
2576
    The return value is SIGN(key_in_row - range_key):
2577
2578
    - 0   : Key is equal to range or 'range' == 0 (no range)
2579
    - -1  : Key is less than range
2580
    - 1   : Key is larger than range
2581
*/
2582
int handler::compare_key(key_range *range)
2583
{
2584
  int cmp;
2585
  if (!range || in_range_check_pushed_down)
2586
    return 0;					// No max range
2587
  cmp= key_cmp(range_key_part, range->key, range->length);
2588
  if (!cmp)
2589
    cmp= key_compare_result_on_equal;
2590
  return cmp;
2591
}
2592
2593
2594
/*
2595
  Same as compare_key() but doesn't check have in_range_check_pushed_down.
2596
  This is used by index condition pushdown implementation.
2597
*/
2598
2599
int handler::compare_key2(key_range *range)
2600
{
2601
  int cmp;
2602
  if (!range)
2603
    return 0;					// no max range
2604
  cmp= key_cmp(range_key_part, range->key, range->length);
2605
  if (!cmp)
2606
    cmp= key_compare_result_on_equal;
2607
  return cmp;
2608
}
2609
656.1.1 by Monty Taylor
OOOh doggie. Got rid of my_alloca.
2610
int handler::index_read_idx_map(unsigned char * buf, uint32_t index,
2611
                                const unsigned char * key,
1 by brian
clean slate
2612
                                key_part_map keypart_map,
2613
                                enum ha_rkey_function find_flag)
2614
{
2615
  int error, error1;
2616
  error= index_init(index, 0);
2617
  if (!error)
2618
  {
2619
    error= index_read_map(buf, key, keypart_map, find_flag);
2620
    error1= index_end();
2621
  }
2622
  return error ?  error : error1;
2623
}
2624
2625
520.1.22 by Brian Aker
Second pass of thd cleanup
2626
static bool stat_print(Session *session, const char *type, uint32_t type_len,
482 by Brian Aker
Remove uint.
2627
                       const char *file, uint32_t file_len,
2628
                       const char *status, uint32_t status_len)
1 by brian
clean slate
2629
{
520.1.22 by Brian Aker
Second pass of thd cleanup
2630
  Protocol *protocol= session->protocol;
971.3.19 by Eric Day
Finished first pass at Protocol cleanup, still some things to remove but they are a bit more involved.
2631
  protocol->prepareForResend();
1054.2.9 by Monty Taylor
Removed CHARSET_INFO stuff from protocol plugin interface - it makes no sense.
2632
  protocol->store(type, type_len);
2633
  protocol->store(file, file_len);
2634
  protocol->store(status, status_len);
1 by brian
clean slate
2635
  if (protocol->write())
56 by brian
Next pass of true/false update.
2636
    return true;
2637
  return false;
1 by brian
clean slate
2638
}
2639
960.2.27 by Monty Taylor
Reworked transformed handlerton into class StorageEngine.
2640
bool ha_show_status(Session *session, StorageEngine *engine, enum ha_stat_type stat)
1 by brian
clean slate
2641
{
2642
  List<Item> field_list;
520.1.22 by Brian Aker
Second pass of thd cleanup
2643
  Protocol *protocol= session->protocol;
1 by brian
clean slate
2644
  bool result;
2645
2646
  field_list.push_back(new Item_empty_string("Type",10));
2647
  field_list.push_back(new Item_empty_string("Name",FN_REFLEN));
2648
  field_list.push_back(new Item_empty_string("Status",10));
2649
971.3.19 by Eric Day
Finished first pass at Protocol cleanup, still some things to remove but they are a bit more involved.
2650
  if (protocol->sendFields(&field_list,
2651
                           Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF))
56 by brian
Next pass of true/false update.
2652
    return true;
1 by brian
clean slate
2653
960.2.38 by Monty Taylor
Removed extraneous send myself to myself argument.
2654
  result= engine->show_status(session, stat_print, stat) ? 1 : 0;
1 by brian
clean slate
2655
2656
  if (!result)
836 by Brian Aker
Fixed session call from function to method.
2657
    session->my_eof();
1 by brian
clean slate
2658
  return result;
2659
}
2660
2661
2662
/**
2663
  Check if the conditions for row-based binlogging is correct for the table.
2664
2665
  A row in the given table should be replicated if:
2666
  - Row-based replication is enabled in the current thread
2667
  - The binlog is enabled
2668
  - It is not a temporary table
2669
  - The binary log is open
2670
  - The database the table resides in shall be binlogged (binlog_*_db rules)
2671
  - table is not mysql.event
2672
*/
2673
1039.1.16 by Brian Aker
A lot of little cleanups (most based off lcov)
2674
static bool log_row_for_replication(Table* table,
667 by Brian Aker
Partial fix (more of replication flushed out)
2675
                           const unsigned char *before_record,
2676
                           const unsigned char *after_record)
1 by brian
clean slate
2677
{
520.1.22 by Brian Aker
Second pass of thd cleanup
2678
  Session *const session= table->in_use;
1 by brian
clean slate
2679
661 by Brian Aker
First major pass through new replication.
2680
  switch (session->lex->sql_command)
2681
  {
2682
  case SQLCOM_REPLACE:
2683
  case SQLCOM_INSERT:
2684
  case SQLCOM_REPLACE_SELECT:
2685
  case SQLCOM_INSERT_SELECT:
669 by Brian Aker
Replication bits (getting CREATE..SELECT right)
2686
  case SQLCOM_CREATE_TABLE:
1039.5.31 by Jay Pipes
This patch does a few things:
2687
    replication_services.insertRecord(session, table);
661 by Brian Aker
First major pass through new replication.
2688
    break;
2689
2690
  case SQLCOM_UPDATE:
1039.5.31 by Jay Pipes
This patch does a few things:
2691
    replication_services.updateRecord(session, table, before_record, after_record);
661 by Brian Aker
First major pass through new replication.
2692
    break;
2693
2694
  case SQLCOM_DELETE:
1039.5.31 by Jay Pipes
This patch does a few things:
2695
    replication_services.deleteRecord(session, table);
661 by Brian Aker
First major pass through new replication.
2696
    break;
2697
665.1.1 by Eric Herman
more EOL whitespace fixups
2698
    /*
662 by Brian Aker
Remove dead methods.
2699
      For everything else we ignore the event (since it just involves a temp table)
2700
    */
661 by Brian Aker
First major pass through new replication.
2701
  default:
2702
    break;
1 by brian
clean slate
2703
  }
592 by Brian Aker
Remove one set.
2704
988.1.6 by Jay Pipes
Removed old protobuf_replicator plugin, fixed up db.cc and other files to use new
2705
  return false; //error;
1 by brian
clean slate
2706
}
2707
520.1.22 by Brian Aker
Second pass of thd cleanup
2708
int handler::ha_external_lock(Session *session, int lock_type)
1 by brian
clean slate
2709
{
2710
  /*
2711
    Whether this is lock or unlock, this should be true, and is to verify that
2712
    if get_auto_increment() was called (thus may have reserved intervals or
2713
    taken a table lock), ha_release_auto_increment() was too.
2714
  */
51.1.77 by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols
2715
  assert(next_insert_id == 0);
1 by brian
clean slate
2716
2717
  /*
2718
    We cache the table flags if the locking succeeded. Otherwise, we
2719
    keep them as they were when they were fetched in ha_open().
2720
  */
319.1.1 by Grant Limberg
renamed all instances of MYSQL_ to DRIZZLE_
2721
  DRIZZLE_EXTERNAL_LOCK(lock_type);
1 by brian
clean slate
2722
520.1.22 by Brian Aker
Second pass of thd cleanup
2723
  int error= external_lock(session, lock_type);
1 by brian
clean slate
2724
  if (error == 0)
2725
    cached_table_flags= table_flags();
1046.1.9 by Brian Aker
Remove caller that wasn't correctly locking, and reverted code to 5.1
2726
  return error;
1 by brian
clean slate
2727
}
2728
2729
2730
/**
2731
  Check handler usage and reset state of file to after 'open'
2732
*/
2733
int handler::ha_reset()
2734
{
1005.2.3 by Monty Taylor
Further reversion of P.
2735
  /* Check that we have called all proper deallocation functions */
2736
  assert((unsigned char*) table->def_read_set.bitmap +
2737
              table->s->column_bitmap_size ==
2738
              (unsigned char*) table->def_write_set.bitmap);
2739
  assert(bitmap_is_set_all(&table->s->all_set));
51.1.77 by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols
2740
  assert(table->key_read == 0);
1 by brian
clean slate
2741
  /* ensure that ha_index_end / ha_rnd_end has been called */
51.1.77 by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols
2742
  assert(inited == NONE);
1 by brian
clean slate
2743
  /* Free cache used by filesort */
1109.1.4 by Brian Aker
More Table refactor
2744
  table->free_io_cache();
1 by brian
clean slate
2745
  /* reset the bitmaps to point to defaults */
2746
  table->default_column_bitmaps();
51.1.77 by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols
2747
  return(reset());
1 by brian
clean slate
2748
}
2749
2750
481 by Brian Aker
Remove all of uchar.
2751
int handler::ha_write_row(unsigned char *buf)
1 by brian
clean slate
2752
{
2753
  int error;
319.1.1 by Grant Limberg
renamed all instances of MYSQL_ to DRIZZLE_
2754
  DRIZZLE_INSERT_ROW_START();
1 by brian
clean slate
2755
873.1.16 by Jay Pipes
This patch pulls the setting of the auto-set timestamp out of the individual
2756
  /* 
2757
   * If we have a timestamp column, update it to the current time 
2758
   * 
2759
   * @TODO Technically, the below two lines can be take even further out of the
2760
   * handler interface and into the fill_record() method.
2761
   */
2762
  if (table->timestamp_field_type & TIMESTAMP_AUTO_SET_ON_INSERT)
2763
    table->timestamp_field->set_time();
2764
1 by brian
clean slate
2765
  mark_trx_read_write();
2766
2767
  if (unlikely(error= write_row(buf)))
1046.1.9 by Brian Aker
Remove caller that wasn't correctly locking, and reverted code to 5.1
2768
    return error;
662 by Brian Aker
Remove dead methods.
2769
1039.1.16 by Brian Aker
A lot of little cleanups (most based off lcov)
2770
  if (unlikely(log_row_for_replication(table, 0, buf)))
662 by Brian Aker
Remove dead methods.
2771
    return HA_ERR_RBR_LOGGING_FAILED; /* purecov: inspected */
2772
319.1.1 by Grant Limberg
renamed all instances of MYSQL_ to DRIZZLE_
2773
  DRIZZLE_INSERT_ROW_END();
1046.1.9 by Brian Aker
Remove caller that wasn't correctly locking, and reverted code to 5.1
2774
  return 0;
1 by brian
clean slate
2775
}
2776
2777
481 by Brian Aker
Remove all of uchar.
2778
int handler::ha_update_row(const unsigned char *old_data, unsigned char *new_data)
1 by brian
clean slate
2779
{
2780
  int error;
2781
2782
  /*
2783
    Some storage engines require that the new record is in record[0]
2784
    (and the old record is in record[1]).
2785
   */
51.1.77 by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols
2786
  assert(new_data == table->record[0]);
1 by brian
clean slate
2787
2788
  mark_trx_read_write();
2789
2790
  if (unlikely(error= update_row(old_data, new_data)))
2791
    return error;
662 by Brian Aker
Remove dead methods.
2792
1039.1.16 by Brian Aker
A lot of little cleanups (most based off lcov)
2793
  if (unlikely(log_row_for_replication(table, old_data, new_data)))
662 by Brian Aker
Remove dead methods.
2794
    return HA_ERR_RBR_LOGGING_FAILED;
2795
1 by brian
clean slate
2796
  return 0;
2797
}
2798
481 by Brian Aker
Remove all of uchar.
2799
int handler::ha_delete_row(const unsigned char *buf)
1 by brian
clean slate
2800
{
2801
  int error;
2802
2803
  mark_trx_read_write();
2804
2805
  if (unlikely(error= delete_row(buf)))
2806
    return error;
662 by Brian Aker
Remove dead methods.
2807
1039.1.16 by Brian Aker
A lot of little cleanups (most based off lcov)
2808
  if (unlikely(log_row_for_replication(table, buf, 0)))
662 by Brian Aker
Remove dead methods.
2809
    return HA_ERR_RBR_LOGGING_FAILED;
2810
1 by brian
clean slate
2811
  return 0;
2812
}