com.github.jinahya.database.metadata.bind.Column Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of database-metadata-bind Show documentation
Show all versions of database-metadata-bind Show documentation
Class for binding results of java.sql.DatabaseMetaData
The newest version!
package com.github.jinahya.database.metadata.bind;
/*-
* #%L
* database-metadata-bind
* %%
* Copyright (C) 2011 - 2019 Jinahya, 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.
* #L%
*/
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import java.sql.DatabaseMetaData;
import java.util.Comparator;
import java.util.Objects;
import java.util.Optional;
import java.util.function.BiPredicate;
import static java.util.Comparator.naturalOrder;
import static java.util.Comparator.nullsFirst;
/**
* A class for binding results of the {@link DatabaseMetaData#getColumns(String, String, String, String)} method.
*
* @author Jin Kwon <jinahya_at_gmail.com>
* @see Context#getColumns(String, String, String, String)
* @see Nullable
*/
//@ParentOf(ColumnPrivilege.class)
@_ChildOf(Table.class)
@Setter
@Getter
@EqualsAndHashCode(callSuper = true, onlyExplicitlyIncluded = true)
@ToString(callSuper = true)
public class Column extends AbstractMetadataType {
private static final long serialVersionUID = -409653682729081530L;
// -----------------------------------------------------------------------------------------------------------------
static final Comparator CASE_INSENSITIVE_ORDER =
Comparator.comparing(Column::getTableCat, nullsFirst(String.CASE_INSENSITIVE_ORDER))
.thenComparing(Column::getTableSchem, nullsFirst(String.CASE_INSENSITIVE_ORDER))
.thenComparing(Column::getTableName, nullsFirst(String.CASE_INSENSITIVE_ORDER))
.thenComparingInt(Column::getOrdinalPosition);
static final Comparator LEXICOGRAPHIC_ORDER =
Comparator.comparing(Column::getTableCat, nullsFirst(naturalOrder()))
.thenComparing(Column::getTableSchem, nullsFirst(naturalOrder()))
.thenComparing(Column::getTableName, nullsFirst(naturalOrder()))
.thenComparingInt(Column::getOrdinalPosition);
// -----------------------------------------------------------------------------------------------------------------
/**
* The column label of {@value}.
*/
public static final String COLUMN_LABEL_TABLE_CAT = "TABLE_CAT";
// -----------------------------------------------------------------------------------------------------------------
/**
* The column label of {@value}.
*/
public static final String COLUMN_LABEL_TABLE_SCHEM = "TABLE_SCHEM";
// -----------------------------------------------------------------------------------------------------------------
/**
* The column label of {@value}.
*/
public static final String COLUMN_LABEL_TABLE_NAME = "TABLE_NAME";
// -----------------------------------------------------------------------------------------------------------------
/**
* The column label of {@value}.
*/
public static final String COLUMN_LABEL_COLUMN_NAME = "COLUMN_NAME";
// -----------------------------------------------------------------------------------------------------------------
/**
* The column label of {@value}.
*/
public static final String COLUMN_LABEL_NULLABLE = "NULLABLE";
/**
* Constants for {@value #COLUMN_LABEL_NULLABLE} column values.
*
* @author Jin Kwon <onacit_at_gmail.com>
*/
public enum Nullable implements _IntFieldEnum {
/**
* A value for {@link DatabaseMetaData#columnNoNulls}({@value DatabaseMetaData#columnNoNulls}).
*/
COLUMN_NO_NULLS(DatabaseMetaData.columnNoNulls),// 0
/**
* A value for {@link DatabaseMetaData#columnNullable}({@value DatabaseMetaData#columnNullable}).
*/
COLUMN_NULLABLE(DatabaseMetaData.columnNullable), // 1
/**
* A value for {@link DatabaseMetaData#columnNullableUnknown}({@value DatabaseMetaData#columnNullableUnknown}).
*/
COLUMN_NULLABLE_UNKNOWN(DatabaseMetaData.columnNullableUnknown) // 2
;
/**
* Finds the value for specified {@link Column#COLUMN_LABEL_NULLABLE} column value.
*
* @param fieldValue the value of {@link Column#COLUMN_LABEL_NULLABLE} column to match.
* @return the value matched.
* @throws IllegalStateException when no value matched.
*/
public static Nullable valueOfFieldValue(final int fieldValue) {
return _IntFieldEnum.valueOfFieldValue(Nullable.class, fieldValue);
}
Nullable(final int fieldValue) {
this.fieldValue = fieldValue;
}
@Override
public int fieldValueAsInt() {
return fieldValue;
}
private final int fieldValue;
}
// -----------------------------------------------------------------------------------------------------------------
public static final String COLUMN_LABEL_IS_AUTOINCREMENT = "IS_AUTOINCREMENT";
// -----------------------------------------------------------------------------------------------------------------
public static final String COLUMN_LABEL_IS_GENERATEDCOLUMN = "IS_GENERATEDCOLUMN";
// -----------------------------------------------------------------------------------------------------------------
static final BiPredicate IS_OF = (c, t) -> {
return Objects.equals(c.tableCat, t.getTableCat()) &&
Objects.equals(c.tableSchem, t.getTableSchem()) &&
Objects.equals(c.tableName, t.getTableName());
};
// -----------------------------------------------------------------------------------------------------------------
boolean isOf(final Table table) {
return Objects.equals(tableCat, table.getTableCat()) &&
Objects.equals(tableSchem, table.getTableSchem()) &&
Objects.equals(tableName, table.getTableName());
}
// -------------------------------------------------------------------------------------------------------- tableCat
// ------------------------------------------------------------------------------------------------------ tableSchem
// -------------------------------------------------------------------------------------------------------- nullable
Nullable getNullableAsEnum() {
return Optional.ofNullable(getNullable())
.map(Nullable::valueOfFieldValue)
.orElse(null);
}
void setNullableAsEnum(final Nullable nullableAsEnum) {
setNullable(
Optional.ofNullable(nullableAsEnum)
.map(_IntFieldEnum::fieldValueAsInt)
.orElse(null)
);
}
// -----------------------------------------------------------------------------------------------------------------
@jakarta.annotation.Nullable
@_NullableBySpecification
@_ColumnLabel(COLUMN_LABEL_TABLE_CAT)
@EqualsAndHashCode.Include
private String tableCat;
@jakarta.annotation.Nullable
@_NullableBySpecification
@_ColumnLabel(COLUMN_LABEL_TABLE_SCHEM)
@EqualsAndHashCode.Include
private String tableSchem;
@_ColumnLabel(COLUMN_LABEL_TABLE_NAME)
@EqualsAndHashCode.Include
private String tableName;
@_ColumnLabel(COLUMN_LABEL_COLUMN_NAME)
@EqualsAndHashCode.Include
private String columnName;
// -----------------------------------------------------------------------------------------------------------------
@_ColumnLabel("DATA_TYPE")
private Integer dataType;
@_ColumnLabel("TYPE_NAME")
private String typeName;
@jakarta.annotation.Nullable
@_NullableBySpecification
@_ColumnLabel("COLUMN_SIZE")
private Integer columnSize;
@_NotUsedBySpecification
@_ColumnLabel("BUFFER_LENGTH")
private Integer bufferLength;
@jakarta.annotation.Nullable
@_NullableBySpecification
@_ColumnLabel("DECIMAL_DIGITS")
private Integer decimalDigits;
@_ColumnLabel("NUM_PREC_RADIX")
private Integer numPrecRadix;
@_ColumnLabel(COLUMN_LABEL_NULLABLE)
private Integer nullable;
@jakarta.annotation.Nullable
@_NullableBySpecification
@_ColumnLabel("REMARKS")
private String remarks;
@jakarta.annotation.Nullable
@_NullableBySpecification
@_ColumnLabel("COLUMN_DEF")
private String columnDef;
@_NotUsedBySpecification
@_ColumnLabel("SQL_DATA_TYPE")
private Integer sqlDataType;
@_NotUsedBySpecification
@_ColumnLabel("SQL_DATETIME_SUB")
private Integer sqlDatetimeSub;
@_ColumnLabel("CHAR_OCTET_LENGTH")
private Integer charOctetLength;
@_ColumnLabel("ORDINAL_POSITION")
private Integer ordinalPosition;
@_ColumnLabel("IS_NULLABLE")
private String isNullable;
@jakarta.annotation.Nullable
@_NullableBySpecification
@_ColumnLabel("SCOPE_CATALOG")
private String scopeCatalog;
@jakarta.annotation.Nullable
@_NullableBySpecification
@_ColumnLabel("SCOPE_SCHEMA")
private String scopeSchema;
@jakarta.annotation.Nullable
@_NullableBySpecification
@_ColumnLabel("SCOPE_TABLE")
private String scopeTable;
@jakarta.annotation.Nullable
@_NullableBySpecification
@_ColumnLabel("SOURCE_DATA_TYPE")
private Integer sourceDataType;
@_ColumnLabel(COLUMN_LABEL_IS_AUTOINCREMENT)
private String isAutoincrement;
@_ColumnLabel(COLUMN_LABEL_IS_GENERATEDCOLUMN)
private String isGeneratedcolumn;
}