~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/plugin/storage_engine.cc

  • Committer: Monty Taylor
  • Date: 2011-02-13 17:26:39 UTC
  • mfrom: (2157.2.2 give-in-to-pkg-config)
  • mto: This revision was merged to the branch mainline in revision 2166.
  • Revision ID: mordred@inaugust.com-20110213172639-nhy7i72sfhoq13ms
Merged in pkg-config fixes.

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
 
4
 *  Copyright (C) 2008 Sun Microsystems, Inc.
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
54
54
#include <drizzled/table_proto.h>
55
55
#include <drizzled/plugin/event_observer.h>
56
56
 
 
57
#include <drizzled/table/shell.h>
 
58
 
 
59
#include "drizzled/message/cache.h"
 
60
 
57
61
#include <boost/algorithm/string/compare.hpp>
58
62
 
59
63
static bool shutdown_has_begun= false; // Once we put in the container for the vector/etc for engines this will go away.
60
64
 
61
 
using namespace std;
62
 
 
63
65
namespace drizzled
64
66
{
65
67
 
80
82
  return vector_of_schema_engines;
81
83
}
82
84
 
83
 
StorageEngine::StorageEngine(const string name_arg,
84
 
                             const bitset<HTON_BIT_SIZE> &flags_arg) :
 
85
StorageEngine::StorageEngine(const std::string name_arg,
 
86
                             const std::bitset<HTON_BIT_SIZE> &flags_arg) :
85
87
  Plugin(name_arg, "StorageEngine"),
86
88
  MonitoredInTransaction(), /* This gives the storage engine a "slot" or ID */
87
89
  flags(flags_arg)
99
101
}
100
102
 
101
103
 
102
 
int StorageEngine::renameTable(Session &session, const TableIdentifier &from, const TableIdentifier &to)
 
104
int StorageEngine::renameTable(Session &session, const identifier::Table &from, const identifier::Table &to)
103
105
{
104
106
  int error;
105
107
  setTransactionReadWrite(session);
135
137
  @retval
136
138
    !0  Error
137
139
*/
138
 
int StorageEngine::doDropTable(Session&, const TableIdentifier &identifier)
 
140
int StorageEngine::doDropTable(Session&, const identifier::Table &identifier)
139
141
                               
140
142
{
141
143
  int error= 0;
190
192
}
191
193
 
192
194
class FindEngineByName
193
 
  : public unary_function<StorageEngine *, bool>
 
195
  : public std::unary_function<StorageEngine *, bool>
194
196
{
195
 
  const string &predicate;
 
197
  const std::string &predicate;
196
198
 
197
199
public:
198
 
  explicit FindEngineByName(const string &target_arg) :
 
200
  explicit FindEngineByName(const std::string &target_arg) :
199
201
    predicate(target_arg)
200
202
  {
201
203
  }
206
208
  }
207
209
};
208
210
 
209
 
StorageEngine *StorageEngine::findByName(const string &predicate)
 
