io.ebean.Query Java Examples

The following examples show how to use io.ebean.Query. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: NodeServerServiceImpl.java    From canal with Apache License 2.0 6 votes vote down vote up
private Query<NodeServer> getBaseQuery(NodeServer nodeServer) {
    Query<NodeServer> query = NodeServer.find.query();
    query.fetch("canalCluster", "name").setDisableLazyLoading(true);

    if (nodeServer != null) {
        if (StringUtils.isNotEmpty(nodeServer.getName())) {
            query.where().like("name", "%" + nodeServer.getName() + "%");
        }
        if (StringUtils.isNotEmpty(nodeServer.getIp())) {
            query.where().eq("ip", nodeServer.getIp());
        }
        if (nodeServer.getClusterId() != null) {
            if (nodeServer.getClusterId() == -1) {
                query.where().isNull("clusterId");
            } else {
                query.where().eq("clusterId", nodeServer.getClusterId());
            }
        }
    }

    return query;
}
 
Example #2
Source File: EbeanQueryWrapper.java    From spring-data-ebean with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
<E> Stream<E> findStream() {
    if (queryType == QUERY) {
        return StreamUtils.createStreamFromIterator(((Query<E>) queryInstance).findIterate());
    }
    throw new IllegalArgumentException("query not supported!");
}
 
Example #3
Source File: EbeanPathProps.java    From ameba with MIT License 5 votes vote down vote up
/** {@inheritDoc} */
@Override
public <T> void apply(final Query<T> query) {
    pathProperties.each(props -> {
        String path = props.getPath();
        String propsStr = props.getPropertiesAsString();

        if (path == null || path.isEmpty()) {
            query.select(propsStr);
        } else {
            query.fetch(path, propsStr);
        }
    });
}
 
Example #4
Source File: EbeanQueryChannelServiceIntegrationTest.java    From spring-data-ebean with Apache License 2.0 5 votes vote down vote up
@Test
public void createSqlQueryMappingColumns() {
  String sql1 = "select first_name, last_name, email_address from user where last_name= :lastName";
  String sql2 = "select first_name as firstName, last_name as lastName, email_address as emailAddress from user where last_name= :lastName";
  Map<String, String> columnsMapping = new HashMap<>();
  columnsMapping.put("first_name", "firstName");
  columnsMapping.put("last_name", "lastName");

  Query<UserInfo> query1 = queryChannel.createSqlQuery(UserInfo.class,
      sql1);
  Query<UserInfo> query2 = queryChannel.createSqlQuery(UserInfo.class,
      sql2);
  Query<UserInfo> query3 = queryChannel.createSqlQueryMappingColumns(UserInfo.class,
      sql1, columnsMapping);

  query1.setParameter("lastName", "Test");
  query2.setParameter("lastName", "Test");
  query3.setParameter("lastName", "Test");
  UserInfo userInfo1 = query1.findOne();
  UserInfo userInfo2 = query2.findOne();
  UserInfo userInfo3 = query3.findOne();
  assertEquals("QueryChannel", userInfo1.getFirstName());
  assertEquals("[email protected]", userInfo1.getEmailAddress());
  assertEquals("QueryChannel", userInfo2.getFirstName());
  assertEquals("[email protected]", userInfo2.getEmailAddress());
  assertEquals("QueryChannel", userInfo3.getFirstName());
  assertEquals("[email protected]", userInfo3.getEmailAddress());
}
 
Example #5
Source File: NamedEbeanQuery.java    From spring-data-ebean with Apache License 2.0 5 votes vote down vote up
/**
 * Looks up a named query for the given {@link org.springframework.data.repository.query.QueryMethod}.
 *
 * @param method
 * @return
 */
public static RepositoryQuery lookupFrom(EbeanQueryMethod method, EbeanServer ebeanServer) {
    final String queryName = method.getNamedQueryName();

    LOG.debug("Looking up named query {}", queryName);

    try {
        Query query = ebeanServer.createNamedQuery(method.getEntityInformation().getJavaType(), queryName);
        return new NamedEbeanQuery(method, ebeanServer, query);
    } catch (PersistenceException e) {
        LOG.debug("Did not find named query {}", queryName);
        return null;
    }
}
 
Example #6
Source File: NamedEbeanQuery.java    From spring-data-ebean with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new {@link NamedEbeanQuery}.
 */
private NamedEbeanQuery(EbeanQueryMethod method, EbeanServer ebeanServer, Query query) {
    super(method, ebeanServer);

    this.queryName = method.getNamedQueryName();
    this.query = query;
}
 
Example #7
Source File: EbeanQueryWrapper.java    From spring-data-ebean with Apache License 2.0 5 votes vote down vote up
void setFirstRow(int firstRow) {
    if (queryType == QUERY) {
        ((Query) queryInstance).setFirstRow(firstRow);
        return;
    }
    throw new IllegalArgumentException("query not supported!");
}
 
