Exemplo n.º 1
0
 public CQueryPredicates(Binder binder, OrmQueryRequest<?> request) {
   this.binder = binder;
   this.request = request;
   this.query = request.getQuery();
   this.bindParams = query.getBindParams();
   this.idValue = query.getId();
 }
Exemplo n.º 2
0
  /** Convert named parameters into an OrderedList. */
  private void buildBindWhereRawSql(boolean buildSql, boolean parseRaw, DeployParser parser) {
    if (buildSql || bindParams != null) {
      whereRawSql = buildWhereRawSql();
      boolean hasRaw = !"".equals(whereRawSql);
      if (hasRaw && parseRaw) {
        // parse with encrypted property awareness. This means that if we have
        // an encrypted property we will insert special named parameter place
        // holders for binding the encryption key values
        parser.setEncrypted(true);
        whereRawSql = parser.parse(whereRawSql);
        parser.setEncrypted(false);
      }

      if (bindParams != null) {
        if (hasRaw) {
          whereRawSql =
              BindParamsParser.parse(bindParams, whereRawSql, request.getBeanDescriptor());

        } else if (query.isRawSql() && !buildSql) {
          // RawSql query hit cached query plan. Need to convert
          // named parameters into positioned parameters so that
          // the named parameters are bound
          RawSql.Sql sql = query.getRawSql().getSql();
          String s = sql.isParsed() ? sql.getPreWhere() : sql.getUnparsedSql();
          if (bindParams.requiresNamedParamsPrepare()) {
            BindParamsParser.parse(bindParams, s);
          }
        }
      }
    }
  }
  /** Create the Sql select based on the request. */
  public CQueryFetchIds(OrmQueryRequest<?> request, CQueryPredicates predicates, String sql) {

    this.request = request;
    this.query = request.getQuery();
    this.sql = sql;
    this.maxRows = query.getMaxRows();

    query.setGeneratedSql(sql);

    this.desc = request.getBeanDescriptor();
    this.predicates = predicates;
  }
 public DLoadContext(
     SpiEbeanServer ebeanServer,
     BeanDescriptor<?> rootDescriptor,
     Boolean readOnly,
     SpiQuery<?> query) {
   this(
       ebeanServer,
       rootDescriptor,
       readOnly,
       Boolean.FALSE.equals(query.isUseBeanCache()),
       query.getParentNode(),
       query.getAutoFetchManager() != null);
 }
Exemplo n.º 5
0
 private String buildWhereRawSql() {
   // this is the where part of a OQL query which
   // may contain bind parameters...
   String whereRaw = query.getRawWhereClause();
   if (whereRaw == null) {
     whereRaw = "";
   }
   // add any additional stuff to the where clause
   String additionalWhere = query.getAdditionalWhere();
   if (additionalWhere != null) {
     whereRaw += additionalWhere;
   }
   return whereRaw;
 }
  /**
   * Remove the +query and +lazy secondary queries and register them with their appropriate
   * LoadBeanContext or LoadManyContext.
   *
   * <p>The parts of the secondary queries are removed and used by LoadBeanContext/LoadManyContext
   * to build the appropriate queries.
   */
  public void registerSecondaryQueries(SpiQuery<?> query) {

    secQuery = query.removeQueryJoins();
    if (secQuery != null) {
      for (int i = 0; i < secQuery.size(); i++) {
        OrmQueryProperties props = secQuery.get(i);
        registerSecondaryQuery(props);
      }
    }

    List<OrmQueryProperties> lazyQueries = query.removeLazyJoins();
    if (lazyQueries != null) {
      for (int i = 0; i < lazyQueries.size(); i++) {
        OrmQueryProperties lazyProps = lazyQueries.get(i);
        registerSecondaryQuery(lazyProps);
      }
    }
  }
