if (column instanceof Field) { AbstractTable table = ((Field) column).getTable(); if (table instanceof Table) { String tableName = ((Table) table).getName().toLowerCase(); String columnName = ((Field) column).getName().toLowerCase(); ResultSet r = conn.getMetaData().getColumns(null, null, tableName, columnName); if (r.next()) { if (tableName.equals(r.getString(3)) && columnName.equals(r.getString(4))) { int columnType = r.getInt(5); if (!((columnType == Types.SMALLINT) || (columnType == Types.INTEGER) || (columnType == Types.BIGINT))) {
if (valueMap.containsKey(column)) { if (column instanceof Field) { AbstractTable table = ((Field) column).getTable(); if (table instanceof Table) { String tableName = ((Table) table).getName().toLowerCase(); String columnName = ((Field) column).getName().toLowerCase(); ResultSet r = conn.getMetaData().getColumns(null, null, tableName, columnName); if (r.next()) { if (tableName.equals(r.getString(3)) && columnName.equals(r.getString(4))) { int columnType = r.getInt(5); if (!((columnType == Types.SMALLINT) || (columnType == Types.INTEGER) || (columnType == Types.BIGINT))) { useOrderByField = false; LOG.info("Cannot generate order field for precomputed" + " table - column " + column.getSQLString() + " is type " + columnType); } } else {
    public PrecomputedTable(Query q, String name, Connection conn) {        if (q == null) {            throw (new NullPointerException("q cannot be null"));        }        if (name == null) {            throw (new NullPointerException("the name of a precomputed table cannot be null"));        }        this.q = q;        this.name = name;        // Now build the valueMap. Do not alter this Query from now on...        valueMap = new HashMap();        Iterator valueIter = q.getSelect().iterator();        while (valueIter.hasNext()) {            SelectValue value = (SelectValue) valueIter.next();            valueMap.put(value.getValue(), value);        }        // Now we should work out if we can create an order by field. First, we need to make sure        // that all the fields in the order by list are integer numbers (that is SMALLINT, INTEGER,        // and BIGINT).        boolean useOrderByField = (q.getOrderBy().size() > 1) && (q.getUnion().size() == 1);        try {            if (useOrderByField) {                Iterator orderByIter = q.getOrderBy().iterator();                while (orderByIter.hasNext() && useOrderByField) {                    AbstractValue column = (AbstractValue) orderByIter.next();                    if (column instanceof Field) {                        AbstractTable table = ((Field) column).getTable();                        if (table instanceof Table) {                            String tableName = ((Table) table).getName().toLowerCase();                            String columnName = ((Field) column).getName().toLowerCase();                            ResultSet r = conn.getMetaData().getColumns(null, null, tableName,                                    columnName);                            if (r.next()) {                                if (tableName.equals(r.getString(3))                                        && columnName.equals(r.getString(4))) {                                    int columnType = r.getInt(5);                                    if (!((columnType == Types.SMALLINT)                                                || (columnType == Types.INTEGER)                                                || (columnType == Types.BIGINT))) {                                        useOrderByField = false;                                        LOG.info("Cannot generate order field for precomputed"                                                + " table - column " + column.getSQLString()                                                + " is type " + columnType);                                    }                                } else {                                    useOrderByField = false;                                    LOG.error("getColumns returned wrong data for column "                                            + column.getSQLString());                                }                            } else {                                useOrderByField = false;                                LOG.error("getColumns return no data for column "                                        + column.getSQLString() + " in table " + tableName);                            }                            if (r.next()) {                                useOrderByField = false;                                LOG.error("getColumns returned too much data for column "                                        + column.getSQLString());                            }                        } else {                            useOrderByField = false;                            LOG.info("Cannot generate order field for precomputed table - column "                                    + column.getSQLString() + " does not belong to a Table");                        }                    } else {                        useOrderByField = false;                        LOG.info("Cannot generate order field for precomputed table - column "                                + column.getSQLString() + " is not a Field");                    }                }            }        } catch (SQLException e) {            useOrderByField = false;            LOG.error("Caught SQLException while examining order by fields: " + e);        }        if (useOrderByField) {            orderByField = "orderby_field";            List orderBy = q.getOrderBy();            StringBuffer extraBuffer = new StringBuffer();            for (int i = orderBy.size() - 1; i > 0; i--) {                extraBuffer.append("((" + ((SQLStringable) orderBy.get(orderBy.size() - 1 - i))                        .getSQLString() + "::numeric) * 1");                for (int o = 0; o < i; o++) {                    extraBuffer.append("00000000000000000000");                }                extraBuffer.append(") + ");            }            extraBuffer.append("(" + ((SQLStringable) orderBy.get(orderBy.size() - 1))                    .getSQLString() + "::numeric) AS orderby_field");            generationSqlString = "CREATE TABLE " + name + " AS "                + q.getSQLStringForPrecomputedTable(extraBuffer.toString());        } else {            orderByField = null;            generationSqlString = "CREATE TABLE " + name + " AS " + q.getSQLString();        }    }