// [PJYF Nov 5 2004] // This is major Hack to generate a diffenrent bind dictionary for update and insert form the // one for select and delete public void addUpdateListAttribute(EOAttribute attribute, Object value) { String attributeName = this.sqlStringForAttribute(attribute); String attributeValue = this.sqlStringForForInsertOrUpdateValue(value, attribute.name()); attributeValue = this.formatSQLString(attributeValue, attribute.writeFormat()); this.appendItemToListString( _NSStringUtilities.concat(attributeName, " = ", attributeValue), this._listString()); }
/* * [PJYF Oct 19 2004] * This is a bad hack to get WO to create indes for external keys. * We use the primary key constrain generation to create our indexes. * But we need to be carefull not to overwrite previous constrains * */ protected boolean isSinglePrimaryKeyAttribute(EOAttribute attribute) { if (attribute == null) return false; EOEntity entity = (EOEntity) attribute.entity(); if ((entity == null) || entity.isAbstractEntity() || (entity.externalName() == null)) return false; NSArray primaryKeyAttributes = entity.primaryKeyAttributes(); if (primaryKeyAttributes.count() != 1) return false; return attribute.name().equals(((EOAttribute) primaryKeyAttributes.lastObject()).name()); }
/** * Overrides the parent implementation to provide a more efficient mechanism for generating * primary keys, while generating the primary key support on the fly. * * @param count the batch size * @param entity the entity requesting primary keys * @param channel open JDBCChannel * @return NSArray of NSDictionary where each dictionary corresponds to a unique primary key value */ public NSArray newPrimaryKeys(int count, EOEntity entity, JDBCChannel channel) { if (isPrimaryKeyGenerationNotSupported(entity)) { return null; } EOAttribute attribute = (EOAttribute) entity.primaryKeyAttributes().lastObject(); String attrName = attribute.name(); boolean isIntType = "i".equals(attribute.valueType()); NSMutableArray results = new NSMutableArray(count); String sequenceName = sequenceNameForEntity(entity); DB2Expression expression = new DB2Expression(entity); boolean succeeded = false; for (int tries = 0; !succeeded && tries < 2; tries++) { while (results.count() < count) { try { StringBuffer sql = new StringBuffer(); sql.append("SELECT "); sql.append("next value for " + sequenceName + " AS KEY"); sql.append(" from sysibm.sysdummy1"); expression.setStatement(sql.toString()); channel.evaluateExpression(expression); try { NSDictionary row; while ((row = channel.fetchRow()) != null) { Enumeration pksEnum = row.allValues().objectEnumerator(); while (pksEnum.hasMoreElements()) { Number pkObj = (Number) pksEnum.nextElement(); Number pk; if (isIntType) { pk = Integer.valueOf(pkObj.intValue()); } else { pk = Long.valueOf(pkObj.longValue()); } results.addObject(new NSDictionary(pk, attrName)); } } } finally { channel.cancelFetch(); } succeeded = true; } catch (JDBCAdaptorException ex) { throw ex; } } } if (results.count() != count) { throw new IllegalStateException( "Unable to generate primary keys from the sequence for " + entity + "."); } return results; }
// [PJYF Oct 19 2004] // We need to prepend the question mark with a type attribute to get the jdbc driver to do the // right thing. // only for B (Binary) public NSMutableDictionary bindVariableDictionaryForAttribute( EOAttribute attribute, Object value) { String prepend = ""; String externalType = (attribute.externalType() != null ? attribute.externalType().toLowerCase() : ""); if ("binary".equals(externalType)) { prepend = "B"; } return new NSMutableDictionary( new Object[] {attribute.name(), prepend + "?", attribute, value}, new Object[] { BindVariableNameKey, BindVariablePlaceHolderKey, BindVariableAttributeKey, BindVariableValueKey }); }
// [PJYF Nov 5 2004] // This is major Hack to generate a diffenrent bind dictionary for update and insert form the // one for select and delete public void addInsertListAttribute(EOAttribute attribute, Object value) { this.appendItemToListString(this.sqlStringForAttribute(attribute), this._listString()); String attributeValue = this.sqlStringForForInsertOrUpdateValue(value, attribute.name()); attributeValue = this.formatSQLString(attributeValue, attribute.writeFormat()); this.appendItemToListString(attributeValue, _valueList()); }
private AttributeRetriever(EOAttribute attribute) { this.propertyName = attribute.name(); this.attribute = attribute; }
@Override @SuppressWarnings("unchecked") public String sqlStringForSQLExpression(EOQualifier eoqualifier, EOSQLExpression e) { ERXToManyQualifier qualifier = (ERXToManyQualifier) eoqualifier; StringBuilder result = new StringBuilder(); EOEntity targetEntity = e.entity(); NSArray<String> toManyKeys = NSArray.componentsSeparatedByString(qualifier.key(), "."); EORelationship targetRelationship = null; for (int i = 0; i < toManyKeys.count() - 1; i++) { targetRelationship = targetEntity.anyRelationshipNamed(toManyKeys.objectAtIndex(i)); targetEntity = targetRelationship.destinationEntity(); } targetRelationship = targetEntity.relationshipNamed(toManyKeys.lastObject()); targetEntity = targetRelationship.destinationEntity(); if (targetRelationship.joins() == null || targetRelationship.joins().isEmpty()) { // we have a flattened many to many String definitionKeyPath = targetRelationship.definition(); NSArray<String> definitionKeys = NSArray.componentsSeparatedByString(definitionKeyPath, "."); EOEntity lastStopEntity = targetRelationship.entity(); EORelationship firstHopRelationship = lastStopEntity.relationshipNamed(definitionKeys.objectAtIndex(0)); EOEntity endOfFirstHopEntity = firstHopRelationship.destinationEntity(); EOJoin join = firstHopRelationship.joins().objectAtIndex(0); // assumes 1 join EOAttribute sourceAttribute = join.sourceAttribute(); EOAttribute targetAttribute = join.destinationAttribute(); EORelationship secondHopRelationship = endOfFirstHopEntity.relationshipNamed(definitionKeys.objectAtIndex(1)); join = secondHopRelationship.joins().objectAtIndex(0); // assumes 1 join EOAttribute secondHopSourceAttribute = join.sourceAttribute(); NSMutableArray<String> lastStopPKeyPath = toManyKeys.mutableClone(); lastStopPKeyPath.removeLastObject(); lastStopPKeyPath.addObject(firstHopRelationship.name()); lastStopPKeyPath.addObject(targetAttribute.name()); String firstHopRelationshipKeyPath = lastStopPKeyPath.componentsJoinedByString("."); result.append(e.sqlStringForAttributeNamed(firstHopRelationshipKeyPath)); result.append(" IN ( SELECT "); result.append(lastStopEntity.externalName()); result.append('.'); result.append(lastStopEntity.primaryKeyAttributes().objectAtIndex(0).columnName()); result.append(" FROM "); result.append(lastStopEntity.externalName()); result.append(','); lastStopPKeyPath.removeLastObject(); String tableAliasForJoinTable = (String) e.aliasesByRelationshipPath() .objectForKey( lastStopPKeyPath.componentsJoinedByString(".")); // "j"; //+random# result.append(endOfFirstHopEntity.externalName()); result.append(' '); result.append(tableAliasForJoinTable); result.append(" WHERE "); appendColumnForAttributeToStringBuilder(sourceAttribute, result); result.append('='); result.append(e.sqlStringForAttributeNamed(firstHopRelationshipKeyPath)); if (qualifier.elements() != null) { NSArray pKeys = ERXEOAccessUtilities.primaryKeysForObjects(qualifier.elements()); result.append(" AND "); result.append(tableAliasForJoinTable); result.append('.'); result.append(secondHopSourceAttribute.columnName()); result.append(" IN ("); EOAttribute pk = targetEntity.primaryKeyAttributes().lastObject(); for (int i = 0; i < pKeys.count(); i++) { Object key = pKeys.objectAtIndex(i); String keyString = e.formatValueForAttribute(key, pk); // AK: default is is broken if ("NULL".equals(keyString)) { keyString = "" + key; } result.append(keyString); if (i < pKeys.count() - 1) { result.append(','); } } result.append(") "); } result.append(" GROUP BY "); appendColumnForAttributeToStringBuilder(sourceAttribute, result); result.append(" HAVING COUNT(*)"); if (qualifier.minCount() <= 0) { result.append("=" + qualifier.elements().count()); } else { result.append(">=" + qualifier.minCount()); } result.append(" )"); } else { throw new RuntimeException("not implemented!!"); } return result.toString(); }