Examples of DBCommand


Examples of org.apache.empire.db.DBCommand

     * </PRE>
   */
  private static boolean databaseExists(Connection conn)
    {
    // Check whether 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, -1, conn) >= 0);
  }
View Full Code Here

Examples of org.apache.empire.db.DBCommand

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

Examples of org.apache.empire.db.DBCommand

     * </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 required columns
        cmd.select(EMP.EMPLOYEE_ID, EMPLOYEE_FULLNAME);
        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 constraints and order
        cmd.where(EMP.LASTNAME.length().isGreaterThan(0));
        cmd.orderBy(EMP.LASTNAME, EMP.FIRSTNAME);

        /*
        // Example for limitRows() and skipRows()
        if (db.getDriver().isSupported(DBDriverFeature.QUERY_LIMIT_ROWS))
        {  // set maximum number of rows
          cmd.limitRows(20);
            if (db.getDriver().isSupported(DBDriverFeature.QUERY_SKIP_ROWS))
                cmd.skipRows(1);
        }
        */
       
    // 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

Examples of org.apache.empire.db.DBCommand

        TEmployees EMP = getDatabase().T_EMPLOYEES;

        DBColumnExpr FULL_NAME = EMP.LAST_NAME.append(", ").append(EMP.FIRST_NAME).as("NAME");
        DBColumnExpr DEPARTMENT = DEP.NAME.as("DEPARTMENT");

        DBCommand queryCmd = createQueryCommand();

        queryCmd.select(EMP.EMPLOYEE_ID, FULL_NAME);
        queryCmd.select(EMP.GENDER, EMP.DATE_OF_BIRTH, EMP.RETIRED);
        // queryCmd.select(EMP.RETIRED.decode(true, "X", "-"));
        queryCmd.select(DEPARTMENT);

        queryCmd.join(DEP.DEPARTMENT_ID, EMP.DEPARTMENT_ID);
        queryCmd.orderBy(EMP.FIRST_NAME);
       
        addAllConstraints(queryCmd);

        employees.initItems(queryCmd);
    }
View Full Code Here

Examples of org.apache.empire.db.DBCommand

    public Options getDepartmentOptions()
    {
      TDepartments DEP = getDatabase().T_DEPARTMENTS;

      DBCommand queryCmd = createQueryCommand();
      queryCmd.select(DEP.DEPARTMENT_ID,DEP.NAME);
     
      SampleDB db = getDatabase();
        return db.queryOptionList(queryCmd, getConnection());
    }
View Full Code Here

Examples of org.apache.empire.db.DBCommand

     *
     * @param queryCmd
     */
    public final void initItems(DBCommand queryCmd, int pageSize)
    {
        DBCommand countCmd = queryCmd.clone();
        initItems(queryCmd, countCmd, 0);
    }
View Full Code Here

Examples of org.apache.empire.db.DBCommand

        // DBReader
        BeanListTableInfo lti = (BeanListTableInfo) getTableInfo();
        DBReader r = new DBReader();
        try
        { // Check command
            DBCommand queryCmd = lti.getQueryCmd();
            if (queryCmd == null)
                throw new ObjectNotValidException(this);

            boolean loadPageFromPosition = lti.isValid() && lti.isAllowPagination();
            lti.setValid(false);

            if (lti.isSortOrderChanged())
            { // Set Sort order
                setOrderBy(queryCmd);
                lti.setSortOrderChanged(false);
            }
           
            int position = 0;
            int skipRows = 0;
            int maxItems = maxItemCount;
            if (loadPageFromPosition)
            {   // detect position
                position = lti.getPosition();
                if (position > lti.getItemCount() - lti.getPageSize())
                { // position > count of entries is not possible, set to max
                    position = lti.getItemCount() - lti.getPageSize();
                }
                if (position < 0)
                { // position < 0 is not possible, set to 0
                    position = 0;
                }
                // maxItems
                maxItems = lti.getPageSize();
                skipRows = position;
                // constraint
                DBDatabaseDriver driver = queryCmd.getDatabase().getDriver();
                if (driver.isSupported(DBDriverFeature.QUERY_LIMIT_ROWS))
                {   // let the database limit the rows
                    if (skipRows>0 && driver.isSupported(DBDriverFeature.QUERY_SKIP_ROWS))
                    {   // let the database skip the rows
                        queryCmd.skipRows(skipRows);
                        skipRows = 0;
                    }
                    queryCmd.limitRows(skipRows+maxItems);
                }
            }

            // DBReader.open immer nur innerhalb eines try {} finally {} blocks!
            r.open(queryCmd, getConnection(queryCmd));
View Full Code Here

Examples of org.apache.empire.db.DBCommand

    @Override
    public DBCommand getItemQueryCmd()
    {
        BeanListTableInfo lti = (BeanListTableInfo) getTableInfo();
        DBCommand cmd = lti.getQueryCmd().clone();
       
        Set<Object[]> items = getSelectedItemKeys();
        if (items.size()>0)
        {
            DBColumn[] pk = rowset.getKeyColumns();
            DBColumnExpr keyExpr = pk[0];
          
            for (int i=1; i<pk.length; i++)
            {
                keyExpr = keyExpr.append(pk[i]);
            }
           
            String[] keys = new String[items.size()];
            int i = 0;

            for (Object[] item : items)
            {
                keys[i++] = StringUtils.arrayToString(item, "");
            }
            if (isInvertSelection())
                cmd.where(keyExpr.notIn(keys));
            else
                cmd.where(keyExpr.in(keys));
        }
        // clear previous settings without the where causes
        cmd.clearSelect();
        cmd.clearGroupBy();
        return cmd;
    }
View Full Code Here

Examples of org.apache.empire.db.DBCommand

    MyDB db = MyDB.get();
    Employees EMP = db.EMPLOYEES;

    DBDatabaseDriver driver = new DBDatabaseDriverHSql();
    db.open(driver, conn);
    DBCommand cmd = db.createCommand();
    cmd.select(EMP.EMPLOYEE_ID, EMP.FIRSTNAME);

    int rowCount = 0;
    DBReader reader = new DBReader();
    try {
      System.err.println(cmd.getSelect());
      reader.open(cmd, conn);
      while (reader.moveNext()) {
        rowCount++;
        System.out.println(reader.getString(EMP.EMPLOYEE_ID) + "\t" + reader.getString(EMP.FIRSTNAME));
      }
View Full Code Here

Examples of org.apache.empire.db.DBCommand

    public Options getFieldOptions(DBColumn column)
    {
        if (column.equals(T.DEPARTMENT_ID))
        {
            SampleDB db = (SampleDB) getDatabase();
            DBCommand cmd = db.createCommand();
            cmd.select(db.T_DEPARTMENTS.DEPARTMENT_ID);
            cmd.select(db.T_DEPARTMENTS.NAME);
            cmd.orderBy(db.T_DEPARTMENTS.NAME);
            return db.queryOptionList(cmd, SampleUtils.getConnection());
        }
        // base class implementation
        return super.getFieldOptions(column);
    }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. 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.