@Override
  public final <T> String evalSqlSelect(final Map<String, Object> pAddParam,
    final Class<T> pEntityClass) throws Exception {
    String tableName = pEntityClass.getSimpleName().toUpperCase();
    StringBuffer result = new StringBuffer("select ");
    StringBuffer joints = new StringBuffer();
    boolean isFirstField = true;
    TableSql tableSql = getTablesMap().get(pEntityClass.getSimpleName());
    if (tableSql == null) {
      throw new ExceptionWithCode(ExceptionWithCode.CONFIGURATION_MISTAKE,
        "where_is_no_table_def_for::" + pEntityClass.getSimpleName());
    }
    @SuppressWarnings("unchecked")
    Collection<String> fieldsNames = (Collection<String>) pAddParam.
      get(pEntityClass.getSimpleName() + "neededFields");
    if (fieldsNames == null) {
      fieldsNames = tableSql.getFieldsMap().keySet();
    }
    String currentLevelNm = "currentLevel";
    List<Integer> currentLevel = new ArrayList<Integer>();
    currentLevel.add(1); //top level entity
    pAddParam.put(currentLevelNm, currentLevel);
    String deepLevelNm = "deepLevel";
    ArrayList<Integer> deepLevel = new ArrayList<Integer>();
    deepLevel.add(2); //top level entity deep default
    pAddParam.put(deepLevelNm, deepLevel);
    for (String fieldName : fieldsNames) {
      FieldSql fieldSql = tableSql.getFieldsMap().get(fieldName);
      if (!fieldSql.getTypeField()
        .equals(ETypeField.DERIVED_FROM_COMPOSITE)) {
        if (fieldSql.getForeignEntity() == null) {
          if (isFirstField) {
            isFirstField = false;
          } else {
            result.append(", ");
          }
          String columnName = fieldName.toUpperCase();
          result.append(tableName + "." + columnName + " as " + columnName);
        } else {
          evalForeignSelect(pAddParam, fieldName, fieldSql, result, joints,
            pEntityClass.getSimpleName(), tableName, isFirstField);
          isFirstField = false;
        }
      }
    }
    pAddParam.remove(currentLevelNm);
    pAddParam.remove(deepLevelNm);
    result.append(" from " + tableName + joints);
    return result.toString();
  }
@ Override public final < T > String evalSqlSelect ( final Map < String , Object > pAddParam , final Class < T > pEntityClass ) throws Exception { String tableName = pEntityClass . getSimpleName ( ) . toUpperCase ( ) ; StringBuffer result = new StringBuffer ( "select " ) ; StringBuffer joints = new StringBuffer ( ) ; boolean isFirstField = true ; TableSql tableSql = getTablesMap ( ) . get ( pEntityClass . getSimpleName ( ) ) ; if ( tableSql == null ) { throw new ExceptionWithCode ( ExceptionWithCode . CONFIGURATION_MISTAKE , "where_is_no_table_def_for::" + pEntityClass . getSimpleName ( ) ) ; } @ SuppressWarnings ( "unchecked" ) Collection < String > fieldsNames = ( Collection < String > ) pAddParam . get ( pEntityClass . getSimpleName ( ) + "neededFields" ) ; if ( fieldsNames == null ) { fieldsNames = tableSql . getFieldsMap ( ) . keySet ( ) ; } String currentLevelNm = "currentLevel" ; List < Integer > currentLevel = new ArrayList < Integer > ( ) ; currentLevel . add ( 1 ) ; pAddParam . put ( currentLevelNm , currentLevel ) ; String deepLevelNm = "deepLevel" ; ArrayList < Integer > deepLevel = new ArrayList < Integer > ( ) ; deepLevel . add ( 2 ) ; pAddParam . put ( deepLevelNm , deepLevel ) ; for ( String fieldName : fieldsNames ) { FieldSql fieldSql = tableSql . getFieldsMap ( ) . get ( fieldName ) ; if ( ! fieldSql . getTypeField ( ) . equals ( ETypeField . DERIVED_FROM_COMPOSITE ) ) { if ( fieldSql . getForeignEntity ( ) == null ) { if ( isFirstField ) { isFirstField = false ; } else { result . append ( ", " ) ; } String columnName = fieldName . toUpperCase ( ) ; result . append ( tableName + "." + columnName + " as " + columnName ) ; } else { evalForeignSelect ( pAddParam , fieldName , fieldSql , result , joints , pEntityClass . getSimpleName ( ) , tableName , isFirstField ) ; isFirstField = false ; } } } pAddParam . remove ( currentLevelNm ) ; pAddParam . remove ( deepLevelNm ) ; result . append ( " from " + tableName + joints ) ; return result . toString ( ) ; }
<p>Evaluate common(without WHERE) SQL SELECT
statement for entity type. It's used externally
to make more complex query with additional joints and filters.</p>
@param pAddParam additional param, e.g. set of needed fields names,
deep of foreign classes, etc.
@param <T> entity type
@param pEntityClass entity class
@return String SQL DML query
@throws Exception - an exception