Search in sources :

Example 1 with Query

use of org.pentaho.platform.dataaccess.metadata.model.impl.Query in project data-access by pentaho.

the class MetadataServiceUtil method convertQuery.

/**
 * Converts a thin query model into a full query
 *
 * @param src
 * @return
 */
public org.pentaho.metadata.query.model.Query convertQuery(Query src) {
    IMetadataDomainRepository domainRepository = getMetadataRepository();
    Domain fullDomain = domainRepository.getDomain(src.getDomainName());
    LogicalModel logicalModel = fullDomain.findLogicalModel(src.getModelId());
    // create a new full query object
    org.pentaho.metadata.query.model.Query dest = new org.pentaho.metadata.query.model.Query(fullDomain, logicalModel);
    // now add the selections
    List<Selection> selections = dest.getSelections();
    for (Column column : src.getColumns()) {
        // get the objects needed for the selection
        LogicalColumn logicalColumn = logicalModel.findLogicalColumn(column.getId());
        org.pentaho.metadata.model.Category category = getCategory(column.getId(), logicalModel);
        AggregationType aggregationType = AggregationType.valueOf(column.getSelectedAggType());
        // create a selection and add it to the list
        Selection selection = new Selection(category, logicalColumn, aggregationType);
        selections.add(selection);
    }
    // now add the filters
    List<Constraint> constraints = dest.getConstraints();
    for (Condition condition : src.getConditions()) {
        org.pentaho.metadata.query.model.CombinationType combinationType = CombinationType.valueOf(condition.getCombinationType());
        LogicalColumn logicalColumn = logicalModel.findLogicalColumn(condition.getColumn());
        String paramName = null;
        for (Parameter parameter : src.getParameters()) {
            if (parameter.getColumn().equals(condition.getColumn())) {
                paramName = parameter.getName() == null ? parameter.getColumn() : parameter.getName();
            }
        }
        // condition.setParameterized(parameterized);
        String formula = condition.getCondition(logicalColumn.getDataType().name(), paramName);
        Constraint constraint = new Constraint(combinationType, formula);
        constraints.add(constraint);
    }
    // now set the disable distinct option
    if (src.getDisableDistinct() != null) {
        dest.setDisableDistinct(src.getDisableDistinct());
    }
    // now add the sorting information
    List<org.pentaho.metadata.query.model.Order> orders = dest.getOrders();
    for (Order order : src.getOrders()) {
        // find the selection
        for (Selection selection : selections) {
            if (selection.getLogicalColumn().getId().equals(order.getColumn())) {
                Type type = Type.valueOf(order.getOrderType());
                org.pentaho.metadata.query.model.Order fullOrder = new org.pentaho.metadata.query.model.Order(selection, type);
                orders.add(fullOrder);
            }
        }
    }
    // now add the parameter information
    List<org.pentaho.metadata.query.model.Parameter> parameters = dest.getParameters();
    for (Parameter parameter : src.getParameters()) {
        // find the column for this parameter
        LogicalColumn logicalColumn = logicalModel.findLogicalColumn(parameter.getColumn());
        DataType type = logicalColumn.getDataType();
        String[] value = parameter.getValue();
        org.pentaho.metadata.query.model.Parameter fullParam = new org.pentaho.metadata.query.model.Parameter(parameter.getColumn(), type, value[0]);
        parameters.add(fullParam);
    }
    return dest;
}
Also used : LogicalColumn(org.pentaho.metadata.model.LogicalColumn) Query(org.pentaho.platform.dataaccess.metadata.model.impl.Query) Constraint(org.pentaho.metadata.query.model.Constraint) Selection(org.pentaho.metadata.query.model.Selection) LocalizedString(org.pentaho.metadata.model.concept.types.LocalizedString) AggregationType(org.pentaho.metadata.model.concept.types.AggregationType) LogicalModel(org.pentaho.metadata.model.LogicalModel) LogicalColumn(org.pentaho.metadata.model.LogicalColumn) IPhysicalColumn(org.pentaho.metadata.model.IPhysicalColumn) Column(org.pentaho.platform.dataaccess.metadata.model.impl.Column) DataType(org.pentaho.metadata.model.concept.types.DataType) Condition(org.pentaho.platform.dataaccess.metadata.model.impl.Condition) Order(org.pentaho.platform.dataaccess.metadata.model.impl.Order) IMetadataDomainRepository(org.pentaho.metadata.repository.IMetadataDomainRepository) DataType(org.pentaho.metadata.model.concept.types.DataType) AggregationType(org.pentaho.metadata.model.concept.types.AggregationType) FieldType(org.pentaho.metadata.model.concept.types.FieldType) Type(org.pentaho.metadata.query.model.Order.Type) CombinationType(org.pentaho.metadata.query.model.CombinationType) Parameter(org.pentaho.platform.dataaccess.metadata.model.impl.Parameter) Domain(org.pentaho.metadata.model.Domain) CombinationType(org.pentaho.metadata.query.model.CombinationType)

