KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > smallsql > database > Utils


1 /* =============================================================
2  * SmallSQL : a free Java DBMS library for the Java(tm) platform
3  * =============================================================
4  *
5  * (C) Copyright 2004-2006, by Volker Berlin.
6  *
7  * Project Info: http://www.smallsql.de/
8  *
9  * This library is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17  * License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22  * USA.
23  *
24  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
25  * in the United States and other countries.]
26  *
27  * ---------------
28  * Utils.java
29  * ---------------
30  * Author: Volker Berlin
31  *
32  */

33 package smallsql.database;
34
35 import java.sql.*;
36
37
38 class Utils {
39
40     static final String JavaDoc MASTER_FILENAME = "smallsql.master";
41     static final String JavaDoc TABLE_VIEW_EXTENTION = ".sdb";
42     private static final String JavaDoc LOB_EXTENTION = ".lob";
43     static final String JavaDoc IDX_EXTENTION = ".idx";
44     private static final Integer JavaDoc[] integerCache = new Integer JavaDoc[260];
45     private static final Short JavaDoc[] shortCache = new Short JavaDoc[260];
46     static{
47         for(int i=-4; i<256; i++){
48             integerCache[ i+4 ] = new Integer JavaDoc(i);
49             shortCache [ i+4 ] = new Short JavaDoc((short)i);
50         }
51     }
52     
53     
54     static SQLException createUnsupportedException( String JavaDoc operation ){
55         return new SmallSQLException( "Unsupported Operation "+operation, "01000", 0 );
56     }
57     
58
59     static SQLException createSQLException( String JavaDoc msg ){
60         return new SmallSQLException( msg, "01000", 0 );
61     }
62     
63
64     static SQLException createSQLException( Throwable JavaDoc e ){
65         if(e instanceof SQLException) return (SQLException)e;
66         return new SmallSQLException( e );
67     }
68     
69
70     static String JavaDoc createTableViewFileName(Database database, String JavaDoc name){
71         return database.getName() + '/' + name + TABLE_VIEW_EXTENTION;
72     }
73
74     static String JavaDoc createLobFileName(Database database, String JavaDoc name){
75         return database.getName() + '/' + name + LOB_EXTENTION;
76     }
77
78     static String JavaDoc createIdxFileName(Database database, String JavaDoc name){
79         return database.getName() + '/' + name + IDX_EXTENTION;
80     }
81
82     static boolean like(String JavaDoc value, String JavaDoc pattern){
83         if(value == null || pattern == null) return false;
84         if(pattern.length() == 0) return true;
85
86         int mIdx = 0;//index in mask Array
87
int sIdx = 0;//index in search Array
88
boolean range = false;
89         weiter:
90         while(pattern.length() > mIdx && value.length() > sIdx) {
91             char m = Character.toUpperCase(pattern.charAt(mIdx++));
92             switch(m) {
93                 case '%':
94                     range = true;
95                     break;
96                 case '_':
97                     sIdx++;
98                     break;
99                 default:
100                     if(range) {//* wildcard ist aktiv
101
for(; sIdx < value.length(); sIdx++) {
102                             if(Character.toUpperCase(value.charAt(sIdx)) == m) break;//Zähler darf nicht vor dem break erhöht werden
103
}
104                         if(sIdx >= value.length()) return false;
105                         int lastmIdx = mIdx - 1;
106                         sIdx++;
107                         while(pattern.length() > mIdx && value.length() > sIdx) {
108                             m = Character.toUpperCase(pattern.charAt(mIdx++));
109                             if(Character.toUpperCase(value.charAt(sIdx)) != m) {
110                                 if(m == '%' || m == '_') {
111                                     mIdx--;
112                                     break;
113                                 }
114                                 mIdx = lastmIdx;
115                                 continue weiter;
116                             }
117                             sIdx++;
118                         }
119                         range = false;
120                     }else{
121                         if(Character.toUpperCase(value.charAt(sIdx)) != m) return false;
122                         sIdx++;
123                     }
124                     break;
125             }
126         }
127         while(pattern.length() > mIdx) {
128             //Suchmaske ist noch nicht zu ende es dürfen nur noch '*' enthalten sein
129
if(Character.toUpperCase(pattern.charAt(mIdx++)) != '*') return false;
130         }
131         while(value.length() > sIdx && !range) return false;
132         return true;
133     }
134     
135     
136     static int long2int(long value){
137         if(value > Integer.MAX_VALUE)
138             return Integer.MAX_VALUE;
139         if(value < Integer.MIN_VALUE)
140             return Integer.MIN_VALUE;
141         return (int)value;
142     }
143     
144     static long double2long(double value){
145         if(value > Long.MAX_VALUE)
146             return Long.MAX_VALUE;
147         if(value < Long.MIN_VALUE)
148             return Long.MIN_VALUE;
149         return (long)value;
150     }
151
152
153
154     static float bytes2float( byte[] bytes ){
155         return Float.intBitsToFloat( bytes2int( bytes ) );
156     }
157
158     static double bytes2double( byte[] bytes ){
159         return Double.longBitsToDouble( bytes2long( bytes ) );
160     }
161
162     static long bytes2long( byte[] bytes ){
163         long result = 0;
164         int length = Math.min( 8, bytes.length);
165         for(int i=0; i<length; i++){
166             result = (result << 8) | (bytes[i] & 0xFF);
167         }
168         return result;
169     }
170
171     static int bytes2int( byte[] bytes ){
172         int result = 0;
173         int length = Math.min( 4, bytes.length);
174         for(int i=0; i<length; i++){
175             result = (result << 8) | (bytes[i] & 0xFF);
176         }
177         return result;
178     }
179
180     static byte[] int2bytes( int value ){
181         byte[] result = new byte[4];
182         result[0] = (byte)(value >> 24);
183         result[1] = (byte)(value >> 16);
184         result[2] = (byte)(value >> 8);
185         result[3] = (byte)(value);
186         return result;
187     }
188
189     static String JavaDoc bytes2hex( byte[] bytes ){
190         StringBuffer JavaDoc buf = new StringBuffer JavaDoc(bytes.length << 1);
191         for(int i=0; i<bytes.length; i++){
192             buf.append( digits[ (bytes[i] >> 4) & 0x0F ] );
193             buf.append( digits[ (bytes[i] ) & 0x0F ] );
194         }
195         return buf.toString();
196     }
197
198     static byte[] hex2bytes( char[] hex, int offset, int length) throws SQLException{
199         try{
200             byte[] bytes = new byte[length / 2];
201             for(int i=0; i<bytes.length; i++){
202                 bytes[i] = (byte)((hexDigit2int( hex[ offset++ ] ) << 4)
203                                 | hexDigit2int( hex[ offset++ ] ));
204             }
205             return bytes;
206         }catch(Exception JavaDoc e){
207              throw Utils.createSQLException("Invalid hex sequense at " + offset/*, offset*/);
208         }
209     }
210
211     private static int hexDigit2int(char digit){
212         if(digit >= '0' && digit <= '9') return digit - '0';
213         digit |= 0x20;
214         if(digit >= 'a' && digit <= 'f') return digit - 'W'; // -'W' == -'a' + 10
215
throw new RuntimeException JavaDoc();
216     }
217
218     static byte[] unique2bytes( String JavaDoc unique ) throws SQLException{
219         char[] chars = unique.toCharArray();
220         byte[] daten = new byte[16];
221         daten[3] = hex2byte( chars, 0 );
222         daten[2] = hex2byte( chars, 2 );
223         daten[1] = hex2byte( chars, 4 );
224         daten[0] = hex2byte( chars, 6 );
225
226         daten[5] = hex2byte( chars, 9 );
227         daten[4] = hex2byte( chars, 11 );
228
229         daten[7] = hex2byte( chars, 14 );
230         daten[6] = hex2byte( chars, 16 );
231
232         daten[8] = hex2byte( chars, 19 );
233         daten[9] = hex2byte( chars, 21 );
234
235         daten[10] = hex2byte( chars, 24 );
236         daten[11] = hex2byte( chars, 26 );
237         daten[12] = hex2byte( chars, 28 );
238         daten[13] = hex2byte( chars, 30 );
239         daten[14] = hex2byte( chars, 32 );
240         daten[15] = hex2byte( chars, 34 );
241         return daten;
242     }
243
244     private static byte hex2byte( char[] hex, int offset) throws SQLException{
245         try{
246                 return (byte)((hexDigit2int( hex[ offset++ ] ) << 4)
247                                 | hexDigit2int( hex[ offset++ ] ));
248         }catch(Exception JavaDoc e){
249              throw Utils.createSQLException("Invalid hex sequense at position " + offset + " in '" + new String JavaDoc(hex) + '\'');
250         }
251     }
252
253     static String JavaDoc bytes2unique( byte[] daten, int offset ){
254         if(daten.length-offset < 16){
255             byte[] temp = new byte[16];
256             System.arraycopy(daten, offset, temp, 0, daten.length-offset);
257             daten = temp;
258         }
259         char[] chars = new char[36];
260         chars[8] = chars[13] = chars[18] = chars[23] = '-';
261
262         chars[0] = digits[ (daten[offset+3] >> 4) & 0x0F ];
263         chars[1] = digits[ (daten[offset+3] ) & 0x0F ];
264         chars[2] = digits[ (daten[offset+2] >> 4) & 0x0F ];
265         chars[3] = digits[ (daten[offset+2] ) & 0x0F ];
266         chars[4] = digits[ (daten[offset+1] >> 4) & 0x0F ];
267         chars[5] = digits[ (daten[offset+1] ) & 0x0F ];
268         chars[6] = digits[ (daten[offset+0] >> 4) & 0x0F ];
269         chars[7] = digits[ (daten[offset+0] ) & 0x0F ];
270
271         chars[ 9] = digits[ (daten[offset+5] >> 4) & 0x0F ];
272         chars[10] = digits[ (daten[offset+5] ) & 0x0F ];
273         chars[11] = digits[ (daten[offset+4] >> 4) & 0x0F ];
274         chars[12] = digits[ (daten[offset+4] ) & 0x0F ];
275
276         chars[14] = digits[ (daten[offset+7] >> 4) & 0x0F ];
277         chars[15] = digits[ (daten[offset+7] ) & 0x0F ];
278         chars[16] = digits[ (daten[offset+6] >> 4) & 0x0F ];
279         chars[17] = digits[ (daten[offset+6] ) & 0x0F ];
280
281         chars[19] = digits[ (daten[offset+8] >> 4) & 0x0F ];
282         chars[20] = digits[ (daten[offset+8] ) & 0x0F ];
283         chars[21] = digits[ (daten[offset+9] >> 4) & 0x0F ];
284         chars[22] = digits[ (daten[offset+9] ) & 0x0F ];
285
286         chars[24] = digits[ (daten[offset+10] >> 4) & 0x0F ];
287         chars[25] = digits[ (daten[offset+10] ) & 0x0F ];
288         chars[26] = digits[ (daten[offset+11] >> 4) & 0x0F ];
289         chars[27] = digits[ (daten[offset+11] ) & 0x0F ];
290         chars[28] = digits[ (daten[offset+12] >> 4) & 0x0F ];
291         chars[29] = digits[ (daten[offset+12] ) & 0x0F ];
292         chars[30] = digits[ (daten[offset+13] >> 4) & 0x0F ];
293         chars[31] = digits[ (daten[offset+13] ) & 0x0F ];
294         chars[32] = digits[ (daten[offset+14] >> 4) & 0x0F ];
295         chars[33] = digits[ (daten[offset+14] ) & 0x0F ];
296         chars[34] = digits[ (daten[offset+15] >> 4) & 0x0F ];
297         chars[35] = digits[ (daten[offset+15] ) & 0x0F ];
298         return new String JavaDoc(chars);
299     }
300
301     static boolean string2boolean( String JavaDoc val){
302         try{
303             return Double.parseDouble( val ) != 0;
304         }catch(NumberFormatException JavaDoc e){/*ignore it if it not a number*/}
305         return "true".equalsIgnoreCase( val ) || "yes".equalsIgnoreCase( val ) || "t".equalsIgnoreCase( val );
306     }
307     
308     
309     static long doubleToMoney(double value){
310         if(value < 0)
311             return (long)(value * 10000 - 0.5);
312         return (long)(value * 10000 + 0.5);
313     }
314
315     static int indexOf( char value, char[] str, int offset, int length ){
316         value |= 0x20;
317         for(int end = offset+length;offset < end; offset++){
318             if((str[offset] | 0x20) == value) return offset;
319         }
320         return -1;
321     }
322
323     static int indexOf( int value, int[] list ){
324         int offset = 0;
325         for(int end = list.length; offset < end; offset++){
326             if((list[offset]) == value) return offset;
327         }
328         return -1;
329     }
330
331     static int indexOf( byte[] value, byte[] list, int offset ){
332         int length = value.length;
333         loop1:
334         for(int end = list.length-length; offset <= end; offset++){
335             for(int i=0; i<length; i++ ){
336                 if(list[offset+i] != value[i]){
337                     continue loop1;
338                 }
339             }
340             return offset;
341         }
342         return -1;
343     }
344
345     static int compareBytes( byte[] leftBytes, byte[] rightBytes){
346         int length = Math.min( leftBytes.length, rightBytes.length );
347         int comp = 0;
348         for(int i=0; i<length; i++){
349             if(leftBytes[i] != rightBytes[i]){
350                 comp = leftBytes[i] < rightBytes[i] ? -1 : 1;
351                 break;
352             }
353         }
354         if(comp == 0 && leftBytes.length != rightBytes.length){
355             comp = leftBytes.length < rightBytes.length ? -1 : 1;
356         }
357         return comp;
358     }
359     
360     
361     /**
362      *
363      * @param colNames
364      * @param data
365      * @return
366      * @throws SQLException
367      */

368     static CommandSelect createMemoryCommandSelect( SSConnection con, String JavaDoc[] colNames, Object JavaDoc[][] data) throws SQLException{
369         MemoryResult source = new MemoryResult(data, colNames.length);
370         CommandSelect cmd = new CommandSelect(con.log);
371         for(int i=0; i<colNames.length; i++){
372             ExpressionName expr = new ExpressionName(colNames[i]);
373             cmd.addColumnExpression( expr );
374             expr.setFrom( source, i, source.getColumn(i));
375         }
376         cmd.setSource(source);
377         return cmd;
378     }
379     
380
381     // receycle Integer objects, this is faster as to garbage the objects
382
static final Integer JavaDoc getInteger(int value){
383         if(value >= -4 && value < 256){
384             return integerCache[ value+4 ];
385         }else
386             return new Integer JavaDoc(value);
387     }
388     
389     // receycle Integer objects, this is faster as to garbage the objects
390
static final Short JavaDoc getShort(int value){
391         if(value >= -4 && value < 256){
392             return shortCache[ value+4 ];
393         }else
394             return new Short JavaDoc((short)value);
395     }
396     
397     final static char[] digits = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
398 }
Popular Tags