handler.cc 49.6 KB
Newer Older
unknown's avatar
unknown committed
1
/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB
unknown's avatar
unknown committed
2

unknown's avatar
unknown committed
3 4 5 6
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
unknown's avatar
unknown committed
7

unknown's avatar
unknown committed
8 9 10 11
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
unknown's avatar
unknown committed
12

unknown's avatar
unknown committed
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */


/* Handler-calling-functions */

#ifdef __GNUC__
#pragma implementation				// gcc: Class implementation
#endif

#include "mysql_priv.h"
#include "ha_heap.h"
#include "ha_myisam.h"
#include "ha_myisammrg.h"
28
#ifdef HAVE_ISAM
unknown's avatar
unknown committed
29 30 31 32 33 34
#include "ha_isam.h"
#include "ha_isammrg.h"
#endif
#ifdef HAVE_BERKELEY_DB
#include "ha_berkeley.h"
#endif
35 36 37
#ifdef HAVE_EXAMPLE_DB
#include "examples/ha_example.h"
#endif
38 39 40
#ifdef HAVE_ARCHIVE_DB
#include "examples/ha_archive.h"
#endif
41 42 43
#ifdef HAVE_CSV_DB
#include "examples/ha_tina.h"
#endif
44
#ifdef HAVE_INNOBASE_DB
45
#include "ha_innodb.h"
46
#endif
unknown's avatar
unknown committed
47 48 49
#ifdef HAVE_NDBCLUSTER_DB
#include "ha_ndbcluster.h"
#endif
unknown's avatar
unknown committed
50 51 52 53 54 55 56
#include <myisampack.h>
#include <errno.h>

	/* static functions defined in this file */

static int NEAR_F delete_file(const char *name,const char *ext,int extflag);

57
ulong ha_read_count, ha_discover_count;
unknown's avatar
unknown committed
58

59
static SHOW_COMP_OPTION have_yes= SHOW_OPTION_YES;
unknown's avatar
unknown committed
60

61
struct show_table_type_st sys_table_types[]=
unknown's avatar
unknown committed
62
{
63
  {"MyISAM",	&have_yes,
unknown's avatar
unknown committed
64
   "Default engine as of MySQL 3.23 with great performance", DB_TYPE_MYISAM},
65
  {"HEAP",	&have_yes,
unknown's avatar
unknown committed
66
   "Alias for MEMORY", DB_TYPE_HEAP},
67
  {"MEMORY",	&have_yes,
unknown's avatar
unknown committed
68
   "Hash based, stored in memory, useful for temporary tables", DB_TYPE_HEAP},
69 70 71 72 73
  {"MERGE",	&have_yes,
   "Collection of identical MyISAM tables", DB_TYPE_MRG_MYISAM},
  {"MRG_MYISAM",&have_yes,
   "Alias for MERGE", DB_TYPE_MRG_MYISAM},
  {"ISAM",	&have_isam,
unknown's avatar
unknown committed
74
   "Obsolete storage engine, now replaced by MyISAM", DB_TYPE_ISAM},
75
  {"MRG_ISAM",  &have_isam,
unknown's avatar
unknown committed
76
   "Obsolete storage engine, now replaced by MERGE", DB_TYPE_MRG_ISAM},
77
  {"InnoDB",	&have_innodb,
unknown's avatar
unknown committed
78
   "Supports transactions, row-level locking, and foreign keys", DB_TYPE_INNODB},
79 80 81 82 83 84
  {"INNOBASE",	&have_innodb,
   "Alias for INNODB", DB_TYPE_INNODB},
  {"BDB",	&have_berkeley_db,
   "Supports transactions and page-level locking", DB_TYPE_BERKELEY_DB},
  {"BERKELEYDB",&have_berkeley_db,
   "Alias for BDB", DB_TYPE_BERKELEY_DB},
unknown's avatar
unknown committed
85
  {"NDBCLUSTER", &have_ndbcluster,
unknown's avatar
unknown committed
86
   "Clustered, fault-tolerant, memory-based tables", DB_TYPE_NDBCLUSTER},
unknown's avatar
unknown committed
87 88
  {"NDB", &have_ndbcluster,
   "Alias for NDBCLUSTER", DB_TYPE_NDBCLUSTER},
89 90
  {"EXAMPLE",&have_example_db,
   "Example storage engine", DB_TYPE_EXAMPLE_DB},
91 92
  {"ARCHIVE",&have_archive_db,
   "Archive storage engine", DB_TYPE_ARCHIVE_DB},
93 94
  {"CSV",&have_csv_db,
   "CSV storage engine", DB_TYPE_CSV_DB},
95
  {NullS, NULL, NullS, DB_TYPE_UNKNOWN}
unknown's avatar
unknown committed
96
};
97

unknown's avatar
unknown committed
98 99 100 101
const char *ha_row_type[] = {
  "", "FIXED", "DYNAMIC", "COMPRESSED","?","?","?"
};

unknown's avatar
unknown committed
102
const char *tx_isolation_names[] =
103 104 105
{ "READ-UNCOMMITTED", "READ-COMMITTED", "REPEATABLE-READ", "SERIALIZABLE",
  NullS};
TYPELIB tx_isolation_typelib= {array_elements(tx_isolation_names)-1,"",
106
			       tx_isolation_names, NULL};
unknown's avatar
unknown committed
107

108
static TYPELIB known_extensions= {0,"known_exts", NULL, NULL};
109
uint known_extensions_id= 0;
110

111 112
enum db_type ha_resolve_by_name(const char *name, uint namelen)
{
113
  THD *thd= current_thd;
114 115
  if (thd && !my_strcasecmp(&my_charset_latin1, name, "DEFAULT")) {
    return (enum db_type) thd->variables.table_type;
116 117 118 119 120 121
  }
  
  show_table_type_st *types;
  for (types= sys_table_types; types->type; types++)
  {
    if (!my_strcasecmp(&my_charset_latin1, name, types->type))
122
      return (enum db_type) types->db_type;
123 124 125 126
  }
  return DB_TYPE_UNKNOWN;
}

unknown's avatar
unknown committed
127
const char *ha_get_storage_engine(enum db_type db_type)
128 129 130 131 132 133 134 135 136 137 138
{
  show_table_type_st *types;
  for (types= sys_table_types; types->type; types++)
  {
    if (db_type == types->db_type)
      return types->type;
  }
  
  return "none";
}

unknown's avatar
unknown committed
139 140 141 142
	/* Use other database handler if databasehandler is not incompiled */

enum db_type ha_checktype(enum db_type database_type)
{
143
  show_table_type_st *types;
144
  THD *thd= current_thd;
145 146 147
  for (types= sys_table_types; types->type; types++)
  {
    if ((database_type == types->db_type) && 
unknown's avatar
unknown committed
148
	(*types->value == SHOW_OPTION_YES))
149 150 151
      return database_type;
  }

unknown's avatar
unknown committed
152 153 154
  switch (database_type) {
#ifndef NO_HASH
  case DB_TYPE_HASH:
155
    return (database_type);
156
#endif
157 158
  case DB_TYPE_MRG_ISAM:
    return (DB_TYPE_MRG_MYISAM);
unknown's avatar
unknown committed
159 160 161
  default:
    break;
  }
162
  
163 164 165 166 167
  return ((enum db_type) thd->variables.table_type != DB_TYPE_UNKNOWN ?
          (enum db_type) thd->variables.table_type :
          (enum db_type) global_system_variables.table_type !=
          DB_TYPE_UNKNOWN ?
          (enum db_type) global_system_variables.table_type : DB_TYPE_MYISAM);
unknown's avatar
unknown committed
168 169 170 171 172 173 174
} /* ha_checktype */


handler *get_new_handler(TABLE *table, enum db_type db_type)
{
  switch (db_type) {
#ifndef NO_HASH
175 176
  case DB_TYPE_HASH:
    return new ha_hash(table);
unknown's avatar
unknown committed
177
#endif
178
#ifdef HAVE_ISAM
unknown's avatar
unknown committed
179 180 181 182
  case DB_TYPE_MRG_ISAM:
    return new ha_isammrg(table);
  case DB_TYPE_ISAM:
    return new ha_isam(table);
183 184 185
#else
  case DB_TYPE_MRG_ISAM:
    return new ha_myisammrg(table);
unknown's avatar
unknown committed
186 187 188 189
#endif
#ifdef HAVE_BERKELEY_DB
  case DB_TYPE_BERKELEY_DB:
    return new ha_berkeley(table);
190 191
#endif
#ifdef HAVE_INNOBASE_DB
192
  case DB_TYPE_INNODB:
193
    return new ha_innobase(table);
194 195 196 197
#endif
#ifdef HAVE_EXAMPLE_DB
  case DB_TYPE_EXAMPLE_DB:
    return new ha_example(table);
198
#endif
199 200 201
#ifdef HAVE_ARCHIVE_DB
  case DB_TYPE_ARCHIVE_DB:
    return new ha_archive(table);
unknown's avatar
Merge  
unknown committed
202
#endif
203 204 205 206
#ifdef HAVE_CSV_DB
  case DB_TYPE_CSV_DB:
    return new ha_tina(table);
#endif
unknown's avatar
unknown committed
207 208 209
#ifdef HAVE_NDBCLUSTER_DB
  case DB_TYPE_NDBCLUSTER:
    return new ha_ndbcluster(table);
unknown's avatar
unknown committed
210 211 212 213
#endif
  case DB_TYPE_HEAP:
    return new ha_heap(table);
  default:					// should never happen
214 215 216 217 218 219 220 221
  {
    enum db_type def=(enum db_type) current_thd->variables.table_type;
    /* Try first with 'default table type' */
    if (db_type != def)
      return get_new_handler(table, def);
  }
  /* Fall back to MyISAM */
  case DB_TYPE_MYISAM:
unknown's avatar
unknown committed
222 223 224 225 226 227
    return new ha_myisam(table);
  case DB_TYPE_MRG_MYISAM:
    return new ha_myisammrg(table);
  }
}

