// Copyright 2021 Dolthub, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

include "encoding.fbs";
include "collation.fbs";

namespace serial;


table TableSchema {
  columns:[Column] (required);
  clustered_index:Index (required);
  secondary_indexes:[Index];
  checks:[CheckConstraint];
  collation:Collation;
}

table Column {
  // column name
  name:string (required);

  // sql column type
  sql_type:string;

  // sql default value
  default_value:string;

  // sql comment
  comment:string;

  // sql display order
  display_order:int16;

  // todo(andy): ideally we'd resolve column identity
  //  without using tags, but the current implementation
  //  of schema.Schema is tightly coupled to tags.
  tag: uint64;

  // storage encoding
  encoding:Encoding;

  // column meta
  primary_key:bool;
  nullable:bool;
  auto_increment:bool;
  hidden:bool;
  generated:bool;
  virtual:bool;
}

table Index {
  // index name
  name:string;

  // sql comment
  comment:string;

  // ordered list of columns defining the index.
  // stored as indices into columns vector.
  index_columns:[uint16] (required);

  // ordered list of columns corresponding to
  // key tuple fields within index storage.
  // stored as indices into columns vector.
  //
  // for secondary indexes, this is typically
  // index columns + primary key columns.
  key_columns:[uint16] (required);

  // ordered list of columns corresponding to
  // value tuple fields within index storage.
  // stored as indices into columns vector.
  //
  // typically, this is only populated for
  // clustered primary key indexes.
  value_columns:[uint16];

  // index meta
  primary_key:bool;
  unique_key:bool;
  system_defined:bool;

  prefix_lengths:[uint16];
  spatial_key:bool;
}

table CheckConstraint {
    name:string;
    expression:string;
    enforced:bool;
}

// KEEP THIS IN SYNC WITH fileidentifiers.go
file_identifier "DSCH";

root_type TableSchema;
