Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   *  Copyright 2013-2014 Pavel Ponec
   *
   *  Licensed under the Apache License, Version 2.0 (the "License");
   *  you may not use this file except in compliance with the License.
   *  You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
  *  Unless required by applicable law or agreed to in writing, software
  *  distributed under the License is distributed on an "AS IS" BASIS,
  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  *  See the License for the specific language governing permissions and
  *  limitations under the License.
  */
 package org.ujorm.orm.template;
 
 import org.ujorm.Key;
The class for building any SQL statement using Ujorm Keys.
Example:
import static org.ujorm.orm.template.AliasTable.Build.*;
 public void example() {
     OrmHandler handler = createHandler();
     AliasTable<Order> order = handler.tableOf(Order.class, "order");
     AliasTable<Item> item = handler.tableOf(Item.class, "item");

     String sql
             = SELECT(order.column(Order.CREATED), item.column(Item.NOTE))
             + FROM (order, item)
             + WHERE(order.column(Order.ID), " = ", item.column(Item.ORDER));

     String sqlExpected = "SELECT order.CREATED, item.NOTE "
             + "FROM db1.ord_order order, db1.ord_item item "
             + "WHERE order.ID = item.fk_order";

     assertEquals(sqlExpected, sql);
 }

 

Author(s):
Pavel Ponec
 
 public class AliasTable<UJO extends OrmUjo> {

    
Default handler
 
     private final OrmHandler handler;
    
Meta table
 
     private final TableWrapper table;
 
     public AliasTable(Class<UJO> tableString aliasOrmHandler handler) {
         this(new TableWrapperImpl(handler.findTableModel(table), alias), handler);
     }
 
     protected AliasTable(TableWrapper tableOrmHandler handler) {
         this. = handler;
         this. = table;
     }

    
Returns SQL Dialect
 
     protected SqlDialect getDialect() {
         return .getModel().getDatabase().getDialect();
     }

    
Get table Alias
 
     public TableWrapper getTableModel() {
         return ;
     }

    
Returns Table witn Alias
 
     public String table() throws IllegalStateException {
         try {
             StringBuilder result = new StringBuilder(32);
             getDialect().printTableAliasDefinition(result);
             return result.toString();
         } catch (IOException e) {
             throw new IllegalStateException(e.getClass().getSimpleName(), e);
         }
     }

    
Print one column including alias
 
     public <T> String column(Key<UJO, T> keythrows IllegalStateException {
         return printColumn
                 ( findColumnModel(key)
                 , null
                 , new StringBuilder(32))
                 .toString();
     }

    
Returns one column including an default special alias name after the 'AS' phrase.
 
     public <T> String columnAs(Key<UJO, T> keythrows IllegalStateException {
         return columnAs(keynull);
    }

    
Returns one column including the required alias name after the 'AS' phrase.

Parameters:
key Related key
columnAlias an alias String or a Key instance for the key Name, the null value use an Key name.
Returns:
Returns one column including the required alias name after the 'AS' phrase.
Throws:
java.lang.IllegalStateException
    public <T> String columnAs(Key<UJO, T> keyCharSequence columnAliasthrows IllegalStateException {
        return printColumn
                ( findColumnModel(key)
                , columnAlias != null ? columnAlias : key.getName()
                , new StringBuilder(64)).toString();
    }

    
Call the column() method for all Keys of Ujorm
    public String allColumns() throws IllegalStateException {
        return allColumns(false);
    }

    
Call the column() method for all Keys of Ujorm
    public String allColumns(boolean includeKeyAliasthrows IllegalStateException {
        final StringBuilder result = new StringBuilder(128);
        for (ColumnWrapper column : this..getModel().getColumns()) {
            if (!column.getModel().isColumn()) {
                continue;
            }
            if (result.length() > 0) {
                result.append(", ");
            }
            printColumn(column.getModel(), includeKeyAlias ? column.getKey().getName() : nullresult);
        }
        return result.toString();
    }

    
Return the same result as table
    @Override
    public String toString() {
        return table();
    }
    // ------------ HELPER METHODS ------------

    
Returns one column including an default special alias after the 'AS' phrase.

Parameters:
key Related key
columnAlias an alias name or a Key instance, the null value use an Key name.
Returns:
Returns one column including an default special alias.
Throws:
java.lang.IllegalStateException
    protected StringBuilder printColumn(MetaColumn columnfinal CharSequence columnAliasStringBuilder outthrows IllegalStateException {
        try {
            getDialect().printColumnAlias(new ColumnWrapperImpl(column.getAlias()), out);
            if (columnAlias != null) {
                out.append(" AS ").append(columnAlias);
            }
            return out;
        } catch (IOException e) {
            throw new IllegalStateException(e.getClass().getSimpleName(), e);
        }
    }

    
Find Column Model or throw an IllegalArgumentException.
    protected MetaColumn findColumnModel(Key<UJO, ?> keythrows IllegalArgumentException {
        return (MetaColumn.findColumnModel((Keykeytrue);
    }
    // ------------ STATIC METHODS ------------

    
Create new Alias with required name
    public static <UJO extends OrmUjoAliasTable<UJO> of(Class<UJO> tableString aliasOrmHandler handler) {
        return new AliasTable<UJO>(tablealiashandler);
    }

    
Create new Alias with default name
    public static <UJO extends OrmUjoAliasTable<UJO> of(Class<UJO> tableOrmHandler handler) {
        return new AliasTable<UJO>(handler.findTableModel(table), handler);
    }
    // ------------ STATIC TOOLS ------------

    
Static building methods.
    public static final class Build {

        
No text separator
        private static final String NO_SEPARATOR = null;

        
Build SQL SELECT statement
        public static String SELECT(Object... params) {
            return "SELECT " + toText(", "params);
        }

        
Build SQL FROM phrase
        public static String FROM(Object... params) {
            return " FROM " + toText(", "params);
        }

        
Build SQL FROM phrase
        public static String INNER_JOIN(Object tableObject... conditions) {
            return " INNER JOIN " + table
                 + " ON " + toText(" "conditions) + " ";
        }

        
Build SQL WHERE phrase
        public static String WHERE(Object... params) {
            return " WHERE " + toText(params);
        }

        
Build any text with the required separator
        public static String toText(String separatorObject... params) {
            final StringBuilder sb = new StringBuilder(256);
            for (Object par : params) {
                if (separator != null && sb.length() > 0) {
                    sb.append(separator);
                }
                sb.append(par);
            }
            return sb.toString();
        }
    }
New to GrepCode? Check out our FAQ X