~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/plugin/storage_engine.cc

  • Committer: Monty Taylor
  • Date: 2010-10-30 01:19:00 UTC
  • mto: (1892.1.2 build)
  • mto: This revision was merged to the branch mainline in revision 1893.
  • Revision ID: mordred@inaugust.com-20101030011900-2tdt8w9vt7a6pbk0
Fixed things to make things compile with clang

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
3
 *
4
 
 *  Copyright (C) 2008 Sun Microsystems, Inc.
 
4
 *  Copyright (C) 2008 Sun Microsystems
5
5
 *
6
6
 *  This program is free software; you can redistribute it and/or modify
7
7
 *  it under the terms of the GNU General Public License as published by
17
17
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
18
 */
19
19
 
20
 
#include <config.h>
 
20
#include "config.h"
21
21
 
22
22
#include <fcntl.h>
23
23
#include <unistd.h>
32
32
#include <google/protobuf/io/zero_copy_stream.h>
33
33
#include <google/protobuf/io/zero_copy_stream_impl.h>
34
34
 
35
 
#include <drizzled/cached_directory.h>
 
35
#include "drizzled/cached_directory.h"
36
36
 
37
37
#include <drizzled/definitions.h>
38
38
#include <drizzled/base.h>
43
43
#include <drizzled/gettext.h>
44
44
#include <drizzled/unireg.h>
45
45
#include <drizzled/data_home.h>
46
 
#include <drizzled/errmsg_print.h>
47
 
#include <drizzled/xid.h>
48
 
#include <drizzled/sql_table.h>
49
 
#include <drizzled/global_charset_info.h>
50
 
#include <drizzled/charset.h>
51
 
#include <drizzled/internal/my_sys.h>
 
46
#include "drizzled/errmsg_print.h"
 
47
#include "drizzled/xid.h"
 
48
#include "drizzled/sql_table.h"
 
49
#include "drizzled/global_charset_info.h"
 
50
#include "drizzled/charset.h"
 
51
#include "drizzled/internal/my_sys.h"
 
52
#include "drizzled/db.h"
52
53
 
53
54
#include <drizzled/table_proto.h>
54
55
#include <drizzled/plugin/event_observer.h>
55
 
#include <drizzled/internal_error_handler.h>
56
56
 
57
57
#include <drizzled/table/shell.h>
58
58
 
59
 
#include <drizzled/message/cache.h>
60
 
 
61
59
#include <boost/algorithm/string/compare.hpp>
62
60
 
63
61
static bool shutdown_has_begun= false; // Once we put in the container for the vector/etc for engines this will go away.
64
62
 
 
63
using namespace std;
 
64
 
65
65
namespace drizzled
66
66
{
67
67
 
82
82
  return vector_of_schema_engines;
83
83
}
84
84
 
85
 
StorageEngine::StorageEngine(const std::string name_arg,
86
 
                             const std::bitset<HTON_BIT_SIZE> &flags_arg) :
 
85
StorageEngine::StorageEngine(const string name_arg,
 
86
                             const bitset<HTON_BIT_SIZE> &flags_arg) :
87
87
  Plugin(name_arg, "StorageEngine"),
88
88
  MonitoredInTransaction(), /* This gives the storage engine a "slot" or ID */
89
89
  flags(flags_arg)
101
101
}
102
102
 
103
103
 
104
 
int StorageEngine::renameTable(Session &session, const identifier::Table &from, const identifier::Table &to)
 
104
int StorageEngine::renameTable(Session &session, const TableIdentifier &from, const TableIdentifier &to)
105
105
{
106
106
  int error;
107
107
  setTransactionReadWrite(session);
137
137
  @retval
138
138
    !0  Error
139
139
*/
140
 
int StorageEngine::doDropTable(Session&, const identifier::Table &identifier)
 
140
int StorageEngine::doDropTable(Session&, const TableIdentifier &identifier)
141
141
                               
142
142
{
143
143
  int error= 0;
192
192
}
193
193
 
194
194
class FindEngineByName
195
 
  : public std::unary_function<StorageEngine *, bool>
 
195
  : public unary_function<StorageEngine *, bool>
196
196
{
197
 
  const std::string &predicate;
 
197
  const string &predicate;
198
198
 
199
199
public:
200
 
  explicit FindEngineByName(const std::string &target_arg) :
 
200
  explicit FindEngineByName(const string &target_arg) :
201
201
    predicate(target_arg)
202
202
  {
203
203
  }
208
208
  }
209
209
};
210
210
 
211
 
StorageEngine *StorageEngine::findByName(const std::string &predicate)
 
