1 24 package org.ofbiz.entity.jdbc; 25 26 import java.io.ByteArrayInputStream ; 27 import java.io.IOException ; 28 import java.io.InputStream ; 29 import java.io.ObjectInputStream ; 30 import java.io.Reader ; 31 import java.math.BigDecimal ; 32 import java.sql.ResultSet ; 33 import java.sql.ResultSetMetaData ; 34 import java.sql.SQLException ; 35 import java.sql.Clob ; 36 import java.util.Collection ; 37 import java.util.Iterator ; 38 import java.util.List ; 39 import java.util.Map ; 40 import java.util.Set ; 41 import java.util.TreeSet ; 42 43 import javolution.util.FastMap; 44 45 import org.ofbiz.base.util.Debug; 46 import org.ofbiz.base.util.ObjectType; 47 import org.ofbiz.base.util.UtilValidate; 48 import org.ofbiz.entity.GenericDataSourceException; 49 import org.ofbiz.entity.GenericEntity; 50 import org.ofbiz.entity.GenericEntityException; 51 import org.ofbiz.entity.GenericModelException; 52 import org.ofbiz.entity.GenericNotImplementedException; 53 import org.ofbiz.entity.GenericValue; 54 import org.ofbiz.entity.condition.EntityConditionParam; 55 import org.ofbiz.entity.condition.OrderByList; 56 import org.ofbiz.entity.config.DatasourceInfo; 57 import org.ofbiz.entity.datasource.GenericDAO; 58 import org.ofbiz.entity.model.ModelEntity; 59 import org.ofbiz.entity.model.ModelField; 60 import org.ofbiz.entity.model.ModelFieldType; 61 import org.ofbiz.entity.model.ModelFieldTypeReader; 62 import org.ofbiz.entity.model.ModelKeyMap; 63 import org.ofbiz.entity.model.ModelViewEntity; 64 65 76 public class SqlJdbcUtil { 77 public static final String module = GenericDAO.class.getName(); 78 79 public static final int CHAR_BUFFER_SIZE = 4096; 80 81 82 public static String makeFromClause(ModelEntity modelEntity, DatasourceInfo datasourceInfo) throws GenericEntityException { 83 StringBuffer sql = new StringBuffer (" FROM "); 84 85 if (modelEntity instanceof ModelViewEntity) { 86 ModelViewEntity modelViewEntity = (ModelViewEntity) modelEntity; 87 88 if ("ansi".equals(datasourceInfo.joinStyle) || "ansi-no-parenthesis".equals(datasourceInfo.joinStyle)) { 89 boolean useParenthesis = true; 90 if ("ansi-no-parenthesis".equals(datasourceInfo.joinStyle)) { 91 useParenthesis = false; 92 } 93 94 96 100 Set joinedAliasSet = new TreeSet (); 106 107 110 StringBuffer openParens = null; 111 if (useParenthesis) openParens = new StringBuffer (); 112 StringBuffer restOfStatement = new StringBuffer (); 113 114 for (int i = 0; i < modelViewEntity.getViewLinksSize(); i++) { 115 if (i > 0 && useParenthesis) openParens.append('('); 117 118 ModelViewEntity.ModelViewLink viewLink = modelViewEntity.getViewLink(i); 119 120 ModelEntity linkEntity = modelViewEntity.getMemberModelEntity(viewLink.getEntityAlias()); 121 ModelEntity relLinkEntity = modelViewEntity.getMemberModelEntity(viewLink.getRelEntityAlias()); 122 123 126 if (i == 0) { 127 restOfStatement.append(makeViewTable(linkEntity, datasourceInfo)); 129 restOfStatement.append(" "); 131 restOfStatement.append(viewLink.getEntityAlias()); 132 133 joinedAliasSet.add(viewLink.getEntityAlias()); 134 } else { 135 if (!joinedAliasSet.contains(viewLink.getEntityAlias())) { 137 throw new GenericModelException("Tried to link the " + viewLink.getEntityAlias() + " alias to the " + viewLink.getRelEntityAlias() + " alias of the " + modelViewEntity.getEntityName() + " view-entity, but it is not the first view-link and has not been included in a previous view-link. In other words, the left/main alias isn't connected to the rest of the member-entities yet."); 138 } 139 } 140 joinedAliasSet.add(viewLink.getRelEntityAlias()); 142 143 if (viewLink.isRelOptional()) { 144 restOfStatement.append(" LEFT OUTER JOIN "); 145 } else { 146 restOfStatement.append(" INNER JOIN "); 147 } 148 149 restOfStatement.append(makeViewTable(relLinkEntity, datasourceInfo)); 150 restOfStatement.append(" "); 152 restOfStatement.append(viewLink.getRelEntityAlias()); 153 restOfStatement.append(" ON "); 154 155 StringBuffer condBuffer = new StringBuffer (); 156 157 for (int j = 0; j < viewLink.getKeyMapsSize(); j++) { 158 ModelKeyMap keyMap = viewLink.getKeyMap(j); 159 ModelField linkField = linkEntity.getField(keyMap.getFieldName()); 160 if (linkField == null) { 161 throw new GenericModelException("Invalid field name in view-link key-map for the " + viewLink.getEntityAlias() + " and the " + viewLink.getRelEntityAlias() + " member-entities of the " + modelViewEntity.getEntityName() + " view-entity; the field [" + keyMap.getFieldName() + "] does not exist on the [" + linkEntity.getEntityName() + "] entity."); 162 } 163 ModelField relLinkField = relLinkEntity.getField(keyMap.getRelFieldName()); 164 if (relLinkField == null) { 165 throw new GenericModelException("Invalid related field name in view-link key-map for the " + viewLink.getEntityAlias() + " and the " + viewLink.getRelEntityAlias() + " member-entities of the " + modelViewEntity.getEntityName() + " view-entity; the field [" + keyMap.getRelFieldName() + "] does not exist on the [" + relLinkEntity.getEntityName() + "] entity."); 166 } 167 168 if (condBuffer.length() > 0) { 169 condBuffer.append(" AND "); 170 } 171 172 condBuffer.append(viewLink.getEntityAlias()); 173 condBuffer.append("."); 174 condBuffer.append(filterColName(linkField.getColName())); 175 176 condBuffer.append(" = "); 177 178 condBuffer.append(viewLink.getRelEntityAlias()); 179 condBuffer.append("."); 180 condBuffer.append(filterColName(relLinkField.getColName())); 181 } 182 if (condBuffer.length() == 0) { 183 throw new GenericModelException("No view-link/join key-maps found for the " + viewLink.getEntityAlias() + " and the " + viewLink.getRelEntityAlias() + " member-entities of the " + modelViewEntity.getEntityName() + " view-entity."); 184 } 185 restOfStatement.append(condBuffer.toString()); 186 187 if (i < (modelViewEntity.getViewLinksSize() - 1) && useParenthesis) restOfStatement.append(')'); 189 } 190 191 if (useParenthesis) sql.append(openParens.toString()); 192 sql.append(restOfStatement.toString()); 193 194 Iterator meIter = modelViewEntity.getMemberModelMemberEntities().entrySet().iterator(); 196 boolean fromEmpty = restOfStatement.length() == 0; 197 198 while (meIter.hasNext()) { 199 Map.Entry entry = (Map.Entry ) meIter.next(); 200 ModelEntity fromEntity = modelViewEntity.getMemberModelEntity((String ) entry.getKey()); 201 202 if (!joinedAliasSet.contains((String ) entry.getKey())) { 203 if (!fromEmpty) sql.append(", "); 204 fromEmpty = false; 205 206 sql.append(makeViewTable(fromEntity, datasourceInfo)); 207 sql.append(" "); 208 sql.append((String ) entry.getKey()); 209 } 210 } 211 212 213 } else if ("theta-oracle".equals(datasourceInfo.joinStyle) || "theta-mssql".equals(datasourceInfo.joinStyle)) { 214 Iterator meIter = modelViewEntity.getMemberModelMemberEntities().entrySet().iterator(); 216 217 while (meIter.hasNext()) { 218 Map.Entry entry = (Map.Entry ) meIter.next(); 219 ModelEntity fromEntity = modelViewEntity.getMemberModelEntity((String ) entry.getKey()); 220 221 sql.append(makeViewTable(fromEntity, datasourceInfo)); 222 sql.append(" "); 223 sql.append((String ) entry.getKey()); 224 if (meIter.hasNext()) sql.append(", "); 225 } 226 227 } else { 229 throw new GenericModelException("The join-style " + datasourceInfo.joinStyle + " is not yet supported"); 230 } 231 } else { 232 sql.append(modelEntity.getTableName(datasourceInfo)); 233 } 234 return sql.toString(); 235 } 236 237 238 public static String makeWhereStringFromFields(List modelFields, Map fields, String operator) { 239 return makeWhereStringFromFields(modelFields, fields, operator, null); 240 } 241 242 243 public static String makeWhereStringFromFields(List modelFields, Map fields, String operator, List entityConditionParams) { 244 if (modelFields.size() < 1) { 245 return ""; 246 } 247 248 StringBuffer returnString = new StringBuffer (""); 249 Iterator iter = modelFields.iterator(); 250 while (iter.hasNext()) { 251 Object item = iter.next(); 252 Object name = null; 253 ModelField modelField = null; 254 if (item instanceof ModelField) { 255 modelField = (ModelField) item; 256 returnString.append(modelField.getColName()); 257 name = modelField.getName(); 258 } else { 259 returnString.append(item); 260 name = item; 261 } 262 263 Object fieldValue = fields.get(name); 264 if (fieldValue != null && fieldValue != GenericEntity.NULL_FIELD) { 265 returnString.append('='); 266 addValue(returnString, modelField, fieldValue, entityConditionParams); 267 } else { 268 returnString.append(" IS NULL"); 269 } 270 271 if (iter.hasNext()) { 272 returnString.append(' '); 273 returnString.append(operator); 274 returnString.append(' '); 275 } 276 } 277 278 return returnString.toString(); 279 } 280 281 public static String makeWhereClause(ModelEntity modelEntity, List modelFields, Map fields, String operator, String joinStyle) throws GenericEntityException { 282 StringBuffer whereString = new StringBuffer (""); 283 284 if (modelFields != null && modelFields.size() > 0) { 285 whereString.append(makeWhereStringFromFields(modelFields, fields, "AND")); 286 } 287 288 String viewClause = makeViewWhereClause(modelEntity, joinStyle); 289 290 if (viewClause.length() > 0) { 291 if (whereString.length() > 0) { 292 whereString.append(' '); 293 whereString.append(operator); 294 whereString.append(' '); 295 } 296 297 whereString.append(viewClause); 298 } 299 300 if (whereString.length() > 0) { 301 return " WHERE " + whereString.toString(); 302 } 303 304 return ""; 305 } 306 307 public static String makeViewWhereClause(ModelEntity modelEntity, String joinStyle) throws GenericEntityException { 308 if (modelEntity instanceof ModelViewEntity) { 309 StringBuffer whereString = new StringBuffer (); 310 ModelViewEntity modelViewEntity = (ModelViewEntity) modelEntity; 311 312 if ("ansi".equals(joinStyle) || "ansi-no-parenthesis".equals(joinStyle)) { 313 } else if ("theta-oracle".equals(joinStyle) || "theta-mssql".equals(joinStyle)) { 315 boolean isOracleStyle = "theta-oracle".equals(joinStyle); 316 boolean isMssqlStyle = "theta-mssql".equals(joinStyle); 317 318 for (int i = 0; i < modelViewEntity.getViewLinksSize(); i++) { 319 ModelViewEntity.ModelViewLink viewLink = modelViewEntity.getViewLink(i); 320 321 ModelEntity linkEntity = modelViewEntity.getMemberModelEntity(viewLink.getEntityAlias()); 322 ModelEntity relLinkEntity = modelViewEntity.getMemberModelEntity(viewLink.getRelEntityAlias()); 323 324 if (linkEntity == null) { 325 throw new GenericEntityException("Link entity not found with alias: " + viewLink.getEntityAlias() + " for entity: " + modelViewEntity.getEntityName()); 326 } 327 328 if (relLinkEntity == null) { 329 throw new GenericEntityException("Rel-Link entity not found with alias: " + viewLink.getRelEntityAlias() + " for entity: " + modelViewEntity.getEntityName()); 330 } 331 332 335 for (int j = 0; j < viewLink.getKeyMapsSize(); j++) { 336 ModelKeyMap keyMap = viewLink.getKeyMap(j); 337 ModelField linkField = linkEntity.getField(keyMap.getFieldName()); 338 ModelField relLinkField = relLinkEntity.getField(keyMap.getRelFieldName()); 339 340 if (whereString.length() > 0) { 341 whereString.append(" AND "); 342 } 343 whereString.append(viewLink.getEntityAlias()); 344 whereString.append("."); 345 whereString.append(filterColName(linkField.getColName())); 346 347 351 if (isMssqlStyle && viewLink.isRelOptional()) whereString.append("*"); 354 whereString.append("="); 355 if (isOracleStyle && viewLink.isRelOptional()) whereString.append(" (+) "); 357 358 whereString.append(viewLink.getRelEntityAlias()); 359 whereString.append("."); 360 whereString.append(filterColName(relLinkField.getColName())); 361 } 362 } 363 } else { 364 throw new GenericModelException("The join-style " + joinStyle + " is not supported"); 365 } 366 367 if (whereString.length() > 0) { 368 return "(" + whereString.toString() + ")"; 369 } 370 } 371 return ""; 372 } 373 374 public static String makeOrderByClause(ModelEntity modelEntity, List orderBy, DatasourceInfo datasourceInfo) throws GenericModelException { 375 return makeOrderByClause(modelEntity, orderBy, false, datasourceInfo); 376 } 377 378 public static String makeOrderByClause(ModelEntity modelEntity, List orderBy, boolean includeTablenamePrefix, DatasourceInfo datasourceInfo) throws GenericModelException { 379 StringBuffer sql = new StringBuffer (""); 380 382 if (orderBy != null && orderBy.size() > 0) { 383 if (Debug.verboseOn()) Debug.logVerbose("Order by list contains: " + orderBy.size() + " entries.", module); 384 OrderByList orderByList = new OrderByList(orderBy); 385 orderByList.checkOrderBy(modelEntity); 386 orderByList.makeOrderByString(sql, modelEntity, includeTablenamePrefix, datasourceInfo); 387 } 388 if (Debug.verboseOn()) Debug.logVerbose("makeOrderByClause: " + sql.toString(), module); 389 return sql.toString(); 390 } 391 392 public static String makeViewTable(ModelEntity modelEntity, DatasourceInfo datasourceInfo) throws GenericEntityException { 393 if (modelEntity instanceof ModelViewEntity) { 394 StringBuffer sql = new StringBuffer ("(SELECT "); 395 Iterator fieldsIter = modelEntity.getFieldsIterator(); 396 if (fieldsIter.hasNext()) { 397 ModelField curField = (ModelField) fieldsIter.next(); 398 String colname = curField.getColName(); 399 sql.append(colname); 400 sql.append(" AS "); 401 sql.append(filterColName(colname)); 402 while (fieldsIter.hasNext()) { 403 curField = (ModelField) fieldsIter.next(); 404 colname = curField.getColName(); 405 sql.append(", "); 406 sql.append(colname); 407 sql.append(" AS "); 408 sql.append(filterColName(colname)); 409 } 410 } 411 sql.append(makeFromClause(modelEntity, datasourceInfo)); 412 String viewWhereClause = makeViewWhereClause(modelEntity, datasourceInfo.joinStyle); 413 if (UtilValidate.isNotEmpty(viewWhereClause)) { 414 sql.append(" WHERE "); 415 sql.append(viewWhereClause); 416 } 417 ModelViewEntity modelViewEntity = (ModelViewEntity)modelEntity; 418 String groupByString = modelViewEntity.colNameString(modelViewEntity.getGroupBysCopy(), ", ", "", false); 419 if (UtilValidate.isNotEmpty(groupByString)) { 420 sql.append(" GROUP BY "); 421 sql.append(groupByString); 422 } 423 424 sql.append(")"); 425 return sql.toString(); 426 } else { 427 return modelEntity.getTableName(datasourceInfo); 428 } 429 } 430 431 public static String filterColName(String colName) { 432 return colName.replace('.', '_').replace('(','_').replace(')','_'); 433 } 434 435 436 437 438 439 448 public static void setValues(SQLProcessor sqlP, List list, GenericEntity entity, ModelFieldTypeReader modelFieldTypeReader) throws GenericEntityException { 449 Iterator fieldIter = list.iterator(); 450 while (fieldIter.hasNext()) { 451 ModelField curField = (ModelField) fieldIter.next(); 452 setValue(sqlP, curField, entity, modelFieldTypeReader); 453 } 454 } 455 456 466 public static void setValuesWhereClause(SQLProcessor sqlP, List list, GenericValue dummyValue, ModelFieldTypeReader modelFieldTypeReader) throws GenericEntityException { 467 Iterator fieldIter = list.iterator(); 468 while (fieldIter.hasNext()) { 469 ModelField curField = (ModelField) fieldIter.next(); 470 if (dummyValue.get(curField.getName()) != null) { 472 setValue(sqlP, curField, dummyValue, modelFieldTypeReader); 473 } 474 } 475 } 476 477 487 public static void setPkValues(SQLProcessor sqlP, ModelEntity modelEntity, GenericEntity entity, ModelFieldTypeReader modelFieldTypeReader) throws GenericEntityException { 488 Iterator pksIter = modelEntity.getPksIterator(); 489 while (pksIter.hasNext()) { 490 ModelField curField = (ModelField) pksIter.next(); 491 492 if (entity.dangerousGetNoCheckButFast(curField) != null) { 494 setValue(sqlP, curField, entity, modelFieldTypeReader); 495 } 496 } 497 } 498 499 public static void getValue(ResultSet rs, int ind, ModelField curField, GenericEntity entity, ModelFieldTypeReader modelFieldTypeReader) throws GenericEntityException { 500 ModelFieldType mft = modelFieldTypeReader.getModelFieldType(curField.getType()); 501 502 if (mft == null) { 503 throw new GenericModelException("definition fieldType " + curField.getType() + " not found, cannot getValue for field " + 504 entity.getEntityName() + "." + curField.getName() + "."); 505 } 506 String fieldType = mft.getJavaType(); 507 508 try { 509 int typeValue = getType(fieldType); 511 ResultSetMetaData rsmd = rs.getMetaData(); 512 int colType = rsmd.getColumnType(ind); 513 514 if (typeValue <= 4 || typeValue >= 11) { 515 switch (typeValue) { 516 case 1: 517 if (java.sql.Types.CLOB == colType) { 518 521 Clob valueClob = rs.getClob(ind); 522 Reader valueReader = null; 523 if (valueClob != null) { 524 valueReader = valueClob.getCharacterStream(); 525 } 526 527 if (valueReader != null) { 529 char[] inCharBuffer = new char[CHAR_BUFFER_SIZE]; 530 StringBuffer strBuf = new StringBuffer (); 531 int charsRead = 0; 532 try { 533 while ((charsRead = valueReader.read(inCharBuffer, 0, CHAR_BUFFER_SIZE)) > 0) { 534 strBuf.append(inCharBuffer, 0, charsRead); 535 } 536 valueReader.close(); 537 } catch (IOException e) { 538 throw new GenericEntityException("Error reading long character stream for field " + curField.getName() + " of entity " + entity.getEntityName(), e); 539 } 540 entity.dangerousSetNoCheckButFast(curField, strBuf.toString()); 541 } else { 542 entity.dangerousSetNoCheckButFast(curField, null); 543 } 544 } else { 545 String value = rs.getString(ind); 546 entity.dangerousSetNoCheckButFast(curField, value); 547 } 548 break; 549 550 case 2: 551 entity.dangerousSetNoCheckButFast(curField, rs.getTimestamp(ind)); 552 break; 553 554 case 3: 555 entity.dangerousSetNoCheckButFast(curField, rs.getTime(ind)); 556 break; 557 558 case 4: 559 entity.dangerousSetNoCheckButFast(curField, rs.getDate(ind)); 560 break; 561 562 case 11: 563 Object obj = null; 564 InputStream binaryInput = null; 565 566 byte[] fieldBytes = rs.getBytes(ind); 567 if (fieldBytes != null && fieldBytes.length > 0) { 568 binaryInput = new ByteArrayInputStream (fieldBytes); 569 } 570 571 if (fieldBytes != null && fieldBytes.length <= 0) { 572 Debug.logWarning("Got bytes back for Object field with length: " + fieldBytes.length + " while getting value : " + curField.getName() + " [" + curField.getColName() + "] (" + ind + "): ", module); 573 } 574 575 581 if (binaryInput != null) { 582 ObjectInputStream in = null; 583 try { 584 in = new ObjectInputStream (binaryInput); 585 obj = in.readObject(); 586 } catch (IOException ex) { 587 throw new GenericDataSourceException("Unable to read BLOB data from input stream while getting value : " + curField.getName() + " [" + curField.getColName() + "] (" + ind + "): " + ex.toString(), ex); 588 } catch (ClassNotFoundException ex) { 589 throw new GenericDataSourceException("Class not found: Unable to cast BLOB data to an Java object while getting value : " + curField.getName() + " [" + curField.getColName() + "] (" + ind + "): " + ex.toString(), ex); 590 } finally { 591 if (in != null) { 592 try { 593 in.close(); 594 } catch (IOException e) { 595 throw new GenericDataSourceException("Unable to close binary input stream while getting value : " + curField.getName() + " [" + curField.getColName() + "] (" + ind + "): " + e.toString(), e); 596 } 597 } 598 } 599 } 600 601 binaryInput = null; 602 entity.dangerousSetNoCheckButFast(curField, obj); 603 break; 604 case 12: 605 entity.dangerousSetNoCheckButFast(curField, rs.getBlob(ind)); 606 break; 607 case 13: 608 entity.dangerousSetNoCheckButFast(curField, rs.getClob(ind)); 609 break; 610 case 14: 611 case 15: 612 entity.dangerousSetNoCheckButFast(curField, rs.getObject(ind)); 613 break; 614 } 615 } else { 616 switch (typeValue) { 617 case 5: 618 int intValue = rs.getInt(ind); 619 if (rs.wasNull()) { 620 entity.dangerousSetNoCheckButFast(curField, null); 621 } else { 622 entity.dangerousSetNoCheckButFast(curField, new Integer (intValue)); 623 } 624 break; 625 626 case 6: 627 long longValue = rs.getLong(ind); 628 if (rs.wasNull()) { 629 entity.dangerousSetNoCheckButFast(curField, null); 630 } else { 631 entity.dangerousSetNoCheckButFast(curField, new Long (longValue)); 632 } 633 break; 634 635 case 7: 636 float floatValue = rs.getFloat(ind); 637 if (rs.wasNull()) { 638 entity.dangerousSetNoCheckButFast(curField, null); 639 } else { 640 entity.dangerousSetNoCheckButFast(curField, new Float (floatValue)); 641 } 642 break; 643 644 case 8: 645 double doubleValue = rs.getDouble(ind); 646 if (rs.wasNull()) { 647 entity.dangerousSetNoCheckButFast(curField, null); 648 } else { 649 entity.dangerousSetNoCheckButFast(curField, new Double (doubleValue)); 650 } 651 break; 652 653 case 9: 654 BigDecimal bigDecimalValue = rs.getBigDecimal(ind); 655 if (rs.wasNull()) { 656 entity.dangerousSetNoCheckButFast(curField, null); 657 } else { 658 entity.dangerousSetNoCheckButFast(curField, bigDecimalValue); 659 } 660 break; 661 662 case 10: 663 boolean booleanValue = rs.getBoolean(ind); 664 if (rs.wasNull()) { 665 entity.dangerousSetNoCheckButFast(curField, null); 666 } else { 667 entity.dangerousSetNoCheckButFast(curField, new Boolean (booleanValue)); 668 } 669 break; 670 } 671 } 672 } catch (SQLException sqle) { 673 throw new GenericDataSourceException("SQL Exception while getting value : " + curField.getName() + " [" + curField.getColName() + "] (" + ind + ")", sqle); 674 } 675 } 676 677 public static void setValue(SQLProcessor sqlP, ModelField modelField, GenericEntity entity, ModelFieldTypeReader modelFieldTypeReader) throws GenericEntityException { 678 Object fieldValue = entity.dangerousGetNoCheckButFast(modelField); 679 680 setValue(sqlP, modelField, entity.getEntityName(), fieldValue, modelFieldTypeReader); 681 } 682 683 public static void setValue(SQLProcessor sqlP, ModelField modelField, String entityName, Object fieldValue, ModelFieldTypeReader modelFieldTypeReader) throws GenericEntityException { 684 ModelFieldType mft = modelFieldTypeReader.getModelFieldType(modelField.getType()); 685 686 if (mft == null) { 687 throw new GenericModelException("GenericDAO.getValue: definition fieldType " + modelField.getType() + " not found, cannot setValue for field " + 688 entityName + "." + modelField.getName() + "."); 689 } 690 691 if (fieldValue == GenericEntity.NULL_FIELD) { 693 fieldValue = null; 694 } 695 696 String fieldType = mft.getJavaType(); 697 if (fieldValue != null) { 698 if (!ObjectType.instanceOf(fieldValue, fieldType)) { 699 Class fieldClass = fieldValue.getClass(); 703 String fieldClassName = fieldClass.getName(); 704 705 if (Debug.verboseOn()) Debug.logVerbose("type of field " + entityName + "." + modelField.getName() + 706 " is " + fieldClassName + ", was expecting " + mft.getJavaType() + "; this may " + 707 "indicate an error in the configuration or in the class, and may result " + 708 "in an SQL-Java data conversion error. Will use the real field type: " + 709 fieldClassName + ", not the definition.", module); 710 fieldType = fieldClassName; 711 } 712 } 713 714 try { 715 int typeValue = getType(fieldType); 716 717 switch (typeValue) { 718 case 1: 719 sqlP.setValue((String ) fieldValue); 720 break; 721 722 case 2: 723 sqlP.setValue((java.sql.Timestamp ) fieldValue); 724 break; 725 726 case 3: 727 sqlP.setValue((java.sql.Time ) fieldValue); 728 break; 729 730 case 4: 731 sqlP.setValue((java.sql.Date ) fieldValue); 732 break; 733 734 case 5: 735 sqlP.setValue((java.lang.Integer ) fieldValue); 736 break; 737 738 case 6: 739 sqlP.setValue((java.lang.Long ) fieldValue); 740 break; 741 742 case 7: 743 sqlP.setValue((java.lang.Float ) fieldValue); 744 break; 745 746 case 8: 747 sqlP.setValue((java.lang.Double ) fieldValue); 748 break; 749 750 case 9: 751 sqlP.setValue((java.math.BigDecimal ) fieldValue); 752 break; 753 754 case 10: 755 sqlP.setValue((java.lang.Boolean ) fieldValue); 756 break; 757 758 case 11: 759 sqlP.setBinaryStream(fieldValue); 760 break; 761 762 case 12: 763 sqlP.setValue((java.sql.Blob ) fieldValue); 764 break; 765 766 case 13: 767 sqlP.setValue((java.sql.Clob ) fieldValue); 768 break; 769 770 case 14: 771 sqlP.setValue(new java.sql.Date (((java.util.Date ) fieldValue).getTime())); 772 break; 773 774 case 15: 775 sqlP.setValue((java.util.Collection ) fieldValue); 776 break; 777 } 778 } catch (GenericNotImplementedException e) { 779 throw new GenericNotImplementedException("Not Implemented Exception while setting value on field [" + modelField.getName() + "] of entity " + entityName + ": " + e.toString(), e); 780 } catch (SQLException sqle) { 781 throw new GenericDataSourceException("SQL Exception while setting value on field [" + modelField.getName() + "] of entity " + entityName + ": ", sqle); 782 } 783 } 784 785 protected static Map fieldTypeMap = FastMap.newInstance(); 786 static { 787 fieldTypeMap.put("java.lang.String", new Integer (1)); 788 fieldTypeMap.put("String", new Integer (1)); 789 fieldTypeMap.put("java.sql.Timestamp", new Integer (2)); 790 fieldTypeMap.put("Timestamp", new Integer (2)); 791 fieldTypeMap.put("java.sql.Time", new Integer (3)); 792 fieldTypeMap.put("Time", new Integer (3)); 793 fieldTypeMap.put("java.sql.Date", new Integer (4)); 794 fieldTypeMap.put("Date", new Integer (4)); 795 fieldTypeMap.put("java.lang.Integer", new Integer (5)); 796 fieldTypeMap.put("Integer", new Integer (5)); 797 fieldTypeMap.put("java.lang.Long", new Integer (6)); 798 fieldTypeMap.put("Long", new Integer (6)); 799 fieldTypeMap.put("java.lang.Float", new Integer (7)); 800 fieldTypeMap.put("Float", new Integer (7)); 801 fieldTypeMap.put("java.lang.Double", new Integer (8)); 802 fieldTypeMap.put("Double", new Integer (8)); 803 fieldTypeMap.put("java.math.BigDecimal", new Integer (9)); 804 fieldTypeMap.put("BigDecimal", new Integer (9)); 805 fieldTypeMap.put("java.lang.Boolean", new Integer (10)); 806 fieldTypeMap.put("Boolean", new Integer (10)); 807 808 fieldTypeMap.put("java.lang.Object", new Integer (11)); 809 fieldTypeMap.put("Object", new Integer (11)); 810 fieldTypeMap.put("java.sql.Blob", new Integer (12)); 811 fieldTypeMap.put("Blob", new Integer (12)); 812 fieldTypeMap.put("java.sql.Clob", new Integer (13)); 813 fieldTypeMap.put("Clob", new Integer (13)); 814 815 fieldTypeMap.put("java.util.Date", new Integer (14)); 816 817 fieldTypeMap.put("java.util.ArrayList", new Integer (15)); 819 fieldTypeMap.put("java.util.HashSet", new Integer (15)); 820 fieldTypeMap.put("java.util.LinkedHashSet", new Integer (15)); 821 fieldTypeMap.put("java.util.LinkedList", new Integer (15)); 822 } 823 824 public static int getType(String fieldType) throws GenericNotImplementedException { 825 Integer val = (Integer ) fieldTypeMap.get(fieldType); 826 827 if (val == null) { 828 throw new GenericNotImplementedException("Java type " + fieldType + " not currently supported. Sorry."); 829 } 830 return val.intValue(); 831 } 832 833 public static void addValueSingle(StringBuffer buffer, ModelField field, Object value, List params) { 834 if (field != null) { 835 buffer.append('?'); 836 } else { 837 buffer.append('\'').append(value).append('\''); 838 } 839 if (field != null && params != null) params.add(new EntityConditionParam(field, value)); 840 } 841 842 public static void addValue(StringBuffer buffer, ModelField field, Object value, List params) { 843 if (value instanceof Collection ) { 844 buffer.append("( "); 845 Iterator it = ((Collection ) value).iterator(); 846 while (it.hasNext()) { 847 Object thisValue = it.next(); 848 addValueSingle(buffer, field, thisValue, params); 849 if (it.hasNext()) buffer.append(", "); 850 } 851 buffer.append(" )"); 852 } else { 853 addValueSingle(buffer, field, value, params); 854 } 855 } 856 } 857 | Popular Tags |