📄 column.java
字号:
}
/**
* Convert the auto-increment flag to a sequence that is linked with this
* table.
*
* @param session the session
* @param schema the schema where the sequence should be generated
* @param id the object id
* @param temporary true if the sequence is temporary and does not need to
* be stored
*/
public void convertAutoIncrementToSequence(Session session, Schema schema, int id, boolean temporary)
throws SQLException {
if (!autoIncrement) {
throw Message.getInternalError();
}
if ("IDENTITY".equals(originalSQL)) {
originalSQL = "BIGINT";
}
String sequenceName;
for (int i = 0;; i++) {
ValueUuid uuid = ValueUuid.getNewRandom();
String s = uuid.getString();
s = s.replace('-', '_').toUpperCase();
sequenceName = "SYSTEM_SEQUENCE_" + s;
if (schema.findSequence(sequenceName) == null) {
break;
}
}
Sequence sequence = new Sequence(schema, id, sequenceName, true);
sequence.setStartValue(start);
sequence.setIncrement(increment);
if (!temporary) {
session.getDatabase().addSchemaObject(session, sequence);
}
setAutoIncrement(false, 0, 0);
SequenceValue seq = new SequenceValue(sequence);
setDefaultExpression(session, seq);
setSequence(sequence);
}
public void prepareExpression(Session session) throws SQLException {
if (defaultExpression != null) {
computeTableFilter = new TableFilter(session, table, null, false, null);
defaultExpression.mapColumns(computeTableFilter, 0);
defaultExpression = defaultExpression.optimize(session);
}
}
public String getCreateSQL() {
StringBuffer buff = new StringBuffer();
if (name != null) {
buff.append(Parser.quoteIdentifier(name));
buff.append(' ');
}
if (originalSQL != null) {
buff.append(originalSQL);
} else {
buff.append(DataType.getDataType(type).name);
switch (type) {
case Value.DECIMAL:
buff.append("(");
buff.append(precision);
buff.append(", ");
buff.append(scale);
buff.append(")");
break;
case Value.BYTES:
case Value.STRING:
case Value.STRING_IGNORECASE:
case Value.STRING_FIXED:
if (precision < Integer.MAX_VALUE) {
buff.append("(");
buff.append(precision);
buff.append(")");
}
break;
default:
}
}
if (defaultExpression != null) {
String sql = defaultExpression.getSQL();
if (sql != null) {
if (isComputed) {
buff.append(" AS ");
buff.append(sql);
} else if (defaultExpression != null) {
buff.append(" DEFAULT ");
buff.append(sql);
}
}
}
if (!nullable) {
buff.append(" NOT NULL");
}
if (convertNullToDefault) {
buff.append(" NULL_TO_DEFAULT");
}
if (sequence != null) {
buff.append(" SEQUENCE ");
buff.append(sequence.getSQL());
}
if (selectivity != 0) {
buff.append(" SELECTIVITY ");
buff.append(selectivity);
}
if (checkConstraint != null) {
buff.append(" CHECK ");
buff.append(checkConstraintSQL);
}
if (comment != null) {
buff.append(" COMMENT ");
buff.append(StringUtils.quoteStringSQL(comment));
}
return buff.toString();
}
public boolean getNullable() {
return nullable;
}
public void setOriginalSQL(String original) {
originalSQL = original;
}
public String getOriginalSQL() {
return originalSQL;
}
public Expression getDefaultExpression() {
return defaultExpression;
}
public boolean getAutoIncrement() {
return autoIncrement;
}
public void setAutoIncrement(boolean autoInc, long start, long increment) {
this.autoIncrement = autoInc;
this.start = start;
this.increment = increment;
this.nullable = false;
if (autoInc) {
convertNullToDefault = true;
}
}
public void setConvertNullToDefault(boolean convert) {
this.convertNullToDefault = convert;
}
public void rename(String newName) {
this.name = newName;
}
public void setSequence(Sequence sequence) {
this.sequence = sequence;
}
public Sequence getSequence() {
return sequence;
}
/**
* Get the selectivity of the column. Selectivity 100 means values are
* unique, 10 means every distinct value appears 10 times on average.
*
* @return the selectivity
*/
public int getSelectivity() {
return selectivity == 0 ? Constants.SELECTIVITY_DEFAULT : selectivity;
}
/**
* Set the new selectivity of a column.
*
* @param selectivity the new value
*/
public void setSelectivity(int selectivity) {
selectivity = selectivity < 0 ? 0 : (selectivity > 100 ? 100 : selectivity);
this.selectivity = selectivity;
}
public void addCheckConstraint(Session session, Expression expr) throws SQLException {
resolver = new SingleColumnResolver(this);
synchronized (this) {
String oldName = name;
if (name == null) {
name = "VALUE";
}
expr.mapColumns(resolver, 0);
name = oldName;
}
expr = expr.optimize(session);
resolver.setValue(ValueNull.INSTANCE);
// check if the column is mapped
synchronized (this) {
expr.getValue(session);
}
if (checkConstraint == null) {
checkConstraint = expr;
} else {
checkConstraint = new ConditionAndOr(ConditionAndOr.AND, checkConstraint, expr);
}
checkConstraintSQL = getCheckConstraintSQL(session, name);
}
public Expression getCheckConstraint(Session session, String asColumnName) throws SQLException {
if (checkConstraint == null) {
return null;
}
Parser parser = new Parser(session);
String sql;
synchronized (this) {
String oldName = name;
name = asColumnName;
sql = checkConstraint.getSQL();
name = oldName;
}
Expression expr = parser.parseExpression(sql);
return expr;
}
public String getDefaultSQL() {
return defaultExpression == null ? null : defaultExpression.getSQL();
}
public int getPrecisionAsInt() {
return MathUtils.convertLongToInt(precision);
}
public DataType getDataType() {
return DataType.getDataType(type);
}
public String getCheckConstraintSQL(Session session, String name) throws SQLException {
Expression constraint = getCheckConstraint(session, name);
return constraint == null ? "" : constraint.getSQL();
}
public void setComment(String comment) {
this.comment = comment;
}
public String getComment() {
return comment;
}
public void setPrimaryKey(boolean primaryKey) {
this.primaryKey = primaryKey;
}
boolean isEverything(ExpressionVisitor visitor) {
if (visitor.type == ExpressionVisitor.GET_DEPENDENCIES) {
if (sequence != null) {
visitor.getDependencies().add(sequence);
}
}
if (defaultExpression != null && !defaultExpression.isEverything(visitor)) {
return false;
}
if (checkConstraint != null && !checkConstraint.isEverything(visitor)) {
return false;
}
return true;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -