Search in sources :

Example 91 with Query

use of org.compiere.model.Query in project adempiere by adempiere.

the class QueryTest method testSetClient_ID.

public void testSetClient_ID() throws Exception {
    int AD_Client_ID = Env.getAD_Client_ID(getCtx());
    String sql = "SELECT COUNT(*) FROM C_Invoice WHERE IsActive='Y' AND AD_Client_ID=" + AD_Client_ID;
    int targetCount = DB.getSQLValue(null, sql);
    //
    int count = new Query(getCtx(), "C_Invoice", "1=1", getTrxName()).setOnlyActiveRecords(true).setClient_ID().count();
    assertEquals("Invoice # not match", targetCount, count);
}
Also used : Query(org.compiere.model.Query)

Example 92 with Query

use of org.compiere.model.Query in project adempiere by adempiere.

the class QueryTest method testFirstId.

public void testFirstId() throws Exception {
    int id = new Query(getCtx(), "AD_Table", "TableName IN (?,?)", getTrxName()).setParameters(new Object[] { "C_Invoice", "M_InOut" }).setOrderBy("TableName").firstId();
    // C_Invoice
    int expectedId = 318;
    assertEquals("Invalid ID", expectedId, id);
}
Also used : Query(org.compiere.model.Query)

Example 93 with Query

use of org.compiere.model.Query in project adempiere by adempiere.

the class QueryTest method testAggregate.

public void testAggregate() throws Exception {
    final int AD_Client_ID = Env.getAD_Client_ID(getCtx());
    final String sqlFrom = "FROM C_InvoiceLine WHERE IsActive='Y' AND AD_Client_ID=" + AD_Client_ID;
    final Query query = new Query(getCtx(), "C_InvoiceLine", null, getTrxName()).setOnlyActiveRecords(true).setClient_ID();
    //
    // Test COUNT:
    assertEquals("COUNT not match", DB.getSQLValueBDEx(getTrxName(), "SELECT COUNT(*) " + sqlFrom), query.aggregate(null, Query.AGGREGATE_COUNT));
    //
    // Test SUM:
    assertEquals("SUM not match", DB.getSQLValueBDEx(getTrxName(), "SELECT SUM(LineNetAmt+TaxAmt) " + sqlFrom), query.aggregate("LineNetAmt+TaxAmt", Query.AGGREGATE_SUM));
    //
    // Test MIN:
    assertEquals("MIN not match", DB.getSQLValueBDEx(getTrxName(), "SELECT MIN(LineNetAmt) " + sqlFrom), query.aggregate("LineNetAmt", Query.AGGREGATE_MIN));
    //
    // Test MAX:
    assertEquals("MAX not match", DB.getSQLValueBDEx(getTrxName(), "SELECT MAX(LineNetAmt) " + sqlFrom), query.aggregate("LineNetAmt", Query.AGGREGATE_MAX));
    //
    // Test aggregate (String) - FR [ 2726447 ]
    assertEquals("MAX not match (String)", DB.getSQLValueStringEx(getTrxName(), "SELECT MAX(Description) " + sqlFrom), (String) query.aggregate("Description", Query.AGGREGATE_MAX, String.class));
    //
    // Test aggregate (Timestamp) - FR [ 2726447 ]
    assertEquals("MAX not match (Timestamp)", DB.getSQLValueTSEx(getTrxName(), "SELECT MAX(Updated) " + sqlFrom), (Timestamp) query.aggregate("Updated", Query.AGGREGATE_MAX, Timestamp.class));
    //
    // Test Exception : No Aggregate Function defined
    assertExceptionThrown("No Aggregate Function defined", DBException.class, new Runnable() {

        public void run() {
            query.aggregate("*", null);
        }
    });
    //
    // Test Exception : No Expression defined
    assertExceptionThrown("No Expression defined", DBException.class, new Runnable() {

        public void run() {
            query.aggregate(null, Query.AGGREGATE_SUM);
        }
    });
}
Also used : Query(org.compiere.model.Query)

Example 94 with Query

use of org.compiere.model.Query in project adempiere by adempiere.

the class QueryTest method testCount.

public void testCount() throws Exception {
    int count = new Query(getCtx(), "AD_Table", "TableName IN (?,?)", getTrxName()).setParameters(new Object[] { "C_Invoice", "M_InOut" }).setOrderBy("TableName").count();
    assertEquals("Invalid count", 2, count);
}
Also used : Query(org.compiere.model.Query)

Example 95 with Query

use of org.compiere.model.Query in project adempiere by adempiere.

the class QueryTest method testOnlySelection.

public void testOnlySelection() throws Exception {
    // Get one AD_PInstance_ID
    int AD_PInstance_ID = DB.getSQLValueEx(null, "SELECT MAX(AD_PInstance_ID) FROM AD_PInstance");
    assertTrue(AD_PInstance_ID > 0);
    // Create selection list
    List<Integer> elements = new ArrayList<Integer>();
    // AD_Element_ID=102 => AD_Client_ID
    elements.add(102);
    // AD_Element_ID=104 => AD_Column_ID
    elements.add(104);
    DB.executeUpdateEx("DELETE FROM T_Selection WHERE AD_PInstance_ID=" + AD_PInstance_ID, getTrxName());
    DB.createT_Selection(AD_PInstance_ID, elements, getTrxName());
    // some dummy where clause
    String whereClause = "1=1";
    int[] ids = new Query(getCtx(), X_AD_Element.Table_Name, whereClause, getTrxName()).setOnlySelection(AD_PInstance_ID).setOrderBy(X_AD_Element.COLUMNNAME_AD_Element_ID).getIDs();
    assertEquals("Resulting number of elements differ", elements.size(), ids.length);
    for (int i = 0; i < elements.size(); i++) {
        int expected = elements.get(i);
        assertEquals("Element " + i + " not equals", expected, ids[i]);
    }
}
Also used : Query(org.compiere.model.Query) ArrayList(java.util.ArrayList)

Aggregations

Query (org.compiere.model.Query)210 ArrayList (java.util.ArrayList)49 BigDecimal (java.math.BigDecimal)25 Properties (java.util.Properties)22 MProduct (org.compiere.model.MProduct)20 AdempiereException (org.adempiere.exceptions.AdempiereException)12 MTable (org.compiere.model.MTable)12 MOrderLine (org.compiere.model.MOrderLine)8 MWarehouse (org.compiere.model.MWarehouse)8 PO (org.compiere.model.PO)8 MBPartner (org.compiere.model.MBPartner)6 MQuery (org.compiere.model.MQuery)6 MPPProductBOM (org.eevolution.model.MPPProductBOM)6 MColumn (org.compiere.model.MColumn)5 MLocation (org.compiere.model.MLocation)5 MPPProductBOMLine (org.eevolution.model.MPPProductBOMLine)5 SQLException (java.sql.SQLException)4 Timestamp (java.sql.Timestamp)4 DefaultMutableTreeNode (javax.swing.tree.DefaultMutableTreeNode)4 MClient (org.compiere.model.MClient)4