KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > sql > execute > RowUtil


1 /*
2
3    Derby - Class org.apache.derby.impl.sql.execute.RowUtil
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to you under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derby.impl.sql.execute;
23  
24 import org.apache.derby.iapi.services.context.ContextManager;
25
26 import org.apache.derby.iapi.services.sanity.SanityManager;
27
28 import org.apache.derby.iapi.sql.conn.LanguageConnectionContext;
29
30 import org.apache.derby.iapi.sql.execute.ExecRow;
31 import org.apache.derby.iapi.sql.execute.ExecIndexRow;
32 import org.apache.derby.iapi.sql.execute.ExecutionContext;
33 import org.apache.derby.iapi.error.StandardException;
34
35 import org.apache.derby.iapi.services.io.FormatableBitSet;
36 import java.util.Vector JavaDoc;
37
38 /**
39   Utility class manipulating rows.
40
41   <P>Note: this class is public so it may be used by Replication execution
42   code. It is basically not public.
43   */

44 public class RowUtil
45 {
46
47     /**
48       Get an empty ExecRow.
49
50       @param columnCount the number of columns in the row.
51       @return the row.
52       */

53     public static ExecRow getEmptyValueRow(int columnCount, LanguageConnectionContext lcc)
54     {
55         ExecutionContext ec;
56
57         ec = lcc.getExecutionContext();
58         return ec.getExecutionFactory().getValueRow(columnCount);
59     }
60
61     /**
62       Get an empty ExecIndexRow.
63
64       @param columnCount the number of columns in the row.
65       @param cm Current ContextManager
66       @return the row.
67       */

68     public static ExecIndexRow getEmptyIndexRow(int columnCount, ContextManager cm)
69     {
70         ExecutionContext ec;
71
72         ec = (ExecutionContext)
73                 cm.getContext(ExecutionContext.CONTEXT_ID);
74         return ec.getExecutionFactory().getIndexableRow(columnCount);
75     }
76
77     /**
78       Clone an ExecRow's columns and place the coloned columns in another
79       ExecRow.
80
81       @param to Place the cloned columns here.
82       @param from Get the columns to clone here.
83       @param count Clone this number of columns.
84       */

85     public static void copyCloneColumns(ExecRow to, ExecRow from, int count)
86     {
87         for (int ix = 1; ix <= count; ix++)
88         {
89             to.setColumn(ix,from.cloneColumn(ix));
90         }
91     }
92
93     /**
94       Copy references for an ExecRow's columns to another ExecRow.
95
96       @param to Place the column references here.
97       @param from Get the column references from here.
98       */

99     public static void copyRefColumns(ExecRow to, ExecRow from)
100     {
101         Object JavaDoc[] src = from.getRowArray();
102         Object JavaDoc[] dst = to.getRowArray();
103         System.arraycopy(src, 0, dst, 0, src.length);
104     }
105
106     /**
107       Copy references for an ExecRow's columns to another ExecRow.
108
109       @param to Place the column references here.
110       @param from Get the column references from here.
111       @param count Copy this number of column references.
112       */

113     public static void copyRefColumns(ExecRow to, ExecRow from, int count)
114         throws StandardException
115     {
116         copyRefColumns(to, 0, from, 0, count);
117     }
118
119     /**
120       Copy references for an ExecRow's columns to another ExecRow.
121
122       @param to Place the column references here.
123       @param from Get the column references from here.
124       @param start The 0 based index of the first column to copy.
125       @param count Copy this number of column references.
126       */

127     public static void copyRefColumns(ExecRow to, ExecRow from,
128                                       int start, int count)
129                                       throws StandardException
130     {
131         copyRefColumns(to, 0, from, start, count);
132     }
133
134     /**
135       Copy references for an ExecRow's columns to another ExecRow.
136       @param to Place the column references here.
137       @param toStart The 0-based index of the first column to replace.
138       @param from Get the column references from here.
139       @param fromStart The 0 based index of the first column to copy.
140       @param count Copy this number of column references.
141       */

142     public static void copyRefColumns(ExecRow to, int toStart, ExecRow from,
143                                       int fromStart, int count) throws StandardException {
144         for (int i = 1; i <= count; i++)
145         {
146             // Uhhh, why doesn't this to an ArrayCopy????
147
to.setColumn(i+toStart, from.getColumn(i+fromStart));
148         }
149     }
150
151     /**
152       Copy references for an ExecRow's columns to another ExecRow.
153
154       @param to Place the column references here.
155       @param from Get the column references from here.
156       @param positions array of 1-based column ids to copy from "from" to "to"
157       */

158     public static void copyRefColumns(ExecRow to, ExecRow from, int[] positions)
159         throws StandardException
160     {
161         if ( positions == null ) { return; }
162
163         int count = positions.length;
164         for (int ix = 0; ix < count; ix++)
165         { to.setColumn( ix + 1, from.getColumn( positions[ix] ) ); }
166     }
167
168     /**
169       Copy references for an ExecRow's columns to another ExecRow.
170       For copying from a compact array to a reconstituted array.
171       E.g. if positions = {2, 4}, and from = {666, 777} then
172       to => {null, 666, null, 777}. Will only go as far as to.getArray().length.
173
174       @param to Place the column references here. Sparse array
175       @param from Get the column references from here. Compact array
176       @param positions array of 1-based column ids to copy from "from" to "to"
177       */

178     public static void copyRefColumns(ExecRow to, ExecRow from, FormatableBitSet positions)
179         throws StandardException
180     {
181         if (positions == null)
182         {
183             return;
184         }
185
186         int max = to.getRowArray().length;
187         int toCount = 1;
188         int fromCount = 1;
189         for (;toCount <= max; toCount++)
190         {
191             if (positions.get(toCount))
192             {
193                 to.setColumn(toCount, from.getColumn(fromCount));
194                 fromCount++;
195             }
196         }
197     }
198
199     /**
200       Empty columns -- i.e. make them refer to a java null.
201
202       <P>This is useful to remove dangling references to a column.
203
204       @param setMe Set columns in this storable to be empty.
205       */

206     public static void copyRefColumns(ExecRow setMe)
207         throws StandardException
208     {
209         for (int ix = 1; ix <= setMe.nColumns(); ix++)
210         {
211             setMe.setColumn(ix,null);
212         }
213     }
214
215     /**
216      * toString
217      *
218      * @param row the row
219      *
220      * @return the string
221      */

222     public static String JavaDoc toString(ExecRow row)
223     {
224         if (SanityManager.DEBUG)
225         {
226             return (row == null) ? "null" : toString(row.getRowArray());
227         }
228         else
229         {
230             return "";
231         }
232     }
233         
234     /**
235      * toString
236      *
237      * @param objs the row array
238      *
239      * @return the string
240      */

241     public static String JavaDoc toString(Object JavaDoc[] objs)
242     {
243         if (SanityManager.DEBUG)
244         {
245             StringBuffer JavaDoc strbuf = new StringBuffer JavaDoc();
246
247             if (objs == null)
248                 return "null";
249
250             strbuf.append("(");
251             for (int i = 0; i < objs.length; i++)
252             {
253                 if (i > 0)
254                 {
255                     strbuf.append(",");
256                 }
257                 strbuf.append(objs[i]);
258             }
259             strbuf.append(")");
260             return strbuf.toString();
261         }
262         else
263         {
264             return "";
265         }
266     }
267
268     /**
269      * toString
270      *
271      * @param row the row
272      * @param startPoint 0 based start point in row array, inclusive
273      * @param endPoint 0 based end point in row array, inclusive
274      *
275      * @return the string
276      */

277     public static String JavaDoc toString(ExecRow row, int startPoint, int endPoint)
278     {
279         return toString(row.getRowArray(), startPoint, endPoint);
280     }
281
282     /**
283      * toString
284      *
285      * @param objs the row array
286      * @param startPoint 0 based start point in row array, inclusive
287      * @param endPoint 0 based end point in row array, inclusive
288      *
289      * @return the string
290      */

291     public static String JavaDoc toString(Object JavaDoc[] objs, int startPoint, int endPoint)
292     {
293         StringBuffer JavaDoc strbuf = new StringBuffer JavaDoc();
294
295         if (SanityManager.DEBUG)
296         {
297             if (endPoint >= objs.length)
298             {
299                 SanityManager.THROWASSERT("endPoint "+endPoint+" is too high,"+
300                     " array only has "+objs.length+" elements");
301             }
302         }
303         strbuf.append("(");
304         for (int i = startPoint; i <= endPoint; i++)
305         {
306             if (i > 0)
307             {
308                 strbuf.append(",");
309             }
310             strbuf.append(objs[i]);
311         }
312         strbuf.append(")");
313         return strbuf.toString();
314     }
315
316
317     /**
318      * toString
319      *
320      * @param row the row
321      * @param positions 1 based array of positions
322      *
323      * @return the string
324      */

325     public static String JavaDoc toString(ExecRow row, int[] positions)
326     {
327         return toString(row.getRowArray(), positions);
328     }
329
330     /**
331      * toString
332      *
333      * @param objs the row array
334      * @param positions 1 based array of positions
335      *
336      * @return the string
337      */

338     public static String JavaDoc toString(Object JavaDoc[] objs, int[] positions)
339     {
340         if (positions == null)
341         {
342             return (String JavaDoc) null;
343         }
344
345         StringBuffer JavaDoc strbuf = new StringBuffer JavaDoc();
346
347         strbuf.append("(");
348         for (int i = 0; i < positions.length; i++)
349         {
350
351             if (i > 0)
352             {
353                 strbuf.append(",");
354             }
355             strbuf.append(objs[positions[i] - 1]);
356         }
357         strbuf.append(")");
358         return strbuf.toString();
359     }
360
361     /**
362      * intArrayToString
363      *
364      * @param colMap the int array
365      *
366      * @return the string
367      */

368     public static String JavaDoc intArrayToString(int[] colMap)
369     {
370         StringBuffer JavaDoc strbuf = new StringBuffer JavaDoc();
371
372         strbuf.append("(");
373         for (int i = 0; i < colMap.length; i++)
374         {
375             if (i > 0)
376             {
377                 strbuf.append(",");
378             }
379             strbuf.append(colMap[i]);
380         }
381         strbuf.append(")");
382         return strbuf.toString();
383     }
384
385     public static boolean inAscendingOrder(int[] colMap)
386     {
387         if (colMap != null)
388         {
389             int lastCol = -1;
390             for (int i = 0; i < colMap.length; i++)
391             {
392                 if (lastCol > colMap[i])
393                 {
394                     return false;
395                 }
396                 lastCol = colMap[i];
397             }
398         }
399         return true;
400     }
401     /**
402      * Shift a FormatableBitSet N bits toward the zero end.
403      * e.g. shift({2,4}) -> {1,3}.
404      *
405      * @param bitSet the bit set
406      * @param n the number of bits to shift
407      *
408      * @return a new FormatableBitSet with the shifted result
409      */

410     public static FormatableBitSet shift(FormatableBitSet bitSet, int n)
411     {
412         FormatableBitSet out = null;
413         if (bitSet != null)
414         {
415             int size = bitSet.size();
416             out = new FormatableBitSet(size);
417             for (int i = n; i < size; i++)
418             {
419                 if (bitSet.get(i))
420                 {
421                     out.set(i-n);
422                 }
423             }
424         }
425         return out;
426     }
427 }
428
Popular Tags