use of org.datanucleus.store.rdbms.table.Column in project datanucleus-rdbms by datanucleus.
the class ResultMetaDataROF method getObject.
/**
* Accessor for the object(s) from the current row of the ResultSet.
* @return The object(s) for this row of the ResultSet.
*/
public Object getObject() {
List returnObjects = new ArrayList();
// A). Process persistent types
PersistentTypeMapping[] persistentTypes = queryResultMetaData.getPersistentTypeMappings();
if (persistentTypes != null) {
if (persistentTypeResultSetGetters == null) {
persistentTypeResultSetGetters = new ResultSetGetter[persistentTypes.length];
}
int startColumnIndex = 0;
for (int i = 0; i < persistentTypes.length; i++) {
Set<String> columnsInThisType = new HashSet<>();
AbstractMemberMetaData[] mmds = new AbstractMemberMetaData[columnNames.length];
Map<String, AbstractMemberMetaData> fieldColumns = new HashMap<>();
DatastoreClass dc = ((RDBMSStoreManager) ec.getStoreManager()).getDatastoreClass(persistentTypes[i].getClassName(), ec.getClassLoaderResolver());
AbstractClassMetaData acmd = ec.getMetaDataManager().getMetaDataForClass(persistentTypes[i].getClassName(), ec.getClassLoaderResolver());
Object id = null;
// and two columns with similar names e.g "Col1" and "col1". Until that situation comes up we ignore it :-)
for (int j = startColumnIndex; j < columnNames.length; j++) {
if (columnsInThisType.contains(columnNames[j])) {
// already added this column, so must be another persistent type
startColumnIndex = j;
break;
}
boolean found = false;
if (acmd.getIdentityType() == IdentityType.DATASTORE) {
JavaTypeMapping datastoreIdMapping = dc.getSurrogateMapping(SurrogateColumnType.DATASTORE_ID, false);
Column df = datastoreIdMapping.getDatastoreMapping(0).getColumn();
if (df.getIdentifier().getName().equalsIgnoreCase(columnNames[j])) {
// add +1 because result sets in jdbc starts with 1
int datastoreIdentityExpressionIndex = j + 1;
id = datastoreIdMapping.getObject(ec, rs, new int[] { datastoreIdentityExpressionIndex });
found = true;
}
}
for (int k = 0; k < acmd.getNoOfManagedMembers() + acmd.getNoOfInheritedManagedMembers() && !found; k++) {
AbstractMemberMetaData apmd = acmd.getMetaDataForManagedMemberAtAbsolutePosition(k);
if (persistentTypes[i].getColumnForField(apmd.getName()) != null) {
if (persistentTypes[i].getColumnForField(apmd.getName()).equalsIgnoreCase(columnNames[j])) {
fieldColumns.put(columnNames[j], apmd);
columnsInThisType.add(columnNames[j]);
mmds[j] = apmd;
found = true;
}
} else {
JavaTypeMapping mapping = dc.getMemberMapping(apmd);
for (int l = 0; l < mapping.getDatastoreMappings().length && !found; l++) {
Column df = mapping.getDatastoreMapping(l).getColumn();
if (df.getIdentifier().getName().equalsIgnoreCase(columnNames[j])) {
fieldColumns.put(columnNames[j], apmd);
columnsInThisType.add(columnNames[j]);
mmds[j] = apmd;
found = true;
}
}
}
}
if (!columnsInThisType.contains(columnNames[j])) {
// column not found in this type, so must be another persistent type
startColumnIndex = j;
break;
}
}
// Build fields and mappings in the results
StatementMappingIndex[] stmtMappings = new StatementMappingIndex[acmd.getNoOfManagedMembers() + acmd.getNoOfInheritedManagedMembers()];
Set<AbstractMemberMetaData> resultMmds = new HashSet<>();
resultMmds.addAll(fieldColumns.values());
int[] resultFieldNumbers = new int[resultMmds.size()];
int j = 0;
for (AbstractMemberMetaData apmd : resultMmds) {
StatementMappingIndex stmtMapping = new StatementMappingIndex(dc.getMemberMapping(apmd));
resultFieldNumbers[j] = apmd.getAbsoluteFieldNumber();
List indexes = new ArrayList();
for (int k = 0; k < mmds.length; k++) {
if (mmds[k] == apmd) {
indexes.add(Integer.valueOf(k));
}
}
int[] indxs = new int[indexes.size()];
for (int k = 0; k < indxs.length; k++) {
// add +1 because result sets in JDBC starts with 1
indxs[k] = ((Integer) indexes.get(k)).intValue() + 1;
}
stmtMapping.setColumnPositions(indxs);
stmtMappings[resultFieldNumbers[j]] = stmtMapping;
j++;
}
Object obj = null;
Class type = ec.getClassLoaderResolver().classForName(persistentTypes[i].getClassName());
if (acmd.getIdentityType() == IdentityType.APPLICATION) {
if (persistentTypeResultSetGetters[i] == null) {
final StatementClassMapping resultMappings = new StatementClassMapping();
for (int k = 0; k < resultFieldNumbers.length; k++) {
resultMappings.addMappingForMember(resultFieldNumbers[k], stmtMappings[resultFieldNumbers[k]]);
}
persistentTypeResultSetGetters[i] = new ResultSetGetter(ec, rs, resultMappings, acmd);
}
ResultSetGetter rsGetter = persistentTypeResultSetGetters[i];
// TODO Make use of discriminator like in PersistentClassROF and set the pcClass in this?
id = IdentityUtils.getApplicationIdentityForResultSetRow(ec, acmd, type, false, rsGetter);
obj = ec.findObject(id, new FieldValues() {
public void fetchFields(ObjectProvider op) {
rsGetter.setObjectProvider(op);
op.replaceFields(resultFieldNumbers, rsGetter, false);
}
public void fetchNonLoadedFields(ObjectProvider op) {
rsGetter.setObjectProvider(op);
op.replaceNonLoadedFields(resultFieldNumbers, rsGetter);
}
public FetchPlan getFetchPlanForLoading() {
return null;
}
}, type, ignoreCache, false);
} else if (acmd.getIdentityType() == IdentityType.DATASTORE) {
if (persistentTypeResultSetGetters[i] == null) {
final StatementClassMapping resultMappings = new StatementClassMapping();
for (int k = 0; k < resultFieldNumbers.length; k++) {
resultMappings.addMappingForMember(resultFieldNumbers[k], stmtMappings[resultFieldNumbers[k]]);
}
persistentTypeResultSetGetters[i] = new ResultSetGetter(ec, rs, resultMappings, acmd);
}
ResultSetGetter rsGetter = persistentTypeResultSetGetters[i];
obj = ec.findObject(id, new FieldValues() {
public void fetchFields(ObjectProvider op) {
rsGetter.setObjectProvider(op);
op.replaceFields(resultFieldNumbers, rsGetter, false);
}
public void fetchNonLoadedFields(ObjectProvider op) {
rsGetter.setObjectProvider(op);
op.replaceNonLoadedFields(resultFieldNumbers, rsGetter);
}
public FetchPlan getFetchPlanForLoading() {
return null;
}
}, type, ignoreCache, false);
} else {
// TODO Handle non-durable
NucleusLogger.QUERY.warn("We do not currently support non-durable objects in the results of this type of query.");
}
returnObjects.add(obj);
}
}
// B). Process simple columns
String[] columns = queryResultMetaData.getScalarColumns();
if (columns != null) {
for (int i = 0; i < columns.length; i++) {
try {
Object obj = rs.getObject(columns[i]);
returnObjects.add(obj);
} catch (SQLException sqe) {
String msg = Localiser.msg("059027", sqe.getMessage());
NucleusLogger.QUERY.error(msg);
throw new NucleusUserException(msg, sqe);
}
}
}
// C). Process constructor type mappings
ConstructorTypeMapping[] ctrTypeMappings = queryResultMetaData.getConstructorTypeMappings();
if (ctrTypeMappings != null) {
for (int i = 0; i < ctrTypeMappings.length; i++) {
String ctrClassName = ctrTypeMappings[i].getClassName();
Class ctrCls = ec.getClassLoaderResolver().classForName(ctrClassName);
List<ConstructorTypeColumn> ctrColumns = ctrTypeMappings[i].getColumnsForConstructor();
Class[] ctrArgTypes = null;
Object[] ctrArgVals = null;
if (ctrColumns != null && ctrColumns.size() > 0) {
int j = 0;
ctrArgTypes = new Class[ctrColumns.size()];
ctrArgVals = new Object[ctrColumns.size()];
Iterator<ConstructorTypeColumn> colIter = ctrColumns.iterator();
while (colIter.hasNext()) {
ConstructorTypeColumn ctrCol = colIter.next();
try {
Object colVal = rs.getObject(ctrCol.getColumnName());
ctrArgTypes[j] = colVal.getClass();
if (ctrCol.getJavaType() != null) {
// Attempt to convert to the type requested
ctrArgTypes[j] = ctrCol.getJavaType();
ctrArgVals[j] = TypeConversionHelper.convertTo(colVal, ctrArgTypes[j]);
} else {
ctrArgTypes[j] = colVal.getClass();
ctrArgVals[j] = colVal;
}
} catch (SQLException sqle) {
// TODO Handle this
}
j++;
}
}
returnObjects.add(ClassUtils.newInstance(ctrCls, ctrArgTypes, ctrArgVals));
}
}
if (returnObjects.size() == 0) {
// No objects so user must have supplied incorrect MetaData
return null;
} else if (returnObjects.size() == 1) {
// Return Object
return returnObjects.get(0);
} else {
// Return Object[]
return returnObjects.toArray(new Object[returnObjects.size()]);
}
}
use of org.datanucleus.store.rdbms.table.Column in project datanucleus-rdbms by datanucleus.
the class PersistableMapping method prepareDatastoreMapping.
/**
* Method to prepare the PC mapping and add its associated datastore mappings.
* @param clr The ClassLoaderResolver
*/
protected void prepareDatastoreMapping(ClassLoaderResolver clr) {
if (roleForMember == FieldRole.ROLE_COLLECTION_ELEMENT) {
// TODO Handle creation of columns in join table for collection of PCs
} else if (roleForMember == FieldRole.ROLE_ARRAY_ELEMENT) {
// TODO Handle creation of columns in join table for array of PCs
} else if (roleForMember == FieldRole.ROLE_MAP_KEY) {
// TODO Handle creation of columns in join table for map of PCs as keys
} else if (roleForMember == FieldRole.ROLE_MAP_VALUE) {
// TODO Handle creation of columns in join table for map of PCs as values
} else {
// Either one end of a 1-1 relation, or the N end of a N-1
AbstractClassMetaData refCmd = storeMgr.getNucleusContext().getMetaDataManager().getMetaDataForClass(mmd.getType(), clr);
JavaTypeMapping referenceMapping = null;
if (refCmd == null) {
// User stupidity
throw new NucleusUserException("You have a field " + mmd.getFullFieldName() + " that has type " + mmd.getTypeName() + " but this type has no known metadata. Your mapping is incorrect");
}
if (refCmd.getInheritanceMetaData() != null && refCmd.getInheritanceMetaData().getStrategy() == InheritanceStrategy.SUBCLASS_TABLE) {
// Find the actual tables storing the other end (can be multiple subclasses)
AbstractClassMetaData[] cmds = storeMgr.getClassesManagingTableForClass(refCmd, clr);
if (cmds != null && cmds.length > 0) {
if (cmds.length > 1) {
// TODO Only log this when it is really necessary. In some situations it is fine
NucleusLogger.PERSISTENCE.warn("Field " + mmd.getFullFieldName() + " represents either a 1-1 relation, " + "or a N-1 relation where the other end uses \"subclass-table\" inheritance strategy and more " + "than 1 subclasses with a table. This is not fully supported");
}
} else {
// TODO Throw an exception ?
return;
}
// TODO We need a mapping for each of the possible subclass tables
referenceMapping = storeMgr.getDatastoreClass(cmds[0].getFullClassName(), clr).getIdMapping();
} else if (refCmd.getInheritanceMetaData() != null && refCmd.getInheritanceMetaData().getStrategy() == InheritanceStrategy.COMPLETE_TABLE) {
// Find the other side of the relation
DatastoreClass refTable = null;
if (refCmd instanceof ClassMetaData && !((ClassMetaData) refCmd).isAbstract()) {
refTable = storeMgr.getDatastoreClass(refCmd.getFullClassName(), clr);
} else {
Collection<String> refSubclasses = storeMgr.getSubClassesForClass(refCmd.getFullClassName(), true, clr);
if (refSubclasses != null && !refSubclasses.isEmpty()) {
// if only 1 subclass then use that
String refSubclassName = refSubclasses.iterator().next();
refTable = storeMgr.getDatastoreClass(refSubclassName, clr);
if (refSubclasses.size() > 1) {
NucleusLogger.DATASTORE_SCHEMA.info("Field " + mmd.getFullFieldName() + " is a 1-1/N-1 relation and the other side had multiple possible classes " + "to which to create a foreign-key. Using first possible (" + refSubclassName + ")");
}
}
}
if (refTable != null) {
referenceMapping = refTable.getIdMapping();
} else {
throw new NucleusUserException("Field " + mmd.getFullFieldName() + " represents either a 1-1 relation, " + "or a N-1 relation where the other end uses \"complete-table\" inheritance strategy and either no table was found, or multiple possible tables!");
}
} else {
// Default is to use the ID of the related object
// TODO Add option to use a natural-id in the other class. Find the mapping using the targetColumnName
referenceMapping = storeMgr.getDatastoreClass(mmd.getType().getName(), clr).getIdMapping();
}
// Generate a mapping from the columns of the referenced object to this mapping's ColumnMetaData
CorrespondentColumnsMapper correspondentColumnsMapping = new CorrespondentColumnsMapper(mmd, table, referenceMapping, true);
// Find any related field where this is part of a bidirectional relation
RelationType relationType = mmd.getRelationType(clr);
boolean createDatastoreMappings = true;
if (relationType == RelationType.MANY_TO_ONE_BI) {
AbstractMemberMetaData[] relatedMmds = mmd.getRelatedMemberMetaData(clr);
// TODO Cater for more than 1 related field
createDatastoreMappings = (relatedMmds[0].getJoinMetaData() == null);
} else if (// TODO If join table then don't need this
relationType == RelationType.ONE_TO_ONE_BI) {
// Put the FK at the end without "mapped-by"
createDatastoreMappings = (mmd.getMappedBy() == null);
}
if (mmd.getJoinMetaData() != null && (relationType == RelationType.MANY_TO_ONE_UNI || relationType == RelationType.ONE_TO_ONE_UNI || relationType == RelationType.ONE_TO_ONE_BI)) {
if (relationType == RelationType.ONE_TO_ONE_UNI || relationType == RelationType.ONE_TO_ONE_BI) {
throw new NucleusUserException("We do not currently support 1-1 relations via join table : " + mmd.getFullFieldName());
}
// create join table
storeMgr.newJoinTable(table, mmd, clr);
} else {
// Loop through the datastore fields in the referenced class and create a datastore field for each
for (int i = 0; i < referenceMapping.getNumberOfDatastoreMappings(); i++) {
DatastoreMapping refDatastoreMapping = referenceMapping.getDatastoreMapping(i);
JavaTypeMapping mapping = storeMgr.getMappingManager().getMapping(refDatastoreMapping.getJavaTypeMapping().getJavaType());
this.addJavaTypeMapping(mapping);
// Create physical datastore columns where we require a FK link to the related table.
if (createDatastoreMappings) {
// Find the Column MetaData that maps to the referenced datastore field
ColumnMetaData colmd = correspondentColumnsMapping.getColumnMetaDataByIdentifier(refDatastoreMapping.getColumn().getIdentifier());
if (colmd == null) {
throw new NucleusUserException(Localiser.msg("041038", refDatastoreMapping.getColumn().getIdentifier(), toString())).setFatal();
}
// Create a Datastore field to equate to the referenced classes datastore field
MappingManager mmgr = storeMgr.getMappingManager();
Column col = mmgr.createColumn(mmd, table, mapping, colmd, refDatastoreMapping.getColumn(), clr);
// Add its datastore mapping
DatastoreMapping datastoreMapping = mmgr.createDatastoreMapping(mapping, col, refDatastoreMapping.getJavaTypeMapping().getJavaTypeForDatastoreMapping(i));
this.addDatastoreMapping(datastoreMapping);
} else {
mapping.setReferenceMapping(referenceMapping);
}
}
}
}
}
use of org.datanucleus.store.rdbms.table.Column in project datanucleus-rdbms by datanucleus.
the class SerialisedElementPCMapping method prepareDatastoreMapping.
/**
* Method to prepare a field mapping for use in the datastore.
* This creates the column in the table.
*/
protected void prepareDatastoreMapping() {
MappingManager mmgr = storeMgr.getMappingManager();
ColumnMetaData colmd = null;
if (mmd.getElementMetaData() != null && mmd.getElementMetaData().getColumnMetaData() != null && mmd.getElementMetaData().getColumnMetaData().length > 0) {
colmd = mmd.getElementMetaData().getColumnMetaData()[0];
}
Column col = mmgr.createColumn(this, getType(), colmd);
mmgr.createDatastoreMapping(this, mmd, 0, col);
}
use of org.datanucleus.store.rdbms.table.Column in project datanucleus-rdbms by datanucleus.
the class SingleFieldMapping method prepareDatastoreMapping.
/**
* Method to prepare a field mapping for use in the datastore.
* This creates the column in the table.
*/
protected void prepareDatastoreMapping() {
MappingManager mmgr = storeMgr.getMappingManager();
Column col = mmgr.createColumn(this, getJavaTypeForDatastoreMapping(0), 0);
mmgr.createDatastoreMapping(this, mmd, 0, col);
}
use of org.datanucleus.store.rdbms.table.Column in project datanucleus-rdbms by datanucleus.
the class MappingManagerImpl method createColumn.
/**
* Method to create a datastore field for a Java type mapping.
* This is used for serialised PC elements/keys/values in a join table.
* TODO Merge this with the method above.
* @param mapping Java type mapping for the field
* @param javaType The type of field being stored in this column
* @param colmd MetaData for the column
* @return The column
*/
public Column createColumn(JavaTypeMapping mapping, String javaType, ColumnMetaData colmd) {
AbstractMemberMetaData mmd = mapping.getMemberMetaData();
Table tbl = mapping.getTable();
if (colmd == null) {
// If column specified add one (use any column name specified on field element)
colmd = new ColumnMetaData();
if (mmd.getColumnMetaData() != null && mmd.getColumnMetaData().length == 1) {
colmd.setName(mmd.getColumnMetaData()[0].getName());
}
mmd.addColumn(colmd);
}
Column col;
IdentifierFactory idFactory = storeMgr.getIdentifierFactory();
if (colmd.getName() == null) {
// No name specified, so generate the identifier from the field name
DatastoreIdentifier identifier = idFactory.newIdentifier(IdentifierType.COLUMN, mmd.getName());
int i = 0;
while (tbl.hasColumn(identifier)) {
identifier = idFactory.newIdentifier(IdentifierType.COLUMN, mmd.getName() + "_" + i);
i++;
}
colmd.setName(identifier.getName());
col = tbl.addColumn(javaType, identifier, mapping, colmd);
} else {
// User has specified a name, so try to keep this unmodified
col = tbl.addColumn(javaType, idFactory.newColumnIdentifier(colmd.getName(), storeMgr.getNucleusContext().getTypeManager().isDefaultEmbeddedType(mmd.getType()), null, true), mapping, colmd);
}
setColumnNullability(mmd, colmd, col);
if (mmd.getNullValue() == NullValue.DEFAULT) {
// Users default should be applied if a null is to be inserted
col.setDefaultable(colmd.getDefaultValue());
}
return col;
}
Aggregations