Exemplo n.º 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);
    }
  }
Exemplo n.º 2
0
 @Override
 public String[] submit(java.util.Collection<gen.model.test.Clicked> domainEvents) {
   java.sql.Connection connection = getConnection();
   try (java.sql.PreparedStatement statement =
           connection.prepareStatement(
               "/*NO LOAD BALANCE*/SELECT \"URI\" FROM \"test\".\"submit_Clicked\"(?)");
       org.revenj.postgres.PostgresWriter sw = org.revenj.postgres.PostgresWriter.create()) {
     if (prepareEvents != null) prepareEvents.accept(domainEvents);
     String[] result = new String[domainEvents.size()];
     org.revenj.postgres.converters.PostgresTuple tuple =
         org.revenj.postgres.converters.ArrayTuple.create(domainEvents, converter::to);
     org.postgresql.util.PGobject pgo = new org.postgresql.util.PGobject();
     pgo.setType("\"test\".\"Clicked_event\"[]");
     tuple.buildTuple(sw, false);
     pgo.setValue(sw.toString());
     statement.setObject(1, pgo);
     try (java.sql.ResultSet rs = statement.executeQuery()) {
       for (int i = 0; i < result.length; i++) {
         rs.next();
         result[i] = rs.getString(1);
       }
     }
     if (assignUris != null) assignUris.accept(domainEvents, result);
     return result;
   } catch (java.sql.SQLException e) {
     throw new RuntimeException(e);
   } finally {
     releaseConnection(connection);
   }
 }
Exemplo n.º 3
0
  /**
   * Insert transportation routes
   *
   * @param transportationRouteList List of transportation routes to insert (id == null)
   * @throws Exception
   */
  public void insertTransportationRoutes(Collection<TransportationRoute> transportationRouteList)
      throws Exception {
    try {
      String query =
          String.format(
              "INSERT INTO %s (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
              table,
              id,
              name,
              validFrom,
              validUntil,
              transportationID,
              operator,
              network,
              extRef,
              description,
              descriptionFrom,
              descriptionTo,
              routeNo);
      PreparedStatement ps = ConnectionManager.getInstance().prepareStatement(query);

      for (TransportationRoute transportationRoute : transportationRouteList) {
        if (transportationRoute.getId() != null || transportationRoute.getType() == null) {
          continue;
        }

        transportationRoute.setId(UUID.randomUUID());
        PGobject toInsertUUID = new PGobject();
        toInsertUUID.setType("uuid");
        toInsertUUID.setValue(String.valueOf(transportationRoute.getId()));

        ps.setObject(1, toInsertUUID);
        ps.setString(2, transportationRoute.getName());
        ps.setTimestamp(3, transportationRoute.getValidFrom());
        ps.setTimestamp(4, transportationRoute.getValidUntil());
        ps.setObject(5, transportationRoute.getType().getId());
        ps.setString(6, transportationRoute.getOperator());
        ps.setString(7, transportationRoute.getNetwork());
        ps.setString(8, transportationRoute.getExtRef());
        ps.setString(9, transportationRoute.getDescription());
        ps.setString(10, transportationRoute.getDescriptionFrom());
        ps.setString(11, transportationRoute.getDescriptionTo());
        ps.setString(12, transportationRoute.getRouteNo());

        ps.addBatch();
      }

      ps.executeBatch();
      ps.close();

      ScheduleDAO scheduleDAO = new ScheduleDAO();
      scheduleDAO.insertOrUpdateSchedules(transportationRouteList);
    } catch (Exception e) {
      ConnectionManager.getInstance().closeConnection(true);

      throw new Exception("Transportation route insert failed: " + e.getMessage());
    }
  }
Exemplo n.º 4
0
 @Override
 public void nullSafeSet(PreparedStatement st, Object value, int index, SessionImplementor session)
     throws HibernateException, SQLException {
   try {
     final String json = value == null ? null : objectMapper.writeValueAsString(value);
     // otherwise PostgreSQL won't recognize the type
     PGobject pgo = new PGobject();
     pgo.setType("jsonb");
     pgo.setValue(json);
     st.setObject(index, pgo);
   } catch (JsonProcessingException ex) {
     throw new HibernateException(ex);
   }
 }
  @Override
  public Object mapParam(final Object value, final Connection connection) {
    if (value == null) {
      return null;
    }

    Object result = value;
    if (clazz.isEnum()) {

      // HACK: should be implemented in PgTypeHelper
      final PGobject pgobj = new PGobject();
      pgobj.setType(typeName);
      try {
        pgobj.setValue(((Enum<?>) value).name());
      } catch (final SQLException ex) {
        if (sensitive) {
          LOG.error("Failed to set PG object value (sensitive parameter, stacktrace hidden)");
        } else {
          LOG.error("Failed to set PG object value", ex);
        }
      }

      result = pgobj;
    } else {
      try {
        result = PgTypeHelper.asPGobject(value, typeName, connection);
      } catch (final SQLException ex) {
        if (sensitive) {
          LOG.error("Failed to serialize PG object (sensitive parameter, stacktrace hidden)");
        } else {
          LOG.error("Failed to serialize PG object", ex);
        }
      }
    }

    return result;
  }
  @Override
  public Integer create(RentalInformation rentalInfo) {
    String insertSQL = "insert into carrental.\"rentalinformation\" values (?,?,?,?)";
    try {
      PreparedStatement preparedStatement = connection.prepareStatement(insertSQL);

      Integer refID = getNextSequenceValue();
      preparedStatement.setInt(1, refID);
      preparedStatement.setString(2, rentalInfo.getRentersName());
      preparedStatement.setInt(3, rentalInfo.getRentersAge());
      String jsonRentalDetail = convertObjectToJsonString(rentalInfo.getRentalDetail());

      PGobject jsonObject = new PGobject();
      jsonObject.setType("json");
      jsonObject.setValue(jsonRentalDetail);
      preparedStatement.setObject(4, jsonObject);
      System.out.println("preparedStatement := " + preparedStatement);
      preparedStatement.executeUpdate();
      return refID;
    } catch (SQLException e) {
      e.printStackTrace();
    }
    return null;
  }
Exemplo n.º 7
0
  @Override
  public boolean exists(org.revenj.patterns.Specification<gen.model.test.Clicked> specification) {
    final String selectType = "SELECT exists(SELECT *";
    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 = null;
      if (specification == null) {
        sql = "SELECT exists(SELECT * 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 {
        try {
          return query(specification).any();
        } catch (java.io.IOException e) {
          throw new RuntimeException(e);
        }
      }
      try (java.sql.PreparedStatement statement = connection.prepareStatement(sql + ")")) {
        applyFilters.accept(statement);
        try (java.sql.ResultSet rs = statement.executeQuery()) {
          rs.next();
          return rs.getBoolean(1);
        }
      } catch (java.sql.SQLException e) {
        throw new RuntimeException(e);
      }
    } finally {
      releaseConnection(connection);
    }
  }