phoenix-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ndimiduk <...@git.apache.org>
Subject [GitHub] phoenix pull request: PHOENIX-2186 : Back end services
Date Thu, 20 Aug 2015 19:41:22 GMT
Github user ndimiduk commented on a diff in the pull request:

    https://github.com/apache/phoenix/pull/111#discussion_r37572345
  
    --- Diff: phoenix-tracing-webapp/src/main/java/org/apache/phoenix/tracingwebapp/http/EntityFactory.java
---
    @@ -0,0 +1,98 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one or more
    + * contributor license agreements.  See the NOTICE file distributed with
    + * this work for additional information regarding copyright ownership.
    + * The ASF licenses this file to you under the Apache License, Version 2.0
    + * (the "License"); you may not use this file except in compliance with
    + * the License.  You may obtain a copy of the License at
    + *
    + * http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +package org.apache.phoenix.tracingwebapp.http;
    +
    +import java.sql.Connection;
    +import java.sql.PreparedStatement;
    +import java.sql.ResultSet;
    +import java.sql.ResultSetMetaData;
    +import java.sql.SQLException;
    +import java.util.ArrayList;
    +import java.util.HashMap;
    +import java.util.List;
    +import java.util.Map;
    +
    +public class EntityFactory {
    +
    +  private final String queryString;
    +  protected Connection connection;
    +
    +  public EntityFactory(Connection connection, String queryString) {
    +    this.queryString = queryString;
    +    this.connection = connection;
    +  }
    +
    +  public Map<String, Object> findSingle(Object[] params) throws SQLException {
    +    List<Map<String, Object>> objects = this.findMultiple(params);
    +
    +    if (objects.size() != 1) {
    +      throw new SQLException("Query did not produce one object it produced: "
    +          + objects.size() + " objects.");
    +    }
    +
    +    Map<String, Object> object = objects.get(0); // get first record;
    +
    +    return object;
    +  }
    +
    +  public List<Map<String, Object>> findMultiple(Object[] params)
    +      throws SQLException {
    +    ResultSet rs = null;
    +    PreparedStatement ps = null;
    +    try {
    +      ps = this.connection.prepareStatement(this.queryString);
    +      for (int i = 0; i < params.length; ++i) {
    +        ps.setObject(1, params[i]);
    +      }
    +
    +      rs = ps.executeQuery();
    +      return getEntitiesFromResultSet(rs);
    +    } catch (SQLException e) {
    +      throw (e);
    +    } finally {
    +      if (rs != null) {
    +        rs.close();
    +      }
    +      if (ps != null) {
    +        ps.close();
    +      }
    +    }
    +  }
    +
    +  protected List<Map<String, Object>> getEntitiesFromResultSet(
    +      ResultSet resultSet) throws SQLException {
    +    ArrayList<Map<String, Object>> entities = new ArrayList<>();
    +    while (resultSet.next()) {
    +      entities.add(getEntityFromResultSet(resultSet));
    +    }
    +    return entities;
    +  }
    +
    +  protected Map<String, Object> getEntityFromResultSet(ResultSet resultSet)
    --- End diff --
    
    this method does not use member variables and thus can be made a static method.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

Mime
View raw message