unknown's avatar
unknown committed
228 229 230 231 232
bool ha_caching_allowed(THD* thd, char* table_key,
                        uint key_length, uint8 cache_type)
{
#ifdef HAVE_INNOBASE_DB
  if (cache_type == HA_CACHE_TBL_ASKTRANSACT)
233
    return innobase_query_caching_of_table_permitted(thd, table_key, key_length);
unknown's avatar
unknown committed
234
#endif
235
  return 1;
unknown's avatar
unknown committed
236 237
}

unknown's avatar
unknown committed
238 239
int ha_init()
{
240
  int error= 0;
unknown's avatar
unknown committed
241
#ifdef HAVE_BERKELEY_DB
242
  if (have_berkeley_db == SHOW_OPTION_YES)
unknown's avatar
unknown committed
243
  {
244 245 246 247 248
    if (berkeley_init())
    {
      have_berkeley_db= SHOW_OPTION_DISABLED;	// If we couldn't use handler
      error= 1;
    }
249
    else
250
      opt_using_transactions=1;
unknown's avatar
unknown committed
251
  }
252 253
#endif
#ifdef HAVE_INNOBASE_DB
254
  if (have_innodb == SHOW_OPTION_YES)
255
  {
unknown's avatar
unknown committed
256
    if (innobase_init())
257 258 259 260
    {
      have_innodb= SHOW_OPTION_DISABLED;	// If we couldn't use handler
      error= 1;
    }
261
    else
262
      opt_using_transactions=1;
263
  }
unknown's avatar
unknown committed
264 265 266 267 268 269 270 271 272 273 274 275
#endif
#ifdef HAVE_NDBCLUSTER_DB
  if (have_ndbcluster == SHOW_OPTION_YES)
  {
    if (ndbcluster_init())
    {
      have_ndbcluster= SHOW_OPTION_DISABLED;
      error= 1;
    }
    else
      opt_using_transactions=1;
  }
unknown's avatar
unknown committed
276
#endif
277
  return error;
unknown's avatar
unknown committed
278 279 280 281 282 283 284 285 286 287 288
}

	/* close, flush or restart databases */
	/* Ignore this for other databases than ours */

int ha_panic(enum ha_panic_function flag)
{
  int error=0;
#ifndef NO_HASH
  error|=h_panic(flag);			/* fix hash */
#endif
289 290
#ifdef HAVE_ISAM
  error|=mrg_panic(flag);
unknown's avatar
unknown committed
291
  error|=nisam_panic(flag);
292 293
#endif
  error|=heap_panic(flag);
unknown's avatar
unknown committed
294 295 296
  error|=mi_panic(flag);
  error|=myrg_panic(flag);
#ifdef HAVE_BERKELEY_DB
297
  if (have_berkeley_db == SHOW_OPTION_YES)
unknown's avatar
unknown committed
298
    error|=berkeley_end();
299 300
#endif
#ifdef HAVE_INNOBASE_DB
301
  if (have_innodb == SHOW_OPTION_YES)
302
    error|=innobase_end();
unknown's avatar
unknown committed
303 304 305 306
#endif
#ifdef HAVE_NDBCLUSTER_DB
  if (have_ndbcluster == SHOW_OPTION_YES)
    error|=ndbcluster_end();
unknown's avatar
unknown committed
307 308 309 310
#endif
  return error;
} /* ha_panic */

311 312 313
void ha_drop_database(char* path)
{
#ifdef HAVE_INNOBASE_DB
314
  if (have_innodb == SHOW_OPTION_YES)
315 316
    innobase_drop_database(path);
#endif
unknown's avatar
unknown committed
317 318 319 320
#ifdef HAVE_NDBCLUSTER_DB
  if (have_ndbcluster == SHOW_OPTION_YES)
    ndbcluster_drop_database(path);
#endif
321
}
unknown's avatar
unknown committed
322

323 324 325
void ha_close_connection(THD* thd)
{
#ifdef HAVE_INNOBASE_DB
326
  if (have_innodb == SHOW_OPTION_YES)
unknown's avatar
unknown committed
327
    innobase_close_connection(thd);
328
#endif
unknown's avatar
unknown committed
329 330 331 332
#ifdef HAVE_NDBCLUSTER_DB
  if (have_ndbcluster == SHOW_OPTION_YES)
    ndbcluster_close_connection(thd);
#endif
333 334
}

335
/*
unknown's avatar
unknown committed
336
  This is used to commit or rollback a single statement depending on the value
unknown's avatar
unknown committed
337 338 339 340 341
  of error. Note that if the autocommit is on, then the following call inside
  InnoDB will commit or rollback the whole transaction (= the statement). The
  autocommit mechanism built into InnoDB is based on counting locks, but if
  the user has used LOCK TABLES then that mechanism does not know to do the
  commit.
342 343
*/

unknown's avatar
unknown committed
344 345 346
int ha_autocommit_or_rollback(THD *thd, int error)
{
  DBUG_ENTER("ha_autocommit_or_rollback");
347
#ifdef USING_TRANSACTIONS
348
  if (opt_using_transactions)
unknown's avatar
unknown committed
349
  {
350 351 352 353 354 355 356
    if (!error)
    {
      if (ha_commit_stmt(thd))
	error=1;
    }
    else
      (void) ha_rollback_stmt(thd);
unknown's avatar
unknown committed
357

unknown's avatar
unknown committed
358
    thd->variables.tx_isolation=thd->session_tx_isolation;
unknown's avatar
unknown committed
359 360 361 362 363
  }
#endif
  DBUG_RETURN(error);
}

unknown's avatar
unknown committed
364 365 366
/*
  This function is called when MySQL writes the log segment of a
  transaction to the binlog. It is called when the LOCK_log mutex is
367
  reserved. Here we communicate to transactional table handlers what
unknown's avatar
unknown committed
368 369 370 371 372
  binlog position corresponds to the current transaction. The handler
  can store it and in recovery print to the user, so that the user
  knows from what position in the binlog to start possible
  roll-forward, for example, if the crashed server was a slave in
  replication. This function also calls the commit of the table
373
  handler, because the order of transactions in the log of the table
unknown's avatar
unknown committed
374
  handler must be the same as in the binlog.
unknown's avatar
unknown committed
375 376 377
  NOTE that to eliminate the bottleneck of the group commit, we do not
  flush the handler log files here, but only later in a call of
  ha_commit_complete().
unknown's avatar
unknown committed
378 379

  arguments:
380
  thd:           the thread handle of the current connection
unknown's avatar
unknown committed
381 382
  log_file_name: latest binlog file name
  end_offset:	 the offset in the binlog file up to which we wrote
383
  return value:  0 if success, 1 if error
unknown's avatar
unknown committed
384
*/
385

unknown's avatar
unknown committed
386 387 388
int ha_report_binlog_offset_and_commit(THD *thd,
				       char *log_file_name,
				       my_off_t end_offset)
389
{
unknown's avatar
unknown committed
390 391
  int  error= 0;
#ifdef HAVE_INNOBASE_DB
392 393
  THD_TRANS *trans;
  trans = &thd->transaction.all;
394
  if (trans->innodb_active_trans)
395
  {
396 397 398 399 400 401
    /*
      If we updated some InnoDB tables (innodb_active_trans is true), the
      binlog coords will be reported into InnoDB during the InnoDB commit
      (innobase_report_binlog_offset_and_commit). But if we updated only
      non-InnoDB tables, we need an explicit call to report it.
    */
402
    if ((error=innobase_report_binlog_offset_and_commit(thd,
403 404 405
                                                        trans->innobase_tid,
                                                        log_file_name,
                                                        end_offset)))
406 407 408 409 410
    {
      my_error(ER_ERROR_DURING_COMMIT, MYF(0), error);
      error=1;
    }
  }
411 412
  else if (opt_innodb_safe_binlog) // Don't report if not useful
    innobase_store_binlog_offset_and_flush_log(log_file_name, end_offset);
413 414 415
#endif
  return error;
}
416

unknown's avatar
unknown committed
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
/*
  Flushes the handler log files (if my.cnf settings do not free us from it)
  after we have called ha_report_binlog_offset_and_commit(). To eliminate
  the bottleneck from the group commit, this should be called when
  LOCK_log has been released in log.cc.

  arguments:
  thd:           the thread handle of the current connection
  return value:  always 0
*/

int ha_commit_complete(THD *thd)
{
#ifdef HAVE_INNOBASE_DB
  THD_TRANS *trans;
  trans = &thd->transaction.all;
  if (trans->innobase_tid)
  {
    innobase_commit_complete(trans->innobase_tid);

    trans->innodb_active_trans=0;
  }
#endif
  return 0;
}
442