Example #8
Source File: EbeanUtils.java    From ameba with MIT License 5 votes vote down vote up
/**
 * <p>checkQuery.</p>
 *
 * @param query     a {@link io.ebean.Query} object.
 * @param whitelist a {@link java.util.Set} object.
 * @param blacklist a {@link java.util.Set} object.
 * @param manager   a {@link InjectionManager} object.
 */
public static void checkQuery(Query<?> query, Set<String> whitelist,
                              Set<String> blacklist, InjectionManager manager) {
    ResourceInfo resource = manager.getInstance(ResourceInfo.class);
    Class<?> rc = resource.getResourceClass();
    Set<String> wl = null, bl = null;
    if (rc != null) {
        Filter filter = rc.getAnnotation(Filter.class);

        if (filter != null) {
            if (filter.whitelist().length > 0) {
                wl = Sets.newLinkedHashSet();
                Collections.addAll(wl, filter.whitelist());
            }
            if (filter.blacklist().length > 0) {
                bl = Sets.newLinkedHashSet();
                Collections.addAll(bl, filter.blacklist());
            }
        }
    }

    if (whitelist != null) {
        if (wl == null) {
            wl = Sets.newLinkedHashSet();
        }
        wl.addAll(whitelist);
    }

    if (blacklist != null) {
        if (bl == null) {
            bl = Sets.newLinkedHashSet();
        }
        bl.addAll(blacklist);
    }
    checkQuery((SpiQuery) query, wl, bl, manager.getInstance(Application.Mode.class).isProd());
}
 
Example #9
Source File: EbeanQueryWrapper.java    From spring-data-ebean with Apache License 2.0 5 votes vote down vote up
void setMaxRows(int maxRows) {
    if (queryType == QUERY) {
        ((Query) queryInstance).setMaxRows(maxRows);
        return;
    }
    throw new IllegalArgumentException("query not supported!");
}
 
Example #10
Source File: EbeanQueryWrapper.java    From spring-data-ebean with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
<E> Slice<E> findSlice(Pageable pageable) {
    List<E> resultList = null;
    int pageSize = pageable.getPageSize();
    int offset = (int) pageable.getOffset();
    if (queryType == QUERY) {
        resultList = ((Query<E>) queryInstance).setFirstRow(offset).setMaxRows(pageSize + 1).findList();
        boolean hasNext = resultList.size() > pageSize;
        return new SliceImpl<E>(hasNext ? resultList.subList(0, pageSize) : resultList, pageable, hasNext);
    }
    throw new IllegalArgumentException("query not supported!");
}
 
Example #11
Source File: EbeanQueryWrapper.java    From spring-data-ebean with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
<E> List<E> findList() {
    if (queryType == QUERY) {
        return ((Query<E>) queryInstance).findList();
    }
    throw new IllegalArgumentException("query not supported!");
}
 
Example #12
Source File: EbeanQueryWrapper.java    From spring-data-ebean with Apache License 2.0 5 votes vote down vote up
int delete() {
    switch (queryType) {
        case QUERY:
            return ((Query) queryInstance).delete();
        case UPDATE:
            return ((Update) queryInstance).execute();
        case SQL_UPDATE:
            return ((SqlUpdate) queryInstance).execute();
        default:
            throw new IllegalArgumentException("query not supported!");
    }
}
 
Example #13
Source File: EbeanQueryWrapper.java    From spring-data-ebean with Apache License 2.0 5 votes vote down vote up
int update() {
    switch (queryType) {
        case QUERY:
            return ((Query) queryInstance).update();
        case UPDATE:
            return ((Update) queryInstance).execute();
        case SQL_UPDATE:
            return ((SqlUpdate) queryInstance).execute();
        default:
            throw new IllegalArgumentException("query not supported!");
    }
}
 
Example #14
Source File: EbeanQueryWrapper.java    From spring-data-ebean with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
<E> Page<E> findPage(Pageable pageable) {
    if (queryType == QUERY) {
        PagedList<E> pagedList = ((Query<E>) queryInstance)
                .setFirstRow((int) pageable.getOffset())
                .setMaxRows(pageable.getPageSize())
                .findPagedList();
        return PageableExecutionUtils.getPage(pagedList.getList(), pageable, pagedList::getTotalCount);

    }
    throw new IllegalArgumentException("query not supported!");
}
 
Example #15
Source File: EbeanQueryWrapper.java    From spring-data-ebean with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
<E> E findOne() {
    if (queryType == QUERY) {
        return ((Query<E>) queryInstance).findOne();
    }
    throw new IllegalArgumentException("query not supported!");
}
 
Example #16
Source File: EbeanQueryWrapper.java    From spring-data-ebean with Apache License 2.0 5 votes vote down vote up
void setParameter(int position, Object value) {
    switch (queryType) {
        case QUERY:
            ((Query) queryInstance).setParameter(position, value);
            break;
        case UPDATE:
            ((Update) queryInstance).setParameter(position, value);
            break;
        case SQL_UPDATE:
            ((SqlUpdate) queryInstance).setParameter(position, value);
            break;
        default:
            throw new IllegalArgumentException("query not supported!");
    }
}
 
