List of usage examples for org.springframework.jdbc.support.rowset SqlRowSet getMetaData
SqlRowSetMetaData getMetaData();
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()); }/* ww w.j av a 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: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 . ja v a 2s .com 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 w w.j a va2 s .co 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.univocity.app.data.Data.java
public void reloadData() { data.clear();//from w w w . ja v a 2s. c om SqlRowSet queryResult = database.getJdbcTemplate().queryForRowSet(selectScript); SqlRowSetMetaData metaData = queryResult.getMetaData(); columnNames = metaData.getColumnNames(); for (int i = 0; i < columnNames.length; i++) { String label = metaData.getColumnLabel(i + 1); if (label != null) { columnNames[i] = label; } } while (queryResult.next()) { Object[] row = new Object[columnNames.length]; for (int i = 0; i < row.length; i++) { row[i] = queryResult.getObject(i + 1); } data.add(row); } }
From source file:com.google.api.ads.adwords.awalerting.sampleimpl.downloader.SqlDbReportDownloader.java
@Override public List<ReportData> downloadReports(ImmutableAdWordsSession protoSession, Set<Long> clientCustomerIds) { Map<Long, ReportData> reportDataMap = new HashMap<Long, ReportData>(); JdbcTemplate jdbcTemplate = getJdbcTemplate(); String sqlQuery = getSqlQueryWithReportColumnNames(); ReportDefinitionReportType reportType = getReportType(); SqlRowSet rowSet = jdbcTemplate.queryForRowSet(sqlQuery); // Get the column index of customer id. int customerIdColumnIndex = rowSet.findColumn(EXTERNAL_CUSTOMER_ID_REPORT_COLUMN_NAME); Preconditions.checkState(customerIdColumnIndex >= 0, "You must choose \"%s\" field to generate report data", EXTERNAL_CUSTOMER_ID_REPORT_COLUMN_NAME); List<String> columnNames = Arrays.asList(rowSet.getMetaData().getColumnNames()); int columns = columnNames.size(); // Read result into map. int rows = 0; while (rowSet.next()) { rows++;//from w w w . jav a 2 s .c o m List<String> row = new ArrayList<String>(columns); for (int i = 0; i < columns; i++) { row.add(rowSet.getString(i)); } String customerIdStr = row.get(customerIdColumnIndex); Long customerId = Long.parseLong(customerIdStr); ReportData reportData = reportDataMap.get(customerId); if (reportData == null) { reportData = new ReportData(customerId, reportType, columnNames); reportDataMap.put(customerId, reportData); } reportData.addRow(row); } LOGGER.info("Retrieved and parsed {} rows from database.", rows); return new ArrayList<ReportData>(reportDataMap.values()); }
From source file:org.restsql.core.impl.AbstractSqlResourceMetaData.java
/** * Builds table and column meta data./*from ww w. java2 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.hygenics.parser.getDAOTemplate.java
/** * Called when needing a count of distincts or other numerical result. * Returns an Big Integer so only an integer should be used * //from w w w . j a v a 2 s . com * @param sql * @param columns * @return */ public BigDecimal queryForBigInt(String sql) { SqlRowSet rs = this.jdbcTemplateObject.queryForRowSet(sql); if (rs.next()) { if (rs.getMetaData().getColumnNames().length > 0) { return rs.getBigDecimal(1); } } return new BigDecimal(0); }
From source file:com.hygenics.parser.getDAOTemplate.java
/** * Called when needing a count of distincts or other numerical result. * Returns an Integer so only an integer should be used * //from ww w . j a va 2s .c om * @param sql * @param columns * @return */ public int queryForInt(String sql) { SqlRowSet rs = this.jdbcTemplateObject.queryForRowSet(sql); if (rs.next()) { if (rs.getMetaData().getColumnNames().length > 0) { return rs.getInt(1); } } return 0; }
From source file:com.hygenics.parser.getDAOTemplate.java
/** * Called when needing a count of distincts or other numerical result. * Returns an Integer so only an integer should be used * /*from w w w . jav a2 s. c o m*/ * @param sql * @param columns * @return */ public double queryForDoubleString(String sql) { SqlRowSet rs = this.jdbcTemplateObject.queryForRowSet(sql); if (rs.next()) { if (rs.getMetaData().getColumnNames().length > 0) { return rs.getDouble(1); } } return 0; }
From source file:com.hygenics.parser.getDAOTemplate.java
/** * Called when needing a count of distincts or other numerical result. * Returns a Float so only an integer should be used * //from ww w. j a va 2 s .c om * @param sql * @param columns * @return */ public float queryForFloat(String sql) { SqlRowSet rs = this.jdbcTemplateObject.queryForRowSet(sql); if (rs.next()) { if (rs.getMetaData().getColumnNames().length > 0) { return rs.getFloat(1); } } return 0f; }