Package com.caucho.util

Examples of com.caucho.util.CharBuffer


   *
   * @return the load SQL.
   */
  public String generateLoadSQL(boolean fullSelect)
  {
    CharBuffer cb = CharBuffer.allocate();

    cb.append("select ");

    if (_isDistinct)
      cb.append(" distinct ");

    for (int i = 0; i < _resultList.size(); i++) {
      if (i != 0)
        cb.append(", ");

      AmberExpr expr = _resultList.get(i);

      if (_groupList == null && expr instanceof LoadEntityExpr)
        ((LoadEntityExpr) expr).generateSelect(cb, fullSelect);
      else
        expr.generateSelect(cb);
    }

    if (_hasFrom)
      cb.append(" from ");

    // jpa/114f: reorder from list for left outer join
    for (int i = 1; i < _fromList.size(); i++) {
      FromItem item = _fromList.get(i);

      if (item.isOuterJoin()) {
        JoinExpr join = item.getJoinExpr();

        if (join == null)
          continue;

        FromItem parent = join.getJoinParent();

        int index = _fromList.indexOf(parent);

        if (index < 0)
          continue;

        _fromList.remove(i);

        if (index < i)
          index++;

        _fromList.add(index, item);
      }
    }

    boolean hasJoinExpr = false;
    boolean isFirst = true;
    for (int i = 0; i < _fromList.size(); i++) {
      FromItem item = _fromList.get(i);

      // jpa/1178
      if (getParentQuery() != null) {
        ArrayList<FromItem> fromList = getParentQuery().getFromList();
        if (fromList != null) {
          if (fromList.contains(item)) {
            hasJoinExpr = true;
            continue;
          }
        }
      }

      if (isFirst) {
        isFirst = false;
      }
      else {
        if (item.isOuterJoin())
          cb.append(" left outer join ");
        else {
          cb.append(", ");

          if (item.getJoinExpr() != null)
            hasJoinExpr = true;
        }
      }

      cb.append(item.getTable().getName());
      cb.append(" ");
      cb.append(item.getName());

      if (item.getJoinExpr() != null && item.isOuterJoin()) {
        cb.append(" on ");
        item.getJoinExpr().generateJoin(cb);
      }

      EntityType entityType = item.getEntityType();

      // jpa/0l44, jpa/0l12
      /* XXX: jpa/0l47 move this to LoadExpr.generateSelect
      if (entityType != null) {
        AmberColumn discriminator = entityType.getDiscriminator();

        if (entityType instanceof SubEntityType &&
            discriminator != null) {
          // jpa/0l4b
          // XXX: needs to use parser.createTableName()
          FromItem discriminatorItem
            = new FromItem((EntityType) entityType,
                           discriminator.getTable(),
                           item.getName() + "_disc",
                           ++i);

          discriminatorItem.setQuery(this);

          _fromList.add(i, discriminatorItem);

          cb.append(", ");
          cb.append(discriminator.getTable().getName());
          cb.append(' ');
          cb.append(discriminatorItem.getName());
        }
      }
      */
    }

    // jpa/0l12
    // if (hasJoinExpr || _where != null) {

    boolean hasExpr = false;

    for (int i = 0; i < _fromList.size(); i++) {
      FromItem item = _fromList.get(i);

      AmberExpr expr = item.getJoinExpr();

      if (expr != null && ! item.isOuterJoin()) {
        if (hasExpr)
          cb.append(" and ");
        else {
          cb.append(" where ");
          hasExpr = true;
        }

        expr.generateJoin(cb);
      }

      EntityType entityType = item.getEntityType();

      // jpa/0l44
      if (entityType != null) {
        AmberColumn discriminator = entityType.getDiscriminator();

        // jpa/0l43
        if (entityType instanceof SubEntityType &&
            discriminator != null) {
          // jpa/0l12, jpa/0l4b

          if (item.getTable() == discriminator.getTable()) {
            if (hasExpr)
              cb.append(" and ");
            else {
              cb.append(" where ");
              hasExpr = true;
            }

            cb.append("(" + item.getName() + "." + discriminator.getName() + " = ");
            cb.append("'" + entityType.getDiscriminatorValue() + "')");
          }
        }
      }
    }

    if (_where != null) {
      if (hasExpr)
        cb.append(" and ");
      else {
        cb.append(" where ");
        hasExpr = true;
      }

      _where.generateWhere(cb);
    }

    if (_groupList != null) {
      cb.append(" group by ");

      for (int i = 0; i < _groupList.size(); i++) {
        if (i != 0)
          cb.append(", ");

        _groupList.get(i).generateSelect(cb);
      }
    }

    if (_having != null) {
      hasExpr = false;

      cb.append(" having ");

      /*
      for (int i = 0; i < _fromList.size(); i++) {
        FromItem item = _fromList.get(i);
        AmberExpr expr = item.getJoinExpr();

        if (expr != null && ! item.isOuterJoin()) {
          if (hasExpr)
            cb.append(" and ");
          hasExpr = true;

          expr.generateJoin(cb);
        }
      }
      */

      if (_having != null) {
        if (hasExpr)
          cb.append(" and ");
        hasExpr = true;

        _having.generateHaving(cb);
      }
    }

    if (_orderList != null) {
      cb.append(" order by ");

      for (int i = 0; i < _orderList.size(); i++) {
        if (i != 0)
          cb.append(", ");

        _orderList.get(i).generateSelect(cb);

        if (Boolean.FALSE.equals(_ascList.get(i)))
          cb.append(" desc");
      }
    }

    return cb.toString();
  }