443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470
/*
  This function should be called when MySQL sends rows of a SELECT result set
  or the EOF mark to the client. It releases a possible adaptive hash index
  S-latch held by thd in InnoDB and also releases a possible InnoDB query
  FIFO ticket to enter InnoDB. To save CPU time, InnoDB allows a thd to
  keep them over several calls of the InnoDB handler interface when a join
  is executed. But when we let the control to pass to the client they have
  to be released because if the application program uses mysql_use_result(),
  it may deadlock on the S-latch if the application on another connection
  performs another SQL query. In MySQL-4.1 this is even more important because
  there a connection can have several SELECT queries open at the same time.

  arguments:
  thd:           the thread handle of the current connection
  return value:  always 0
*/

int ha_release_temporary_latches(THD *thd)
{
#ifdef HAVE_INNOBASE_DB
  THD_TRANS *trans;
  trans = &thd->transaction.all;
  if (trans->innobase_tid)
    innobase_release_temporary_latches(trans->innobase_tid);
#endif
  return 0;
}

471
int ha_commit_trans(THD *thd, THD_TRANS* trans)
unknown's avatar
unknown committed
472 473
{
  int error=0;
474
  DBUG_ENTER("ha_commit_trans");
475
#ifdef USING_TRANSACTIONS
476
  if (opt_using_transactions)
477
  {
unknown's avatar
unknown committed
478
    bool transaction_commited= 0;
479
    bool operation_done= 0, need_start_waiters= 0;
480

481
    /* If transaction has done some updates to tables */
482 483
    if (trans == &thd->transaction.all && mysql_bin_log.is_open() &&
        my_b_tell(&thd->transaction.trans_log))
484
    {
485
      if ((error= wait_if_global_read_lock(thd, 0, 0)))
486
      {
487 488 489
        /*
          Note that ROLLBACK [TO SAVEPOINT] does not have this test; it's
          because ROLLBACK never updates data, so needn't wait on the lock.
490
        */
491 492 493 494 495 496 497 498
        my_error(ER_ERROR_DURING_COMMIT, MYF(0), error);
        error= 1;
      }
      else
        need_start_waiters= 1;
      if (mysql_bin_log.is_open())
      {
        mysql_bin_log.write(thd, &thd->transaction.trans_log, 1);
unknown's avatar
unknown committed
499 500 501 502 503 504 505 506 507 508
        statistic_increment(binlog_cache_use, &LOCK_status);
        if (thd->transaction.trans_log.disk_writes != 0)
        {
          /* 
            We have to do this after addition of trans_log to main binlog since
            this operation can cause flushing of end of trans_log to disk. 
          */
          statistic_increment(binlog_cache_disk_use, &LOCK_status);
          thd->transaction.trans_log.disk_writes= 0;
        }
509 510 511
        reinit_io_cache(&thd->transaction.trans_log,
                        WRITE_CACHE, (my_off_t) 0, 0, 1);
        thd->transaction.trans_log.end_of_file= max_binlog_cache_size;
512
      }
513
    }
unknown's avatar
unknown committed
514 515 516 517 518
#ifdef HAVE_NDBCLUSTER_DB
    if (trans->ndb_tid)
    {
      if ((error=ndbcluster_commit(thd,trans->ndb_tid)))
      {
519
	if (error == -1)
520
	  my_error(ER_ERROR_DURING_COMMIT, MYF(0));
unknown's avatar
unknown committed
521 522 523 524 525 526 527
        error=1;
      }
      if (trans == &thd->transaction.all)
        operation_done= transaction_commited= 1;
      trans->ndb_tid=0;
    }
#endif
unknown's avatar
unknown committed
528
#ifdef HAVE_BERKELEY_DB
529
    if (trans->bdb_tid)
unknown's avatar
unknown committed
530
    {
531 532 533 534 535
      if ((error=berkeley_commit(thd,trans->bdb_tid)))
      {
	my_error(ER_ERROR_DURING_COMMIT, MYF(0), error);
	error=1;
      }
536
      else
537 538
	if (!(thd->options & OPTION_BEGIN))
	  transaction_commited= 1; 
539
      trans->bdb_tid=0;
unknown's avatar
unknown committed
540
    }
541 542
#endif
#ifdef HAVE_INNOBASE_DB
543
    if (trans->innobase_tid)
544
    {
545 546 547 548 549
      if ((error=innobase_commit(thd,trans->innobase_tid)))
      {
	my_error(ER_ERROR_DURING_COMMIT, MYF(0), error);
	error=1;
      }
unknown's avatar
unknown committed
550
      trans->innodb_active_trans=0;
unknown's avatar
unknown committed
551
      if (trans == &thd->transaction.all)
552
	operation_done= transaction_commited= 1;
553
    }
554
#endif
unknown's avatar
unknown committed
555
#ifdef HAVE_QUERY_CACHE
unknown's avatar
unknown committed
556
    if (transaction_commited && thd->transaction.changed_tables)
557
      query_cache.invalidate(thd->transaction.changed_tables);
unknown's avatar
unknown committed
558
#endif /*HAVE_QUERY_CACHE*/
559
    if (error && trans == &thd->transaction.all && mysql_bin_log.is_open())
unknown's avatar
unknown committed
560
      sql_print_error("Got error during commit;  Binlog is not up to date!");
unknown's avatar
unknown committed
561
    thd->variables.tx_isolation=thd->session_tx_isolation;
562
    if (operation_done)
563
    {
564
      statistic_increment(thd->status_var.ha_commit_count,&LOCK_status);
565 566
      thd->transaction.cleanup();
    }
567 568
    if (need_start_waiters)
      start_waiting_global_read_lock(thd);
569
  }
570
#endif // using transactions
unknown's avatar
unknown committed
571 572 573
  DBUG_RETURN(error);
}

574

575
int ha_rollback_trans(THD *thd, THD_TRANS *trans)
unknown's avatar
unknown committed
576 577
{
  int error=0;
578
  DBUG_ENTER("ha_rollback_trans");
579 580
#ifdef USING_TRANSACTIONS
  if (opt_using_transactions)
unknown's avatar
unknown committed
581
  {
582
    bool operation_done=0;
583 584 585 586 587 588
    /*
      As rollback can be 30 times slower than insert in InnoDB, and user may
      not know there's rollback (if it's because of a dupl row), better warn.
    */
    const char *save_proc_info= thd->proc_info;
    thd->proc_info= "Rolling back";
unknown's avatar
unknown committed
589 590 591 592 593
#ifdef HAVE_NDBCLUSTER_DB
    if (trans->ndb_tid)
    {
      if ((error=ndbcluster_rollback(thd, trans->ndb_tid)))
      {
594
	if (error == -1)
595
	  my_error(ER_ERROR_DURING_ROLLBACK, MYF(0));
unknown's avatar
unknown committed
596 597 598 599 600 601
        error=1;
      }
      trans->ndb_tid = 0;
      operation_done=1;
    }
#endif
602 603
#ifdef HAVE_BERKELEY_DB
    if (trans->bdb_tid)
unknown's avatar
unknown committed
604
    {
605 606 607 608 609 610
      if ((error=berkeley_rollback(thd, trans->bdb_tid)))
      {
	my_error(ER_ERROR_DURING_ROLLBACK, MYF(0), error);
	error=1;
      }
      trans->bdb_tid=0;
611
      operation_done=1;
unknown's avatar
unknown committed
612
    }
613 614
#endif
#ifdef HAVE_INNOBASE_DB
615
    if (trans->innobase_tid)
616
    {
617 618 619 620 621
      if ((error=innobase_rollback(thd, trans->innobase_tid)))
      {
	my_error(ER_ERROR_DURING_ROLLBACK, MYF(0), error);
	error=1;
      }
unknown's avatar
unknown committed
622
      trans->innodb_active_trans=0;
623
      operation_done=1;
624
    }
unknown's avatar
unknown committed
625
#endif
626
    if ((trans == &thd->transaction.all) && mysql_bin_log.is_open())
unknown's avatar
unknown committed
627 628
    {
      /* 
629 630 631 632 633
         Update the binary log with a BEGIN/ROLLBACK block if we have
         cached some queries and we updated some non-transactional
         table. Such cases should be rare (updating a
         non-transactional table inside a transaction...).  Count disk
         writes to trans_log in any case.
unknown's avatar
unknown committed
634
      */
635 636 637 638 639 640 641 642 643 644 645 646 647 648 649
      if (my_b_tell(&thd->transaction.trans_log))
      {
        if (unlikely(thd->options & OPTION_STATUS_NO_TRANS_UPDATE))
          mysql_bin_log.write(thd, &thd->transaction.trans_log, 0);
        statistic_increment(binlog_cache_use, &LOCK_status);
        if (thd->transaction.trans_log.disk_writes != 0)
        {
          /* 
            We have to do this after addition of trans_log to main binlog since
            this operation can cause flushing of end of trans_log to disk. 
          */
          statistic_increment(binlog_cache_disk_use, &LOCK_status);
          thd->transaction.trans_log.disk_writes= 0;
        }
      }
unknown's avatar
unknown committed
650
      /* Flushed or not, empty the binlog cache */
651
      reinit_io_cache(&thd->transaction.trans_log,
unknown's avatar
unknown committed
652 653
                      WRITE_CACHE, (my_off_t) 0, 0, 1);
      thd->transaction.trans_log.end_of_file= max_binlog_cache_size;
654 655
      if (operation_done)
        thd->transaction.cleanup();
unknown's avatar
unknown committed
656
    }
unknown's avatar
unknown committed
657
    thd->variables.tx_isolation=thd->session_tx_isolation;
658
    if (operation_done)
unknown's avatar
unknown committed
659
      statistic_increment(thd->status_var.ha_rollback_count,&LOCK_status);
660
    thd->proc_info= save_proc_info;
661 662
  }
#endif /* USING_TRANSACTIONS */
unknown's avatar
unknown committed
663 664 665
  DBUG_RETURN(error);
}

