1 56 package org.objectstyle.cayenne.access.jdbc; 57 58 import java.io.IOException ; 59 import java.io.Writer ; 60 import java.util.Collection ; 61 62 import org.apache.velocity.context.InternalContextAdapter; 63 import org.apache.velocity.exception.MethodInvocationException; 64 import org.apache.velocity.exception.ParseErrorException; 65 import org.apache.velocity.exception.ResourceNotFoundException; 66 import org.apache.velocity.runtime.directive.Directive; 67 import org.apache.velocity.runtime.parser.node.Node; 68 import org.objectstyle.cayenne.dba.TypesMapping; 69 import org.objectstyle.cayenne.util.ConversionUtil; 70 71 98 public class BindDirective extends Directive { 99 100 public String getName() { 101 return "bind"; 102 } 103 104 public int getType() { 105 return LINE; 106 } 107 108 113 public boolean render(InternalContextAdapter context, Writer writer, Node node) 114 throws 115 IOException , 116 ResourceNotFoundException, 117 ParseErrorException, 118 MethodInvocationException { 119 120 Object value = getChild(context, node, 0); 121 Object type = getChild(context, node, 1); 122 Object precision = getChild(context, node, 2); 123 124 int jdbcType = TypesMapping.NOT_DEFINED; 125 if (type != null) { 126 jdbcType = TypesMapping.getSqlTypeByName(type.toString()); 127 } 128 else if (value != null) { 129 jdbcType = TypesMapping.getSqlTypeByJava(value.getClass()); 130 } 131 132 if (jdbcType == TypesMapping.NOT_DEFINED) { 133 throw new ParseErrorException( 134 "Can't determine JDBC type of binding (" 135 + value 136 + ", " 137 + type 138 + ") at line " 139 + node.getLine() 140 + ", column " 141 + node.getColumn()); 142 } 143 144 ParameterBinding binding = 145 new ParameterBinding(value, jdbcType, ConversionUtil.toInt(precision, -1)); 146 147 render(context, writer, binding); 148 return true; 149 } 150 151 protected void render( 152 InternalContextAdapter context, 153 Writer writer, 154 ParameterBinding binding) 155 throws IOException { 156 157 bind(context, binding); 158 writer.write('?'); 159 } 160 161 protected Object getChild(InternalContextAdapter context, Node node, int i) 162 throws MethodInvocationException { 163 return (i >= 0 && i < node.jjtGetNumChildren()) 164 ? node.jjtGetChild(i).value(context) 165 : null; 166 } 167 168 171 protected void bind(InternalContextAdapter context, ParameterBinding binding) { 172 Collection bindings = 173 (Collection ) context.getInternalUserContext().get( 174 SQLTemplateProcessor.BINDINGS_LIST_KEY); 175 176 if (bindings != null) { 177 bindings.add(binding); 178 } 179 } 180 } 181 | Popular Tags |