Search in sources :

Example 6 with ColumnDescriptor

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());
}
Also used : ColumnDescriptor(org.apache.cayenne.access.jdbc.ColumnDescriptor) SQLStatement(org.apache.cayenne.access.jdbc.SQLStatement) Test(org.junit.Test)

Example 7 with ColumnDescriptor

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);
}
Also used : ColumnDescriptor(org.apache.cayenne.access.jdbc.ColumnDescriptor) DataRow(org.apache.cayenne.DataRow) Test(org.junit.Test)

Example 8 with ColumnDescriptor

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);
    }
}
Also used : ColumnDescriptor(org.apache.cayenne.access.jdbc.ColumnDescriptor)

Example 9 with ColumnDescriptor

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);
}
Also used : ColumnDescriptor(org.apache.cayenne.access.jdbc.ColumnDescriptor) Procedure(org.apache.cayenne.map.Procedure) DataRow(org.apache.cayenne.DataRow) ProcedureQuery(org.apache.cayenne.query.ProcedureQuery) Test(org.junit.Test)

Example 10 with ColumnDescriptor

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));
    }
}
Also used : ProcedureParameter(org.apache.cayenne.map.ProcedureParameter) ColumnDescriptor(org.apache.cayenne.access.jdbc.ColumnDescriptor) ResultSet(java.sql.ResultSet) ExtendedType(org.apache.cayenne.access.types.ExtendedType) DataRow(org.apache.cayenne.DataRow) RowDescriptor(org.apache.cayenne.access.jdbc.RowDescriptor)

Aggregations

ColumnDescriptor (org.apache.cayenne.access.jdbc.ColumnDescriptor)17 DbAttribute (org.apache.cayenne.map.DbAttribute)5 Test (org.junit.Test)5 DataRow (org.apache.cayenne.DataRow)4 ArrayList (java.util.ArrayList)3 ClassDescriptor (org.apache.cayenne.reflect.ClassDescriptor)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)2 RowDescriptor (org.apache.cayenne.access.jdbc.RowDescriptor)2 SQLStatement (org.apache.cayenne.access.jdbc.SQLStatement)2 ExtendedType (org.apache.cayenne.access.types.ExtendedType)2 Expression (org.apache.cayenne.exp.Expression)2 DataMap (org.apache.cayenne.map.DataMap)2 DbEntity (org.apache.cayenne.map.DbEntity)2 ObjAttribute (org.apache.cayenne.map.ObjAttribute)2 ObjEntity (org.apache.cayenne.map.ObjEntity)2 SelectQuery (org.apache.cayenne.query.SelectQuery)2 ResultSet (java.sql.ResultSet)1 Collection (java.util.Collection)1