unknown's avatar
unknown committed
666 667

/*
unknown's avatar
unknown committed
668 669 670 671 672 673 674 675 676 677
  Rolls the current transaction back to a savepoint.
  Return value: 0 if success, 1 if there was not a savepoint of the given
  name.
  NOTE: how do we handle this (unlikely but legal) case:
  [transaction] + [update to non-trans table] + [rollback to savepoint] ?
  The problem occurs when a savepoint is before the update to the
  non-transactional table. Then when there's a rollback to the savepoint, if we
  simply truncate the binlog cache, we lose the part of the binlog cache where
  the update is. If we want to not lose it, we need to write the SAVEPOINT
  command and the ROLLBACK TO SAVEPOINT command to the binlog cache. The latter
678 679 680 681 682 683
  is easy: it's just write at the end of the binlog cache, but the former
  should be *inserted* to the place where the user called SAVEPOINT. The
  solution is that when the user calls SAVEPOINT, we write it to the binlog
  cache (so no need to later insert it). As transactions are never intermixed
  in the binary log (i.e. they are serialized), we won't have conflicts with
  savepoint names when using mysqlbinlog or in the slave SQL thread.
unknown's avatar
unknown committed
684 685 686 687 688
  Then when ROLLBACK TO SAVEPOINT is called, if we updated some
  non-transactional table, we don't truncate the binlog cache but instead write
  ROLLBACK TO SAVEPOINT to it; otherwise we truncate the binlog cache (which
  will chop the SAVEPOINT command from the binlog cache, which is good as in
  that case there is no need to have it in the binlog).
unknown's avatar
unknown committed
689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711
*/

int ha_rollback_to_savepoint(THD *thd, char *savepoint_name)
{
  my_off_t binlog_cache_pos=0;
  bool operation_done=0;
  int error=0;
  DBUG_ENTER("ha_rollback_to_savepoint");
#ifdef USING_TRANSACTIONS
  if (opt_using_transactions)
  {
#ifdef HAVE_INNOBASE_DB
    /*
    Retrieve the trans_log binlog cache position corresponding to the
    savepoint, and if the rollback is successful inside InnoDB reset the write
    position in the binlog cache to what it was at the savepoint.
    */
    if ((error=innobase_rollback_to_savepoint(thd, savepoint_name,
						  &binlog_cache_pos)))
    {
      my_error(ER_ERROR_DURING_ROLLBACK, MYF(0), error);
      error=1;
    }
712
    else if (mysql_bin_log.is_open())
unknown's avatar
unknown committed
713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729
    {
      /* 
         Write ROLLBACK TO SAVEPOINT to the binlog cache if we have updated some
         non-transactional table. Otherwise, truncate the binlog cache starting
         from the SAVEPOINT command.
      */
      if (unlikely((thd->options & OPTION_STATUS_NO_TRANS_UPDATE) &&
                   my_b_tell(&thd->transaction.trans_log)))
      {
        Query_log_event qinfo(thd, thd->query, thd->query_length, TRUE);
        if (mysql_bin_log.write(&qinfo))
          error= 1;
      }
      else
        reinit_io_cache(&thd->transaction.trans_log, WRITE_CACHE,
                        binlog_cache_pos, 0, 0);
    }
unknown's avatar
unknown committed
730 731 732
    operation_done=1;
#endif
    if (operation_done)
733
      statistic_increment(thd->status_var.ha_rollback_count,&LOCK_status);
unknown's avatar
unknown committed
734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752
  }
#endif /* USING_TRANSACTIONS */

  DBUG_RETURN(error);
}


/*
Sets a transaction savepoint.
Return value: always 0, that is, succeeds always
*/

int ha_savepoint(THD *thd, char *savepoint_name)
{
  int error=0;
  DBUG_ENTER("ha_savepoint");
#ifdef USING_TRANSACTIONS
  if (opt_using_transactions)
  {
753
    /* Write it to the binary log (see comments of ha_rollback_to_savepoint) */
unknown's avatar
unknown committed
754 755
    if (mysql_bin_log.is_open())
    {
756 757 758 759
#ifdef HAVE_INNOBASE_DB
      innobase_savepoint(thd,savepoint_name,
                         my_b_tell(&thd->transaction.trans_log));
#endif
unknown's avatar
unknown committed
760 761 762 763
      Query_log_event qinfo(thd, thd->query, thd->query_length, TRUE);
      if (mysql_bin_log.write(&qinfo))
	error= 1;
    }
764 765 766 767
#ifdef HAVE_INNOBASE_DB
    else
      innobase_savepoint(thd,savepoint_name,0);
#endif
unknown's avatar
unknown committed
768 769 770 771 772
  }
#endif /* USING_TRANSACTIONS */
  DBUG_RETURN(error);
}

773 774 775 776 777 778 779 780 781 782 783 784

int ha_start_consistent_snapshot(THD *thd)
{
#ifdef HAVE_INNOBASE_DB
  if ((have_innodb == SHOW_OPTION_YES) &&
      !innobase_start_trx_and_assign_read_view(thd))
    return 0;
#endif
  /*
    Same idea as when one wants to CREATE TABLE in one engine which does not
    exist:
  */
785 786 787
  push_warning(thd, MYSQL_ERROR::WARN_LEVEL_WARN, ER_UNKNOWN_ERROR,
               "This MySQL server does not support any "
               "consistent-read capable storage engine");
788 789 790 791
  return 0;
}


unknown's avatar
unknown committed
792 793 794 795
bool ha_flush_logs()
{
  bool result=0;
#ifdef HAVE_BERKELEY_DB
796
  if ((have_berkeley_db == SHOW_OPTION_YES) &&
797
      berkeley_flush_logs())
unknown's avatar
unknown committed
798
    result=1;
799 800
#endif
#ifdef HAVE_INNOBASE_DB
801
  if ((have_innodb == SHOW_OPTION_YES) &&
802
      innobase_flush_logs())
803
    result=1;
unknown's avatar
unknown committed
804 805 806 807
#endif
  return result;
}

unknown's avatar
unknown committed
808 809 810 811
/*
  This should return ENOENT if the file doesn't exists.
  The .frm file will be deleted only if we return 0 or ENOENT
*/
unknown's avatar
unknown committed
812 813 814

int ha_delete_table(enum db_type table_type, const char *path)
{
815
  handler *file;
unknown's avatar
unknown committed
816
  char tmp_path[FN_REFLEN];
817 818 819 820

  /* DB_TYPE_UNKNOWN is used in ALTER TABLE when renaming only .frm files */
  if (table_type == DB_TYPE_UNKNOWN ||
      ! (file=get_new_handler((TABLE*) 0, table_type)))
unknown's avatar
unknown committed
821
    return ENOENT;
822

unknown's avatar
unknown committed
823 824 825 826
  if (lower_case_table_names == 2 && !(file->table_flags() & HA_FILE_BASED))
  {
    /* Ensure that table handler get path in lower case */
    strmov(tmp_path, path);
827
    my_casedn_str(files_charset_info, tmp_path);
unknown's avatar
unknown committed
828 829
    path= tmp_path;
  }
unknown's avatar
unknown committed
830 831 832 833
  int error=file->delete_table(path);
  delete file;
  return error;
}
unknown's avatar
unknown committed
834

unknown's avatar
unknown committed
835

unknown's avatar
unknown committed
836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899
void ha_store_ptr(byte *buff, uint pack_length, my_off_t pos)
{
  switch (pack_length) {
#if SIZEOF_OFF_T > 4
  case 8: mi_int8store(buff,pos); break;
  case 7: mi_int7store(buff,pos); break;
  case 6: mi_int6store(buff,pos); break;
  case 5: mi_int5store(buff,pos); break;
#endif
  case 4: mi_int4store(buff,pos); break;
  case 3: mi_int3store(buff,pos); break;
  case 2: mi_int2store(buff,(uint) pos); break;
  case 1: buff[0]= (uchar) pos; break;
  }
  return;
}

my_off_t ha_get_ptr(byte *ptr, uint pack_length)
{
  my_off_t pos;
  switch (pack_length) {
#if SIZEOF_OFF_T > 4
  case 8:
    pos= (my_off_t) mi_uint8korr(ptr);
    break;
  case 7:
    pos= (my_off_t) mi_uint7korr(ptr);
    break;
  case 6:
    pos= (my_off_t) mi_uint6korr(ptr);
    break;
  case 5:
    pos= (my_off_t) mi_uint5korr(ptr);
    break;
#endif
  case 4:
    pos= (my_off_t) mi_uint4korr(ptr);
    break;
  case 3:
    pos= (my_off_t) mi_uint3korr(ptr);
    break;
  case 2:
    pos= (my_off_t) mi_uint2korr(ptr);
    break;
  case 1:
    pos= (my_off_t) mi_uint2korr(ptr);
    break;
  default:
    pos=0;					// Impossible
    break;
  }
 return pos;
}