Exemplo n.º 7
0
  /** This combines the sql from named/positioned parameters and expressions. */
  private void prepare(boolean buildSql, boolean parseRaw, DeployParser deployParser) {

    buildBindWhereRawSql(buildSql, parseRaw, deployParser);
    buildBindHavingRawSql(buildSql, parseRaw, deployParser);

    SpiExpressionList<?> whereExp = query.getWhereExpressions();
    if (whereExp != null) {
      DefaultExpressionRequest whereReq = new DefaultExpressionRequest(request, deployParser);
      whereExprBindValues = whereExp.buildBindValues(whereReq);
      if (buildSql) {
        whereExprSql = whereExp.buildSql(whereReq);
      }
    }

    BeanPropertyAssocMany<?> manyProperty = request.getManyProperty();
    if (manyProperty != null) {
      OrmQueryProperties chunk = query.getDetail().getChunk(manyProperty.getName(), false);
      SpiExpressionList<?> filterMany = chunk.getFilterMany();
      if (filterMany != null) {
        DefaultExpressionRequest filterReq = new DefaultExpressionRequest(request, deployParser);
        filterManyExprBindValues = filterMany.buildBindValues(filterReq);
        if (buildSql) {
          filterManyExprSql = filterMany.buildSql(filterReq);
        }
      }
    }

    // having expression
    SpiExpressionList<?> havingExpr = query.getHavingExpressions();
    if (havingExpr != null) {
      DefaultExpressionRequest havingReq = new DefaultExpressionRequest(request, deployParser);
      havingExprBindValues = havingExpr.buildBindValues(havingReq);
      if (buildSql) {
        havingExprSql = havingExpr.buildSql(havingReq);
      }
    }

    if (buildSql) {
      parsePropertiesToDbColumns(deployParser);
    }
  }
Exemplo n.º 8
0
 private void buildBindHavingRawSql(
     boolean buildSql, boolean parseRaw, DeployParser deployParser) {
   if (buildSql || bindParams != null) {
     // having clause with named parameters...
     havingRawSql = query.getAdditionalHaving();
     if (parseRaw) {
       havingRawSql = deployParser.parse(havingRawSql);
     }
     if (havingRawSql != null && bindParams != null) {
       // convert and order named parameters if required
       havingNamedParams = BindParamsParser.parseNamedParams(bindParams, havingRawSql);
       havingRawSql = havingNamedParams.getPreparedSql();
     }
   }
 }
  /** Log the FindMany to the transaction log. */
  private void logFindManySummary(CQuery<?> q) {

    SpiQuery<?> query = q.getQueryRequest().getQuery();
    String loadMode = query.getLoadMode();
    String loadDesc = query.getLoadDescription();
    String lazyLoadProp = query.getLazyLoadProperty();
    ObjectGraphNode node = query.getParentNode();

    String originKey;
    if (node == null || node.getOriginQueryPoint() == null) {
      originKey = null;
    } else {
      originKey = node.getOriginQueryPoint().getKey();
    }

    StringBuilder msg = new StringBuilder(200);
    msg.append("FindMany ");
    if (loadMode != null) {
      msg.append("mode[").append(loadMode).append("] ");
    }
    msg.append("type[").append(q.getBeanName()).append("] ");
    if (query.isAutofetchTuned()) {
      msg.append("tuned[true] ");
    }
    if (originKey != null) {
      msg.append("origin[").append(originKey).append("] ");
    }
    if (lazyLoadProp != null) {
      msg.append("lazyLoadProp[").append(lazyLoadProp).append("] ");
    }
    if (loadDesc != null) {
      msg.append("load[").append(loadDesc).append("] ");
    }
    msg.append("exeMicros[").append(q.getQueryExecutionTimeMicros());
    msg.append("] rows[").append(q.getLoadedRowDetail());
    msg.append("] name[").append(q.getName());
    msg.append("] predicates[").append(q.getLogWhereSql());
    msg.append("] bind[").append(q.getBindLog()).append("]");

    q.getTransaction().logSummary(msg.toString());
  }
