Search in sources :

Example 1 with IntValueHashMap

use of org.hsqldb_voltpatches.lib.IntValueHashMap in project voltdb by VoltDB.

the class QuerySpecification method setUpdatability.

void setUpdatability() {
    if (isAggregated || isGrouped || isDistinctSelect || !isTopLevel) {
        return;
    }
    if (sortAndSlice.hasLimit() || sortAndSlice.hasOrder()) {
        return;
    }
    if (rangeVariables.length != 1) {
        return;
    }
    RangeVariable rangeVar = rangeVariables[0];
    Table table = rangeVar.getTable();
    Table baseTable = table.getBaseTable();
    isInsertable = table.isInsertable();
    isUpdatable = table.isUpdatable();
    if (!isInsertable && !isUpdatable) {
        return;
    }
    IntValueHashMap columns = new IntValueHashMap();
    boolean[] checkList;
    int[] baseColumnMap = table.getBaseTableColumnMap();
    int[] columnMap = new int[indexLimitVisible];
    for (int i = 0; i < indexLimitVisible; i++) {
        Expression expression = exprColumns[i];
        if (expression.getType() == OpTypes.COLUMN) {
            String name = expression.getColumn().getName().name;
            if (columns.containsKey(name)) {
                columns.put(name, 1);
                continue;
            }
            columns.put(name, 0);
        }
    }
    isUpdatable = false;
    for (int i = 0; i < indexLimitVisible; i++) {
        if (accessibleColumns[i]) {
            Expression expression = exprColumns[i];
            if (expression.getType() == OpTypes.COLUMN) {
                String name = expression.getColumn().getName().name;
                if (columns.get(name) == 0) {
                    int index = table.findColumn(name);
                    columnMap[i] = baseColumnMap[index];
                    if (columnMap[i] != -1) {
                        isUpdatable = true;
                    }
                    continue;
                }
            }
        }
        columnMap[i] = -1;
        isInsertable = false;
    }
    if (isInsertable) {
        checkList = baseTable.getColumnCheckList(columnMap);
        for (int i = 0; i < checkList.length; i++) {
            if (checkList[i]) {
                continue;
            }
            ColumnSchema column = baseTable.getColumn(i);
            if (column.isIdentity() || column.isGenerated() || column.hasDefault() || column.isNullable()) {
            } else {
                isInsertable = false;
                break;
            }
        }
    }
    if (!isUpdatable) {
        isInsertable = false;
    }
    if (isUpdatable) {
        this.columnMap = columnMap;
        this.baseTable = baseTable;
        if (persistenceScope == TableBase.SCOPE_STATEMENT) {
            return;
        }
        indexLimitRowId++;
        indexLimitData = indexLimitRowId;
    }
}
Also used : IntValueHashMap(org.hsqldb_voltpatches.lib.IntValueHashMap)

Example 2 with IntValueHashMap

use of org.hsqldb_voltpatches.lib.IntValueHashMap in project voltdb by VoltDB.

the class QuerySpecification method setReferenceableColumns.

void setReferenceableColumns() {
    accessibleColumns = new boolean[indexLimitVisible];
    IntValueHashMap aliases = new IntValueHashMap();
    // Bundle up all the user defined aliases here.
    // We can't import java.util.Set because there is a Set
    // already imported into this class from Hsql itself.
    java.util.Set<String> userAliases = new java.util.HashSet<>();
    // Bundle up all the generated aliases here.
    java.util.Map<String, Integer> genAliases = new java.util.HashMap<>();
    for (int i = 0; i < indexLimitVisible; i++) {
        Expression expression = exprColumns[i];
        String alias = expression.getAlias();
        if (alias.length() == 0) {
            SimpleName name = HsqlNameManager.getAutoColumnName(i);
            expression.setAlias(name);
            genAliases.put(name.name, i);
            continue;
        }
        int index = aliases.get(alias, -1);
        userAliases.add(alias);
        if (index == -1) {
            aliases.put(alias, i);
            accessibleColumns[i] = true;
        } else {
            accessibleColumns[index] = false;
        }
    }
    for (java.util.Map.Entry<String, Integer> genAlias : genAliases.entrySet()) {
        String alias = genAlias.getKey();
        while (userAliases.contains(alias)) {
            alias = "_" + alias;
        }
        if (!alias.equals(genAlias.getKey())) {
            int idx = genAlias.getValue();
            SimpleName realAlias = HsqlNameManager.getAutoColumnName(alias);
            exprColumns[idx].setAlias(realAlias);
        }
    }
}
Also used : IntValueHashMap(org.hsqldb_voltpatches.lib.IntValueHashMap) SimpleName(org.hsqldb_voltpatches.HsqlNameManager.SimpleName) IntValueHashMap(org.hsqldb_voltpatches.lib.IntValueHashMap) IntValueHashMap(org.hsqldb_voltpatches.lib.IntValueHashMap) OrderedHashSet(org.hsqldb_voltpatches.lib.OrderedHashSet) HashSet(org.hsqldb_voltpatches.lib.HashSet) OrderedIntHashSet(org.hsqldb_voltpatches.lib.OrderedIntHashSet)

Example 3 with IntValueHashMap

use of org.hsqldb_voltpatches.lib.IntValueHashMap in project voltdb by VoltDB.

