List of usage examples for org.springframework.jdbc.support.rowset SqlRowSetMetaData getColumnCount
int getColumnCount() throws InvalidResultSetAccessException;
From source file:org.owasp.proxy.http.dao.JdbcMessageDAOTest.java
private static void dump(String sql) { logger.fine("\n" + sql); SqlRowSet rs = dao.getJdbcTemplate().queryForRowSet(sql); try {/*from w w w . j a v a2s. c o m*/ SqlRowSetMetaData rsmd = rs.getMetaData(); int c = rsmd.getColumnCount(); StringBuffer buff = new StringBuffer(); for (int i = 1; i <= c; i++) { buff.append(rsmd.getColumnLabel(i)); buff.append(i == c ? "\n" : "\t"); } logger.fine(buff.toString()); buff.delete(0, buff.length()); while (rs.next()) { for (int i = 1; i <= c; i++) { buff.append(rs.getObject(i)); buff.append(i == c ? "\n" : "\t"); } logger.fine(buff.toString()); buff.delete(0, buff.length()); } logger.fine("================\n\n"); } catch (Exception e) { e.printStackTrace(); } }
From source file:transaction.script.ProjectTrScript.java
/** * @param template//from w ww . j a v a2 s .c o m * @param query * @param conditionsMapList * @return * @throws SQLException */ public static boolean query(JdbcTemplate template, String query, List<Map<String, Object>> conditionsMapList) throws SQLException { logger.info("Query to execute is: " + query); SqlRowSet set = template.queryForRowSet(query); boolean result = true; SqlRowSetMetaData mdata = set.getMetaData(); int columnAmount = mdata.getColumnCount(); logger.info("Columns: " + columnAmount); logger.info("Map size: " + conditionsMapList.size()); //TODO if (set.first()) { set.last(); int rowNum = set.getRow(); result = (rowNum == conditionsMapList.size()); set.beforeFirst(); } else { if (!conditionsMapList.get(0).isEmpty()) { result = false; } } logger.info("Two maps comparison result is " + result); if (result) { while (set.next()) { int rowNum = set.getRow(); Map<String, Object> map = conditionsMapList.get(rowNum - 1); for (int i = 1; i <= columnAmount; i++) { result &= map.containsKey(mdata.getColumnName(i)) && map.get(mdata.getColumnName(i)).toString().equals(set.getObject(i).toString()); } } } return result; }
From source file:com.esa.infocontrol.data.jdbc.BaseDataJDBC.java
public static DataArrayWrapper getList(DataSource dataSource, String query, MapSqlParameterSource params) { LOG.debug("QUERY: {}", query); if (params != null) { LOG.debug("\tPARAMETERS: {}", params.getValues().toString()); }/*from w ww. j a va 2 s. c om*/ NamedParameterJdbcTemplate jdbcTemplate = new NamedParameterJdbcTemplate(dataSource); SqlRowSet rs = jdbcTemplate.queryForRowSet(query, params); SqlRowSetMetaData md = rs.getMetaData(); LOG.debug("\tCOLUMNS: {}", Arrays.toString(md.getColumnNames())); List<DataRow> dataList = new ArrayList<>(); ColumnMetaData[] columnMetaData = new ColumnMetaData[md.getColumnCount()]; for (int i = 1; i <= md.getColumnCount(); ++i) { columnMetaData[i - 1] = new ColumnMetaData(md.getColumnLabel(i), md.getColumnType(i)); } while (rs.next()) { DataRow row = new DataRow(md.getColumnCount()); for (int i = 1; i <= md.getColumnCount(); ++i) { row.add(rs.getString(i)); } dataList.add(row); } return new DataArrayWrapper(dataList, columnMetaData); }
From source file:com.simplymeasured.prognosticator.HiveQueryCursorImpl.java
@Override public Map<String, Object> get() { Map<String, Object> result = Maps.newHashMap(); final SqlRowSetMetaData metadata = rowSet.getMetaData(); for (int i = 1; i <= metadata.getColumnCount(); i++) { String columnTypeName = metadata.getColumnTypeName(i); final Object value; if ("array".equalsIgnoreCase(columnTypeName)) { value = parseJson(rowSet.getString(i), List.class); } else if ("map".equalsIgnoreCase(columnTypeName) || "struct".equalsIgnoreCase(columnTypeName)) { value = parseJson(rowSet.getString(i), Map.class); } else if ("string".equalsIgnoreCase(columnTypeName)) { value = HiveUtils.unescapeString(rowSet.getString(i)); } else {// ww w . j a v a 2s. com value = rowSet.getObject(i); } result.put(metadata.getColumnName(i), value); } return result; }
From source file:com.simplymeasured.prognosticator.HiveQueryCursorImplTest.java
License:asdf
@Test public void testGet() { // build up a result inside the mock, test it. // column 1 = string, named STR // column 2 = long, named LNG // column 3 = Map, named MAP // column 4 = Array, named LIST // column 5 = Struct (serialized to a Java Map), named STRUCT SqlRowSetMetaData metadata = mock(SqlRowSetMetaData.class); when(metadata.getColumnCount()).thenReturn(5); when(metadata.getColumnName(1)).thenReturn("STR"); when(metadata.getColumnTypeName(1)).thenReturn("STRING"); when(metadata.getColumnName(2)).thenReturn("LNG"); when(metadata.getColumnTypeName(2)).thenReturn("BIGINT"); when(metadata.getColumnName(3)).thenReturn("MAP"); when(metadata.getColumnTypeName(3)).thenReturn("MAP"); when(metadata.getColumnName(4)).thenReturn("LIST"); when(metadata.getColumnTypeName(4)).thenReturn("ARRAY"); when(metadata.getColumnName(5)).thenReturn("STRUCT"); when(metadata.getColumnTypeName(5)).thenReturn("STRUCT"); when(rowSet.getMetaData()).thenReturn(metadata); when(rowSet.getString(1)).thenReturn("My string"); when(rowSet.getObject(2)).thenReturn(123L); when(rowSet.getString(3)).thenReturn("{\"foo\":\"str1\", \"bar\":\"str2\"}"); when(rowSet.getString(4)).thenReturn("[1, 2, 3]"); when(rowSet.getString(5)).thenReturn("{\"struct1\": \"asdf\", \"struct2\": 1234 }"); Map<String, Object> result = cursor.get(); Assert.assertNotNull(result);// w w w . ja va 2 s. co m Assert.assertFalse(result.isEmpty()); Map<String, Object> expected = new HashMap<String, Object>() { { put("STR", "My string"); put("LNG", 123L); put("MAP", new HashMap<String, String>() { { put("foo", "str1"); put("bar", "str2"); } }); put("LIST", new ArrayList<Integer>() { { add(1); add(2); add(3); } }); put("STRUCT", new HashMap<String, Object>() { { put("struct1", "asdf"); put("struct2", 1234); } }); } }; Assert.assertEquals(expected, result); }
From source file:org.restsql.core.impl.AbstractSqlResourceMetaData.java
/** * Builds table and column meta data.// ww w . j av a2 s.c o m * * @throws SqlResourceException */ @SuppressWarnings("fallthrough") private void buildTablesAndColumns(final SqlRowSet resultSet) throws SqlResourceException { final SqlRowSetMetaData resultSetMetaData = resultSet.getMetaData(); final int columnCount = resultSetMetaData.getColumnCount(); allReadColumns = new ArrayList<ColumnMetaData>(columnCount); parentReadColumns = new ArrayList<ColumnMetaData>(columnCount); childReadColumns = new ArrayList<ColumnMetaData>(columnCount); tableMap = new HashMap<String, TableMetaData>(DEFAULT_NUMBER_TABLES); tables = new ArrayList<TableMetaData>(DEFAULT_NUMBER_TABLES); childPlusExtTables = new ArrayList<TableMetaData>(DEFAULT_NUMBER_TABLES); parentPlusExtTables = new ArrayList<TableMetaData>(DEFAULT_NUMBER_TABLES); final HashSet<String> databases = new HashSet<String>(DEFAULT_NUMBER_DATABASES); for (int colNumber = 1; colNumber <= columnCount; colNumber++) { final String databaseName, qualifiedTableName, tableName; // boolean readOnly = isColumnReadOnly(resultSetMetaData, // colNumber); // if (readOnly) { databaseName = SqlResourceDefinitionUtils.getDefaultDatabase(definition); tableName = SqlResourceDefinitionUtils.getTable(definition, TableRole.Parent).getName(); qualifiedTableName = getQualifiedTableName(databaseName, tableName); final ColumnMetaDataImpl column = new ColumnMetaDataImpl(colNumber, databaseName, qualifiedTableName, tableName, getColumnName(definition, resultSetMetaData, colNumber), resultSetMetaData.getColumnLabel(colNumber), resultSetMetaData.getColumnTypeName(colNumber), resultSetMetaData.getColumnType(colNumber), true, this); TableMetaDataImpl table = (TableMetaDataImpl) tableMap.get(column.getQualifiedTableName()); if (table == null) { // Create table metadata object and add to special references final Table tableDef = SqlResourceDefinitionUtils.getTable(definition, column); if (tableDef == null) { throw new SqlResourceException("Definition requires table element for " + column.getTableName() + ", referenced by column " + column.getColumnLabel()); } table = new TableMetaDataImpl(tableName, qualifiedTableName, databaseName, TableRole.valueOf(tableDef.getRole())); tableMap.put(column.getQualifiedTableName(), table); tables.add(table); switch (table.getTableRole()) { case Parent: parentTable = table; if (tableDef.getAlias() != null) { table.setTableAlias(tableDef.getAlias()); } // fall through case ParentExtension: parentPlusExtTables.add(table); break; case Child: childTable = table; if (tableDef.getAlias() != null) { table.setTableAlias(tableDef.getAlias()); } // fall through case ChildExtension: childPlusExtTables.add(table); break; case Join: // unlikely to be in the select columns, but just in // case joinTable = table; joinList = new ArrayList<TableMetaData>(1); joinList.add(joinTable); break; default: // Unknown } } // Add column to the table table.addColumn(column); column.setTableRole(table.getTableRole()); // Add column to special column lists allReadColumns.add(column); switch (table.getTableRole()) { case Parent: case ParentExtension: parentReadColumns.add(column); break; case Child: case ChildExtension: childReadColumns.add(column); break; default: // Unknown } } // Determine number of databases multipleDatabases = databases.size() > 1; }
From source file:com.krawler.crm.reportBuilder.bizservice.ReportBuilderServiceImpl.java
public List getIdsList(SqlRowSet rs) { List<Object> ll = new ArrayList<Object>(); List<String> idList = new ArrayList<String>(); Boolean productColFlag = false; try {/*w ww . java 2 s . c o m*/ SqlRowSetMetaData rsmd = rs.getMetaData(); while (rs.next()) { for (int i = 1; i <= rsmd.getColumnCount(); i++) { String columnLabel = rsmd.getColumnLabel(i); if (columnLabel.equals(Constants.Crm_leadid) || columnLabel.equals(Constants.Crm_productid) || columnLabel.equals(Constants.Crm_accountid) || columnLabel.equals(Constants.Crm_contactid) || columnLabel.equals(Constants.Crm_caseid) || columnLabel.equals(Constants.Crm_opportunityid)) { if (rs.getObject(i) != null) { idList.add(rs.getObject(i).toString()); } } else if (columnLabel.equals(Constants.Crm_lead_product_key) || columnLabel.equals(Constants.Crm_opportunity_product_key) || columnLabel.equals(Constants.Crm_account_product_key) || columnLabel.equals(Constants.Crm_case_product_key)) { productColFlag = true; } } } rs.beforeFirst(); ll.add(idList); ll.add(productColFlag); } catch (Exception ex) { LOGGER.warn(ex.getMessage(), ex); } return ll; }
From source file:com.krawler.crm.reportBuilder.bizservice.ReportBuilderServiceImpl.java
public JSONObject getReportJSON(int moduleId, SqlRowSet rs, HashMap<String, DefaultMasterItem> defaultMasterMap, HashMap<String, FieldComboData> customComboMap, Map<String, List<CrmProduct>> productsMap, Map ownersMap, HashMap<String, String> dataIndexReftableMap, boolean detailFlag, Boolean productColFlag) { JSONObject jb = new JSONObject(); try {//from w w w .j av a 2 s .c o m SqlRowSetMetaData rsmd = rs.getMetaData(); while (rs.next()) { String primaryKeyVal = ""; JSONObject jobj = new JSONObject(); for (int i = 1; i <= rsmd.getColumnCount(); i++) { String columnLabel = rsmd.getColumnLabel(i); // boolean linkFlag = !detailFlag && customReportObj.isGroupflag() && columnLabel.equals(customReportObj.getGroupfield()); if (rs.getObject(i) != null) { if (columnLabel.equals(Constants.Crm_leadid) || columnLabel.equals(Constants.Crm_contactid) || columnLabel.equals(Constants.Crm_accountid) || columnLabel.equals(Constants.Crm_caseid) || columnLabel.equals(Constants.Crm_opportunityid) || columnLabel.equals(Constants.Crm_productid)) { primaryKeyVal = rs.getObject(i).toString(); } if (dataIndexReftableMap.containsKey(columnLabel) && dataIndexReftableMap.get(columnLabel).equals("defaultmasteritem")) { if (defaultMasterMap.containsKey(rs.getObject(i))) { jobj.put(columnLabel, defaultMasterMap.get(rs.getObject(i)).getValue()); jobj.put(columnLabel + "_id", rs.getObject(i)); } else { jobj.put(columnLabel, ""); } } else if (dataIndexReftableMap.containsKey(columnLabel) && dataIndexReftableMap.get(columnLabel).equals("users")) { User userObj = (User) kwlCommonTablesDAOObj .getClassObject("com.krawler.common.admin.User", rs.getObject(i).toString()); if (userObj != null) { jobj.put(columnLabel, userObj.getFullname()); jobj.put(columnLabel + "_id", rs.getObject(i)); } else { jobj.put(columnLabel, "(Blank)"); } } else if (dataIndexReftableMap.containsKey(columnLabel) && dataIndexReftableMap.get(columnLabel).equals(Constants.Crm_account)) { CrmAccount accObj = (CrmAccount) kwlCommonTablesDAOObj .getClassObject(Constants.Crm_account_classpath, rs.getObject(i).toString()); if (accObj != null) { jobj.put(columnLabel, accObj.getAccountname()); jobj.put(columnLabel + "_id", rs.getObject(i)); } else { jobj.put(columnLabel, rs.getObject(i)); } } else if (dataIndexReftableMap.containsKey(columnLabel) && dataIndexReftableMap.get(columnLabel).equals("fieldcombodata")) { String[] comboIds = rs.getObject(i).toString().split(","); String comboValue = " "; String comboValueId = ""; for (int cnt = 0; cnt < comboIds.length; cnt++) { String comboid = comboIds[cnt]; if (customComboMap.containsKey(comboid)) { comboValue += customComboMap.get(comboid).getValue() + ","; comboValueId += customComboMap.get(comboid).getId() + ","; } } jobj.put(columnLabel, comboValue.substring(0, comboValue.length() - 1)); jobj.put(columnLabel + "_id", (comboValueId.length() > 0 ? comboValueId.substring(0, comboValueId.length() - 1) : comboValueId)); } else { jobj.put(columnLabel, rs.getObject(i)); } } else { jobj.put(columnLabel, ""); } } if (productColFlag && productsMap.containsKey(primaryKeyVal)) { List<CrmProduct> crmProducts = productsMap.get(primaryKeyVal); String productNames = ""; for (CrmProduct product : crmProducts) { productNames += product.getProductname() + ", "; } String key = ""; productNames = productNames.substring(0, productNames.lastIndexOf(",")); switch (moduleId) { case 1: key = Constants.Crm_account_product_key; break; case 2: key = Constants.Crm_lead_product_key; break; case 3: key = Constants.Crm_case_product_key; break; case 5: key = Constants.Crm_opportunity_product_key; break; } jobj.put(key, productNames); } jb.append("data", jobj); } } catch (Exception ex) { LOGGER.warn(ex.getMessage(), ex); } return jb; }
From source file:com.gst.infrastructure.dataqueries.service.ReadWriteNonCoreDataServiceImpl.java
private List<ResultsetRowData> fillDatatableResultSetDataRows(final String sql) { final SqlRowSet rs = this.jdbcTemplate.queryForRowSet(sql); final List<ResultsetRowData> resultsetDataRows = new ArrayList<>(); final SqlRowSetMetaData rsmd = rs.getMetaData(); while (rs.next()) { final List<String> columnValues = new ArrayList<>(); for (int i = 0; i < rsmd.getColumnCount(); i++) { final String columnName = rsmd.getColumnName(i + 1); final String columnValue = rs.getString(columnName); columnValues.add(columnValue); }//from w w w.j a v a2 s . c om final ResultsetRowData resultsetDataRow = ResultsetRowData.create(columnValues); resultsetDataRows.add(resultsetDataRow); } return resultsetDataRows; }
From source file:ome.testing.OMEData.java
/** * returns a list of results from the sql statement. if there is more than * one column in the result set, a map from column name to Object is * returned, else the Object itself.// w ww . jav a 2 s. c o m * * @param sql * @return */ List runSql(String sql) { JdbcTemplate jt = new JdbcTemplate(ds); SqlRowSet rows = jt.queryForRowSet(sql); List result = new ArrayList(); while (rows.next()) { SqlRowSetMetaData meta = rows.getMetaData(); int count = meta.getColumnCount(); if (count > 1) { Map cols = new HashMap(); String[] names = meta.getColumnNames(); for (int i = 0; i < names.length; i++) { cols.put(names[i], rows.getObject(names[i])); } result.add(cols); } else { result.add(rows.getObject(1)); } } log.debug("SQL:" + sql + "\n\nResult:" + result); return result; }