211
StorageEngine *StorageEngine::findByName(const string &predicate)
212
212
{
213
 
  EngineVector::iterator iter= std::find_if(vector_of_engines.begin(),
214
 
                                            vector_of_engines.end(),
215
 
                                            FindEngineByName(predicate));
 
213
  EngineVector::iterator iter= find_if(vector_of_engines.begin(),
 
214
                                       vector_of_engines.end(),
 
215
                                       FindEngineByName(predicate));
216
216
  if (iter != vector_of_engines.end())
217
217
  {
218
218
    StorageEngine *engine= *iter;
223
223
  return NULL;
224
224
}
225
225
 
226
 
StorageEngine *StorageEngine::findByName(Session& session, const std::string &predicate)
 
226
StorageEngine *StorageEngine::findByName(Session& session, const string &predicate)
227
227
{
228
228
  if (boost::iequals(predicate, DEFAULT_STRING))
229
229
    return session.getDefaultStorageEngine();
230
230
 
231
 
  EngineVector::iterator iter= std::find_if(vector_of_engines.begin(),
232
 
                                            vector_of_engines.end(),
233
 
                                            FindEngineByName(predicate));
 
231
  EngineVector::iterator iter= find_if(vector_of_engines.begin(),
 
232
                                       vector_of_engines.end(),
 
233
                                       FindEngineByName(predicate));
234
234
  if (iter != vector_of_engines.end())
235
235
  {
236
236
    StorageEngine *engine= *iter;
241
241
  return NULL;
242
242
}
243
243
 
244
 
class StorageEngineCloseConnection : public std::unary_function<StorageEngine *, void>
 
244
class StorageEngineCloseConnection : public unary_function<StorageEngine *, void>
245
245
{
246
246
  Session *session;
247
247
public:
263
263
*/
264
264
void StorageEngine::closeConnection(Session* session)
265
265
{
266
 
  std::for_each(vector_of_engines.begin(), vector_of_engines.end(),
267
 
                StorageEngineCloseConnection(session));
 
266
  for_each(vector_of_engines.begin(), vector_of_engines.end(),
 
267
           StorageEngineCloseConnection(session));
268
268
}
269
269
 
270
270
bool StorageEngine::flushLogs(StorageEngine *engine)
271
271
{
272
272
  if (engine == NULL)
273
273
  {
274
 
    if (std::find_if(vector_of_engines.begin(), vector_of_engines.end(),
275
 
                     std::mem_fun(&StorageEngine::flush_logs))
 
274
    if (find_if(vector_of_engines.begin(), vector_of_engines.end(),
 
275
                mem_fun(&StorageEngine::flush_logs))
276
276
        != vector_of_engines.begin())
277
277
      return true;
278
278
  }
284
284
  return false;
285
285
}
286
286
 
287
 
class StorageEngineGetTableDefinition: public std::unary_function<StorageEngine *,bool>
 
287
class StorageEngineGetTableDefinition: public unary_function<StorageEngine *,bool>
288
288
{
289
289
  Session& session;
290
 
  const identifier::Table &identifier;
 
290
  const TableIdentifier &identifier;
291
291
  message::Table &table_message;
292
 
  drizzled::error_t &err;
 
292
  int &err;
293
293
 
294
294
public:
295
295
  StorageEngineGetTableDefinition(Session& session_arg,
296
 
                                  const identifier::Table &identifier_arg,
 
296
                                  const TableIdentifier &identifier_arg,
297
297
                                  message::Table &table_message_arg,
298
 
                                  drizzled::error_t &err_arg) :
 
298
                                  int &err_arg) :
299
299
    session(session_arg), 
300
300
    identifier(identifier_arg),
301
301
    table_message(table_message_arg), 
306
306
    int ret= engine->doGetTableDefinition(session, identifier, table_message);
307
307
 
308
308
    if (ret != ENOENT)
309
 
      err= static_cast<drizzled::error_t>(ret);
 
309
      err= ret;
310
310
 
311
 
    return err == static_cast<drizzled::error_t>(EEXIST) or err != static_cast<drizzled::error_t>(ENOENT);
 
311
    return err == EEXIST || err != ENOENT;
312
312
  }
313
313
};
314
314
 
315
 
class StorageEngineDoesTableExist: public std::unary_function<StorageEngine *, bool>
 
315
class StorageEngineDoesTableExist: public unary_function<StorageEngine *, bool>
316
316
{
317
317
  Session& session;
318
 
  const identifier::Table &identifier;
 
318
  const TableIdentifier &identifier;
319
319
 
320
320
public:
321
 
  StorageEngineDoesTableExist(Session& session_arg, const identifier::Table &identifier_arg) :
 
321
  StorageEngineDoesTableExist(Session& session_arg, const TableIdentifier &identifier_arg) :
322
322
    session(session_arg), 
323
323
    identifier(identifier_arg) 
324
324
  { }
333
333
  Utility method which hides some of the details of getTableDefinition()
334
334
*/
335
335
bool plugin::StorageEngine::doesTableExist(Session &session,
336
 
                                           const identifier::Table &identifier,
 
336
                                           const TableIdentifier &identifier,
337
337
                                           bool include_temporary_tables)
338
338
{
339
339
  if (include_temporary_tables)
343
343
  }
344
344
 
345
345
  EngineVector::iterator iter=
346
 
    std::find_if(vector_of_engines.begin(), vector_of_engines.end(),
347
 
                 StorageEngineDoesTableExist(session, identifier));
 
346
    find_if(vector_of_engines.begin(), vector_of_engines.end(),
 
347
            StorageEngineDoesTableExist(session, identifier));
348
348
 
349
349
  if (iter == vector_of_engines.end())
350
350
  {
354
354
  return true;
355
355
}
356
356
 
357
 
bool plugin::StorageEngine::doDoesTableExist(Session&, const drizzled::identifier::Table&)
 
357
bool plugin::StorageEngine::doDoesTableExist(Session&, const drizzled::TableIdentifier&)
358
358
{
359
 
  std::cerr << " Engine was called for doDoesTableExist() and does not implement it: " << this->getName() << "\n";
 
359
  cerr << " Engine was called for doDoesTableExist() and does not implement it: " << this->getName() << "\n";
360
360
  assert(0);
361
361
  return false;
362
362
}
363
363
 
364
 
message::table::shared_ptr StorageEngine::getTableMessage(Session& session,
365
 
                                                          identifier::Table::const_reference identifier,
366
 
                                                          bool include_temporary_tables)
 
364
/**
 
365
  Call this function in order to give the Cursor the possiblity
 
366
  to ask engine if there are any new tables that should be written to disk
 
367
  or any dropped tables that need to be removed from disk
 
368
*/
 
369
int StorageEngine::getTableDefinition(Session& session,
 
370
                                      const TableIdentifier &identifier,
 
371
                                      message::Table &table_message,
 
372
                                      bool include_temporary_tables)
367
373
{
368
 
  drizzled::error_t error;
369
 
  error= static_cast<drizzled::error_t>(ENOENT);
 
374
  int err= ENOENT;
370
375
 
371
376
  if (include_temporary_tables)
372
377
  {
373
 
    Table *table= session.find_temporary_table(identifier);
374
 
    if (table)
375
 
    {
376
 
      return message::table::shared_ptr(new message::Table(*table->getShare()->getTableMessage()));
377
 
    }
378
 
  }
379
 
 
380
 
  drizzled::message::table::shared_ptr table_ptr;
381
 
  if ((table_ptr= drizzled::message::Cache::singleton().find(identifier)))
382
 
  {
383
 
    (void)table_ptr;
384
 
  }
385
 
 
386
 
  message::Table message;
 
378
    if (session.doGetTableDefinition(identifier, table_message) == EEXIST)
 
379
      return EEXIST;
 
380
  }
 
381
 
387
382
  EngineVector::iterator iter=
388
 
    std::find_if(vector_of_engines.begin(), vector_of_engines.end(),
389
 
                 StorageEngineGetTableDefinition(session, identifier, message, error));
 
383
    find_if(vector_of_engines.begin(), vector_of_engines.end(),
 
384
            StorageEngineGetTableDefinition(session, identifier, table_message, err));
390
385
 
391
386
  if (iter == vector_of_engines.end())
392
387
  {
393
 
    return message::table::shared_ptr();
 
388
    return ENOENT;
394
389
  }
395
 
  message::table::shared_ptr table_message(new message::Table(message));
396
 
 
397
 
  drizzled::message::Cache::singleton().insert(identifier, table_message);
398
 
 
399
 
  return table_message;
 
390
 
 
391
  return err;
400
392
}
401
393
 
402
394
/**
409
401
{
410
402
public:
411
403
  Ha_delete_table_error_handler() : Internal_error_handler() {}
412
 
  virtual bool handle_error(drizzled::error_t sql_errno,
 
404
  virtual bool handle_error(uint32_t sql_errno,
413
405
                            const char *message,
414
406
                            DRIZZLE_ERROR::enum_warning_level level,
415
407
                            Session *session);
419
411
 
420
412
bool
421
413
Ha_delete_table_error_handler::
422
 
handle_error(drizzled::error_t ,
 
414
handle_error(uint32_t ,
423
415
             const char *message,
424
416
             DRIZZLE_ERROR::enum_warning_level ,
425
417
             Session *)
429
421
  return true;
430
422
}
431
423
 
432
 
class DropTableByIdentifier: public std::unary_function<EngineVector::value_type, bool>
433
 
{
434
 
  Session::reference session;
435
 
  identifier::Table::const_reference identifier;
436
 
  drizzled::error_t &error;
437
 
 
438
 
public:
439
 
 
440
 
  DropTableByIdentifier(Session::reference session_arg,
441
 
                        identifier::Table::const_reference identifier_arg,
442
 
                        drizzled::error_t &error_arg) :
443
 
    session(session_arg),
444
 
    identifier(identifier_arg),
445
 
    error(error_arg)
446
 
  { }
447
 
 
448
 
  result_type operator() (argument_type engine)
449
 
  {
450
 
    if (not engine->doDoesTableExist(session, identifier))
451
 
      return false;
452
 
 
453
 
    int local_error= engine->doDropTable(session, identifier);
454
 
 
455
 
 
456
 
    if (not local_error)
457
 
      return true;
458
 
 
459
 
    switch (local_error)
460
 
    {
461
 
    case HA_ERR_NO_SUCH_TABLE:
462
 
    case ENOENT:
463
 
      error= static_cast<drizzled::error_t>(HA_ERR_NO_SUCH_TABLE);
464
 
      return false;
465
 
 
466
 
    default:
467
 
      error= static_cast<drizzled::error_t>(local_error);
468
 
      return true;
469
 
    }
470
 
  } 
471
 
};
472
 
 
473
 
 
474
 
bool StorageEngine::dropTable(Session::reference session,
475
 
                              identifier::Table::const_reference identifier,
476
 
                              drizzled::error_t &error)
477
 
{
478
 
  error= EE_OK;
479
 
 
480
 
  EngineVector::const_iterator iter= std::find_if(vector_of_engines.begin(), vector_of_engines.end(),
481
 
                                                  DropTableByIdentifier(session, identifier, error));
482
 
 
483
 
  if (error)
484
 
  {
485
 
    return false;
486
 
  }
487
 
  else if (iter == vector_of_engines.end())
488
 
  {
489
 
    error= ER_BAD_TABLE_ERROR;
490
 
    return false;
491
 
  }
492
 
 
493
 
  drizzled::message::Cache::singleton().erase(identifier);
494
 
 
495
 
  return true;
496
 
}
497
 
 
498
 
bool StorageEngine::dropTable(Session& session,
499
 
                              const identifier::Table &identifier)
500
 
{
501
 
  drizzled::error_t error;
502
 
 
503
 
  if (not dropTable(session, identifier, error))
504
 
  {
505
 
    return false;
506
 
  }
507
 
 
508
 
  return true;
509
 
}
510
 
 
511
 
bool StorageEngine::dropTable(Session::reference session,
512
 
                              StorageEngine &engine,
513
 
                              identifier::Table::const_reference identifier,
514
 
                              drizzled::error_t &error)
515
 
{
516
 
  error= EE_OK;
 
424
/**
 
425
   returns ENOENT if the file doesn't exists.
 
426
*/
 
427
int StorageEngine::dropTable(Session& session,
 
428
                             const TableIdentifier &identifier)
 
429
{
 
430
  int error= 0;
 
431
  int error_proto;
 
432
  message::Table src_proto;
 
433
  StorageEngine *engine;
 
434
 
 
435
  error_proto= StorageEngine::getTableDefinition(session, identifier, src_proto);
 
436
 
 
437
  if (error_proto == ER_CORRUPT_TABLE_DEFINITION)
 
438
  {
 
439
    string error_message;
 
440
 
 
441
    error_message.append(const_cast<TableIdentifier &>(identifier).getSQLPath());
 
442
    error_message.append(" : ");
 
443
    error_message.append(src_proto.InitializationErrorString());
 
444
 
 
445
    my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0), error_message.c_str());
 
446
 
 
447
    return ER_CORRUPT_TABLE_DEFINITION;
 
448
  }
 
449
 
 
450
  engine= StorageEngine::findByName(session, src_proto.engine().name());
 
451
 
 
452
  if (not engine)
 
453
  {
 
454
    my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0), const_cast<TableIdentifier &>(identifier).getSQLPath().c_str());
 
