NdbDictionary.hpp 37.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/* Copyright (C) 2003 MySQL AB

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

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */

#ifndef NdbDictionary_H
#define NdbDictionary_H

#include <ndb_types.h>

class Ndb;
unknown's avatar
unknown committed
23 24
struct charset_info_st;
typedef struct charset_info_st CHARSET_INFO;
25 26 27 28 29

/**
 * @class NdbDictionary
 * @brief Data dictionary class
 * 
unknown's avatar
unknown committed
30
 * The preferred and supported way to create and drop tables and indexes
31 32 33 34 35 36
 * in ndb is through the 
 * MySQL Server (see MySQL reference Manual, section MySQL Cluster).
 *
 * Tables and indexes that are created directly through the 
 * NdbDictionary class
 * can not be viewed from the MySQL Server.
unknown's avatar
unknown committed
37 38
 * Dropping indexes directly via the NdbApi will cause inconsistencies
 * if they were originally created from a MySQL Cluster.
39 40
 * 
 * This class supports schema data enquiries such as:
41 42 43 44 45 46 47
 * -# Enquiries about tables
 *    (Dictionary::getTable, Table::getNoOfColumns, 
 *    Table::getPrimaryKey, and Table::getNoOfPrimaryKeys)
 * -# Enquiries about indexes
 *    (Dictionary::getIndex, Index::getNoOfColumns, 
 *    and Index::getColumn)
 *
48 49 50 51 52 53 54
 * This class supports schema data definition such as:
 * -# Creating tables (Dictionary::createTable) and table columns
 * -# Dropping tables (Dictionary::dropTable)
 * -# Creating secondary indexes (Dictionary::createIndex)
 * -# Dropping secondary indexes (Dictionary::dropIndex)
 *
 * NdbDictionary has several help (inner) classes to support this:
55
 * -# NdbDictionary::Dictionary the dictionary handling dictionary objects
56 57 58
 * -# NdbDictionary::Table for creating tables
 * -# NdbDictionary::Column for creating table columns
 * -# NdbDictionary::Index for creating secondary indexes
59
 *
unknown's avatar
unknown committed
60
 * See @ref ndbapi_simple_index.cpp for details of usage.
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
 */
class NdbDictionary {
public:
  /**
   * @class Object
   * @brief Meta information about a database object (a table, index, etc)
   */
  class Object {
  public:
    /**
     * Status of object
     */
    enum Status {
      New,                    ///< The object only exist in memory and 
                              ///< has not been created in the NDB Kernel
      Changed,                ///< The object has been modified in memory 
                              ///< and has to be commited in NDB Kernel for 
                              ///< changes to take effect
      Retrieved               ///< The object exist and has been read 
                              ///< into main memory from NDB Kernel
    };

    /**
     * Get status of object
     */
    virtual Status getObjectStatus() const = 0;

    /**
     * Get version of object
     */
    virtual int getObjectVersion() const = 0;

    /**
     * Object type
     */
    enum Type {
      TypeUndefined = 0,      ///< Undefined
      SystemTable = 1,        ///< System table
      UserTable = 2,          ///< User table (may be temporary)
      UniqueHashIndex = 3,    ///< Unique un-ordered hash index
      OrderedIndex = 6,       ///< Non-unique ordered index
      HashIndexTrigger = 7,   ///< Index maintenance, internal
      IndexTrigger = 8,       ///< Index maintenance, internal
      SubscriptionTrigger = 9,///< Backup or replication, internal
      ReadOnlyConstraint = 10 ///< Trigger, internal
    };

    /**
     * Object state
     */
    enum State {
      StateUndefined = 0,     ///< Undefined
      StateOffline = 1,       ///< Offline, not usable
      StateBuilding = 2,      ///< Building, not yet usable
      StateDropping = 3,      ///< Offlining or dropping, not usable
      StateOnline = 4,        ///< Online, usable
      StateBroken = 9         ///< Broken, should be dropped and re-created
    };

    /**
     * Object store
     */
    enum Store {
      StoreUndefined = 0,     ///< Undefined
      StoreTemporary = 1,     ///< Object or data deleted on system restart
      StorePermanent = 2      ///< Permanent. logged to disk
    };

    /**
     * Type of fragmentation.
     *
     * This parameter specifies how data in the table or index will
     * be distributed among the db nodes in the cluster.<br>
     * The bigger the table the more number of fragments should be used.
     * Note that all replicas count as same "fragment".<br>
     * For a table, default is FragAllMedium.  For a unique hash index,
     * default is taken from underlying table and cannot currently
     * be changed.
     */
    enum FragmentType { 
      FragUndefined = 0,      ///< Fragmentation type undefined or default
      FragSingle = 1,         ///< Only one fragment
      FragAllSmall = 2,       ///< One fragment per node group
      FragAllMedium = 3,      ///< Default value. Two fragments per node group.
      FragAllLarge = 4        ///< Eight fragments per node group.
    };
  };
unknown's avatar
unknown committed
148 149

  class Table; // forward declaration
150 151 152
  