211
StorageEngine *StorageEngine::findByName(const std::string &predicate)
210
212
{
211
 
  EngineVector::iterator iter= find_if(vector_of_engines.begin(),
212
 
                                       vector_of_engines.end(),
213
 
                                       FindEngineByName(predicate));
 
213
  EngineVector::iterator iter= std::find_if(vector_of_engines.begin(),
 
214
                                            vector_of_engines.end(),
 
215
                                            FindEngineByName(predicate));
214
216
  if (iter != vector_of_engines.end())
215
217
  {
216
218
    StorageEngine *engine= *iter;
221
223
  return NULL;
222
224
}
223
225
 
224
 
StorageEngine *StorageEngine::findByName(Session& session, const string &predicate)
 
226
StorageEngine *StorageEngine::findByName(Session& session, const std::string &predicate)
225
227
{
226
228
  if (boost::iequals(predicate, DEFAULT_STRING))
227
229
    return session.getDefaultStorageEngine();
228
230
 
229
 
  EngineVector::iterator iter= find_if(vector_of_engines.begin(),
230
 
                                       vector_of_engines.end(),
231
 
                                       FindEngineByName(predicate));
 
231
  EngineVector::iterator iter= std::find_if(vector_of_engines.begin(),
 
232
                                            vector_of_engines.end(),
 
233
                                            FindEngineByName(predicate));
232
234
  if (iter != vector_of_engines.end())
233
235
  {
234
236
    StorageEngine *engine= *iter;
239
241
  return NULL;
240
242
}
241
243
 
242
 
class StorageEngineCloseConnection : public unary_function<StorageEngine *, void>
 
244
class StorageEngineCloseConnection : public std::unary_function<StorageEngine *, void>
243
245
{
244
246
  Session *session;
245
247
public:
261
263
*/
262
264
void StorageEngine::closeConnection(Session* session)
263
265
{
264
 
  for_each(vector_of_engines.begin(), vector_of_engines.end(),
265
 
           StorageEngineCloseConnection(session));
 
266
  std::for_each(vector_of_engines.begin(), vector_of_engines.end(),
 
267
                StorageEngineCloseConnection(session));
266
268
}
267
269
 
268
270
bool StorageEngine::flushLogs(StorageEngine *engine)
269
271
{
270
272
  if (engine == NULL)
271
273
  {
272
 
    if (find_if(vector_of_engines.begin(), vector_of_engines.end(),
273
 
                mem_fun(&StorageEngine::flush_logs))
 
274
    if (std::find_if(vector_of_engines.begin(), vector_of_engines.end(),
 
275
                     std::mem_fun(&StorageEngine::flush_logs))
274
276
        != vector_of_engines.begin())
275
277
      return true;
276
278
  }
282
284
  return false;
283
285
}
284
286
 
285
 
class StorageEngineGetTableDefinition: public unary_function<StorageEngine *,bool>
 
287
class StorageEngineGetTableDefinition: public std::unary_function<StorageEngine *,bool>
286
288
{
287
289
  Session& session;
288
 
  const TableIdentifier &identifier;
 
290
  const identifier::Table &identifier;
289
291
  message::Table &table_message;
290
 
  int &err;
 
292
  drizzled::error_t &err;
291
293
 
292
294
public:
293
295
  StorageEngineGetTableDefinition(Session& session_arg,
294
 
                                  const TableIdentifier &identifier_arg,
 
296
                                  const identifier::Table &identifier_arg,
295
297
                                  message::Table &table_message_arg,
296
 
                                  int &err_arg) :
 
298
                                  drizzled::error_t &err_arg) :
297
299
    session(session_arg), 
298
300
    identifier(identifier_arg),
299
301
    table_message(table_message_arg), 
304
306
    int ret= engine->doGetTableDefinition(session, identifier, table_message);
305
307
 
306
308
    if (ret != ENOENT)
307
 
      err= ret;
 
309
      err= static_cast<drizzled::error_t>(ret);
308
310
 
309
 
    return err == EEXIST || err != ENOENT;
 
311
    return err == static_cast<drizzled::error_t>(EEXIST) or err != static_cast<drizzled::error_t>(ENOENT);
310
312
  }
311
313
};
312
314
 
313
 
class StorageEngineDoesTableExist: public unary_function<StorageEngine *, bool>
 
315
class StorageEngineDoesTableExist: public std::unary_function<StorageEngine *, bool>
314
316
{
315
317
  Session& session;
316
 
  const TableIdentifier &identifier;
 
318
  const identifier::Table &identifier;
317
319
 
318
320
public:
319
 
  StorageEngineDoesTableExist(Session& session_arg, const TableIdentifier &identifier_arg) :
 
321
  StorageEngineDoesTableExist(Session& session_arg, const identifier::Table &identifier_arg) :
320
322
    session(session_arg), 
321
323
    identifier(identifier_arg) 
322
324
  { }
331
333
  Utility method which hides some of the details of getTableDefinition()
332
334
*/
333
335
bool plugin::StorageEngine::doesTableExist(Session &session,
334
 
                                           const TableIdentifier &identifier,
 
336
                                           const identifier::Table &identifier,
335
337
                                           bool include_temporary_tables)
336
338
{
337
339
  if (include_temporary_tables)
341
343
  }
342
344
 
343
345
  EngineVector::iterator iter=
344
 
    find_if(vector_of_engines.begin(), vector_of_engines.end(),
345
 
            StorageEngineDoesTableExist(session, identifier));
 
346
    std::find_if(vector_of_engines.begin(), vector_of_engines.end(),
 
347
                 StorageEngineDoesTableExist(session, identifier));
346
348
 
347
349
  if (iter == vector_of_engines.end())
348
350
  {
352
354
  return true;
353
355
}
354
356
 
355
 
bool plugin::StorageEngine::doDoesTableExist(Session&, const drizzled::TableIdentifier&)
 
357
bool plugin::StorageEngine::doDoesTableExist(Session&, const drizzled::identifier::Table&)
356
358
{
357
 
  cerr << " Engine was called for doDoesTableExist() and does not implement it: " << this->getName() << "\n";
 
359
  std::cerr << " Engine was called for doDoesTableExist() and does not implement it: " << this->getName() << "\n";
358
360
  assert(0);
359
361
  return false;
360
362
}
365
367
  or any dropped tables that need to be removed from disk
366
368
*/
367
369
int StorageEngine::getTableDefinition(Session& session,
368
 
                                      const TableIdentifier &identifier,
369
 
                                      message::Table &table_message,
 
370
                                      const identifier::Table &identifier,
 
371
                                      message::table::shared_ptr &table_message,
370
372
                                      bool include_temporary_tables)
371
373
{
372
 
  int err= ENOENT;
 
374
  drizzled::error_t err= static_cast<drizzled::error_t>(ENOENT);
373
375
 
374
376
  if (include_temporary_tables)
375
377
  {
376
 
    if (session.doGetTableDefinition(identifier, table_message) == EEXIST)
 
378
    Table *table= session.find_temporary_table(identifier);
 
379
    if (table)
 
380
    {
 
381
      table_message.reset(new message::Table(*table->getShare()->getTableMessage()));
377
382
      return EEXIST;
378
 
  }
379
 
 
 
383
    }
 
384
  }
 
385
 
 
386
  drizzled::message::table::shared_ptr table_ptr;
 
387
  if ((table_ptr= drizzled::message::Cache::singleton().find(identifier)))
 
388
  {
 
389
    table_message= table_ptr;
 
390
  }
 
391
 
 
392
  message::Table message;
380
393
  EngineVector::iterator iter=
381
 
    find_if(vector_of_engines.begin(), vector_of_engines.end(),
382
 
            StorageEngineGetTableDefinition(session, identifier, table_message, err));
 
394
    std::find_if(vector_of_engines.begin(), vector_of_engines.end(),
 
395
                 StorageEngineGetTableDefinition(session, identifier, message, err));
383
396
 
384
397
  if (iter == vector_of_engines.end())
385
398
  {
386
399
    return ENOENT;
387
400
  }
 
401
  table_message.reset(new message::Table(message));
 
402
 
 
403
 drizzled::message::Cache::singleton().insert(identifier, table_message);
388
404
 
389
405
  return err;
390
406
}
391
407
 
 
408
message::table::shared_ptr StorageEngine::getTableMessage(Session& session,
 
409
                                                          identifier::Table::const_reference identifier,
 
410
                                                          drizzled::error_t &error,
 
411
                                                          bool include_temporary_tables)
 
412
{
 
413
  error= static_cast<drizzled::error_t>(ENOENT);
 
414
 
 
415
  if (include_temporary_tables)
 
416
  {
 
417
    Table *table= session.find_temporary_table(identifier);
 
418
    if (table)
 
419
    {
 
420
      error= EE_OK;
 
421
      return message::table::shared_ptr(new message::Table(*table->getShare()->getTableMessage()));
 
422
    }
 
423
  }
 
424
 
 
425
  drizzled::message::table::shared_ptr table_ptr;
 
426
  if ((table_ptr= drizzled::message::Cache::singleton().find(identifier)))
 
427
  {
 
428
    (void)table_ptr;
 
429
  }
 
430
 
 
431
  message::Table message;
 
432
  EngineVector::iterator iter=
 
433
    std::find_if(vector_of_engines.begin(), vector_of_engines.end(),
 
434
                 StorageEngineGetTableDefinition(session, identifier, message, error));
 
435
 
 
436
  if (iter == vector_of_engines.end())
 
437
  {
 
438
    error= static_cast<drizzled::error_t>(ENOENT);
 
439
    return message::table::shared_ptr();
 
440
  }
 
441
  message::table::shared_ptr table_message(new message::Table(message));
 
442
 
 
443
  drizzled::message::Cache::singleton().insert(identifier, table_message);
 
444
 
 
445
  return table_message;
 
446
}
 
447
 
392
448
/**
393
449
  An interceptor to hijack the text of the error message without
394
450
  setting an error in the thread. We need the text to present it
399
455
{
400
456
public:
401
457
  Ha_delete_table_error_handler() : Internal_error_handler() {}
402
 
  virtual bool handle_error(uint32_t sql_errno,
 
458
  virtual bool handle_error(drizzled::error_t sql_errno,
403
459
                            const char *message,
404
460
                            DRIZZLE_ERROR::enum_warning_level level,
405
461
                            Session *session);
409
465
 
410
466
bool
411
467
Ha_delete_table_error_handler::
412
 
handle_error(uint32_t ,
 
468
handle_error(drizzled::error_t ,
413
469
             const char *message,
414
470
             DRIZZLE_ERROR::enum_warning_level ,
415
471
             Session *)
419
475
  return true;
420
476
}
421
477
 
422
 
/**
423
 
   returns ENOENT if the file doesn't exists.
424
 
*/
425
 
int StorageEngine::dropTable(Session& session,
426
 
                             const TableIdentifier &identifier)
427
 
{
428
 
  int error= 0;
429
 
  int error_proto;
430
 
  message::Table src_proto;
431
 
  StorageEngine *engine;
432
 
 
433
 
  error_proto= StorageEngine::getTableDefinition(session, identifier, src_proto);
434
 
 
435
 
  if (error_proto == ER_CORRUPT_TABLE_DEFINITION)
436
 
  {
437
 
    string error_message;
438
 
 
439
 
    error_message.append(const_cast<TableIdentifier &>(identifier).getSQLPath());
440
 
    error_message.append(" : ");
441
 
    error_message.append(src_proto.InitializationErrorString());
442
 
 
443
 
    my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0), error_message.c_str());
444
 
 
445
 
    return ER_CORRUPT_TABLE_DEFINITION;
446
 
  }
447
 
 
448
 
  engine= StorageEngine::findByName(session, src_proto.engine().name());
449
 
 
450
 
  if (not engine)
451
 
  {
452
 
    my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0), const_cast<TableIdentifier &>(identifier).getSQLPath().c_str());
