Search in sources :

Example 1 with DbEntityDescriptor

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

the class GenericDao method deleteById.

/**
	 * Delete single object by its id. Resets ID value.
	 */
public void deleteById(Object entity) {
    if (entity != null) {
        int result = query(DbEntitySql.deleteById(entity)).autoClose().executeUpdate();
        if (result != 0) {
            // now reset the ID value
            DbOomManager dboom = DbOomManager.getInstance();
            Class type = entity.getClass();
            DbEntityDescriptor ded = dboom.lookupType(type);
            setEntityId(ded, entity, 0);
        }
    }
}
Also used : DbOomManager(jodd.db.oom.DbOomManager) DbEntityDescriptor(jodd.db.oom.DbEntityDescriptor)

Example 2 with DbEntityDescriptor

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

the class GenericDao method store.

/**
	 * Saves or updates entity. If ID is not <code>null</code>, entity will be updated.
	 * Otherwise, entity will be inserted into the database.
	 */
public <E> E store(E entity) {
    DbOomManager dboom = DbOomManager.getInstance();
    Class type = entity.getClass();
    DbEntityDescriptor ded = dboom.lookupType(type);
    if (ded == null) {
        throw new DbOomException("Not an entity: " + type);
    }
    if (!isPersistent(ded, entity)) {
        DbQuery q;
        if (keysGeneratedByDatabase) {
            q = query(insert(entity));
            q.setGeneratedKey();
            q.executeUpdate();
            long nextId = q.getGeneratedKey();
            setEntityId(ded, entity, nextId);
        } else {
            long nextId = generateNextId(ded);
            setEntityId(ded, entity, nextId);
            q = query(insert(entity));
            q.executeUpdate();
        }
        q.close();
    } else {
        query(DbEntitySql.updateAll(entity)).autoClose().executeUpdate();
    }
    return entity;
}
Also used : DbOomException(jodd.db.oom.DbOomException) DbQuery(jodd.db.DbQuery) DbOomManager(jodd.db.oom.DbOomManager) DbEntityDescriptor(jodd.db.oom.DbEntityDescriptor)

Example 3 with DbEntityDescriptor

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

the class DefaultResultSetMapper method resolveTables.

// ---------------------------------------------------------------- delegates
/**
	 * {@inheritDoc}
	 */
public Class[] resolveTables() {
    List<Class> classes = new ArrayList<>(tableNames.length);
    String lastTableName = null;
    resultColumns.clear();
    for (int i = 0; i < tableNames.length; i++) {
        String tableName = tableNames[i];
        String columnName = columnNames[i];
        if (tableName == null) {
            // maybe JDBC driver does not support it
            throw new DbOomException(dbOomQuery, "Table name missing in meta-data");
        }
        if ((!tableName.equals(lastTableName)) || (resultColumns.contains(columnName))) {
            resultColumns.clear();
            lastTableName = tableName;
            DbEntityDescriptor ded = dbOomManager.lookupTableName(tableName);
            if (ded == null) {
                throw new DbOomException(dbOomQuery, "Table name not registered: " + tableName);
            }
            classes.add(ded.getType());
        }
        resultColumns.add(columnName);
    }
    return classes.toArray(new Class[classes.size()]);
}
Also used : DbOomException(jodd.db.oom.DbOomException) ArrayList(java.util.ArrayList) DbEntityDescriptor(jodd.db.oom.DbEntityDescriptor)

Example 4 with DbEntityDescriptor

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

the class DefaultResultSetMapper method resolveMappedTypesTableNames.

/**
	 * Resolved mapped type names for each type.
	 */
protected String[][] resolveMappedTypesTableNames(Class[] types) {
    if (cachedMappedNames == null) {
        String[][] names = new String[types.length][];
        for (int i = 0; i < types.length; i++) {
            Class type = types[i];
            if (type != null) {
                DbEntityDescriptor ded = cachedDbEntityDescriptors[i];
                if (ded != null) {
                    Class[] mappedTypes = ded.getMappedTypes();
                    if (mappedTypes != null) {
                        names[i] = createTypesTableNames(mappedTypes);
                    }
                }
            }
        }
        cachedMappedNames = names;
    }
    return cachedMappedNames;
}
Also used : DbEntityDescriptor(jodd.db.oom.DbEntityDescriptor)

Example 5 with DbEntityDescriptor

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

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