use of org.h2.tools.SimpleResultSet in project ignite by apache.
the class GAGridFunction method getChromosomes.
/**
* Helper routine to return 'pivoted' results using the provided query param
*
* @param query Sql
* @return Result set
*/
private static SimpleResultSet getChromosomes(String query) {
Ignite ignite = Ignition.localIgnite();
List<Chromosome> chromosomes = GAGridUtils.getChromosomes(ignite, query);
SimpleResultSet rs2 = new SimpleResultSet();
Chromosome aChrom = chromosomes.get(0);
int genesCount = aChrom.getGenes().length;
rs2.addColumn("Chromosome Id", Types.INTEGER, 0, 0);
rs2.addColumn("Fitness Score", Types.DOUBLE, 0, 0);
for (int i = 0; i < genesCount; i++) {
int columnIndex = i + 1;
rs2.addColumn("Gene " + columnIndex, Types.VARCHAR, 0, 0);
}
for (Chromosome rowChrom : chromosomes) {
Object[] row = new Object[genesCount + 2];
row[0] = rowChrom.id();
row[1] = rowChrom.getFitnessScore();
List<Gene> genes = GAGridUtils.getGenesInOrderForChromosome(ignite, rowChrom);
int i = 2;
for (Gene gene : genes) {
row[i] = gene.getValue().toString();
i = i + 1;
}
// Add a row for an individual Chromosome
rs2.addRow(row);
}
return rs2;
}
use of org.h2.tools.SimpleResultSet in project h2database by h2database.
the class FullText method search.
/**
* Do the search.
*
* @param conn the database connection
* @param text the query
* @param limit the limit
* @param offset the offset
* @param data whether the raw data should be returned
* @return the result set
*/
protected static ResultSet search(Connection conn, String text, int limit, int offset, boolean data) throws SQLException {
SimpleResultSet result = createResultSet(data);
if (conn.getMetaData().getURL().startsWith("jdbc:columnlist:")) {
// this is just to query the result set columns
return result;
}
if (text == null || text.trim().length() == 0) {
return result;
}
FullTextSettings setting = FullTextSettings.getInstance(conn);
if (!setting.isInitialized()) {
init(conn);
}
Set<String> words = new HashSet<>();
addWords(setting, words, text);
Set<Integer> rIds = null, lastRowIds;
PreparedStatement prepSelectMapByWordId = setting.prepare(conn, SELECT_MAP_BY_WORD_ID);
for (String word : words) {
lastRowIds = rIds;
rIds = new HashSet<>();
Integer wId = setting.getWordId(word);
if (wId == null) {
continue;
}
prepSelectMapByWordId.setInt(1, wId.intValue());
ResultSet rs = prepSelectMapByWordId.executeQuery();
while (rs.next()) {
Integer rId = rs.getInt(1);
if (lastRowIds == null || lastRowIds.contains(rId)) {
rIds.add(rId);
}
}
}
if (rIds == null || rIds.isEmpty()) {
return result;
}
PreparedStatement prepSelectRowById = setting.prepare(conn, SELECT_ROW_BY_ID);
int rowCount = 0;
for (int rowId : rIds) {
prepSelectRowById.setInt(1, rowId);
ResultSet rs = prepSelectRowById.executeQuery();
if (!rs.next()) {
continue;
}
if (offset > 0) {
offset--;
} else {
String key = rs.getString(1);
int indexId = rs.getInt(2);
IndexInfo index = setting.getIndexInfo(indexId);
if (data) {
Object[][] columnData = parseKey(conn, key);
result.addRow(index.schema, index.table, columnData[0], columnData[1], 1.0);
} else {
String query = StringUtils.quoteIdentifier(index.schema) + "." + StringUtils.quoteIdentifier(index.table) + " WHERE " + key;
result.addRow(query, 1.0);
}
rowCount++;
if (limit > 0 && rowCount >= limit) {
break;
}
}
}
return result;
}
use of org.h2.tools.SimpleResultSet in project h2database by h2database.
the class FullTextLucene method search.
/**
* Do the search.
*
* @param conn the database connection
* @param text the query
* @param limit the limit
* @param offset the offset
* @param data whether the raw data should be returned
* @return the result set
*/
protected static ResultSet search(Connection conn, String text, int limit, int offset, boolean data) throws SQLException {
SimpleResultSet result = createResultSet(data);
if (conn.getMetaData().getURL().startsWith("jdbc:columnlist:")) {
// this is just to query the result set columns
return result;
}
if (text == null || text.trim().length() == 0) {
return result;
}
try {
IndexAccess access = getIndexAccess(conn);
// take a reference as the searcher may change
IndexSearcher searcher = access.getSearcher();
try {
// reuse the same analyzer; it's thread-safe;
// also allows subclasses to control the analyzer used.
Analyzer analyzer = access.writer.getAnalyzer();
QueryParser parser = new QueryParser(Version.LUCENE_30, LUCENE_FIELD_DATA, analyzer);
Query query = parser.parse(text);
// Lucene 3 insists on a hard limit and will not provide
// a total hits value. Take at least 100 which is
// an optimal limit for Lucene as any more
// will trigger writing results to disk.
int maxResults = (limit == 0 ? 100 : limit) + offset;
TopDocs docs = searcher.search(query, maxResults);
if (limit == 0) {
limit = docs.totalHits;
}
for (int i = 0, len = docs.scoreDocs.length; i < limit && i + offset < docs.totalHits && i + offset < len; i++) {
ScoreDoc sd = docs.scoreDocs[i + offset];
Document doc = searcher.doc(sd.doc);
float score = sd.score;
String q = doc.get(LUCENE_FIELD_QUERY);
if (data) {
int idx = q.indexOf(" WHERE ");
JdbcConnection c = (JdbcConnection) conn;
Session session = (Session) c.getSession();
Parser p = new Parser(session);
String tab = q.substring(0, idx);
ExpressionColumn expr = (ExpressionColumn) p.parseExpression(tab);
String schemaName = expr.getOriginalTableAliasName();
String tableName = expr.getColumnName();
q = q.substring(idx + " WHERE ".length());
Object[][] columnData = parseKey(conn, q);
result.addRow(schemaName, tableName, columnData[0], columnData[1], score);
} else {
result.addRow(q, score);
}
}
} finally {
access.returnSearcher(searcher);
}
} catch (Exception e) {
throw convertException(e);
}
return result;
}
use of org.h2.tools.SimpleResultSet in project h2database by h2database.
the class WebApp method getMetaResultSet.
private ResultSet getMetaResultSet(Connection conn, String sql) throws SQLException {
DatabaseMetaData meta = conn.getMetaData();
if (isBuiltIn(sql, "@best_row_identifier")) {
String[] p = split(sql);
int scale = p[4] == null ? 0 : Integer.parseInt(p[4]);
boolean nullable = Boolean.parseBoolean(p[5]);
return meta.getBestRowIdentifier(p[1], p[2], p[3], scale, nullable);
} else if (isBuiltIn(sql, "@catalogs")) {
return meta.getCatalogs();
} else if (isBuiltIn(sql, "@columns")) {
String[] p = split(sql);
return meta.getColumns(p[1], p[2], p[3], p[4]);
} else if (isBuiltIn(sql, "@column_privileges")) {
String[] p = split(sql);
return meta.getColumnPrivileges(p[1], p[2], p[3], p[4]);
} else if (isBuiltIn(sql, "@cross_references")) {
String[] p = split(sql);
return meta.getCrossReference(p[1], p[2], p[3], p[4], p[5], p[6]);
} else if (isBuiltIn(sql, "@exported_keys")) {
String[] p = split(sql);
return meta.getExportedKeys(p[1], p[2], p[3]);
} else if (isBuiltIn(sql, "@imported_keys")) {
String[] p = split(sql);
return meta.getImportedKeys(p[1], p[2], p[3]);
} else if (isBuiltIn(sql, "@index_info")) {
String[] p = split(sql);
boolean unique = Boolean.parseBoolean(p[4]);
boolean approx = Boolean.parseBoolean(p[5]);
return meta.getIndexInfo(p[1], p[2], p[3], unique, approx);
} else if (isBuiltIn(sql, "@primary_keys")) {
String[] p = split(sql);
return meta.getPrimaryKeys(p[1], p[2], p[3]);
} else if (isBuiltIn(sql, "@procedures")) {
String[] p = split(sql);
return meta.getProcedures(p[1], p[2], p[3]);
} else if (isBuiltIn(sql, "@procedure_columns")) {
String[] p = split(sql);
return meta.getProcedureColumns(p[1], p[2], p[3], p[4]);
} else if (isBuiltIn(sql, "@schemas")) {
return meta.getSchemas();
} else if (isBuiltIn(sql, "@tables")) {
String[] p = split(sql);
String[] types = p[4] == null ? null : StringUtils.arraySplit(p[4], ',', false);
return meta.getTables(p[1], p[2], p[3], types);
} else if (isBuiltIn(sql, "@table_privileges")) {
String[] p = split(sql);
return meta.getTablePrivileges(p[1], p[2], p[3]);
} else if (isBuiltIn(sql, "@table_types")) {
return meta.getTableTypes();
} else if (isBuiltIn(sql, "@type_info")) {
return meta.getTypeInfo();
} else if (isBuiltIn(sql, "@udts")) {
String[] p = split(sql);
int[] types;
if (p[4] == null) {
types = null;
} else {
String[] t = StringUtils.arraySplit(p[4], ',', false);
types = new int[t.length];
for (int i = 0; i < t.length; i++) {
types[i] = Integer.parseInt(t[i]);
}
}
return meta.getUDTs(p[1], p[2], p[3], types);
} else if (isBuiltIn(sql, "@version_columns")) {
String[] p = split(sql);
return meta.getVersionColumns(p[1], p[2], p[3]);
} else if (isBuiltIn(sql, "@memory")) {
SimpleResultSet rs = new SimpleResultSet();
rs.addColumn("Type", Types.VARCHAR, 0, 0);
rs.addColumn("KB", Types.VARCHAR, 0, 0);
rs.addRow("Used Memory", "" + Utils.getMemoryUsed());
rs.addRow("Free Memory", "" + Utils.getMemoryFree());
return rs;
} else if (isBuiltIn(sql, "@info")) {
SimpleResultSet rs = new SimpleResultSet();
rs.addColumn("KEY", Types.VARCHAR, 0, 0);
rs.addColumn("VALUE", Types.VARCHAR, 0, 0);
rs.addRow("conn.getCatalog", conn.getCatalog());
rs.addRow("conn.getAutoCommit", "" + conn.getAutoCommit());
rs.addRow("conn.getTransactionIsolation", "" + conn.getTransactionIsolation());
rs.addRow("conn.getWarnings", "" + conn.getWarnings());
String map;
try {
map = "" + conn.getTypeMap();
} catch (SQLException e) {
map = e.toString();
}
rs.addRow("conn.getTypeMap", "" + map);
rs.addRow("conn.isReadOnly", "" + conn.isReadOnly());
rs.addRow("conn.getHoldability", "" + conn.getHoldability());
addDatabaseMetaData(rs, meta);
return rs;
} else if (isBuiltIn(sql, "@attributes")) {
String[] p = split(sql);
return meta.getAttributes(p[1], p[2], p[3], p[4]);
} else if (isBuiltIn(sql, "@super_tables")) {
String[] p = split(sql);
return meta.getSuperTables(p[1], p[2], p[3]);
} else if (isBuiltIn(sql, "@super_types")) {
String[] p = split(sql);
return meta.getSuperTypes(p[1], p[2], p[3]);
} else if (isBuiltIn(sql, "@prof_stop")) {
if (profiler != null) {
profiler.stopCollecting();
SimpleResultSet rs = new SimpleResultSet();
rs.addColumn("Top Stack Trace(s)", Types.VARCHAR, 0, 0);
rs.addRow(profiler.getTop(3));
profiler = null;
return rs;
}
}
return null;
}
use of org.h2.tools.SimpleResultSet in project h2database by h2database.
the class TestValue method testResultSetOperation.
private void testResultSetOperation(Object obj) throws SQLException {
SimpleResultSet rs = new SimpleResultSet();
rs.setAutoClose(false);
int valueType = DataType.getTypeFromClass(obj.getClass());
int sqlType = DataType.convertTypeToSQLType(valueType);
rs.addColumn("X", sqlType, 10, 0);
rs.addRow(new Object[] { obj });
rs.next();
Value v = DataType.readValue(null, rs, 1, valueType);
Value v2 = DataType.convertToValue(null, obj, valueType);
if (v.getType() == Value.RESULT_SET) {
assertEquals(v.toString(), v2.toString());
} else {
assertTrue(v.equals(v2));
}
}
Aggregations