  /**
   * @class Column
unknown's avatar
unknown committed
153
   * @brief Represents a column in an NDB Cluster table
154
   *
unknown's avatar
unknown committed
155
   * Each column has a type. The type of a column is determined by a number 
156 157 158 159
   * of type specifiers.
   * The type specifiers are:
   * - Builtin type
   * - Array length or max length
unknown's avatar
unknown committed
160 161 162 163 164 165 166
   * - Precision and scale (not used yet)
   * - Character set for string types
   * - Inline and part sizes for blobs
   *
   * Types in general correspond to MySQL types and their variants.
   * Data formats are same as in MySQL.  NDB API provides no support for
   * constructing such formats.  NDB kernel checks them however.
167 168 169 170 171 172 173
   */
  class Column {
  public:
    /**
     * The builtin column types
     */
    enum Type {
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
      Undefined = NDB_TYPE_UNDEFINED,   ///< Undefined 
      Tinyint = NDB_TYPE_TINYINT,       ///< 8 bit. 1 byte signed integer, can be used in array
      Tinyunsigned = NDB_TYPE_TINYUNSIGNED,  ///< 8 bit. 1 byte unsigned integer, can be used in array
      Smallint = NDB_TYPE_SMALLINT,      ///< 16 bit. 2 byte signed integer, can be used in array
      Smallunsigned = NDB_TYPE_SMALLUNSIGNED, ///< 16 bit. 2 byte unsigned integer, can be used in array
      Mediumint = NDB_TYPE_MEDIUMINT,     ///< 24 bit. 3 byte signed integer, can be used in array
      Mediumunsigned = NDB_TYPE_MEDIUMUNSIGNED,///< 24 bit. 3 byte unsigned integer, can be used in array
      Int = NDB_TYPE_INT,           ///< 32 bit. 4 byte signed integer, can be used in array
      Unsigned = NDB_TYPE_UNSIGNED,      ///< 32 bit. 4 byte unsigned integer, can be used in array
      Bigint = NDB_TYPE_BIGINT,        ///< 64 bit. 8 byte signed integer, can be used in array
      Bigunsigned = NDB_TYPE_BIGUNSIGNED,   ///< 64 Bit. 8 byte signed integer, can be used in array
      Float = NDB_TYPE_FLOAT,         ///< 32-bit float. 4 bytes float, can be used in array
      Double = NDB_TYPE_DOUBLE,        ///< 64-bit float. 8 byte float, can be used in array
      Decimal = NDB_TYPE_DECIMAL,       ///< Precision, Scale are applicable
      Char = NDB_TYPE_CHAR,          ///< Len. A fixed array of 1-byte chars
unknown's avatar
unknown committed
189
      Varchar = NDB_TYPE_VARCHAR,       ///< Length bytes: 1, Max: 255
190
      Binary = NDB_TYPE_BINARY,        ///< Len
unknown's avatar
unknown committed
191
      Varbinary = NDB_TYPE_VARBINARY,     ///< Length bytes: 1, Max: 255
192
      Datetime = NDB_TYPE_DATETIME,    ///< Precision down to 1 sec (sizeof(Datetime) == 8 bytes )
unknown's avatar
Merge  
unknown committed
193
      Date = NDB_TYPE_DATE,            ///< Precision down to 1 day(sizeof(Date) == 4 bytes )
194
      Blob = NDB_TYPE_BLOB,        ///< Binary large object (see NdbBlob)
unknown's avatar
unknown committed
195 196 197
      Text = NDB_TYPE_TEXT,         ///< Text blob
      Bit = NDB_TYPE_BIT,          ///< Bit, length specifies no of bits
      Longvarchar = NDB_TYPE_LONG_VARCHAR,  ///< Length bytes: 2, little-endian
unknown's avatar
Merge  
unknown committed
198
      Longvarbinary = NDB_TYPE_LONG_VARBINARY, ///< Length bytes: 2, little-endian
unknown's avatar
Merge  
unknown committed
199
      Time = NDB_TYPE_TIME         ///< Time without date
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
    };

    /** 
     * @name General 
     * @{
     */
    
    /**
     * Get name of column
     * @return  Name of the column
     */
    const char* getName() const;

    /**
     * Get if the column is nullable or not
     */
    bool getNullable() const;
    
    /**
     * Check if column is part of primary key
     */
    bool getPrimaryKey() const;

    /**
     *  Get number of column (horizontal position within table)
     */
    int getColumnNo() const;

    /**
     * Check if column is equal to some other column
     * @param  column  Column to compare with
     * @return  true if column is equal to some other column otherwise false.
     */
    bool equal(const Column& column) const;

235

236 237
    /** @} *******************************************************************/
    /** 
238
     * @name Get Type Specifiers
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
     * @{
     */

    /**
     * Get type of column
     */
    Type getType() const;

    /**
     * Get precision of column.
     * @note Only applicable for builtin type Decimal
     */
    int getPrecision() const;

    /**
     * Get scale of column.
     * @note Only applicable for builtin type Decimal
     */
    int getScale() const;

