use of de.neemann.digital.hdl.model2.expression.Expression in project h2database by h2database.
the class Parser method parseSelectSimpleSelectPart.
private void parseSelectSimpleSelectPart(Select command) {
Select temp = currentSelect;
// make sure aggregate functions will not work in TOP and LIMIT
currentSelect = null;
if (readIf("TOP")) {
// can't read more complex expressions here because
// SELECT TOP 1 +? A FROM TEST could mean
// SELECT TOP (1+?) A FROM TEST or
// SELECT TOP 1 (+?) AS A FROM TEST
Expression limit = readTerm().optimize(session);
command.setLimit(limit);
} else if (readIf("LIMIT")) {
Expression offset = readTerm().optimize(session);
command.setOffset(offset);
Expression limit = readTerm().optimize(session);
command.setLimit(limit);
}
currentSelect = temp;
if (readIf("DISTINCT")) {
command.setDistinct(true);
} else {
readIf("ALL");
}
ArrayList<Expression> expressions = New.arrayList();
do {
if (readIf("*")) {
expressions.add(new Wildcard(null, null));
} else {
Expression expr = readExpression();
if (readIf("AS") || currentTokenType == IDENTIFIER) {
String alias = readAliasIdentifier();
boolean aliasColumnName = database.getSettings().aliasColumnName;
aliasColumnName |= database.getMode().aliasColumnName;
expr = new Alias(expr, alias, aliasColumnName);
}
expressions.add(expr);
}
} while (readIf(","));
command.setExpressions(expressions);
}
use of de.neemann.digital.hdl.model2.expression.Expression in project h2database by h2database.
the class Parser method readTableFilter.
private TableFilter readTableFilter() {
Table table;
String alias = null;
if (readIf("(")) {
if (isSelect()) {
Query query = parseSelectUnion();
read(")");
query.setParameterList(new ArrayList<>(parameters));
query.init();
Session s;
if (createView != null) {
s = database.getSystemSession();
} else {
s = session;
}
alias = session.getNextSystemIdentifier(sqlCommand);
table = TableView.createTempView(s, session.getUser(), alias, query, currentSelect);
} else {
TableFilter top;
top = readTableFilter();
top = readJoin(top);
read(")");
alias = readFromAlias(null);
if (alias != null) {
top.setAlias(alias);
ArrayList<String> derivedColumnNames = readDerivedColumnNames();
if (derivedColumnNames != null) {
top.setDerivedColumns(derivedColumnNames);
}
}
return top;
}
} else if (readIf("VALUES")) {
table = parseValuesTable(0).getTable();
} else {
String tableName = readIdentifierWithSchema(null);
Schema schema = getSchema();
boolean foundLeftBracket = readIf("(");
if (foundLeftBracket && readIf("INDEX")) {
// Sybase compatibility with
// "select * from test (index table1_index)"
readIdentifierWithSchema(null);
read(")");
foundLeftBracket = false;
}
if (foundLeftBracket) {
Schema mainSchema = database.getSchema(Constants.SCHEMA_MAIN);
if (equalsToken(tableName, RangeTable.NAME) || equalsToken(tableName, RangeTable.ALIAS)) {
Expression min = readExpression();
read(",");
Expression max = readExpression();
if (readIf(",")) {
Expression step = readExpression();
read(")");
table = new RangeTable(mainSchema, min, max, step, false);
} else {
read(")");
table = new RangeTable(mainSchema, min, max, false);
}
} else {
Expression expr = readFunction(schema, tableName);
if (!(expr instanceof FunctionCall)) {
throw getSyntaxError();
}
FunctionCall call = (FunctionCall) expr;
if (!call.isDeterministic()) {
recompileAlways = true;
}
table = new FunctionTable(mainSchema, session, expr, call);
}
} else if (equalsToken("DUAL", tableName)) {
table = getDualTable(false);
} else if (database.getMode().sysDummy1 && equalsToken("SYSDUMMY1", tableName)) {
table = getDualTable(false);
} else {
table = readTableOrView(tableName);
}
}
ArrayList<String> derivedColumnNames = null;
IndexHints indexHints = null;
// for backward compatibility, handle case where USE is a table alias
if (readIf("USE")) {
if (readIf("INDEX")) {
indexHints = parseIndexHints(table);
} else {
alias = "USE";
derivedColumnNames = readDerivedColumnNames();
}
} else {
alias = readFromAlias(alias);
if (alias != null) {
derivedColumnNames = readDerivedColumnNames();
// if alias present, a second chance to parse index hints
if (readIf("USE")) {
read("INDEX");
indexHints = parseIndexHints(table);
}
}
}
// inherit alias for CTE as views from table name
if (table.isView() && table.isTableExpression() && alias == null) {
alias = table.getName();
}
TableFilter filter = new TableFilter(session, table, alias, rightsChecked, currentSelect, orderInFrom++, indexHints);
if (derivedColumnNames != null) {
filter.setDerivedColumns(derivedColumnNames);
}
return filter;
}
use of de.neemann.digital.hdl.model2.expression.Expression in project h2database by h2database.
the class CreateTable method generateColumnsFromQuery.
private void generateColumnsFromQuery() {
int columnCount = asQuery.getColumnCount();
ArrayList<Expression> expressions = asQuery.getExpressions();
ColumnNamer columnNamer = new ColumnNamer(session);
for (int i = 0; i < columnCount; i++) {
Expression expr = expressions.get(i);
int type = expr.getType();
String name = columnNamer.getColumnName(expr, i, expr.getAlias());
long precision = expr.getPrecision();
int displaySize = expr.getDisplaySize();
DataType dt = DataType.getDataType(type);
if (precision > 0 && (dt.defaultPrecision == 0 || (dt.defaultPrecision > precision && dt.defaultPrecision < Byte.MAX_VALUE))) {
// dont' set precision to MAX_VALUE if this is the default
precision = dt.defaultPrecision;
}
int scale = expr.getScale();
if (scale > 0 && (dt.defaultScale == 0 || (dt.defaultScale > scale && dt.defaultScale < precision))) {
scale = dt.defaultScale;
}
if (scale > precision) {
precision = scale;
}
String[] enumerators = null;
if (dt.type == Value.ENUM) {
/**
* Only columns of tables may be enumerated.
*/
if (!(expr instanceof ExpressionColumn)) {
throw DbException.get(ErrorCode.GENERAL_ERROR_1, "Unable to resolve enumerators of expression");
}
enumerators = ((ExpressionColumn) expr).getColumn().getEnumerators();
}
Column col = new Column(name, type, precision, scale, displaySize, enumerators);
addColumn(col);
}
}
use of de.neemann.digital.hdl.model2.expression.Expression in project h2database by h2database.
the class IndexCondition method getCurrentValueList.
/**
* Get the current value list of the expression. The value list is of the
* same type as the column, distinct, and sorted.
*
* @param session the session
* @return the value list
*/
public Value[] getCurrentValueList(Session session) {
HashSet<Value> valueSet = new HashSet<>();
for (Expression e : expressionList) {
Value v = e.getValue(session);
v = column.convert(v);
valueSet.add(v);
}
Value[] array = valueSet.toArray(new Value[valueSet.size()]);
final CompareMode mode = session.getDatabase().getCompareMode();
Arrays.sort(array, new Comparator<Value>() {
@Override
public int compare(Value o1, Value o2) {
return o1.compareTo(o2, mode);
}
});
return array;
}
use of de.neemann.digital.hdl.model2.expression.Expression in project h2database by h2database.
the class TestColumnNamer method test.
@Override
public void test() {
ColumnNamer columnNamer = new ColumnNamer(null);
columnNamer.getConfiguration().configure("MAX_IDENTIFIER_LENGTH = 30");
columnNamer.getConfiguration().configure("REGULAR_EXPRESSION_MATCH_ALLOWED = '[A-Za-z0-9_]+'");
columnNamer.getConfiguration().configure("REGULAR_EXPRESSION_MATCH_DISALLOWED = '[^A-Za-z0-9_]+'");
columnNamer.getConfiguration().configure("DEFAULT_COLUMN_NAME_PATTERN = 'colName$$'");
columnNamer.getConfiguration().configure("GENERATE_UNIQUE_COLUMN_NAMES = 1");
int index = 0;
for (String id : ids) {
Expression columnExp = ValueExpression.getDefault();
String newColumnName = columnNamer.getColumnName(columnExp, index + 1, id);
assertNotNull(newColumnName);
assertTrue(newColumnName.length() <= 30);
assertTrue(newColumnName.length() >= 1);
assertEquals(newColumnName, expectedColumnName[index]);
index++;
}
}
Aggregations