Search in sources :

Example 6 with DbEntityDescriptor

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

the class MatchChunk method process.

@Override
public void process(StringBuilder out) {
    if (objectRef != null) {
        data = templateData.lookupObject(objectRef);
    }
    DbEntityDescriptor ded = lookupTableRef(tableRef);
    String table = resolveTable(tableRef, ded);
    DbEntityColumnDescriptor[] decList = ded.getColumnDescriptors();
    String typeName = StringUtil.uncapitalize(ded.getEntityName());
    int count = 0;
    out.append('(');
    for (DbEntityColumnDescriptor dec : decList) {
        if ((includeColumns == COLS_ONLY_IDS) && (!dec.isId())) {
            continue;
        }
        String property = dec.getPropertyName();
        Object value = BeanUtil.declaredSilent.getProperty(data, property);
        if ((includeColumns == COLS_ONLY_EXISTING) && (value == null)) {
            continue;
        }
        if (includeColumns == COLS_ONLY_EXISTING) {
            if (DbOomUtil.isEmptyColumnValue(dec, value)) {
                continue;
            }
        }
        if (count > 0) {
            out.append(AND);
        }
        count++;
        out.append(table).append('.').append(dec.getColumnName()).append('=');
        String propertyName = objectRef != null ? objectRef : typeName;
        propertyName += '.' + property;
        defineParameter(out, propertyName, value, dec);
    }
    if (count == 0) {
        out.append(DEFAULT);
    }
    out.append(')');
}
Also used : DbEntityDescriptor(jodd.db.oom.DbEntityDescriptor) DbEntityColumnDescriptor(jodd.db.oom.DbEntityColumnDescriptor)

Example 7 with DbEntityDescriptor

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

the class ReferenceChunk method process.

// ---------------------------------------------------------------- process
@Override
public void process(StringBuilder out) {
    DbEntityDescriptor ded;
    if (tableRef != null) {
        ded = lookupTableRef(tableRef);
        String tableName = resolveTable(tableRef, ded);
        out.append(tableName);
    } else {
        ded = findColumnRef(columnRef);
    }
    if (onlyId) {
        if (tableRef != null) {
            out.append('.');
        }
        out.append(ded.getIdColumnName());
    } else if (columnRef != null) {
        DbEntityColumnDescriptor dec = ded.findByPropertyName(columnRef);
        templateData.lastColumnDec = dec;
        String column = dec == null ? null : dec.getColumnName();
        //String column = ded.getColumnName(columnRef);
        if (column == null) {
            throw new DbSqlBuilderException("Invalid column reference: " + tableRef + '.' + columnRef);
        }
        if (tableRef != null) {
            out.append('.');
        }
        out.append(column);
    }
}
Also used : DbEntityDescriptor(jodd.db.oom.DbEntityDescriptor) DbSqlBuilderException(jodd.db.oom.sqlgen.DbSqlBuilderException) DbEntityColumnDescriptor(jodd.db.oom.DbEntityColumnDescriptor)

Example 8 with DbEntityDescriptor

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

the class DefaultResultSetMapper method cacheResultSetEntities.

/**
	 * Caches returned entities. Replaces new instances with existing ones.
	 */
protected void cacheResultSetEntities(Object[] result) {
    if (entitiesCache == null) {
        entitiesCache = new HashMap<>();
    }
    for (int i = 0; i < result.length; i++) {
        Object object = result[i];
        if (object == null) {
            continue;
        }
        DbEntityDescriptor ded = cachedDbEntityDescriptors[i];
        if (ded == null) {
            // not a type, continue
            continue;
        }
        // calculate key
        Object key;
        if (ded.hasIdColumn()) {
            //noinspection unchecked
            key = ded.getKeyValue(object);
        } else {
            key = object;
        }
        Object cachedObject = entitiesCache.get(key);
        if (cachedObject == null) {
            // object is not in the cache, add it
            entitiesCache.put(key, object);
        } else {
            // object is in the cache, replace it
            result[i] = cachedObject;
        }
    }
}
Also used : DbEntityDescriptor(jodd.db.oom.DbEntityDescriptor)