    /**
     * Get length for column
     * Array length for column or max length for variable length arrays.
     */
    int getLength() const;
unknown's avatar
unknown committed
264

unknown's avatar
unknown committed
265
    /**
266
     * For Char or Varchar or Text, get MySQL CHARSET_INFO.  This
unknown's avatar
unknown committed
267 268 269 270 271
     * specifies both character set and collation.  See get_charset()
     * etc in MySQL.  (The cs is not "const" in MySQL).
     */
    CHARSET_INFO* getCharset() const;

272

unknown's avatar
unknown committed
273
    /**
274
     * For blob, get "inline size" i.e. number of initial bytes
unknown's avatar
unknown committed
275 276 277
     * to store in table's blob attribute.  This part is normally in
     * main memory and can be indexed and interpreted.
     */
278
    int getInlineSize() const;
unknown's avatar
unknown committed
279 280

    /**
281
     * For blob, get "part size" i.e. number of bytes to store in
unknown's avatar
unknown committed
282 283
     * each tuple of the "blob table".  Can be set to zero to omit parts
     * and to allow only inline bytes ("tinyblob").
unknown's avatar
unknown committed
284
     */
285
    int getPartSize() const;
unknown's avatar
unknown committed
286 287 288 289 290

    /**
     * For blob, set or get "stripe size" i.e. number of consecutive
     * <em>parts</em> to store in each node group.
     */
291
    int getStripeSize() const;
unknown's avatar
unknown committed
292

unknown's avatar
unknown committed
293 294 295
    /**
     * Get size of element
     */
unknown's avatar
unknown committed
296
    int getSize() const;
unknown's avatar
unknown committed
297

298
    /** 
299
     * Check if column is part of partition key
300
     *
301
     * A <em>partition key</em> is a set of attributes which are used
302
     * to distribute the tuples onto the NDB nodes.
303
     * The partition key uses the NDB Cluster hashing function.
304 305
     *
     * An example where this is useful is TPC-C where it might be
306
     * good to use the warehouse id and district id as the partition key. 
307 308 309 310 311 312
     * This would place all data for a specific district and warehouse 
     * in the same database node.
     *
     * Locally in the fragments the full primary key 
     * will still be used with the hashing algorithm.
     *
313
     * @return  true then the column is part of 
314
     *                 the partition key.
315
     */
316 317 318 319
    bool getPartitionKey() const;
#ifndef DOXYGEN_SHOULD_SKIP_DEPRECATED
    inline bool getDistributionKey() const { return getPartitionKey(); };
#endif
320 321 322 323 324 325 326 327 328 329 330 331

    /** @} *******************************************************************/


    /** 
     * @name Column creation
     * @{
     *
     * These operations should normally not be performed in an NbdApi program
     * as results will not be visable in the MySQL Server
     * 
     */
332 333

    /**
334 335
     * Constructor
     * @param   name   Name of column
336
     */
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
    Column(const char * name = "");
    /**
     * Copy constructor
     * @param  column  Column to be copied
     */
    Column(const Column& column); 
    ~Column();

    /**
     * Set name of column
     * @param  name  Name of the column
     */
    void setName(const char * name);

    /**
     * Set whether column is nullable or not
     */
    void setNullable(bool);

    /**
     * Set that column is part of primary key
     */
    void setPrimaryKey(bool);

    /**
     * Set type of column
     * @param  type  Type of column
     *
     * @note setType resets <em>all</em> column attributes
     *       to (type dependent) defaults and should be the first
     *       method to call.  Default type is Unsigned.
     */
    void setType(Type type);

    /**
     * Set precision of column.
     * @note Only applicable for builtin type Decimal
     */
    void setPrecision(int);

    /**
     * Set scale of column.
     * @note Only applicable for builtin type Decimal
     */
    void setScale(int);

    /**
     * Set length for column
     * Array length for column or max length for variable length arrays.
     */
    void setLength(int length);

    /**
     * For Char or Varchar or Text, get MySQL CHARSET_INFO.  This
     * specifies both character set and collation.  See get_charset()
     * etc in MySQL.  (The cs is not "const" in MySQL).
     */
    void setCharset(CHARSET_INFO* cs);

    /**
     * For blob, get "inline size" i.e. number of initial bytes
     * to store in table's blob attribute.  This part is normally in
     * main memory and can be indexed and interpreted.
     */
    void setInlineSize(int size);

    /**
     * For blob, get "part size" i.e. number of bytes to store in
     * each tuple of the "blob table".  Can be set to zero to omit parts
     * and to allow only inline bytes ("tinyblob").
     */
    void setPartSize(int size);

    /**
     * For blob, get "stripe size" i.e. number of consecutive
     * <em>parts</em> to store in each node group.
     */
    void setStripeSize(int size);

    /** 
417 418
     * Set partition key
     * @see getPartitionKey
419 420
     *
     * @param  enable  If set to true, then the column will be part of 
421
     *                 the partition key.
422
     */
423 424 425 426 427
    void setPartitionKey(bool enable);
#ifndef DOXYGEN_SHOULD_SKIP_DEPRECATED
    inline void setDistributionKey(bool enable)
    { setPartitionKey(enable); };
#endif
428

429
    /** @} *******************************************************************/
unknown's avatar
unknown committed
430

431
#ifndef DOXYGEN_SHOULD_SKIP_INTERNAL
unknown's avatar
unknown committed
432 433
    const Table * getBlobTable() const;

unknown's avatar
unknown committed
434
    void setAutoIncrement(bool);
435 436 437 438
    bool getAutoIncrement() const;
    void setAutoIncrementInitialValue(Uint64 val);
    void setDefaultValue(const char*);   
    const char* getDefaultValue() const;
unknown's avatar
unknown committed
439 440