455
 
 
456
    return ER_CORRUPT_TABLE_DEFINITION;
 
457
  }
 
458
 
 
459
  error= StorageEngine::dropTable(session, *engine, identifier);
 
460
 
 
461
  if (error_proto && error == 0)
 
462
    return 0;
 
463
 
 
464
  return error;
 
465
}
 
466
 
 
467
int StorageEngine::dropTable(Session& session,
 
468
                             StorageEngine &engine,
 
469
                             const TableIdentifier &identifier)
 
470
{
 
471
  int error;
 
472
 
517
473
  engine.setTransactionReadWrite(session);
518
 
 
519
 
  assert(identifier.isTmp());
520
474
  
521
475
  if (unlikely(plugin::EventObserver::beforeDropTable(session, identifier)))
522
476
  {
524
478
  }
525
479
  else
526
480
  {
527
 
    error= static_cast<drizzled::error_t>(engine.doDropTable(session, identifier));
528
 
 
 
481
    error= engine.doDropTable(session, identifier);
529
482
    if (unlikely(plugin::EventObserver::afterDropTable(session, identifier, error)))
530
483
    {
531
484
      error= ER_EVENT_OBSERVER_PLUGIN;
532
485
    }
533
486
  }
534
487
 
535
 
  drizzled::message::Cache::singleton().erase(identifier);
536
 
 
537
 
  if (error)
538
 
  {
539
 
    return false;
540
 
  }
541
 
 
542
 
  return true;
 
488
 
 
489
  return error;
543
490
}
544
491
 
