private void generateColumnsFromQuery() { int columnCount = asQuery.getColumnCount(); ArrayList<Expression> expressions = asQuery.getExpressions(); for (int i = 0; i < columnCount; i++) { Expression expr = expressions.get(i); int type = expr.getType(); String name = 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; } Column col = new Column(name, type, precision, scale, displaySize); addColumn(col); } }
private void initColumnsAndTables(Session session) { Column[] cols; removeViewFromTables(); try { Query query = recompileQuery(session); tables = New.arrayList(query.getTables()); ArrayList<Expression> expressions = query.getExpressions(); ArrayList<Column> list = New.arrayList(); for (int i = 0; i < query.getColumnCount(); i++) { Expression expr = expressions.get(i); String name = null; if (columnNames != null && columnNames.length > i) { name = columnNames[i]; } if (name == null) { name = expr.getAlias(); } int type = expr.getType(); long precision = expr.getPrecision(); int scale = expr.getScale(); int displaySize = expr.getDisplaySize(); Column col = new Column(name, type, precision, scale, displaySize); col.setTable(this, i); list.add(col); } cols = new Column[list.size()]; list.toArray(cols); createException = null; viewQuery = query; } catch (DbException e) { createException = e; // if it can't be compiled, then it's a 'zero column table' // this avoids problems when creating the view when opening the // database tables = New.arrayList(); cols = new Column[0]; if (recursive && columnNames != null) { cols = new Column[columnNames.length]; for (int i = 0; i < columnNames.length; i++) { cols[i] = new Column(columnNames[i], Value.STRING); } index.setRecursive(true); createException = null; } } setColumns(cols); if (getId() != 0) { addViewToTables(); } }
@Override public void prepare() { if (isPrepared) { // sometimes a subquery is prepared twice (CREATE TABLE AS SELECT) return; } if (SysProperties.CHECK && !checkInit) { DbException.throwInternalError("not initialized"); } isPrepared = true; left.prepare(); right.prepare(); int len = left.getColumnCount(); // set the correct expressions now expressions = New.arrayList(); ArrayList<Expression> le = left.getExpressions(); ArrayList<Expression> re = right.getExpressions(); for (int i = 0; i < len; i++) { Expression l = le.get(i); Expression r = re.get(i); int type = Value.getHigherOrder(l.getType(), r.getType()); long prec = Math.max(l.getPrecision(), r.getPrecision()); int scale = Math.max(l.getScale(), r.getScale()); int displaySize = Math.max(l.getDisplaySize(), r.getDisplaySize()); Column col = new Column(l.getAlias(), type, prec, scale, displaySize); Expression e = new ExpressionColumn(session.getDatabase(), col); expressions.add(e); } if (orderList != null) { initOrder(session, expressions, null, orderList, getColumnCount(), true, null); sort = prepareOrder(orderList, expressions.size()); orderList = null; } expressionArray = new Expression[expressions.size()]; expressions.toArray(expressionArray); }