    static const Column * FRAGMENT;
441
    static const Column * FRAGMENT_MEMORY;
unknown's avatar
unknown committed
442 443
    static const Column * ROW_COUNT;
    static const Column * COMMIT_COUNT;
444
    static const Column * ROW_SIZE;
445
    static const Column * RANGE_NO;
446 447 448
#endif
    
  private:
449
#ifndef DOXYGEN_SHOULD_SKIP_INTERNAL
unknown's avatar
unknown committed
450
    friend class NdbRecAttr;
451
    friend class NdbColumnImpl;
452
#endif
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
    class NdbColumnImpl & m_impl;
    Column(NdbColumnImpl&);
    Column& operator=(const Column&);
  };

#ifndef DOXYGEN_SHOULD_SKIP_INTERNAL
  /**
   * ???
   */
  typedef Column Attribute;
#endif
  
  /**
   * @brief Represents a table in NDB Cluster
   *
   * <em>TableSize</em><br>
   * When calculating the data storage one should add the size of all 
   * attributes (each attributeconsumes at least 4 bytes) and also an overhead
   * of 12 byte. Variable size attributes (not supported yet) will have a 
   * size of 12 bytes plus the actual data storage parts where there is an 
   * additional overhead based on the size of the variable part.<br>
   * An example table with 5 attributes: 
   * one 64 bit attribute, one 32 bit attribute, 
   * two 16 bit attributes and one array of 64 8 bits. 
   * This table will consume 
   * 12 (overhead) + 8 + 4 + 2*4 (4 is minimum) + 64 = 96 bytes per record.
   * Additionally an overhead of about 2 % as page headers and waste should 
   * be allocated. Thus, 1 million records should consume 96 MBytes
   * plus the overhead 2 MByte and rounded up to 100 000 kBytes.<br>
   *
   */
  class Table : public Object {
  public:
    /** 
     * @name General
     * @{
     */

    /**
     * Get table name
     */
    const char * getName() const;

    /**
     * Get table id
     */ 
    int getTableId() const;
    
    /**
     * Get column definition via name.
     * @return null if none existing name
     */
    const Column* getColumn(const char * name) const;
    
unknown's avatar
unknown committed
507 508 509 510 511 512 513 514 515 516 517 518
    /**
     * Get column definition via index in table.
     * @return null if none existing name
     */
    Column* getColumn(const int attributeId);

    /**
     * Get column definition via name.
     * @return null if none existing name
     */
    Column* getColumn(const char * name);
    
519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606
    /**
     * Get column definition via index in table.
     * @return null if none existing name
     */
    const Column* getColumn(const int attributeId) const;
    
    /** @} *******************************************************************/
    /**
     * @name Storage
     * @{
     */

    /**
     * If set to false, then the table is a temporary 
     * table and is not logged to disk.
     *
     * In case of a system restart the table will still
     * be defined and exist but will be empty. 
     * Thus no checkpointing and no logging is performed on the table.
     *
     * The default value is true and indicates a normal table 
     * with full checkpointing and logging activated.
     */
    bool getLogging() const;

    /**
     * Get fragmentation type
     */
    FragmentType getFragmentType() const;
    
    /**
     * Get KValue (Hash parameter.)
     * Only allowed value is 6.
     * Later implementations might add flexibility in this parameter.
     */
    int getKValue() const;

    /**
     * Get MinLoadFactor  (Hash parameter.)
     * This value specifies the load factor when starting to shrink 
     * the hash table. 
     * It must be smaller than MaxLoadFactor.
     * Both these factors are given in percentage.
     */
    int getMinLoadFactor() const;

    /**
     * Get MaxLoadFactor  (Hash parameter.)
     * This value specifies the load factor when starting to split 
     * the containers in the local hash tables. 
     * 100 is the maximum which will optimize memory usage.
     * A lower figure will store less information in each container and thus
     * find the key faster but consume more memory.
     */
    int getMaxLoadFactor() const;

    /** @} *******************************************************************/
    /** 
     * @name Other
     * @{
     */
    
    /**
     * Get number of columns in the table
     */
    int getNoOfColumns() const;
    
    /**
     * Get number of primary keys in the table
     */
    int getNoOfPrimaryKeys() const;

    /**
     * Get name of primary key 
     */
    const char* getPrimaryKey(int no) const;
    
    /**
     * Check if table is equal to some other table
     */
    bool equal(const Table&) const;

    /**
     * Get frm file stored with this table
     */
    const void* getFrmData() const;
    Uint32 getFrmLength() const;

607
    /** @} *******************************************************************/
608

609 610 611 612 613 614 615
    /** 
     * @name Table creation
     * @{
     *
     * These methods should normally not be used in an application as
     * the result is not accessible from the MySQL Server
     *
616 617 618
     */

    /**
619 620
     * Constructor
     * @param  name   Name of table
621
     */
622 623 624 625 626 627 628 629 630 631 632 633 634
    Table(const char * name = "");

    /** 
     * Copy constructor 
     * @param  table  Table to be copied
     */
    Table(const Table& table); 
    virtual ~Table();
    
