/*
 * Copyright 2010-2012 Luca Garulli (l.garulli--at--orientechnologies.com)
 *
 * 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 com.orientechnologies.orient.core.sql;

import java.util.Collection;
import java.util.Map;

import com.orientechnologies.orient.core.command.OCommandRequest;
import com.orientechnologies.orient.core.record.impl.ODocument;

/**
 * Explains the execution of a command returning profiling information.
 * 
 * @author Luca Garulli
 */
public class OCommandExecutorSQLExplain extends OCommandExecutorSQLDelegate {
  public static final String KEYWORD_EXPLAIN = "EXPLAIN";

  @SuppressWarnings("unchecked")
  @Override
  public OCommandExecutorSQLExplain parse(OCommandRequest iCommand) {
    String cmd = ((OCommandSQL) iCommand).getText();
    super.parse(new OCommandSQL(cmd.substring(KEYWORD_EXPLAIN.length())));
    return this;
  }

  @Override
  public Object execute(Map<Object, Object> iArgs) {
    delegate.getContext().setRecordingMetrics(true);

    final long startTime = System.currentTimeMillis();

    final Object result = super.execute(iArgs);
    final ODocument report = new ODocument(delegate.getContext().getVariables());

    report.field("elapsed", System.currentTimeMillis() - startTime);

    if (result instanceof Collection<?>) {
      report.field("resultType", "collection");
      report.field("resultSize", ((Collection<?>) result).size());
    } else if (result instanceof ODocument) {
      report.field("resultType", "document");
      report.field("resultSize", 1);
    } else if (result instanceof Number) {
      report.field("resultType", "number");
    }

    return report;
  }
}

@@ -1079,18 +1079,17 @@
 gate %7B%0D%0A
-  
+%09
 public s
@@ -1106,17 +1106,17 @@
 l String
- 
+%09
 KEYWORD_
@@ -1122,17 +1122,17 @@
 _EXPLAIN
- 
+%09
 = %22EXPLA
@@ -1139,18 +1139,17 @@
 IN%22;%0D%0A%0D%0A
-  
+%09
 @Suppres
@@ -1172,18 +1172,17 @@
 cked%22)%0D%0A
-  
+%09
 @Overrid
@@ -1176,34 +1176,33 @@
 %22)%0D%0A%09@Override%0D%0A
-  
+%09
 public OCommandE
@@ -1254,20 +1254,18 @@
 and) %7B%0D%0A
-    
+%09%09
 String c
@@ -1306,20 +1306,18 @@
 ext();%0D%0A
-    
+%09%09
 super.pa
@@ -1380,20 +1380,18 @@
 ())));%0D%0A
-    
+%09%09
 return t
@@ -1396,25 +1396,23 @@
  this;%0D%0A
-  
+%09
 %7D%0D%0A%0D%0A
-  
+%09
 @Overrid
@@ -1414,18 +1414,17 @@
 erride%0D%0A
-  
+%09
 public O
@@ -1467,20 +1467,18 @@
 rgs) %7B%0D%0A
-    
+%09%09
 delegate
@@ -1517,28 +1517,26 @@
 s(true);%0D%0A%0D%0A
-    
+%09%09
 final long s
@@ -1557,36 +1557,25 @@
 tem.
-currentTimeMillis
+nanoTime
 ();%0D%0A%0D%0A
-    
+%09%09
 fina
@@ -1615,20 +1615,18 @@
 Args);%0D%0A
-    
+%09%09
 final OD
@@ -1698,20 +1698,18 @@
 ());%0D%0A%0D%0A
-    
+%09%09
 report.f
@@ -1728,56 +1728,61 @@
 d%22, 
+(
 System.
-currentTimeMillis() - startTime
+nanoTime() - startTime) / 1000000000f
 );%0D%0A%0D%0A
-    
+%09%09
 if (
@@ -1813,30 +1813,27 @@
 tion%3C?%3E) %7B%0D%0A
-      
+%09%09%09
 report.field
@@ -1859,30 +1859,27 @@
 lection%22);%0D%0A
-      
+%09%09%09
 report.field
@@ -1924,28 +1924,26 @@
 ).size());%0D%0A
-    
+%09%09
 %7D else if (r
@@ -1969,30 +1969,27 @@
 ocument) %7B%0D%0A
-      
+%09%09%09
 report.field
@@ -2017,22 +2017,19 @@
 ent%22);%0D%0A
-      
+%09%09%09
 report.f
@@ -2052,20 +2052,18 @@
 %22, 1);%0D%0A
-    
+%09%09
 %7D else i
@@ -2098,14 +2098,11 @@
  %7B%0D%0A
-      
+%09%09%09
 repo
@@ -2140,21 +2140,17 @@
 );%0D%0A
-    
+%09%09
 %7D%0D%0A%0D%0A
-    
+%09%09
 retu
@@ -2161,16 +2161,15 @@
 eport;%0D%0A
-  
+%09
 %7D%0D%0A%7D%0D%0A

Changed the time of EXPLAIN from ms to seconds giving the precision to the nanosecond.