001: /*
002:
003: Derby - Class org.apache.derby.impl.sql.execute.RowUtil
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to you under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derby.impl.sql.execute;
023:
024: import org.apache.derby.iapi.services.context.ContextManager;
025:
026: import org.apache.derby.iapi.services.sanity.SanityManager;
027:
028: import org.apache.derby.iapi.sql.conn.LanguageConnectionContext;
029:
030: import org.apache.derby.iapi.sql.execute.ExecRow;
031: import org.apache.derby.iapi.sql.execute.ExecIndexRow;
032: import org.apache.derby.iapi.sql.execute.ExecutionContext;
033: import org.apache.derby.iapi.error.StandardException;
034:
035: import org.apache.derby.iapi.services.io.FormatableBitSet;
036: import java.util.Vector;
037:
038: /**
039: Utility class manipulating rows.
040:
041: <P>Note: this class is public so it may be used by Replication execution
042: code. It is basically not public.
043: */
044: public class RowUtil {
045:
046: /**
047: Get an empty ExecRow.
048:
049: @param columnCount the number of columns in the row.
050: @return the row.
051: */
052: public static ExecRow getEmptyValueRow(int columnCount,
053: LanguageConnectionContext lcc) {
054: ExecutionContext ec;
055:
056: ec = lcc.getExecutionContext();
057: return ec.getExecutionFactory().getValueRow(columnCount);
058: }
059:
060: /**
061: Get an empty ExecIndexRow.
062:
063: @param columnCount the number of columns in the row.
064: @param cm Current ContextManager
065: @return the row.
066: */
067: public static ExecIndexRow getEmptyIndexRow(int columnCount,
068: ContextManager cm) {
069: ExecutionContext ec;
070:
071: ec = (ExecutionContext) cm
072: .getContext(ExecutionContext.CONTEXT_ID);
073: return ec.getExecutionFactory().getIndexableRow(columnCount);
074: }
075:
076: /**
077: Clone an ExecRow's columns and place the coloned columns in another
078: ExecRow.
079:
080: @param to Place the cloned columns here.
081: @param from Get the columns to clone here.
082: @param count Clone this number of columns.
083: */
084: public static void copyCloneColumns(ExecRow to, ExecRow from,
085: int count) {
086: for (int ix = 1; ix <= count; ix++) {
087: to.setColumn(ix, from.cloneColumn(ix));
088: }
089: }
090:
091: /**
092: Copy references for an ExecRow's columns to another ExecRow.
093:
094: @param to Place the column references here.
095: @param from Get the column references from here.
096: */
097: public static void copyRefColumns(ExecRow to, ExecRow from) {
098: Object[] src = from.getRowArray();
099: Object[] dst = to.getRowArray();
100: System.arraycopy(src, 0, dst, 0, src.length);
101: }
102:
103: /**
104: Copy references for an ExecRow's columns to another ExecRow.
105:
106: @param to Place the column references here.
107: @param from Get the column references from here.
108: @param count Copy this number of column references.
109: */
110: public static void copyRefColumns(ExecRow to, ExecRow from,
111: int count) throws StandardException {
112: copyRefColumns(to, 0, from, 0, count);
113: }
114:
115: /**
116: Copy references for an ExecRow's columns to another ExecRow.
117:
118: @param to Place the column references here.
119: @param from Get the column references from here.
120: @param start The 0 based index of the first column to copy.
121: @param count Copy this number of column references.
122: */
123: public static void copyRefColumns(ExecRow to, ExecRow from,
124: int start, int count) throws StandardException {
125: copyRefColumns(to, 0, from, start, count);
126: }
127:
128: /**
129: Copy references for an ExecRow's columns to another ExecRow.
130: @param to Place the column references here.
131: @param toStart The 0-based index of the first column to replace.
132: @param from Get the column references from here.
133: @param fromStart The 0 based index of the first column to copy.
134: @param count Copy this number of column references.
135: */
136: public static void copyRefColumns(ExecRow to, int toStart,
137: ExecRow from, int fromStart, int count)
138: throws StandardException {
139: for (int i = 1; i <= count; i++) {
140: // Uhhh, why doesn't this to an ArrayCopy????
141: to.setColumn(i + toStart, from.getColumn(i + fromStart));
142: }
143: }
144:
145: /**
146: Copy references for an ExecRow's columns to another ExecRow.
147:
148: @param to Place the column references here.
149: @param from Get the column references from here.
150: @param positions array of 1-based column ids to copy from "from" to "to"
151: */
152: public static void copyRefColumns(ExecRow to, ExecRow from,
153: int[] positions) throws StandardException {
154: if (positions == null) {
155: return;
156: }
157:
158: int count = positions.length;
159: for (int ix = 0; ix < count; ix++) {
160: to.setColumn(ix + 1, from.getColumn(positions[ix]));
161: }
162: }
163:
164: /**
165: Copy references for an ExecRow's columns to another ExecRow.
166: For copying from a compact array to a reconstituted array.
167: E.g. if positions = {2, 4}, and from = {666, 777} then
168: to => {null, 666, null, 777}. Will only go as far as to.getArray().length.
169:
170: @param to Place the column references here. Sparse array
171: @param from Get the column references from here. Compact array
172: @param positions array of 1-based column ids to copy from "from" to "to"
173: */
174: public static void copyRefColumns(ExecRow to, ExecRow from,
175: FormatableBitSet positions) throws StandardException {
176: if (positions == null) {
177: return;
178: }
179:
180: int max = to.getRowArray().length;
181: int toCount = 1;
182: int fromCount = 1;
183: for (; toCount <= max; toCount++) {
184: if (positions.get(toCount)) {
185: to.setColumn(toCount, from.getColumn(fromCount));
186: fromCount++;
187: }
188: }
189: }
190:
191: /**
192: Empty columns -- i.e. make them refer to a java null.
193:
194: <P>This is useful to remove dangling references to a column.
195:
196: @param setMe Set columns in this storable to be empty.
197: */
198: public static void copyRefColumns(ExecRow setMe)
199: throws StandardException {
200: for (int ix = 1; ix <= setMe.nColumns(); ix++) {
201: setMe.setColumn(ix, null);
202: }
203: }
204:
205: /**
206: * toString
207: *
208: * @param row the row
209: *
210: * @return the string
211: */
212: public static String toString(ExecRow row) {
213: if (SanityManager.DEBUG) {
214: return (row == null) ? "null" : toString(row.getRowArray());
215: } else {
216: return "";
217: }
218: }
219:
220: /**
221: * toString
222: *
223: * @param objs the row array
224: *
225: * @return the string
226: */
227: public static String toString(Object[] objs) {
228: if (SanityManager.DEBUG) {
229: StringBuffer strbuf = new StringBuffer();
230:
231: if (objs == null)
232: return "null";
233:
234: strbuf.append("(");
235: for (int i = 0; i < objs.length; i++) {
236: if (i > 0) {
237: strbuf.append(",");
238: }
239: strbuf.append(objs[i]);
240: }
241: strbuf.append(")");
242: return strbuf.toString();
243: } else {
244: return "";
245: }
246: }
247:
248: /**
249: * toString
250: *
251: * @param row the row
252: * @param startPoint 0 based start point in row array, inclusive
253: * @param endPoint 0 based end point in row array, inclusive
254: *
255: * @return the string
256: */
257: public static String toString(ExecRow row, int startPoint,
258: int endPoint) {
259: return toString(row.getRowArray(), startPoint, endPoint);
260: }
261:
262: /**
263: * toString
264: *
265: * @param objs the row array
266: * @param startPoint 0 based start point in row array, inclusive
267: * @param endPoint 0 based end point in row array, inclusive
268: *
269: * @return the string
270: */
271: public static String toString(Object[] objs, int startPoint,
272: int endPoint) {
273: StringBuffer strbuf = new StringBuffer();
274:
275: if (SanityManager.DEBUG) {
276: if (endPoint >= objs.length) {
277: SanityManager.THROWASSERT("endPoint " + endPoint
278: + " is too high," + " array only has "
279: + objs.length + " elements");
280: }
281: }
282: strbuf.append("(");
283: for (int i = startPoint; i <= endPoint; i++) {
284: if (i > 0) {
285: strbuf.append(",");
286: }
287: strbuf.append(objs[i]);
288: }
289: strbuf.append(")");
290: return strbuf.toString();
291: }
292:
293: /**
294: * toString
295: *
296: * @param row the row
297: * @param positions 1 based array of positions
298: *
299: * @return the string
300: */
301: public static String toString(ExecRow row, int[] positions) {
302: return toString(row.getRowArray(), positions);
303: }
304:
305: /**
306: * toString
307: *
308: * @param objs the row array
309: * @param positions 1 based array of positions
310: *
311: * @return the string
312: */
313: public static String toString(Object[] objs, int[] positions) {
314: if (positions == null) {
315: return (String) null;
316: }
317:
318: StringBuffer strbuf = new StringBuffer();
319:
320: strbuf.append("(");
321: for (int i = 0; i < positions.length; i++) {
322:
323: if (i > 0) {
324: strbuf.append(",");
325: }
326: strbuf.append(objs[positions[i] - 1]);
327: }
328: strbuf.append(")");
329: return strbuf.toString();
330: }
331:
332: /**
333: * intArrayToString
334: *
335: * @param colMap the int array
336: *
337: * @return the string
338: */
339: public static String intArrayToString(int[] colMap) {
340: StringBuffer strbuf = new StringBuffer();
341:
342: strbuf.append("(");
343: for (int i = 0; i < colMap.length; i++) {
344: if (i > 0) {
345: strbuf.append(",");
346: }
347: strbuf.append(colMap[i]);
348: }
349: strbuf.append(")");
350: return strbuf.toString();
351: }
352:
353: public static boolean inAscendingOrder(int[] colMap) {
354: if (colMap != null) {
355: int lastCol = -1;
356: for (int i = 0; i < colMap.length; i++) {
357: if (lastCol > colMap[i]) {
358: return false;
359: }
360: lastCol = colMap[i];
361: }
362: }
363: return true;
364: }
365:
366: /**
367: * Shift a FormatableBitSet N bits toward the zero end.
368: * e.g. shift({2,4}) -> {1,3}.
369: *
370: * @param bitSet the bit set
371: * @param n the number of bits to shift
372: *
373: * @return a new FormatableBitSet with the shifted result
374: */
375: public static FormatableBitSet shift(FormatableBitSet bitSet, int n) {
376: FormatableBitSet out = null;
377: if (bitSet != null) {
378: int size = bitSet.size();
379: out = new FormatableBitSet(size);
380: for (int i = n; i < size; i++) {
381: if (bitSet.get(i)) {
382: out.set(i - n);
383: }
384: }
385: }
386: return out;
387: }
388: }
|