~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/plugin/storage_engine.cc

  • Committer: Brian Aker
  • Date: 2011-02-22 06:12:02 UTC
  • mfrom: (2190.1.6 drizzle-build)
  • Revision ID: brian@tangent.org-20110222061202-k03czxykqy4x9hjs
List update, header fixes, multiple symbols, and David deletes some code.

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
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"
52
 
#include "drizzled/db.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>
53
52
 
54
53
#include <drizzled/table_proto.h>
55
54
#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
 
59
61
#include <boost/algorithm/string/compare.hpp>
60
62
 
61
63
static bool shutdown_has_begun= false; // Once we put in the container for the vector/etc for engines this will go away.
62
64
 
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 string name_arg,
86
 
                             const bitset<HTON_BIT_SIZE> &flags_arg) :
 
85
StorageEngine::StorageEngine(const std::string name_arg,
 
86
                             const std::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 TableIdentifier &from, const TableIdentifier &to)
 
104
int StorageEngine::renameTable(Session &session, const identifier::Table &from, const identifier::Table &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 TableIdentifier &identifier)
 
140
int StorageEngine::doDropTable(Session&, const identifier::Table &identifier)
141
141
                               
142
142
{
143
143
  int error= 0;
192
192
}
193
193
 
194
194
class FindEngineByName
195
 
  : public unary_function<StorageEngine *, bool>
 
195
  : public std::unary_function<StorageEngine *, bool>
196
196
{
197
 
  const string &predicate;
 
197
  const std::string &predicate;
198
198
 
199
199
public:
200
 
  explicit FindEngineByName(const string &target_arg) :
 
200
  explicit FindEngineByName(const std::string &target_arg) :
201
201
    predicate(target_arg)
202
202
  {
203
203
  }
208
208
  }
209
209
};
210
210
 
211
 
StorageEngine *StorageEngine::findByName(const string &predicate)
 
211
StorageEngine *StorageEngine::findByName(const std::string &predicate)
212
212
{
213
 
  EngineVector::iterator iter= find_if(vector_of_engines.begin(),
214
 
                                       vector_of_engines.end(),
215
 
                                       FindEngineByName(predicate));
 
213
  EngineVector::iterator iter= std::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 string &predicate)
 
226
StorageEngine *StorageEngine::findByName(Session& session, const std::string &predicate)
227
227
{
228
228
  if (boost::iequals(predicate, DEFAULT_STRING))
229
229
    return session.getDefaultStorageEngine();
230
230
 
231
 
  EngineVector::iterator iter= find_if(vector_of_engines.begin(),
232
 
                                       vector_of_engines.end(),
233
 
                                       FindEngineByName(predicate));
 
231
  EngineVector::iterator iter= std::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 unary_function<StorageEngine *, void>
 
244
class StorageEngineCloseConnection : public std::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
 
  for_each(vector_of_engines.begin(), vector_of_engines.end(),
267
 
           StorageEngineCloseConnection(session));
 
266
  std::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 (find_if(vector_of_engines.begin(), vector_of_engines.end(),
275
 
                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))
276
276
        != vector_of_engines.begin())
277
277
      return true;
278
278
  }
284
284
  return false;
285
285
}
286
286
 
287
 
class StorageEngineGetTableDefinition: public unary_function<StorageEngine *,bool>
 
287
class StorageEngineGetTableDefinition: public std::unary_function<StorageEngine *,bool>
288
288
{
289
289
  Session& session;
290
 
  const TableIdentifier &identifier;
 
290
  const identifier::Table &identifier;
291
291
  message::Table &table_message;
292
 
  int &err;
 
292
  drizzled::error_t &err;
293
293
 
294
294
public:
295
295
  StorageEngineGetTableDefinition(Session& session_arg,
296
 
                                  const TableIdentifier &identifier_arg,
 
296
                                  const identifier::Table &identifier_arg,
297
297
                                  message::Table &table_message_arg,
298
 
                                  int &err_arg) :
 
298
                                  drizzled::error_t &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= ret;
 
309
      err= static_cast<drizzled::error_t>(ret);
310
310
 
311
 
    return err == EEXIST || err != ENOENT;
 
311
    return err == static_cast<drizzled::error_t>(EEXIST) or err != static_cast<drizzled::error_t>(ENOENT);
312
312
  }