Example 9 with DbEntityDescriptor

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

the class DbEntitySql method findForeign.

/**
	 * Creates SELECT criteria for the entity matched by foreign key.
	 * Foreign key is created by concatenating foreign table name and column name.
	 */
public static DbSqlBuilder findForeign(Class entity, Object value) {
    String tableRef = createTableRefName(entity);
    DbOomManager dbOomManager = DbOomManager.getInstance();
    DbEntityDescriptor dedFk = dbOomManager.lookupType(value.getClass());
    String tableName = dbOomManager.getTableNames().convertTableNameToEntityName(dedFk.getTableName());
    String columnName = dbOomManager.getColumnNames().convertColumnNameToPropertyName(dedFk.getIdColumnName());
    String fkColumn = uncapitalize(tableName) + capitalize(columnName);
    Object idValue = BeanUtil.pojo.getProperty(value, dedFk.getIdPropertyName());
    return sql().$(SELECT).column(tableRef).$(FROM).table(entity, tableRef).$(WHERE).ref(tableRef, fkColumn).$(EQUALS).columnValue(idValue);
}
Also used : DbOomManager(jodd.db.oom.DbOomManager) DbEntityDescriptor(jodd.db.oom.DbEntityDescriptor)

Example 10 with DbEntityDescriptor

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

the class ColumnsSelectChunk method process.

@Override
public void process(StringBuilder out) {
    // hints
    if (templateData.hasHints()) {
        templateData.registerHint(hint == null ? tableRef : hint);
    }
    // columns
    separateByCommaOrSpace(out);
    // special case, only column name, no table ref/name
    if (tableRef.length() == 0) {
        out.append(columnRef);
        return;
    }
    boolean useTableReference = true;
    DbEntityDescriptor ded = lookupTableRef(tableRef, false);
    if (ded == null) {
        useTableReference = false;
        ded = lookupName(tableRef);
    }
    if (columnRef == null) {
        DbEntityColumnDescriptor[] decList = ded.getColumnDescriptors();
        int count = 0;
        boolean withIds = (columnRefArr != null) && ArraysUtil.contains(columnRefArr, StringPool.PLUS);
        for (DbEntityColumnDescriptor dec : decList) {
            if ((includeColumns == COLS_ONLY_IDS) && (!dec.isId())) {
                continue;
            }
            if ((includeColumns == COLS_ALL_BUT_ID) && (dec.isId())) {
                continue;
            }
            if ((includeColumns == COLS_NA_MULTI) && (!withIds || (!dec.isId())) && (!ArraysUtil.contains(columnRefArr, dec.getPropertyName()))) {
                continue;
            }
            if (count > 0) {
                out.append(',').append(' ');
            }
            templateData.lastColumnDec = dec;
            if (useTableReference) {
                appendColumnName(out, ded, dec.getColumnName());
            } else {
                appendAlias(out, ded, dec.getColumnName());
            }
            count++;
        }
    } else {
        DbEntityColumnDescriptor dec = ded.findByPropertyName(columnRef);
        templateData.lastColumnDec = dec;
        String columnName = dec == null ? null : dec.getColumnName();
        //String columnName = ded.getColumnName(columnRef);
        if (columnName == null) {
            throw new DbSqlBuilderException("Invalid column reference: " + tableRef + '.' + columnRef);
        }
        if (useTableReference) {
            appendColumnName(out, ded, columnName);
        } else {
            appendAlias(out, ded, columnName);
        }
    }
}
Also used : DbEntityDescriptor(jodd.db.oom.DbEntityDescriptor) DbSqlBuilderException(jodd.db.oom.sqlgen.DbSqlBuilderException) DbEntityColumnDescriptor(jodd.db.oom.DbEntityColumnDescriptor)

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