/****************************************************************************
** General handler functions
****************************************************************************/

	/* Open database-handler. Try O_RDONLY if can't open as O_RDWR */
	/* Don't wait for locks if not HA_OPEN_WAIT_IF_LOCKED is set */

int handler::ha_open(const char *name, int mode, int test_if_locked)
{
  int error;
900
  DBUG_ENTER("handler::ha_open");
901
  DBUG_PRINT("enter",("name: %s  db_type: %d  db_stat: %d  mode: %d  lock_test: %d",
902 903
		      name, table->db_type, table->db_stat, mode,
		      test_if_locked));
unknown's avatar
unknown committed
904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922

  if ((error=open(name,mode,test_if_locked)))
  {
    if ((error == EACCES || error == EROFS) && mode == O_RDWR &&
	(table->db_stat & HA_TRY_READ_ONLY))
    {
      table->db_stat|=HA_READ_ONLY;
      error=open(name,O_RDONLY,test_if_locked);
    }
  }
  if (error)
  {
    my_errno=error;			/* Safeguard */
    DBUG_PRINT("error",("error: %d  errno: %d",error,errno));
  }
  else
  {
    if (table->db_options_in_use & HA_OPTION_READ_ONLY_DATA)
      table->db_stat|=HA_READ_ONLY;
923 924
    (void) extra(HA_EXTRA_NO_READCHECK);	// Not needed in SQL

unknown's avatar
unknown committed
925
    if (!alloc_root_inited(&table->mem_root))	// If temporary table
926
      ref=(byte*) sql_alloc(ALIGN_SIZE(ref_length)*2);
unknown's avatar
unknown committed
927 928 929
    else
      ref=(byte*) alloc_root(&table->mem_root, ALIGN_SIZE(ref_length)*2);
    if (!ref)
unknown's avatar
unknown committed
930 931 932 933 934 935 936 937 938 939
    {
      close();
      error=HA_ERR_OUT_OF_MEM;
    }
    else
      dupp_ref=ref+ALIGN_SIZE(ref_length);
  }
  DBUG_RETURN(error);
}

940 941 942 943 944
/*
  Read first row (only) from a table
  This is never called for InnoDB or BDB tables, as these table types
  has the HA_NOT_EXACT_COUNT set.
*/
unknown's avatar
unknown committed
945

946
int handler::read_first_row(byte * buf, uint primary_key)
unknown's avatar
unknown committed
947 948
{
  register int error;
949
  DBUG_ENTER("handler::read_first_row");
unknown's avatar
unknown committed
950

951
  statistic_increment(current_thd->status_var.ha_read_first_count,&LOCK_status);
952 953 954 955 956

  /*
    If there is very few deleted rows in the table, find the first row by
    scanning the table.
  */
unknown's avatar
unknown committed
957
  if (deleted < 10 || primary_key >= MAX_KEY)
958
  {
unknown's avatar
unknown committed
959
    (void) ha_rnd_init(1);
960
    while ((error= rnd_next(buf)) == HA_ERR_RECORD_DELETED) ;
unknown's avatar
unknown committed
961
    (void) ha_rnd_end();
962 963 964 965
  }
  else
  {
    /* Find the first row through the primary key */
unknown's avatar
unknown committed
966
    (void) ha_index_init(primary_key);
967
    error=index_first(buf);
unknown's avatar
unknown committed
968
    (void) ha_index_end();
969
  }
unknown's avatar
unknown committed
970 971 972 973
  DBUG_RETURN(error);
}


974
/*
975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031
  Generate the next auto-increment number based on increment and offset
  
  In most cases increment= offset= 1, in which case we get:
  1,2,3,4,5,...
  If increment=10 and offset=5 and previous number is 1, we get:
  1,5,15,25,35,...
*/

inline ulonglong
next_insert_id(ulonglong nr,struct system_variables *variables)
{
  nr= (((nr+ variables->auto_increment_increment -
         variables->auto_increment_offset)) /
       (ulonglong) variables->auto_increment_increment);
  return (nr* (ulonglong) variables->auto_increment_increment +
          variables->auto_increment_offset);
}


/*
  Updates columns with type NEXT_NUMBER if:

  - If column value is set to NULL (in which case
    auto_increment_field_not_null is 0)
  - If column is set to 0 and (sql_mode & MODE_NO_AUTO_VALUE_ON_ZERO) is not
    set. In the future we will only set NEXT_NUMBER fields if one sets them
    to NULL (or they are not included in the insert list).


  There are two different cases when the above is true:

  - thd->next_insert_id == 0  (This is the normal case)
    In this case we set the set the column for the first row to the value
    next_insert_id(get_auto_increment(column))) which is normally
    max-used-column-value +1.

    We call get_auto_increment() only for the first row in a multi-row
    statement. For the following rows we generate new numbers based on the
    last used number.

  - thd->next_insert_id != 0.  This happens when we have read a statement
    from the binary log or when one has used SET LAST_INSERT_ID=#.

    In this case we will set the column to the value of next_insert_id.
    The next row will be given the id
    next_insert_id(next_insert_id)

    The idea is that generated auto_increment values are predictable and
    independent of the column values in the table.  This is needed to be
    able to replicate into a table that already has rows with a higher
    auto-increment value than the one that is inserted.

    After we have already generated an auto-increment number and the user
    inserts a column with a higher value than the last used one, we will
    start counting from the inserted value.

    thd->next_insert_id is cleared after it's been used for a statement.
1032
*/
unknown's avatar
unknown committed
1033 1034 1035

void handler::update_auto_increment()
{
1036 1037 1038
  ulonglong nr;
  THD *thd= table->in_use;
  struct system_variables *variables= &thd->variables;
1039
  DBUG_ENTER("handler::update_auto_increment");
1040 1041 1042 1043 1044 1045 1046 1047

  /*
    We must save the previous value to be able to restore it if the
    row was not inserted
  */
  thd->prev_insert_id= thd->next_insert_id;

  if ((nr= table->next_number_field->val_int()) != 0 ||
unknown's avatar
unknown committed
1048
      table->auto_increment_field_not_null &&
1049
      thd->variables.sql_mode & MODE_NO_AUTO_VALUE_ON_ZERO)
1050
  {
1051
    /* Clear flag for next row */
unknown's avatar
unknown committed
1052
    table->auto_increment_field_not_null= FALSE;
1053
    /* Mark that we didn't generate a new value **/
1054
    auto_increment_column_changed=0;
1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065

    /* Update next_insert_id if we have already generated a value */
    if (thd->clear_next_insert_id && nr >= thd->next_insert_id)
    {
      if (variables->auto_increment_increment != 1)
        nr= next_insert_id(nr, variables);
      else
        nr++;
      thd->next_insert_id= nr;
      DBUG_PRINT("info",("next_insert_id: %lu", (ulong) nr));
    }
unknown's avatar
unknown committed
1066
    DBUG_VOID_RETURN;
1067
  }
unknown's avatar
unknown committed
1068
  table->auto_increment_field_not_null= FALSE;
1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087
  if (!(nr= thd->next_insert_id))
  {
    nr= get_auto_increment();
    if (variables->auto_increment_increment != 1)
      nr= next_insert_id(nr-1, variables);
    /*
      Update next row based on the found value. This way we don't have to
      call the handler for every generated auto-increment value on a
      multi-row statement
    */
    thd->next_insert_id= nr;
  }

  DBUG_PRINT("info",("auto_increment: %lu", (ulong) nr));

  /* Mark that we should clear next_insert_id before next stmt */
  thd->clear_next_insert_id= 1;

  if (!table->next_number_field->store((longlong) nr))
1088
    thd->insert_id((ulonglong) nr);
1089 1090
  else
    thd->insert_id(table->next_number_field->val_int());
1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109

  /*
    We can't set next_insert_id if the auto-increment key is not the
    first key part, as there is no guarantee that the first parts will be in
    sequence
  */
  if (!table->next_number_key_offset)
  {
    /*
      Set next insert id to point to next auto-increment value to be able to
      handle multi-row statements
      This works even if auto_increment_increment > 1
    */
    thd->next_insert_id= next_insert_id(nr, variables);
  }
  else
    thd->next_insert_id= 0;

  /* Mark that we generated a new value */
1110
  auto_increment_column_changed=1;
unknown's avatar
unknown committed
1111 1112 1113
  DBUG_VOID_RETURN;
}

1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127
/*
  restore_auto_increment

  In case of error on write, we restore the last used next_insert_id value
  because the previous value was not used.
*/

void handler::restore_auto_increment()
{
  THD *thd= table->in_use;
  if (thd->next_insert_id)
    thd->next_insert_id= thd->prev_insert_id;
}

unknown's avatar
unknown committed
1128