Example 2 with Query

use of org.pentaho.platform.dataaccess.metadata.model.impl.Query in project data-access by pentaho.

the class MetadataServiceTest method testDoQuery.

@Test
public void testDoQuery() {
    when(metadataService.doQuery(any(Query.class), anyInt())).thenCallRealMethod();
    when(metadataService.doXmlQuery(anyString(), any(Integer.class))).thenCallRealMethod();
    when(metadataServiceUtil.convertQuery(any(Query.class))).thenCallRealMethod();
    when(metadataServiceUtil.getCategory(anyString(), any(LogicalModel.class))).thenCallRealMethod();
    MarshallableResultSet marshallableResultSet = getMarshallableResultSet();
    when(metadataService.getMarshallableResultSet()).thenReturn(marshallableResultSet);
    Query query = buildQuery();
    MarshallableResultSet marshallableResultSetReturned = metadataService.doQuery(query, ROWS);
    MarshallableRow[] rows = marshallableResultSetReturned.getRows();
    String[] cell = rows[0].getCell();
    MarshallableColumnNames columnNames = marshallableResultSetReturned.getColumnNames();
    String[] columnName = columnNames.getColumnName();
    // Check the result rows lenght
    Assert.assertTrue(rows.length <= ROWS);
    // Check the result row value
    Assert.assertTrue(cell[0].equals(RESULT));
    // Check the result column name
    Assert.assertTrue(columnName[0].equals(COLUMN_NAME));
}
Also used : LogicalModel(org.pentaho.metadata.model.LogicalModel) MarshallableResultSet(org.pentaho.commons.connection.marshal.MarshallableResultSet) MarshallableRow(org.pentaho.commons.connection.marshal.MarshallableRow) Query(org.pentaho.platform.dataaccess.metadata.model.impl.Query) MarshallableColumnNames(org.pentaho.commons.connection.marshal.MarshallableColumnNames) Matchers.anyString(org.mockito.Matchers.anyString) LocalizedString(org.pentaho.metadata.model.concept.types.LocalizedString) Test(org.junit.Test)

Example 3 with Query

use of org.pentaho.platform.dataaccess.metadata.model.impl.Query in project data-access by pentaho.

the class MetadataServiceTest method buildQuery.

