/*

   Derby - Class org.apache.derby.impl.sql.execute.xplain.XPLAINSystemTableVisitor

   Licensed to the Apache Software Foundation (ASF) under one or more
   contributor license agreements.  See the NOTICE file distributed with
   this work for additional information regarding copyright ownership.
   The ASF licenses this file to You 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 org.apache.derby.impl.sql.execute.xplain;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Stack;

import org.apache.derby.catalog.UUID;
import org.apache.derby.iapi.jdbc.ConnectionContext;
import org.apache.derby.iapi.error.StandardException;
import org.apache.derby.iapi.services.info.JVMInfo;
import org.apache.derby.iapi.sql.Activation;
import org.apache.derby.iapi.sql.conn.LanguageConnectionContext;
import org.apache.derby.iapi.sql.dictionary.DataDictionary;
import org.apache.derby.impl.sql.catalog.XPLAINResultSetDescriptor;
import org.apache.derby.impl.sql.catalog.XPLAINResultSetTimingsDescriptor;
import org.apache.derby.impl.sql.catalog.XPLAINScanPropsDescriptor;
import org.apache.derby.impl.sql.catalog.XPLAINSortPropsDescriptor;
import org.apache.derby.impl.sql.catalog.XPLAINStatementDescriptor;
import org.apache.derby.impl.sql.catalog.XPLAINStatementTimingsDescriptor;
import org.apache.derby.iapi.sql.execute.RunTimeStatistics;
import org.apache.derby.iapi.sql.execute.xplain.XPLAINVisitor;
import org.apache.derby.iapi.sql.execute.ResultSetStatistics;

/**
 * This is the Visitor, which explains the information and stores the statistics in 
 * the system catalogs. It traverses the result set statistics tree and extracts the
 * information.  
 *
 */
public class XPLAINSystemTableVisitor implements XPLAINVisitor {
    
    private boolean no_call_stmts = true;
    
    
    // ---------------------------------------------------------
    // member variables
    // ---------------------------------------------------------
    
    // the needed system objects for writing to the dictionary
    private LanguageConnectionContext lcc;
    private DataDictionary dd;
    
    // the stmt activation object
    private Activation activation;

    // a flag which is used to reflect if the statistics timings is on
    private boolean considerTimingInformation = false;
    
    // the different tuple descriptors describing the query characteristics
    // regarding the stmt
    private XPLAINStatementDescriptor stmt;
    private XPLAINStatementTimingsDescriptor stmtTimings = null;
    private UUID stmtUUID; // the UUID to save for the resultsets
    
    // now the lists of descriptors regarding the resultsets
    private List rsets; // for the resultset descriptors
    private List rsetsTimings; // for the resultset timings descriptors
    private List sortrsets; // for the sort props descriptors
    private List scanrsets; // fot the scan props descriptors
    
    // the number of children of the current explained node
    private int noChildren;
    
    // this stack keeps track of the result set UUIDs, which get popped by the
    // children of the current explained node
    private Stack UUIDStack;
    
    // ---------------------------------------------------------
    // Constructor
    // ---------------------------------------------------------
    
    public XPLAINSystemTableVisitor(){
        // System.out.println("System Table Visitor created...");
        // initialize lists
        rsets        = new ArrayList();
        rsetsTimings = new ArrayList();
        sortrsets    = new ArrayList();
        scanrsets    = new ArrayList();
        
        // init UUIDStack
        UUIDStack    = new Stack();
        
    }

    
    /** helper method, which pushes the UUID,
     *  "number of Children" times onto the UUIDStack.
     * @param uuid the UUID to push
     */
    private void pushUUIDnoChildren(UUID uuid){
        for (int i=0;i rsetsiter = rsets.iterator();
        while (rsetsiter.hasNext())
        {
            XPLAINResultSetDescriptor rset =
                rsetsiter.next();
            rset.setStatementParameters(ps);
            ps.executeUpdate();
        }
        ps.close();

        // add the resultset timings descriptors, if timing is on
        if(considerTimingInformation)
        {
            ps = conn.prepareStatement(
                (String)lcc.getXplainStatement("SYSXPLAIN_RESULTSET_TIMINGS"));
            Iterator timingsiter = rsetsTimings.iterator();
            while (timingsiter.hasNext())
            {
                XPLAINResultSetTimingsDescriptor rsetT =
                    (XPLAINResultSetTimingsDescriptor)timingsiter.next();
                rsetT.setStatementParameters(ps);
                ps.executeUpdate();
            }
            ps.close();
        }
        ps = conn.prepareStatement(
            (String)lcc.getXplainStatement("SYSXPLAIN_SCAN_PROPS"));
        Iterator scaniter = scanrsets.iterator();
        while (scaniter.hasNext())
        {
            XPLAINScanPropsDescriptor scanProps =
                scaniter.next();
            scanProps.setStatementParameters(ps);
            ps.executeUpdate();
        }
        ps.close();

        ps = conn.prepareStatement(
            (String)lcc.getXplainStatement("SYSXPLAIN_SORT_PROPS"));
        Iterator sortiter = sortrsets.iterator();
        while (sortiter.hasNext())
        {
            XPLAINSortPropsDescriptor sortProps =
                sortiter.next();
            sortProps.setStatementParameters(ps);
            ps.executeUpdate();
        }
        ps.close();

        conn.close();
        lcc.setRunTimeStatisticsMode(statsSave);
    }

}

 
 

Ads help maintain this website.