1 21 package net.mlw.vlh.adapter.jdbc.util; 22 23 import java.sql.Connection ; 24 import java.sql.PreparedStatement ; 25 import java.sql.ResultSet ; 26 import java.sql.SQLException ; 27 import java.text.ParseException ; 28 import java.util.Collections ; 29 import java.util.Iterator ; 30 import java.util.LinkedList ; 31 import java.util.List ; 32 import java.util.Map ; 33 34 import net.mlw.vlh.adapter.jdbc.util.setter.StringSetter; 35 import net.mlw.vlh.adapter.util.FilterTextManipulator; 36 import net.mlw.vlh.adapter.util.TextManipulator; 37 import net.mlw.vlh.adapter.util.TokenReplaceTextManipulator; 38 39 import org.apache.commons.logging.Log; 40 import org.apache.commons.logging.LogFactory; 41 42 62 public class ConfigurableStatementBuilder implements StatementBuilder 63 { 64 65 private static final Log LOGGER = LogFactory.getLog(ConfigurableStatementBuilder.class); 66 67 private Map setters; 68 69 private Setter defaultSetter = new StringSetter(); 70 71 private List textManipulators; 72 73 private boolean init = false; 74 75 77 public void init() 78 { 79 if (textManipulators == null) 80 { 81 textManipulators = new LinkedList (); 82 textManipulators.add(new FilterTextManipulator()); 83 textManipulators.add(new TokenReplaceTextManipulator()); 84 } 85 init = true; 86 } 87 88 91 public PreparedStatement generate(Connection conn, StringBuffer query, Map whereClause, boolean scrollable) throws SQLException , 92 ParseException 93 { 94 if (!init) 95 { 96 init(); 97 } 98 99 if (whereClause == null) 100 { 101 whereClause = Collections.EMPTY_MAP; 102 } 103 104 for (Iterator iter = textManipulators.iterator(); iter.hasNext();) 105 { 106 TextManipulator manipulator = (TextManipulator) iter.next(); 107 manipulator.manipulate(query, whereClause); 108 } 109 110 LinkedList arguments = new LinkedList (); 111 112 for (int i = 0, end = 0, start; ((start = query.toString().indexOf('{', end)) >= 0); i++) 115 { 116 end = query.toString().indexOf('}', start); 117 118 String key = query.substring(start + 1, end); 119 120 Object value = whereClause.get(key); 121 if (value == null) 122 { 123 throw new NullPointerException ("Property '" + key + "' was not provided."); 124 } 125 arguments.add(new NamedPair(key, value)); 126 Setter setter = getSetter(key); 127 query.replace(start, end + 1, setter.getReplacementString(value)); 128 end -= (key.length() + 2); 129 } 130 131 PreparedStatement statement = null; 132 if (scrollable) 133 { 134 statement = conn.prepareStatement(query.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); 135 } 136 else 137 { 138 statement = conn.prepareStatement(query.toString()); 139 } 140 141 int index = 1; 142 for (Iterator iter = arguments.iterator(); iter.hasNext();) 144 { 145 NamedPair namedPair = (NamedPair) iter.next(); 146 Setter setter = getSetter(namedPair.getName()); 147 try 148 { 149 index = setter.set(statement, index, namedPair.getValue()); 150 } 151 catch (RuntimeException e) 152 { 153 String message = "Cannot set value of " + namedPair.getName() + " (setter = " + setter + ")"; 154 LOGGER.error(message, e); 155 throw new RuntimeException (message, e); 156 } 157 } 158 159 return statement; 160 } 161 162 167 public Setter getSetter(String name) 168 { 169 Setter setter = ((setters == null) ? defaultSetter : (Setter) setters.get(name)); 170 return (setter == null) ? defaultSetter : setter; 171 } 172 173 176 public void setSetters(Map setters) 177 { 178 this.setters = setters; 179 } 180 181 184 public void setDefaultSetter(Setter defaultSetter) 185 { 186 this.defaultSetter = defaultSetter; 187 } 188 189 192 public void setTextManipulators(List textManipulators) 193 { 194 this.textManipulators = textManipulators; 195 } 196 } | Popular Tags |