Package com.db4o.query

Examples of com.db4o.query.Query


    ObjectSet result = query.execute();
    listResult(result);
  }

  public static void retrieveByComparison(ObjectContainer db) {
    Query query = db.query();
    query.constrain(Pilot.class);
    query.descend("points").constrain(new Integer(99)).greater();
    ObjectSet result = query.execute();
    listResult(result);
  }
View Full Code Here


  }

  public static void retrieveByDefaultFieldValue(ObjectContainer db) {
    Pilot somebody = new Pilot("Somebody else", 0);
    db.set(somebody);
    Query query = db.query();
    query.constrain(Pilot.class);
    query.descend("points").constrain(new Integer(0));
    ObjectSet result = query.execute();
    listResult(result);
    db.delete(somebody);
  }
View Full Code Here

    listResult(result);
    db.delete(somebody);
  }

  public static void retrieveSorted(ObjectContainer db) {
    Query query = db.query();
    query.constrain(Pilot.class);
    query.descend("name").orderAscending();
    ObjectSet result = query.execute();
    listResult(result);
    query.descend("name").orderDescending();
    result = query.execute();
    listResult(result);
  }
View Full Code Here

  @Override
  public <E extends IEntity> List<E> findEntities(Criteria<E> criteria, final Sorting sorting)
      throws InvalidCriteriaException, DataAccessException {
    if(criteria == null) throw new InvalidCriteriaException("No criteria specified.");

    final Query query = getDb4oTemplate().query();

    if(criteria.getCriteriaType().isQuery()) {
      if(nqt == null) throw new InvalidCriteriaException("No db4o named query translator specified.");
      nqt.translateNamedQuery(criteria.getNamedQueryDefinition(), criteria.getQueryParams(), query);
    }
    else {
      query.constrain(criteria.getEntityClass());
      final CriterionGroup pg = criteria.getPrimaryGroup();
      if(pg != null && pg.isSet()) {
        for(final ICriterion ic : pg) {
          if(ic.isGroup()) throw new InvalidCriteriaException("Nested criterion groups are not supported");
          if(!ic.isSet()) throw new InvalidCriteriaException("criterion not set");
          final Criterion ctn = (Criterion) ic;
          final Object checkValue = ctn.getValue();
          final String pname = ctn.getPropertyName();

          Query pquery;
          if(pname.indexOf('.') > 0) {
            pquery = query;
            // descend one time for each node in the pname (which may be a dot
            // notated property path)
            final PropertyPath path = new PropertyPath(pname);
            for(final String node : path.nodes()) {
              pquery = pquery.descend(node);
            }
          }
          else {
            pquery = query.descend(pname);
          }

          switch(ctn.getComparator()) {
            case BETWEEN: {
              Object min, max;
              if(checkValue instanceof NumberRange) {
                final NumberRange range = (NumberRange) checkValue;
                min = range.getMinimumNumber();
                max = range.getMaximumNumber();
              }
              else if(checkValue instanceof DateRange) {
                final DateRange range = (DateRange) checkValue;
                min = range.getStartDate();
                max = range.getEndDate();
              }
              else {
                // presume an object array
                final Object[] oarr = (Object[]) checkValue;
                min = oarr[0];
                max = oarr[1];
              }
              pquery.constrain(min).greater().equal().or(pquery.constrain(max).smaller().equal());
              break;
            }
            case CONTAINS:
              pquery.constrain(checkValue).contains();
              break;
            case ENDS_WITH:
              pquery.constrain(checkValue).endsWith(ctn.isCaseSensitive());
              break;
            case EQUALS:
              if(!ctn.isCaseSensitive())
                throw new InvalidCriteriaException("Case insensitive equals checking is currently not supported");
              pquery.constrain(checkValue);
              break;
            case GREATER_THAN:
              pquery.constrain(checkValue).greater();
              break;
            case GREATER_THAN_EQUALS:
              pquery.constrain(checkValue).greater().equal();
              break;
            case IN: {
              Object[] arr;
              if(checkValue.getClass().isArray()) {
                arr = (Object[]) checkValue;
              }
              else if(checkValue instanceof Collection<?>) {
                arr = ((Collection) checkValue).toArray();
              }
              else if(checkValue instanceof String) {
                // assume comma-delimited string
                arr =
                    org.springframework.util.ObjectUtils.toObjectArray(org.springframework.util.StringUtils
                        .commaDelimitedListToStringArray((String) checkValue));
              }
              else {
                throw new InvalidCriteriaException(
                    "Unsupported or null type for IN comparator: " + checkValue == null ? "<null>" : checkValue
                        .getClass().toString());
              }
              Constraint c = null;
              for(final Object o : arr) {
                if(c == null) {
                  c = pquery.constrain(o);
                }
                else {
                  c.or(pquery.constrain(o));
                }
              }
              break;
            }
            case IS:
              if(checkValue instanceof DBType == false) {
                throw new InvalidCriteriaException("IS clauses support only check values of type: "
                    + DBType.class.getSimpleName());
              }
              final DBType dbType = (DBType) checkValue;
              if(dbType == DBType.NULL) {
                // null
                pquery.constrain(null);
              }
              else {
                // not null
                pquery.constrain(null).not();
              }
            case LESS_THAN:
              pquery.constrain(checkValue).smaller();
              break;
            case LESS_THAN_EQUALS:
              pquery.constrain(checkValue).smaller().equal();
              break;
            case LIKE:
              pquery.constrain(checkValue).like();
              break;
            case NOT_EQUALS:
              pquery.constrain(checkValue).not();
              break;
            case STARTS_WITH:
              pquery.constrain(checkValue).startsWith(ctn.isCaseSensitive());
              break;
          } // comparator switch
        }
      }
    }
View Full Code Here

   * @param <E> object type
   * @param type
   * @param sorting optional
   */
  protected <E> List<E> loadAllOfType(Class<E> type, Sorting sorting) {
    final Query q = getDb4oTemplate().query();
    q.constrain(type);
    applySorting(q, sorting);
    return runQuery(q);
  }
View Full Code Here

      q.constrain(Interface.class);
    }
    else if(SelectNamedQueries.ACCOUNT_INTERFACE_SUMMARY_LISTING.getQueryName().equals(qname)) {
      q.constrain(Interface.class);
      final String et = (String) params.get(0).getValue();
      Query sq;
      if("asp".equals(et))
        sq = q.descend("isAvailableAsp");
      else if("isp".equals(et))
        sq = q.descend("isAvailableIsp");
      else if("merchant".equals(et))
        sq = q.descend("isAvailableMerchant");
      else if("customer".equals(et))
        sq = q.descend("isAvailableCustomer");
      else
        throw new InvalidCriteriaException();
      sq.constrain(Boolean.TRUE);
    }

    else
      throw new InvalidCriteriaException("Unhandled named query: " + qname);
  }
