1 19 20 package org.apache.cayenne.access.jdbc; 21 22 import java.io.IOException ; 23 import java.io.Writer ; 24 import java.math.BigDecimal ; 25 import java.math.BigInteger ; 26 import java.sql.Date ; 27 import java.sql.Time ; 28 import java.sql.Timestamp ; 29 import java.util.Collection ; 30 import java.util.HashMap ; 31 import java.util.Map ; 32 33 import org.apache.velocity.context.InternalContextAdapter; 34 import org.apache.velocity.exception.MethodInvocationException; 35 import org.apache.velocity.exception.ParseErrorException; 36 import org.apache.velocity.exception.ResourceNotFoundException; 37 import org.apache.velocity.runtime.directive.Directive; 38 import org.apache.velocity.runtime.parser.node.Node; 39 import org.apache.cayenne.util.Util; 40 41 67 public class ResultDirective extends Directive { 68 69 private static final Map typesGuess = new HashMap (); 70 71 static { 72 74 typesGuess.put("long", Long .class.getName()); 76 typesGuess.put("double", Double .class.getName()); 77 typesGuess.put("byte", Byte .class.getName()); 78 typesGuess.put("boolean", Boolean .class.getName()); 79 typesGuess.put("float", Float .class.getName()); 80 typesGuess.put("short", Short .class.getName()); 81 typesGuess.put("int", Integer .class.getName()); 82 83 typesGuess.put("Long", Long .class.getName()); 85 typesGuess.put("Double", Double .class.getName()); 86 typesGuess.put("Byte", Byte .class.getName()); 87 typesGuess.put("Boolean", Boolean .class.getName()); 88 typesGuess.put("Float", Float .class.getName()); 89 typesGuess.put("Short", Short .class.getName()); 90 typesGuess.put("Integer", Integer .class.getName()); 91 92 typesGuess.put("String", String .class.getName()); 94 typesGuess.put("Date", Date .class.getName()); 95 typesGuess.put("Time", Time .class.getName()); 96 typesGuess.put("Timestamp", Timestamp .class.getName()); 97 typesGuess.put("BigDecimal", BigDecimal .class.getName()); 98 typesGuess.put("BigInteger", BigInteger .class.getName()); 99 } 100 101 public String getName() { 102 return "result"; 103 } 104 105 public int getType() { 106 return LINE; 107 } 108 109 public boolean render(InternalContextAdapter context, Writer writer, Node node) 110 throws IOException , ResourceNotFoundException, ParseErrorException, 111 MethodInvocationException { 112 113 String column = getChildAsString(context, node, 0); 114 if (column == null) { 115 throw new ParseErrorException("Column name expected at line " 116 + node.getLine() 117 + ", column " 118 + node.getColumn()); 119 } 120 121 String alias = getChildAsString(context, node, 2); 122 String dataRowKey = getChildAsString(context, node, 3); 123 124 String label = (!Util.isEmptyString(dataRowKey)) ? dataRowKey : (!Util 126 .isEmptyString(alias)) ? alias : null; 127 128 ColumnDescriptor columnDescriptor = new ColumnDescriptor(); 129 columnDescriptor.setName(column); 130 columnDescriptor.setLabel(label); 131 132 String type = getChildAsString(context, node, 1); 133 if (type != null) { 134 columnDescriptor.setJavaClass(guessType(type.toString())); 135 } 136 137 Object jdbcType = getChild(context, node, 4); 140 if(jdbcType instanceof Number ) { 141 columnDescriptor.setJdbcType(((Number ) jdbcType).intValue()); 142 } 143 144 writer.write(column); 145 146 148 if (!Util.isEmptyString(alias) && !alias.equals(column)) { 152 writer.write(" AS "); 153 writer.write(alias); 154 } 155 156 bindResult(context, columnDescriptor); 157 return true; 158 } 159 160 protected Object getChild(InternalContextAdapter context, Node node, int i) 161 throws MethodInvocationException { 162 return (i >= 0 && i < node.jjtGetNumChildren()) ? node.jjtGetChild(i).value( 163 context) : null; 164 } 165 166 171 protected String getChildAsString(InternalContextAdapter context, Node node, int i) 172 throws MethodInvocationException { 173 Object value = getChild(context, node, i); 174 return (value != null) ? value.toString() : null; 175 } 176 177 182 protected String guessType(String type) { 183 String guessed = (String ) typesGuess.get(type); 184 return guessed != null ? guessed : type; 185 } 186 187 190 protected void bindResult( 191 InternalContextAdapter context, 192 ColumnDescriptor columnDescriptor) { 193 194 Collection resultColumns = (Collection ) context.getInternalUserContext().get( 195 SQLTemplateProcessor.RESULT_COLUMNS_LIST_KEY); 196 197 if (resultColumns != null) { 198 resultColumns.add(columnDescriptor); 199 } 200 } 201 } 202 | Popular Tags |