protected static boolean verifyField( AbstractSession session, DatabaseField field, ClassDescriptor descriptor) { boolean ok = true; if (field.equals(descriptor.getSequenceNumberField())) { ok = false; session .getIntegrityChecker() .handleError( DescriptorException.returningPolicyFieldNotSupported(field.getName(), descriptor)); } else if (descriptor.hasInheritance() && field.equals(descriptor.getInheritancePolicy().getClassIndicatorField())) { ok = false; session .getIntegrityChecker() .handleError( DescriptorException.returningPolicyFieldNotSupported(field.getName(), descriptor)); } else if (descriptor.usesOptimisticLocking()) { OptimisticLockingPolicy optimisticLockingPolicy = descriptor.getOptimisticLockingPolicy(); if (optimisticLockingPolicy instanceof VersionLockingPolicy) { VersionLockingPolicy versionLockingPolicy = (VersionLockingPolicy) optimisticLockingPolicy; if (field.equals(versionLockingPolicy.getWriteLockField())) { ok = false; session .getIntegrityChecker() .handleError( DescriptorException.returningPolicyFieldNotSupported( field.getName(), descriptor)); } } } return ok; }
protected Hashtable removeDuplicateAndValidateInfos(AbstractSession session) { Hashtable infoHashtable = new Hashtable(); for (int i = 0; i < infos.size(); i++) { Info info1 = infos.get(i); info1 = (Info) info1.clone(); DatabaseField descField = getDescriptor().buildField(info1.getField()); if (info1.getField().getType() == null) { info1.setField(descField); } else { // keep the original type if specified info1.getField().setName(descField.getName()); info1 .getField() .setTableName( getDescriptor().getDefaultTable().getQualifiedNameDelimited(session.getPlatform())); } Info info2 = (Info) infoHashtable.get(info1.getField()); if (info2 == null) { infoHashtable.put(info1.getField(), info1); } else { Info infoMerged = mergeInfos(info1, info2, session, getDescriptor()); if (infoMerged != null) { // substitute info2 with infoMerged infoHashtable.put(infoMerged.getField(), infoMerged); } else { // couldn't merge info1 and info2 due to a conflict. // substitute info2 with info1 infoHashtable.put(info1.getField(), info1); } } } return infoHashtable; }
/** Munge the table for all the fields in the row. */ protected AbstractRecord convertToFullyQualifiedRow(AbstractRecord row) { DatabaseRecord result = new DatabaseRecord(row.size()); for (Enumeration stream = row.keys(); stream.hasMoreElements(); ) { DatabaseField key = (DatabaseField) stream.nextElement(); result.put(new DatabaseField(key.getName(), this.getRootElement()), row.get(key)); } return result; }
protected static boolean verifyFieldAndMapping( AbstractSession session, DatabaseField field, ClassDescriptor descriptor, DatabaseMapping mapping) { verifyField(session, field, descriptor); while (mapping.isAggregateObjectMapping()) { ClassDescriptor referenceDescriptor = ((AggregateObjectMapping) mapping).getReferenceDescriptor(); mapping = referenceDescriptor.getObjectBuilder().getMappingForField(field); verifyFieldAndMapping(session, field, referenceDescriptor, mapping); } if (!mapping.isDirectToFieldMapping() && !mapping.isTransformationMapping()) { String mappingTypeName = Helper.getShortClassName(mapping); session .getIntegrityChecker() .handleError( DescriptorException.returningPolicyMappingNotSupported( field.getName(), mappingTypeName, mapping)); return false; } else { return true; } }
/** INTERNAL: */ public void validationAfterDescriptorInitialization(AbstractSession session) { Hashtable mapped = new Hashtable(); for (int operation = INSERT; operation <= UPDATE; operation++) { if ((main[operation][MAPPED] != null) && !main[operation][MAPPED].isEmpty()) { Iterator it = main[operation][MAPPED].iterator(); while (it.hasNext()) { DatabaseField field = (DatabaseField) it.next(); mapped.put(field, field); } } } if (!mapped.isEmpty()) { for (Enumeration fields = getDescriptor().getFields().elements(); fields.hasMoreElements(); ) { DatabaseField fieldInDescriptor = (DatabaseField) fields.nextElement(); DatabaseField fieldInMain = (DatabaseField) mapped.get(fieldInDescriptor); if (fieldInMain != null) { if (fieldInMain.getType() == null) { if (getDescriptor().isReturnTypeRequiredForReturningPolicy()) { session .getIntegrityChecker() .handleError( DescriptorException.returningPolicyMappedFieldTypeNotSet( fieldInMain.getName(), getDescriptor())); } } else if (isThereATypeConflict(fieldInMain, fieldInDescriptor)) { session .getIntegrityChecker() .handleError( DescriptorException.returningPolicyAndDescriptorFieldTypeConflict( fieldInMain.getName(), fieldInMain.getType().getName(), fieldInDescriptor.getType().getName(), getDescriptor())); } } } } if (!(session.getDatasourcePlatform() instanceof DatabasePlatform)) { // don't attempt further diagnostics on non-relational platforms return; } WriteObjectQuery[] query = { getDescriptor().getQueryManager().getInsertQuery(), getDescriptor().getQueryManager().getUpdateQuery() }; String[] queryTypeName = {"InsertObjectQuery", "UpdateObjectQuery"}; for (int operation = INSERT; operation <= UPDATE; operation++) { if ((main[operation][ALL] != null) && !main[operation][ALL].isEmpty()) { // this operation requires some fields to be returned if ((query[operation] == null) || (query[operation].getDatasourceCall() == null)) { if (!session.getPlatform().canBuildCallWithReturning()) { session .getIntegrityChecker() .handleError( DescriptorException.noCustomQueryForReturningPolicy( queryTypeName[operation], Helper.getShortClassName(session.getPlatform()), getDescriptor())); } } else if (query[operation].getDatasourceCall() instanceof StoredProcedureCall) { // SQLCall with custom SQL calculates its outputRowFields later (in prepare() method) - // that's why SQLCall can't be verified here. DatabaseCall customCall = (DatabaseCall) query[operation].getDatasourceCall(); Enumeration outputRowFields = customCall.getOutputRowFields().elements(); Collection notFoundInOutputRow = createCollection(); notFoundInOutputRow.addAll(main[operation][ALL]); while (outputRowFields.hasMoreElements()) { notFoundInOutputRow.remove(outputRowFields.nextElement()); } if (!notFoundInOutputRow.isEmpty()) { Iterator it = notFoundInOutputRow.iterator(); while (it.hasNext()) { DatabaseField field = (DatabaseField) it.next(); session .getIntegrityChecker() .handleError( DescriptorException.customQueryAndReturningPolicyFieldConflict( field.getName(), queryTypeName[operation], getDescriptor())); } } } } } }
/** INTERNAL: */ public void initialize(AbstractSession session) { clearInitialization(); main = new Collection[NUM_OPERATIONS][MAIN_SIZE]; // The order of descriptor initialization guarantees initialization of Parent before children. // main array is copied from Parent's ReturningPolicy if (getDescriptor().isChildDescriptor()) { ClassDescriptor parentDescriptor = getDescriptor().getInheritancePolicy().getParentDescriptor(); if (parentDescriptor.hasReturningPolicy()) { copyMainFrom(parentDescriptor.getReturningPolicy()); } } if (!infos.isEmpty()) { Hashtable infoHashtable = removeDuplicateAndValidateInfos(session); Hashtable infoHashtableUnmapped = (Hashtable) infoHashtable.clone(); for (Enumeration fields = getDescriptor().getFields().elements(); fields.hasMoreElements(); ) { DatabaseField field = (DatabaseField) fields.nextElement(); Info info = (Info) infoHashtableUnmapped.get(field); if (info != null) { infoHashtableUnmapped.remove(field); if (verifyFieldAndMapping(session, field)) { if (info.getField().getType() == null) { addMappedFieldToMain(field, info); } else { addMappedFieldToMain(info.getField(), info); fieldIsNotFromDescriptor(info.getField()); } } } } if (!infoHashtableUnmapped.isEmpty()) { Enumeration fields = infoHashtableUnmapped.keys(); while (fields.hasMoreElements()) { DatabaseField field = (DatabaseField) fields.nextElement(); Info info = (Info) infoHashtableUnmapped.get(field); if (verifyField(session, field, getDescriptor())) { if (field.getType() != null) { addUnmappedFieldToMain(field, info); fieldIsNotFromDescriptor(field); session.log( SessionLog.FINEST, SessionLog.QUERY, "added_unmapped_field_to_returning_policy", info.toString(), getDescriptor().getJavaClassName()); } else { if (getDescriptor().isReturnTypeRequiredForReturningPolicy()) { session .getIntegrityChecker() .handleError( DescriptorException.returningPolicyUnmappedFieldTypeNotSet( field.getName(), getDescriptor())); } } } } } } initializeIsUsedToSetPrimaryKey(); }
public void createDDL() { DatabaseConvertGUI.setReadSuccess(true); databaseName = generateDatabaseName(); sb.append("CREATE DATABASE " + databaseName + ";\r\n"); sb.append("USE " + databaseName + ";\r\n"); for (int boundCount = 0; boundCount <= maxBound; boundCount++) { // process tables in order from least dependent (least number of bound // tables) to most dependent for (int tableCount = 0; tableCount < numBoundTables.length; tableCount++) { // step through list of tables if (numBoundTables[tableCount] == boundCount) { // sb.append("CREATE TABLE " + tables[tableCount].getName() + " (\r\n"); int[] nativeFields = tables[tableCount].getNativeFieldsArray(); int[] relatedFields = tables[tableCount].getRelatedFieldsArray(); boolean[] primaryKey = new boolean[nativeFields.length]; int numPrimaryKey = 0; int numForeignKey = 0; for (int nativeFieldCount = 0; nativeFieldCount < nativeFields.length; nativeFieldCount++) { // print out the fields DatabaseField currentField = getField(nativeFields[nativeFieldCount]); sb.append( "\t" + currentField.getName() + " " + strDataType[currentField.getDataType()]); if (currentField.getDataType() == 0) { // varchar sb.append( "(" + currentField.getVarcharValue() + ")"); // append varchar length in () if data type is varchar } if (currentField.getDisallowNull()) { sb.append(" NOT NULL"); } if (!currentField.getDefaultValue().equals("")) { if (currentField.getDataType() == 1) { // boolean data type sb.append(" DEFAULT " + convertStrBooleanToInt(currentField.getDefaultValue())); } else { // any other data type sb.append(" DEFAULT " + currentField.getDefaultValue()); } } if (currentField.getIsPrimaryKey()) { primaryKey[nativeFieldCount] = true; numPrimaryKey++; } else { primaryKey[nativeFieldCount] = false; } if (currentField.getFieldBound() != 0) { numForeignKey++; } sb.append(",\r\n"); // end of field } if (numPrimaryKey > 0) { // table has primary key(s) sb.append("CONSTRAINT " + tables[tableCount].getName() + "_PK PRIMARY KEY ("); for (int i = 0; i < primaryKey.length; i++) { if (primaryKey[i]) { sb.append(getField(nativeFields[i]).getName()); numPrimaryKey--; if (numPrimaryKey > 0) { sb.append(", "); } } } sb.append(")"); if (numForeignKey > 0) { sb.append(","); } sb.append("\r\n"); } if (numForeignKey > 0) { // table has foreign keys int currentFK = 1; for (int i = 0; i < relatedFields.length; i++) { if (relatedFields[i] != 0) { sb.append( "CONSTRAINT " + tables[tableCount].getName() + "_FK" + currentFK + " FOREIGN KEY(" + getField(nativeFields[i]).getName() + ") REFERENCES " + getTable(getField(nativeFields[i]).getTableBound()).getName() + "(" + getField(relatedFields[i]).getName() + ")"); if (currentFK < numForeignKey) { sb.append(",\r\n"); } currentFK++; } } sb.append("\r\n"); } sb.append(");\r\n\r\n"); // end of table } } } }