    /**
     * Assignment operator, deep copy
     * @param  table  Table to be copied
     */
unknown's avatar
unknown committed
635
    Table& operator=(const Table& table);
636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688

    /**
     * Name of table
     * @param  name  Name of table
     */
    void setName(const char * name);

    /**
     * Add a column definition to a table
     * @note creates a copy
     */
    void addColumn(const Column &);
    
    /**
     * @see NdbDictionary::Table::getLogging.
     */
    void setLogging(bool); 
   
    /**
     * Set fragmentation type
     */
    void setFragmentType(FragmentType);

    /**
     * Set KValue (Hash parameter.)
     * Only allowed value is 6.
     * Later implementations might add flexibility in this parameter.
     */
    void setKValue(int kValue);
    
    /**
     * Set MinLoadFactor  (Hash parameter.)
     * This value specifies the load factor when starting to shrink 
     * the hash table. 
     * It must be smaller than MaxLoadFactor.
     * Both these factors are given in percentage.
     */
    void setMinLoadFactor(int);

    /**
     * Set MaxLoadFactor  (Hash parameter.)
     * This value specifies the load factor when starting to split 
     * the containers in the local hash tables. 
     * 100 is the maximum which will optimize memory usage.
     * A lower figure will store less information in each container and thus
     * find the key faster but consume more memory.
     */
    void setMaxLoadFactor(int);

    /**
     * Get table object type
     */
    Object::Type getObjectType() const;
689 690 691 692 693 694 695 696 697 698 699

    /**
     * Get object status
     */
    virtual Object::Status getObjectStatus() const;

    /**
     * Get object version
     */
    virtual int getObjectVersion() const;

700 701 702 703 704 705 706 707 708 709
    /**
     * Set frm file to store with this table
     */ 
    void setFrm(const void* data, Uint32 len);

    /**
     * Set table object type
     */
    void setObjectType(Object::Type type);

710 711
    /** @} *******************************************************************/

712
#ifndef DOXYGEN_SHOULD_SKIP_INTERNAL
713 714 715 716 717 718 719 720
    void setStoredTable(bool x) { setLogging(x); }
    bool getStoredTable() const { return getLogging(); }

    int getRowSizeInBytes() const ;
    int createTableInDb(Ndb*, bool existingEqualIsOk = true) const ;
#endif

  private:
721
#ifndef DOXYGEN_SHOULD_SKIP_INTERNAL
722
    friend class NdbTableImpl;
723
#endif
724 725 726 727 728 729 730 731 732 733
    class NdbTableImpl & m_impl;
    Table(NdbTableImpl&);
  };
  
  /**
   * @class Index
   * @brief Represents an index in an NDB Cluster
   */
  class Index : public Object {
  public:
734 735 736 737
    
    /** 
     * @name Getting Index properties
     * @{
738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754
     */

    /**
     * Get the name of an index
     */
    const char * getName() const;
    
    /**
     * Get the name of the table being indexed
     */
    const char * getTable() const;
    
    /**
     * Get the number of columns in the index
     */
    unsigned getNoOfColumns() const;

755
#ifndef DOXYGEN_SHOULD_SKIP_DEPRECATED
756 757 758 759 760
    /**
     * Get the number of columns in the index
     * Depricated, use getNoOfColumns instead.
     */
    int getNoOfIndexColumns() const;
761
#endif
762 763 764 765

    /**
     * Get a specific column in the index
     */
unknown's avatar
unknown committed
766
    const Column * getColumn(unsigned no) const ;
767

768
#ifndef DOXYGEN_SHOULD_SKIP_DEPRECATED
769 770 771 772 773
    /**
     * Get a specific column name in the index
     * Depricated, use getColumn instead.
     */
    const char * getIndexColumn(int no) const ;
774
#endif
775

776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838
    /**
     * Represents type of index
     */
    enum Type {
      Undefined = 0,          ///< Undefined object type (initial value)
      UniqueHashIndex = 3,    ///< Unique un-ordered hash index 
                              ///< (only one currently supported)
      OrderedIndex = 6        ///< Non-unique ordered index
    };

    /**
     * Get index type of the index
     */
    Type getType() const;
    
    /**
     * Check if index is set to be stored on disk
     *
     * @return if true then logging id enabled
     *
     * @note Non-logged indexes are rebuilt at system restart.
     * @note Ordered index does not currently support logging.
     */
    bool getLogging() const;

    /**
     * Get object status
     */
    virtual Object::Status getObjectStatus() const;

    /**
     * Get object version
     */
    virtual int getObjectVersion() const;

    /** @} *******************************************************************/

    /** 
     * @name Index creation
     * @{
     *
     * These methods should normally not be used in an application as
     * the result will not be visible from the MySQL Server
     *
     */

    /**
     *  Constructor
     *  @param  name  Name of index
     */
    Index(const char * name = "");
    virtual ~Index();

    /**
     * Set the name of an index
     */
    void setName(const char * name);

    /**
     * Define the name of the table to be indexed
     */
    void setTable(const char * name);

839 840 841 842 843 844 845 846 847 848 849 850 851 852
    /**
     * Add a column to the index definition
     * Note that the order of columns will be in
     * the order they are added (only matters for ordered indexes).
     */
    void addColumn(const Column & c);

