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;
}
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()]);
}
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;
}
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;
}
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;
}
Aggregations