Search in sources :

Example 1 with DbEntityColumnDescriptor

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

the class InsertChunk method process.

@Override
public void process(StringBuilder out) {
    DbEntityDescriptor ded = entityName != null ? lookupName(entityName) : lookupType(entityType);
    StringBuilder col = new StringBuilder();
    StringBuilder val = new StringBuilder();
    DbEntityColumnDescriptor[] decList = ded.getColumnDescriptors();
    String typeName = StringUtil.uncapitalize(ded.getEntityName());
    int size = 0;
    for (DbEntityColumnDescriptor dec : decList) {
        String property = dec.getPropertyName();
        Object value = BeanUtil.declared.getProperty(data, property);
        if (value == null) {
            continue;
        }
        if (size > 0) {
            col.append(',').append(' ');
            val.append(',').append(' ');
        }
        size++;
        col.append(dec.getColumnName());
        String propertyName = typeName + '.' + property;
        defineParameter(val, propertyName, value, dec);
    }
    out.append("insert into ").append(ded.getTableName()).append(" (").append(col).append(") values (").append(val).append(')');
}
Also used : DbEntityDescriptor(jodd.db.oom.DbEntityDescriptor) DbEntityColumnDescriptor(jodd.db.oom.DbEntityColumnDescriptor)

Example 2 with DbEntityColumnDescriptor

use of jodd.db.oom.DbEntityColumnDescriptor 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 3 with DbEntityColumnDescriptor

use of jodd.db.oom.DbEntityColumnDescriptor 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 4 with DbEntityColumnDescriptor

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

Example 5 with DbEntityColumnDescriptor

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

Aggregations

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