~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/plugin/storage_engine.cc

  • Committer: Stewart Smith
  • Date: 2011-01-14 05:16:49 UTC
  • mto: (2086.1.3 build)
  • mto: This revision was merged to the branch mainline in revision 2087.
  • Revision ID: stewart@flamingspork.com-20110114051649-4kg0mdrgdtako8vp
add a FIXME to flesh out insert docs

Show diffs side-by-side

added added

removed removed

Lines of Context:
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>
 
59
#include "drizzled/message/cache.h"
60
60
 
61
61
#include <boost/algorithm/string/compare.hpp>
62
62
 
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;
287
287
class StorageEngineGetTableDefinition: public std::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
315
class StorageEngineDoesTableExist: public std::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)
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
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
 
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::shared_ptr &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
378
    Table *table= session.find_temporary_table(identifier);
374
379
    if (table)
375
380
    {
376
 
      return message::table::shared_ptr(new message::Table(*table->getShare()->getTableMessage()));
 
381
      table_message.reset(new message::Table(*table->getShare()->getTableProto()));
 
382
      return EEXIST;
377
383
    }
378
384
  }
379
385
 
380
386
  drizzled::message::table::shared_ptr table_ptr;
381
387
  if ((table_ptr= drizzled::message::Cache::singleton().find(identifier)))
382
388
  {
383
 
    (void)table_ptr;
 
389
    table_message= table_ptr;
384
390
  }
385
391
 
386
392
  message::Table message;
387
393
  EngineVector::iterator iter=
388
394
    std::find_if(vector_of_engines.begin(), vector_of_engines.end(),
389
 
                 StorageEngineGetTableDefinition(session, identifier, message, error));
 
395
                 StorageEngineGetTableDefinition(session, identifier, message, err));
390
396
 
391
397
  if (iter == vector_of_engines.end())
392
398
  {
393
 
    return message::table::shared_ptr();
 
399
    return ENOENT;
394
400
  }
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;
 
401
  table_message.reset(new message::Table(message));
 
402
 
 
403
 drizzled::message::Cache::singleton().insert(identifier, table_message);
 
404
 
 
405
  return err;
400
406
}
401
407
 
402
408
/**
409
415
{
410
416
public:
411
417
  Ha_delete_table_error_handler() : Internal_error_handler() {}
412
 
  virtual bool handle_error(drizzled::error_t sql_errno,
 
418
  virtual bool handle_error(uint32_t sql_errno,
413
419
                            const char *message,
414
420
                            DRIZZLE_ERROR::enum_warning_level level,
415
421
                            Session *session);
419
425
 
420
426
bool
421
427
Ha_delete_table_error_handler::
422
 
handle_error(drizzled::error_t ,
 
428
handle_error(uint32_t ,
423
429
             const char *message,
424
430
             DRIZZLE_ERROR::enum_warning_level ,
425
431
             Session *)
432
438
class DropTableByIdentifier: public std::unary_function<EngineVector::value_type, bool>
433
439
{
434
440
  Session::reference session;
435
 
  identifier::Table::const_reference identifier;
 
441
  TableIdentifier::const_reference identifier;
436
442
  drizzled::error_t &error;
437
443
 
438
444
public:
439
445
 
440
446
  DropTableByIdentifier(Session::reference session_arg,
441
 
                        identifier::Table::const_reference identifier_arg,
 
447
                        TableIdentifier::const_reference identifier_arg,
442
448
                        drizzled::error_t &error_arg) :
443
449
    session(session_arg),
444
450
    identifier(identifier_arg),
472
478
 
473
479
 
474
480
bool StorageEngine::dropTable(Session::reference session,
475
 
                              identifier::Table::const_reference identifier,
 
481
                              TableIdentifier::const_reference identifier,
476
482
                              drizzled::error_t &error)
477
483
{
478
484
  error= EE_OK;
496
502
}
497
503
 
498
504
bool StorageEngine::dropTable(Session& session,
499
 
                              const identifier::Table &identifier)
 
505
                              const TableIdentifier &identifier)
500
506
{
501
507
  drizzled::error_t error;
502
508
 
510
516
 
511
517
bool StorageEngine::dropTable(Session::reference session,
512
518
                              StorageEngine &engine,
513
 
                              identifier::Table::const_reference identifier,
 
519
                              TableIdentifier::const_reference identifier,
514
520
                              drizzled::error_t &error)
515
521
{
516
522
  error= EE_OK;
551
557
  @retval
552
558
   1  error
553
559
*/
554
 