View Full Code Here

  @Override
  public <E extends IEntity> List<E> findEntities(Criteria<E> criteria, final Sorting sorting)
  throws InvalidCriteriaException, DataAccessException {
    if(criteria == null) throw new InvalidCriteriaException("No criteria specified.");

    final Query query = getDb4oTemplate().query();

    if(criteria.getCriteriaType().isQuery()) {
      if(nqt == null) throw new InvalidCriteriaException("No db4o named query translator specified.");
      nqt.translateNamedQuery(criteria.getNamedQueryDefinition(), criteria.getQueryParams(), query);
    }
    else {
      query.constrain(criteria.getEntityClass());
      final CriterionGroup pg = criteria.getPrimaryGroup();
      if(pg != null && pg.isSet()) {
        for(final ICriterion ic : pg) {
          if(ic.isGroup()) throw new InvalidCriteriaException("Nested criterion groups are not supported");
          if(!ic.isSet()) throw new InvalidCriteriaException("criterion not set");
          final Criterion ctn = (Criterion) ic;
          final Object checkValue = ctn.getValue();
          final String pname = ctn.getPropertyName();

          Query pquery;
          if(pname.indexOf('.') > 0) {
            pquery = query;
            // descend one time for each node in the pname (which may be a dot
            // notated property path)
            final PropertyPath path = new PropertyPath(pname);
            for(final String node : path.nodes()) {
              pquery = pquery.descend(node);
            }
          }
          else {
            pquery = query.descend(pname);
          }

          switch(ctn.getComparator()) {
          case BETWEEN: {
            Object min, max;
            if(checkValue instanceof NumberRange) {
              final NumberRange range = (NumberRange) checkValue;
              min = range.getMinimumNumber();
              max = range.getMaximumNumber();
            }
            else if(checkValue instanceof DateRange) {
              final DateRange range = (DateRange) checkValue;
              min = range.getStartDate();
              max = range.getEndDate();
            }
            else {
              // presume an object array
              final Object[] oarr = (Object[]) checkValue;
              min = oarr[0];
              max = oarr[1];
            }
            pquery.constrain(min).greater().equal().or(pquery.constrain(max).smaller().equal());
            break;
          }
          case CONTAINS:
            pquery.constrain(checkValue).contains();
            break;
          case ENDS_WITH:
            pquery.constrain(checkValue).endsWith(ctn.isCaseSensitive());
            break;
          case EQUALS:
            if(!ctn.isCaseSensitive())
              throw new InvalidCriteriaException("Case insensitive equals checking is currently not supported");
            pquery.constrain(checkValue);
            break;
          case GREATER_THAN:
            pquery.constrain(checkValue).greater();
            break;
          case GREATER_THAN_EQUALS:
            pquery.constrain(checkValue).greater().equal();
            break;
          case IN: {
            Object[] arr;
            if(checkValue.getClass().isArray()) {
              arr = (Object[]) checkValue;
            }
            else if(checkValue instanceof Collection<?>) {
              arr = ((Collection) checkValue).toArray();
            }
            else if(checkValue instanceof String) {
              // assume comma-delimited string
              arr =
                org.springframework.util.ObjectUtils.toObjectArray(org.springframework.util.StringUtils
                    .commaDelimitedListToStringArray((String) checkValue));
            }
            else {
              throw new InvalidCriteriaException(
                  "Unsupported or null type for IN comparator: " + checkValue == null ? "<null>" : checkValue
                      .getClass().toString());
            }
            Constraint c = null;
            for(final Object o : arr) {
              if(c == null) {
                c = pquery.constrain(o);
              }
              else {
                c.or(pquery.constrain(o));
              }
            }
            break;
          }
          case IS:
            if(checkValue instanceof DBType == false) {
              throw new InvalidCriteriaException("IS clauses support only check values of type: "
                  + DBType.class.getSimpleName());
            }
            final DBType dbType = (DBType) checkValue;
            if(dbType == DBType.NULL) {
              // null
              pquery.constrain(null);
            }
            else {
              // not null
              pquery.constrain(null).not();
            }
          case LESS_THAN:
            pquery.constrain(checkValue).smaller();
            break;
          case LESS_THAN_EQUALS:
            pquery.constrain(checkValue).smaller().equal();
            break;
          case LIKE:
            pquery.constrain(checkValue).like();
            break;
          case NOT_EQUALS:
            pquery.constrain(checkValue).not();
            break;
          case STARTS_WITH:
            pquery.constrain(checkValue).startsWith(ctn.isCaseSensitive());
            break;
          } // comparator switch
        }
      }
    }
View Full Code Here

    }
    else if(SelectNamedQueries.ACCOUNT_INTERFACE_SUMMARY_LISTING.getQueryName().equals(qname)) {
      // 1 param: accountType (SmbizEntityType)
      final SmbizEntityType et = (SmbizEntityType) params.get(0).getValue();
      q.constrain(Interface.class);
      Query sq;
      switch(et) {
      case ASP:
        sq = q.descend("isAvailableAsp");
        break;
      case ISP:
        sq = q.descend("isAvailableIsp");
        break;
      case MERCHANT:
        sq = q.descend("isAvailableMerchant");
        break;
      case CUSTOMER:
        sq = q.descend("isAvailableCustomer");
        break;
      default:
        throw new InvalidCriteriaException();
      }
      sq.constrain(true);
    }

    else throw new InvalidCriteriaException("Unhandled named query: " + qname);
  }