    /**
     * Add a column name to the index definition
     * Note that the order of indexes will be in
     * the order they are added (only matters for ordered indexes).
     */
    void addColumnName(const char * name);

853
#ifndef DOXYGEN_SHOULD_SKIP_DEPRECATED
854 855 856 857 858 859 860
    /**
     * Add a column name to the index definition
     * Note that the order of indexes will be in
     * the order they are added (only matters for ordered indexes).
     * Depricated, use addColumnName instead.
     */
    void addIndexColumn(const char * name);
861
#endif
862 863 864 865 866 867 868 869

    /**
     * Add several column names to the index definition
     * Note that the order of indexes will be in
     * the order they are added (only matters for ordered indexes).
     */
    void addColumnNames(unsigned noOfNames, const char ** names);

870
#ifndef DOXYGEN_SHOULD_SKIP_DEPRECATED
871 872 873 874 875 876 877
    /**
     * Add several column names to the index definition
     * Note that the order of indexes will be in
     * the order they are added (only matters for ordered indexes).
     * Depricated, use addColumnNames instead.
     */
    void addIndexColumns(int noOfNames, const char ** names);
878
#endif
879 880 881 882 883 884 885 886 887 888 889

    /**
     * Set index type of the index
     */
    void setType(Type type);

    /**
     * Enable/Disable index storage on disk
     *
     * @param enable  If enable is set to true, then logging becomes enabled
     *
890
     * @see NdbDictionary::Index::getLogging
891 892 893 894 895 896 897 898
     */
    void setLogging(bool enable); 

#ifndef DOXYGEN_SHOULD_SKIP_DEPRECATED
    void setStoredIndex(bool x) { setLogging(x); }
    bool getStoredIndex() const { return getLogging(); }
#endif
    
899
    /** @} *******************************************************************/
900 901

  private:
902
#ifndef DOXYGEN_SHOULD_SKIP_INTERNAL
903
    friend class NdbIndexImpl;
904
#endif
905 906 907 908 909 910 911 912 913 914
    class NdbIndexImpl & m_impl;
    Index(NdbIndexImpl&);
  };

  /**
   * @brief Represents an Event in NDB Cluster
   *
   */
  class Event : public Object  {
  public:
915 916 917
    /**
     * Specifies the type of database operations an Event listens to
     */
918 919 920 921 922 923 924
    enum TableEvent { 
      TE_INSERT=1, ///< Insert event on table
      TE_DELETE=2, ///< Delete event on table
      TE_UPDATE=4, ///< Update event on table
      TE_ALL=7     ///< Any/all event on table (not relevant when 
                   ///< events are received)
    };
925 926 927 928
    /**
     *  Specifies the durability of an event
     * (future version may supply other types)
     */
929
    enum EventDurability { 
930 931 932 933
      ED_UNDEFINED
#ifndef DOXYGEN_SHOULD_SKIP_INTERNAL
      = 0
#endif
934
#if 0 // not supported
935
      ,ED_SESSION = 1, 
936 937 938
      // Only this API can use it
      // and it's deleted after api has disconnected or ndb has restarted
      
939
      ED_TEMPORARY = 2
940 941
      // All API's can use it,
      // But's its removed when ndb is restarted
942
#endif
943 944
      ,ED_PERMANENT    ///< All API's can use it.
                       ///< It's still defined after a cluster system restart
945 946 947
#ifndef DOXYGEN_SHOULD_SKIP_INTERNAL
      = 3
#endif
948
    };
949

unknown's avatar
unknown committed
950
    /**
951 952 953
     *  Constructor
     *  @param  name  Name of event
     */
954
    Event(const char *name);
unknown's avatar
unknown committed
955
    /**
956 957 958 959 960
     *  Constructor
     *  @param  name  Name of event
     *  @param  table Reference retrieved from NdbDictionary
     */
    Event(const char *name, const NdbDictionary::Table& table);
961
    virtual ~Event();
unknown's avatar
unknown committed
962
    /**
963
     * Set unique identifier for the event
unknown's avatar
unknown committed
964 965
     */
    void setName(const char *name);
966 967 968
    /**
     * Get unique identifier for the event
     */
unknown's avatar
unknown committed
969
    const char *getName() const;
970 971 972 973 974 975 976 977 978 979
    /**
     * Define table on which events should be detected
     *
     * @note calling this method will default to detection
     *       of events on all columns. Calling subsequent
     *       addEventColumn calls will override this.
     *
     * @param table reference retrieved from NdbDictionary
     */
    void setTable(const NdbDictionary::Table& table);
unknown's avatar
unknown committed
980 981
    /**
     * Set table for which events should be detected
982
     *
983
     * @note preferred way is using setTable(const NdbDictionary::Table&)
984
     *       or constructor with table object parameter
unknown's avatar
unknown committed
985 986
     */
    void setTable(const char *tableName);
unknown's avatar
unknown committed
987 988 989 990 991 992
    /**
     * Get table name for events
     *
     * @return table name
     */
    const char* getTableName() const;
unknown's avatar
unknown committed
993 994 995 996 997
    /**
     * Add type of event that should be detected
     */
    void addTableEvent(const TableEvent te);
    /**
unknown's avatar
unknown committed
998 999 1000 1001 1002
     * Set durability of the event
     */
    void setDurability(EventDurability);
    /**
     * Get durability of the event
unknown's avatar
unknown committed
1003
     */
unknown's avatar
unknown committed
1004
    EventDurability getDurability() const;
unknown's avatar
unknown committed
1005
#ifndef DOXYGEN_SHOULD_SKIP_INTERNAL
1006
    void addColumn(const Column &c);
unknown's avatar
unknown committed
1007 1008 1009 1010 1011 1012 1013 1014
#endif
    /**
     * Add a column on which events should be detected
     *
     * @param attrId Column id
     *
     * @note errors will mot be detected until createEvent() is called
     */
1015
    void addEventColumn(unsigned attrId);
unknown's avatar
unknown committed
1016 1017 1018 1019 1020
    /**
     * Add a column on which events should be detected
     *
     * @param columnName Column name
     *
1021
     * @note errors will not be detected until createEvent() is called
unknown's avatar
unknown committed
1022
     */
1023
    void addEventColumn(const char * columnName);
unknown's avatar
unknown committed
1024 1025 1026 1027 1028 1029 1030 1031 1032
    /**
     * Add several columns on which events should be detected
     *
     * @param n Number of columns
     * @param columnNames Column names
     *
     * @note errors will mot be detected until 
     *       NdbDictionary::Dictionary::createEvent() is called
     */
1033 1034
    void addEventColumns(int n, const char ** columnNames);

unknown's avatar
unknown committed
1035 1036 1037 1038 1039 1040 1041
    /**
     * Get no of columns defined in an Event
     *
     * @return Number of columns, -1 on error
     */
    int getNoOfEventColumns() const;

1042 1043 1044 1045 1046 1047 1048 1049 1050 1051
    /**
     * Get object status
     */
    virtual Object::Status getObjectStatus() const;