bool StorageEngine::createTable(Session &session,
555
 
                                const identifier::Table &identifier,
556
 
                                message::Table& table_message)
 
560
int StorageEngine::createTable(Session &session,
 
561
                               const TableIdentifier &identifier,
 
562
                               message::Table& table_message)
557
563
{
558
 
  drizzled::error_t error= EE_OK;
559
 
 
 
564
  int error= 1;
560
565
  TableShare share(identifier);
561
566
  table::Shell table(share);
562
567
  message::Table tmp_proto;
564
569
  if (share.parse_table_proto(session, table_message) || share.open_table_from_share(&session, identifier, "", 0, 0, table))
565
570
  { 
566
571
    // @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
572
  }
573
573
  else
574
574
  {
587
587
    {
588
588
      share.storage_engine->setTransactionReadWrite(session);
589
589
 
590
 
      error= static_cast<drizzled::error_t>(share.storage_engine->doCreateTable(session,
591
 
                                                                                table,
592
 
                                                                                identifier,
593
 
                                                                                table_message));
 
590
      error= share.storage_engine->doCreateTable(session,
 
591
                                                 table,
 
592
                                                 identifier,
 
593
                                                 table_message);
594
594
    }
595
595
 
596
596
    if (error == ER_TABLE_PERMISSION_DENIED)
607
607
    table.delete_table();
608
608
  }
609
609
 
610
 
  return(error == EE_OK);
 
610
  return(error != 0);
611
611
}
612
612
 
613
613
Cursor *StorageEngine::getCursor(Table &arg)
619
619
  public std::unary_function<StorageEngine *, void>
620
620
{
621
621
  CachedDirectory &directory;
622
 
  const identifier::Schema &identifier;
623
 
  identifier::Table::vector &set_of_identifiers;
 
622
  const SchemaIdentifier &identifier;
 
623
  TableIdentifier::vector &set_of_identifiers;
624
624
 
625
625
public:
626
626
 
627
 
  AddTableIdentifier(CachedDirectory &directory_arg, const identifier::Schema &identifier_arg, identifier::Table::vector &of_names) :
 
627
  AddTableIdentifier(CachedDirectory &directory_arg, const SchemaIdentifier &identifier_arg, TableIdentifier::vector &of_names) :
628
628
    directory(directory_arg),
629
629
    identifier(identifier_arg),
630
630
    set_of_identifiers(of_names)
638
638
};
639
639
 
640
640
 
641
 
void StorageEngine::getIdentifiers(Session &session, const identifier::Schema &schema_identifier, identifier::Table::vector &set_of_identifiers)
 
641
void StorageEngine::getIdentifiers(Session &session, const SchemaIdentifier &schema_identifier, TableIdentifier::vector &set_of_identifiers)
642
642
{
 
643
  static SchemaIdentifier INFORMATION_SCHEMA_IDENTIFIER("information_schema");
 
644
  static SchemaIdentifier DATA_DICTIONARY_IDENTIFIER("data_dictionary");
 
645
 
643
646
  CachedDirectory directory(schema_identifier.getPath(), set_of_table_definition_ext);
644
647
 
645
648
  if (schema_identifier == INFORMATION_SCHEMA_IDENTIFIER)
672
675
  session.doGetTableIdentifiers(directory, schema_identifier, set_of_identifiers);
673
676
}
674
677
 
675
 
class DropTable: public std::unary_function<identifier::Table&, bool>
 
678
class DropTable: public std::unary_function<TableIdentifier&, bool>
676
679
{
677
680
  Session &session;
678
681
  StorageEngine *engine;
690
693
  } 
691
694
};
692
695
 
693
 
/* This will later be converted to identifier::Tables */
 
