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);
   }
 }
 SelectListColumnResolver(Select select) {
   this.select = select;
   int columnCount = select.getColumnCount();
   columns = new Column[columnCount];
   expressions = new Expression[columnCount];
   ArrayList<Expression> columnList = select.getExpressions();
   for (int i = 0; i < columnCount; i++) {
     Expression expr = columnList.get(i);
     Column column = new Column(expr.getAlias(), Value.NULL);
     column.setTable(null, i);
     columns[i] = column;
     expressions[i] = expr.getNonAliasExpression();
   }
 }
Exemple #3
0
 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();
   }
 }
Exemple #4
0
 @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);
 }
Exemple #5
0
 /**
  * Initialize the order by list. This call may extend the expressions list.
  *
  * @param session the session
  * @param expressions the select list expressions
  * @param expressionSQL the select list SQL snippets
  * @param orderList the order by list
  * @param visible the number of visible columns in the select list
  * @param mustBeInResult all order by expressions must be in the select list
  * @param filters the table filters
  */
 static void initOrder(
     Session session,
     ArrayList<Expression> expressions,
     ArrayList<String> expressionSQL,
     ArrayList<SelectOrderBy> orderList,
     int visible,
     boolean mustBeInResult,
     ArrayList<TableFilter> filters) {
   Database db = session.getDatabase();
   for (SelectOrderBy o : orderList) {
     Expression e = o.expression;
     if (e == null) {
       continue;
     }
     // special case: SELECT 1 AS A FROM DUAL ORDER BY A
     // (oracle supports it, but only in order by, not in group by and
     // not in having):
     // SELECT 1 AS A FROM DUAL ORDER BY -A
     boolean isAlias = false;
     int idx = expressions.size();
     if (e instanceof ExpressionColumn) {
       // order by expression
       ExpressionColumn exprCol = (ExpressionColumn) e;
       String tableAlias = exprCol.getOriginalTableAliasName();
       String col = exprCol.getOriginalColumnName();
       for (int j = 0; j < visible; j++) {
         boolean found = false;
         Expression ec = expressions.get(j);
         if (ec instanceof ExpressionColumn) {
           // select expression
           ExpressionColumn c = (ExpressionColumn) ec;
           found = db.equalsIdentifiers(col, c.getColumnName());
           if (found && tableAlias != null) {
             String ca = c.getOriginalTableAliasName();
             if (ca == null) {
               found = false;
               if (filters != null) {
                 // select id from test order by test.id
                 for (int i = 0, size = filters.size(); i < size; i++) {
                   TableFilter f = filters.get(i);
                   if (db.equalsIdentifiers(f.getTableAlias(), tableAlias)) {
                     found = true;
                     break;
                   }
                 }
               }
             } else {
               found = db.equalsIdentifiers(ca, tableAlias);
             }
           }
         } else if (!(ec instanceof Alias)) {
           continue;
         } else if (tableAlias == null && db.equalsIdentifiers(col, ec.getAlias())) {
           found = true;
         } else {
           Expression ec2 = ec.getNonAliasExpression();
           if (ec2 instanceof ExpressionColumn) {
             ExpressionColumn c2 = (ExpressionColumn) ec2;
             String ta = exprCol.getSQL();
             String tb = c2.getSQL();
             String s2 = c2.getColumnName();
             found = db.equalsIdentifiers(col, s2);
             if (!db.equalsIdentifiers(ta, tb)) {
               found = false;
             }
           }
         }
         if (found) {
           idx = j;
           isAlias = true;
           break;
         }
       }
     } else {
       String s = e.getSQL();
       if (expressionSQL != null) {
         for (int j = 0, size = expressionSQL.size(); j < size; j++) {
           String s2 = expressionSQL.get(j);
           if (db.equalsIdentifiers(s2, s)) {
             idx = j;
             isAlias = true;
             break;
           }
         }
       }
     }
     if (!isAlias) {
       if (mustBeInResult) {
         throw DbException.get(ErrorCode.ORDER_BY_NOT_IN_RESULT, e.getSQL());
       }
       expressions.add(e);
       String sql = e.getSQL();
       expressionSQL.add(sql);
     }
     o.columnIndexExpr = ValueExpression.get(ValueInt.get(idx + 1));
     Expression expr = expressions.get(idx).getNonAliasExpression();
     o.expression = expr;
   }
 }
  public void init() {
    if (SysProperties.CHECK && checkInit) {
      DbException.throwInternalError();
    }
    expandColumnList();
    visibleColumnCount = expressions.size();
    ArrayList<String> expressionSQL;
    if (orderList != null || group != null) {
      expressionSQL = New.arrayList();
      for (int i = 0; i < visibleColumnCount; i++) {
        Expression expr = expressions.get(i);
        expr = expr.getNonAliasExpression();
        String sql = expr.getSQL();
        expressionSQL.add(sql);
      }
    } else {
      expressionSQL = null;
    }
    if (orderList != null) {
      initOrder(
          session, expressions, expressionSQL, orderList, visibleColumnCount, distinct, filters);
    }
    distinctColumnCount = expressions.size();
    if (having != null) {
      expressions.add(having);
      havingIndex = expressions.size() - 1;
      having = null;
    } else {
      havingIndex = -1;
    }

    Database db = session.getDatabase();

    // first the select list (visible columns),
    // then 'ORDER BY' expressions,
    // then 'HAVING' expressions,
    // and 'GROUP BY' expressions at the end
    if (group != null) {
      int size = group.size();
      int expSize = expressionSQL.size();
      groupIndex = new int[size];
      for (int i = 0; i < size; i++) {
        Expression expr = group.get(i);
        String sql = expr.getSQL();
        int found = -1;
        for (int j = 0; j < expSize; j++) {
          String s2 = expressionSQL.get(j);
          if (db.equalsIdentifiers(s2, sql)) {
            found = j;
            break;
          }
        }
        if (found < 0) {
          // special case: GROUP BY a column alias
          for (int j = 0; j < expSize; j++) {
            Expression e = expressions.get(j);
            if (db.equalsIdentifiers(sql, e.getAlias())) {
              found = j;
              break;
            }
          }
        }
        if (found < 0) {
          int index = expressions.size();
          groupIndex[i] = index;
          expressions.add(expr);
        } else {
          groupIndex[i] = found;
        }
      }
      groupByExpression = new boolean[expressions.size()];
      for (int gi : groupIndex) {
        groupByExpression[gi] = true;
      }
      group = null;
    }
    // map columns in select list and condition
    for (TableFilter f : filters) {
      for (Expression expr : expressions) {
        expr.mapColumns(f, 0);
      }
      if (condition != null) {
        condition.mapColumns(f, 0);
      }
    }
    if (havingIndex >= 0) {
      Expression expr = expressions.get(havingIndex);
      SelectListColumnResolver res = new SelectListColumnResolver(this);
      expr.mapColumns(res, 0);
    }
    checkInit = true;
  }