use of de.neemann.digital.hdl.model2.expression.Expression in project h2database by h2database.
the class SortOrder method getColumn.
/**
* Get the column for the given table filter, if the sort column is for this
* filter.
*
* @param index the column index (0, 1,..)
* @param filter the table filter
* @return the column, or null
*/
public Column getColumn(int index, TableFilter filter) {
if (orderList == null) {
return null;
}
SelectOrderBy order = orderList.get(index);
Expression expr = order.expression;
if (expr == null) {
return null;
}
expr = expr.getNonAliasExpression();
if (expr.isConstant()) {
return null;
}
if (!(expr instanceof ExpressionColumn)) {
return null;
}
ExpressionColumn exprCol = (ExpressionColumn) expr;
if (exprCol.getTableFilter() != filter) {
return null;
}
return exprCol.getColumn();
}
use of de.neemann.digital.hdl.model2.expression.Expression in project h2database by h2database.
the class TableView method createQueryColumnTemplateList.
/**
* Creates a list of column templates from a query (usually from WITH query,
* but could be any query)
*
* @param cols - an optional list of column names (can be specified by WITH
* clause overriding usual select names)
* @param theQuery - the query object we want the column list for
* @param querySQLOutput - array of length 1 to receive extra 'output' field
* in addition to return value - containing the SQL query of the
* Query object
* @return a list of column object returned by withQuery
*/
public static List<Column> createQueryColumnTemplateList(String[] cols, Query theQuery, String[] querySQLOutput) {
List<Column> columnTemplateList = new ArrayList<>();
theQuery.prepare();
// String array of length 1 is to receive extra 'output' field in addition to
// return value
querySQLOutput[0] = StringUtils.cache(theQuery.getPlanSQL());
ColumnNamer columnNamer = new ColumnNamer(theQuery.getSession());
ArrayList<Expression> withExpressions = theQuery.getExpressions();
for (int i = 0; i < withExpressions.size(); ++i) {
Expression columnExp = withExpressions.get(i);
// use the passed in column name if supplied, otherwise use alias
// (if found) otherwise use column name derived from column
// expression
String columnName = columnNamer.getColumnName(columnExp, i, cols);
columnTemplateList.add(new Column(columnName, columnExp.getType()));
}
return columnTemplateList;
}
use of de.neemann.digital.hdl.model2.expression.Expression in project h2database by h2database.
the class TableView method initColumnsAndTables.
private void initColumnsAndTables(Session session, boolean literalsChecked) {
Column[] cols;
removeCurrentViewFromOtherTables();
setTableExpression(isTableExpression);
try {
Query compiledQuery = compileViewQuery(session, querySQL, literalsChecked, getName());
this.querySQL = compiledQuery.getPlanSQL();
tables = new ArrayList<>(compiledQuery.getTables());
ArrayList<Expression> expressions = compiledQuery.getExpressions();
ArrayList<Column> list = New.arrayList();
ColumnNamer columnNamer = new ColumnNamer(session);
for (int i = 0, count = compiledQuery.getColumnCount(); i < count; i++) {
Expression expr = expressions.get(i);
String name = null;
int type = Value.UNKNOWN;
if (columnTemplates != null && columnTemplates.length > i) {
name = columnTemplates[i].getName();
type = columnTemplates[i].getType();
}
if (name == null) {
name = expr.getAlias();
}
name = columnNamer.getColumnName(expr, i, name);
if (type == Value.UNKNOWN) {
type = expr.getType();
}
long precision = expr.getPrecision();
int scale = expr.getScale();
int displaySize = expr.getDisplaySize();
String[] enumerators = null;
if (type == Value.ENUM) {
if (expr instanceof ExpressionColumn) {
enumerators = ((ExpressionColumn) expr).getColumn().getEnumerators();
}
}
Column col = new Column(name, type, precision, scale, displaySize, enumerators);
col.setTable(this, i);
// Fetch check constraint from view column source
ExpressionColumn fromColumn = null;
if (expr instanceof ExpressionColumn) {
fromColumn = (ExpressionColumn) expr;
} else if (expr instanceof Alias) {
Expression aliasExpr = expr.getNonAliasExpression();
if (aliasExpr instanceof ExpressionColumn) {
fromColumn = (ExpressionColumn) aliasExpr;
}
}
if (fromColumn != null) {
Expression checkExpression = fromColumn.getColumn().getCheckConstraint(session, name);
if (checkExpression != null) {
col.addCheckConstraint(session, checkExpression);
}
}
list.add(col);
}
cols = list.toArray(new Column[0]);
createException = null;
viewQuery = compiledQuery;
} catch (DbException e) {
e.addSQL(getCreateSQL());
createException = e;
// table expression query.
if (isRecursiveQueryExceptionDetected(createException)) {
this.isRecursiveQueryDetected = true;
}
tables = New.arrayList();
cols = new Column[0];
if (allowRecursive && columnTemplates != null) {
cols = new Column[columnTemplates.length];
for (int i = 0; i < columnTemplates.length; i++) {
cols[i] = columnTemplates[i].getClone();
}
index.setRecursive(true);
createException = null;
}
}
setColumns(cols);
if (getId() != 0) {
addDependentViewToTables();
}
}
use of de.neemann.digital.hdl.model2.expression.Expression in project h2database by h2database.
the class Column method validateConvertUpdateSequence.
/**
* Validate the value, convert it if required, and update the sequence value
* if required. If the value is null, the default value (NULL if no default
* is set) is returned. Check constraints are validated as well.
*
* @param session the session
* @param value the value or null
* @return the new or converted value
*/
public Value validateConvertUpdateSequence(Session session, Value value) {
// take a local copy of defaultExpression to avoid holding the lock
// while calling getValue
final Expression localDefaultExpression;
synchronized (this) {
localDefaultExpression = defaultExpression;
}
Mode mode = session.getDatabase().getMode();
if (value == null) {
if (localDefaultExpression == null) {
value = ValueNull.INSTANCE;
} else {
value = convert(localDefaultExpression.getValue(session), mode);
if (!localDefaultExpression.isConstant()) {
session.getGeneratedKeys().add(this);
}
if (primaryKey) {
session.setLastIdentity(value);
}
}
}
if (value == ValueNull.INSTANCE) {
if (convertNullToDefault) {
value = convert(localDefaultExpression.getValue(session), mode);
if (!localDefaultExpression.isConstant()) {
session.getGeneratedKeys().add(this);
}
}
if (value == ValueNull.INSTANCE && !nullable) {
if (mode.convertInsertNullToZero) {
DataType dt = DataType.getDataType(type);
if (dt.decimal) {
value = ValueInt.get(0).convertTo(type);
} else if (dt.type == Value.TIMESTAMP) {
value = ValueTimestamp.fromMillis(session.getTransactionStart());
} else if (dt.type == Value.TIMESTAMP_TZ) {
long ms = session.getTransactionStart();
value = ValueTimestampTimeZone.fromDateValueAndNanos(DateTimeUtils.dateValueFromDate(ms), DateTimeUtils.nanosFromDate(ms), (short) 0);
} else if (dt.type == Value.TIME) {
value = ValueTime.fromNanos(0);
} else if (dt.type == Value.DATE) {
value = ValueDate.fromMillis(session.getTransactionStart());
} else {
value = ValueString.get("").convertTo(type);
}
} else {
throw DbException.get(ErrorCode.NULL_NOT_ALLOWED, name);
}
}
}
if (checkConstraint != null) {
resolver.setValue(value);
Value v;
synchronized (this) {
v = checkConstraint.getValue(session);
}
// Both TRUE and NULL are ok
if (v != ValueNull.INSTANCE && !v.getBoolean()) {
throw DbException.get(ErrorCode.CHECK_CONSTRAINT_VIOLATED_1, checkConstraint.getSQL());
}
}
value = value.convertScale(mode.convertOnlyToSmallerScale, scale);
if (precision > 0) {
if (!value.checkPrecision(precision)) {
String s = value.getTraceSQL();
if (s.length() > 127) {
s = s.substring(0, 128) + "...";
}
throw DbException.get(ErrorCode.VALUE_TOO_LONG_2, getCreateSQL(), s + " (" + value.getPrecision() + ")");
}
}
if (isEnumerated() && value != ValueNull.INSTANCE) {
if (!ValueEnum.isValid(enumerators, value)) {
String s = value.getTraceSQL();
if (s.length() > 127) {
s = s.substring(0, 128) + "...";
}
throw DbException.get(ErrorCode.ENUM_VALUE_NOT_PERMITTED, getCreateSQL(), s);
}
value = ValueEnum.get(enumerators, value.getInt());
}
updateSequenceIfRequired(session, value);
return value;
}
use of de.neemann.digital.hdl.model2.expression.Expression in project OpenNotebook by jaltekruse.
the class BasicDistributiveProperty method generateExpression.
@Override
protected Node[] generateExpression(int difficulty) throws NodeException {
Node[] n = new Node[2];
if (difficulty == ProblemGenerator.EASY) {
double bigNum = ExUtil.randomInt(2, 10, true) * 10, smallNum = ExUtil.randomInt(1, 5, true), multiplier = ExUtil.randomInt(2, 9, true);
if (ExUtil.randomBoolean()) {
n[0] = new Expression(new Operator.Addition(), new Number(bigNum), new Number(smallNum));
} else {
n[0] = new Expression(new Operator.Subtraction(), new Number(bigNum), new Number(smallNum));
}
n[0].setDisplayParentheses(true);
n[0] = ExUtil.randomlyStaggerOperation(new Operator.Multiplication(), n[0], new Number(multiplier));
} else if (difficulty == ProblemGenerator.MEDIUM) {
double multiplier = ExUtil.randomInt(2, 9, true);
n[0] = ExUtil.randomLinearExpression(ExUtil.randomVarName(), 1, 8);
n[0].setDisplayParentheses(true);
n[0].setDisplayParentheses(true);
n[0] = ExUtil.randomlyStaggerOperation(new Operator.Multiplication(), n[0], new Number(multiplier));
} else {
double multiplier = ExUtil.randomInt(2, 14, true);
String varName = ExUtil.randomVarName();
n[0] = ExUtil.randomLinearExpression(varName, 1, 12);
Node newNode = null;
if (ExUtil.randomBoolean())
newNode = new Number(ExUtil.randomInt(2, 15, true));
else
newNode = ExUtil.randomTerm(1, varName, 2, 9);
if (ExUtil.randomBoolean()) {
n[0] = ExUtil.randomlyStaggerOperation(new Operator.Addition(), n[0], newNode);
} else {
n[0] = ExUtil.randomlyStaggerOperation(new Operator.Subtraction(), n[0], newNode);
}
n[0].setDisplayParentheses(true);
n[0] = ExUtil.randomlyStaggerOperation(new Operator.Multiplication(), n[0], new Number(multiplier));
}
n[1] = n[0].cloneNode().smartNumericSimplify().standardFormat();
return n;
}
Aggregations