313
313
};
314
314
 
315
 
class StorageEngineDoesTableExist: public unary_function<StorageEngine *, bool>
 
315
class StorageEngineDoesTableExist: public std::unary_function<StorageEngine *, bool>
316
316
{
317
317
  Session& session;
318
 
  const TableIdentifier &identifier;
 
318
  const identifier::Table &identifier;
319
319
 
320
320
public:
321
 
  StorageEngineDoesTableExist(Session& session_arg, const TableIdentifier &identifier_arg) :
 
321
  StorageEngineDoesTableExist(Session& session_arg, const identifier::Table &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 TableIdentifier &identifier,
 
336
                                           const identifier::Table &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
 
    find_if(vector_of_engines.begin(), vector_of_engines.end(),
347
 
            StorageEngineDoesTableExist(session, identifier));
 
346
    std::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::TableIdentifier&)
 
357
bool plugin::StorageEngine::doDoesTableExist(Session&, const drizzled::identifier::Table&)
358
358
{
359
 
  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";
360
360
  assert(0);
361
361
  return false;
362
362
}
363
363
 
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)
 
364
message::table::shared_ptr StorageEngine::getTableMessage(Session& session,
 
365
                                                          identifier::Table::const_reference identifier,
 
366
                                                          bool include_temporary_tables)
373
367
{
374
 
  int err= ENOENT;
 
368
  drizzled::error_t error;
 
369
  error= static_cast<drizzled::error_t>(ENOENT);
375
370
 
376
371
  if (include_temporary_tables)
377
372
  {
378
 
    if (session.doGetTableDefinition(identifier, table_message) == EEXIST)
379
 
      return EEXIST;
380
 
  }
381
 
 
 
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;
382
387
  EngineVector::iterator iter=
383
 
    find_if(vector_of_engines.begin(), vector_of_engines.end(),
384
 
            StorageEngineGetTableDefinition(session, identifier, table_message, err));
 
388
    std::find_if(vector_of_engines.begin(), vector_of_engines.end(),
 
389
                 StorageEngineGetTableDefinition(session, identifier, message, error));
385
390
 
386
391
  if (iter == vector_of_engines.end())
387
392
  {
388
 
    return ENOENT;
 
393
    return message::table::shared_ptr();
389
394
  }
390
 
 
391
 
  return err;
 
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;
392
400
}
393
401
 
394
402
/**
401
409
{
402
410
public:
403
411
  Ha_delete_table_error_handler() : Internal_error_handler() {}
404
 
  virtual bool handle_error(uint32_t sql_errno,
 
412
  virtual bool handle_error(drizzled::error_t sql_errno,
405
413
                            const char *message,
406
414
                            DRIZZLE_ERROR::enum_warning_level level,
407
415
                            Session *session);
411
419
 
412
420
bool
413
421
Ha_delete_table_error_handler::
414
 
handle_error(uint32_t ,
 
422
handle_error(drizzled::error_t ,
415
423
             const char *message,
416
424
             DRIZZLE_ERROR::enum_warning_level ,
417
425
             Session *)
421
429
  return true;
422
430
}
423
431
 
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
 
 
 
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;
473
517
  engine.setTransactionReadWrite(session);
 
518
 
 
519
  assert(identifier.isTmp());
474
520
  
475
521
  if (unlikely(plugin::EventObserver::beforeDropTable(session, identifier)))
476
522
  {
478
524
  }
479
525
  else
480
526
  {
481
 
    error= engine.doDropTable(session, identifier);
 
527
    error= static_cast<drizzled::error_t>(engine.doDropTable(session, identifier));
 
528
 
482
529
    if (unlikely(plugin::EventObserver::afterDropTable(session, identifier, error)))
483
530
    {
484
531
      error= ER_EVENT_OBSERVER_PLUGIN;
485
532
    }
486
533
  }
487
534
 
488
 
 
489
 
  return error;
 
535
  drizzled::message::Cache::singleton().erase(identifier);
 
536
 
 
537
  if (error)
 
538
  {
 
539
    return false;
 
540
  }
 
541
 
 
542
  return true;
490
543
}
491
544
 
492
545
 
498
551
  @retval
499
552
   1  error
500
553
*/
501
 