1129
ulonglong handler::get_auto_increment()
unknown's avatar
unknown committed
1130
{
1131
  ulonglong nr;
unknown's avatar
unknown committed
1132
  int error;
unknown's avatar
unknown committed
1133

unknown's avatar
unknown committed
1134 1135
  (void) extra(HA_EXTRA_KEYREAD);
  index_init(table->next_number_index);
unknown's avatar
unknown committed
1136 1137 1138 1139 1140 1141 1142
  if (!table->next_number_key_offset)
  {						// Autoincrement at key-start
    error=index_last(table->record[1]);
  }
  else
  {
    byte key[MAX_KEY_LENGTH];
1143 1144
    key_copy(key, table->record[0],
             table->key_info + table->next_number_index,
unknown's avatar
unknown committed
1145 1146 1147 1148 1149
             table->next_number_key_offset);
    error=index_read(table->record[1], key, table->next_number_key_offset,
                     HA_READ_PREFIX_LAST);
  }

unknown's avatar
unknown committed
1150 1151 1152
  if (error)
    nr=1;
  else
1153 1154
    nr=((ulonglong) table->next_number_field->
        val_int_offset(table->rec_buff_length)+1);
unknown's avatar
unknown committed
1155
  index_end();
unknown's avatar
unknown committed
1156
  (void) extra(HA_EXTRA_NO_KEYREAD);
unknown's avatar
unknown committed
1157 1158 1159 1160 1161 1162 1163
  return nr;
}

	/* Print error that we got from handler function */

void handler::print_error(int error, myf errflag)
{
1164
  DBUG_ENTER("handler::print_error");
unknown's avatar
unknown committed
1165 1166 1167 1168
  DBUG_PRINT("enter",("error: %d",error));

  int textno=ER_GET_ERRNO;
  switch (error) {
1169 1170 1171
  case EACCES:
    textno=ER_OPEN_AS_READONLY;
    break;
unknown's avatar
unknown committed
1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182
  case EAGAIN:
    textno=ER_FILE_USED;
    break;
  case ENOENT:
    textno=ER_FILE_NOT_FOUND;
    break;
  case HA_ERR_KEY_NOT_FOUND:
  case HA_ERR_NO_ACTIVE_RECORD:
  case HA_ERR_END_OF_FILE:
    textno=ER_KEY_NOT_FOUND;
    break;
unknown's avatar
unknown committed
1183
  case HA_ERR_WRONG_MRG_TABLE_DEF:
unknown's avatar
unknown committed
1184 1185 1186 1187 1188 1189 1190 1191 1192
    textno=ER_WRONG_MRG_TABLE;
    break;
  case HA_ERR_FOUND_DUPP_KEY:
  {
    uint key_nr=get_dup_key(error);
    if ((int) key_nr >= 0)
    {
      /* Write the dupplicated key in the error message */
      char key[MAX_KEY_LENGTH];
1193
      String str(key,sizeof(key),system_charset_info);
unknown's avatar
unknown committed
1194
      key_unpack(&str,table,(uint) key_nr);
unknown's avatar
unknown committed
1195
      uint max_length=MYSQL_ERRMSG_SIZE-(uint) strlen(ER(ER_DUP_ENTRY));
unknown's avatar
unknown committed
1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215
      if (str.length() >= max_length)
      {
	str.length(max_length-4);
	str.append("...");
      }
      my_error(ER_DUP_ENTRY,MYF(0),str.c_ptr(),key_nr+1);
      DBUG_VOID_RETURN;
    }
    textno=ER_DUP_KEY;
    break;
  }
  case HA_ERR_FOUND_DUPP_UNIQUE:
    textno=ER_DUP_UNIQUE;
    break;
  case HA_ERR_RECORD_CHANGED:
    textno=ER_CHECKREAD;
    break;
  case HA_ERR_CRASHED:
    textno=ER_NOT_KEYFILE;
    break;
1216 1217 1218 1219 1220 1221
  case HA_ERR_CRASHED_ON_USAGE:
    textno=ER_CRASHED_ON_USAGE;
    break;
  case HA_ERR_CRASHED_ON_REPAIR:
    textno=ER_CRASHED_ON_REPAIR;
    break;
unknown's avatar
unknown committed
1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236
  case HA_ERR_OUT_OF_MEM:
    my_error(ER_OUT_OF_RESOURCES,errflag);
    DBUG_VOID_RETURN;
  case HA_ERR_WRONG_COMMAND:
    textno=ER_ILLEGAL_HA;
    break;
  case HA_ERR_OLD_FILE:
    textno=ER_OLD_KEYFILE;
    break;
  case HA_ERR_UNSUPPORTED:
    textno=ER_UNSUPPORTED_EXTENSION;
    break;
  case HA_ERR_RECORD_FILE_FULL:
    textno=ER_RECORD_FILE_FULL;
    break;
1237 1238 1239 1240 1241 1242
  case HA_ERR_LOCK_WAIT_TIMEOUT:
    textno=ER_LOCK_WAIT_TIMEOUT;
    break;
  case HA_ERR_LOCK_TABLE_FULL:
    textno=ER_LOCK_TABLE_FULL;
    break;
1243 1244 1245
  case HA_ERR_LOCK_DEADLOCK:
    textno=ER_LOCK_DEADLOCK;
    break;
1246 1247 1248
  case HA_ERR_READ_ONLY_TRANSACTION:
    textno=ER_READ_ONLY_TRANSACTION;
    break;
unknown's avatar
Merge  
unknown committed
1249 1250 1251 1252 1253 1254 1255 1256 1257
  case HA_ERR_CANNOT_ADD_FOREIGN:
    textno=ER_CANNOT_ADD_FOREIGN;
    break;
  case HA_ERR_ROW_IS_REFERENCED:
    textno=ER_ROW_IS_REFERENCED;
    break;
  case HA_ERR_NO_REFERENCED_ROW:
    textno=ER_NO_REFERENCED_ROW;
    break;
1258 1259
  case HA_ERR_NO_SUCH_TABLE:
  {
unknown's avatar
unknown committed
1260 1261 1262 1263 1264
    /*
      We have to use path to find database name instead of using
      table->table_cache_key because if the table didn't exist, then
      table_cache_key was not set up
    */
1265 1266 1267 1268 1269 1270 1271 1272
    char *db;
    char buff[FN_REFLEN];
    uint length=dirname_part(buff,table->path);
    buff[length-1]=0;
    db=buff+dirname_length(buff);
    my_error(ER_NO_SUCH_TABLE,MYF(0),db,table->table_name);
    break;
  }
unknown's avatar
unknown committed
1273 1274
  default:
    {
1275 1276 1277
      /* The error was "unknown" to this function.
	 Ask handler if it has got a message for this error */
      bool temporary= FALSE;
1278 1279 1280
      String str;
      temporary= get_error_message(error, &str);
      if (!str.is_empty())
1281
      {
1282
	const char* engine= table_type();
1283
	if (temporary)
1284
	  my_error(ER_GET_TEMPORARY_ERRMSG,MYF(0),error,str.ptr(),engine);
1285
	else
1286
	  my_error(ER_GET_ERRMSG,MYF(0),error,str.ptr(),engine);
1287 1288 1289
      }
      else       
	my_error(ER_GET_ERRNO,errflag,error);
unknown's avatar
unknown committed
1290 1291 1292 1293 1294 1295 1296
      DBUG_VOID_RETURN;
    }
  }
  my_error(textno,errflag,table->table_name,error);
  DBUG_VOID_RETURN;
}

unknown's avatar
unknown committed
1297

1298 1299 1300 1301
/* 
   Return an error message specific to this handler
   
   SYNOPSIS
1302 1303
   error        error code previously returned by handler
   buf          Pointer to String where to add error message
1304
   
1305
   Returns true if this is a temporary error
1306 1307
 */

1308
bool handler::get_error_message(int error, String* buf)
1309
{
unknown's avatar
unknown committed
1310
  return FALSE;
1311 1312 1313
}


unknown's avatar
unknown committed
1314
/* Return key if error because of duplicated keys */
unknown's avatar
unknown committed
1315 1316 1317

uint handler::get_dup_key(int error)
{
1318
  DBUG_ENTER("handler::get_dup_key");
unknown's avatar
unknown committed
1319 1320 1321 1322 1323 1324
  table->file->errkey  = (uint) -1;
  if (error == HA_ERR_FOUND_DUPP_KEY || error == HA_ERR_FOUND_DUPP_UNIQUE)
    info(HA_STATUS_ERRKEY | HA_STATUS_NO_LOCK);
  DBUG_RETURN(table->file->errkey);
}

unknown's avatar
unknown committed
1325

unknown's avatar
unknown committed
1326 1327
int handler::delete_table(const char *name)
{
unknown's avatar
unknown committed
1328
  int error=0;
unknown's avatar
unknown committed
1329 1330 1331
  for (const char **ext=bas_ext(); *ext ; ext++)
  {
    if (delete_file(name,*ext,2))
unknown's avatar
unknown committed
1332 1333 1334 1335
    {
      if ((error=errno) != ENOENT)
	break;
    }
unknown's avatar
unknown committed
1336
  }
unknown's avatar
unknown committed
1337
  return error;
unknown's avatar
unknown committed
1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351
}


int handler::rename_table(const char * from, const char * to)
{
  DBUG_ENTER("handler::rename_table");
  for (const char **ext=bas_ext(); *ext ; ext++)
  {
    if (rename_file_ext(from,to,*ext))
      DBUG_RETURN(my_errno);
  }
  DBUG_RETURN(0);
}

unknown's avatar
unknown committed
1352
/*
1353
  Tell the handler to turn on or off transaction in the handler
1354
*/
unknown's avatar
unknown committed
1355

