View Javadoc

    1   package sharin.sql.generator;
    2
    3   public class JoinInfo {
    4
    5       private final String sourceColumnName;
    6
    7       private final EntityInfo targetEntityInfo;
    8
    9       private final String targetTableAlias;
    10
    11      private final String targetColumnName;
    12
    13      private final String[] targetColumnAliases;
    14
    15      public JoinInfo(String sourceColumnName, EntityInfo targetEntityInfo,
    16              String targetTableAlias) {
    17
    18          this(sourceColumnName, targetEntityInfo, targetTableAlias,
    19                  targetEntityInfo.getIdColumnName());
    20      }
    21
    22      public JoinInfo(String sourceColumnName, EntityInfo targetEntityInfo,
    23              String targetTableAlias, String targetColumnName) {
    24
    25          this.sourceColumnName = sourceColumnName;
    26          this.targetEntityInfo = targetEntityInfo;
    27          this.targetTableAlias = targetTableAlias;
    28          this.targetColumnName = targetColumnName;
    29
    30          String[] propertyNames = targetEntityInfo.getPropertyNames();
    31          String[] targetColumnAliases = new String[propertyNames.length];
    32
    33          for (int i = 0; i < propertyNames.length; i++) {
    34              targetColumnAliases[i] = targetTableAlias + '.' + propertyNames[i];
    35          }
    36
    37          this.targetColumnAliases = targetColumnAliases;
    38      }
    39
    40      public void appendJoin(StringBuilder builder, String sourceTableAlias) {
    41          builder.append("LEFT JOIN ");
    42
    43          String targetTableName = targetEntityInfo.getTableName();
    44          builder.append(targetTableName);
    45
    46          if (!targetTableAlias.equals(targetTableName)) {
    47              builder.append(' ');
    48              builder.append(targetTableAlias);
    49          }
    50
    51          builder.append(" ON ");
    52          builder.append(sourceTableAlias);
    53          builder.append('.');
    54          builder.append(sourceColumnName);
    55          builder.append(" = ");
    56          builder.append(targetTableAlias);
    57          builder.append('.');
    58          builder.append(targetColumnName);
    59      }
    60
    61      public void appendField(StringBuilder builder, String propertyName) {
    62          builder.append(targetTableAlias);
    63          builder.append('.');
    64          builder.append(targetEntityInfo.getColumnName(propertyName));
    65          builder.append(" AS \"");
    66          builder.append(targetTableAlias);
    67          builder.append('.');
    68          builder.append(propertyName);
    69          builder.append('"');
    70      }
    71
    72      public String[] getTargetColumnAliases() {
    73          return targetColumnAliases;
    74      }
    75
    76      public String getTargetColumnName(String propertyName) {
    77          return targetTableAlias + '.'
    78                  + targetEntityInfo.getColumnName(propertyName);
    79      }
    80
    81      public boolean hasTargetColumnName(String propertyName) {
    82          return targetEntityInfo.hasColumnName(propertyName);
    83      }
    84
    85      public String getTargetTableAlias() {
    86          return targetTableAlias;
    87      }
    88  }