1 17 package org.apache.ws.jaxme.pm.generator.jdbc; 18 19 import java.sql.Connection ; 20 import java.sql.Date ; 21 import java.sql.PreparedStatement ; 22 import java.sql.ResultSet ; 23 import java.sql.SQLException ; 24 import java.sql.Time ; 25 import java.sql.Timestamp ; 26 import java.sql.Types ; 27 import java.util.ArrayList ; 28 import java.util.Calendar ; 29 import java.util.Iterator ; 30 import java.util.List ; 31 32 import javax.xml.bind.Element; 33 import javax.xml.bind.JAXBException; 34 35 import org.apache.ws.jaxme.Observer; 36 import org.apache.ws.jaxme.PMException; 37 import org.apache.ws.jaxme.PMParams; 38 import org.apache.ws.jaxme.generator.sg.AttributeSG; 39 import org.apache.ws.jaxme.generator.sg.ComplexTypeSGChain; 40 import org.apache.ws.jaxme.generator.sg.ParticleSG; 41 import org.apache.ws.jaxme.generator.sg.PropertySG; 42 import org.apache.ws.jaxme.generator.sg.TypeSG; 43 import org.apache.ws.jaxme.generator.sg.TypeSGChain; 44 import org.apache.ws.jaxme.generator.sg.impl.TypeSGChainImpl; 45 import org.apache.ws.jaxme.js.DirectAccessible; 46 import org.apache.ws.jaxme.js.JavaInnerClass; 47 import org.apache.ws.jaxme.js.JavaMethod; 48 import org.apache.ws.jaxme.js.JavaQName; 49 import org.apache.ws.jaxme.js.JavaQNameImpl; 50 import org.apache.ws.jaxme.js.JavaSource; 51 import org.apache.ws.jaxme.js.LocalJavaField; 52 import org.apache.ws.jaxme.js.Parameter; 53 import org.apache.ws.jaxme.pm.jdbc.*; 54 import org.apache.ws.jaxme.sqls.Column; 55 import org.apache.ws.jaxme.sqls.Table; 56 import org.apache.ws.jaxme.xs.XSType; 57 import org.xml.sax.SAXException ; 58 59 60 63 public class JdbcTypeSG extends TypeSGChainImpl { 64 private final JaxMeJdbcSG jdbcSG; 65 private XSType xsType; 66 67 protected JdbcTypeSG(JaxMeJdbcSG pJdbcSG, TypeSGChain o, XSType pType) { 68 super(o); 69 this.jdbcSG = pJdbcSG; 70 if (pType == null) { 71 throw new NullPointerException ("The XSType argument must not be null."); 72 } 73 xsType = pType; 74 } 75 76 public Object newComplexTypeSG(TypeSG pController) throws SAXException { 77 if (xsType == null) { 78 throw new IllegalStateException ("An instance of ComplexTypeSGChain has already been created."); 79 } 80 ComplexTypeSGChain chain = (ComplexTypeSGChain) super.newComplexTypeSG(pController); 81 chain = new JdbcComplexTypeSG(jdbcSG, chain, xsType); 82 xsType = null; return chain; 84 } 85 86 public void generate(TypeSG pController) throws SAXException { 87 super.generate(pController); 88 if (!pController.isComplex()) { 89 return; 90 } 91 CustomTableData customTableData = (CustomTableData) pController.getProperty(jdbcSG.getKey()); 92 if (customTableData != null) { 93 JavaQName qName = pController.getComplexTypeSG().getClassContext().getPMName(); 94 JavaSource js = pController.getSchema().getJavaSourceFactory().newJavaSource(qName, JavaSource.PUBLIC); 95 getPMClass(pController, js, customTableData); 96 } 97 } 98 99 public void generate(TypeSG pController, JavaSource pSource) throws SAXException { 100 super.generate(pController, pSource); 101 if (!pController.isComplex()) { 102 return; 103 } 104 CustomTableData customTableData = (CustomTableData) pController.getProperty(jdbcSG.getKey()); 105 if (customTableData != null) { 106 JavaQName qName = pController.getComplexTypeSG().getClassContext().getPMName(); 107 JavaInnerClass jic = pSource.newJavaInnerClass(qName.getClassName()); 108 getPMClass(pController, jic, customTableData); 109 } 110 } 111 112 114 protected void setPreparedStatementValue(JavaMethod pMethod, Column pColumn, 115 Object pStmt, Object pParamNum, 116 Object pValue, TypeSG pTypeSG) 117 throws SAXException { 118 boolean isPrimitive = pTypeSG.getSimpleTypeSG().getRuntimeType().isPrimitive(); 119 Column.Type type = pColumn.getType(); 120 if (isPrimitive) { 121 if (Column.Type.BIGINT.equals(type)) { 122 pMethod.addLine(pStmt, ".setLong(", pParamNum, ", ", pValue, ");"); 123 } else if (Column.Type.BIT.equals(type)) { 124 pMethod.addLine(pStmt, ".setBoolean(", pParamNum, ", ", pValue, ");"); 125 } else if (Column.Type.DOUBLE.equals(type)) { 126 pMethod.addLine(pStmt, ".setDouble(", pParamNum, ", ", pValue, ");"); 127 } else if (Column.Type.FLOAT.equals(type)) { 128 pMethod.addLine(pStmt, ".setFloat(", pParamNum, ", ", pValue, ");"); 129 } else if (Column.Type.INTEGER.equals(type)) { 130 pMethod.addLine(pStmt, ".setInt(", pParamNum, ", ", pValue, ");"); 131 } else if (Column.Type.SMALLINT.equals(type)) { 132 pMethod.addLine(pStmt, ".setShort(", pParamNum, ", ", pValue, ");"); 133 } else if (Column.Type.TINYINT.equals(type)) { 134 pMethod.addLine(pStmt, ".setByte(", pParamNum, ", ", pValue, ");"); 135 } 136 } else { 137 if (!(pValue instanceof DirectAccessible)) { 138 LocalJavaField v = pMethod.newJavaField(pTypeSG.getRuntimeType()); 139 v.addLine(pValue); 140 pValue = v; 141 } 142 pMethod.addIf(pValue, " == null"); 143 pMethod.addLine(pStmt, ".setNull(", pParamNum, ", ", Types .class, ".", type, ");"); 144 pMethod.addElse(); 145 if (Column.Type.BIGINT.equals(type)) { 146 pMethod.addLine(pStmt, ".setLong(", pParamNum, ", ", pValue, ".longValue());"); 147 } else if (pColumn.isBinaryColumn()) { 148 pMethod.addLine(pStmt, ".setBytes(", pParamNum, ", ", pValue, ");"); 149 } else if (pColumn.isStringColumn()) { 150 pMethod.addLine(pStmt, ".setString(", pParamNum, ", ", pValue, ");"); 151 } else if (Column.Type.DATE.equals(type)) { 152 pMethod.addLine(pStmt, ".setDate(", pParamNum, ", new ", java.sql.Date .class, 153 "(", pValue, ".getTime().getTime()));"); 154 } else if (Column.Type.DOUBLE.equals(type)) { 155 pMethod.addLine(pStmt, ".setDouble(", pParamNum, ", ", pValue, ".doubleValue());"); 156 } else if (Column.Type.FLOAT.equals(type)) { 157 pMethod.addLine(pStmt, ".setFloat(", pParamNum, ", ", pValue, ".floatValue());"); 158 } else if (Column.Type.INTEGER.equals(type)) { 159 pMethod.addLine(pStmt, ".setInt(", pParamNum, ", ", pValue, ".intValue());"); 160 } else if (Column.Type.SMALLINT.equals(type)) { 161 pMethod.addLine(pStmt, ".setShort(", pParamNum, ", ", pValue, ".shortValue());"); 162 } else if (Column.Type.TIME.equals(type)) { 163 pMethod.addLine(pStmt, ".setTime(", pParamNum, ", new ", java.sql.Date .class, 164 "(", pValue, ".getTime().getTime()));"); 165 } else if (Column.Type.TIMESTAMP.equals(type)) { 166 pMethod.addLine(pStmt, ".setTimestamp(", pParamNum, ", new ", Timestamp .class, 167 "(", pValue, ".getTime().getTime()));"); 168 } else if (Column.Type.TINYINT.equals(type)) { 169 pMethod.addLine(pStmt, ".setByte(", pParamNum, ", ", pValue, ".byteValue());"); 170 } else { 171 throw new IllegalStateException ("Unknown column type: " + type); 172 } 173 pMethod.addEndIf(); 174 } 175 } 176 177 179 protected Object getResultSetValue(JavaMethod pMethod, Column pColumn, 180 Object pRs, Object pParamNum, 181 TypeSG pTypeSG) 182 throws SAXException { 183 boolean isPrimitive = pTypeSG.getSimpleTypeSG().getRuntimeType().isPrimitive(); 184 Column.Type type = pColumn.getType(); 185 if (isPrimitive) { 186 if (Column.Type.BIGINT.equals(type)) { 187 return new Object []{pRs, ".getLong(", pParamNum, ")"}; 188 } else if (Column.Type.BIT.equals(type)) { 189 return new Object []{pRs, ".getBoolean(", pParamNum, ")"}; 190 } else if (Column.Type.DOUBLE.equals(type)) { 191 return new Object []{pRs, ".getDouble(", pParamNum, ")"}; 192 } else if (Column.Type.FLOAT.equals(type)) { 193 return new Object []{pRs, ".getFloat(", pParamNum, ")"}; 194 } else if (Column.Type.INTEGER.equals(type)) { 195 return new Object []{pRs, ".getInt(", pParamNum, ")"}; 196 } else if (Column.Type.SMALLINT.equals(type)) { 197 return new Object []{pRs, ".getShort(", pParamNum, ")"}; 198 } else if (Column.Type.TINYINT.equals(type)) { 199 return new Object []{pRs, ".getByte(", pParamNum, ")"}; 200 } else { 201 throw new IllegalStateException ("Unknown primitive type: " + type); 202 } 203 } else { 204 if (pColumn.isStringColumn()) { 205 return new Object []{pRs, ".getString(", pParamNum, ")"}; 206 } else if (pColumn.isBinaryColumn()) { 207 return new Object []{pRs, ".getBytes(", pParamNum, ")"}; 208 } else if (Column.Type.DATE.equals(type)) { 209 LocalJavaField cal = pMethod.newJavaField(Calendar .class); 210 LocalJavaField date = pMethod.newJavaField(Date .class); 211 date.addLine(pRs, ".getDate(", pParamNum, ")"); 212 pMethod.addIf(date, " == null"); 213 pMethod.addLine(cal, " = null;"); 214 pMethod.addElse(); 215 pMethod.addLine(cal, " = ", Calendar .class, ".getInstance();"); 216 pMethod.addLine(cal, ".setTime(", date, ");"); 217 pMethod.addLine(cal, ".set(", Calendar .class, ".HOUR, 0);"); 218 pMethod.addLine(cal, ".set(", Calendar .class, ".MINUTE, 0);"); 219 pMethod.addLine(cal, ".set(", Calendar .class, ".SECOND, 0);"); 220 pMethod.addLine(cal, ".set(", Calendar .class, ".MILLISECOND, 0);"); 221 pMethod.addEndIf(); 222 return cal; 223 } else if (Column.Type.TIME.equals(type)) { 224 LocalJavaField cal = pMethod.newJavaField(Calendar .class); 225 LocalJavaField date = pMethod.newJavaField(Time .class); 226 date.addLine(pRs, ".getTime(", pParamNum, ")"); 227 pMethod.addIf(date, " == null"); 228 pMethod.addLine(cal, " = null;"); 229 pMethod.addElse(); 230 pMethod.addLine(cal, " = ", Calendar .class, ".getInstance();"); 231 pMethod.addLine(cal, ".setTime(", date, ");"); 232 pMethod.addLine(cal, ".set(", Calendar .class, ".YEAR, 0);"); 233 pMethod.addLine(cal, ".set(", Calendar .class, ".MONTH, 0);"); 234 pMethod.addLine(cal, ".set(", Calendar .class, ".DAY_OF_MONTH, 0);"); 235 pMethod.addEndIf(); 236 return cal; 237 } else if (Column.Type.TIMESTAMP.equals(type)) { 238 LocalJavaField cal = pMethod.newJavaField(Calendar .class); 239 LocalJavaField date = pMethod.newJavaField(Timestamp .class); 240 date.addLine(pRs, ".getTimestamp(", pParamNum, ")"); 241 pMethod.addIf(date, " == null"); 242 pMethod.addLine(cal, " = null;"); 243 pMethod.addElse(); 244 pMethod.addLine(cal, " = ", Calendar .class, ".getInstance();"); 245 pMethod.addLine(cal, ".setTime(", date, ");"); 246 pMethod.addEndIf(); 247 return cal; 248 } else if (Column.Type.BIT.equals(type)) { 249 LocalJavaField b = pMethod.newJavaField(boolean.class); 250 b.addLine(pRs, ".getBoolean(", pParamNum, ")"); 251 return new Object []{"(", pRs, ".wasNull() ? null : ", 252 b, " ? ", Boolean .class, ".TRUE : ", Boolean .class, ".FALSE)"}; 253 } else if (Column.Type.BIGINT.equals(type)) { 254 LocalJavaField l = pMethod.newJavaField(long.class); 255 l.addLine(pRs, ".getLong(", pParamNum, ")"); 256 return new Object []{"(", pRs, ".wasNull() ? null : new Long(", l, ")"}; 257 } else if (Column.Type.DOUBLE.equals(type)) { 258 LocalJavaField d = pMethod.newJavaField(double.class); 259 d.addLine(pRs, ".getDouble(", pParamNum, ")"); 260 return new Object []{"(", pRs, ".wasNull() ? null : new Double(", d, ")"}; 261 } else if (Column.Type.FLOAT.equals(type)) { 262 LocalJavaField f = pMethod.newJavaField(float.class); 263 f.addLine(pRs, ".getFloat(", pParamNum, ")"); 264 return new Object []{"(", pRs, ".wasNull() ? null : new Float(", f, ")"}; 265 } else if (Column.Type.INTEGER.equals(type)) { 266 LocalJavaField i = pMethod.newJavaField(int.class); 267 i.addLine(pRs, ".getInt(", pParamNum, ")"); 268 return new Object []{"(", pRs, ".wasNull() ? null : new Integer(", i, ")"}; 269 } else if (Column.Type.SMALLINT.equals(type)) { 270 LocalJavaField s = pMethod.newJavaField(short.class); 271 s.addLine(pRs, ".getShort(", pParamNum, ")"); 272 return new Object []{"(", pRs, ".wasNull() ? null : new Short(", s, ")"}; 273 } else if (Column.Type.TINYINT.equals(type)) { 274 LocalJavaField b = pMethod.newJavaField(byte.class); 275 b.addLine(pRs, ".getByte(", pParamNum, ")"); 276 return new Object []{"(", pRs, ".wasNull() ? null : new Byte(", b, ")"}; 277 } else { 278 throw new IllegalStateException ("Unknown column type: " + type); 279 } 280 } 281 } 282 283 protected void getFinally(JavaMethod pMethod, DirectAccessible pRessource, Object pSqlMsg, 284 Object pJaxbMsg) { 285 pMethod.addLine(pRessource, ".close();"); 286 pMethod.addLine(pRessource, " = null;"); 287 if (pSqlMsg != null) { 288 DirectAccessible e = pMethod.addCatch(SQLException .class); 289 pMethod.addThrowNew(PMException.class, pSqlMsg, " + ", JavaSource.getQuoted(": "), 290 " + ", e, ".getMessage(), ", e); 291 } 292 if (pJaxbMsg != null) { 293 DirectAccessible e = pMethod.addCatch(JAXBException.class); 294 pMethod.addIf(e, " instanceof ", PMException.class); 295 pMethod.addLine("throw (", PMException.class, ") ", e, ";"); 296 pMethod.addElse(); 297 pMethod.addThrowNew(PMException.class, pSqlMsg, " + ", JavaSource.getQuoted(": "), 298 " + ", e, ".getMessage(), ", e); 299 pMethod.addEndIf(); 300 } 301 pMethod.addFinally(); 302 pMethod.addIf(pRessource, " != null"); 303 pMethod.addTry(); 304 pMethod.addLine(pRessource, ".close();"); 305 pMethod.addCatch(Throwable .class); 306 pMethod.addEndTry(); 307 pMethod.addEndIf(); 308 pMethod.addEndTry(); 309 } 310 311 private int getPreparedStatementParameters(JavaMethod pMethod, Object pStmt, DirectAccessible pElement, 312 Iterator pColumns, int pParamNum) 313 throws SAXException { 314 for (Iterator iter = pColumns; iter.hasNext(); ) { 315 Column col = (Column) iter.next(); 316 CustomColumnData colData = (CustomColumnData) col.getCustomData(); 317 Object sg = colData.getSG(); 318 PropertySG propertySG; 319 TypeSG typeSG; 320 if (sg instanceof AttributeSG) { 321 AttributeSG attrSG = (AttributeSG) sg; 322 propertySG = attrSG.getPropertySG(); 323 typeSG = attrSG.getTypeSG(); 324 } else if (sg instanceof ParticleSG) { 325 ParticleSG particleSG = (ParticleSG) sg; 326 propertySG = particleSG.getPropertySG(); 327 typeSG = particleSG.getObjectSG().getTypeSG(); 328 } else { 329 throw new IllegalStateException ("Invalid SG type for column " + col.getName() + ": " + sg); 330 } 331 Object value = propertySG.getValue(pElement); 332 setPreparedStatementValue(pMethod, col, pStmt, new Integer (++pParamNum), value, typeSG); 333 } 334 return pParamNum; 335 } 336 337 private int getResultSet(JavaMethod pMethod, DirectAccessible pRs, DirectAccessible pElement, 338 Iterator pColumns, int pParamNum) 339 throws SAXException { 340 for (Iterator iter = pColumns; iter.hasNext(); ) { 341 Column col = (Column) iter.next(); 342 CustomColumnData colData = (CustomColumnData) col.getCustomData(); 343 Object sg = colData.getSG(); 344 PropertySG propertySG; 345 TypeSG typeSG; 346 if (sg instanceof AttributeSG) { 347 AttributeSG attrSG = (AttributeSG) sg; 348 propertySG = attrSG.getPropertySG(); 349 typeSG = attrSG.getTypeSG(); 350 } else if (sg instanceof ParticleSG) { 351 ParticleSG particleSG = (ParticleSG) sg; 352 propertySG = particleSG.getPropertySG(); 353 typeSG = particleSG.getObjectSG().getTypeSG(); 354 } else { 355 throw new IllegalStateException ("Invalid SG type for column " + col.getName() + ": " + sg); 356 } 357 Object value = getResultSetValue(pMethod, col, pRs, new Integer (++pParamNum), typeSG); 358 propertySG.setValue(pMethod, pElement, value, null); 359 } 360 return pParamNum; 361 } 362 363 protected JavaMethod getPMClassInsertMethod(TypeSG pController, JavaSource pSource, CustomTableData pData) 364 throws SAXException { 365 JavaMethod jm = pSource.newJavaMethod("insert", JavaQNameImpl.VOID, JavaSource.PUBLIC); 366 Parameter pElement = jm.addParam(Element.class, "pElement"); 367 jm.addThrows(PMException.class); 368 Table table = pData.getTable(); 369 370 String q = table.getSchema().getSQLFactory().newSQLGenerator().getQuery(table.getInsertStatement()); 371 LocalJavaField query = jm.newJavaField(String .class); 372 query.setFinal(true); 373 query.addLine(JavaSource.getQuoted(q)); 374 375 JavaQName qName = pController.getComplexTypeSG().getClassContext().getXMLInterfaceName(); 376 LocalJavaField elem = jm.newJavaField(qName); 377 elem.addLine("(", qName, ") ", pElement); 378 379 LocalJavaField connection = jm.newJavaField(Connection .class); 380 connection.addLine("null"); 381 jm.addTry(); 382 jm.addLine(connection, " = getConnection();"); 383 LocalJavaField stmt = jm.newJavaField(PreparedStatement .class); 384 stmt.addLine(connection, ".prepareStatement(", query, ")"); 385 jm.addTry(); 386 getPreparedStatementParameters(jm, stmt, elem, table.getColumns(), 0); 387 jm.addLine(stmt, ".executeUpdate();"); 388 getFinally(jm, stmt, null, null); 389 getFinally(jm, connection, new Object []{JavaSource.getQuoted("Failed to execute query "), 390 " + ", query}, null); 391 return jm; 392 } 393 394 protected JavaMethod getPMClassUpdateMethod(TypeSG pController, JavaSource pSource, CustomTableData pData) 395 throws SAXException { 396 JavaMethod jm = pSource.newJavaMethod("update", JavaQNameImpl.VOID, JavaSource.PUBLIC); 397 Parameter pElement = jm.addParam(Element.class, "pElement"); 398 jm.addThrows(PMException.class); 399 Table table = pData.getTable(); 400 401 String q = table.getSchema().getSQLFactory().newSQLGenerator().getQuery(table.getUpdateStatement()); 402 LocalJavaField query = jm.newJavaField(String .class); 403 query.setFinal(true); 404 query.addLine(JavaSource.getQuoted(q)); 405 406 JavaQName qName = pController.getComplexTypeSG().getClassContext().getXMLInterfaceName(); 407 LocalJavaField elem = jm.newJavaField(qName); 408 elem.addLine("(", qName, ") ", pElement); 409 410 LocalJavaField connection = jm.newJavaField(Connection .class); 411 connection.addLine("null"); 412 jm.addTry(); 413 jm.addLine(connection, " = getConnection();"); 414 415 LocalJavaField stmt = jm.newJavaField(PreparedStatement .class); 416 stmt.addLine(connection, ".prepareStatement(", query, ")"); 417 jm.addTry(); 418 419 List nonKeyColumns = new ArrayList (); 420 for (Iterator iter = table.getColumns(); iter.hasNext(); ) { 421 Column col = (Column) iter.next(); 422 if (!col.isPrimaryKeyPart()) { 423 nonKeyColumns.add(col); 424 } 425 } 426 int i = 0; 427 i = getPreparedStatementParameters(jm, stmt, elem, nonKeyColumns.iterator(), i); 428 getPreparedStatementParameters(jm, stmt, elem, table.getPrimaryKey().getColumns(), i); 429 jm.addLine(stmt, ".executeUpdate();"); 430 431 getFinally(jm, stmt, null, null); 432 getFinally(jm, connection, new Object []{JavaSource.getQuoted("Failed to execute query "), 433 " + ", query}, null); 434 return jm; 435 } 436 437 protected JavaMethod getPMClassDeleteMethod(TypeSG pController, JavaSource pSource, CustomTableData pData) 438 throws SAXException { 439 JavaMethod jm = pSource.newJavaMethod("delete", JavaQNameImpl.VOID, JavaSource.PUBLIC); 440 Parameter pElement = jm.addParam(Element.class, "pElement"); 441 jm.addThrows(PMException.class); 442 Table table = pData.getTable(); 443 444 JavaQName qName = pController.getComplexTypeSG().getClassContext().getXMLInterfaceName(); 445 LocalJavaField elem = jm.newJavaField(qName); 446 elem.addLine("(", qName, ") ", pElement); 447 448 String q = table.getSchema().getSQLFactory().newSQLGenerator().getQuery(table.getDeleteStatement()); 449 LocalJavaField query = jm.newJavaField(String .class); 450 query.setFinal(true); 451 query.addLine(JavaSource.getQuoted(q)); 452 453 LocalJavaField connection = jm.newJavaField(Connection .class); 454 connection.addLine("null"); 455 jm.addTry(); 456 jm.addLine(connection, " = getConnection();"); 457 458 LocalJavaField stmt = jm.newJavaField(PreparedStatement .class); 459 stmt.addLine(connection, ".prepareStatement(", query, ")"); 460 jm.addTry(); 461 getPreparedStatementParameters(jm, stmt, elem, table.getPrimaryKey().getColumns(), 0); 462 jm.addLine(stmt, ".executeUpdate();"); 463 464 getFinally(jm, stmt, null, null); 465 getFinally(jm, connection, new Object []{JavaSource.getQuoted("Failed to execute query "), 466 " + ", query}, null); 467 return jm; 468 } 469 470 protected JavaMethod getPMClassSelectMethod(TypeSG pController, JavaSource pSource, CustomTableData pData) 471 throws SAXException { 472 JavaMethod jm = pSource.newJavaMethod("select", JavaQNameImpl.VOID, JavaSource.PUBLIC); 473 Parameter pObserver = jm.addParam(Observer.class, "pObserver"); 474 Parameter pQuery = jm.addParam(String .class, "pQuery"); 475 Parameter pParams = jm.addParam(PMParams.class, "pParams"); 476 jm.addThrows(PMException.class); 477 Table table = pData.getTable(); 478 479 JavaQName qName = pController.getComplexTypeSG().getClassContext().getXMLInterfaceName(); 480 StringBuffer sb = new StringBuffer (); 481 for (Iterator iter = table.getColumns(); iter.hasNext(); ) { 482 Column col = (Column) iter.next(); 483 if (sb.length() > 0) sb.append(", "); 484 sb.append(col.getName().getName()); 485 } 486 LocalJavaField query = jm.newJavaField(String .class); 487 jm.addIf(pParams, " != null && pParams.isDistinct()"); 488 jm.addLine(query, " = ", JavaSource.getQuoted("SELECT DISTINCT"), ";"); 489 jm.addElse(); 490 jm.addLine(query, " = ", JavaSource.getQuoted("SELECT"), ";"); 491 jm.addEndIf(); 492 jm.addLine(query, " += ", JavaSource.getQuoted(" " + sb + " FROM " + table.getQName()), ";"); 493 jm.addIf(pQuery, " != null"); 494 jm.addLine(query, " += ", JavaSource.getQuoted(" WHERE "), " + ", pQuery, ";"); 495 jm.addEndIf(); 496 497 LocalJavaField connection = jm.newJavaField(Connection .class); 498 connection.addLine("null"); 499 jm.addTry(); 500 jm.addLine(connection, " = getConnection();"); 501 LocalJavaField stmt = jm.newJavaField(PreparedStatement .class); 502 stmt.addLine(connection, ".prepareStatement(", query, ")"); 503 jm.addTry(); 504 LocalJavaField rs = jm.newJavaField(ResultSet .class); 505 rs.addLine(stmt, ".executeQuery();"); 506 jm.addTry(); 507 jm.addWhile(rs, ".next()"); 508 LocalJavaField elem = jm.newJavaField(qName); 509 elem.addLine("(", qName, ") create()"); 510 getResultSet(jm, rs, elem, table.getColumns(), 0); 511 jm.addLine(pObserver, ".notify(", elem, ");"); 512 jm.addEndWhile(); 513 514 getFinally(jm, rs, null, null); 515 getFinally(jm, stmt, null, null); 516 Object sqlMsg = new Object []{JavaSource.getQuoted("Failed to execute query "), 517 " + ", query}; 518 Object jaxbMsg = new Object []{JavaSource.getQuoted("Failed to create instance of "), 519 " + ", qName, ".class.getName()"}; 520 getFinally(jm, connection, sqlMsg, jaxbMsg); 521 return jm; 522 } 523 524 protected void getPMClass(TypeSG pController, JavaSource pSource, CustomTableData pTableData) throws SAXException { 525 pSource.addExtends(PMJdbcImpl.class); 526 getPMClassInsertMethod(pController, pSource, pTableData); 527 getPMClassUpdateMethod(pController, pSource, pTableData); 528 getPMClassDeleteMethod(pController, pSource, pTableData); 529 getPMClassSelectMethod(pController, pSource, pTableData); 530 } 531 } 532 | Popular Tags |