View Full Code Here


  // private

  private void generateInternalWhere(CharBuffer cb,
                                     boolean select)
  {
    CharBuffer term = new CharBuffer();

    if (_fromItem != null) {

      if (select) {
        term.append(_fromItem.getName());
        term.append('.');
      }

      term.append(_column.getName());
    }
    else {

      if (select) {
        term.append(_parent.getChildFromItem().getName());
        term.append('.');
      }

      term.append(_column.getName());
    }

    AmberPersistenceUnit manager = _column.getTable().getAmberManager();

    JdbcMetaData metaData = manager.getMetaData();

    cb.append(metaData.generateBoolean(term.toString()));
  }
View Full Code Here

  /**
   * Generates SQL select.
   */
  public String generateSelectSQL(String table)
  {
    CharBuffer cb = new CharBuffer();

    for (int i = 0; i < _columns.size(); i++) {
      if (i != 0)
        cb.append(", ");

      if (table != null) {
        cb.append(table);
        cb.append(".");
      }

      cb.append(_columns.get(i).getName());
    }

    return cb.toString();
  }
View Full Code Here

  /**
   * Generates SQL select.
   */
  public String generateUpdateSQL()
  {
    CharBuffer cb = new CharBuffer();

    for (int i = 0; i < _columns.size(); i++) {
      if (i != 0)
        cb.append(", ");

      cb.append(_columns.get(i).getName() + "=?");
    }

    return cb.toString();
  }
View Full Code Here

  /**
   * Generates SQL match.
   */
  public String generateMatchArgSQL(String table)
  {
    CharBuffer cb = new CharBuffer();

    for (int i = 0; i < _columns.size(); i++) {
      if (i != 0)
        cb.append(" and ");

      if (table != null) {
        cb.append(table);
        cb.append(".");
      }

      cb.append(_columns.get(i).getName());
      cb.append("=?");
    }

    return cb.toString();
  }
View Full Code Here

   */
  public String generateJoin(String sourceTable,
                             String targetTable,
                             boolean isArg)
  {
    CharBuffer cb = new CharBuffer();

    cb.append('(');

    for (int i = 0; i < _columns.size(); i++) {
      ForeignColumn column = _columns.get(i);

      if (i != 0)
        cb.append(" and ");

      cb.append(sourceTable);
      cb.append('.');
      cb.append(column.getName());

      cb.append(" = ");

      cb.append(targetTable);

      if (isArg)
        continue;

      cb.append('.');
      cb.append(column.getTargetColumn().getName());
    }

    cb.append(')');

    return cb.toString();
  }
View Full Code Here

    // Implemented for jpa/10cb

    if (manyToOneJoin._columns.size() != _columns.size())
      return "";

    CharBuffer cb = new CharBuffer();

    cb.append('(');

    for (int i = 0; i < _columns.size(); i++) {
      ForeignColumn column = _columns.get(i);
      ForeignColumn otherColumn = manyToOneJoin._columns.get(i);

      if (i != 0)
        cb.append(" and ");

      cb.append(sourceTable1);
      cb.append('.');
      cb.append(column.getName());

      cb.append(" = ");

      cb.append(sourceTable2);

      cb.append('.');
      cb.append(otherColumn.getName());
    }

    cb.append(')');

    return cb.toString();
  }