1356
int ha_enable_transaction(THD *thd, bool on)
1357 1358 1359
{
  int error=0;

1360 1361
  DBUG_ENTER("ha_enable_transaction");
  thd->transaction.on= on;
1362 1363 1364
  DBUG_RETURN(error);
}

unknown's avatar
unknown committed
1365 1366 1367 1368 1369
int handler::index_next_same(byte *buf, const byte *key, uint keylen)
{
  int error;
  if (!(error=index_next(buf)))
  {
unknown's avatar
unknown committed
1370
    if (key_cmp_if_same(table, key, active_index, keylen))
unknown's avatar
unknown committed
1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383
    {
      table->status=STATUS_NOT_FOUND;
      error=HA_ERR_END_OF_FILE;
    }
  }
  return error;
}


/****************************************************************************
** Some general functions that isn't in the handler class
****************************************************************************/

unknown's avatar
unknown committed
1384 1385 1386 1387
/*
  Initiates table-file and calls apropriate database-creator
  Returns 1 if something got wrong
*/
unknown's avatar
unknown committed
1388 1389 1390 1391 1392 1393

int ha_create_table(const char *name, HA_CREATE_INFO *create_info,
		    bool update_create_info)
{
  int error;
  TABLE table;
unknown's avatar
unknown committed
1394
  char name_buff[FN_REFLEN];
unknown's avatar
unknown committed
1395 1396
  DBUG_ENTER("ha_create_table");

1397
  if (openfrm(current_thd, name,"",0,(uint) READ_ALL, 0, &table))
unknown's avatar
unknown committed
1398 1399 1400 1401 1402
    DBUG_RETURN(1);
  if (update_create_info)
  {
    update_create_info_from_table(create_info, &table);
  }
unknown's avatar
unknown committed
1403 1404 1405 1406 1407
  if (lower_case_table_names == 2 &&
      !(table.file->table_flags() & HA_FILE_BASED))
  {
    /* Ensure that handler gets name in lower case */
    strmov(name_buff, name);
1408
    my_casedn_str(files_charset_info, name_buff);
unknown's avatar
unknown committed
1409 1410 1411
    name= name_buff;
  }

unknown's avatar
unknown committed
1412 1413
  error=table.file->create(name,&table,create_info);
  VOID(closefrm(&table));
unknown's avatar
unknown committed
1414
  if (error)
unknown's avatar
unknown committed
1415
    my_error(ER_CANT_CREATE_TABLE,MYF(ME_BELL+ME_WAITTANG),name,error);
unknown's avatar
unknown committed
1416 1417 1418
  DBUG_RETURN(error != 0);
}

1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435
/*
  Try to discover table from engine and 
  if found, write the frm file to disk.
  
  RETURN VALUES:
   0 : Table existed in engine and created 
       on disk if so requested
   1 : Table does not exist
  >1 : error

*/

int ha_create_table_from_engine(THD* thd, 
				const char *db, 
				const char *name,
				bool create_if_found)
{
unknown's avatar
unknown committed
1436 1437 1438
  int error;
  const void *frmblob;
  uint frmlen;
1439 1440 1441 1442
  char path[FN_REFLEN];
  HA_CREATE_INFO create_info;
  TABLE table;
  DBUG_ENTER("ha_create_table_from_engine");
unknown's avatar
unknown committed
1443 1444
  DBUG_PRINT("enter", ("name '%s'.'%s'  create_if_found: %d",
                       db, name, create_if_found));
1445 1446 1447 1448 1449

  bzero((char*) &create_info,sizeof(create_info));

  if ((error= ha_discover(thd, db, name, &frmblob, &frmlen)))
    DBUG_RETURN(error); 
unknown's avatar
unknown committed
1450 1451 1452 1453
  /*
    Table exists in handler
    frmblob and frmlen are set
  */
1454

unknown's avatar
unknown committed
1455
  if (create_if_found)
1456 1457 1458 1459 1460 1461
  {
    (void)strxnmov(path,FN_REFLEN,mysql_data_home,"/",db,"/",name,NullS);
    // Save the frm file    
    if ((error = writefrm(path, frmblob, frmlen)))
      goto err_end;

1462
    if (openfrm(thd, path,"",0,(uint) READ_ALL, 0, &table))
1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479
      DBUG_RETURN(1);

    update_create_info_from_table(&create_info, &table);
    create_info.table_options|= HA_CREATE_FROM_ENGINE;

    if (lower_case_table_names == 2 &&
	!(table.file->table_flags() & HA_FILE_BASED))
    {
      /* Ensure that handler gets name in lower case */
      my_casedn_str(files_charset_info, path);
    }
    
    error=table.file->create(path,&table,&create_info);
    VOID(closefrm(&table));
  }

err_end:
1480
  my_free((char*) frmblob, MYF(MY_ALLOW_ZERO_PTR));
1481 1482 1483
  DBUG_RETURN(error);  
}

1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508
static int NEAR_F delete_file(const char *name,const char *ext,int extflag)
{
  char buff[FN_REFLEN];
  VOID(fn_format(buff,name,"",ext,extflag | 4));
  return(my_delete_with_symlink(buff,MYF(MY_WME)));
}

void st_ha_check_opt::init()
{
  flags= sql_flags= 0;
  sort_buffer_size = current_thd->variables.myisam_sort_buff_size;
}


/*****************************************************************************
  Key cache handling.

  This code is only relevant for ISAM/MyISAM tables

  key_cache->cache may be 0 only in the case where a key cache is not
  initialized or when we where not able to init the key cache in a previous
  call to ha_init_key_cache() (probably out of memory)
*****************************************************************************/

/* Init a key cache if it has not been initied before */
unknown's avatar
unknown committed
1509

1510

unknown's avatar
unknown committed
1511
int ha_init_key_cache(const char *name, KEY_CACHE *key_cache)
unknown's avatar
unknown committed
1512
{
1513 1514
  DBUG_ENTER("ha_init_key_cache");

unknown's avatar
unknown committed
1515
  if (!key_cache->key_cache_inited)
1516 1517
  {
    pthread_mutex_lock(&LOCK_global_system_variables);
unknown's avatar
unknown committed
1518 1519 1520 1521
    long tmp_buff_size= (long) key_cache->param_buff_size;
    long tmp_block_size= (long) key_cache->param_block_size;
    uint division_limit= key_cache->param_division_limit;
    uint age_threshold=  key_cache->param_age_threshold;
1522
    pthread_mutex_unlock(&LOCK_global_system_variables);
unknown's avatar
unknown committed
1523
    DBUG_RETURN(!init_key_cache(key_cache,
1524 1525
				tmp_block_size,
				tmp_buff_size,
unknown's avatar
unknown committed
1526
				division_limit, age_threshold));
1527
  }
1528
  DBUG_RETURN(0);
unknown's avatar
unknown committed
1529 1530
}

1531 1532 1533

/* Resize key cache */

unknown's avatar
unknown committed
1534
int ha_resize_key_cache(KEY_CACHE *key_cache)
unknown's avatar
unknown committed
1535
{
1536 1537
  DBUG_ENTER("ha_resize_key_cache");

unknown's avatar
unknown committed
1538
  if (key_cache->key_cache_inited)
1539 1540
  {
    pthread_mutex_lock(&LOCK_global_system_variables);
unknown's avatar
unknown committed
1541 1542 1543 1544
    long tmp_buff_size= (long) key_cache->param_buff_size;
    long tmp_block_size= (long) key_cache->param_block_size;
    uint division_limit= key_cache->param_division_limit;
    uint age_threshold=  key_cache->param_age_threshold;
1545
    pthread_mutex_unlock(&LOCK_global_system_variables);
unknown's avatar
unknown committed
1546 1547 1548
    DBUG_RETURN(!resize_key_cache(key_cache, tmp_block_size,
				  tmp_buff_size,
				  division_limit, age_threshold));
1549
  }
1550
  DBUG_RETURN(0);
1551 1552
}

1553 1554 1555

/* Change parameters for key cache (like size) */

unknown's avatar
unknown committed
1556
int ha_change_key_cache_param(KEY_CACHE *key_cache)
1557
{
unknown's avatar
unknown committed
1558 1559 1560 1561 1562 1563 1564 1565
  if (key_cache->key_cache_inited)
  {
    pthread_mutex_lock(&LOCK_global_system_variables);
    uint division_limit= key_cache->param_division_limit;
    uint age_threshold=  key_cache->param_age_threshold;
    pthread_mutex_unlock(&LOCK_global_system_variables);
    change_key_cache_param(key_cache, division_limit, age_threshold);
  }
1566 1567
  return 0;
}
unknown's avatar
unknown committed
1568

1569 1570
/* Free memory allocated by a key cache */

unknown's avatar
unknown committed
1571
int ha_end_key_cache(KEY_CACHE *key_cache)
unknown's avatar
unknown committed
1572
{
unknown's avatar
unknown committed
1573
  end_key_cache(key_cache, 1);		// Can never fail
1574
  return 0;
unknown's avatar
unknown committed
1575
}
unknown's avatar
unknown committed
1576

1577
/* Move all tables from one key cache to another one */
unknown's avatar
unknown committed
1578

unknown's avatar
unknown committed
1579 1580
int ha_change_key_cache(KEY_CACHE *old_key_cache,
			KEY_CACHE *new_key_cache)