545
492
 
551
498
  @retval
552
499
   1  error
553
500
*/
554
 
bool StorageEngine::createTable(Session &session,
555
 
                                const identifier::Table &identifier,
556
 
                                message::Table& table_message)
 
501
int StorageEngine::createTable(Session &session,
 
502
                               const TableIdentifier &identifier,
 
503
                               message::Table& table_message)
557
504
{
558
 
  drizzled::error_t error= EE_OK;
559
 
 
 
505
  int error= 1;
560
506
  TableShare share(identifier);
561
507
  table::Shell table(share);
562
508
  message::Table tmp_proto;
564
510
  if (share.parse_table_proto(session, table_message) || share.open_table_from_share(&session, identifier, "", 0, 0, table))
565
511
  { 
566
512
    // @note Error occured, we should probably do a little more here.
567
 
    // ER_CORRUPT_TABLE_DEFINITION,ER_CORRUPT_TABLE_DEFINITION_ENUM 
568
 
    
569
 
    my_error(ER_CORRUPT_TABLE_DEFINITION_UNKNOWN, identifier);
570
 
 
571
 
    return false;
572
513
  }
573
514
  else
574
515
  {
587
528
    {
588
529
      share.storage_engine->setTransactionReadWrite(session);
589
530
 
590
 
      error= static_cast<drizzled::error_t>(share.storage_engine->doCreateTable(session,
591
 
                                                                                table,
592
 
                                                                                identifier,
593
 
                                                                                table_message));
 
531
      error= share.storage_engine->doCreateTable(session,
 
532
                                                 table,
 
533
                                                 identifier,
 
534
                                                 table_message);
594
535
    }
595
536
 
596
 
    if (error == ER_TABLE_PERMISSION_DENIED)
597
 
    {
598
 
      my_error(ER_TABLE_PERMISSION_DENIED, identifier);
599
 
    }
600
 
    else if (error)
601
 
    {
602
 
      std::string path;
603
 
      identifier.getSQLPath(path);
604
 
      my_error(ER_CANT_CREATE_TABLE, MYF(ME_BELL+ME_WAITTANG), path.c_str(), error);
 
537
    if (error)
 
538
    {
 
539
      my_error(ER_CANT_CREATE_TABLE, MYF(ME_BELL+ME_WAITTANG), const_cast<TableIdentifier &>(identifier).getSQLPath().c_str(), error);
605
540
    }
606
541
 
607
542
    table.delete_table();
608
543
  }
609
544
 
610
 
  return(error == EE_OK);
 
545
  return(error != 0);
611
546
}
612
547
 
613
548
Cursor *StorageEngine::getCursor(Table &arg)
616
551
}
617
552
 