Example #17
Source File: EbeanQueryWrapper.java    From spring-data-ebean with Apache License 2.0 5 votes vote down vote up
void setParameter(String name, Object value) {
    switch (queryType) {
        case QUERY:
            ((Query) queryInstance).setParameter(name, value);
            break;
        case UPDATE:
            ((Update) queryInstance).setParameter(name, value);
            break;
        case SQL_UPDATE:
            ((SqlUpdate) queryInstance).setParameter(name, value);
            break;
        default:
            throw new IllegalArgumentException("query not supported!");
    }
}
 
Example #18
Source File: EbeanQueryWrapper.java    From spring-data-ebean with Apache License 2.0 5 votes vote down vote up
public EbeanQueryWrapper(T queryInstance) {
    this.queryInstance = queryInstance;
    if (queryInstance instanceof Query) {
        this.queryType = QUERY;
    } else if (queryInstance instanceof Update) {
        this.queryType = QueryType.UPDATE;
    } else if (queryInstance instanceof SqlUpdate) {
        this.queryType = QueryType.SQL_UPDATE;
    } else {
        throw new IllegalArgumentException("query not supported!");
    }
}
 
Example #19
Source File: PartTreeEbeanQuery.java    From spring-data-ebean with Apache License 2.0 5 votes vote down vote up
protected EbeanQueryCreator createCreator(ParametersParameterAccessor accessor) {
    EbeanServer ebeanServer = getEbeanServer();
    Query ebeanQuery = ebeanServer.createQuery(domainClass);
    ExpressionList expressionList = ebeanQuery.where();

    ParameterMetadataProvider provider = new ParameterMetadataProvider(accessor);

    ResultProcessor processor = getQueryMethod().getResultProcessor();

    return new EbeanQueryCreator(tree, processor.getReturnedType(), expressionList, provider);
}
 
Example #20
Source File: CanalClusterServiceImpl.java    From canal with Apache License 2.0 4 votes vote down vote up
public List<CanalCluster> findList(CanalCluster canalCluster) {
    Query<CanalCluster> query = CanalCluster.find.query();
    query.order().asc("id");
    return query.findList();
}
 
Example #21
Source File: NodeServerServiceImpl.java    From canal with Apache License 2.0 4 votes vote down vote up
public List<NodeServer> findAll(NodeServer nodeServer) {
    Query<NodeServer> query = getBaseQuery(nodeServer);
    query.order().asc("id");
    return query.findList();
}
 
Example #22
Source File: EbeanQueryWrapper.java    From spring-data-ebean with Apache License 2.0 4 votes vote down vote up
int getFirstRow() {
    if (queryType == QUERY) {
        return ((Query) queryInstance).getFirstRow();
    }
    throw new IllegalArgumentException("query not supported!");
}
 
Example #23
Source File: EbeanQueryWrapper.java    From spring-data-ebean with Apache License 2.0 4 votes vote down vote up
Integer getMaxRows() {
    if (queryType == QUERY) {
        return ((Query) queryInstance).getMaxRows();
    }
    throw new IllegalArgumentException("query not supported!");
}
 
Example #24
Source File: EbeanQueryWrapper.java    From spring-data-ebean with Apache License 2.0 4 votes vote down vote up
boolean isExists() {
    if (queryType == QUERY) {
        return ((Query) queryInstance).findCount() > 0;
    }
    throw new IllegalArgumentException("query not supported!");
}
 
Example #25
Source File: EbeanQueryCreator.java    From spring-data-ebean with Apache License 2.0 4 votes vote down vote up
/**
 * Finalizes the given {@link ExpressionList} and applies the given sort.
 */
@Override
protected final Query complete(Expression expression, Sort sort) {
    return root.add(expression).query();
}
 
Example #26
Source File: EbeanUtils.java    From ameba with MIT License 2 votes vote down vote up
/**
 * <p>checkQuery.</p>
 *
 * @param query   a {@link io.ebean.Query} object.
 * @param manager a {@link InjectionManager} object.
 */
public static void checkQuery(Query<?> query, InjectionManager manager) {
    checkQuery(query, null, null, manager);
}
 
Example #27
Source File: Filters.java    From ameba with MIT License 2 votes vote down vote up
/**
 * <p>createQuery.</p>
 *
 * @param beanClass a {@link java.lang.Class} object.
 * @param server a {@link io.ebean.EbeanServer} object.
 * @return a {@link io.ebean.Query} object.
 */
public static Query createQuery(Class beanClass, EbeanServer server) {
    return server.createQuery(beanClass);
}
 
Example #28
Source File: FilterExpression.java    From ameba with MIT License 2 votes vote down vote up
/**
 * <p>Constructor for FilterExpression.</p>
 *
 * @param path      a {@link java.lang.String} object.
 * @param expr      a {@link io.ebean.ExpressionFactory} object.
 * @param rootQuery a {@link io.ebean.Query} object.
 */
public FilterExpression(String path, ExpressionFactory expr, Query<T> rootQuery) {
    super(new FilterExprPath(path), expr, rootQuery);
    this.path = path;
}