Search in sources :

Example 11 with DbEntityDescriptor

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

the class UpdateSetChunk method process.

@Override
public void process(StringBuilder out) {
    if (isPreviousChunkOfType(CHUNK_TABLE)) {
        appendMissingSpace(out);
    }
    DbEntityDescriptor ded = tableRef != null ? lookupTableRef(tableRef) : lookupType(resolveClass(data));
    out.append(SET);
    DbEntityColumnDescriptor[] decList = ded.getColumnDescriptors();
    String typeName = StringUtil.uncapitalize(ded.getEntityName());
    //String table = resolveTable(tableRef, ded);
    int size = 0;
    for (DbEntityColumnDescriptor dec : decList) {
        String property = dec.getPropertyName();
        Object value = BeanUtil.declared.getProperty(data, property);
        if (includeColumns == COLS_ONLY_EXISTING) {
            if (DbOomUtil.isEmptyColumnValue(dec, value)) {
                continue;
            }
        }
        if (size > 0) {
            out.append(',').append(' ');
        }
        size++;
        // do not add table reference in set
        // as only one table can be updated
        // also, Postgress database does not allow it (see #JODD-21)
        //out.append(table).append('.');
        out.append(dec.getColumnName()).append('=');
        String propertyName = typeName + '.' + property;
        defineParameter(out, propertyName, value, dec);
    }
    if (size > 0) {
        out.append(' ');
    }
}
Also used : DbEntityDescriptor(jodd.db.oom.DbEntityDescriptor) DbEntityColumnDescriptor(jodd.db.oom.DbEntityColumnDescriptor)

Example 12 with DbEntityDescriptor

use of jodd.db.oom.DbEntityDescriptor 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)

Example 13 with DbEntityDescriptor

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

the class DefaultResultSetMapper method createTypesTableNames.

/**
	 * Creates table names for given types.
	 */
protected String[] createTypesTableNames(Class[] types) {
    String[] names = new String[types.length];
    for (int i = 0; i < types.length; i++) {
        if (types[i] == null) {
            names[i] = null;
            continue;
        }
        DbEntityDescriptor ded = dbOomManager.lookupType(types[i]);
        if (ded != null) {
            String tableName = ded.getTableName();
            tableName = tableName.toUpperCase();
            names[i] = tableName;
        }
    }
    return names;
}
Also used : DbEntityDescriptor(jodd.db.oom.DbEntityDescriptor)

Example 14 with DbEntityDescriptor

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

the class DbIdGenerator method nextId.

/**
	 * Returns next ID for given entity type.
	 * On the first call, it finds the max value of all IDs and stores it.
	 * On later calls, stored id is incremented and returned.
	 */
public synchronized long nextId(Class entityType) {
    MutableLong lastId = entityIdsMap.get(entityType);
    if (lastId == null) {
        DbOomManager dbOomManager = DbOomManager.getInstance();
        DbEntityDescriptor ded = dbOomManager.lookupType(entityType);
        String tableName = ded.getTableName();
        String idColumn = ded.getIdColumnName();
        DbOomQuery dbOomQuery = query("select max(" + idColumn + ") from " + tableName);
        long lastLong = dbOomQuery.autoClose().executeCount();
        if (log.isDebugEnabled()) {
            log.debug("Last id for " + entityType.getName() + " is " + lastLong);
        }
        lastId = new MutableLong(lastLong);
        entityIdsMap.put(entityType, lastId);
    }
    lastId.value++;
    return lastId.value;
}
Also used : MutableLong(jodd.mutable.MutableLong) DbOomQuery(jodd.db.oom.DbOomQuery) DbOomManager(jodd.db.oom.DbOomManager) DbEntityDescriptor(jodd.db.oom.DbEntityDescriptor)

Aggregations

DbEntityDescriptor (jodd.db.oom.DbEntityDescriptor)14 DbEntityColumnDescriptor (jodd.db.oom.DbEntityColumnDescriptor)6 DbOomManager (jodd.db.oom.DbOomManager)4 DbOomException (jodd.db.oom.DbOomException)2 DbSqlBuilderException (jodd.db.oom.sqlgen.DbSqlBuilderException)2 ArrayList (java.util.ArrayList)1 DbQuery (jodd.db.DbQuery)1 DbOomQuery (jodd.db.oom.DbOomQuery)1 MutableLong (jodd.mutable.MutableLong)1