int StorageEngine::createTable(Session &session,
502
 
                               const TableIdentifier &identifier,
503
 
                               message::Table& table_message)
 
554
bool StorageEngine::createTable(Session &session,
 
555
                                const identifier::Table &identifier,
 
556
                                message::Table& table_message)
504
557
{
505
 
  int error= 1;
 
558
  drizzled::error_t error= EE_OK;
 
559
 
506
560
  TableShare share(identifier);
507
561
  table::Shell table(share);
508
562
  message::Table tmp_proto;
510
564
  if (share.parse_table_proto(session, table_message) || share.open_table_from_share(&session, identifier, "", 0, 0, table))
511
565
  { 
512
566
    // @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;
513
572
  }
514
573
  else
515
574
  {
528
587
    {
529
588
      share.storage_engine->setTransactionReadWrite(session);
530
589
 
531
 
      error= share.storage_engine->doCreateTable(session,
532
 
                                                 table,
533
 
                                                 identifier,
534
 
                                                 table_message);
 
590
      error= static_cast<drizzled::error_t>(share.storage_engine->doCreateTable(session,
 
591
                                                                                table,
 
592
                                                                                identifier,
 
593
                                                                                table_message));
535
594
    }
536
595
 
537
 
    if (error)
538
 
    {
539
 
      my_error(ER_CANT_CREATE_TABLE, MYF(ME_BELL+ME_WAITTANG), const_cast<TableIdentifier &>(identifier).getSQLPath().c_str(), error);
 
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);
540
605
    }
541
606
 
542
607
    table.delete_table();
543
608
  }
544
609
 
545
 
  return(error != 0);
 
610
  return(error == EE_OK);
546
611
}
547
612
 
548
613
Cursor *StorageEngine::getCursor(Table &arg)
551
616
}
552
617
 
553
618
class AddTableIdentifier : 
554
 
  public unary_function<StorageEngine *, void>
 
619
  public std::unary_function<StorageEngine *, void>
555
620
{
556
621
  CachedDirectory &directory;
557
 
  const SchemaIdentifier &identifier;
558
 
  TableIdentifiers &set_of_identifiers;
 
622
  const identifier::Schema &identifier;
 
623
  identifier::Table::vector &set_of_identifiers;
559
624
 
560
625
public:
561
626
 
562
 
  AddTableIdentifier(CachedDirectory &directory_arg, const SchemaIdentifier &identifier_arg, TableIdentifiers &of_names) :
 
627
  AddTableIdentifier(CachedDirectory &directory_arg, const identifier::Schema &identifier_arg, identifier::Table::vector &of_names) :
563
628
    directory(directory_arg),
564
629
    identifier(identifier_arg),
565
630
    set_of_identifiers(of_names)
573
638
};
574
639
 
575
640
 
576
 
void StorageEngine::getIdentifiers(Session &session, const SchemaIdentifier &schema_identifier, TableIdentifiers &set_of_identifiers)
 
