KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > broker > util > SqlHelper


1 package org.apache.ojb.broker.util;
2
3 /* Copyright 2002-2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 import java.sql.ResultSet JavaDoc;
19 import java.sql.SQLException JavaDoc;
20
21 import org.apache.commons.lang.StringUtils;
22
23 /**
24  * Helper class for all SQL related stuff.
25  *
26  * @author <a HREF="mailto:armin@codeAuLait.de">Armin Waibel </a>
27  * @version $Id: SqlHelper.java,v 1.24.2.4 2005/12/21 22:27:47 tomdz Exp $
28  */

29 public class SqlHelper
30 {
31     /** define the name of the pseudo column holding the class to be instantiated. */
32     public static final String JavaDoc OJB_CLASS_COLUMN = "OJB_CLAZZ";
33
34     /**
35      * Helper Class for a split column <br>
36      * ie: sum (distinct amount) as theAmount
37      *
38      * <pre>
39      * prefix = 'sum (distinct '
40      * column = 'amount'
41      * suffix = ') as theAmount'
42      * </pre>
43      */

44     public static final class PathInfo
45     {
46         public String JavaDoc column;
47         public String JavaDoc prefix;
48         public String JavaDoc suffix;
49         public final String JavaDoc path; //original Path
50

51         PathInfo(String JavaDoc aPath, String JavaDoc aPrefix, String JavaDoc aColumn, String JavaDoc aSuffix)
52         {
53             path = aPath;
54             column = aColumn;
55             prefix = aPrefix;
56             suffix = aSuffix;
57         }
58     }
59
60     /**
61      * remove functions and () from path <br>
62      * ie: avg(amount) -> amount <br>
63      * ie: sum (accounts.amount) -> accounts.amount <br>
64      * ie: count(distinct id) as theCount-> id <br>
65      *
66      * @param aPath
67      * the path to the attribute
68      */

69     public static String JavaDoc cleanPath(String JavaDoc aPath)
70     {
71         return splitPath(aPath).column;
72     }
73
74     /**
75      * Split a path into column , prefix and suffix, the prefix contains all
76      * info up to the column <br>
77      * ie: avg(amount) -> amount , avg( , )<br>
78      * ie: sum (accounts.amount) as theSum -> accounts.amount , sum( , ) as
79      * theSum <br>
80      * ie: count( distinct id ) as bla -> id , count(distinct , ) as bla <br>
81      * Supports simple expressions ie: price * 1.05
82      *
83      * TODO: cannot resolve multiple attributes in expression
84      * ie: price - bonus
85      *
86      * @param aPath
87      * @return PathInfo
88      */

89     public static PathInfo splitPath(String JavaDoc aPath)
90     {
91         String JavaDoc prefix = null;
92         String JavaDoc suffix = null;
93         String JavaDoc colName = aPath;
94  
95         if (aPath == null)
96         {
97             return new PathInfo(null, null, null, null);
98         }
99
100         // ignore leading ( and trailing ) ie: sum(avg(col1))
101
int braceBegin = aPath.lastIndexOf("(");
102         int braceEnd = aPath.indexOf(")");
103         int opPos = StringUtils.indexOfAny(aPath, "+-/*");
104
105         if (braceBegin >= 0 && braceEnd >= 0 && braceEnd > braceBegin)
106         {
107             int colBegin;
108             int colEnd;
109             String JavaDoc betweenBraces;
110
111             betweenBraces = aPath.substring(braceBegin + 1, braceEnd).trim();
112             // look for ie 'distinct name'
113
colBegin = betweenBraces.indexOf(" ");
114             // look for multiarg function like to_char(col,'format_mask')
115
colEnd = betweenBraces.indexOf(",");
116             colEnd = colEnd > 0 ? colEnd : betweenBraces.length();
117             prefix = aPath.substring(0, braceBegin + 1) + betweenBraces.substring(0, colBegin + 1);
118             colName = betweenBraces.substring(colBegin + 1, colEnd);
119             suffix = betweenBraces.substring(colEnd) + aPath.substring(braceEnd);
120         }
121         else if (opPos >= 0)
122         {
123             colName = aPath.substring(0, opPos).trim();
124             suffix = aPath.substring(opPos);
125         }
126         
127         return new PathInfo(aPath, prefix, colName.trim(), suffix);
128     }
129     
130     /**
131      * Returns the name of the class to be instantiated.
132      * @param rs the Resultset
133      * @return null if the column is not available
134      */

135     public static String JavaDoc getOjbClassName(ResultSet JavaDoc rs)
136     {
137         try
138         {
139             return rs.getString(OJB_CLASS_COLUMN);
140         }
141         catch (SQLException JavaDoc e)
142         {
143             return null;
144         }
145     }
146
147 }
148
Popular Tags