KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb > plugins > cmp > jdbc > metadata > JDBCFunctionMappingMetaData


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.ejb.plugins.cmp.jdbc.metadata;
23
24 import java.io.IOException JavaDoc;
25 import java.io.StringReader JavaDoc;
26 import java.util.ArrayList JavaDoc;
27
28 import org.jboss.deployment.DeploymentException;
29 import org.jboss.metadata.MetaData;
30 import org.w3c.dom.Element JavaDoc;
31
32 public final class JDBCFunctionMappingMetaData
33 {
34    private final String JavaDoc functionName;
35    private String JavaDoc[] sqlChunks;
36    private int[] parameters;
37
38    public JDBCFunctionMappingMetaData(String JavaDoc functionName, String JavaDoc[] sqlChunks, int[] parameters)
39    {
40       this.functionName = functionName;
41       this.sqlChunks = sqlChunks;
42       this.parameters = parameters;
43    }
44
45    public JDBCFunctionMappingMetaData(Element JavaDoc element) throws DeploymentException
46    {
47       functionName = MetaData.getUniqueChildContent(element, "function-name");
48
49       String JavaDoc sql = MetaData.getUniqueChildContent(element, "function-sql");
50       initFromString(sql);
51    }
52
53    public JDBCFunctionMappingMetaData(String JavaDoc functionName, String JavaDoc sql) throws DeploymentException
54    {
55       this.functionName = functionName;
56       initFromString(sql);
57    }
58
59    private void initFromString(String JavaDoc sql) throws DeploymentException
60    {
61       ArrayList JavaDoc chunkList = new ArrayList JavaDoc();
62       ArrayList JavaDoc parameterList = new ArrayList JavaDoc();
63
64       // add a dummy chunk so we can be assured that the sql started with chunk before a number
65
if(sql.charAt(0) == '?')
66       {
67          chunkList.add("");
68       }
69       // break the sql into chunks and parameters
70
StringBuffer JavaDoc chunk = new StringBuffer JavaDoc();
71       StringReader JavaDoc reader = new StringReader JavaDoc(sql);
72       try
73       {
74          for(int c = reader.read(); c >= 0; c = reader.read())
75          {
76             if(c != '?')
77             {
78                chunk.append((char)c);
79             }
80             else
81             {
82                chunkList.add(chunk.toString());
83                chunk = new StringBuffer JavaDoc();
84
85                // read the number
86
StringBuffer JavaDoc number = new StringBuffer JavaDoc();
87                for(int digit = reader.read(); digit >= 0; digit = reader.read())
88                {
89                   if(Character.isDigit((char)digit))
90                   {
91                      number.append((char)digit);
92                   }
93                   else
94                   {
95                      if(digit >= 0)
96                      {
97                         chunk.append((char)digit);
98                      }
99                      break;
100                   }
101                }
102                if(number.length() == 0)
103                {
104                   throw new DeploymentException("Invalid parameter in function-sql: " + sql);
105                }
106                Integer JavaDoc parameter;
107                try
108                {
109                   parameter = new Integer JavaDoc(number.toString());
110                }
111                catch(NumberFormatException JavaDoc e)
112                {
113                   throw new DeploymentException("Invalid parameter number in function-sql: number=" + number + " sql=" + sql);
114                }
115                parameterList.add(parameter);
116             }
117          }
118       }
119       catch(IOException JavaDoc e)
120       {
121          // will never happen because io is in memory, but required by the interface
122
throw new DeploymentException("Error parsing function-sql: " + sql);
123       }
124       chunkList.add(chunk.toString());
125
126       // save out the chunks
127
sqlChunks = new String JavaDoc[chunkList.size()];
128       chunkList.toArray(sqlChunks);
129
130       // save out the parameter order
131
parameters = new int[parameterList.size()];
132       for(int i = 0; i < parameters.length; i++)
133       {
134          parameters[i] = ((Integer JavaDoc)parameterList.get(i)).intValue() - 1;
135       }
136    }
137
138    public String JavaDoc getFunctionName()
139    {
140       return functionName;
141    }
142
143    public StringBuffer JavaDoc getFunctionSql(Object JavaDoc[] args, StringBuffer JavaDoc buf)
144    {
145       for(int i = 0; i < sqlChunks.length; i++)
146       {
147          if(i < parameters.length)
148          {
149             // the logic is that if there is a parameter
150
// than append its chunck unless the parameter is null
151
// FIXME: I am not sure it's ok for any kind of template.
152
Object JavaDoc arg = args[parameters[i]];
153             if(arg != null)
154             {
155                buf.append(sqlChunks[i]);
156                buf.append(arg);
157             }
158          }
159          else
160          {
161             // this is tail
162
buf.append(sqlChunks[i]);
163          }
164       }
165       return buf;
166    }
167 }
168
Popular Tags