<commit_before>/**
 * Copyright (C) 2009-2013 FoundationDB, LLC
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.foundationdb.ais.model;

import com.foundationdb.ais.model.validation.AISInvariants;
import com.foundationdb.server.collation.AkCollator;
import com.foundationdb.server.rowdata.FieldDef;
import com.foundationdb.server.types.TClass;
import com.foundationdb.server.types.TInstance;
import com.foundationdb.server.types.common.types.DecimalAttribute;
import com.foundationdb.server.types.common.types.StringAttribute;
import com.foundationdb.server.types.common.types.StringFactory;
import com.foundationdb.server.types.common.types.TBinary;
import com.foundationdb.server.types.common.types.TString;

import java.util.UUID;

public class Column implements ColumnContainer, Visitable
{
    public static Column create(Columnar table, String name, Integer position, TInstance type) {
        return create(table, name, position, type, null, null, null);
    }

    public static Column create(Columnar table, String name, Integer position, TInstance type, Long initialAutoIncValue)
    {
        return create(table, name, position, type, initialAutoIncValue,
                      null, null);
    }

    public static Column create(Columnar table, String name, Integer position, TInstance type, Long initialAutoIncValue,
                                Long maxStorageSize, Integer prefixSize)
    {
        table.checkMutability();
        AISInvariants.checkNullName(name, "column", "column name");
        AISInvariants.checkDuplicateColumnsInTable(table, name);
        AISInvariants.checkDuplicateColumnPositions(table, position);
        Column column = new Column(table, name, position, type, initialAutoIncValue,
                                   maxStorageSize, prefixSize);
        table.addColumn(column);
        return column;
    }

    /**
     * Create an independent copy of an existing Column.
     * @param columnar Destination Columnar.
     * @param column Column to copy.
     * @param position Position of the new column, or <code>null</code> to copy from the given column.
     * @return Copy of the Column.
     * */
    public static Column create(Columnar columnar, Column column, String name, Integer position) {
        Integer finalPosition = (position != null) ? position : column.position;
        String finalName = (name != null) ? name :  column.getName();
        Column out = create(columnar, finalName, finalPosition, column.type, column.initialAutoIncrementValue,
                            column.maxStorageSize, column.prefixSize);
        if(column.identityGenerator != null) {
            Sequence newGenerator = columnar.getAIS().getSequence(column.identityGenerator.getSequenceName());
            if(newGenerator != null) {
                out.setDefaultIdentity(column.defaultIdentity);
                out.setIdentityGenerator(newGenerator);
            }
        }
        out.setDefaultValue(column.getDefaultValue());
        out.setDefaultFunction(column.getDefaultFunction());
        out.setUuid(column.getUuid());
        return out;
    }

    @Override
    public Column getColumn() {
        return this;
    }

    @Override
    public String toString()
    {
        return columnar.getName().getTableName() + "." + columnName;
    }

    public void finishCreating() {
    }

    public void setAutoIncrement(Boolean autoIncrement)
    {
        this.initialAutoIncrementValue = autoIncrement ? 1L /* mysql default */ : null;
        columnar.markColumnsStale();
    }

    public final void setDefaultIdentity(Boolean defaultIdentity) {
        this.defaultIdentity = defaultIdentity;
        columnar.markColumnsStale();
    }

    public final void setIdentityGenerator(Sequence identityGenerator) {
        this.identityGenerator = identityGenerator;
        columnar.markColumnsStale();
    }

    public String getDescription()
    {
        StringBuilder buffer = new StringBuilder();
        buffer.append(columnar.getName().getSchemaName());
        buffer.append(".");
        buffer.append(columnar.getName().getTableName());
        buffer.append(".");
        buffer.append(getName());
        return buffer.toString();
    }

    public String getName()
    {
        return columnName;
    }

    public Integer getPosition()
    {
        return position;
    }

    public Columnar getColumnar()
    {
        return columnar;
    }

    public Table getTable()
    {
        return (Table)columnar;
    }

    /**
     * The initial auto-increment value used in the MySQL 
     * generated table identity columns. 
     */
    public Long getInitialAutoIncrementValue()
    {
        return initialAutoIncrementValue;
    }

    public void setInitialAutoIncrementValue(Long initialAutoIncrementValue)
    {
        this.initialAutoIncrementValue = initialAutoIncrementValue;
        columnar.markColumnsStale();
    }

    /**
     * <p>
     * This is a three state boolean:
     * <ul>
     *   <li><b>True</b>: column created with GENERATED BY DEFAULT AS IDENTITY</li>
     *   <li><b>False</b>: Column created with GENERATED ALWAYS AS IDENTITY</li>
     *   <li><b>null</b>: Not generated by identity column</li>
     * </ul>
     * </p>
     * <p>
     * <b>NOTE</b>: It is possible for the GetInitialAutoIncrement to be
     * not null and this to be null, as MySQL generated tables use
     *  auto-increment value, where as the SQL Layer uses identity generators.
     * </p>
     */
    public final Boolean getDefaultIdentity() {
        return defaultIdentity;
    }

    /**
     * The identity generator sequence for this column. 
     * Will be null if not a IDENTITY Column. 
     * The null/not-null state matches the getDefaultIdentity
     */
    public final Sequence getIdentityGenerator() {
        return identityGenerator;
     }

    // TODO ensure this never returns null
    public TInstance getType() {
        return type;
    }

    public void setType(TInstance type) {
        columnar.checkMutability();
        this.type = type;
        clearMaxAndPrefixSize();
        columnar.markColumnsStale();
    }

    public String getTypeName() {
        return type.typeClass().name().unqualifiedName();<commit_msg>Followed data flow for Column.getType(), not null<commit_after>/**
 * Copyright (C) 2009-2013 FoundationDB, LLC
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.foundationdb.ais.model;

import com.foundationdb.ais.model.validation.AISInvariants;
import com.foundationdb.server.collation.AkCollator;
import com.foundationdb.server.rowdata.FieldDef;
import com.foundationdb.server.types.TClass;
import com.foundationdb.server.types.TInstance;
import com.foundationdb.server.types.common.types.DecimalAttribute;
import com.foundationdb.server.types.common.types.StringAttribute;
import com.foundationdb.server.types.common.types.StringFactory;
import com.foundationdb.server.types.common.types.TBinary;
import com.foundationdb.server.types.common.types.TString;

import java.util.UUID;

public class Column implements ColumnContainer, Visitable
{
    public static Column create(Columnar table, String name, Integer position, TInstance type) {
        return create(table, name, position, type, null, null, null);
    }

    public static Column create(Columnar table, String name, Integer position, TInstance type, Long initialAutoIncValue)
    {
        return create(table, name, position, type, initialAutoIncValue,
                      null, null);
    }

    public static Column create(Columnar table, String name, Integer position, TInstance type, Long initialAutoIncValue,
                                Long maxStorageSize, Integer prefixSize)
    {
        table.checkMutability();
        AISInvariants.checkNullName(name, "column", "column name");
        AISInvariants.checkDuplicateColumnsInTable(table, name);
        AISInvariants.checkDuplicateColumnPositions(table, position);
        Column column = new Column(table, name, position, type, initialAutoIncValue,
                                   maxStorageSize, prefixSize);
        table.addColumn(column);
        return column;
    }

    /**
     * Create an independent copy of an existing Column.
     * @param columnar Destination Columnar.
     * @param column Column to copy.
     * @param position Position of the new column, or <code>null</code> to copy from the given column.
     * @return Copy of the Column.
     * */
    public static Column create(Columnar columnar, Column column, String name, Integer position) {
        Integer finalPosition = (position != null) ? position : column.position;
        String finalName = (name != null) ? name :  column.getName();
        Column out = create(columnar, finalName, finalPosition, column.type, column.initialAutoIncrementValue,
                            column.maxStorageSize, column.prefixSize);
        if(column.identityGenerator != null) {
            Sequence newGenerator = columnar.getAIS().getSequence(column.identityGenerator.getSequenceName());
            if(newGenerator != null) {
                out.setDefaultIdentity(column.defaultIdentity);
                out.setIdentityGenerator(newGenerator);
            }
        }
        out.setDefaultValue(column.getDefaultValue());
        out.setDefaultFunction(column.getDefaultFunction());
        out.setUuid(column.getUuid());
        return out;
    }

    @Override
    public Column getColumn() {
        return this;
    }

    @Override
    public String toString()
    {
        return columnar.getName().getTableName() + "." + columnName;
    }

    public void finishCreating() {
    }

    public void setAutoIncrement(Boolean autoIncrement)
    {
        this.initialAutoIncrementValue = autoIncrement ? 1L /* mysql default */ : null;
        columnar.markColumnsStale();
    }

    public final void setDefaultIdentity(Boolean defaultIdentity) {
        this.defaultIdentity = defaultIdentity;
        columnar.markColumnsStale();
    }

    public final void setIdentityGenerator(Sequence identityGenerator) {
        this.identityGenerator = identityGenerator;
        columnar.markColumnsStale();
    }

    public String getDescription()
    {
        StringBuilder buffer = new StringBuilder();
        buffer.append(columnar.getName().getSchemaName());
        buffer.append(".");
        buffer.append(columnar.getName().getTableName());
        buffer.append(".");
        buffer.append(getName());
        return buffer.toString();
    }

    public String getName()
    {
        return columnName;
    }

    public Integer getPosition()
    {
        return position;
    }

    public Columnar getColumnar()
    {
        return columnar;
    }

    public Table getTable()
    {
        return (Table)columnar;
    }

    /**
     * The initial auto-increment value used in the MySQL 
     * generated table identity columns. 
     */
    public Long getInitialAutoIncrementValue()
    {
        return initialAutoIncrementValue;
    }

    public void setInitialAutoIncrementValue(Long initialAutoIncrementValue)
    {
        this.initialAutoIncrementValue = initialAutoIncrementValue;
        columnar.markColumnsStale();
    }

    /**
     * <p>
     * This is a three state boolean:
     * <ul>
     *   <li><b>True</b>: column created with GENERATED BY DEFAULT AS IDENTITY</li>
     *   <li><b>False</b>: Column created with GENERATED ALWAYS AS IDENTITY</li>
     *   <li><b>null</b>: Not generated by identity column</li>
     * </ul>
     * </p>
     * <p>
     * <b>NOTE</b>: It is possible for the GetInitialAutoIncrement to be
     * not null and this to be null, as MySQL generated tables use
     *  auto-increment value, where as the SQL Layer uses identity generators.
     * </p>
     */
    public final Boolean getDefaultIdentity() {
        return defaultIdentity;
    }

    /**
     * The identity generator sequence for this column. 
     * Will be null if not a IDENTITY Column. 
     * The null/not-null state matches the getDefaultIdentity
     */
    public final Sequence getIdentityGenerator() {
        return identityGenerator;
     }

    public TInstance getType() {
        return type;
    }

    public void setType(TInstance type) {
        columnar.checkMutability();
        this.type = type;
        clearMaxAndPrefixSize();
        columnar.markColumnsStale();
    }

    public String getTypeName() {
        return type.typeClass().name().unqualifiedName();
{'fraction_non_alphanumeric': 0.09127301841473179, 'fraction_numerical': 0.0014557100225635054, 'mean_word_length': 2.800829875518672, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2562780', 'n_tokens_mistral': 3678, 'n_tokens_neox': 3568, 'n_words': 1049}