453
 
 
454
 
    return ER_CORRUPT_TABLE_DEFINITION;
455
 
  }
456
 
 
457
 
  error= StorageEngine::dropTable(session, *engine, identifier);
458
 
 
459
 
  if (error_proto && error == 0)
460
 
    return 0;
461
 
 
462
 
  return error;
463
 
}
464
 
 
465
 
int StorageEngine::dropTable(Session& session,
466
 
                             StorageEngine &engine,
467
 
                             const TableIdentifier &identifier)
468
 
{
469
 
  int error;
470
 
 
 
478
class DropTableByIdentifier: public std::unary_function<EngineVector::value_type, bool>
 
479
{
 
480
  Session::reference session;
 
481
  identifier::Table::const_reference identifier;
 
482
  drizzled::error_t &error;
 
483
 
 
484
public:
 
485
 
 
486
  DropTableByIdentifier(Session::reference session_arg,
 
487
                        identifier::Table::const_reference identifier_arg,
 
488
                        drizzled::error_t &error_arg) :
 
489
    session(session_arg),
 
490
    identifier(identifier_arg),
 
491
    error(error_arg)
 
492
  { }
 
493
 
 
494
  result_type operator() (argument_type engine)
 
495
  {
 
496
    if (not engine->doDoesTableExist(session, identifier))
 
497
      return false;
 
498
 
 
499
    int local_error= engine->doDropTable(session, identifier);
 
500
 
 
501
 
 
502
    if (not local_error)
 
503
      return true;
 
504
 
 
505
    switch (local_error)
 
506
    {
 
507
    case HA_ERR_NO_SUCH_TABLE:
 
508
    case ENOENT:
 
509
      error= static_cast<drizzled::error_t>(HA_ERR_NO_SUCH_TABLE);
 
510
      return false;
 
511
 
 
512
    default:
 
513
      error= static_cast<drizzled::error_t>(local_error);
 
514
      return true;
 
515
    }
 
516
  } 
 
517
};
 
518
 
 
519
 
 
520
bool StorageEngine::dropTable(Session::reference session,
 
521
                              identifier::Table::const_reference identifier,
 
522
                              drizzled::error_t &error)
 
523
{
 
524
  error= EE_OK;
 
525
 
 
526
  EngineVector::const_iterator iter= std::find_if(vector_of_engines.begin(), vector_of_engines.end(),
 
527
                                                  DropTableByIdentifier(session, identifier, error));
 
528
 
 
529
  if (error)
 
530
  {
 
531
    return false;
 
532
  }
 
533
  else if (iter == vector_of_engines.end())
 
534
  {
 
535
    error= ER_BAD_TABLE_ERROR;
 
536
    return false;
 
537
  }
 
538
 
 
539
  drizzled::message::Cache::singleton().erase(identifier);
 
540
 
 
541
  return true;
 
542
}
 
543
 
 
544
bool StorageEngine::dropTable(Session& session,
 
545
                              const identifier::Table &identifier)
 
546
{
 
547
  drizzled::error_t error;
 
548
 
 
549
  if (not dropTable(session, identifier, error))
 
550
  {
 
551
    return false;
 
552
  }
 
553
 
 
554
  return true;
 
555
}
 
556
 
 
557
bool StorageEngine::dropTable(Session::reference session,
 
558
                              StorageEngine &engine,
 
559
                              identifier::Table::const_reference identifier,
 
560
                              drizzled::error_t &error)
 
561
{
 
562
  error= EE_OK;
471
563
  engine.setTransactionReadWrite(session);
 
564
 
 
565
  assert(identifier.isTmp());
472
566
  
473
567
  if (unlikely(plugin::EventObserver::beforeDropTable(session, identifier)))
474
568
  {
476
570
  }
477
571
  else
478
572
  {
479
 
    error= engine.doDropTable(session, identifier);
 
573
    error= static_cast<drizzled::error_t>(engine.doDropTable(session, identifier));
 
574
 
480
575
    if (unlikely(plugin::EventObserver::afterDropTable(session, identifier, error)))
481
576
    {
482
577
      error= ER_EVENT_OBSERVER_PLUGIN;
483
578
    }
484
579
  }
485
580
 
486
 
 
487
 
  return error;
 
581
  drizzled::message::Cache::singleton().erase(identifier);
 
582
 
 
583
  if (error)
 
584
  {
 
585
    return false;
 
586
  }
 
587
 
 
588
  return true;
488
589
}
489
590
 
490
591
 
496
597
  @retval
497
598
   1  error
498
599
*/
499
 
int StorageEngine::createTable(Session &session,
500
 
                               const TableIdentifier &identifier,
501
 
                               message::Table& table_message)
 
600
bool StorageEngine::createTable(Session &session,
 
601
                                const identifier::Table &identifier,
 
602
                                message::Table& table_message)
502
603
{
503
 
  int error= 1;
504
 
  Table table;
 
604
  drizzled::error_t error= EE_OK;
 
605
 
505
606
  TableShare share(identifier);
 
607
  table::Shell table(share);
506
608
  message::Table tmp_proto;
507
609
 
508
610
  if (share.parse_table_proto(session, table_message) || share.open_table_from_share(&session, identifier, "", 0, 0, table))
509
611
  { 
510
612
    // @note Error occured, we should probably do a little more here.
 
613
    // ER_CORRUPT_TABLE_DEFINITION,ER_CORRUPT_TABLE_DEFINITION_ENUM 
 
614
    
 
615
    my_error(ER_CORRUPT_TABLE_DEFINITION_UNKNOWN, identifier);
 
616
 
 
617
    return false;
511
618
  }
512
619
  else
513
620
  {
526
633
    {
527
634
      share.storage_engine->setTransactionReadWrite(session);
528
635
 
529
 
      error= share.storage_engine->doCreateTable(session,
530
 
                                                 table,
531
 
                                                 identifier,
532
 
                                                 table_message);
 
636
      error= static_cast<drizzled::error_t>(share.storage_engine->doCreateTable(session,
 
637
                                                                                table,
 
638
                                                                                identifier,
 
639
                                                                                table_message));
533
640
    }
534
641
 
535
 
    if (error)
536
 
    {
537
 
      my_error(ER_CANT_CREATE_TABLE, MYF(ME_BELL+ME_WAITTANG), const_cast<TableIdentifier &>(identifier).getSQLPath().c_str(), error);
 
642
    if (error == ER_TABLE_PERMISSION_DENIED)
 
643
    {
 
644
      my_error(ER_TABLE_PERMISSION_DENIED, identifier);
 
645
    }
 
646
    else if (error)
 
647
    {
 
648
      std::string path;
 
649
      identifier.getSQLPath(path);
 
650
      my_error(ER_CANT_CREATE_TABLE, MYF(ME_BELL+ME_WAITTANG), path.c_str(), error);
538
651
    }
539
652
 
540
653
    table.delete_table();
541
654
  }
542
655
 
543
 
  return(error != 0);
 
656
  return(error == EE_OK);
544
657
}
545
658
 
546
 
Cursor *StorageEngine::getCursor(TableShare &share)
 
659
Cursor *StorageEngine::getCursor(Table &arg)
547
660
{
548
 
  return create(share);
 
661
  return create(arg);
549
662
}
550
663
 
551
664
class AddTableIdentifier : 
552
 
  public unary_function<StorageEngine *, void>
 
665
  public std::unary_function<StorageEngine *, void>
553
666
{
554
667
  CachedDirectory &directory;
555
 
  const SchemaIdentifier &identifier;
556
 
  TableIdentifiers &set_of_identifiers;
 
668
  const identifier::Schema &identifier;
 
669
  identifier::Table::vector &set_of_identifiers;
557
670
 
558
671
public:
559
672
 
560
 
  AddTableIdentifier(CachedDirectory &directory_arg, const SchemaIdentifier &identifier_arg, TableIdentifiers &of_names) :
 
673
  AddTableIdentifier(CachedDirectory &directory_arg, const identifier::Schema &identifier_arg, identifier::Table::vector &of_names) :
561
674
    directory(directory_arg),
562
675
    identifier(identifier_arg),
563
676
    set_of_identifiers(of_names)
571
684
};
572
685
 
573
686
 
574
 
void StorageEngine::getIdentifiers(Session &session, const SchemaIdentifier &schema_identifier, TableIdentifiers &set_of_identifiers)
 
687
void StorageEngine::getIdentifiers(Session &session, const identifier::Schema &schema_identifier, identifier::Table::vector &set_of_identifiers)
575
688
{
576
 
  static SchemaIdentifier INFORMATION_SCHEMA_IDENTIFIER("information_schema");
577
 
  static SchemaIdentifier DATA_DICTIONARY_IDENTIFIER("data_dictionary");
578
 
 
579
689
  CachedDirectory directory(schema_identifier.getPath(), set_of_table_definition_ext);
580
690
 
581
691
  if (schema_identifier == INFORMATION_SCHEMA_IDENTIFIER)
588
698
    {
589
699
      errno= directory.getError();
590
700
      if (errno == ENOENT)
591
 
        my_error(ER_BAD_DB_ERROR, MYF(ME_BELL+ME_WAITTANG), const_cast<SchemaIdentifier &>(schema_identifier).getSQLPath().c_str());
 
701
      {
 
702
        std::string path;
 
703
        schema_identifier.getSQLPath(path);
 
704
        my_error(ER_BAD_DB_ERROR, MYF(ME_BELL+ME_WAITTANG), path.c_str());
 
705
      }
592
706
      else
 
707
      {
593
708
        my_error(ER_CANT_READ_DIR, MYF(ME_BELL+ME_WAITTANG), directory.getPath(), errno);
 
709
      }
 
710
 
594
711
      return;
595
712
    }
596
713
  }
597
714
 
598
 
  for_each(vector_of_engines.begin(), vector_of_engines.end(),
599
 
           AddTableIdentifier(directory, schema_identifier, set_of_identifiers));
 
715
  std::for_each(vector_of_engines.begin(), vector_of_engines.end(),
 
716
                AddTableIdentifier(directory, schema_identifier, set_of_identifiers));
600
717
 
601
718
  session.doGetTableIdentifiers(directory, schema_identifier, set_of_identifiers);
602
719
}
603
720
 
604
 
class DropTable: public unary_function<TableIdentifier&, bool>
 
721
class DropTable: public std::unary_function<identifier::Table&, bool>
605
722
{
606
723
  Session &session;
607
724
  StorageEngine *engine;
619
736
  } 
620
737
};
621
738
 