696
/* This will later be converted to TableIdentifiers */
694
697
class DropTables: public std::unary_function<StorageEngine *, void>
695
698
{
696
699
  Session &session;
697
 
  identifier::Table::vector &table_identifiers;
 
700
  TableIdentifier::vector &table_identifiers;
698
701
 
699
702
public:
700
703
 
701
 
  DropTables(Session &session_arg, identifier::Table::vector &table_identifiers_arg) :
 
704
  DropTables(Session &session_arg, TableIdentifier::vector &table_identifiers_arg) :
702
705
    session(session_arg),
703
706
    table_identifiers(table_identifiers_arg)
704
707
  { }
722
725
void StorageEngine::removeLostTemporaryTables(Session &session, const char *directory)
723
726
{
724
727
  CachedDirectory dir(directory, set_of_table_definition_ext);
725
 
  identifier::Table::vector table_identifiers;
 
728
  TableIdentifier::vector table_identifiers;
726
729
 
727
730
  if (dir.fail())
728
731
  {
751
754
    message::Table definition;
752
755
    if (StorageEngine::readTableFile(path, definition))
753
756
    {
754
 
      identifier::Table identifier(definition.schema(), definition.name(), path);
 
757
      TableIdentifier identifier(definition.schema(), definition.name(), path);
755
758
      table_identifiers.push_back(identifier);
756
759
    }
757
760
  }
801
804
    - table->getShare()->path
802
805
    - table->alias
803
806
*/
804
 
void StorageEngine::print_error(int error, myf errflag, const Table &table) const
 
807
void StorageEngine::print_error(int error, myf errflag, Table &table)
805
808
{
806
809
  drizzled::error_t textno= ER_GET_ERRNO;
807
810
  switch (error) {
933
936
    textno=ER_TABLE_DEF_CHANGED;
934
937
    break;
935
938
  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
 
    }
 
939
    my_error(ER_NO_SUCH_TABLE, MYF(0), table.getShare()->getSchemaName(),
 
940
             table.getShare()->getTableName());
 
941
    return;
941
942
  case HA_ERR_RBR_LOGGING_FAILED:
942
943
    textno= ER_BINLOG_ROW_LOGGING_FAILED;
943
944
    break;
1005
1006
  @return
1006
1007
    Returns true if this is a temporary error
1007
1008
*/
1008
 
bool StorageEngine::get_error_message(int , String* ) const
 
1009
bool StorageEngine::get_error_message(int , String* )
1009
1010
{
1010
1011
  return false;
1011
1012
}
1012
1013
 
1013
1014
 
1014
 
void StorageEngine::print_keydup_error(uint32_t key_nr, const char *msg, const Table &table) const
 
1015
void StorageEngine::print_keydup_error(uint32_t key_nr, const char *msg, Table &table)
1015
1016
{
1016
1017
  /* Write the duplicated key in the error message */
1017
1018
  char key[MAX_KEY_LENGTH];
1039
1040
}
1040
1041
 
1041
1042
 
1042
 
int StorageEngine::deleteDefinitionFromPath(const identifier::Table &identifier)
 
1043
int StorageEngine::deleteDefinitionFromPath(const TableIdentifier &identifier)
1043
1044
{
1044
1045
  std::string path(identifier.getPath());
1045
1046
 
1048
1049
  return internal::my_delete(path.c_str(), MYF(0));
1049
1050
}
1050
1051
 
1051
 
int StorageEngine::renameDefinitionFromPath(const identifier::Table &dest, const identifier::Table &src)
 
1052
int StorageEngine::renameDefinitionFromPath(const TableIdentifier &dest, const TableIdentifier &src)
1052
1053
{
1053
1054
  message::Table table_message;
1054
1055
  std::string src_path(src.getPath());
1077
1078
  return error;
1078
1079
}
1079
1080
 
1080
 
int StorageEngine::writeDefinitionFromPath(const identifier::Table &identifier, message::Table &table_message)
 
1081
int StorageEngine::writeDefinitionFromPath(const TableIdentifier &identifier, message::Table &table_message)
1081
1082
{
1082
1083
  char definition_file_tmp[FN_REFLEN];
1083
1084
  std::string file_name(identifier.getPath());
1156
1157
 
1157
1158
class CanCreateTable: public std::unary_function<StorageEngine *, bool>
1158
1159
{
1159
 
  const identifier::Table &identifier;
 
1160
  const TableIdentifier &identifier;
1160
1161
 
1161
1162
public:
1162
 
  CanCreateTable(const identifier::Table &identifier_arg) :
 
1163
  CanCreateTable(const TableIdentifier &identifier_arg) :
1163
1164
    identifier(identifier_arg)
1164
1165
  { }
1165
1166
 
1173
1174
/**
1174
1175
  @note on success table can be created.
1175
1176
*/
1176
 
bool StorageEngine::canCreateTable(const identifier::Table &identifier)
 
1177
bool StorageEngine::canCreateTable(const TableIdentifier &identifier)
1177
1178
{
1178
1179
  EngineVector::iterator iter=
1179
1180
    std::find_if(vector_of_engines.begin(), vector_of_engines.end(),