import os, sys
sys.path.append(os.path.join(os.path.dirname(__file__), os.pardir))

from baseanalyzer import BaseAnalyzer

import logging

## =====================================================================
## LOGGING CONFIGURATION
## =====================================================================
LOG = logging.getLogger()

## =====================================================================
## SQLITE3 ANALYZER
## =====================================================================
class SQLite3Analyzer(BaseAnalyzer):
    
    def __init__(self, deployer):
        BaseAnalyzer.__init__(self, deployer)

    def count_transaction(self, queries):
        raise NotImplementedError("Unimplemented %s" % self.__init__.im_class)

    def analyze_queries(self, queries):
        raise NotImplementedError("Unimplemented %s" % self.__init__.im_class)

    def analyze_database(self):
        try:
            conn = self.deployer.get_database_connection()
            cur = conn.cursor()
            database = self.deployer.get_database_name()
            
            # the number of tables
            cur.execute("SELECT COUNT(*) FROM sqlite_master WHERE type = 'table';")
            self.database_stats['num_tables'] = int(cur.fetchone()[0])

            # the number of indexes
            cur.execute("SELECT COUNT(*) FROM sqlite_master WHERE type = 'index';")
            self.database_stats['num_indexes'] = int(cur.fetchone()[0])

            cur.close()
            conn.close()
        except Exception, e:
            LOG.exception(e)
@@ -77,16 +77,32 @@
 rdir))%0A%0A
+import logging%0A%0A
 from bas
@@ -132,32 +132,16 @@
 alyzer%0A%0A
-import logging%0A%0A
 ## =====
@@ -627,16 +627,102 @@
     def 
+analyze_queries(self, queries):%0A        self.queries_stats%5B'num_transactions'%5D = self.
 count_tr
@@ -731,30 +731,24 @@
 saction(
-self, 
 queries)
 :%0A      
@@ -731,33 +731,33 @@
 saction(queries)
-:
+%0A
 %0A        raise N
@@ -753,197 +753,832 @@
     
-raise NotImplementedError(%22Unimplemented %25s%22 %25 self.__init__.im_class)%0A%0A    def analyze_queries(self, queries):%0A        raise NotImplementedError(%22Unimplemented %25s%22 %25 self.__init__.im_class
+try:%0A            conn = self.deployer.get_database_connection()%0A            cur = conn.cursor()%0A%0A            for query in queries:%0A                try:%0A                    if self.is_valid_for_explain(query%5B'raw'%5D):%0A                        explain_query = 'EXPLAIN %7B%7D;'.format(query%5B'raw'%5D)%0A                        # print explain_query%0A                        cur.execute(explain_query)%0A                        rows = cur.fetchall()%0A                        output = '%5Cn'%0A                        for row in rows:%0A                            output += str(row) + '%5Cn'%0A                        query%5B'explain'%5D = output%0A                except Exception, e:%0A                    pass%0A                    # LOG.exception(e)%0A%0A            cur.close()%0A            conn.close()%0A        except Exception, e:%0A            LOG.exception(e
 )%0A%0A 

update SQLite3 analyzer