com.github.drinkjava2.jdialects.model.ColumnModel Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jsqlbox Show documentation
Show all versions of jsqlbox Show documentation
jSqlBox is a full function DAO tool
/*
* Copyright 2016 the original author or authors.
*
* 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.
*/
package com.github.drinkjava2.jdialects.model;
import java.util.Iterator;
import java.util.List;
import com.github.drinkjava2.jdialects.DebugUtils;
import com.github.drinkjava2.jdialects.DialectException;
import com.github.drinkjava2.jdialects.StrUtils;
import com.github.drinkjava2.jdialects.Type;
import com.github.drinkjava2.jdialects.annotation.jpa.GenerationType;
import com.github.drinkjava2.jdialects.id.IdGenerator;
import com.github.drinkjava2.jdialects.id.UUIDAnyGenerator;
/**
* A ColumnModel definition represents a platform dependent column in a Database
* Table, from 1.0.5 this class name changed from "Column" to "ColumnModel" to
* avoid naming conflict to JPA's "@Column" annotation
*
*
*
* @author Yong Zhu
* @since 1.0.0
*/
@SuppressWarnings("all")
public class ColumnModel {
private String columnName;// no need explain
private TableModel tableModel; // belong to which tableModel
private Type columnType;// See com.github.drinkjava2.jdialects.Type
private Boolean pkey = false; // if is primary key
private Boolean nullable = true; // if nullable
/** DDL check string */
private String check;
/** DDL default value */
private String defaultValue;
/** Optional, put an extra tail String at end of column definition DDL */
private String tail;
/** Comment of this column */
private String comment;
/** length, precision, scale all share use lengths array */
private Integer[] lengths = new Integer[] {};
// =======================================================================
private GenerationType idGenerationType;
private String idGeneratorName;
// =======================================================================
// =====Below fields are designed only for ORM tools ==========
/**
* If not equal null, means this column has a converter class to translate field
* value to column value or reverse. Note: @Version and @Enumerated annotated
* will also be recorded as converterClass in this field
*/
private Object converterClassOrName;
/** Map to a Java entity field, for ORM tool use only */
private String entityField;
/** The column length, for ORM tool use only */
private Integer length = 255;
/** The numeric precision, for ORM tool use only */
private Integer precision = 0;
/** The numeric scale, for ORM tool use only */
private Integer scale = 0;
/** If insert-able or not, for ORM tool use only */
private Boolean insertable = true;
/** If update-able or not, for ORM tool use only */
private Boolean updatable = true;
/** If this is a Transient type, for ORM tool use only */
private Boolean transientable = false;
/** ShardTable strategy and parameters, for ORM tool use only */
private String[] shardTable = null;
/** ShardDatabase strategy and parameters, for ORM tool use only */
private String[] shardDatabase = null;
public ColumnModel(String columnName) {
if (StrUtils.isEmpty(columnName))
DialectException.throwEX("columnName is not allowed empty");
this.columnName = columnName;
}
/** Add a not null DDL piece if support */
public ColumnModel notNull() {
this.nullable = false;
return this;
}
/** Add a column check DDL piece if support */
public ColumnModel check(String check) {
this.check = check;
return this;
}
public ColumnModel newCopy() {
ColumnModel col = new ColumnModel(columnName);
col.columnType = columnType;
col.pkey = pkey.booleanValue();
col.nullable = nullable;
col.check = check;
col.defaultValue = defaultValue;
col.tail = tail;
col.comment = comment;
col.lengths = lengths;
col.entityField = entityField;
col.length = length;
col.precision = precision;
col.scale = scale;
col.insertable = insertable;
col.updatable = updatable;
col.transientable = transientable;
col.idGeneratorName = idGeneratorName;
col.idGenerationType = idGenerationType;
col.shardTable = shardTable;
col.shardDatabase = shardDatabase;
col.converterClassOrName=converterClassOrName;
return col;
}
/**
* A shortcut method to add a index for single column, for multiple columns
* index please use tableModel.index() method
*/
public ColumnModel singleIndex(String indexName) {
makeSureTableModelExist();
DialectException.assureNotEmpty(indexName, "indexName can not be empty");
this.tableModel.index(indexName).columns(this.getColumnName());
return this;
}
/**
* A shortcut method to add a index for single column, for multiple columns
* index please use tableModel.index() method
*/
public ColumnModel singleIndex() {
makeSureTableModelExist();
this.tableModel.index().columns(this.getColumnName());
return this;
}
/**
* A shortcut method to add a unique constraint for single column, for multiple
* columns index please use tableModel.unique() method
*/
public ColumnModel singleUnique(String uniqueName) {
makeSureTableModelExist();
DialectException.assureNotEmpty(uniqueName, "indexName can not be empty");
this.tableModel.unique(uniqueName).columns(this.getColumnName());
return this;
}
/**
* A shortcut method to add a unique constraint for single column, for multiple
* columns index please use tableModel.unique() method
*/
public ColumnModel singleUnique() {
makeSureTableModelExist();
this.tableModel.unique().columns(this.getColumnName());
return this;
}
private void makeSureTableModelExist() {
DialectException.assureNotNull(this.tableModel,
"ColumnModel should belong to a TableModel, please call tableModel.column() method first.");
}
/** Default value for column's definition DDL */
public ColumnModel defaultValue(String value) {
this.defaultValue = value;
return this;
}
/** Add comments at end of column definition DDL */
public ColumnModel comment(String comment) {
this.comment = comment;
return this;
}
/** Mark primary key, if more than one will build compound Primary key */
public ColumnModel pkey() {
this.pkey = true;
return this;
}
/** Mark is a shartTable column, for ORM tool use */
public ColumnModel shardTable(String... shardTable) {
this.shardTable = shardTable;
return this;
}
/** Mark is a shartDatabase column, for ORM tool use */
public ColumnModel shardDatabase(String... shardDatabase) {
this.shardDatabase = shardDatabase;
return this;
}
/**
* equal to pkey method. Mark primary key, if more than one will build compound
* Primary key
*/
public ColumnModel id() {
this.pkey = true;
return this;
}
/**
* A shortcut method to add Foreign constraint for single column, for multiple
* columns please use tableModel.fkey() method instead
*/
public FKeyModel singleFKey(String... refTableAndColumns) {
makeSureTableModelExist();
if (refTableAndColumns == null || refTableAndColumns.length > 2)
throw new DialectException(
"singleFKey() first parameter should be table name, second parameter(optional) should be column name");
return this.tableModel.fkey().columns(this.columnName).refs(refTableAndColumns);
}
// ===========id generator methods=======================
public IdGenerator getIdGenerator() {
makeSureTableModelExist();
return this.tableModel.getIdGenerator(idGenerationType, idGeneratorName);
}
/** Mark a field will use database's native identity type. */
public ColumnModel identityId() {
makeSureTableModelExist();
this.idGenerationType = GenerationType.IDENTITY;
this.idGeneratorName = null;
return this;
}
public ColumnModel uuid25() {
makeSureTableModelExist();
this.idGenerationType = GenerationType.UUID25;
this.idGeneratorName = null;
return this;
}
public ColumnModel uuid32() {
makeSureTableModelExist();
this.idGenerationType = GenerationType.UUID32;
this.idGeneratorName = null;
return this;
}
public ColumnModel uuid36() {
makeSureTableModelExist();
this.idGenerationType = GenerationType.UUID36;
this.idGeneratorName = null;
return this;
}
public ColumnModel snowflake() {
makeSureTableModelExist();
this.idGenerationType = GenerationType.SNOWFLAKE;
this.idGeneratorName = null;
return this;
}
public ColumnModel uuidAny(String name, Integer length) {
makeSureTableModelExist();
this.idGenerationType = GenerationType.UUID_ANY;
this.idGeneratorName = name;
if (this.tableModel.getIdGenerator(GenerationType.UUID_ANY, idGeneratorName) == null)
this.tableModel.getIdGenerators().add(new UUIDAnyGenerator(name, length));
return this;
}
public ColumnModel timeStampId() {
makeSureTableModelExist();
this.idGenerationType = GenerationType.TIMESTAMP;
this.idGeneratorName = null;
return this;
}
/** Bind column to a global Auto Id generator, can be Sequence or a Table */
public ColumnModel autoId() {
makeSureTableModelExist();
this.idGenerationType = GenerationType.AUTO;
this.idGeneratorName = null;
return this;
}
public ColumnModel sortedUUID(String name, Integer sortedLength, Integer uuidLength) {
makeSureTableModelExist();
this.tableModel.sortedUUIDGenerator(name, sortedLength, uuidLength);
this.idGenerationType = GenerationType.SORTED_UUID;
this.idGeneratorName = null;
return this;
}
/** The value of this column will be generated by a sequence */
public ColumnModel sequenceGenerator(String name, String sequenceName, Integer initialValue,
Integer allocationSize) {
makeSureTableModelExist();
this.tableModel.sequenceGenerator(name, sequenceName, initialValue, allocationSize);
this.idGenerationType = GenerationType.SEQUENCE;
this.idGeneratorName = name;
return this;
}
/**
* The value of this column will be generated by a sequence or table generator
*/
public ColumnModel idGenerator(String idGeneratorName) {
makeSureTableModelExist();
this.idGenerationType = null;
this.idGeneratorName = idGeneratorName;
return this;
}
public ColumnModel tableGenerator(String name, String tableName, String pkColumnName, String valueColumnName,
String pkColumnValue, Integer initialValue, Integer allocationSize) {
makeSureTableModelExist();
this.tableModel.tableGenerator(name, tableName, pkColumnName, valueColumnName, pkColumnValue, initialValue,
allocationSize);
this.idGenerationType = GenerationType.TABLE;
this.idGeneratorName = name;
return this;
}
// ===================================================
/**
* Put an extra tail String manually at the end of column definition DDL
*/
public ColumnModel tail(String tail) {
this.tail = tail;
return this;
}
/**
* Mark this column map to a Java entity field, if exist other columns map to
* this field, delete other columns. This method only designed for ORM tool
*/
public ColumnModel entityField(String entityFieldName) {
DialectException.assureNotEmpty(entityFieldName, "entityFieldName can not be empty");
this.entityField = entityFieldName;
if (this.tableModel != null) {
List