Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
vertigo - simple java starter Copyright (C) 2013, KleeGroup, direction.technique@kleegroup.com (http://www.kleegroup.com) KleeGroup, Centre d'affaire la Boursidiere - BP 159 - 92357 Le Plessis Robinson Cedex - France Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 
 package io.vertigo.studio.plugins.reporting.task.metrics.explainplan;
 
 
Plugin qui va lancer la commande de calcul puis d'affichage du plan d'exécution.

Author(s):
tchassagnette
 
 public final class ExplainPlanMetricEngine implements MetricEngine<TaskDefinition> {
 	private int sequence;
 
 	private final TaskManager taskManager;

Constructeur apr défaut.

Parameters:
taskManager Manager des tasks
 
 	public ExplainPlanMetricEngine(final TaskManager taskManager) {
 		Assertion.checkNotNull(taskManager);
 		//-----
 		this. = taskManager;
 	}

 
 	public Metric execute(final TaskDefinition taskDefinition) {
 		Assertion.checkNotNull(taskDefinition);
 		//-----
 		try {
 			if (TaskEngineSelect.class.isAssignableFrom(taskDefinition.getTaskEngineClass())) {
 				final int currentSequence = ++;
 				final String explainPlan = getExplainPlanElement(taskDefinitioncurrentSequence);
 				return createMetric(explainPlan.null);
 			}
 
 			return createMetric(null.null);
 		} catch (final Exception e) {
 			return createMetric(null.e);
 		}
 	}

Parameters:
explainPlan Plan d'execution
 
 	private static Metric createMetric(final String explainPlanfinal Status statusfinal Throwable throwable) {
 		Assertion.checkNotNull(explainPlan);
 		//-----
 		final Integer value = createValue(explainPlan);
 		final String valueInformation = createValueInformation(explainPlanstatusthrowable);
 		return new MetricBuilder()
 				.withTitle("Explain Plan")
 				.withStatus(status)
 				.withValue(value)
 				.withValueInformation(valueInformation)
 				.build();
 	}
 
	private static Integer createValue(final String explainPlan) {
		if (explainPlan != null) {
			return explainPlan.split("TABLE ACCESS FULL").length - 1;
		}
		return null;
	}
	private static String createValueInformation(final String explainPlanfinal Status statusfinal Throwable throwable) {
		if (explainPlan != null) {
			return explainPlan.replaceAll("TABLE ACCESS FULL""<b style='color: red;'>TABLE ACCESS FULL</b>");
		}
		if (status == .) {
			final StringWriter sw = new StringWriter();
			throwable.printStackTrace(new PrintWriter(sw));
			return sw.getBuffer().toString();
		}
		return "Rejected";
	}
	private String getExplainPlanElement(final TaskDefinition taskDefinitionfinal int currentSequence) {
		final String taskDefinitionName = truncate("TK_EXPLAIN_" + taskDefinition.getName(), 59, "_");
		final String explainPlanRequest = "explain plan set statement_id = 'PLAN_" + currentSequence + "' for " + taskDefinition.getRequest();
		//final String explainPlanRequest = "explain plan for " + taskDefinition.getRequest();
		final TaskDefinitionBuilder taskDefinitionBuilder = new TaskDefinitionBuilder(taskDefinitionName)
				.withEngine(taskDefinition.getTaskEngineClass())
				.withRequest(explainPlanRequest)
		for (final TaskAttribute attribute : taskDefinition.getAttributes()) {
			if (attribute.isIn()) {
				taskDefinitionBuilder.withAttribute(attribute.getName(), attribute.getDomain(), attribute.isNotNull(), attribute.isIn());
else {
				taskDefinitionBuilder.withAttribute(attribute.getName(), attribute.getDomain(), falsefalse);
			}
		}
		final TaskDefinition taskExplain = taskDefinitionBuilder.build();
		try {
			final Task currentTask = new TaskPopulator(taskExplain).populateTask();
			/*TaskResult taskResult =*/.execute(currentTask);
			//On n'exploite pas le résultat
			return readExplainPlan(taskDefinitioncurrentSequence);
catch (final Exception e) {
			throw new RuntimeException("explainPlanElement"e);
		}
	}
	private static String truncate(final String valuefinal int maxSizefinal String endTruncString) {
		if (value.length() <= maxSize) {
			return value;
		}
		return value.substring(0, maxSize - endTruncString.length()) + endTruncString;
	}
	private static String readExplainPlan(final TaskDefinition taskDefinitionfinal int currentSequence) {
		//		final String taskDefinitionName = "TK_EXPLAIN_PLAN";
		//		final TaskDefinitionFactory taskDefinitionFactory = getTaskManager().createTaskDefinitionFactory();
		//		final String explainPlanRequest = "select * from plan_table where statement_id = #STATEMENT_ID# order by id";
		//		taskDefinitionFactory.init(taskDefinitionName, TaskEngineSelect.class, explainPlanRequest, TaskEngineSelect.class.getPackage().getName());
		//		taskDefinitionFactory.addAttribute(taskDefinitionName, "STATEMENT_ID", getDomainManager().getNameSpace().getDomain("DO_LIBELLE_LONG"), true, true);
		//
		//		final TaskDefinition taskExplain = taskDefinitionFactory.createTaskDefinition(taskDefinitionName);
		//		try {
		//			final Task currentTask = getTaskManager().getFactory().createTask(taskExplain);
		//			currentTask.setValue("STATEMENT_ID", taskDefinition.getShortName());
		//			getWorkManager().process(currentTask);
		//			return new ExplainPlanMetric();
		//		} catch (final Exception e) {
		//			throw new KSystemException("explainPlanElement", e);
		//		}
		final StringBuilder sb = new StringBuilder();
		final String sql = "SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY('PLAN_TABLE', 'PLAN_" + currentSequence + "'))";
		//final String sql = "SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY())";
		final SqlConnection kConnection = getCurrentConnection();
		final Connection connection = kConnection.getJdbcConnection();
		try (final PreparedStatement statement = connection.prepareStatement(sql)) {
			try (final ResultSet resultSet = statement.executeQuery()) {
				while (resultSet.next()) {
					sb.append(resultSet.getString(1)).append("\n");
				}
				return sb.toString();
			}
catch (final SQLException e) {
			throw new RuntimeException("doGetExplainPlan"e);
		}
	}

Retourne la connexion SQL de cette transaction en la demandant au pool de connexion si nécessaire.

Returns:
Connexion SQL
	private static SqlConnection getCurrentConnection() {
	}
New to GrepCode? Check out our FAQ X