-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #54 from instana/Informix_QueryOptimization
Informix query optimization
- Loading branch information
Showing
4 changed files
with
125 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
.../main/java/com/instana/dc/rdb/impl/informix/metric/collection/MetricsDataQueryConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/* | ||
* (c) Copyright IBM Corp. 2024 | ||
* (c) Copyright Instana Inc. | ||
*/ | ||
|
||
package com.instana.dc.rdb.impl.informix.metric.collection; | ||
import static com.instana.dc.rdb.DbDcUtil.getMetricWithSql; | ||
import static com.instana.dc.rdb.DbDcUtil.getSimpleMetricWithSql; | ||
import com.instana.dc.SimpleQueryResult; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
import org.apache.commons.dbcp2.BasicDataSource; | ||
import java.sql.Connection; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.HashMap; | ||
import java.sql.SQLException; | ||
import java.sql.Connection; | ||
import java.sql.ResultSet; | ||
import java.sql.Statement; | ||
|
||
/** | ||
* This class which holds the meta data of a given metrics | ||
*/ | ||
public class MetricsDataQueryConfig { | ||
|
||
private static final Logger logger = Logger.getLogger(MetricsDataQueryConfig.class.getName()); | ||
private String query; | ||
private final Class<?> returnType; | ||
private String metricKey; | ||
private String scriptName; | ||
private final BasicDataSource dataSource; | ||
private ResultSet rs; | ||
private String[] attr; | ||
|
||
private HashMap<String,List<SimpleQueryResult>> results; | ||
|
||
|
||
public MetricsDataQueryConfig(String query, Class<?> returnType, BasicDataSource dataSource, String... attr) { | ||
this.query = query; | ||
this.returnType = returnType; | ||
this.attr = attr; | ||
this.dataSource = dataSource; | ||
this.results = new HashMap<String,List<SimpleQueryResult>>(); | ||
for (int attrIndex = 0;attrIndex<attr.length;attrIndex++) { | ||
this.results.put(this.attr[attrIndex],new ArrayList<>()); | ||
} | ||
} | ||
|
||
public void fetchQueryResults() { | ||
try (Connection connection = this.dataSource.getConnection()) { | ||
|
||
ResultSet rs = executeQuery(connection, this.query); | ||
if (rs.isClosed()) { | ||
logger.severe("getMetricWithSql: ResultSet is closed"); | ||
} | ||
while (rs.next()) { | ||
Object obj = rs.getObject(2); | ||
if (obj == null) { | ||
obj = "null"; | ||
} | ||
if (obj instanceof String) { | ||
obj = ((String) obj).trim(); | ||
} | ||
for(int attrIndex = 0;attrIndex<this.attr.length;attrIndex++) { | ||
if(attrIndex == 1) { | ||
continue; | ||
} | ||
SimpleQueryResult result = new SimpleQueryResult((Number) rs.getObject(attrIndex+1)); | ||
result.setAttribute(this.attr[1], obj); | ||
result.setKey(obj.toString()); | ||
List<SimpleQueryResult> ls = this.results.get(this.attr[attrIndex]); | ||
ls.add(result); | ||
this.results.put(this.attr[attrIndex],ls); | ||
} | ||
} | ||
} | ||
catch (SQLException exp) { | ||
logger.log(Level.SEVERE, "Unable to execute the sql command, Exception: " + exp); | ||
} | ||
} | ||
|
||
public static ResultSet executeQuery(Connection connection, String query) throws SQLException { | ||
Statement statement = connection.createStatement(); | ||
return statement.executeQuery(query); | ||
} | ||
|
||
|
||
public String getMetricKey() { | ||
return metricKey; | ||
} | ||
|
||
public String[] getAttr() { | ||
return attr; | ||
} | ||
|
||
public String getQuery() { | ||
return query; | ||
} | ||
|
||
public Class<?> getReturnType() { | ||
return returnType; | ||
} | ||
|
||
public List<SimpleQueryResult> getResults(String key) { | ||
return this.results.get(key); | ||
} | ||
} |