@Override public boolean isNameInUse(String name) { DbEntity entity = (DbEntity) parent; // check if either attribute or relationship name matches... return entity.getAttribute(name) != null || entity.getRelationship(name) != null; } });
private String getDBAttrType(String dbAttributeName) { DbEntity currentEnt = ((ObjEntity) attr.getEntity()).getDbEntity(); if (currentEnt != null && currentEnt.getAttributes() != null && dbAttributeName != null) { DbAttribute dbAttr = (DbAttribute) currentEnt.getAttribute(dbAttributeName); if (dbAttr != null) { return TypesMapping.getSqlNameByType(dbAttr.getType()); } } return null; }
@Override void processResultSet(DbEntity dbEntity, DbLoadDataStore map, ResultSet rs) throws SQLException { String columnName = rs.getString("COLUMN_NAME"); DbAttribute attribute = dbEntity.getAttribute(columnName); if (attribute == null) { // why an attribute might be null is not quiet clear // but there is a bug report 731406 indicating that it is // possible so just print the warning, and ignore LOGGER.warn("Can't locate attribute for primary key: " + columnName); return; } attribute.setPrimaryKey(true); ((DetectedDbEntity) dbEntity).setPrimaryKeyName(rs.getString("PK_NAME")); } }
private void appendSimpleProperties(Map dbDiff) { // populate changed columns if (currentPropertyDiff != null) { Iterator it = currentPropertyDiff.entrySet().iterator(); while (it.hasNext()) { Map.Entry entry = (Map.Entry) it.next(); ObjAttribute attribute = (ObjAttribute) objEntity.getAttribute(entry .getKey() .toString()); // this takes care of the flattened attributes, as 'getDbAttributeName' // returns the last path component... Attribute dbAttribute = dbEntity.getAttribute(attribute .getDbAttributeName()); dbDiff.put(dbAttribute.getName(), entry.getValue()); } } }
public DbAttribute getTarget() { if (targetName == null) { return null; } DbRelationship r = getNonNullRelationship(); DbEntity entity = r.getTargetEntity(); if (entity == null) { return null; } return entity.getAttribute(targetName); }
/** * Returns DbAttribute on on the left side of the join. */ public DbAttribute getSource() { if (sourceName == null) { return null; } DbRelationship r = getNonNullRelationship(); DbEntity entity = r.getSourceEntity(); if (entity == null) { return null; } return entity.getAttribute(sourceName); }
private void processLastPathComponent() { DbAttribute attribute = currentEntity.getAttribute(lastPathComponent); if (attribute != null) { processTerminatingAttribute(attribute); return; } DbRelationship relationship = currentEntity.getRelationship(lastPathComponent); if (relationship != null) { processTerminatingRelationship(relationship); return; } throw new IllegalStateException("Invalid path component: " + lastPathComponent); }
private void processStartDbAttributeRef(Attributes atts) throws SAXException { String name = atts.getValue("", "name"); if ((attrib instanceof DerivedDbAttribute) && (dbEntity instanceof DerivedDbEntity)) { DbEntity parent = ((DerivedDbEntity) dbEntity).getParentEntity(); DbAttribute ref = (DbAttribute) parent.getAttribute(name); ((DerivedDbAttribute) attrib).addParam(ref); } else { throw new SAXException( "Referenced attributes are not supported by regular DbAttributes. " + " Offending attribute name '" + attrib.getName() + "'."); } }
public void setUpdatedValueAt(Object aValue, int row, int column) { DbJoin join = getJoin(row); if (join == null) { return; } String value = (String) aValue; if (column == SOURCE) { if (source == null || source.getAttribute(value) == null) { value = null; } join.setSourceName(value); } else if (column == TARGET) { if (target == null || target.getAttribute(value) == null) { value = null; } join.setTargetName(value); } fireTableRowsUpdated(row, row); }
<T> List<ColumnDescriptor> appendIdColumns(final List<ColumnDescriptor> columns, ObjEntity objEntity) { Set<ColumnTracker> skipSet = new HashSet<>(); DbEntity dbEntity = objEntity.getDbEntity(); for (ObjAttribute attribute : objEntity.getPrimaryKeys()) { // synthetic objattributes can't reliably lookup their DbAttribute, // so do it manually.. DbAttribute dbAttribute = dbEntity.getAttribute(attribute.getDbAttributeName()); appendColumn(columns, attribute, dbAttribute, skipSet, null); } return columns; }
private void createAndAppendJoins(Set<ExportedKey> exportedKeys, DbEntity pkEntity, DbEntity fkEntity, DbRelationship forwardRelationship, DbRelationship reverseRelationship) { for (ExportedKey exportedKey : exportedKeys) { // Create and append joins String pkName = exportedKey.getPk().getColumn(); String fkName = exportedKey.getFk().getColumn(); // skip invalid joins... DbAttribute pkAtt = pkEntity.getAttribute(pkName); if (pkAtt == null) { LOGGER.info("no attribute for declared primary key: " + pkName); continue; } DbAttribute fkAtt = fkEntity.getAttribute(fkName); if (fkAtt == null) { LOGGER.info("no attribute for declared foreign key: " + fkName); continue; } addJoin(forwardRelationship, pkName, fkName); addJoin(reverseRelationship, fkName, pkName); } }
/** * Appends custom columns from SelectQuery to the provided list. */ List appendCustomColumns(List columns, SelectQuery query) { List customAttributes = query.getCustomDbAttributes(); DbEntity table = getRootDbEntity(); int len = customAttributes.size(); for (int i = 0; i < len; i++) { DbAttribute attribute = (DbAttribute) table .getAttribute((String) customAttributes.get(i)); if (attribute == null) { throw new CayenneRuntimeException("Attribute does not exist: " + customAttributes.get(i)); } String alias = aliasForTable((DbEntity) attribute.getEntity()); columns.add(new ColumnDescriptor(attribute, alias)); } return columns; }
@SuppressWarnings("unchecked") public Iterator<CayenneMapEntry> getDbPathIterator(ObjEntity entity) { if (dbAttributePath == null) { return IteratorUtils.EMPTY_ITERATOR; } if (entity == null) { return IteratorUtils.EMPTY_ITERATOR; } DbEntity dbEnt = entity.getDbEntity(); if (dbEnt == null) { return IteratorUtils.EMPTY_ITERATOR; } int lastPartStart = dbAttributePath.lastIndexOf('.'); if (lastPartStart < 0) { DbAttribute attribute = dbEnt.getAttribute(dbAttributePath); if (attribute == null) { return IteratorUtils.EMPTY_ITERATOR; } return IteratorUtils.singletonIterator(attribute); } return dbEnt.resolvePathComponents(dbAttributePath); }
@Override protected BatchTranslator deleteTranslator(DeleteBatchQuery query, DbAdapter adapter, String trimFunction) { DbAttribute attr = query.getDbEntity().getAttribute(deletedFieldName); boolean needsSoftDelete = attr != null && attr.getType() == Types.BOOLEAN; return needsSoftDelete ? new SoftDeleteBatchTranslator(query, adapter, trimFunction, deletedFieldName) : super .deleteTranslator(query, adapter, trimFunction); }
public Iterator getDbPathIterator() { if (dbAttributePath == null) { return IteratorUtils.EMPTY_ITERATOR; } ObjEntity ent = (ObjEntity) getEntity(); if (ent == null) { return IteratorUtils.EMPTY_ITERATOR; } DbEntity dbEnt = ent.getDbEntity(); if (dbEnt == null) { return IteratorUtils.EMPTY_ITERATOR; } int lastPartStart = dbAttributePath.lastIndexOf('.'); if (lastPartStart < 0) { Attribute attribute = dbEnt.getAttribute(dbAttributePath); if (attribute == null) { return IteratorUtils.EMPTY_ITERATOR; } return IteratorUtils.singletonIterator(attribute); } return dbEnt.resolvePathComponents(dbAttributePath); }
private void addToDbEntity(DbEntity entity, DbAttribute attribute) { attribute.setEntity(entity); // override existing attributes if it comes again if (entity.getAttribute(attribute.getName()) != null) { entity.removeAttribute(attribute.getName()); } entity.addAttribute(attribute); }
@Override protected DbAttributeBinding[] createBindings() { DbAttributeBinding[] superBindings = super.createBindings(); int slen = superBindings.length; DbAttributeBinding[] bindings = new DbAttributeBinding[slen + 1]; DbAttribute deleteAttribute = query.getDbEntity().getAttribute(deletedFieldName); String typeName = TypesMapping.getJavaBySqlType(deleteAttribute.getType()); ExtendedType extendedType = adapter.getExtendedTypes().getRegisteredType(typeName); bindings[0] = new DbAttributeBinding(deleteAttribute); bindings[0].include(1, true, extendedType); System.arraycopy(superBindings, 0, bindings, 1, slen); return bindings; }
/** * Creates an index of PK columns in the RowDescriptor. */ protected void indexPK() { if (rootEntity == null) { throw new CayenneRuntimeException("Null root DbEntity, can't index PK"); } int len = rootEntity.getPrimaryKey().size(); // sanity check if (len == 0) { throw new CayenneRuntimeException("Root DbEntity has no PK defined: " + rootEntity); } int[] pk = new int[len]; ColumnDescriptor[] columns = rowDescriptor.getColumns(); for (int i = 0, j = 0; i < columns.length; i++) { DbAttribute a = (DbAttribute) rootEntity.getAttribute(columns[i].getName()); if (a != null && a.isPrimaryKey()) { pk[j++] = i; } } this.pkIndices = pk; }
@Override public void setUpdatedValueAt(Object value, int row, int col) { EmbeddableAttribute attribute = getEmbeddableAttribute(row); if (col == DB_ATTRIBUTE) { attribute.setDbAttributeName(value != null ? value.toString() : null); fireTableCellUpdated(row, col); this.isAttributeOverrideChange = true; ((ObjAttributeInfoDialogView) ((ObjAttributeInfoDialog) eventSource) .getView()).getSaveButton().setEnabled(true); if (value != null) { DbEntity currentEnt = ((ObjEntity) attr.getEntity()).getDbEntity(); if (currentEnt != null) { DbAttribute dbAttr = (DbAttribute) currentEnt.getAttribute(value .toString()); if (dbAttr != null) { fireTableCellUpdated(DB_ATTRIBUTE_TYPE, col); } } } fireTableRowsUpdated(row, row); } }
@Override public boolean visitIdentifier(EJBQLExpression expression) { Map<String, String> fields = null; if (context.isAppendingResultColumns()) { fields = context.nextEntityResult().getFields(); } String idVar = expression.getText(); ClassDescriptor descriptor = context.getEntityDescriptor(idVar); ObjEntity oe = descriptor.getEntity(); for (ObjAttribute oa : oe.getPrimaryKeys()) { DbAttribute t = oe.getDbEntity().getAttribute(oa.getDbAttributeName()); appendColumn(idVar, oa, t, fields, oa.getType()); } return false; }