public int compare(DbAttribute a1, DbAttribute a2) { if (a1.isGenerated() != a2.isGenerated()) { return a1.isGenerated() ? -1 : 1; } else { return a1.getName().compareTo(a2.getName()); } } }
public int compare(Object o1, Object o2) { DbAttribute a1 = (DbAttribute) o1; DbAttribute a2 = (DbAttribute) o2; if (a1.isGenerated() != a2.isGenerated()) { return a1.isGenerated() ? -1 : 1; } else { return a1.getName().compareTo(a2.getName()); } } }
@Override public void createTableAppendColumn(StringBuffer sqlBuffer, DbAttribute column) { super.createTableAppendColumn(sqlBuffer, column); if (column.isGenerated()) { sqlBuffer.append(" AUTO_INCREMENT"); } }
private DbAttribute findGeneratedAttribute() { for (DbAttribute attribute : dbEntity.getPrimaryKeys()) { if(attribute.isGenerated()) { return attribute; } } return null; }
private void checkIsGenerated(DbAttribute original, DbAttribute imported, List<MergerToken> tokens) { if(original.isGenerated() == imported.isGenerated()) { return; } tokens.add(getTokenFactory().createSetGeneratedFlagToDb(original.getEntity(), original, original.isGenerated())); }
/** * Overrides super implementation to correctly set up identity columns. * * @since 1.2 */ @Override public void createTableAppendColumn(StringBuffer sqlBuffer, DbAttribute column) { super.createTableAppendColumn(sqlBuffer, column); if (column.isGenerated()) { // current limitation - we don't allow to set identity parameters... sqlBuffer.append(" IDENTITY (1, 1)"); } }
/** * Appends AUTO_INCREMENT clause to the column definition for generated columns. */ protected void createTableAppendColumn(StringBuffer sqlBuffer, DbAttribute column) { super.createTableAppendColumn(sqlBuffer, column); if (column.isGenerated()) { sqlBuffer.append(" AUTO_INCREMENT"); } }
/** * Overrides super implementation to correctly set up identity columns. * * @since 1.2 */ protected void createTableAppendColumn(StringBuffer sqlBuffer, DbAttribute column) { super.createTableAppendColumn(sqlBuffer, column); if(column.isGenerated()) { // current limitation - we don't allow to set identity parameters... sqlBuffer.append(" IDENTITY (1, 1)"); } } }
@Override protected void createTableAppendPKClause(StringBuffer sqlBuffer, DbEntity entity) { // do not append " PRIMARY KEY () " for single column generated primary key if (entity.getPrimaryKeys().size() == 1) { DbAttribute column = entity.getPrimaryKeys().iterator().next(); if (column.isGenerated()) { return; } } super.createTableAppendPKClause(sqlBuffer, entity); } }
/** * Returns true if an attribute should be included in the batch. * * @since 1.2 */ protected boolean includeInBatch(DbAttribute attribute) { // attribute inclusion rule - one of the rules below must be true: // (1) attribute not generated // (2) attribute is generated and PK and adapter does not support // generated // keys return !attribute.isGenerated() || (attribute.isPrimaryKey() && !adapter.supportsGeneratedKeys()); } }
/** * Returns a collection of DbAttributes that should be generated by Cayenne. */ // TODO, andrus 4/12/2006 - move to DbEntity in 2.0+ Collection getAutogeneratedKeys(DataNode node, DbEntity entity) { boolean supportsGeneratedKeys = node.getAdapter().supportsGeneratedKeys(); Iterator it = entity.getPrimaryKey().iterator(); Collection generated = new ArrayList(1); while (it.hasNext()) { DbAttribute next = (DbAttribute) it.next(); if (supportsGeneratedKeys && next.isGenerated()) { continue; } if (isPropagated(next)) { continue; } generated.add(next); } return generated; }
/** * Returns true if an attribute should be included in the batch. * * @since 1.2 */ protected boolean includeInBatch(DbAttribute attribute) { // attribute inclusion rule - one of the rules below must be true: // (1) attribute not generated // (2) attribute is generated and PK and adapter does not support generated // keys return !attribute.isGenerated() || (attribute.isPrimaryKey() && !adapter.supportsGeneratedKeys()); } }
/** * Appends AUTOINCREMENT clause to the column definition for generated columns. */ @Override public void createTableAppendColumn(StringBuffer sqlBuffer, DbAttribute column) { super.createTableAppendColumn(sqlBuffer, column); DbEntity entity = column.getEntity(); if (column.isGenerated() && column.isPrimaryKey() && entity.getPrimaryKeys().size() == 1) { sqlBuffer.append(" PRIMARY KEY AUTOINCREMENT"); } }
public void itemStateChanged(ItemEvent e) { Object item = e.getItem(); if (item instanceof DbAttribute) { boolean generated = e.getStateChange() == ItemEvent.SELECTED; DbAttribute a = (DbAttribute) item; if (a.isGenerated() != generated) { a.setGenerated(generated); mediator.fireDbEntityEvent(new EntityEvent(this, entity)); } } } });
private boolean needAutoPkSupport() { DbEntity entity = getEntity(); if(entity.getPrimaryKeyGenerator() != null) { return false; } for(DbAttribute attribute : entity.getPrimaryKeys()) { if(attribute.isGenerated()) { return false; } } return true; }
protected void onInitInternal(DbEntity entity) { resetStrategy(entity, true, false); Collection pkAttributes = entity.getPrimaryKeys(); // by default check the only numeric PK if (pkAttributes.size() == 1) { DbAttribute pk = (DbAttribute) pkAttributes.iterator().next(); if (TypesMapping.isNumeric(pk.getType()) && !pk.isGenerated()) { pk.setGenerated(true); mediator.fireDbEntityEvent(new EntityEvent(this, entity)); } } updateView(entity); }
private void createAttribute(DbEntity ent, QuotingStrategy context, StringBuilder buf, DbAttribute at) { // attribute may not be fully valid, do a simple check if (at.getType() == TypesMapping.NOT_DEFINED) { throw new CayenneRuntimeException("Undefined type for attribute '%s.%s'" , ent.getFullyQualifiedName(), at.getName()); } String[] types = externalTypesForJdbcType(at.getType()); if (types == null || types.length == 0) { throw new CayenneRuntimeException("Undefined type for attribute '%s.%s': %s" , ent.getFullyQualifiedName(), at.getName(), at.getType()); } // Checking that attribute is generated and we have alternative types in types.xml. // If so, use those autoincremented types. For example serial, bigserial, smallserial. String type = (at.isGenerated() && types.length > 1) ? types[1] : types[0]; buf.append(context.quotedName(at)).append(' ').append(type).append(sizeAndPrecision(this, at)) .append(at.isMandatory() ? " NOT" : "").append(" NULL"); }
@Override public void createTableAppendColumn(StringBuffer sqlBuffer, DbAttribute column) { // CAY-1095: if the column is type double, temporarily set the max // length to 0 to // avoid adding precision information. if (column.getType() == Types.DOUBLE && column.getMaxLength() > 0) { int len = column.getMaxLength(); column.setMaxLength(0); super.createTableAppendColumn(sqlBuffer, column); column.setMaxLength(len); } else { super.createTableAppendColumn(sqlBuffer, column); } if(column.isGenerated()) { sqlBuffer.append(" GENERATED BY DEFAULT AS IDENTITY (START WITH 1)"); } } }
private void checkOnGeneratedStrategyConflict(DbRelationship relationship, ValidationResult validationResult) { if (relationship.isToDependentPK()) { Collection<DbAttribute> attributes = relationship.getTargetEntity().getGeneratedAttributes(); for (DbAttribute attribute : attributes) { if (attribute.isGenerated()) { addFailure( validationResult, relationship, "'To Dep Pk' incompatible with Database-Generated on '%s' relationship", toString(relationship)); } } } }
@Override public boolean willWrite(DataObject target, Object value) { ObjectId id = target.getObjectId(); if (Util.nullSafeEquals(id.getIdSnapshot().get(pk.getName()), value)) { return false; } if (Util.nullSafeEquals(id.getReplacementIdMap().get(pk.getName()), value)) { return false; } if (pk.isGenerated()) { throw new LmRuntimeException("'" + pk.getName() + "' is autogenerated and is not allowed to be synced from source."); } return true; } }