1 56 57 package org.objectstyle.cayenne.access.jdbc; 58 59 import java.io.IOException ; 60 import java.io.Writer ; 61 import java.math.BigDecimal ; 62 import java.math.BigInteger ; 63 import java.sql.Date ; 64 import java.sql.Time ; 65 import java.sql.Timestamp ; 66 import java.util.Collection ; 67 import java.util.HashMap ; 68 import java.util.Map ; 69 70 import org.apache.velocity.context.InternalContextAdapter; 71 import org.apache.velocity.exception.MethodInvocationException; 72 import org.apache.velocity.exception.ParseErrorException; 73 import org.apache.velocity.exception.ResourceNotFoundException; 74 import org.apache.velocity.runtime.directive.Directive; 75 import org.apache.velocity.runtime.parser.node.Node; 76 import org.objectstyle.cayenne.util.Util; 77 78 102 public class ResultDirective extends Directive { 103 104 private static final Map typesGuess = new HashMap (); 105 106 static { 107 109 typesGuess.put("long", Long .class.getName()); 111 typesGuess.put("double", Double .class.getName()); 112 typesGuess.put("byte", Byte .class.getName()); 113 typesGuess.put("boolean", Boolean .class.getName()); 114 typesGuess.put("float", Float .class.getName()); 115 typesGuess.put("short", Short .class.getName()); 116 typesGuess.put("int", Integer .class.getName()); 117 118 typesGuess.put("Long", Long .class.getName()); 120 typesGuess.put("Double", Double .class.getName()); 121 typesGuess.put("Byte", Byte .class.getName()); 122 typesGuess.put("Boolean", Boolean .class.getName()); 123 typesGuess.put("Float", Float .class.getName()); 124 typesGuess.put("Short", Short .class.getName()); 125 typesGuess.put("Integer", Integer .class.getName()); 126 127 typesGuess.put("String", String .class.getName()); 129 typesGuess.put("Date", Date .class.getName()); 130 typesGuess.put("Time", Time .class.getName()); 131 typesGuess.put("Timestamp", Timestamp .class.getName()); 132 typesGuess.put("BigDecimal", BigDecimal .class.getName()); 133 typesGuess.put("BigInteger", BigInteger .class.getName()); 134 } 135 136 public String getName() { 137 return "result"; 138 } 139 140 public int getType() { 141 return LINE; 142 } 143 144 public boolean render(InternalContextAdapter context, Writer writer, Node node) 145 throws 146 IOException , 147 ResourceNotFoundException, 148 ParseErrorException, 149 MethodInvocationException { 150 151 String column = getChildAsString(context, node, 0); 152 if (column == null) { 153 throw new ParseErrorException("Column name expected at line " 154 + node.getLine() 155 + ", column " 156 + node.getColumn()); 157 } 158 159 String alias = getChildAsString(context, node, 2); 160 String dataRowKey = getChildAsString(context, node, 3); 161 162 String label = (!Util.isEmptyString(dataRowKey)) ? dataRowKey : (!Util 164 .isEmptyString(alias)) ? alias : null; 165 166 167 ColumnDescriptor columnDescriptor = new ColumnDescriptor(); 168 columnDescriptor.setName(column); 169 columnDescriptor.setLabel(label); 170 171 String type = getChildAsString(context, node, 1); 172 if (type != null) { 173 columnDescriptor.setJavaClass(guessType(type.toString())); 174 } 175 176 writer.write(column); 177 178 if (!Util.isEmptyString(alias) && !alias.equals(column)) { 180 writer.write(" AS "); 181 writer.write(alias); 182 } 183 184 bindResult(context, columnDescriptor); 185 return true; 186 } 187 188 protected Object getChild(InternalContextAdapter context, Node node, int i) 189 throws MethodInvocationException { 190 return (i >= 0 && i < node.jjtGetNumChildren()) 191 ? node.jjtGetChild(i).value(context) 192 : null; 193 } 194 195 200 protected String getChildAsString(InternalContextAdapter context, Node node, int i) 201 throws MethodInvocationException { 202 Object value = getChild(context, node, i); 203 return (value != null) ? value.toString() : null; 204 } 205 206 211 protected String guessType(String type) { 212 String guessed = (String ) typesGuess.get(type); 213 return guessed != null ? guessed : type; 214 } 215 216 219 protected void bindResult( 220 InternalContextAdapter context, 221 ColumnDescriptor columnDescriptor) { 222 223 Collection resultColumns = 224 (Collection ) context.getInternalUserContext().get( 225 SQLTemplateProcessor.RESULT_COLUMNS_LIST_KEY); 226 227 if (resultColumns != null) { 228 resultColumns.add(columnDescriptor); 229 } 230 } 231 } 232 | Popular Tags |