641
void StorageEngine::getIdentifiers(Session &session, const identifier::Schema &schema_identifier, identifier::Table::vector &set_of_identifiers)
577
642
{
578
 
  static SchemaIdentifier INFORMATION_SCHEMA_IDENTIFIER("information_schema");
579
 
  static SchemaIdentifier DATA_DICTIONARY_IDENTIFIER("data_dictionary");
580
 
 
581
643
  CachedDirectory directory(schema_identifier.getPath(), set_of_table_definition_ext);
582
644
 
583
645
  if (schema_identifier == INFORMATION_SCHEMA_IDENTIFIER)
590
652
    {
591
653
      errno= directory.getError();
592
654
      if (errno == ENOENT)
593
 
        my_error(ER_BAD_DB_ERROR, MYF(ME_BELL+ME_WAITTANG), const_cast<SchemaIdentifier &>(schema_identifier).getSQLPath().c_str());
 
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
      }
594
660
      else
 
661
      {
595
662
        my_error(ER_CANT_READ_DIR, MYF(ME_BELL+ME_WAITTANG), directory.getPath(), errno);
 
663
      }
 
664
 
596
665
      return;
597
666
    }
598
667
  }
599
668
 
600
 
  for_each(vector_of_engines.begin(), vector_of_engines.end(),
601
 
           AddTableIdentifier(directory, schema_identifier, set_of_identifiers));
 
669
  std::for_each(vector_of_engines.begin(), vector_of_engines.end(),
 
670
                AddTableIdentifier(directory, schema_identifier, set_of_identifiers));
602
671
 
603
672
  session.doGetTableIdentifiers(directory, schema_identifier, set_of_identifiers);
604
673
}
605
674
 
606
 
class DropTable: public unary_function<TableIdentifier&, bool>
 
675
class DropTable: public std::unary_function<identifier::Table&, bool>
607
676
{
608
677
  Session &session;
609
678
  StorageEngine *engine;
621
690
  } 
622
691
};
623
692
 
624
 
/* This will later be converted to TableIdentifiers */
625
 
class DropTables: public unary_function<StorageEngine *, void>
 
693
/* This will later be converted to identifier::Tables */
 
