ddl::DDLStatementList Schema::Dump()

in backend/schema/catalog/schema.cc [559:684]


ddl::DDLStatementList Schema::Dump() const {
  ddl::DDLStatementList ddl_statements;
  // Do named schemas first since tables, views, sequences, and indexes rely on
  // them.
  for (const NamedSchema* named_schema : named_schemas_) {
    DumpNamedSchema(named_schema,
                    *ddl_statements.add_statement()->mutable_create_schema());
  }

  // Print sequences next, since other schema objects may use them.
  for (const Sequence* sequence : sequences_) {
    if (sequence->is_internal_use()) {
      // Do not print internal sequences.
      continue;
    }
    DumpSequence(sequence,
                 *ddl_statements.add_statement()->mutable_create_sequence());
  }

  for (const Table* table : tables_) {
    ddl::CreateTable* create_table =
        ddl_statements.add_statement()->mutable_create_table();
    create_table->set_table_name(table->Name());
    for (const Column* column : table->columns()) {
      DumpColumn(column, *create_table->add_column());
    }

    for (const ForeignKey* foreign_key : table->foreign_keys()) {
      DumpForeignKey(foreign_key, *create_table->add_foreign_key());
    }

    for (const KeyColumn* key_column : table->primary_key()) {
      ddl::KeyPartClause* key_part_clause = create_table->add_primary_key();
      key_part_clause->set_key_name(key_column->column()->Name());
    }

    if (table->parent() != nullptr) {
      DumpInterleaveClause(table, *create_table->mutable_interleave_clause());
    }

    // Unnamed check constraints are printed before named ones.
    for (const CheckConstraint* check_constraint : table->check_constraints()) {
      if (check_constraint->has_generated_name()) {
        DumpCheckConstraint(check_constraint,
                            *create_table->add_check_constraint());
      }
    }
    // Named check constraints.
    for (const CheckConstraint* check_constraint : table->check_constraints()) {
      if (!check_constraint->has_generated_name()) {
        DumpCheckConstraint(check_constraint,
                            *create_table->add_check_constraint());
      }
    }

    if (table->row_deletion_policy().has_value()) {
      *create_table->mutable_row_deletion_policy() =
          *table->row_deletion_policy();
    }
  }

  for (const auto& [unused_name, index] : index_map_) {
    if (!index->is_managed()) {
      DumpIndex(index, *ddl_statements.add_statement()->mutable_create_index());
    }
  }

  for (const Model* model : models_) {
    DumpModel(model, *ddl_statements.add_statement()->mutable_create_model());
  }

  for (const PropertyGraph* graph : property_graphs_) {
    DumpPropertyGraph(
        graph,
        *ddl_statements.add_statement()->mutable_create_property_graph());
  }

  for (const View* view : views_) {
    ddl::CreateFunction* create_function =
        ddl_statements.add_statement()->mutable_create_function();
    create_function->set_function_kind(ddl::Function::VIEW);
    create_function->set_function_name(view->Name());
    if (view->security() == View::INVOKER) {
      create_function->set_sql_security(ddl::Function::INVOKER);
    }
    create_function->set_sql_body(view->body());
    if (view->body_origin().has_value()) {
      create_function->mutable_sql_body_origin()->set_original_expression(
          *view->body_origin());
    }
  }

  for (const Udf* udf : udfs_) {
    ddl::CreateFunction* create_function =
        ddl_statements.add_statement()->mutable_create_function();
    create_function->set_function_kind(ddl::Function::FUNCTION);
    create_function->set_function_name(udf->Name());
    create_function->set_return_typename(
        udf->signature()->result_type().argument_name());
    create_function->set_sql_body(udf->body());
    if (udf->body_origin().has_value()) {
      create_function->mutable_sql_body_origin()->set_original_expression(
          *udf->body_origin());
    }
  }

  for (const ChangeStream* change_stream : change_streams_) {
    DumpChangeStream(
        change_stream,
        *ddl_statements.add_statement()->mutable_create_change_stream());
  }

  if (database_options_ != nullptr) {
    DumpDatabaseOptions(
        database_options_,
        *ddl_statements.add_statement()->mutable_alter_database());
  }

  for (const LocalityGroup* locality_group : locality_groups_) {
    DumpLocalityGroup(
        locality_group,
        *ddl_statements.add_statement()->mutable_create_locality_group());
  }

  return ddl_statements;
}