use of org.apache.cayenne.access.jdbc.ColumnDescriptor in project cayenne by apache.
the class VelocitySQLTemplateProcessor_SelectTest method testProcessSelectTemplate3.
@Test
public void testProcessSelectTemplate3() throws Exception {
String sqlTemplate = "SELECT #result('A' 'String' 'B') FROM ME";
SQLStatement compiled = processor.processTemplate(sqlTemplate, Collections.<String, Object>emptyMap());
assertEquals("SELECT A AS B FROM ME", compiled.getSql());
assertEquals(0, compiled.getBindings().length);
assertEquals(1, compiled.getResultColumns().length);
ColumnDescriptor column = compiled.getResultColumns()[0];
assertEquals("A", column.getName());
assertEquals("B", column.getDataRowKey());
assertEquals("java.lang.String", column.getJavaClass());
}
use of org.apache.cayenne.access.jdbc.ColumnDescriptor in project cayenne by apache.
the class ProcedureCallIT method testSelectWithRowDescriptor.
@Test
public void testSelectWithRowDescriptor() throws Exception {
if (!accessStackAdapter.supportsStoredProcedures()) {
return;
}
// create an artist with painting in the database
createArtist(1000.0);
// TESTING THIS ***
// A.ARTIST_ID, A.DATE_OF_BIRTH, A.ARTIST_NAME
ColumnDescriptor[] columns = new ColumnDescriptor[3];
// read ID as Long, and everything else as default types
columns[0] = new ColumnDescriptor("ARTIST_ID", Types.BIGINT);
columns[1] = new ColumnDescriptor("ARTIST_NAME", Types.CHAR);
columns[2] = new ColumnDescriptor("DATE_OF_BIRTH", Types.DATE);
List<?> rows = runProcedureSelect(ProcedureCall.dataRowQuery(SELECT_STORED_PROCEDURE).param("aName", "An Artist").param("paintingPrice", 3000).resultDescriptor(columns)).firstList();
// check the results
assertNotNull("Null result from StoredProcedure.", rows);
assertEquals(1, rows.size());
DataRow artistRow = (DataRow) rows.get(0);
assertEquals(3, artistRow.size());
artistRow = uppercaseConverter(artistRow);
Object id = artistRow.get("ARTIST_ID");
assertNotNull(id);
assertTrue("Expected Long, got: " + id.getClass().getName(), id instanceof Long);
}
use of org.apache.cayenne.access.jdbc.ColumnDescriptor in project cayenne by apache.
the class Result method apply.
@Override
public void apply(Context context, ASTExpression... expressions) {
ColumnDescriptor columnDescriptor = new ColumnDescriptor();
String column = expressions[0].evaluateAsString(context);
columnDescriptor.setName(column);
if (expressions.length > 1) {
String type = expressions[1].evaluateAsString(context);
columnDescriptor.setJavaClass(guessType(type));
}
String alias = null;
if (expressions.length > 2) {
alias = expressions[2].evaluateAsString(context);
}
String dataRowKey = null;
if (expressions.length > 3) {
dataRowKey = expressions[3].evaluateAsString(context);
}
// determine what we want to name this column in a resulting DataRow...
String label = (!Util.isEmptyString(dataRowKey)) ? dataRowKey : (!Util.isEmptyString(alias)) ? alias : null;
columnDescriptor.setDataRowKey(label);
if (expressions.length > 4) {
int jdbcType = (int) expressions[4].evaluateAsLong(context);
columnDescriptor.setJdbcType(jdbcType);
}
context.addColumnDescriptor(columnDescriptor);
context.getBuilder().append(column);
if (!Util.isEmptyString(alias) && !alias.equals(column)) {
context.getBuilder().append(" AS ").append(alias);
}
}
use of org.apache.cayenne.access.jdbc.ColumnDescriptor in project cayenne by apache.
the class DataContextProcedureQueryIT method testSelectWithRowDescriptor.
@Test
public void testSelectWithRowDescriptor() throws Exception {
if (!accessStackAdapter.supportsStoredProcedures()) {
return;
}
// create an artist with painting in the database
createArtist(1000.0);
// test ProcedureQuery with Procedure as root
Procedure proc = context.getEntityResolver().getProcedure(SELECT_STORED_PROCEDURE);
ProcedureQuery q = new ProcedureQuery(proc);
q.setFetchingDataRows(true);
q.addParameter("aName", "An Artist");
q.addParameter("paintingPrice", new Integer(3000));
// TESTING THIS ***
// A.ARTIST_ID, A.DATE_OF_BIRTH, A.ARTIST_NAME
ColumnDescriptor[] columns = new ColumnDescriptor[3];
// read ID as Long, and everything else as default types
columns[0] = new ColumnDescriptor("ARTIST_ID", Types.BIGINT);
columns[1] = new ColumnDescriptor("ARTIST_NAME", Types.CHAR);
columns[2] = new ColumnDescriptor("DATE_OF_BIRTH", Types.DATE);
q.addResultDescriptor(columns);
List<?> rows = runProcedureSelect(q);
// check the results
assertNotNull("Null result from StoredProcedure.", rows);
assertEquals(1, rows.size());
DataRow artistRow = (DataRow) rows.get(0);
assertEquals(3, artistRow.size());
artistRow = uppercaseConverter(artistRow);
Object id = artistRow.get("ARTIST_ID");
assertNotNull(id);
assertTrue("Expected Long, got: " + id.getClass().getName(), id instanceof Long);
}
use of org.apache.cayenne.access.jdbc.ColumnDescriptor in project cayenne by apache.
the class OracleProcedureAction method readProcedureOutParameters.
/**
* Helper method that reads OUT parameters of a CallableStatement.
*/
@Override
protected void readProcedureOutParameters(CallableStatement statement, OperationObserver delegate) throws SQLException, Exception {
long t1 = System.currentTimeMillis();
// build result row...
DataRow result = null;
List<ProcedureParameter> parameters = getProcedure().getCallParameters();
for (int i = 0; i < parameters.size(); i++) {
ProcedureParameter parameter = parameters.get(i);
if (!parameter.isOutParam()) {
continue;
}
// ==== start Oracle-specific part
if (parameter.getType() == OracleAdapter.getOracleCursorType()) {
try (ResultSet rs = (ResultSet) statement.getObject(i + 1)) {
RowDescriptor rsDescriptor = describeResultSet(rs, processedResultSets++);
readResultSet(rs, rsDescriptor, query, delegate);
}
} else // ==== end Oracle-specific part
{
if (result == null) {
result = new DataRow(2);
}
ColumnDescriptor descriptor = new ColumnDescriptor(parameter);
ExtendedType type = dataNode.getAdapter().getExtendedTypes().getRegisteredType(descriptor.getJavaClass());
Object val = type.materializeObject(statement, i + 1, descriptor.getJdbcType());
result.put(descriptor.getDataRowKey(), val);
}
}
if (result != null && !result.isEmpty()) {
// treat out parameters as a separate data row set
dataNode.getJdbcEventLogger().logSelectCount(1, System.currentTimeMillis() - t1);
delegate.nextRows(query, Collections.singletonList(result));
}
}
Aggregations