View Full Code Here

   * @param targetTable the SQL table name for the target
   */
  public String generateWhere(String sourceTable,
                              String targetTable)
  {
    CharBuffer cb = new CharBuffer();

    cb.append('(');

    for (int i = 0; i < _columns.size(); i++) {
      ForeignColumn column = _columns.get(i);

      if (i != 0)
        cb.append(" and ");

      if (! column.isNotNull()) {

        if (sourceTable == null)
          cb.append('?');
        else {
          cb.append(sourceTable);
          cb.append('.');
          cb.append(column.getName());
        }

        cb.append(" is not null ");
      }

      cb.append(" and ");

      // jpa/10c9
      if (sourceTable == null) {
        cb.append('?');
      }
      else {
        cb.append(sourceTable);
        cb.append('.');
        cb.append(column.getName());
      }

      cb.append(" = ");

      cb.append(targetTable);
      cb.append('.');
      cb.append(column.getTargetColumn().getName());
    }

    cb.append(')');

    return cb.toString();
  }
View Full Code Here

      // ejb/06c5 vs jpa/0h60
      // jpa/0h60, the application should be responsible for deleting
      // the incoming links even when there are FK constraints.
      if (! (isJPA || isSourceCascadeDelete())) {
        CharBuffer cb = new CharBuffer();

        cb.append("update " + sourceTable + " set ");

        ArrayList<ForeignColumn> columns = getColumns();

        for (int i = 0; i < columns.size(); i++) {
          if (i != 0)
            cb.append (", ");

          cb.append(columns.get(i).getName() + "=null");
        }

        cb.append(" where ");

        for (int i = 0; i < columns.size(); i++) {
          if (i != 0)
            cb.append (" and ");

          cb.append(columns.get(i).getName() + "=?");
        }

        // See catch (Exception) below.
        sql = cb.toString();

        pstmt = aConn.prepareStatement(sql);

        entity.__caucho_setKey(pstmt, 1);

        pstmt.executeUpdate();

        aConn.addCompletion(_sourceTable.getUpdateCompletion());
      }
      else if (_sourceTable.isCascadeDelete()) {
        // if the link cascades deletes to the source and the source
        // table also has cascade deletes, then we need to load the
        // target entities and delete them recursively
        //
        // in theory, this could cause a loop, but we're ignoring that
        // case for now

        EntityType entityType = (EntityType) _sourceTable.getType();

        CharBuffer cb = new CharBuffer();

        cb.append("select ");
        cb.append(entityType.getId().generateSelect("o"));
        cb.append(" from " + sourceTable + " o");
        cb.append(" where ");

        ArrayList<ForeignColumn> columns = getColumns();

        for (int i = 0; i < columns.size(); i++) {
          if (i != 0)
            cb.append (" and ");

          cb.append(columns.get(i).getName() + "=?");
        }

        // See catch (Exception) below.
        sql = cb.toString();

        pstmt = aConn.prepareStatement(sql);

        entity.__caucho_setKey(pstmt, 1);

        ArrayList<Object> proxyList = new ArrayList<Object>();

        rs = pstmt.executeQuery();
        while (rs.next()) {
          proxyList.add(entityType.getHome().loadLazy(aConn, rs, 1));
        }
        rs.close();

        for (Object obj : proxyList) {
          entityType.getHome().getEntityFactory().delete(aConn, obj);
        }
      } // jpa/0i5e vs. jpa/0h25, jpa/0s2d
      else if ((! isJPA) || (isOwner && (_sourceTable.getType() == null))) {
        CharBuffer cb = new CharBuffer();

        cb.append("delete from " + sourceTable +
                  " where ");

        ArrayList<ForeignColumn> columns = getColumns();

        for (int i = 0; i < columns.size(); i++) {
          if (i != 0)
            cb.append (" and ");

          cb.append(columns.get(i).getName() + "=?");
        }

        // See catch (Exception) below.
        sql = cb.toString();

        pstmt = aConn.prepareStatement(sql);

        entity.__caucho_setKey(pstmt, 1);
View Full Code Here

  /**
   * Generates the SQL to create the table.
   */
  private String generateCreateTableSQL(AmberPersistenceUnit amberPersistenceUnit)
  {
    CharBuffer cb = new CharBuffer();

    cb.append("create table " + getName() + " (");

    boolean hasColumn = false;
    for (AmberColumn column : _columns) {
      String columnSQL = column.generateCreateTableSQL(amberPersistenceUnit);

      if (columnSQL == null) {
      }
      else if (! hasColumn) {
        hasColumn = true;
        cb.append("\n  " + columnSQL);
      }
      else {
        cb.append(",\n  " + columnSQL);
      }
    }

    cb.append("\n)");

    return cb.close();
  }
View Full Code Here

TOP

Related Classes of com.caucho.util.CharBuffer

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.