Package org.apache.empire.db

Examples of org.apache.empire.db.DBCommand


     * </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 required 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 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

        emp.create(db.DATA);
        emp.setValue(db.DATA.DATA, new byte[]{1,2,3});
        emp.update(conn);
   
    // read a value
    DBCommand cmd = db.createCommand();
    cmd.select(db.DATA.DATA);
    DBReader reader = new DBReader();
    reader.open(cmd, conn);
    while(reader.moveNext()){
      byte[] value = (byte[]) reader.getValue(db.DATA.DATA);
      Assert.assertArrayEquals(new byte[]{1,2,3}, value);
View Full Code Here

   * Please note that in this case an error will appear in the log which can be ignored.
   */
  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"
   
    try{
      return (db.querySingleInt(cmd, -1, conn) >= 0);
    }catch(QueryFailedException ex){
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, -1, conn) >= 0);
        } catch (Exception e) {
            return false;
        }
    }
View Full Code Here

        // Iterate through all changed indexes
        DBColumn[] keyColumns = rowset.getKeyColumns();
        for (DBIndex idx : changed)
        {
            // Select all key columns
            DBCommand cmd = rowset.getDatabase().createCommand();
            cmd.select(keyColumns);
            // add constraints
            boolean allNull = true;
            DBColumn[] idxColumns = idx.getColumns();
            for (int i=0; i<idxColumns.length; i++)
            {   // Check if column has changed
                Object value = record.getValue(idxColumns[i]);
                cmd.where(idxColumns[i].is(value));
                if (value!=null)
                    allNull = false;
            }
            // Check whether all constraints are null
            if (allNull)
                continue;
            // Exclude current record
            if (record.isNew()==false)
            {   // add restriction
                if (keyColumns.length>1)
                {   // Multiple key columns
                    Object value = record.getValue(keyColumns[0]);
                    DBCompareExpr notExpr = keyColumns[0].is(value);
                    for (int i=1; i<keyColumns.length; i++)
                    {   // Check if column has changed
                        cmd.where(keyColumns[i].is(value));
                    }
                    cmd.where(notExpr.not());
                }
                else
                {   // Single key column
                    Object value = record.getValue(keyColumns[0]);
                    cmd.where(keyColumns[0].isNot(value));
                }
            }
            // Query now
            DBReader reader = new DBReader();
            try {
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

            throw new ObjectNotValidException(this);
        Set<DBColumn> joinColumns = new HashSet<DBColumn>();
        updateJoin.addReferencedColumns(joinColumns);
        // using
        buf.append("\r\nUSING ");
        DBCommand inner = this.clone();
        inner.clearSelect();
        inner.clearOrderBy();
        for (DBColumn jcol : joinColumns)
        {   // Select join columns
            if (jcol.getRowSet()!=table)
                inner.select(jcol);
        }
        for (DBSetExpr sex : set)
        {   // Select set expressions
            Object val = sex.getValue();
            if (val instanceof DBColumnExpr)
                inner.select(((DBColumnExpr)val));
        }
        // remove join (if not necessary)
        if (inner.hasConstraintOn(table)==false)
            inner.removeJoinsOn(table);
        // add SQL for inner statement
        inner.addSQL(buf, CTX_DEFAULT);
        // find the source table
        DBColumnExpr left  = updateJoin.getLeft();
        DBColumnExpr right = updateJoin.getRight();
        DBRowSet source = right.getUpdateColumn().getRowSet();
        if (source==table)
View Full Code Here

     * </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

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.