Search in sources :

Example 6 with DbEntityColumnDescriptor

use of jodd.db.oom.DbEntityColumnDescriptor in project jodd by oblac.

the class DefaultResultSetMapper method parseObjects.

/**
	 * {@inheritDoc}
	 */
public Object[] parseObjects(Class... types) {
    resultColumns.clear();
    int totalTypes = types.length;
    Object[] result = new Object[totalTypes];
    boolean[] resultUsage = new boolean[totalTypes];
    DbEntityDescriptor[] dbEntityDescriptors = resolveDbEntityDescriptors(types);
    String[] typesTableNames = resolveTypesTableNames(types);
    String[][] mappedNames = resolveMappedTypesTableNames(types);
    int currentResult = 0;
    cachedColumnNdx = -1;
    int colNdx = 0;
    while (colNdx < totalColumns) {
        // no more types for mapping?
        if (currentResult >= totalTypes) {
            break;
        }
        // skip columns that doesn't map
        Class currentType = types[currentResult];
        if (currentType == null) {
            colNdx++;
            currentResult++;
            resultColumns.clear();
            continue;
        }
        String columnName = columnNames[colNdx];
        int columnDbSqlType = columnDbSqlTypes[colNdx];
        String tableName = tableNames[colNdx];
        String resultTableName = typesTableNames[currentResult];
        if (resultTableName == null) {
            // match: simple type
            result[currentResult] = readColumnValue(colNdx, currentType, null, columnDbSqlType);
            resultUsage[currentResult] = true;
            colNdx++;
            currentResult++;
            resultColumns.clear();
            continue;
        }
        // match table
        boolean tableMatched = false;
        if (tableName == null) {
            tableMatched = true;
        } else if (resultTableName.equals(tableName)) {
            tableMatched = true;
        } else {
            String[] mapped = mappedNames[currentResult];
            if (mapped != null) {
                for (String m : mapped) {
                    if (m.equals(tableName)) {
                        tableMatched = true;
                        break;
                    }
                }
            }
        }
        if (tableMatched) {
            if (!resultColumns.contains(columnName)) {
                //DbEntityDescriptor ded = dbOomManager.lookupType(currentType);
                DbEntityDescriptor ded = dbEntityDescriptors[currentResult];
                DbEntityColumnDescriptor dec = ded.findByColumnName(columnName);
                String propertyName = (dec == null ? null : dec.getPropertyName());
                // check if a property that matches column name exist
                if (propertyName != null) {
                    // of some entity), create the instance and store it
                    if (result[currentResult] == null) {
                        result[currentResult] = dbOomManager.createEntityInstance(currentType);
                    }
                    /*
						boolean success = value != null ?
										BeanUtil.setDeclaredPropertySilent(result[currentResult], propertyName, value) :
										BeanUtil.hasDeclaredProperty(result[currentResult], propertyName);
*/
                    Class type = BeanUtil.declared.getPropertyType(result[currentResult], propertyName);
                    if (type != null) {
                        // match: entity
                        // updates column db sql type information for the entity!!!
                        dec.updateDbSqlType(columnDbSqlType);
                        Class<? extends SqlType> sqlTypeClass = dec.getSqlTypeClass();
                        Object value = readColumnValue(colNdx, type, sqlTypeClass, columnDbSqlType);
                        if (value != null) {
                            // inject column value into existing entity
                            BeanUtil.declared.setProperty(result[currentResult], propertyName, value);
                            resultUsage[currentResult] = true;
                        }
                        colNdx++;
                        resultColumns.add(columnName);
                        continue;
                    }
                }
            }
        }
        // go to next type, i.e. result
        currentResult++;
        resultColumns.clear();
    }
    resultColumns.clear();
    for (int i = 0; i < resultUsage.length; i++) {
        if (!resultUsage[i]) {
            result[i] = null;
        }
    }
    if (cacheEntities) {
        cacheResultSetEntities(result);
    }
    return result;
}
Also used : DbEntityColumnDescriptor(jodd.db.oom.DbEntityColumnDescriptor) DbEntityDescriptor(jodd.db.oom.DbEntityDescriptor)

Aggregations

DbEntityColumnDescriptor (jodd.db.oom.DbEntityColumnDescriptor)6 DbEntityDescriptor (jodd.db.oom.DbEntityDescriptor)6 DbSqlBuilderException (jodd.db.oom.sqlgen.DbSqlBuilderException)2