Package org.apache.empire.db

Examples of org.apache.empire.db.DBCommand


    public Options getDepartments()
    {
        if (departments==null)
        {
            SampleDB db = getDatabase();
            DBCommand cmd = db.createCommand();
            cmd.select(db.T_DEPARTMENTS.C_DEPARTMENT_ID);
            cmd.select(db.T_DEPARTMENTS.C_NAME);
            cmd.orderBy(db.T_DEPARTMENTS.C_NAME);
            departments = db.queryOptionList(cmd.getSelect(), getConnection());
        }
        return departments;
    }
View Full Code Here


    {
        SampleDB db = getDatabase();
        SampleDB.Employees EMP = db.T_EMPLOYEES;
        SampleDB.Departments DEP = db.T_DEPARTMENTS;
       
        DBCommand cmd = db.createCommand();
        cmd.select(EMP.C_EMPLOYEE_ID);
        cmd.select(C_FULL_NAME, EMP.C_GENDER, EMP.C_DATE_OF_BIRTH);
        cmd.select(C_DEPARTMENT);
        cmd.join  (DEP.C_DEPARTMENT_ID, EMP.C_DEPARTMENT_ID);

        // Set filter constraints
        SearchInfo si = getSearchInfo();
        if (si.getDepartmentId()!=null)
            cmd.where(EMP.C_DEPARTMENT_ID.is(si.getDepartmentId()));
        if (StringUtils.isValid( si.getFirstName()) )
            cmd.where(EMP.C_FIRSTNAME.likeUpper( si.getFirstName()+"%" )
                  .or(EMP.C_FIRSTNAME.is(null)));
        if (StringUtils.isValid( si.getLastName()) )
            cmd.where(EMP.C_LASTNAME.likeUpper( si.getLastName()+"%" ));
       
        cmd.orderBy(EMP.C_LASTNAME);
        cmd.orderBy(EMP.C_FIRSTNAME);
       
        // Init BeanList
        if (!employeeBeanList.initBeanList(cmd))
        {
            setActionError(employeeBeanList);
View Full Code Here

    }

    private boolean databaseExists(Connection conn)
    {
        // Check wether DB exists
        DBCommand cmd = db.createCommand();
        cmd.select(db.DEPARTMENTS.count());
        int deps = db.querySingleInt(cmd.getSelect(), -1, conn);
        return (deps >= 0);
    }
View Full Code Here

     * </PRE>
   */
  private static boolean databaseExists(Connection conn)
    {
    // Check wether DB exists
    DBCommand cmd = db.createCommand();
    cmd.select(db.DEPARTMENTS.count());
    // Check using "select count(*) from DEPARTMENTS"
    System.out.println("Checking whether table DEPARTMENTS exists (SQLException will be logged if not - please ignore) ...");
    return (db.querySingleInt(cmd.getSelect(), -1, conn) >= 0);
  }
View Full Code Here

   * Empties all Tables.
     * </PRE>
   */
  private static void clearDatabase(Connection conn)
    {
    DBCommand cmd = db.createCommand();
    // Delete all Employees (no constraints)
    db.executeSQL(cmd.getDelete(db.EMPLOYEES), conn);
    // Delete all Departments (no constraints)
    db.executeSQL(cmd.getDelete(db.DEPARTMENTS), conn);
  }
View Full Code Here

     * </PRE>
   */
  private static void queryRecords(Connection conn, QueryType queryType)
    {
      // Define the query
      DBCommand cmd = db.createCommand();
      // Define shortcuts for tables used - not necessary but convenient
      SampleDB.Employees   EMP = db.EMPLOYEES;
      SampleDB.Departments DEP = db.DEPARTMENTS;

      // The following expression concats lastname + ', ' + firstname
        DBColumnExpr EMPLOYEE_FULLNAME = EMP.LASTNAME.append(", ").append(EMP.FIRSTNAME).as("FULL_NAME");
       
        // The following expression extracts the extension number from the phone field
        // e.g. substr(PHONE_NUMBER, length(PHONE_NUMBER)-instr(reverse(PHONE_NUMBER), '-')+2) AS PHONE_EXTENSION
        // Hint: Since the reverse() function is not supported by HSQLDB there is special treatment for HSQL
        DBColumnExpr PHONE_LAST_DASH;
        if ( db.getDriver() instanceof DBDatabaseDriverHSql
            || db.getDriver() instanceof DBDatabaseDriverDerby
            || db.getDriver() instanceof DBDatabaseDriverH2)
             PHONE_LAST_DASH = EMP.PHONE_NUMBER.indexOf("-", EMP.PHONE_NUMBER.indexOf("-").plus(1)).plus(1); // HSQLDB only
        else PHONE_LAST_DASH = EMP.PHONE_NUMBER.length().minus(EMP.PHONE_NUMBER.reverse().indexOf("-")).plus(2)
        DBColumnExpr PHONE_EXT_NUMBER = EMP.PHONE_NUMBER.substring(PHONE_LAST_DASH).as("PHONE_EXTENSION");
       
        // DBColumnExpr genderExpr = cmd.select(EMP.GENDER.decode(EMP.GENDER.getOptions()).as(EMP.GENDER.getName()));
    // Select requried columns
    cmd.select(EMP.EMPLOYEE_ID, EMPLOYEE_FULLNAME);
    if(db.getDriver() instanceof DBDatabaseDriverPostgreSQL)
    {
      // postgres does not support the substring expression
      cmd.select(EMP.GENDER, EMP.PHONE_NUMBER);
    }else{
      cmd.select(EMP.GENDER, EMP.PHONE_NUMBER, PHONE_EXT_NUMBER);
     
    }
    cmd.select(DEP.NAME.as("DEPARTMENT"));
    cmd.select(DEP.BUSINESS_UNIT);
    cmd.join(EMP.DEPARTMENT_ID, DEP.DEPARTMENT_ID);
        // Set contraints and order
        cmd.where(EMP.LASTNAME.length().isGreaterThan(0));
        cmd.orderBy(EMP.LASTNAME);
        cmd.orderBy(EMP.FIRSTNAME);

    // Query Records and print output
    DBReader reader = new DBReader();
    try
        {
      // Open Reader
      System.out.println("Running Query:");
      System.out.println(cmd.getSelect());
      reader.open(cmd, conn);
      // Print output
      System.out.println("---------------------------------");
      switch(queryType)
      {
View Full Code Here

        if (owner==null || owner.length()==0)
            throw new InvalidArgumentException("owner", owner);
        // Database definition
        OracleSYSDatabase sysDB = new OracleSYSDatabase(this);
        // Check Columns
        DBCommand sysDBCommand = sysDB.createCommand();
        sysDBCommand.select(sysDB.CI.getColumns());
        sysDBCommand.where (sysDB.CI.C_OWNER.is(owner));
       
        OracleDataDictionnary dataDictionnary = new OracleDataDictionnary();
        DBReader rd = new DBReader();
        try
        {
View Full Code Here

   * Empties all Tables.
     * </PRE>
   */
  private static void clearDatabase(Connection conn, CompanyDB db)
    {
    DBCommand cmd = db.createCommand();
    // Delete all Employees (no constraints)
    db.executeSQL(cmd.getDelete(db.DEPARTMENT), conn);
  }
View Full Code Here

     * </PRE>
   */
  private static boolean databaseExists(Connection conn, CompanyDB db)
    {
    // Check whether DB exists
    DBCommand cmd = db.createCommand();
    cmd.select(db.DEPARTMENT.count());
    // Check using "select count(*) from DEPARTMENTS"
    System.out.println("Checking whether table DEPARTMENTS exists (SQLException will be logged if not - please ignore) ...");
    return (db.querySingleInt(cmd.getSelect(), -1, conn) >= 0);
  }
View Full Code Here

        try {
            DBDatabase db = getDatabase();
            if (db.getTables() == null || db.getTables().isEmpty()) {
                throw new AssertionError("There are no tables in this database!");
            }
            DBCommand cmd = db.createCommand();
            if (cmd == null) {
                throw new AssertionError("The DBCommand object is null.");
            }
            DBTable t = db.getTables().get(0);
            cmd.select(t.count());
            return (db.querySingleInt(cmd.getSelect(), -1, conn) >= 0);
        } catch (Exception e) {
            return false;
        }
    }
View Full Code Here

TOP

Related Classes of org.apache.empire.db.DBCommand

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.