622
 
/* This will later be converted to TableIdentifiers */
623
 
class DropTables: public unary_function<StorageEngine *, void>
 
739
/* This will later be converted to identifier::Tables */
 
740
class DropTables: public std::unary_function<StorageEngine *, void>
624
741
{
625
742
  Session &session;
626
 
  TableIdentifiers &table_identifiers;
 
743
  identifier::Table::vector &table_identifiers;
627
744
 
628
745
public:
629
746
 
630
 
  DropTables(Session &session_arg, TableIdentifiers &table_identifiers_arg) :
 
747
  DropTables(Session &session_arg, identifier::Table::vector &table_identifiers_arg) :
631
748
    session(session_arg),
632
749
    table_identifiers(table_identifiers_arg)
633
750
  { }
636
753
  {
637
754
    // True returning from DropTable means the table has been successfully
638
755
    // deleted, so it should be removed from the list of tables to drop
639
 
    table_identifiers.erase(remove_if(table_identifiers.begin(),
640
 
                                      table_identifiers.end(),
641
 
                                      DropTable(session, engine)),
 
756
    table_identifiers.erase(std::remove_if(table_identifiers.begin(),
 
757
                                           table_identifiers.end(),
 
758
                                           DropTable(session, engine)),
642
759
                            table_identifiers.end());
643
760
  }
644
761
};
651
768
void StorageEngine::removeLostTemporaryTables(Session &session, const char *directory)
652
769
{
653
770
  CachedDirectory dir(directory, set_of_table_definition_ext);
654
 
  TableIdentifiers table_identifiers;
 
771
  identifier::Table::vector table_identifiers;
655
772
 
656
773
  if (dir.fail())
657
774
  {
667
784
       fileIter != files.end(); fileIter++)
668
785
  {
669
786
    size_t length;
670
 
    string path;
 
787
    std::string path;
671
788
    CachedDirectory::Entry *entry= *fileIter;
672
789
 
673
790
    /* We remove the file extension. */
680
797
    message::Table definition;
681
798
    if (StorageEngine::readTableFile(path, definition))
682
799
    {
683
 
      TableIdentifier identifier(definition.schema(), definition.name(), path);
 
800
      identifier::Table identifier(definition.schema(), definition.name(), path);
684
801
      table_identifiers.push_back(identifier);
685
802
    }
686
803
  }
687
804
 
688
 
  for_each(vector_of_engines.begin(), vector_of_engines.end(),
689
 
           DropTables(session, table_identifiers));
690
 
  
 
805
  std::for_each(vector_of_engines.begin(), vector_of_engines.end(),
 
806
                DropTables(session, table_identifiers));
 
807
 
691
808
  /*
692
809
    Now we just clean up anything that might left over.
693
810
 
694
811
    We rescan because some of what might have been there should
695
812
    now be all nice and cleaned up.
696
813
  */
697
 
  set<string> all_exts= set_of_table_definition_ext;
 
814
  std::set<std::string> all_exts= set_of_table_definition_ext;
698
815
 
699
816
  for (EngineVector::iterator iter= vector_of_engines.begin();
700
817
       iter != vector_of_engines.end() ; iter++)
709
826
  for (CachedDirectory::Entries::iterator fileIter= files.begin();
710
827
       fileIter != files.end(); fileIter++)
711
828
  {
712
 
    string path;
 
829
    std::string path;
713
830
    CachedDirectory::Entry *entry= *fileIter;
714
831
 
715
832
    path+= directory;
730
847
    - table->getShare()->path
731
848
    - table->alias
732
849
*/
733
 
void StorageEngine::print_error(int error, myf errflag, Table &table)
734
 
{
735
 
  print_error(error, errflag, &table);
736
 
}
737
 
 
738
 
void StorageEngine::print_error(int error, myf errflag, Table *table)
739
 
{
740
 
  int textno= ER_GET_ERRNO;
 
850
void StorageEngine::print_error(int error, myf errflag, const Table &table) const
 
851
{
 
852
  drizzled::error_t textno= ER_GET_ERRNO;
741
853
  switch (error) {
742
854
  case EACCES:
743
855
    textno=ER_OPEN_AS_READONLY;
758
870
    break;
759
871
  case HA_ERR_FOUND_DUPP_KEY:
760
872
  {
761
 
    assert(table);
762
 
    uint32_t key_nr= table->get_dup_key(error);
 
873
    uint32_t key_nr= table.get_dup_key(error);
763
874
    if ((int) key_nr >= 0)
764
875
    {
765
876
      const char *err_msg= ER(ER_DUP_ENTRY_WITH_KEY_NAME);
766
877
 
767
 
      print_keydup_error(key_nr, err_msg, *table);
 
878
      print_keydup_error(key_nr, err_msg, table);
768
879
 
769
880
      return;
770
881
    }
773
884
  }
774
885
  case HA_ERR_FOREIGN_DUPLICATE_KEY:
775
886
  {
776
 
    assert(table);
777
 
    uint32_t key_nr= table->get_dup_key(error);
 
887
    uint32_t key_nr= table.get_dup_key(error);
778
888
    if ((int) key_nr >= 0)
779
889
    {
780
890
      uint32_t max_length;
784
894
      String str(key,sizeof(key),system_charset_info);
785
895
 
786
896
      /* Table is opened and defined at this point */
787
 
      key_unpack(&str,table,(uint32_t) key_nr);
 
897
      key_unpack(&str, &table,(uint32_t) key_nr);
788
898
      max_length= (DRIZZLE_ERRMSG_SIZE-
789
899
                   (uint32_t) strlen(ER(ER_FOREIGN_DUPLICATE_KEY)));
790
900
      if (str.length() >= max_length)
792
902
        str.length(max_length-4);
793
903
        str.append(STRING_WITH_LEN("..."));
794
904
      }
795
 
      my_error(ER_FOREIGN_DUPLICATE_KEY, MYF(0), table->getShare()->getTableName(),
 
905
      my_error(ER_FOREIGN_DUPLICATE_KEY, MYF(0), table.getShare()->getTableName(),
796
906
        str.c_ptr(), key_nr+1);
797
907
      return;
798
908
    }
815
925
    textno=ER_CRASHED_ON_USAGE;
816
926
    break;
817
927
  case HA_ERR_NOT_A_TABLE:
818
 
    textno= error;
 
928
    textno= static_cast<drizzled::error_t>(error);
819
929
    break;
820
930
  case HA_ERR_CRASHED_ON_REPAIR:
821
931
    textno=ER_CRASHED_ON_REPAIR;
869
979
    textno=ER_TABLE_DEF_CHANGED;
870
980
    break;
871
981
  case HA_ERR_NO_SUCH_TABLE:
872
 
    assert(table);
873
 
    my_error(ER_NO_SUCH_TABLE, MYF(0), table->getShare()->getSchemaName(),
874
 
             table->getShare()->getTableName());
875
 
    return;
 
982
    {
 
983
      identifier::Table identifier(table.getShare()->getSchemaName(), table.getShare()->getTableName());
 
984
      my_error(ER_TABLE_UNKNOWN, identifier);
 
985
      return;
 
986
    }
876
987
  case HA_ERR_RBR_LOGGING_FAILED:
877
988
    textno= ER_BINLOG_ROW_LOGGING_FAILED;
878
989
    break;
879
990
  case HA_ERR_DROP_INDEX_FK:
880
991
  {
881
 
    assert(table);
882
992
    const char *ptr= "???";
883
 
    uint32_t key_nr= table->get_dup_key(error);
 
993
    uint32_t key_nr= table.get_dup_key(error);
884
994
    if ((int) key_nr >= 0)
885
 
      ptr= table->key_info[key_nr].name;
 
995
      ptr= table.key_info[key_nr].name;
886
996
    my_error(ER_DROP_INDEX_FK, MYF(0), ptr);
887
997
    return;
888
998
  }
927
1037
      return;
928
1038
    }
929
1039
  }
930
 
  my_error(textno, errflag, table->getShare()->getTableName(), error);
 
1040
 
 
1041
  my_error(textno, errflag, table.getShare()->getTableName(), error);
931
1042
}
932
1043
 
933
1044
 
940
1051
  @return
941
1052
    Returns true if this is a temporary error
942
1053
*/
943
 
bool StorageEngine::get_error_message(int , String* )
 
1054
bool StorageEngine::get_error_message(int , String* ) const
944
1055
{
945
1056
  return false;
946
1057
}
947
1058
 
948
1059
 
949
 
void StorageEngine::print_keydup_error(uint32_t key_nr, const char *msg, Table &table)
 
1060
void StorageEngine::print_keydup_error(uint32_t key_nr, const char *msg, const Table &table) const
950
1061
{
951
1062
  /* Write the duplicated key in the error message */
952
1063
  char key[MAX_KEY_LENGTH];
974
1085
}
975
1086
 
976
1087
 
977
 
int StorageEngine::deleteDefinitionFromPath(const TableIdentifier &identifier)
 
1088
int StorageEngine::deleteDefinitionFromPath(const identifier::Table &identifier)
978
1089
{
979
 
  string path(identifier.getPath());
 
1090
  std::string path(identifier.getPath());
980
1091
 
981
1092
  path.append(DEFAULT_DEFINITION_FILE_EXT);
982
1093
 
983
1094
  return internal::my_delete(path.c_str(), MYF(0));
984
1095
}
985
1096
 
986
 
int StorageEngine::renameDefinitionFromPath(const TableIdentifier &dest, const TableIdentifier &src)
 
1097
int StorageEngine::renameDefinitionFromPath(const identifier::Table &dest, const identifier::Table &src)
987
1098
{
988
1099
  message::Table table_message;
989
 
  string src_path(src.getPath());
990
 
  string dest_path(dest.getPath());
 
1100
  std::string src_path(src.getPath());
 
1101
  std::string dest_path(dest.getPath());
991
1102
 
992
1103
  src_path.append(DEFAULT_DEFINITION_FILE_EXT);
993
1104
  dest_path.append(DEFAULT_DEFINITION_FILE_EXT);
1012
1123
  return error;
1013
1124
}
1014
1125
 
1015
 
int StorageEngine::writeDefinitionFromPath(const TableIdentifier &identifier, message::Table &table_message)
 
1126
int StorageEngine::writeDefinitionFromPath(const identifier::Table &identifier, message::Table &table_message)
1016
1127
{
1017
1128
  char definition_file_tmp[FN_REFLEN];
1018
 
  string file_name(identifier.getPath());
 
1129
  std::string file_name(identifier.getPath());
1019
1130
 
1020
1131
  file_name.append(DEFAULT_DEFINITION_FILE_EXT);
1021
1132
 
1034
1145
 
1035
1146
  bool success;
1036
1147
 
1037
 
  try {
 
1148
  try
 
1149
  {
1038
1150
    success= table_message.SerializeToZeroCopyStream(output);
1039
1151
  }
1040
1152
  catch (...)
1044
1156
 
1045
1157
  if (not success)
1046
1158
  {
 
1159
    std::string error_message;
 
1160
    identifier.getSQLPath(error_message);
 
1161
 
1047
1162
    my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0),
 
1163
             error_message.c_str(),
1048
1164
             table_message.InitializationErrorString().c_str());
1049
1165
    delete output;
1050
1166
 
1084
1200
  return 0;
1085
1201
}
1086
1202
 
1087
 
class CanCreateTable: public unary_function<StorageEngine *, bool>
 
1203
class CanCreateTable: public std::unary_function<StorageEngine *, bool>
1088
1204
{
1089
 
  const TableIdentifier &identifier;
 
1205
  const identifier::Table &identifier;
1090
1206
 
1091
1207
public:
1092
 
  CanCreateTable(const TableIdentifier &identifier_arg) :
 
1208
  CanCreateTable(const identifier::Table &identifier_arg) :
1093
1209
    identifier(identifier_arg)
1094
1210
  { }
1095
1211
 
1103
1219
/**
1104
1220
  @note on success table can be created.
1105
1221
*/
1106
 
bool StorageEngine::canCreateTable(const TableIdentifier &identifier)
 
1222
bool StorageEngine::canCreateTable(const identifier::Table &identifier)
1107
1223
{
1108
1224
  EngineVector::iterator iter=
1109
 
    find_if(vector_of_engines.begin(), vector_of_engines.end(),
1110
 
            CanCreateTable(identifier));
 
1225
    std::find_if(vector_of_engines.begin(), vector_of_engines.end(),
 
1226
                 CanCreateTable(identifier));
1111
1227
 
1112
1228
  if (iter == vector_of_engines.end())
1113
1229
  {
1119
1235
 
1120
1236
bool StorageEngine::readTableFile(const std::string &path, message::Table &table_message)
1121
1237
{
1122
 
  fstream input(path.c_str(), ios::in | ios::binary);
 
1238
  std::fstream input(path.c_str(), std::ios::in | std::ios::binary);
1123
1239
 
1124
1240
  if (input.good())
1125
1241
  {
1132
1248
    catch (...)
1133
1249
    {
1134
1250
      my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0),
 
1251
               table_message.name().empty() ? path.c_str() : table_message.name().c_str(),
1135
1252
               table_message.InitializationErrorString().empty() ? "": table_message.InitializationErrorString().c_str());
1136
1253
    }
1137
1254
  }
1143
1260
  return false;
1144
1261
}
1145
1262
 
 
1263
std::ostream& operator<<(std::ostream& output, const StorageEngine &engine)
 
1264
{
 
1265
  output << "StorageEngine:(";
 
1266
  output <<  engine.getName();
 
1267
  output << ")";
1146
1268
 
 
1269
  return output;
 
1270
}
1147
1271
 
1148
1272
} /* namespace plugin */
1149
1273
} /* namespace drizzled */