View Full Code Here

       BaseFeed<BaseFeed, BaseEntry> persistentFeed = getFeedOnly(feed.getId(),feed.getServiceType());
       /*
        * prevent previously added entries in long running storage instances
        */
       clearDynamicElements(persistentFeed);
        Query query = this.container.query();
        query.constrain(DB4oEntry.class);
        query.descend("feedId").constrain(feed.getId()).equal();
        query.descend("updateTime").orderDescending();

        ObjectSet<DB4oEntry> set = query.execute();
      
        int size = set.size();
       
        if (size < feed.getStartIndex()) {
            if (LOG.isDebugEnabled())
View Full Code Here

    @SuppressWarnings("unchecked")
    private BaseFeed<BaseFeed, BaseEntry> getFeedOnly(final String feedId, final String serviceId)
            throws StorageException {
        if(!checkService(feedId,serviceId))
            throw new StorageException();
        Query query = this.container.query();
        query.constrain(ServerBaseFeed.class);
     
        query.constrain(BaseFeed.class);

        query.descend("id").constrain(feedId).equal();

        ObjectSet set = query.execute();
        if (set.size() > 1)
            throw new StorageException("Query for feed id " + feedId
                    + " returns more than one result");
        if (set.hasNext())
        return (BaseFeed<BaseFeed, BaseEntry>) set.next();
View Full Code Here

TOP

Related Classes of com.db4o.query.Query

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.