Search in sources :

Example 1 with IndexColumnOrder

use of org.apache.derby.impl.sql.execute.IndexColumnOrder in project derby by apache.

the class OrderedColumnList method getColumnOrdering.

/**
 * Get an array of ColumnOrderings to pass to the store
 */
IndexColumnOrder[] getColumnOrdering() {
    IndexColumnOrder[] ordering;
    int numCols = size();
    int actualCols;
    ordering = new IndexColumnOrder[numCols];
    /*
			order by is fun, in that we need to ensure
			there are no duplicates in the list.  later copies
			of an earlier entry are considered purely redundant,
			they won't affect the result, so we can drop them.
			We don't know how many columns are in the source,
			so we use a hashtable for lookup of the positions
		*/
    HashSet<Integer> hashColumns = new HashSet<Integer>();
    actualCols = 0;
    for (int i = 0; i < numCols; i++) {
        OrderedColumn oc = elementAt(i);
        // order by (lang) positions are 1-based,
        // order items (store) are 0-based.
        int position = oc.getColumnPosition() - 1;
        if (hashColumns.add(position)) {
            ordering[i] = new IndexColumnOrder(position, oc.isAscending(), oc.isNullsOrderedLow());
            actualCols++;
        }
    }
    /*
			If there were duplicates removed, we need
			to shrink the array down to what we used.
		*/
    if (actualCols < numCols) {
        IndexColumnOrder[] newOrdering = new IndexColumnOrder[actualCols];
        System.arraycopy(ordering, 0, newOrdering, 0, actualCols);
        ordering = newOrdering;
    }
    return ordering;
}
Also used : IndexColumnOrder(org.apache.derby.impl.sql.execute.IndexColumnOrder) HashSet(java.util.HashSet)

Example 2 with IndexColumnOrder

use of org.apache.derby.impl.sql.execute.IndexColumnOrder in project derby by apache.

the class ExpressionClassBuilder method getColumnOrdering.

// /////////////////////////////////////////////////////////////////////
// 
// COLUMN ORDERING
// 
// /////////////////////////////////////////////////////////////////////
/**
 *		These utility methods buffers compilation from the IndexColumnOrder
 *		class.
 *
 *		They create an ordering based on their parameter, stuff that into
 *		the prepared statement, and then return the entry # for
 *		use in the generated code.
 *
 *		We could write another utility method to generate code to
 *		turn an entry # back into an object, but so far no-one needs it.
 *
 *		WARNING: this is a crafty method that ASSUMES that
 *		you want every column in the list ordered, and that every
 *		column in the list is the entire actual result colunm.
 *		It is only useful for DISTINCT in select.
 */
FormatableArrayHolder getColumnOrdering(ResultColumnList rclist) {
    IndexColumnOrder[] ordering;
    int numCols = (rclist == null) ? 0 : rclist.size();
    // skip the columns which are not exclusively part of the insert list
    // ie columns with default and autoincrement. These columns will not
    // be part of ordering.
    int numRealCols = 0;
    for (int i = 0; i < numCols; i++) {
        if (!(rclist.getResultColumn(i + 1).isGeneratedForUnmatchedColumnInInsert()))
            numRealCols++;
    }
    ordering = new IndexColumnOrder[numRealCols];
    for (int i = 0, j = 0; i < numCols; i++) {
        if (!(rclist.getResultColumn(i + 1).isGeneratedForUnmatchedColumnInInsert())) {
            ordering[j] = new IndexColumnOrder(i);
            j++;
        }
    }
    return new FormatableArrayHolder(ordering);
}
Also used : FormatableArrayHolder(org.apache.derby.iapi.services.io.FormatableArrayHolder) IndexColumnOrder(org.apache.derby.impl.sql.execute.IndexColumnOrder)

Example 3 with IndexColumnOrder

use of org.apache.derby.impl.sql.execute.IndexColumnOrder in project derby by apache.

the class ExpressionClassBuilder method addColumnToOrdering.

/**
 * Add a column to the existing Ordering list.  Takes
 * a column id and only adds it if it isn't in the list.
 *
 * @return the ColumnOrdering array
 */
FormatableArrayHolder addColumnToOrdering(FormatableArrayHolder orderingHolder, int columnNum) {
    /*
		** We don't expect a lot of order by columns, so
		** linear search.
		*/
    ColumnOrdering[] ordering = orderingHolder.getArray(ColumnOrdering[].class);
    int length = ordering.length;
    for (int i = 0; i < length; i++) {
        if (ordering[i].getColumnId() == columnNum)
            return orderingHolder;
    }
    /*
		** Didn't find it.  Allocate a bigger array
		** and add it to the end
		*/
    IndexColumnOrder[] newOrdering = new IndexColumnOrder[length + 1];
    System.arraycopy(ordering, 0, newOrdering, 0, length);
    newOrdering[length] = new IndexColumnOrder(columnNum);
    return new FormatableArrayHolder(newOrdering);
}
Also used : FormatableArrayHolder(org.apache.derby.iapi.services.io.FormatableArrayHolder) ColumnOrdering(org.apache.derby.iapi.store.access.ColumnOrdering) IndexColumnOrder(org.apache.derby.impl.sql.execute.IndexColumnOrder)

Aggregations

IndexColumnOrder (org.apache.derby.impl.sql.execute.IndexColumnOrder)3 FormatableArrayHolder (org.apache.derby.iapi.services.io.FormatableArrayHolder)2 HashSet (java.util.HashSet)1 ColumnOrdering (org.apache.derby.iapi.store.access.ColumnOrdering)1