示例#1
2
  @Override
  public java.util.List<gen.model.test.Clicked> search(
      org.revenj.patterns.Specification<gen.model.test.Clicked> specification,
      Integer limit,
      Integer offset) {
    final String selectType = "SELECT it";
    java.util.function.Consumer<java.sql.PreparedStatement> applyFilters = ps -> {};
    java.sql.Connection connection = getConnection();
    try (org.revenj.postgres.PostgresWriter pgWriter =
        org.revenj.postgres.PostgresWriter.create()) {
      String sql;
      if (specification == null) {
        sql = "SELECT r FROM \"test\".\"Clicked_event\" r";
      } else if (specification instanceof gen.model.test.Clicked.BetweenNumbers) {
        gen.model.test.Clicked.BetweenNumbers spec =
            (gen.model.test.Clicked.BetweenNumbers) specification;
        sql = selectType + " FROM \"test\".\"Clicked.BetweenNumbers\"(?, ?, ?) it";

        applyFilters =
            applyFilters.andThen(
                ps -> {
                  try {
                    ps.setBigDecimal(1, spec.getMin());
                  } catch (Exception e) {
                    throw new RuntimeException(e);
                  }
                });
        applyFilters =
            applyFilters.andThen(
                ps -> {
                  try {

                    Object[] __arr = new Object[spec.getInSet().size()];
                    int __ind = 0;
                    for (Object __it : spec.getInSet()) __arr[__ind++] = __it;
                    ps.setArray(2, connection.createArrayOf("numeric", __arr));
                  } catch (Exception e) {
                    throw new RuntimeException(e);
                  }
                });
        applyFilters =
            applyFilters.andThen(
                ps -> {
                  try {
                    if (spec.getEn() == null) ps.setNull(3, java.sql.Types.OTHER);
                    else {
                      org.postgresql.util.PGobject __pgo = new org.postgresql.util.PGobject();
                      __pgo.setType("\"test\".\"En\"");
                      __pgo.setValue(
                          gen.model.test.converters.EnConverter.stringValue(spec.getEn()));
                      ps.setObject(3, __pgo);
                    }
                  } catch (Exception e) {
                    throw new RuntimeException(e);
                  }
                });
      } else {
        org.revenj.patterns.Query<gen.model.test.Clicked> query = query(specification);
        if (offset != null) {
          query = query.skip(offset);
        }
        if (limit != null) {
          query = query.limit(limit);
        }
        try {
          return query.list();
        } catch (java.io.IOException e) {
          throw new RuntimeException(e);
        }
      }
      if (limit != null) {
        sql += " LIMIT " + Integer.toString(limit);
      }
      if (offset != null) {
        sql += " OFFSET " + Integer.toString(offset);
      }
      try (java.sql.PreparedStatement statement = connection.prepareStatement(sql)) {
        applyFilters.accept(statement);
        return readFromDb(statement, new java.util.ArrayList<>());
      } catch (java.sql.SQLException | java.io.IOException e) {
        throw new RuntimeException(e);
      }
    } finally {
      releaseConnection(connection);
    }
  }
示例#2
0
 @Test
 public void simpleQuery() throws IOException {
   ServiceLocator locator = container;
   NextRepository repository = locator.resolve(NextRepository.class);
   repository.insert(new Next());
   List<Next> search = repository.search();
   Query<Next> stream = repository.query();
   List<Next> list = stream.list();
   Assert.assertEquals(search.size(), list.size());
 }