{
// Perform any sanity checking of input for SELECT queries
ObjectManager om = query.getObjectManager();
MappedStoreManager storeMgr = (MappedStoreManager)om.getStoreManager();
ClassLoaderResolver clr = om.getClassLoaderResolver();
AbstractClassMetaData cmd = om.getMetaDataManager().getMetaDataForClass(candidateClass, clr);
if (cmd == null)
{
throw new ClassNotPersistenceCapableException(candidateClass.getName());
}
if (cmd.getPersistenceCapableSuperclass() != null)
{
// throw new PersistentSuperclassNotAllowedException(candidateClass.getName());
}
if (query.getResultClass() == null)
{
// Check the presence of the required columns (id, version, discriminator) in the candidate class
String selections = compiledSQL.trim().substring(7); // Skip "SELECT "
int fromStart = selections.indexOf("FROM");
if (fromStart == -1)
{
fromStart = selections.indexOf("from");
}
selections = selections.substring(0, fromStart).trim();
String[] selectedColumns = StringUtils.split(selections, ",");
if (selectedColumns == null || selectedColumns.length == 0)
{
throw new JPOXUserException(LOCALISER_RDBMS.msg("059003", compiledSQL));
}
if (selectedColumns.length == 1 && selectedColumns[0].trim().equals("*"))
{
// SQL Query using * so just return since all possible columns will be selected
return compiledSQL;
}
// Generate id column field information for later checking the id is present
DatastoreClass table = storeMgr.getDatastoreClass(candidateClass.getName(), clr);
PersistenceCapableMapping idMapping = (PersistenceCapableMapping)table.getIDMapping();
String[] idColNames = new String[idMapping.getNumberOfDatastoreFields()];
boolean[] idColMissing = new boolean[idMapping.getNumberOfDatastoreFields()];
for (int i=0;i<idMapping.getNumberOfDatastoreFields();i++)
{
DatastoreMapping m = idMapping.getDataStoreMapping(i);
idColNames[i] = m.getDatastoreField().getIdentifier().toString();
idColMissing[i] = true;
}
// Generate discriminator/version information for later checking they are present
String discriminatorColName = table.getDiscriminatorMapping(false) != null ?
table.getDiscriminatorMapping(false).getDataStoreMapping(0).getDatastoreField().getIdentifier().toString() : null;
String versionColName = table.getVersionMapping(false) != null ?
table.getVersionMapping(false).getDataStoreMapping(0).getDatastoreField().getIdentifier().toString() : null;
boolean discrimMissing = (discriminatorColName != null);
boolean versionMissing = true;
if (versionColName == null)
{
versionMissing = false;
}
// Go through the selected fields and check the existence of id, version, discriminator cols
DatastoreAdapter dba = storeMgr.getDatastoreAdapter();
final AbstractClassMetaData candidateCmd = om.getMetaDataManager().getMetaDataForClass(candidateClass, clr);
for (int i = 0; i < selectedColumns.length; i++)
{
String colName = selectedColumns[i].trim();
if (colName.indexOf(" AS ") > 0)
{
// Allow for user specification of "XX.YY AS ZZ"
colName = colName.substring(colName.indexOf(" AS ")+4).trim();
}
else if (colName.indexOf(" as ") > 0)
{
// Allow for user specification of "XX.YY as ZZ"
colName = colName.substring(colName.indexOf(" as ")+4).trim();
}
if (candidateCmd.getIdentityType() == IdentityType.DATASTORE)
{
// Check for existence of id column, allowing for any RDBMS using quoted identifiers
if (SQLQuery.columnNamesAreTheSame(dba, idColNames[0], colName))
{
idColMissing[0] = false;
}
}
else if (candidateCmd.getIdentityType() == IdentityType.APPLICATION)
{
for (int j=0; j<idColNames.length; j++)
{
// Check for existence of id column, allowing for any RDBMS using quoted identifiers
if (SQLQuery.columnNamesAreTheSame(dba, idColNames[j], colName))