1 19 20 package org.apache.cayenne.access.jdbc; 21 22 import java.io.IOException ; 23 import java.io.Writer ; 24 import java.util.Collection ; 25 import java.util.Iterator ; 26 27 import org.apache.velocity.context.InternalContextAdapter; 28 import org.apache.velocity.exception.MethodInvocationException; 29 import org.apache.velocity.exception.ParseErrorException; 30 import org.apache.velocity.exception.ResourceNotFoundException; 31 import org.apache.velocity.runtime.directive.Directive; 32 import org.apache.velocity.runtime.parser.node.Node; 33 import org.apache.cayenne.dba.TypesMapping; 34 import org.apache.cayenne.util.ConversionUtil; 35 36 63 public class BindDirective extends Directive { 64 65 public String getName() { 66 return "bind"; 67 } 68 69 public int getType() { 70 return LINE; 71 } 72 73 78 public boolean render(InternalContextAdapter context, Writer writer, Node node) 79 throws 80 IOException , 81 ResourceNotFoundException, 82 ParseErrorException, 83 MethodInvocationException { 84 85 Object value = getChild(context, node, 0); 86 Object type = getChild(context, node, 1); 87 Object precision = getChild(context, node, 2); 88 89 int jdbcType = TypesMapping.NOT_DEFINED; 90 if (type != null) { 91 jdbcType = TypesMapping.getSqlTypeByName(type.toString()); 92 } 93 else if (value != null) { 94 jdbcType = TypesMapping.getSqlTypeByJava(value.getClass()); 95 } 96 97 if (jdbcType == TypesMapping.NOT_DEFINED) { 98 throw new ParseErrorException( 99 "Can't determine JDBC type of binding (" 100 + value 101 + ", " 102 + type 103 + ") at line " 104 + node.getLine() 105 + ", column " 106 + node.getColumn()); 107 } 108 109 ParameterBinding binding = 110 new ParameterBinding(value, jdbcType, ConversionUtil.toInt(precision, -1)); 111 112 render(context, writer, binding); 113 return true; 114 } 115 116 protected void render( 117 InternalContextAdapter context, 118 Writer writer, 119 ParameterBinding binding) 120 throws IOException { 121 122 bind(context, binding); 123 124 if (binding.getValue() instanceof Collection ) { 125 Collection bindingList = (Collection ) binding.getValue(); 126 for (Iterator bindingIter = bindingList.iterator(); bindingIter.hasNext(); ) { 127 128 bindingIter.next(); 129 writer.write('?'); 130 131 if (bindingIter.hasNext()) { 132 writer.write(','); 133 } 134 135 } 136 } else { 137 writer.write('?'); 138 } 139 } 140 141 protected Object getChild(InternalContextAdapter context, Node node, int i) 142 throws MethodInvocationException { 143 return (i >= 0 && i < node.jjtGetNumChildren()) 144 ? node.jjtGetChild(i).value(context) 145 : null; 146 } 147 148 151 protected void bind(InternalContextAdapter context, ParameterBinding binding) { 152 Collection bindings = 153 (Collection ) context.getInternalUserContext().get( 154 SQLTemplateProcessor.BINDINGS_LIST_KEY); 155 156 if (bindings != null) { 157 158 if (binding.getValue() instanceof Collection ) { 159 Collection bindingList = (Collection ) binding.getValue(); 160 for (Iterator bindingIter = bindingList.iterator(); bindingIter.hasNext(); ) { 161 bindings.add(new ParameterBinding(bindingIter.next(), binding.getJdbcType(), binding.getPrecision())); 162 } 163 } else { 164 bindings.add(binding); 165 } 166 } 167 } 168 } 169 | Popular Tags |