unknown's avatar
unknown committed
1581
{
1582 1583
  mi_change_key_cache(old_key_cache, new_key_cache);
  return 0;
unknown's avatar
unknown committed
1584
}
1585 1586


unknown's avatar
unknown committed
1587 1588
/*
  Try to discover one table from handler(s)
unknown's avatar
unknown committed
1589 1590 1591 1592

  RETURN
    0  ok. In this case *frmblob and *frmlen are set
    1  error.  frmblob and frmlen may not be set
unknown's avatar
unknown committed
1593 1594
*/

unknown's avatar
unknown committed
1595 1596
int ha_discover(THD *thd, const char *db, const char *name,
		const void **frmblob, uint *frmlen)
unknown's avatar
unknown committed
1597 1598 1599
{
  int error= 1; // Table does not exist in any handler
  DBUG_ENTER("ha_discover");
1600
  DBUG_PRINT("enter", ("db: %s, name: %s", db, name));
unknown's avatar
unknown committed
1601 1602
#ifdef HAVE_NDBCLUSTER_DB
  if (have_ndbcluster == SHOW_OPTION_YES)
1603
    error= ndbcluster_discover(thd, db, name, frmblob, frmlen);
unknown's avatar
unknown committed
1604 1605 1606 1607 1608 1609 1610
#endif
  if (!error)
    statistic_increment(ha_discover_count,&LOCK_status);
  DBUG_RETURN(error);
}


1611
/*
1612 1613 1614
  Call this function in order to give the handler the possiblity 
  to ask engine if there are any new tables that should be written to disk 
  or any dropped tables that need to be removed from disk
1615 1616
*/

1617 1618
int
ha_find_files(THD *thd,const char *db,const char *path,
unknown's avatar
unknown committed
1619
	      const char *wild, bool dir, List<char> *files)
1620 1621
{
  int error= 0;
1622 1623 1624
  DBUG_ENTER("ha_find_files");
  DBUG_PRINT("enter", ("db: %s, path: %s, wild: %s, dir: %d", 
		       db, path, wild, dir));
1625 1626
#ifdef HAVE_NDBCLUSTER_DB
  if (have_ndbcluster == SHOW_OPTION_YES)
unknown's avatar
unknown committed
1627
    error= ndbcluster_find_files(thd, db, path, wild, dir, files);
1628 1629
#endif
  DBUG_RETURN(error);
1630 1631
  
  
1632 1633
}

unknown's avatar
unknown committed
1634 1635
#ifdef NOT_YET_USED

1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656
/*
  Ask handler if the table exists in engine

  RETURN
    0                   Table does not exist
    1                   Table exists
    #                   Error code

 */
int ha_table_exists(THD* thd, const char* db, const char* name)
{
  int error= 2;
  DBUG_ENTER("ha_table_exists");
  DBUG_PRINT("enter", ("db: %s, name: %s", db, name));
#ifdef HAVE_NDBCLUSTER_DB
  if (have_ndbcluster == SHOW_OPTION_YES)
    error= ndbcluster_table_exists(thd, db, name);
#endif
  DBUG_RETURN(error);
}

unknown's avatar
unknown committed
1657
#endif
1658 1659


1660 1661 1662 1663 1664 1665 1666 1667
/*
  Read first row between two ranges.
  Store ranges for future calls to read_range_next

  SYNOPSIS
    read_range_first()
    start_key		Start key. Is 0 if no min range
    end_key		End key.  Is 0 if no max range
unknown's avatar
unknown committed
1668
    eq_range_arg	Set to 1 if start_key == end_key		
1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681
    sorted		Set to 1 if result should be sorted per key

  NOTES
    Record is read into table->record[0]

  RETURN
    0			Found row
    HA_ERR_END_OF_FILE	No rows in range
    #			Error code
*/

int handler::read_range_first(const key_range *start_key,
			      const key_range *end_key,
unknown's avatar
unknown committed
1682
			      bool eq_range_arg, bool sorted)
1683 1684 1685 1686
{
  int result;
  DBUG_ENTER("handler::read_range_first");

unknown's avatar
unknown committed
1687
  eq_range= eq_range_arg;
1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705
  end_range= 0;
  if (end_key)
  {
    end_range= &save_end_range;
    save_end_range= *end_key;
    key_compare_result_on_equal= ((end_key->flag == HA_READ_BEFORE_KEY) ? 1 :
				  (end_key->flag == HA_READ_AFTER_KEY) ? -1 : 0);
  }
  range_key_part= table->key_info[active_index].key_part;

  if (!start_key)			// Read first record
    result= index_first(table->record[0]);
  else
    result= index_read(table->record[0],
		       start_key->key,
		       start_key->length,
		       start_key->flag);
  if (result)
unknown's avatar
unknown committed
1706 1707 1708
    DBUG_RETURN((result == HA_ERR_KEY_NOT_FOUND) 
		? HA_ERR_END_OF_FILE
		: result);
1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728

  DBUG_RETURN (compare_key(end_range) <= 0 ? 0 : HA_ERR_END_OF_FILE);
}


/*
  Read next row between two ranges.

  SYNOPSIS
    read_range_next()

  NOTES
    Record is read into table->record[0]

  RETURN
    0			Found row
    HA_ERR_END_OF_FILE	No rows in range
    #			Error code
*/

unknown's avatar
unknown committed
1729
int handler::read_range_next()
1730 1731 1732 1733 1734
{
  int result;
  DBUG_ENTER("handler::read_range_next");

  if (eq_range)
unknown's avatar
unknown committed
1735 1736 1737 1738 1739 1740 1741
  {
    /* We trust that index_next_same always gives a row in range */
    DBUG_RETURN(index_next_same(table->record[0],
                                end_range->key,
                                end_range->length));
  }
  result= index_next(table->record[0]);
1742 1743 1744 1745 1746 1747 1748
  if (result)
    DBUG_RETURN(result);
  DBUG_RETURN(compare_key(end_range) <= 0 ? 0 : HA_ERR_END_OF_FILE);
}


/*
unknown's avatar
unknown committed
1749
  Compare if found key (in row) is over max-value
1750 1751 1752

  SYNOPSIS
    compare_key
unknown's avatar
unknown committed
1753
    range		range to compare to row. May be 0 for no range
1754 1755
 
  NOTES
unknown's avatar
unknown committed
1756
    See key.cc::key_cmp() for details
1757 1758

  RETURN
unknown's avatar
unknown committed
1759 1760
    The return value is SIGN(key_in_row - range_key):

1761 1762 1763 1764 1765 1766 1767
    0			Key is equal to range or 'range' == 0 (no range)
   -1			Key is less than range
    1			Key is larger than range
*/

int handler::compare_key(key_range *range)
{
unknown's avatar
unknown committed
1768
  int cmp;
1769 1770
  if (!range)
    return 0;					// No max range
unknown's avatar
unknown committed
1771 1772 1773 1774
  cmp= key_cmp(range_key_part, range->key, range->length);
  if (!cmp)
    cmp= key_compare_result_on_equal;
  return cmp;
1775
}
unknown's avatar
unknown committed
1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787

int handler::index_read_idx(byte * buf, uint index, const byte * key,
			     uint key_len, enum ha_rkey_function find_flag)
{
  int error= ha_index_init(index);
  if (!error)
    error= index_read(buf, key, key_len, find_flag);
  if (!error)
    error= ha_index_end();
  return error;
}

1788

1789 1790 1791 1792 1793 1794 1795 1796
/*
  Returns a list of all known extensions.

  SYNOPSIS
    ha_known_exts()
 
  NOTES
    No mutexes, worst case race is a minor surplus memory allocation
1797 1798
    We have to recreate the extension map if mysqld is restarted (for example
    within libmysqld)
1799 1800 1801 1802

  RETURN VALUE
    pointer		pointer to TYPELIB structure
*/
1803

1804 1805
TYPELIB *ha_known_exts(void)
{
1806
  if (!known_extensions.type_names || mysys_usage_id != known_extensions_id)
1807 1808 1809 1810
  {
    show_table_type_st *types;
    List<char> found_exts;
    List_iterator_fast<char> it(found_exts);
1811 1812 1813 1814
    const char **ext, *old_ext;

    known_extensions_id= mysys_usage_id;
    found_exts.push_back((char*) ".db");
1815 1816 1817 1818 1819 1820 1821
    for (types= sys_table_types; types->type; types++)
    {      
      if (*types->value == SHOW_OPTION_YES)
      {
	handler *file= get_new_handler(0,(enum db_type) types->db_type);
	for (ext= file->bas_ext(); *ext; ext++)
	{
1822 1823 1824
	  while ((old_ext= it++))
          {
	    if (!strcmp(old_ext, *ext))
1825
	      break;
1826 1827 1828
          }
	  if (!old_ext)
	    found_exts.push_back((char *) *ext);
1829 1830 1831 1832 1833 1834

	  it.rewind();
	}
	delete file;
      }
    }
1835 1836 1837
    ext= (const char **) my_once_alloc(sizeof(char *)*
                                       (found_exts.elements+1),
                                       MYF(MY_WME | MY_FAE));
1838 1839 1840 1841
    
    DBUG_ASSERT(ext);
    known_extensions.count= found_exts.elements;
    known_extensions.type_names= ext;
1842 1843 1844 1845

    while ((old_ext= it++))
      *ext++= old_ext;
    *ext= 0;
1846 1847 1848
  }
  return &known_extensions;
}