618
553
class AddTableIdentifier : 
619
 
  public std::unary_function<StorageEngine *, void>
 
554
  public unary_function<StorageEngine *, void>
620
555
{
621
556
  CachedDirectory &directory;
622
 
  const identifier::Schema &identifier;
623
 
  identifier::Table::vector &set_of_identifiers;
 
557
  const SchemaIdentifier &identifier;
 
558
  TableIdentifiers &set_of_identifiers;
624
559
 
625
560
public:
626
561
 
627
 
  AddTableIdentifier(CachedDirectory &directory_arg, const identifier::Schema &identifier_arg, identifier::Table::vector &of_names) :
 
562
  AddTableIdentifier(CachedDirectory &directory_arg, const SchemaIdentifier &identifier_arg, TableIdentifiers &of_names) :
628
563
    directory(directory_arg),
629
564
    identifier(identifier_arg),
630
565
    set_of_identifiers(of_names)
638
573
};
639
574
 
640
575
 
641
 
void StorageEngine::getIdentifiers(Session &session, const identifier::Schema &schema_identifier, identifier::Table::vector &set_of_identifiers)
 
576
void StorageEngine::getIdentifiers(Session &session, const SchemaIdentifier &schema_identifier, TableIdentifiers &set_of_identifiers)
642
577
{
 
578
  static SchemaIdentifier INFORMATION_SCHEMA_IDENTIFIER("information_schema");
 
579
  static SchemaIdentifier DATA_DICTIONARY_IDENTIFIER("data_dictionary");
 
580
 
643
581
  CachedDirectory directory(schema_identifier.getPath(), set_of_table_definition_ext);
644
582
 
645
583
  if (schema_identifier == INFORMATION_SCHEMA_IDENTIFIER)
652
590
    {
653
591
      errno= directory.getError();
654
592
      if (errno == ENOENT)
655
 
      {
656
 
        std::string path;
657
 
        schema_identifier.getSQLPath(path);
658
 
        my_error(ER_BAD_DB_ERROR, MYF(ME_BELL+ME_WAITTANG), path.c_str());
659
 
      }
 
593
        my_error(ER_BAD_DB_ERROR, MYF(ME_BELL+ME_WAITTANG), const_cast<SchemaIdentifier &>(schema_identifier).getSQLPath().c_str());
660
594
      else
661
 
      {
662
595
        my_error(ER_CANT_READ_DIR, MYF(ME_BELL+ME_WAITTANG), directory.getPath(), errno);
663
 
      }
664
 
 
665
596
      return;
666
597
    }
667
598
  }