Exemplo n.º 10
0
  public String bind(DataBind dataBind) throws SQLException {

    StringBuilder bindLog = new StringBuilder();

    if (query.isVersionsBetween() && binder.isBindAsOfWithFromClause()) {
      // sql2011 based versions between timestamp syntax
      Timestamp start = query.getVersionStart();
      Timestamp end = query.getVersionEnd();
      bindLog.append("between ").append(start).append(" and ").append(end);
      binder.bindObject(dataBind, start);
      binder.bindObject(dataBind, end);
      bindLog.append(", ");
    }

    List<String> historyTableAlias = query.getAsOfTableAlias();
    if (historyTableAlias != null && binder.isBindAsOfWithFromClause()) {
      // bind the asOf value for each table alias as part of the from/join clauses
      // there is one effective date predicate per table alias
      Timestamp asOf = query.getAsOf();
      bindLog.append("asOf ").append(asOf);
      for (int i = 0; i < historyTableAlias.size() * binder.getAsOfBindCount(); i++) {
        binder.bindObject(dataBind, asOf);
      }
      bindLog.append(", ");
    }

    if (idValue != null) {
      // this is a find by id type query...
      request.getBeanDescriptor().bindId(dataBind, idValue);
      bindLog.append(idValue);
    }

    if (bindParams != null) {
      // bind named and positioned parameters...
      binder.bind(bindParams, dataBind, bindLog);
    }

    if (whereExprBindValues != null) {
      for (int i = 0; i < whereExprBindValues.size(); i++) {
        Object bindValue = whereExprBindValues.get(i);
        bindValue = binder.bindObject(dataBind, bindValue);
        if (i > 0 || idValue != null) {
          bindLog.append(",");
        }
        bindLog.append(bindValue);
      }
    }

    if (filterManyExprBindValues != null) {
      for (int i = 0; i < filterManyExprBindValues.size(); i++) {
        Object bindValue = filterManyExprBindValues.get(i);
        bindValue = binder.bindObject(dataBind, bindValue);
        if (i > 0 || idValue != null) {
          bindLog.append(",");
        }
        bindLog.append(bindValue);
      }
    }

    if (historyTableAlias != null && !binder.isBindAsOfWithFromClause()) {
      // bind the asOf value for each table alias after all the normal predicates
      // there is one effective date predicate per table alias
      Timestamp asOf = query.getAsOf();
      bindLog.append(" asOf ").append(asOf);
      for (int i = 0; i < historyTableAlias.size() * binder.getAsOfBindCount(); i++) {
        binder.bindObject(dataBind, asOf);
      }
    }

    if (havingNamedParams != null) {
      // bind named parameters in having...
      bindLog.append(" havingNamed ");
      binder.bind(havingNamedParams.list(), dataBind, bindLog);
    }

    if (havingExprBindValues != null) {
      // bind having expression...
      bindLog.append(" having ");
      for (int i = 0; i < havingExprBindValues.size(); i++) {
        Object bindValue = havingExprBindValues.get(i);
        bindValue = binder.bindObject(dataBind, bindValue);
        if (i > 0) {
          bindLog.append(",");
        }
        bindLog.append(bindValue);
      }
    }

    return bindLog.toString();
  }
Exemplo n.º 11
0
 /** Return the mode of the query of if null return CURRENT mode. */
 public static TemporalMode of(SpiQuery<?> query) {
   return (query != null) ? query.getTemporalMode() : TemporalMode.CURRENT;
 }
Exemplo n.º 12
0
  /** Execute the query returning the row count. */
  public BeanIdList findIds() throws SQLException {

    long startNano = System.nanoTime();

    try {
      // get the list that we are going to put the id's into.
      // This was already set so that it is available to be
      // read by other threads (it is a synchronised list)
      List<Object> idList = query.getIdList();
      if (idList == null) {
        // running in foreground thread (not FutureIds query)
        idList = Collections.synchronizedList(new ArrayList<Object>());
        query.setIdList(idList);
      }

      BeanIdList result = new BeanIdList(idList);

      SpiTransaction t = request.getTransaction();
      Connection conn = t.getInternalConnection();
      pstmt = conn.prepareStatement(sql);

      if (query.getBufferFetchSizeHint() > 0) {
        pstmt.setFetchSize(query.getBufferFetchSizeHint());
      }

      if (query.getTimeout() > 0) {
        pstmt.setQueryTimeout(query.getTimeout());
      }

      bindLog = predicates.bind(new DataBind(pstmt));

      ResultSet rset = pstmt.executeQuery();
      dataReader = new RsetDataReader(rset);

      boolean hitMaxRows = false;
      boolean hasMoreRows = false;
      rowCount = 0;

      DbReadContext ctx = new DbContext();

      while (rset.next()) {
        Object idValue = desc.getIdBinder().read(ctx);
        idList.add(idValue);
        // reset back to 0
        dataReader.resetColumnPosition();
        rowCount++;

        if (maxRows > 0 && rowCount == maxRows) {
          hitMaxRows = true;
          hasMoreRows = rset.next();
          break;
        }
      }

      if (hitMaxRows) {
        result.setHasMore(hasMoreRows);
      }

      long exeNano = System.nanoTime() - startNano;
      executionTimeMicros = (int) exeNano / 1000;

      return result;

    } finally {
      close();
    }
  }