1 22 package org.jboss.ejb.plugins.cmp.jdbc.metadata; 23 24 import java.io.IOException ; 25 import java.io.StringReader ; 26 import java.util.ArrayList ; 27 28 import org.jboss.deployment.DeploymentException; 29 import org.jboss.metadata.MetaData; 30 import org.w3c.dom.Element ; 31 32 public final class JDBCFunctionMappingMetaData 33 { 34 private final String functionName; 35 private String [] sqlChunks; 36 private int[] parameters; 37 38 public JDBCFunctionMappingMetaData(String functionName, String [] sqlChunks, int[] parameters) 39 { 40 this.functionName = functionName; 41 this.sqlChunks = sqlChunks; 42 this.parameters = parameters; 43 } 44 45 public JDBCFunctionMappingMetaData(Element element) throws DeploymentException 46 { 47 functionName = MetaData.getUniqueChildContent(element, "function-name"); 48 49 String sql = MetaData.getUniqueChildContent(element, "function-sql"); 50 initFromString(sql); 51 } 52 53 public JDBCFunctionMappingMetaData(String functionName, String sql) throws DeploymentException 54 { 55 this.functionName = functionName; 56 initFromString(sql); 57 } 58 59 private void initFromString(String sql) throws DeploymentException 60 { 61 ArrayList chunkList = new ArrayList (); 62 ArrayList parameterList = new ArrayList (); 63 64 if(sql.charAt(0) == '?') 66 { 67 chunkList.add(""); 68 } 69 StringBuffer chunk = new StringBuffer (); 71 StringReader reader = new StringReader (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 (); 84 85 StringBuffer number = new StringBuffer (); 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 parameter; 107 try 108 { 109 parameter = new Integer (number.toString()); 110 } 111 catch(NumberFormatException 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 e) 120 { 121 throw new DeploymentException("Error parsing function-sql: " + sql); 123 } 124 chunkList.add(chunk.toString()); 125 126 sqlChunks = new String [chunkList.size()]; 128 chunkList.toArray(sqlChunks); 129 130 parameters = new int[parameterList.size()]; 132 for(int i = 0; i < parameters.length; i++) 133 { 134 parameters[i] = ((Integer )parameterList.get(i)).intValue() - 1; 135 } 136 } 137 138 public String getFunctionName() 139 { 140 return functionName; 141 } 142 143 public StringBuffer getFunctionSql(Object [] args, StringBuffer buf) 144 { 145 for(int i = 0; i < sqlChunks.length; i++) 146 { 147 if(i < parameters.length) 148 { 149 Object arg = args[parameters[i]]; 153 if(arg != null) 154 { 155 buf.append(sqlChunks[i]); 156 buf.append(arg); 157 } 158 } 159 else 160 { 161 buf.append(sqlChunks[i]); 163 } 164 } 165 return buf; 166 } 167 } 168 | Popular Tags |