Search in sources :

Example 1 with DbOomException

use of jodd.db.oom.DbOomException 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 2 with DbOomException

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

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

the class DbQueryBuilder method resolveMethodParameterNames.

/**
	 * Resolves method parameter names.
	 */
protected String[] resolveMethodParameterNames(ProxyTargetInfo proxyTargetInfo) {
    String[] paramNames;
    Method method;
    try {
        method = proxyTargetInfo.targetClass.getDeclaredMethod(proxyTargetInfo.targetMethodName, proxyTargetInfo.argumentsClasses);
    } catch (NoSuchMethodException ex) {
        throw new DbOomException(ex);
    }
    MethodParameter[] methodParameters = Paramo.resolveParameters(method);
    paramNames = new String[methodParameters.length];
    for (int i = 0; i < methodParameters.length; i++) {
        MethodParameter methodParameter = methodParameters[i];
        paramNames[i] = methodParameter.getName();
    }
    return paramNames;
}
Also used : DbOomException(jodd.db.oom.DbOomException) Method(java.lang.reflect.Method) MethodParameter(jodd.paramo.MethodParameter)

Example 4 with DbOomException

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

the class DbQueryBuilder method createAndPopulateDbQuery.

/**
	 * Prepares <code>DbQuery</code>.
	 */
public DbOomQuery createAndPopulateDbQuery(ProxyTargetInfo proxyTargetInfo, String query) {
    if (query == null) {
        query = resolveQuery(proxyTargetInfo);
        if (query == null) {
            throw new DbOomException("Query not resolved.");
        }
    }
    // sql generator
    DbSqlGenerator dbSqlGenerator = parsedSqlMap.get(query);
    if (dbSqlGenerator == null) {
        ParsedSql parsedSql = sql(query).parse();
        parsedSqlMap.put(query, parsedSql);
        dbSqlGenerator = parsedSql;
    }
    // db oom query
    DbOomQuery dbOomQuery = query(dbSqlGenerator);
    // parameter names
    String keyName = proxyTargetInfo.targetClass.getName() + "." + proxyTargetInfo.targetMethodName;
    String[] paramNames = methodParamNames.get(keyName);
    if (paramNames == null) {
        paramNames = resolveMethodParameterNames(proxyTargetInfo);
        methodParamNames.put(keyName, paramNames);
    }
    for (int i = 0; i < paramNames.length; i++) {
        String paramName = paramNames[i];
        dbOomQuery.setObject(paramName, proxyTargetInfo.arguments[i]);
    }
    return dbOomQuery;
}
Also used : DbOomException(jodd.db.oom.DbOomException) DbSqlGenerator(jodd.db.oom.DbSqlGenerator) DbOomQuery(jodd.db.oom.DbOomQuery) ParsedSql(jodd.db.oom.sqlgen.ParsedSql)

Example 5 with DbOomException

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

the class DefaultResultSetMapper method readColumnValue.

// ---------------------------------------------------------------- parse object
/**
	 * Reads column value from result set. Since this method may be called more then once for
	 * the same column, it caches column values.
	 */
@SuppressWarnings({ "unchecked" })
protected Object readColumnValue(int colNdx, Class destinationType, Class<? extends SqlType> sqlTypeClass, int columnDbSqlType) {
    if (colNdx != cachedColumnNdx) {
        try {
            SqlType sqlType;
            if (sqlTypeClass != null) {
                sqlType = SqlTypeManager.lookupSqlType(sqlTypeClass);
            } else {
                sqlType = SqlTypeManager.lookup(destinationType);
            }
            if (sqlType != null) {
                cachedColumnValue = sqlType.readValue(resultSet, colNdx + 1, destinationType, columnDbSqlType);
            } else {
                cachedColumnValue = resultSet.getObject(colNdx + 1);
                cachedColumnValue = TypeConverterManager.convertType(cachedColumnValue, destinationType);
            }
        } catch (SQLException sex) {
            throw new DbOomException(dbOomQuery, "Invalid value for column #" + (colNdx + 1), sex);
        }
        cachedColumnNdx = colNdx;
    }
    return cachedColumnValue;
}
Also used : DbOomException(jodd.db.oom.DbOomException) SQLException(java.sql.SQLException) SqlType(jodd.db.type.SqlType)

Aggregations

DbOomException (jodd.db.oom.DbOomException)7 DbEntityDescriptor (jodd.db.oom.DbEntityDescriptor)2 InputStream (java.io.InputStream)1 Method (java.lang.reflect.Method)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 DbQuery (jodd.db.DbQuery)1 DbOomManager (jodd.db.oom.DbOomManager)1 DbOomQuery (jodd.db.oom.DbOomQuery)1 DbSqlGenerator (jodd.db.oom.DbSqlGenerator)1 DbTable (jodd.db.oom.meta.DbTable)1 ParsedSql (jodd.db.oom.sqlgen.ParsedSql)1 SqlType (jodd.db.type.SqlType)1 MethodParameter (jodd.paramo.MethodParameter)1