    /**
     * Get object version
     */
    virtual int getObjectVersion() const;

unknown's avatar
unknown committed
1052
#ifndef DOXYGEN_SHOULD_SKIP_INTERNAL
1053
    void print();
unknown's avatar
unknown committed
1054
#endif
1055 1056

  private:
1057
#ifndef DOXYGEN_SHOULD_SKIP_INTERNAL
1058 1059
    friend class NdbEventImpl;
    friend class NdbEventOperationImpl;
1060
#endif
1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122
    class NdbEventImpl & m_impl;
    Event(NdbEventImpl&);
  };

  /**
   * @class Dictionary
   * @brief Dictionary for defining and retreiving meta data
   */
  class Dictionary {
  public:
    /**
     * @class List
     * @brief Structure for retrieving lists of object names
     */
    struct List {
      /**
       * @struct  Element
       * @brief   Object to be stored in an NdbDictionary::Dictionary::List
       */
      struct Element {
	unsigned id;            ///< Id of object
        Object::Type type;      ///< Type of object
        Object::State state;    ///< State of object
        Object::Store store;    ///< How object is stored
	char * database;        ///< In what database the object resides 
	char * schema;          ///< What schema the object is defined in
	char * name;            ///< Name of object
        Element() :
          id(0),
          type(Object::TypeUndefined),
          state(Object::StateUndefined),
          store(Object::StoreUndefined),
	  database(0),
	  schema(0),
          name(0) {
        }
      };
      unsigned count;           ///< Number of elements in list
      Element * elements;       ///< Pointer to array of elements
      List() : count(0), elements(0) {}
      ~List() {
        if (elements != 0) {
          for (unsigned i = 0; i < count; i++) {
            delete[] elements[i].database;
            delete[] elements[i].schema;
            delete[] elements[i].name;
            elements[i].name = 0;
          }
          delete[] elements;
          count = 0;
          elements = 0;
        }
      }
    };

    /** 
     * @name General
     * @{
     */

    /**
     * Fetch list of all objects, optionally restricted to given type.
unknown's avatar
unknown committed
1123 1124 1125 1126 1127 1128 1129
     *
     * @param list   List of objects returned in the dictionary
     * @param type   Restrict returned list to only contain objects of
     *               this type
     *
     * @return       -1 if error.
     *
1130 1131
     */
    int listObjects(List & list, Object::Type type = Object::TypeUndefined);
1132
    int listObjects(List & list,
1133
		    Object::Type type = Object::TypeUndefined) const;
1134 1135 1136 1137 1138 1139 1140 1141 1142

    /**
     * Get the latest error
     *
     * @return   Error object.
     */			     
    const struct NdbError & getNdbError() const;

    /** @} *******************************************************************/
1143

1144
    /** 
1145
     * @name Retrieving references to Tables and Indexes
1146 1147 1148
     * @{
     */

1149 1150 1151 1152 1153
    /**
     * Get table with given name, NULL if undefined
     * @param name   Name of table to get
     * @return table if successful otherwise NULL.
     */
unknown's avatar
unknown committed
1154
    const Table * getTable(const char * name) const;
1155 1156 1157 1158 1159 1160 1161 1162

    /**
     * Get index with given name, NULL if undefined
     * @param indexName  Name of index to get.
     * @param tableName  Name of table that index belongs to.
     * @return  index if successful, otherwise 0.
     */
    const Index * getIndex(const char * indexName,
unknown's avatar
unknown committed
1163
			   const char * tableName) const;
1164 1165 1166 1167 1168 1169 1170 1171