the class JDBCResultSet method findColumn.

//----------------------------------------------------------------
/**
     * <!-- start generic documentation -->
     * Maps the given <code>ResultSet</code> column label to its
     * <code>ResultSet</code> column index.
     * <!-- end generic documentation -->
     *
     * <!-- start release-specific documentation -->
     * <div class="ReleaseSpecificDocumentation">
     * <h3>HSQLDB-Specific Information:</h3> <p>
     *
     * Starting with 1.9.x, HSQLDB does an exhaustive search, backed by
     * a cache lookup (to improve performance for subsequent invocations with
     * a given input). <p>
     *
     * This is in response to an observation posted here: <p>
     *
     * http://sourceforge.net/forum/forum.php?thread_id=1388727&forum_id=73674<p>
     *
     * Upon careful investigation of the JDBC specification and the behaviour
     * of existing JDBC drivers, there is actually nothing preventing the
     * findColumn method from doing an exhaustive search, as long as it conforms
     * to the following rules (which describe the new implementation): <p>
     *
     * <ol>
     * <li> the entire search is case insensitive
     * <li> each search iteration occurs from leftmost to rightmost column,
     *      returning the first match encountered
     * <li> the first pass matches only bare column labels
     * <li> the second pass matches only simple column names
     * <li> further passes conform to the identifier qualification
     *      and identifier quoting rules of the engine
     * </ol>
     *
     * In this implementation, the SQL tokenizer is not employed, both because
     * it does not yet correctly handle greater than two part qualification
     * and also because is is not immediately considered important to do a
     * truly exhaustive search, handling the full range of possibly mixed quoted
     * and unquoted identifier components. <p>
     *
     * Instead: <p>
     * <ul>
     * <li> a third pass matches simple table-dot-column qualified names
     * <li> a fourth pass matches simple schema-dot-table-dot-column qualified column names
     * </ul>
     * </div>
     *
     * @param columnLabel the label for the column specified with the SQL AS clause.  If the SQL AS clause was not specified, then the label is the name of the column
     * @return the column index of the given column name
     * @exception SQLException if the <code>ResultSet</code> object
     * does not contain a column labeled <code>columnLabel</code>, a database access error occurs
     *  or this method is called on a closed result set
     */
public int findColumn(final String columnLabel) throws SQLException {
    checkClosed();
    if (columnLabel == null) {
        throw Util.nullArgument();
    }
    int columnIndex;
    // faster lookup for subsequent access
    if (columnMap != null) {
        columnIndex = columnMap.get(columnLabel, -1);
        if (columnIndex != -1) {
            return columnIndex;
        }
    }
    final String[] colLabels = resultMetaData.columnLabels;
    columnIndex = -1;
    // column labels first, to preference column aliases
    for (int i = 0; i < columnCount; i++) {
        if (columnLabel.equalsIgnoreCase(colLabels[i])) {
            columnIndex = i;
            break;
        }
    }
    final ColumnBase[] columns = resultMetaData.columns;
    // quoted column idents that *may* contain "."
    if (columnIndex < 0) {
        for (int i = 0; i < columnCount; i++) {
            if (columnLabel.equalsIgnoreCase(columns[i].getNameString())) {
                columnIndex = i;
                break;
            }
        }
    }
    // (we don't yet bother with catalog qualification)
    if (columnIndex < 0) {
        int position = columnLabel.indexOf('.');
        if (position < 0) {
            throw Util.sqlException(ErrorCode.JDBC_COLUMN_NOT_FOUND, columnLabel);
        }
        for (int i = 0; i < columnCount; i++) {
            final String tabName = columns[i].getTableNameString();
            if (tabName == null || tabName.length() == 0) {
                continue;
            }
            final String colName = columns[i].getNameString();
            if (columnLabel.equalsIgnoreCase(tabName + '.' + colName)) {
                columnIndex = i;
                break;
            }
            final String schemName = columns[i].getSchemaNameString();
            if (schemName == null || schemName.length() == 0) {
                continue;
            }
            String match = new StringBuffer(schemName).append('.').append(tabName).append('.').append(colName).toString();
            if (columnLabel.equalsIgnoreCase(match)) {
                columnIndex = i;
                break;
            }
        }
    }
    if (columnIndex < 0) {
        throw Util.sqlException(ErrorCode.JDBC_COLUMN_NOT_FOUND, columnLabel);
    }
    columnIndex++;
    if (columnMap == null) {
        columnMap = new IntValueHashMap();
    }
    columnMap.put(columnLabel, columnIndex);
    return columnIndex;
}
Also used : IntValueHashMap(org.hsqldb_voltpatches.lib.IntValueHashMap) ColumnBase(org.hsqldb_voltpatches.ColumnBase)

Aggregations

IntValueHashMap (org.hsqldb_voltpatches.lib.IntValueHashMap)3 ColumnBase (org.hsqldb_voltpatches.ColumnBase)1 SimpleName (org.hsqldb_voltpatches.HsqlNameManager.SimpleName)1 HashSet (org.hsqldb_voltpatches.lib.HashSet)1 OrderedHashSet (org.hsqldb_voltpatches.lib.OrderedHashSet)1 OrderedIntHashSet (org.hsqldb_voltpatches.lib.OrderedIntHashSet)1