694
class DropTables: public std::unary_function<StorageEngine *, void>
626
695
{
627
696
  Session &session;
628
 
  TableIdentifiers &table_identifiers;
 
697
  identifier::Table::vector &table_identifiers;
629
698
 
630
699
public:
631
700
 
632
 
  DropTables(Session &session_arg, TableIdentifiers &table_identifiers_arg) :
 
701
  DropTables(Session &session_arg, identifier::Table::vector &table_identifiers_arg) :
633
702
    session(session_arg),
634
703
    table_identifiers(table_identifiers_arg)
635
704
  { }
638
707
  {
639
708
    // True returning from DropTable means the table has been successfully
640
709
    // deleted, so it should be removed from the list of tables to drop
641
 
    table_identifiers.erase(remove_if(table_identifiers.begin(),
642
 
                                      table_identifiers.end(),
643
 
                                      DropTable(session, engine)),
 
710
    table_identifiers.erase(std::remove_if(table_identifiers.begin(),
 
711
                                           table_identifiers.end(),
 
712
                                           DropTable(session, engine)),
644
713
                            table_identifiers.end());
645
714
  }
646
715
};
653
722
void StorageEngine::removeLostTemporaryTables(Session &session, const char *directory)
654
723
{
655
724
  CachedDirectory dir(directory, set_of_table_definition_ext);
656
 
  TableIdentifiers table_identifiers;
 
725
  identifier::Table::vector table_identifiers;
657
726
 
658
727
  if (dir.fail())
659
728
  {
669
738
       fileIter != files.end(); fileIter++)
670
739
  {
671
740
    size_t length;
672
 
    string path;
 
741
    std::string path;
673
742
    CachedDirectory::Entry *entry= *fileIter;
674
743
 
675
744
    /* We remove the file extension. */
682
751
    message::Table definition;
683
752
    if (StorageEngine::readTableFile(path, definition))
684
753
    {
685
 
      TableIdentifier identifier(definition.schema(), definition.name(), path);
 
754
      identifier::Table identifier(definition.schema(), definition.name(), path);
686
755
      table_identifiers.push_back(identifier);
687
756
    }
688
757
  }
689
758
 
690
 
  for_each(vector_of_engines.begin(), vector_of_engines.end(),
691
 
           DropTables(session, table_identifiers));
692
 
  
 
759
  std::for_each(vector_of_engines.begin(), vector_of_engines.end(),
 
760
                DropTables(session, table_identifiers));
 
761
 
693
762
  /*
694
763
    Now we just clean up anything that might left over.
695
764
 
696
765
    We rescan because some of what might have been there should
697
766
    now be all nice and cleaned up.
698
767
  */
699
 
  set<string> all_exts= set_of_table_definition_ext;
 
768
  std::set<std::string> all_exts= set_of_table_definition_ext;
700
769
 
701
770
  for (EngineVector::iterator iter= vector_of_engines.begin();
702
771
       iter != vector_of_engines.end() ; iter++)
711
780
  for (CachedDirectory::Entries::iterator fileIter= files.begin();
712
781
       fileIter != files.end(); fileIter++)
713
782
  {
714
 
    string path;
 
783
    std::string path;
715
784
    CachedDirectory::Entry *entry= *fileIter;
716
785
 
717
786
    path+= directory;
732
801
    - table->getShare()->path
733
802
    - table->alias
734
803
*/
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;
 
804
void StorageEngine::print_error(int error, myf errflag, const Table &table) const
 
805
{
 
806
  drizzled::error_t textno= ER_GET_ERRNO;
743
807
  switch (error) {
744
808
  case EACCES:
745
809
    textno=ER_OPEN_AS_READONLY;
760
824
    break;
761
825
  case HA_ERR_FOUND_DUPP_KEY:
762
826
  {
763
 
    assert(table);
764
 
    uint32_t key_nr= table->get_dup_key(error);
 
827
    uint32_t key_nr= table.get_dup_key(error);
765
828
    if ((int) key_nr >= 0)
766
829
    {
767
830
      const char *err_msg= ER(ER_DUP_ENTRY_WITH_KEY_NAME);
768
831
 
769
 
      print_keydup_error(key_nr, err_msg, *table);
 
832
      print_keydup_error(key_nr, err_msg, table);
770
833
 
771
834
      return;
772
835
    }
775
838
  }
776
839
  case HA_ERR_FOREIGN_DUPLICATE_KEY:
777
840
  {
778
 
    assert(table);
779
 
    uint32_t key_nr= table->get_dup_key(error);
 
841
    uint32_t key_nr= table.get_dup_key(error);
780
842
    if ((int) key_nr >= 0)
781
843
    {
782
844
      uint32_t max_length;
786
848
      String str(key,sizeof(key),system_charset_info);
787
849
 
788
850
      /* Table is opened and defined at this point */
789
 
      key_unpack(&str,table,(uint32_t) key_nr);
 
851
      key_unpack(&str, &table,(uint32_t) key_nr);
790
852
      max_length= (DRIZZLE_ERRMSG_SIZE-
791
853
                   (uint32_t) strlen(ER(ER_FOREIGN_DUPLICATE_KEY)));
792
854
      if (str.length() >= max_length)
794
856
        str.length(max_length-4);
795
857
        str.append(STRING_WITH_LEN("..."));
796
858
      }
797
 
      my_error(ER_FOREIGN_DUPLICATE_KEY, MYF(0), table->getShare()->getTableName(),
 
859
      my_error(ER_FOREIGN_DUPLICATE_KEY, MYF(0), table.getShare()->getTableName(),
798
860
        str.c_ptr(), key_nr+1);
799
861
      return;
800
862
    }
817
879
    textno=ER_CRASHED_ON_USAGE;
818
880
    break;
819
881
  case HA_ERR_NOT_A_TABLE:
820
 
    textno= error;
 
882
    textno= static_cast<drizzled::error_t>(error);
821
883
    break;
822
884
  case HA_ERR_CRASHED_ON_REPAIR:
823
885
    textno=ER_CRASHED_ON_REPAIR;
871
933
    textno=ER_TABLE_DEF_CHANGED;
872
934
    break;
873
935
  case HA_ERR_NO_SUCH_TABLE:
874
 
    assert(table);
875
 
    my_error(ER_NO_SUCH_TABLE, MYF(0), table->getShare()->getSchemaName(),
876
 
             table->getShare()->getTableName());
877
 
    return;
 
936
    {
 
937
      identifier::Table identifier(table.getShare()->getSchemaName(), table.getShare()->getTableName());
 
938
      my_error(ER_TABLE_UNKNOWN, identifier);
 
939
      return;
 
940
    }
878
941
  case HA_ERR_RBR_LOGGING_FAILED:
879
942
    textno= ER_BINLOG_ROW_LOGGING_FAILED;
880
943
    break;
881
944
  case HA_ERR_DROP_INDEX_FK:
882
945
  {
883
 
    assert(table);
884
946
    const char *ptr= "???";
885
 
    uint32_t key_nr= table->get_dup_key(error);
 
947
    uint32_t key_nr= table.get_dup_key(error);
886
948
    if ((int) key_nr >= 0)
887
 
      ptr= table->key_info[key_nr].name;
 
949
      ptr= table.key_info[key_nr].name;
888
950
    my_error(ER_DROP_INDEX_FK, MYF(0), ptr);
889
951
    return;
890
952
  }
929
991
      return;
930
992
    }
931
993
  }
932
 
  my_error(textno, errflag, table->getShare()->getTableName(), error);
 
994
 
 
995
  my_error(textno, errflag, table.getShare()->getTableName(), error);
933
996
}
934
997
 
935
998
 
942
1005
  @return
943
1006
    Returns true if this is a temporary error
944
1007
*/
945
 
bool StorageEngine::get_error_message(int , String* )
 
1008
bool StorageEngine::get_error_message(int , String* ) const
946
1009
{
947
1010
  return false;
948
1011
}
949
1012
 
950
1013
 
951
 
void StorageEngine::print_keydup_error(uint32_t key_nr, const char *msg, Table &table)
 
1014
void StorageEngine::print_keydup_error(uint32_t key_nr, const char *msg, const Table &table) const
952
1015
{
953
1016
  /* Write the duplicated key in the error message */
954
1017
  char key[MAX_KEY_LENGTH];
976
1039
}
977
1040
 
978
1041
 
979
 
int StorageEngine::deleteDefinitionFromPath(const TableIdentifier &identifier)
 
1042
int StorageEngine::deleteDefinitionFromPath(const identifier::Table &identifier)
980
1043
{
981
 
  string path(identifier.getPath());
 
1044
  std::string path(identifier.getPath());
982
1045
 
983
1046
  path.append(DEFAULT_DEFINITION_FILE_EXT);
984
1047
 
985
1048
  return internal::my_delete(path.c_str(), MYF(0));
986
1049
}
987
1050
 
988
 
int StorageEngine::renameDefinitionFromPath(const TableIdentifier &dest, const TableIdentifier &src)
 
1051
int StorageEngine::renameDefinitionFromPath(const identifier::Table &dest, const identifier::Table &src)
989
1052
{
990
1053
  message::Table table_message;
991
 
  string src_path(src.getPath());
992
 
  string dest_path(dest.getPath());
 
1054
  std::string src_path(src.getPath());
 
1055
  std::string dest_path(dest.getPath());
993
1056
 
994
1057
  src_path.append(DEFAULT_DEFINITION_FILE_EXT);
995
1058
  dest_path.append(DEFAULT_DEFINITION_FILE_EXT);
1014
1077
  return error;
1015
1078
}
1016
1079
 
1017
 
int StorageEngine::writeDefinitionFromPath(const TableIdentifier &identifier, message::Table &table_message)
 
1080
int StorageEngine::writeDefinitionFromPath(const identifier::Table &identifier, message::Table &table_message)
1018
1081
{
1019
1082
  char definition_file_tmp[FN_REFLEN];
1020
 
  string file_name(identifier.getPath());
 
1083
  std::string file_name(identifier.getPath());
1021
1084
 
1022
1085
  file_name.append(DEFAULT_DEFINITION_FILE_EXT);
1023
1086
 
1036
1099
 
1037
1100
  bool success;
1038
1101
 
1039
 
  try {
 
1102
  try
 
1103
  {
1040
1104
    success= table_message.SerializeToZeroCopyStream(output);
1041
1105
  }
1042
1106
  catch (...)
1046
1110
 
1047
1111
  if (not success)
1048
1112
  {
 
1113
    std::string error_message;
 
1114
    identifier.getSQLPath(error_message);
 
1115
 
1049
1116
    my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0),
 
1117
             error_message.c_str(),
1050
1118
             table_message.InitializationErrorString().c_str());
1051
1119
    delete output;
1052
1120
 
1086
1154
  return 0;
1087
1155
}
1088
1156
 
1089
 
class CanCreateTable: public unary_function<StorageEngine *, bool>
 
1157
class CanCreateTable: public std::unary_function<StorageEngine *, bool>
1090
1158
{
1091
 
  const TableIdentifier &identifier;
 
1159
  const identifier::Table &identifier;
1092
1160
 
1093
1161
public:
1094
 
  CanCreateTable(const TableIdentifier &identifier_arg) :
 
1162
  CanCreateTable(const identifier::Table &identifier_arg) :
1095
1163
    identifier(identifier_arg)
1096
1164
  { }
1097
1165
 
1105
1173
/**
1106
1174
  @note on success table can be created.
1107
1175
*/
1108
 
bool StorageEngine::canCreateTable(const TableIdentifier &identifier)
 
1176
bool StorageEngine::canCreateTable(const identifier::Table &identifier)
1109
1177
{
1110
1178
  EngineVector::iterator iter=
1111
 
    find_if(vector_of_engines.begin(), vector_of_engines.end(),
1112
 
            CanCreateTable(identifier));
 
1179
    std::find_if(vector_of_engines.begin(), vector_of_engines.end(),
 
1180
                 CanCreateTable(identifier));
1113
1181
 
1114
1182
  if (iter == vector_of_engines.end())
1115
1183
  {
1121
1189
 
1122
1190
bool StorageEngine::readTableFile(const std::string &path, message::Table &table_message)
1123
1191
{
1124
 
  fstream input(path.c_str(), ios::in | ios::binary);
 
1192
  std::fstream input(path.c_str(), std::ios::in | std::ios::binary);
1125
1193
 
1126
1194
  if (input.good())
1127
1195
  {
1134
1202
    catch (...)
1135
1203
    {
1136
1204
      my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0),
 
1205
               table_message.name().empty() ? path.c_str() : table_message.name().c_str(),
1137
1206
               table_message.InitializationErrorString().empty() ? "": table_message.InitializationErrorString().c_str());
1138
1207
    }
1139
1208
  }
1145
1214
  return false;
1146
1215
}
1147
1216
 
 
1217
std::ostream& operator<<(std::ostream& output, const StorageEngine &engine)
 
1218
{
 
1219
  output << "StorageEngine:(";
 
1220
  output <<  engine.getName();
 
1221
  output << ")";
1148
1222
 
 
1223
  return output;
 
1224
}
1149
1225
 
1150
1226
} /* namespace plugin */
1151
1227
} /* namespace drizzled */