    /**
     * Fetch list of indexes of given table.
     * @param list  Reference to list where to store the listed indexes
     * @param tableName  Name of table that index belongs to.
     * @return  0 if successful, otherwise -1
     */
    int listIndexes(List & list, const char * tableName);
1172
    int listIndexes(List & list, const char * tableName) const;
1173 1174 1175 1176 1177 1178 1179 1180 1181

    /** @} *******************************************************************/
    /** 
     * @name Events
     * @{
     */
    
    /**
     * Create event given defined Event instance
unknown's avatar
unknown committed
1182
     * @param event Event to create
1183 1184
     * @return 0 if successful otherwise -1.
     */
unknown's avatar
unknown committed
1185
    int createEvent(const Event &event);
1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210

    /**
     * Drop event with given name
     * @param eventName  Name of event to drop.
     * @return 0 if successful otherwise -1.
     */
    int dropEvent(const char * eventName);
    
    /**
     * Get event with given name.
     * @param eventName  Name of event to get.
     * @return an Event if successful, otherwise NULL.
     */
    const Event * getEvent(const char * eventName);

    /** @} *******************************************************************/

    /** 
     * @name Table creation
     * @{
     *
     * These methods should normally not be used in an application as
     * the result will not be visible from the MySQL Server
     */

1211 1212
    /**
     * Create defined table given defined Table instance
unknown's avatar
unknown committed
1213
     * @param table Table to create
1214 1215
     * @return 0 if successful otherwise -1.
     */
unknown's avatar
unknown committed
1216
    int createTable(const Table &table);
1217 1218 1219

    /**
     * Drop table given retrieved Table instance
unknown's avatar
unknown committed
1220
     * @param table Table to drop
1221 1222
     * @return 0 if successful otherwise -1.
     */
unknown's avatar
unknown committed
1223
    int dropTable(Table & table);
1224 1225 1226 1227 1228 1229 1230 1231

    /**
     * Drop table given table name
     * @param name   Name of table to drop 
     * @return 0 if successful otherwise -1.
     */
    int dropTable(const char * name);
    
1232
#ifndef DOXYGEN_SHOULD_SKIP_INTERNAL
1233 1234
    /**
     * Alter defined table given defined Table instance
unknown's avatar
unknown committed
1235
     * @param table Table to alter
1236 1237 1238 1239
     * @return  -2 (incompatible version) <br>
     *          -1 general error          <br>
     *           0 success                 
     */
unknown's avatar
unknown committed
1240
    int alterTable(const Table &table);
1241 1242 1243 1244 1245 1246 1247 1248 1249

    /**
     * Invalidate cached table object
     * @param name  Name of table to invalidate
     */
    void invalidateTable(const char * name);
#endif

    /**
1250
     * Remove table from local cache
1251 1252
     */
    void removeCachedTable(const char * table);
1253 1254 1255
    /**
     * Remove index from local cache
     */
1256 1257 1258 1259 1260
    void removeCachedIndex(const char * index, const char * table);

    
    /** @} *******************************************************************/
    /** 
1261
     * @name Index creation
1262
     * @{
1263 1264 1265 1266
     *
     * These methods should normally not be used in an application as
     * the result will not be visible from the MySQL Server
     *
1267 1268 1269 1270
     */
    
    /**
     * Create index given defined Index instance
unknown's avatar
unknown committed
1271
     * @param index Index to create
1272 1273
     * @return 0 if successful otherwise -1.
     */
unknown's avatar
unknown committed
1274
    int createIndex(const Index &index);
1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299

    /**
     * Drop index with given name
     * @param indexName  Name of index to drop.
     * @param tableName  Name of table that index belongs to.
     * @return 0 if successful otherwise -1.
     */
    int dropIndex(const char * indexName,
		  const char * tableName);
    
#ifndef DOXYGEN_SHOULD_SKIP_INTERNAL
    /**
     * Invalidate cached index object
     */
    void invalidateIndex(const char * indexName,
                         const char * tableName);
#endif

    /** @} *******************************************************************/

  protected:
    Dictionary(Ndb & ndb);
    ~Dictionary();
    
  private:
1300
#ifndef DOXYGEN_SHOULD_SKIP_INTERNAL
1301 1302
    friend class NdbDictionaryImpl;
    friend class UtilTransactions;
unknown's avatar
unknown committed
1303
    friend class NdbBlob;
1304
#endif
1305 1306 1307
    class NdbDictionaryImpl & m_impl;
    Dictionary(NdbDictionaryImpl&);
    const Table * getIndexTable(const char * indexName, 
unknown's avatar
unknown committed
1308
				const char * tableName) const;
1309
  public:
unknown's avatar
unknown committed
1310
#ifndef DOXYGEN_SHOULD_SKIP_INTERNAL
unknown's avatar
unknown committed
1311
    const Table * getTable(const char * name, void **data) const;
1312
    void set_local_table_data_size(unsigned sz);
unknown's avatar
unknown committed
1313
#endif
1314 1315 1316
  };
};

unknown's avatar
unknown committed
1317
class NdbOut& operator <<(class NdbOut& out, const NdbDictionary::Column& col);
unknown's avatar
unknown committed
1318

1319
#endif