/** @throws Exception */ @Test public void testSpecialExpression2() throws Exception { QueryDefinition queryDefn = newReportQuery(); // column mapping String[] name = new String[] {"AMOUNT", "testColumn1"}; ScriptExpression[] se = new ScriptExpression[name.length]; se[0] = new ScriptExpression("dataSetRow.AMOUNT"); se[1] = new ScriptExpression( "if ( row.AMOUNT >200 ){ Total.runningSum(row.AMOUNT); }else{ row.AMOUNT;}"); for (int i = 0; i < name.length; i++) queryDefn.addBinding(new Binding(name[i], se[i])); IResultIterator ri = executeQuery(queryDefn); while (ri.next()) { String str = ""; for (int i = 0; i < name.length; i++) { str += ri.getValue(name[i]); if (i < name.length - 1) str += ", "; } testPrintln(str); } checkOutputFile(); }
/** @throws Exception */ @Test public void testSpecialExpression3() throws Exception { QueryDefinition queryDefn = newReportQuery(); // column mapping String[] name = new String[] {"AMOUNT", "testColumn1", "testColumn2"}; ScriptExpression[] se = new ScriptExpression[name.length]; se[0] = new ScriptExpression("dataSetRow.AMOUNT"); se[1] = new ScriptExpression( "var p=dataSetRow.AMOUNT+1;if( p >200 ){\"A large amount!\"; } else{ \"A small amount!\";}"); se[2] = new ScriptExpression("row[\"testColumn1\"]+dataSetRow.AMOUNT"); for (int i = 0; i < name.length; i++) queryDefn.addBinding(new Binding(name[i], se[i])); IResultIterator ri = executeQuery(queryDefn); while (ri.next()) { String str = ""; for (int i = 0; i < name.length; i++) { str += ri.getValue(name[i]); if (i < name.length - 1) str += ", "; } testPrintln(str); } checkOutputFile(); }
/** * @param name * @param dataType * @param se * @throws BirtException */ private void basicTestNoDataSet(String[] name, int[] dataType, ScriptExpression[] se) throws BirtException { DataEngine dataEngine = new DataEngineImpl( DataEngineContext.newInstance(DataEngineContext.DIRECT_PRESENTATION, null, null, null)); QueryDefinition queryDefn = new QueryDefinition(); for (int i = 0; i < name.length; i++) queryDefn.addBinding(new Binding(name[i], se[i])); IResultIterator ri = dataEngine.prepare(queryDefn).execute(null).getResultIterator(); if (ri.next()) { String str = ""; for (int i = 0; i < name.length; i++) { Object value = ri.getValue(name[i]); str += value; if (i < name.length - 1) str += ", "; if (dataType[i] == DataType.INTEGER_TYPE) assertTrue(value.getClass().equals(Integer.class)); else if (dataType[i] == DataType.DOUBLE_TYPE) assertTrue(value.getClass().equals(Double.class)); else if (dataType[i] == DataType.DATE_TYPE) assertTrue(value.getClass().equals(Date.class)); } testPrintln(str); } }
/** @throws Exception */ @Test public void testSpecialExpression() throws Exception { IComputedColumn cc = new ComputedColumn("AMOUNT2", "row.AMOUNT*2"); this.dataSet.addComputedColumn(cc); QueryDefinition queryDefn = newReportQuery(); // column mapping String[] name = new String[] {"testColumn1"}; ScriptExpression[] se = new ScriptExpression[name.length]; se[0] = new ScriptExpression("if ( 2<1 ){ true; }else{ false;}"); SortDefinition[] sortDefn = new SortDefinition[] {new SortDefinition()}; sortDefn[0].setExpression("row.testColumn1"); sortDefn[0].setSortDirection(ISortDefinition.SORT_DESC); for (int i = 0; i < name.length; i++) queryDefn.addBinding(new Binding(name[i], se[i])); for (int i = 0; i < sortDefn.length; i++) { queryDefn.addSort(sortDefn[i]); } IResultIterator ri = executeQuery(queryDefn); while (ri.next()) { String str = ""; for (int i = 0; i < name.length; i++) { str += ri.getValue(name[i]); if (i < name.length - 1) str += ", "; } testPrintln(str); } checkOutputFile(); }
/** * Sort on table * * @throws Exception */ @Test public void testFilterOnTable() throws Exception { QueryDefinition queryDefn = newReportQuery(); // column mapping String[] name = new String[] {"testColumn1", "testColumn2", "AMOUNT1"}; ScriptExpression[] se = new ScriptExpression[name.length]; se[0] = new ScriptExpression("dataSetRow.COUNTRY"); se[1] = new ScriptExpression("dataSetRow.CITY"); se[2] = new ScriptExpression("dataSetRow.AMOUNT"); for (int i = 0; i < name.length; i++) queryDefn.addBinding(new Binding(name[i], se[i])); ScriptExpression filterExpr = new ScriptExpression("row.AMOUNT1>100"); FilterDefinition filterDefn = new FilterDefinition(filterExpr); queryDefn.addFilter(filterDefn); IResultIterator ri = executeQuery(queryDefn); while (ri.next()) { String str = ""; for (int i = 0; i < name.length; i++) { str += ri.getValue(name[i]); if (i < name.length - 1) str += ", "; } testPrintln(str); } }
/** * Computed column on data set, without total * * @throws Exception */ @Test public void testComputedOnDataSet() throws Exception { IComputedColumn cc = new ComputedColumn("AMOUNT2", "row.AMOUNT*2"); this.dataSet.addComputedColumn(cc); QueryDefinition queryDefn = newReportQuery(); // column mapping String[] name = new String[] {"testColumn1", "testColumn2", "AMOUNT1", "AMOUNT2"}; ScriptExpression[] se = new ScriptExpression[name.length]; se[0] = new ScriptExpression("dataSetRow.COUNTRY"); se[1] = new ScriptExpression("dataSetRow.CITY"); se[2] = new ScriptExpression("dataSetRow.AMOUNT"); se[3] = new ScriptExpression("dataSetRow.AMOUNT2"); for (int i = 0; i < name.length; i++) queryDefn.addBinding(new Binding(name[i], se[i])); IResultIterator ri = executeQuery(queryDefn); while (ri.next()) { String str = ""; for (int i = 0; i < name.length; i++) { str += ri.getValue(name[i]); if (i < name.length - 1) str += ", "; } testPrintln(str); } checkOutputFile(); }
/** * Filtering on data set, without total * * @throws Exception */ @Test public void testFilterOnDateType() throws Exception { FilterDefinition filterDefn = new FilterDefinition( new ConditionalExpression( "row.SALE_DATE", ConditionalExpression.OP_BETWEEN, "\'2004-05-01 00:00:00\'", "\'2004-06-05 00:00:00\'")); this.dataSet.addFilter(filterDefn); QueryDefinition queryDefn = newReportQuery(); // column mapping String[] name = new String[] {"testColumn1", "testColumn2", "SALE_DATE"}; ScriptExpression[] se = new ScriptExpression[name.length]; se[0] = new ScriptExpression("dataSetRow.COUNTRY"); se[1] = new ScriptExpression("dataSetRow.CITY"); se[2] = new ScriptExpression("dataSetRow.SALE_DATE"); for (int i = 0; i < name.length; i++) queryDefn.addBinding(new Binding(name[i], se[i])); IResultIterator ri = executeQuery(queryDefn); while (ri.next()) { String str = ""; for (int i = 0; i < name.length; i++) { str += ri.getValue(name[i]); if (i < name.length - 1) str += ", "; } testPrintln(str); } checkOutputFile(); }
/** @throws Exception */ @Test public void testBindingNameWithDoubleQuote() throws Exception { QueryDefinition queryDefn = newReportQuery(); // column mapping String[] name = new String[] {"test\"Column1", "test\"Column2", "AMOUNT1"}; ScriptExpression[] se = new ScriptExpression[name.length]; se[0] = new ScriptExpression("dataSetRow.COUNTRY"); se[1] = new ScriptExpression("row[\"test\\\"Column1\"]"); se[2] = new ScriptExpression("dataSetRow.AMOUNT"); for (int i = 0; i < name.length; i++) queryDefn.addBinding(new Binding(name[i], se[i])); IResultIterator ri = executeQuery(queryDefn); while (ri.next()) { String str = ""; for (int i = 0; i < name.length; i++) { str += ri.getValue(name[i]); if (i < name.length - 1) str += ", "; } testPrintln(str); } checkOutputFile(); }
/** @throws Exception */ @Test public void testNoDataSetWithSubQuery() throws Exception { // outer query without data set int[] dataType = new int[] {DataType.DATE_TYPE}; String[] name = new String[] {"testColumn1"}; IResultIterator ri2 = null; { ScriptExpression[] se = new ScriptExpression[] { new ScriptExpression("new Date()", dataType[0]), }; QueryDefinition queryDefn = new QueryDefinition(); for (int i = 0; i < name.length; i++) queryDefn.addBinding(new Binding(name[i], se[i])); // sub query String subQueryName = "TEST"; SubqueryDefinition subQueryDefn = new SubqueryDefinition(subQueryName, queryDefn); for (int i = 0; i < name.length; i++) subQueryDefn.addBinding( new Binding(name[i], new ScriptExpression("row._outer." + name[i], dataType[i]))); queryDefn.addSubquery(subQueryDefn); DataEngine myDataEngine = new DataEngineImpl( DataEngineContext.newInstance( DataEngineContext.DIRECT_PRESENTATION, null, null, null)); IResultIterator ri = myDataEngine.prepare(queryDefn).execute(null).getResultIterator(); ri.next(); ri2 = ri.getSecondaryIterator(subQueryName, null); } if (ri2.next()) { String str = ""; for (int i = 0; i < name.length; i++) { Object value = ri2.getValue(name[i]); str += value; if (i < name.length - 1) str += ", "; if (dataType[0] == DataType.DATE_TYPE) assertTrue(value.getClass().equals(Date.class)); } testPrintln(str); } ri2.close(); }
/** @throws Exception */ @Test public void testNoDataSetWithNestedQuery() throws Exception { // outer query without data set String[] name = new String[] {"testColumn1"}; IQueryResults queryResult = null; { int[] dataType = new int[] {DataType.DATE_TYPE}; ScriptExpression[] se = new ScriptExpression[] { new ScriptExpression("new Date()", dataType[0]), }; DataEngine myDataEngine = new DataEngineImpl( DataEngineContext.newInstance( DataEngineContext.DIRECT_PRESENTATION, null, null, null)); QueryDefinition queryDefn = new QueryDefinition(); for (int i = 0; i < name.length; i++) queryDefn.addBinding(new Binding(name[i], se[i])); IResultIterator ri = myDataEngine.prepare(queryDefn).execute(null).getResultIterator(); queryResult = ri.getQueryResults(); } // inner query with data set QueryDefinition queryDefn2 = this.newReportQuery(); for (int i = 0; i < name.length; i++) queryDefn2.addBinding(new Binding(name[i], new ScriptExpression("row._outer." + name[i]))); IResultIterator ri2 = this.dataEngine.prepare(queryDefn2).execute(queryResult, null).getResultIterator(); if (ri2.next()) { String str = ""; for (int i = 0; i < name.length; i++) { Object value = ri2.getValue(name[i]); str += value; if (i < name.length - 1) str += ", "; } testPrintln(str); } ri2.close(); }
/** @throws Exception */ @Test public void testGroup() throws Exception { QueryDefinition queryDefn = newReportQuery(); // column mapping String[] name = new String[] {"testColumn1", "testColumn2", "AMOUNT1"}; ScriptExpression[] se = new ScriptExpression[name.length]; se[0] = new ScriptExpression("dataSetRow.COUNTRY"); se[1] = new ScriptExpression("dataSetRow.CITY"); se[2] = new ScriptExpression("dataSetRow.AMOUNT"); for (int i = 0; i < name.length; i++) queryDefn.addBinding(new Binding(name[i], se[i])); GroupDefinition groupDefn = new GroupDefinition("group1"); groupDefn.setKeyColumn("testColumn1"); String name2 = "testColumn3"; ScriptExpression se2 = new ScriptExpression("Total.sum(dataSetRow.AMOUNT)"); se2.setGroupName("group1"); // groupDefn.addResultSetExpression( name2, new ColumnBindingExpression(se2) ); queryDefn.addBinding(new Binding(name2, se2)); queryDefn.addGroup(groupDefn); String name3 = "testColumn4"; ScriptExpression se3 = new ScriptExpression("row[\"testColumn1\"]"); GroupDefinition groupDefn1 = new GroupDefinition("group2"); groupDefn1.setKeyColumn("testColumn4"); se3.setGroupName("group2"); // groupDefn1.addResultSetExpression( name3, new ColumnBindingExpression(se3) ); queryDefn.addBinding(new Binding(name3, se3)); queryDefn.addGroup(groupDefn1); IResultIterator ri = executeQuery(queryDefn); while (ri.next()) { String str = ""; for (int i = 0; i < name.length; i++) { str += ri.getValue(name[i]); str += ", "; } str += ri.getValue(name2); testPrintln(str); } checkOutputFile(); }
/** @throws Exception */ @Test public void testAutoBinding() throws Exception { QueryDefinition queryDefn = newReportQuery(true); this.dataSet.addComputedColumn(new ComputedColumn("COUN\"TRY", "row[\"COUNTRY\"]")); // column mapping String[] name = new String[] {"COUN\"TRY", "CITY", "AMOUNT"}; IResultIterator ri = executeQuery(queryDefn); while (ri.next()) { String str = ""; for (int i = 0; i < name.length; i++) { str += ri.getValue(name[i]); if (i < name.length - 1) str += ", "; } testPrintln(str); } ri.close(); checkOutputFile(); }