private Query buildQuery() {
    // Build the query Columns
    AggregationType defaultAggType = AggregationType.NONE;
    Column[] columns = new Column[1];
    Column column = mock(Column.class);
    when(column.getId()).thenReturn(COLUMN_ID);
    when(column.getName()).thenReturn(COLUMN_NAME);
    when(column.getSelectedAggType()).thenReturn(defaultAggType.name());
    columns[0] = column;
    // Build the query Conditions
    CombinationType combinationType = CombinationType.AND;
    Condition[] conditions = new Condition[1];
    Condition condition = mock(Condition.class);
    when(condition.getCombinationType()).thenReturn(combinationType.name());
    when(condition.getColumn()).thenReturn(COLUMN_NAME);
    when(condition.getCondition(anyString(), anyString())).thenReturn("[" + CATEGORY_NAME + "." + COLUMN_NAME + "] = " + VALUE);
    conditions[0] = condition;
    // Build the query Parameters
    String[] values = { VALUE };
    Parameter[] parameters = new Parameter[1];
    Parameter parameter = mock(Parameter.class);
    when(parameter.getColumn()).thenReturn(COLUMN_NAME);
    when(parameter.getValue()).thenReturn(values);
    parameters[0] = parameter;
    // Build the query Order
    Order[] orders = new Order[1];
    Order order = mock(Order.class);
    orders[0] = order;
    // Build the Query
    Query query = mock(Query.class);
    when(query.getDomainName()).thenReturn(DOMAIN_ID);
    when(query.getModelId()).thenReturn(LOGICAL_MODEL_ID);
    when(query.getColumns()).thenReturn(columns);
    when(query.getConditions()).thenReturn(conditions);
    when(query.getParameters()).thenReturn(parameters);
    when(query.getOrders()).thenReturn(orders);
    return query;
}
Also used : Condition(org.pentaho.platform.dataaccess.metadata.model.impl.Condition) Order(org.pentaho.platform.dataaccess.metadata.model.impl.Order) Query(org.pentaho.platform.dataaccess.metadata.model.impl.Query) CombinationType(org.pentaho.metadata.query.model.CombinationType) Matchers.anyString(org.mockito.Matchers.anyString) LocalizedString(org.pentaho.metadata.model.concept.types.LocalizedString) AggregationType(org.pentaho.metadata.model.concept.types.AggregationType) LogicalColumn(org.pentaho.metadata.model.LogicalColumn) IPhysicalColumn(org.pentaho.metadata.model.IPhysicalColumn) Column(org.pentaho.platform.dataaccess.metadata.model.impl.Column) Parameter(org.pentaho.platform.dataaccess.metadata.model.impl.Parameter)

Example 4 with Query

use of org.pentaho.platform.dataaccess.metadata.model.impl.Query in project data-access by pentaho.

the class MetadataService method getQueryXmlFromJson.

/**
 * Converts a JSON query into a full Query object by going via a thin Query object
 *
 * @param json
 * @return
 */
protected String getQueryXmlFromJson(String json) {
    MetadataServiceUtil util = getMetadataServiceUtil();
    Query query = util.deserializeJsonQuery(json);
    try {
        // convert the thin query model into a full one
        org.pentaho.metadata.query.model.Query fullQuery = util.convertQuery(query);
        // get the XML for the query
        QueryXmlHelper helper = new QueryXmlHelper();
        String xml = helper.toXML(fullQuery);
        return xml;
    } catch (Exception e) {
        // $NON-NLS-1$
        error(Messages.getErrorString("MetadataService.ERROR_0008_BAD_QUERY"), e);
    }
    return null;
}
Also used : Query(org.pentaho.platform.dataaccess.metadata.model.impl.Query) QueryXmlHelper(org.pentaho.metadata.query.model.util.QueryXmlHelper) JSONException(org.json.JSONException) PentahoMetadataException(org.pentaho.pms.core.exception.PentahoMetadataException) IOException(java.io.IOException)

Aggregations

Query (org.pentaho.platform.dataaccess.metadata.model.impl.Query)4 LocalizedString (org.pentaho.metadata.model.concept.types.LocalizedString)3 Matchers.anyString (org.mockito.Matchers.anyString)2 IPhysicalColumn (org.pentaho.metadata.model.IPhysicalColumn)2 LogicalColumn (org.pentaho.metadata.model.LogicalColumn)2 LogicalModel (org.pentaho.metadata.model.LogicalModel)2 AggregationType (org.pentaho.metadata.model.concept.types.AggregationType)2 CombinationType (org.pentaho.metadata.query.model.CombinationType)2 Column (org.pentaho.platform.dataaccess.metadata.model.impl.Column)2 Condition (org.pentaho.platform.dataaccess.metadata.model.impl.Condition)2 Order (org.pentaho.platform.dataaccess.metadata.model.impl.Order)2 Parameter (org.pentaho.platform.dataaccess.metadata.model.impl.Parameter)2 IOException (java.io.IOException)1 JSONException (org.json.JSONException)1 Test (org.junit.Test)1 MarshallableColumnNames (org.pentaho.commons.connection.marshal.MarshallableColumnNames)1 MarshallableResultSet (org.pentaho.commons.connection.marshal.MarshallableResultSet)1 MarshallableRow (org.pentaho.commons.connection.marshal.MarshallableRow)1 Domain (org.pentaho.metadata.model.Domain)1 DataType (org.pentaho.metadata.model.concept.types.DataType)1