1 10 11 package com.triactive.jdo.store; 12 13 import com.triactive.jdo.PersistenceManager; 14 import java.sql.Connection ; 15 import java.sql.PreparedStatement ; 16 import java.sql.SQLException ; 17 import java.util.ArrayList ; 18 import java.util.Collections ; 19 import java.util.HashMap ; 20 import java.util.HashSet ; 21 import java.util.Iterator ; 22 import java.util.Set ; 23 import javax.jdo.JDOFatalInternalException; 24 import org.apache.log4j.Category; 25 26 27 class StatementText 28 { 29 private static final Category LOG = Category.getInstance(StatementText.class); 30 31 private static int nextParamID = 0; 32 33 private StringBuffer sql; 34 private ArrayList parameterNames = null; 35 private HashMap parameterMappingsByName = null; 36 private HashMap parameterValuesByName = null; 37 private HashSet referencedColumns = null; 38 39 40 public StatementText() 41 { 42 sql = new StringBuffer (); 43 } 44 45 public StatementText(String initialSql) 46 { 47 sql = new StringBuffer (initialSql); 48 } 49 50 private void initParameters() 51 { 52 if (parameterNames == null) 53 { 54 parameterNames = new ArrayList (); 55 parameterMappingsByName = new HashMap (); 56 parameterValuesByName = new HashMap (); 57 } 58 } 59 60 private void initReferencedColumns() 61 { 62 if (referencedColumns == null) 63 referencedColumns = new HashSet (); 64 } 65 66 public Set getReferencedColumns() 67 { 68 if (referencedColumns == null) 69 return Collections.EMPTY_SET; 70 else 71 return Collections.unmodifiableSet(referencedColumns); 72 } 73 74 public StatementText prepend(char c) 75 { 76 sql.insert(0, c); 77 return this; 78 } 79 80 public StatementText prepend(String s) 81 { 82 sql.insert(0, s); 83 return this; 84 } 85 86 public StatementText append(char c) 87 { 88 sql.append(c); 89 return this; 90 } 91 92 public StatementText append(String s) 93 { 94 sql.append(s); 95 return this; 96 } 97 98 public StatementText append(QueryStatement.QueryColumn qsc) 99 { 100 sql.append(qsc); 101 102 initReferencedColumns(); 103 referencedColumns.add(qsc); 104 return this; 105 } 106 107 public StatementText append(StatementText st) 108 { 109 sql.append(st); 110 111 if (st.parameterNames != null) 112 { 113 initParameters(); 114 parameterNames.addAll(st.parameterNames); 115 parameterMappingsByName.putAll(st.parameterMappingsByName); 116 parameterValuesByName.putAll(st.parameterValuesByName); 117 } 118 119 if (st.referencedColumns != null) 120 { 121 initReferencedColumns(); 122 referencedColumns.addAll(st.referencedColumns); 123 } 124 125 return this; 126 } 127 128 public StatementText append(SQLExpression expr) 129 { 130 return append(expr.toStatementText()); 131 } 132 133 public StatementText append(Object o) 134 { 135 sql.append(o); 136 return this; 137 } 138 139 public String appendParameter(ColumnMapping mapping, Object value) 140 { 141 String name = "param-" + nextParamID++; 142 143 sql.append('?'); 144 145 initParameters(); 146 parameterNames.add(name); 147 parameterMappingsByName.put(name, mapping); 148 parameterValuesByName.put(name, value); 149 150 return name; 151 } 152 153 public void setParameterValue(String name, Object value) 154 { 155 initParameters(); 156 157 if (!parameterValuesByName.containsKey(name)) 158 throw new JDOFatalInternalException("No such statement parameter: " + name); 159 160 parameterValuesByName.put(name, value); 161 } 162 163 public PreparedStatement prepareStatement(PersistenceManager pm, 164 Connection conn) throws SQLException 165 { 166 String stmtText = toString(); 167 168 LOG.debug(stmtText); 169 170 PreparedStatement ps = conn.prepareStatement(stmtText); 171 boolean done = false; 172 173 try 174 { 175 setParameters(pm, ps); 176 done = true; 177 } 178 finally 179 { 180 if (!done) 181 ps.close(); 182 } 183 184 return ps; 185 } 186 187 public PreparedStatement prepareStatement(PersistenceManager pm, 188 Connection conn, 189 int resultSetType, 190 int resultSetConcurrency) throws SQLException 191 { 192 String stmtText = toString(); 193 194 LOG.debug(stmtText); 195 196 PreparedStatement ps = conn.prepareStatement(stmtText, resultSetType, resultSetConcurrency); 197 boolean done = false; 198 199 try 200 { 201 setParameters(pm, ps); 202 done = true; 203 } 204 finally 205 { 206 if (!done) 207 ps.close(); 208 } 209 210 return ps; 211 } 212 213 private void setParameters(PersistenceManager pm, PreparedStatement ps) 214 { 215 if (parameterNames != null) 216 { 217 Iterator i = parameterNames.iterator(); 218 int stmtParamNum = 1; 219 220 while (i.hasNext()) 221 { 222 String name = (String )i.next(); 223 ColumnMapping mapping = (ColumnMapping)parameterMappingsByName.get(name); 224 Object value = parameterValuesByName.get(name); 225 226 mapping.setObject(pm, ps, stmtParamNum++, value); 227 } 228 } 229 } 230 231 public String toString() 232 { 233 return sql.toString(); 234 } 235 } 236 | Popular Tags |