private String buildSQL(JDBCDeclaredQueryMetaData metadata) {
    StringBuffer sql = new StringBuffer(300);

    sql.append(SQLUtil.SELECT);
    if (metadata.isSelectDistinct()) {
      sql.append(SQLUtil.DISTINCT);
    }

    String alias = metadata.getAlias();
    String from = metadata.getFrom();
    String table;
    String selectList;
    if (metadata.getFieldName() == null) {
      // we are selecting a full entity
      table = this.entity.getQualifiedTableName();

      // get a list of all fields to be loaded
      // put pk fields in front
      String tableAlias = getTableAlias(alias, from, this.entity.getTableName());
      selectList =
          SQLUtil.getColumnNamesClause(
                  this.entity.getPrimaryKeyFields(), tableAlias, new StringBuffer(35))
              .toString();
    } else {
      // we are just selecting one field
      JDBCStoreManager2 manager = (JDBCStoreManager2) selectedField.getManager();
      table = manager.getEntityBridge().getQualifiedTableName();
      selectList =
          SQLUtil.getColumnNamesClause(
                  selectedField,
                  getTableAlias(alias, from, manager.getEntityBridge().getTableName()),
                  new StringBuffer())
              .toString();
    }
    sql.append(selectList);
    String additionalColumns = metadata.getAdditionalColumns();
    if (additionalColumns != null) {
      sql.append(additionalColumns);
    }
    sql.append(SQLUtil.FROM).append(table);
    if (alias != null) {
      sql.append(' ').append(alias);
    }
    if (from != null) {
      sql.append(' ').append(from);
    }

    String where = metadata.getWhere();
    if (where != null && where.trim().length() > 0) {
      sql.append(SQLUtil.WHERE).append(where);
    }

    String order = metadata.getOrder();
    if (order != null && order.trim().length() > 0) {
      sql.append(SQLUtil.ORDERBY).append(order);
    }

    String other = metadata.getOther();
    if (other != null && other.trim().length() > 0) {
      sql.append(' ').append(other);
    }
    return sql.toString();
  }
Esempio n. 2
0
  public void init() throws Exception {
    dataSource = manager.getDataSource(metadata.getDataSourceName());
    qualifiedTableName = SQLUtil.fixTableName(metadata.getDefaultTableName(), dataSource);
    int dotIndex = qualifiedTableName.indexOf('.');
    tableName = dotIndex == -1 ? qualifiedTableName : qualifiedTableName.substring(dotIndex + 1);

    // CMP fields
    loadCMPFields(metadata);

    // CMR fields
    loadCMRFields(metadata);

    // create locking field
    JDBCOptimisticLockingMetaData lockMetaData = metadata.getOptimisticLocking();
    if (lockMetaData != null && lockMetaData.getLockingField() != null) {
      JDBCOptimisticLockingMetaData.LockingStrategy strategy = lockMetaData.getLockingStrategy();
      JDBCCMPFieldMetaData versionMD = lockMetaData.getLockingField();

      versionField = getCMPFieldByName(versionMD.getFieldName());
      boolean hidden = versionField == null;
      switch (strategy) {
        case VERSION_COLUMN_STRATEGY:
          {
            if (hidden) versionField = new JDBCLongVersionFieldBridge(manager, versionMD);
            else versionField = new JDBCLongVersionFieldBridge((JDBCCMP2xFieldBridge) versionField);
            break;
          }
        case TIMESTAMP_COLUMN_STRATEGY:
          {
            if (hidden) versionField = new JDBCTimestampVersionFieldBridge(manager, versionMD);
            else
              versionField =
                  new JDBCTimestampVersionFieldBridge((JDBCCMP2xFieldBridge) versionField);
            break;
          }
        case KEYGENERATOR_COLUMN_STRATEGY:
          {
            if (hidden)
              versionField =
                  new JDBCKeyGenVersionFieldBridge(
                      manager, versionMD, lockMetaData.getKeyGeneratorFactory());
            else
              versionField =
                  new JDBCKeyGenVersionFieldBridge(
                      (JDBCCMP2xFieldBridge) versionField, lockMetaData.getKeyGeneratorFactory());
            break;
          }
      }
      if (hidden) addCMPField(versionField);
      else tableFields[versionField.getTableIndex()] = versionField;
    }

    // audit fields
    JDBCAuditMetaData auditMetaData = metadata.getAudit();
    if (auditMetaData != null) {
      JDBCCMPFieldMetaData auditField = auditMetaData.getCreatedPrincipalField();
      if (auditField != null) {
        createdPrincipalField = getCMPFieldByName(auditField.getFieldName());
        if (createdPrincipalField == null) {
          createdPrincipalField = new JDBCCMP2xFieldBridge(manager, auditField);
          addCMPField(createdPrincipalField);
        }
      } else {
        createdPrincipalField = null;
      }

      auditField = auditMetaData.getCreatedTimeField();
      if (auditField != null) {
        createdTimeField = getCMPFieldByName(auditField.getFieldName());
        if (createdTimeField == null) {
          createdTimeField =
              new JDBCCMP2xFieldBridge(manager, auditField, JDBCTypeFactory.EQUALS, false);
          addCMPField(createdTimeField);
        } else {
          // just to override state factory and check-dirty-after-get
          createdTimeField =
              new JDBCCMP2xFieldBridge(
                  (JDBCCMP2xFieldBridge) createdTimeField, JDBCTypeFactory.EQUALS, false);
          tableFields[createdTimeField.getTableIndex()] = createdTimeField;
        }
      } else {
        createdTimeField = null;
      }

      auditField = auditMetaData.getUpdatedPrincipalField();
      if (auditField != null) {
        updatedPrincipalField = getCMPFieldByName(auditField.getFieldName());
        if (updatedPrincipalField == null) {
          updatedPrincipalField = new JDBCCMP2xUpdatedPrincipalFieldBridge(manager, auditField);
          addCMPField(updatedPrincipalField);
        } else {
          updatedPrincipalField =
              new JDBCCMP2xUpdatedPrincipalFieldBridge(
                  (JDBCCMP2xFieldBridge) updatedPrincipalField);
          tableFields[updatedPrincipalField.getTableIndex()] = updatedPrincipalField;
        }
      } else {
        updatedPrincipalField = null;
      }

      auditField = auditMetaData.getUpdatedTimeField();
      if (auditField != null) {
        updatedTimeField = getCMPFieldByName(auditField.getFieldName());
        if (updatedTimeField == null) {
          updatedTimeField = new JDBCCMP2xUpdatedTimeFieldBridge(manager, auditField);
          addCMPField(updatedTimeField);
        } else {
          updatedTimeField =
              new JDBCCMP2xUpdatedTimeFieldBridge((JDBCCMP2xFieldBridge) updatedTimeField);
          tableFields[updatedTimeField.getTableIndex()] = updatedTimeField;
        }
      } else {
        updatedTimeField = null;
      }
    }

    // ejbSelect methods
    loadSelectors(metadata);
  }