Esempio n. 1
0
  public ComplexValue(SqlRowSet rowSet, boolean isNullValues) {
    rowValues = new HashMap<String, Object>(rowSet.getMetaData().getColumnCount());

    int index = 1;
    for (String columnName : rowSet.getMetaData().getColumnNames()) {
      rowValues.put(columnName.toLowerCase(), isNullValues ? null : rowSet.getObject(index++));
    }
  }
Esempio n. 2
0
  /*
   * Convert result set into a collection of entities.
   */
  private CollectionResource<Entity> buildCollectionResource(String entityType, SqlRowSet rowSet) {
    List<EntityResource<Entity>> results = new ArrayList<EntityResource<Entity>>();

    // Extract the returned column names. May be a subset of the ones
    // requested.
    String[] columnNames = rowSet.getMetaData().getColumnNames();

    // For all rows returned add an entity to the collection.
    while (rowSet.next()) {
      EntityProperties properties = new EntityProperties();

      // For all columns in this row.
      for (String columnName : columnNames) {
        Object value = rowSet.getObject(columnName);

        // Only return non null values
        if (null != value) {
          // Add object to the property. getObject() returns an object
          // with the correct java type for each sql type. So we don't
          // need to cast.
          properties.setProperty(new EntityProperty(columnName, value));
        }
      }

      // Create entity.
      // Note: Despite the variable name the first arg of both these is
      // the entity type name. Not it's key.
      Entity entity = new Entity(entityType, properties);
      results.add(new EntityResource<Entity>(entity.getName(), entity));
    }

    // Note: This line looks a bit odd but the {} at the end is required.
    return new CollectionResource<Entity>(results) {};
  }
Esempio n. 3
0
  /** Writes all rows in the SqlRowSet to the given Grid. */
  public static void addRows(Grid grid, SqlRowSet rs) {
    int cols = rs.getMetaData().getColumnCount();

    while (rs.next()) {
      grid.addRow();

      for (int i = 1; i <= cols; i++) {
        grid.addValue(rs.getObject(i));
      }
    }
  }
Esempio n. 4
0
 private String transResultsetToString(SqlRowSet sqlRowSet) throws SQLException {
   SqlRowSetMetaData sqlRowSetMetaData = sqlRowSet.getMetaData();
   int colCount = sqlRowSetMetaData.getColumnCount();
   StringBuffer sb = new StringBuffer();
   while (sqlRowSet.next()) {
     for (int i = 1; i <= colCount; i++) {
       int type = sqlRowSetMetaData.getColumnType(i);
       sb.append(getValue(sqlRowSet, i, type));
       if (i < colCount) {
         sb.append(";");
       }
     }
     sb.append("\n");
   }
   return sb.toString();
 }
Esempio n. 5
0
  /*
   * Convert result to a single entry.
   */
  EntityResource<Entity> createEntityResource(String entityType, SqlRowSet rowSet)
      throws JdbcException {

    // Extract the returned column names. May be a subset of the ones
    // requested.
    String[] columnNames = rowSet.getMetaData().getColumnNames();

    // Set cursor to first row
    if (!rowSet.next()) {
      throw (new JdbcException(
          Status.NOT_FOUND, "Row not found. Entry with given key possibly not present."));
    }

    // Build up properties for this row
    EntityProperties properties = new EntityProperties();

    // For all columns in this row.
    for (String columnName : columnNames) {
      Object value = rowSet.getObject(columnName);

      // Only return non null values
      if (null != value) {
        // Add object to the property. getObject() returns an object
        // with the correct java type for each sql type. So we don't
        // need to cast.
        properties.setProperty(new EntityProperty(columnName, value));
      }
    }

    // Make an entity
    Entity entity = new Entity(entityType, properties);

    // Make an entity resource
    EntityResource<Entity> entityResource = new EntityResource<Entity>(entityType, entity);

    // Check for additional rows. Not expected for a 'single'
    // command.
    if (rowSet.next()) {
      throw (new JdbcException(
          Status.INTERNAL_SERVER_ERROR, "Multiple rows returned for a single entity"));
    }

    return entityResource;
  }
  /**
   * this function will return a jsonstring given a resultset in a format that is required for
   * WTF.Grid just use this inside the ur jsp file and set the url for the grid datastore as that
   * jsp file
   */
  public JSONObject GetJsonForGrid(SqlRowSet rs) {
    JSONObject jb = null;
    try {
      SqlRowSetMetaData rsmd = rs.getMetaData();
      JSONArray jArr = new JSONArray();
      while (rs.next()) {
        JSONObject jobj = new JSONObject();
        for (int i = 1; i <= rsmd.getColumnCount(); i++) {
          if (rs.getObject(i) != null) jobj.put(rsmd.getColumnName(i), rs.getObject(i));
          else jobj.put(rsmd.getColumnName(i), "");
        }
        jArr.put(jobj);
      }
      if (jArr.length() > 0) jb = new JSONObject().put("data", jArr);
    } catch (Exception ex) {
      System.out.println("exception -->" + ex);
    }

    return jb;
  }
  private List<ResultsetRowData> fillDatatableResultSetDataRows(final String sql) {

    final SqlRowSet rs = this.jdbcTemplate.queryForRowSet(sql);

    final List<ResultsetRowData> resultsetDataRows = new ArrayList<ResultsetRowData>();

    final SqlRowSetMetaData rsmd = rs.getMetaData();

    while (rs.next()) {
      final List<String> columnValues = new ArrayList<String>();
      for (int i = 0; i < rsmd.getColumnCount(); i++) {
        final String columnName = rsmd.getColumnName(i + 1);
        final String columnValue = rs.getString(columnName);
        columnValues.add(columnValue);
      }

      final ResultsetRowData resultsetDataRow = ResultsetRowData.create(columnValues);
      resultsetDataRows.add(resultsetDataRow);
    }

    return resultsetDataRows;
  }