668
599
 
669
 
  std::for_each(vector_of_engines.begin(), vector_of_engines.end(),
670
 
                AddTableIdentifier(directory, schema_identifier, set_of_identifiers));
 
600
  for_each(vector_of_engines.begin(), vector_of_engines.end(),
 
601
           AddTableIdentifier(directory, schema_identifier, set_of_identifiers));
671
602
 
672
603
  session.doGetTableIdentifiers(directory, schema_identifier, set_of_identifiers);
673
604
}
674
605
 
675
 
class DropTable: public std::unary_function<identifier::Table&, bool>
 
606
class DropTable: public unary_function<TableIdentifier&, bool>
676
607
{
677
608
  Session &session;
678
609
  StorageEngine *engine;
690
621
  } 
691
622
};
692
623
 
693
 
/* This will later be converted to identifier::Tables */
694
 
class DropTables: public std::unary_function<StorageEngine *, void>
 
624
/* This will later be converted to TableIdentifiers */
 
625
class DropTables: public unary_function<StorageEngine *, void>
695
626
{
696
627
  Session &session;
697
 
  identifier::Table::vector &table_identifiers;
 
628
  TableIdentifiers &table_identifiers;
698
629
 
699
630
public:
700
631
 
701
 
  DropTables(Session &session_arg, identifier::Table::vector &table_identifiers_arg) :
 
632
  DropTables(Session &session_arg, TableIdentifiers &table_identifiers_arg) :
702
633
    session(session_arg),
703
634
    table_identifiers(table_identifiers_arg)
704
635
  { }
707
638
  {
708
639
    // True returning from DropTable means the table has been successfully
709
640
    // deleted, so it should be removed from the list of tables to drop
710
 
    table_identifiers.erase(std::remove_if(table_identifiers.begin(),
711
 
                                           table_identifiers.end(),
712
 
                                           DropTable(session, engine)),
 
641
    table_identifiers.erase(remove_if(table_identifiers.begin(),
 
642
                                      table_identifiers.end(),
 
643
                                      DropTable(session, engine)),
713
644
                            table_identifiers.end());
714
645
  }
715
646
};
722
653
void StorageEngine::removeLostTemporaryTables(Session &session, const char *directory)
723
654
{
724
655
  CachedDirectory dir(directory, set_of_table_definition_ext);
725
 
  identifier::Table::vector table_identifiers;
 
656
  TableIdentifiers table_identifiers;
726
657
 
727
658
  if (dir.fail())
728
659
  {
738
669
       fileIter != files.end(); fileIter++)
739
670
  {
740
671
    size_t length;
741
 
    std::string path;
 
672
    string path;
742
673
    CachedDirectory::Entry *entry= *fileIter;
743
674
 
744
675
    /* We remove the file extension. */
751
682
    message::Table definition;
752
683
    if (StorageEngine::readTableFile(path, definition))
753
684
    {
754
 
      identifier::Table identifier(definition.schema(), definition.name(), path);
 
685
      TableIdentifier identifier(definition.schema(), definition.name(), path);
755
686
      table_identifiers.push_back(identifier);
756
687
    }
757
688
  }
758
689
 
759
 
  std::for_each(vector_of_engines.begin(), vector_of_engines.end(),
760
 
                DropTables(session, table_identifiers));
761
 
 
 
690
  for_each(vector_of_engines.begin(), vector_of_engines.end(),
 
691
           DropTables(session, table_identifiers));
 
692
  
762
693
  /*
763
694
    Now we just clean up anything that might left over.
764
695
 
765
696
    We rescan because some of what might have been there should
766
697
    now be all nice and cleaned up.
767
698
  */
768
 
  std::set<std::string> all_exts= set_of_table_definition_ext;
 
699
  set<string> all_exts= set_of_table_definition_ext;
769
700
 
770
701
  for (EngineVector::iterator iter= vector_of_engines.begin();
771
702
       iter != vector_of_engines.end() ; iter++)
780
711
  for (CachedDirectory::Entries::iterator fileIter= files.begin();
781
712
       fileIter != files.end(); fileIter++)
782
713
  {
783
 
    std::string path;
 
714
    string path;
784
715
    CachedDirectory::Entry *entry= *fileIter;
785
716
 
786
717
    path+= directory;
801
732
    - table->getShare()->path
802
733
    - table->alias
803
734
*/
804
 
void StorageEngine::print_error(int error, myf errflag, const Table &table) const
805
 
{
806
 
  drizzled::error_t textno= ER_GET_ERRNO;
 
735
void StorageEngine::print_error(int error, myf errflag, Table &table)
 
736
{
 
737
  print_error(error, errflag, &table);
 
738
}
 
739
 
 
740
void StorageEngine::print_error(int error, myf errflag, Table *table)
 
741
{
 
742
  int textno= ER_GET_ERRNO;
807
743
  switch (error) {
808
744
  case EACCES:
809
745
    textno=ER_OPEN_AS_READONLY;
824
760
    break;
825
761
  case HA_ERR_FOUND_DUPP_KEY:
826
762
  {
827
 
    uint32_t key_nr= table.get_dup_key(error);
 
763
    assert(table);
 
764
    uint32_t key_nr= table->get_dup_key(error);
828
765
    if ((int) key_nr >= 0)
829
766
    {
830
767
      const char *err_msg= ER(ER_DUP_ENTRY_WITH_KEY_NAME);
831
768
 
832
 
      print_keydup_error(key_nr, err_msg, table);
 
769
      print_keydup_error(key_nr, err_msg, *table);
833
770
 
834
771
      return;
835
772
    }
838
775
  }
839
776
  case HA_ERR_FOREIGN_DUPLICATE_KEY:
840
777
  {
841
 
    uint32_t key_nr= table.get_dup_key(error);
 
778
    assert(table);
 
779
    uint32_t key_nr= table->get_dup_key(error);
842
780
    if ((int) key_nr >= 0)
843
781
    {
844
782
      uint32_t max_length;
848
786
      String str(key,sizeof(key),system_charset_info);
849
787
 
850
788
      /* Table is opened and defined at this point */
851
 
      key_unpack(&str, &table,(uint32_t) key_nr);
 
789
      key_unpack(&str,table,(uint32_t) key_nr);
852
790
      max_length= (DRIZZLE_ERRMSG_SIZE-
853
791
                   (uint32_t) strlen(ER(ER_FOREIGN_DUPLICATE_KEY)));
854
792
      if (str.length() >= max_length)
856
794
        str.length(max_length-4);
857
795
        str.append(STRING_WITH_LEN("..."));
858
796
      }
859
 
      my_error(ER_FOREIGN_DUPLICATE_KEY, MYF(0), table.getShare()->getTableName(),
 
797
      my_error(ER_FOREIGN_DUPLICATE_KEY, MYF(0), table->getShare()->getTableName(),
860
798
        str.c_ptr(), key_nr+1);
861
799
      return;
862
800
    }
879
817
    textno=ER_CRASHED_ON_USAGE;
880
818
    break;
881
819
  case HA_ERR_NOT_A_TABLE:
882
 
    textno= static_cast<drizzled::error_t>(error);
 
820
    textno= error;
883
821
    break;
884
822
  case HA_ERR_CRASHED_ON_REPAIR:
885
823
    textno=ER_CRASHED_ON_REPAIR;
933
871
    textno=ER_TABLE_DEF_CHANGED;
934
872
    break;
935
873
  case HA_ERR_NO_SUCH_TABLE:
936
 
    {
937
 
      identifier::Table identifier(table.getShare()->getSchemaName(), table.getShare()->getTableName());
938
 
      my_error(ER_TABLE_UNKNOWN, identifier);
939
 
      return;
940
 
    }
 
874
    assert(table);
 
875
    my_error(ER_NO_SUCH_TABLE, MYF(0), table->getShare()->getSchemaName(),
 
876
             table->getShare()->getTableName());
 
877
    return;
941
878
  case HA_ERR_RBR_LOGGING_FAILED:
942
879
    textno= ER_BINLOG_ROW_LOGGING_FAILED;
943
880
    break;
944
881
  case HA_ERR_DROP_INDEX_FK:
945
882
  {
 
883
    assert(table);
946
884
    const char *ptr= "???";
947
 
    uint32_t key_nr= table.get_dup_key(error);
 
885
    uint32_t key_nr= table->get_dup_key(error);
948
886
    if ((int) key_nr >= 0)
949
 
      ptr= table.key_info[key_nr].name;
 
887
      ptr= table->key_info[key_nr].name;
950
888
    my_error(ER_DROP_INDEX_FK, MYF(0), ptr);
951
889
    return;
952
890
  }
991
929
      return;
992
930
    }
993
931
  }
994
 
 
995
 
  my_error(textno, errflag, table.getShare()->getTableName(), error);
 
932
  my_error(textno, errflag, table->getShare()->getTableName(), error);
996
933
}
997
934
 
998
935
 
1005
942
  @return
1006
943
    Returns true if this is a temporary error
1007
944
*/
1008
 
bool StorageEngine::get_error_message(int , String* ) const
 
945
bool StorageEngine::get_error_message(int , String* )
1009
946
{
1010
947
  return false;
1011
948
}
1012
949
 
1013
950
 
1014
 
void StorageEngine::print_keydup_error(uint32_t key_nr, const char *msg, const Table &table) const
 
951
void StorageEngine::print_keydup_error(uint32_t key_nr, const char *msg, Table &table)
1015
952
{
1016
953
  /* Write the duplicated key in the error message */
1017
954
  char key[MAX_KEY_LENGTH];
1039
976
}
1040
977
 
1041
978
 
1042
 
int StorageEngine::deleteDefinitionFromPath(const identifier::Table &identifier)
 
979
int StorageEngine::deleteDefinitionFromPath(const TableIdentifier &identifier)
1043
980
{
1044
 
  std::string path(identifier.getPath());
 
981
  string path(identifier.getPath());
1045
982
 
1046
983
  path.append(DEFAULT_DEFINITION_FILE_EXT);
1047
984
 
1048
985
  return internal::my_delete(path.c_str(), MYF(0));
1049
986
}
1050
987
 
1051
 
int StorageEngine::renameDefinitionFromPath(const identifier::Table &dest, const identifier::Table &src)
 
988
int StorageEngine::renameDefinitionFromPath(const TableIdentifier &dest, const TableIdentifier &src)
1052
989
{
1053
990
  message::Table table_message;
1054
 
  std::string src_path(src.getPath());
1055
 
  std::string dest_path(dest.getPath());
 
991
  string src_path(src.getPath());
 
992
  string dest_path(dest.getPath());
1056
993
 
1057
994
  src_path.append(DEFAULT_DEFINITION_FILE_EXT);
1058
995
  dest_path.append(DEFAULT_DEFINITION_FILE_EXT);
1077
1014
  return error;
1078
1015
}
1079
1016
 
1080
 
int StorageEngine::writeDefinitionFromPath(const identifier::Table &identifier, message::Table &table_message)
 
1017
int StorageEngine::writeDefinitionFromPath(const TableIdentifier &identifier, message::Table &table_message)
1081
1018
{
1082
1019
  char definition_file_tmp[FN_REFLEN];
1083
 
  std::string file_name(identifier.getPath());
 
1020
  string file_name(identifier.getPath());
1084
1021
 
1085
1022
  file_name.append(DEFAULT_DEFINITION_FILE_EXT);
1086
1023
 
1099
1036
 
1100
1037
  bool success;
1101
1038
 
1102
 
  try
1103
 
  {
 
1039
  try {
1104
1040
    success= table_message.SerializeToZeroCopyStream(output);
1105
1041
  }
1106
1042
  catch (...)
1110
1046
 
1111
1047
  if (not success)
1112
1048
  {
1113
 
    std::string error_message;
1114
 
    identifier.getSQLPath(error_message);
1115
 
 
1116
1049
    my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0),
1117
 
             error_message.c_str(),
1118
1050
             table_message.InitializationErrorString().c_str());
1119
1051
    delete output;
1120
1052
 
1154
1086
  return 0;
1155
1087
}
1156
1088
 
1157
 
class CanCreateTable: public std::unary_function<StorageEngine *, bool>
 
1089
class CanCreateTable: public unary_function<StorageEngine *, bool>
1158
1090
{
1159
 
  const identifier::Table &identifier;
 
1091
  const TableIdentifier &identifier;
1160
1092
 
1161
1093
public:
1162
 
  CanCreateTable(const identifier::Table &identifier_arg) :
 
1094
  CanCreateTable(const TableIdentifier &identifier_arg) :
1163
1095
    identifier(identifier_arg)
1164
1096
  { }
1165
1097
 
1173
1105
/**
1174
1106
  @note on success table can be created.
1175
1107
*/
1176
 
bool StorageEngine::canCreateTable(const identifier::Table &identifier)
 
1108
bool StorageEngine::canCreateTable(const TableIdentifier &identifier)
1177
1109
{
1178
1110
  EngineVector::iterator iter=
1179
 
    std::find_if(vector_of_engines.begin(), vector_of_engines.end(),
1180
 
                 CanCreateTable(identifier));
 
1111
    find_if(vector_of_engines.begin(), vector_of_engines.end(),
 
1112
            CanCreateTable(identifier));
1181
1113
 
1182
1114
  if (iter == vector_of_engines.end())
1183
1115
  {
1189
1121
 
1190
1122
bool StorageEngine::readTableFile(const std::string &path, message::Table &table_message)
1191
1123
{
1192
 
  std::fstream input(path.c_str(), std::ios::in | std::ios::binary);
 
1124
  fstream input(path.c_str(), ios::in | ios::binary);
1193
1125
 
1194
1126
  if (input.good())
1195
1127
  {
1202
1134
    catch (...)
1203
1135
    {
1204
1136
      my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0),
1205
 
               table_message.name().empty() ? path.c_str() : table_message.name().c_str(),
1206
1137
               table_message.InitializationErrorString().empty() ? "": table_message.InitializationErrorString().c_str());
1207
1138
    }
1208
1139
  }
1214
1145
  return false;
1215
1146
}
1216
1147
 
1217
 
std::ostream& operator<<(std::ostream& output, const StorageEngine &engine)
1218
 
{
1219
 
  output << "StorageEngine:(";
1220
 
  output <<  engine.getName();
1221
 
  output << ")";
1222
1148
 
1223
 
  return output;
1224
 
}
1225
1149
 
1226
1150
} /* namespace plugin */
1227
1151
} /* namespace drizzled */