/*
  Message format for tables.
*/
package drizzled.message;
option optimize_for = SPEED;

option java_package = "org.drizzle.messages";
option java_outer_classname = "TableMessage";

import "engine.proto";
import "access.proto";
import "replication_options.proto";

message Table {

  enum TableType {
    STANDARD = 0;
    TEMPORARY = 1;
    INTERNAL = 2;
    FUNCTION = 3;
  }

  message TableOptions {
    optional bool has_user_set_auto_increment_value = 1;
    optional string collation = 2;
    optional uint32 collation_id = 3;
    optional string data_file_name = 5;
    optional string index_file_name = 6;
    optional uint64 max_rows = 7;
    optional uint64 min_rows = 8;
    optional uint64 auto_increment_value = 9;
    optional uint32 avg_row_length = 11;
    optional uint32 block_size = 13;
    optional string comment = 14;
    optional bool pack_record = 16;
    optional bool checksum = 17;
    optional bool page_checksum = 18;
    optional bool delay_key_write = 19;
    optional bool unused = 20;
  }

  message ForeignKeyConstraint {
    optional string name = 1;
    repeated string column_names = 2;
    required string references_table_name = 3;
    repeated string references_columns = 4;

    enum ForeignKeyMatchOption {
      MATCH_UNDEFINED = 0;
      MATCH_FULL = 1;
      MATCH_PARTIAL = 2;
      MATCH_SIMPLE = 3;
    }
    required ForeignKeyMatchOption match = 5;

    enum ForeignKeyOption {
      OPTION_UNDEF = 0;
      OPTION_RESTRICT = 1;
      OPTION_CASCADE = 2;
      OPTION_SET_NULL = 3;
      OPTION_NO_ACTION = 4;
      OPTION_SET_DEFAULT = 5;
    }

    required ForeignKeyOption update_option = 6 [ default = OPTION_UNDEF ];
    required ForeignKeyOption delete_option = 7 [ default = OPTION_UNDEF ];
  }

  message Field {

    enum FieldType {
      DOUBLE = 0;
      VARCHAR = 1;
      BLOB = 2;
      ENUM = 3;
      INTEGER = 4;
      BIGINT = 5;
      DECIMAL = 6;
      DATE = 7;
      EPOCH = 9;
      DATETIME = 10;
      UUID = 11;
      TIME = 12;
      BOOLEAN = 13;
      IPV6 = 14;
    }

    message FieldOptions {
      optional string default_value = 1;
      optional string update_value = 2;
      optional bool default_null = 3 [default = false];
      optional bytes default_bin_value = 4;
      optional string default_expression = 5;
      optional string update_expression = 6;
    }

    message FieldConstraints {
      optional bool is_nullable = 1 [default = true]; // Dead option, do not use
      optional bool is_unsigned = 2 [default = false];
      optional bool is_notnull = 3 [default = false];
      optional bool is_unique = 4 [default = false];
      repeated string expression = 16; /* Reserve 0-15 for frequenty accessed attributes */
    }

    message NumericFieldOptions {
      optional bool is_autoincrement = 1 [default = false];
      optional uint32 scale = 2;
      optional uint32 precision = 3;
    }

    message StringFieldOptions {
      optional bool is_fixed_width = 1 [default = false];
      optional uint32 length = 2;
      optional uint32 collation_id = 3;
      optional string collation = 4;
    }

    message EnumerationValues {
      optional uint32 collation_id = 2;
      optional string collation = 3;
      repeated string field_value = 4;
    }

    /*
      Do we store microseconds or timezone.
    */
    message TimeFieldOptions {
      optional bool microseconds = 1;
    }

    required string name = 1;
    required FieldType type = 2;
    optional FieldOptions options = 4;
    optional FieldConstraints constraints = 5;
    optional NumericFieldOptions numeric_options = 6;
    optional StringFieldOptions string_options = 7;
    optional TimeFieldOptions time_options = 8;

    optional string comment = 16; /* Reserve 0-15 for frequently accessed attributes */
    optional EnumerationValues enumeration_values = 17;
  }

  message Index {

    enum IndexType {
    /* Kept in sync with enum ha_key_alg if only for stewart sanity. */
      UNKNOWN_INDEX = 0;
      BTREE = 1;
      RTREE = 2;
      HASH  = 3;
      FULLTEXT = 4;
    }

    message IndexPart {
      required uint32 fieldnr = 1;
      optional uint32 compare_length = 2;
      optional bool in_reverse_order = 3 [default = false];
    }

    message Options {
      optional bool pack_key = 1;
      optional bool binary_pack_key = 2;
      optional bool var_length_key = 3;
      optional bool null_part_key = 4;
      optional uint32 key_block_size = 5;
      optional bool has_partial_segments =6;
      optional bool auto_generated_key = 7;
    }      

    required string name = 1;
    required bool is_primary = 2;
    required bool is_unique = 3;
    required IndexType type = 4 [default = UNKNOWN_INDEX];
    required uint32 key_length = 5;
    repeated IndexPart index_part = 6;
    optional Options options= 7;
    optional string comment = 8;
  }

  required string name = 1;
  required string schema = 6;
  required TableType type = 5;
  required Engine engine = 2;
  repeated Field field = 3;
  repeated Index indexes = 4;

  repeated ForeignKeyConstraint fk_constraint = 8;
  optional TableOptions options = 9;
  required uint64 creation_timestamp= 11 [default = 0];
  required uint64 update_timestamp= 12 [default = 0];
  optional string catalog = 13;
  optional string uuid = 14;
  /*
    A version value of 0, means that it was never set.
    */
  optional uint64 version = 15;

  optional ReplicationOptions replication_options= 16;
  optional Access access= 17;
}

message AddedFields {
  repeated Table.Field added_field = 1;
}
