]> git.andy128k.dev Git - ipf.git/commitdiff
SQL Profiler
authoravl <alex.litovchenko@gmail.com>
Fri, 29 Aug 2008 02:42:47 +0000 (04:42 +0200)
committeravl <alex.litovchenko@gmail.com>
Fri, 29 Aug 2008 02:42:47 +0000 (04:42 +0200)
ipf/orm/connection/profiler.php [new file with mode: 0644]
ipf/orm/exception/profiler.php [new file with mode: 0644]
ipf/template/tag/sql.php [new file with mode: 0644]

diff --git a/ipf/orm/connection/profiler.php b/ipf/orm/connection/profiler.php
new file mode 100644 (file)
index 0000000..3c0a340
--- /dev/null
@@ -0,0 +1,89 @@
+<?php
+
+class IPF_ORM_Connection_Profiler implements IPF_ORM_Overloadable, IteratorAggregate, Countable
+{
+    private $listeners  = array('query',
+                                'prepare',
+                                'commit',
+                                'rollback',
+                                'connect',
+                                'begintransaction',
+                                'exec',
+                                'execute');
+
+    private $events     = array();
+    public function __construct() {
+    }
+
+    public function setFilterQueryType() {
+    }                                         
+
+    public function __call($m, $a)
+    {
+        if ( ! ($a[0] instanceof IPF_ORM_Event)) {
+            throw new IPF_ORM_Exception_Profiler("Couldn't listen event. Event should be an instance of Doctrine_Event.");
+        }
+
+
+        if (substr($m, 0, 3) === 'pre') {
+            // pre-event listener found
+            $a[0]->start();
+
+            if ( ! in_array($a[0], $this->events, true)) {
+                $this->events[] = $a[0];
+            }
+        } else {
+            // after-event listener found
+            $a[0]->end();
+        }
+        /**
+         * If filtering by query type is enabled, only keep the query if
+         * it was one of the allowed types.
+         */
+         /*
+        if ( !is_null($this->filterTypes)) {
+            if ( ! ($a[0]->getQueryType() & $this->_filterTypes)) {
+
+            }
+        }
+        */
+    }
+
+    public function get($key) 
+    {
+        if (isset($this->events[$key])) {
+            return $this->events[$key];
+        }
+        return null;
+    }
+
+    public function getAll() 
+    {
+        return $this->events;
+    }
+
+    public function getIterator()
+    {
+        return new ArrayIterator($this->events);
+    }
+
+    public function count() 
+    {
+        return count($this->events);
+    }
+
+    public function pop() 
+    {
+        return array_pop($this->events);
+    }
+
+    public function lastEvent()
+    {
+        if (empty($this->events)) {
+            return false;
+        }
+
+        end($this->events);
+        return current($this->events);
+    }
+}
\ No newline at end of file
diff --git a/ipf/orm/exception/profiler.php b/ipf/orm/exception/profiler.php
new file mode 100644 (file)
index 0000000..5fa9ad5
--- /dev/null
@@ -0,0 +1,3 @@
+<?php
+
+class IPF_ORM_Exception_Profiler extends IPF_ORM_Exception{}
diff --git a/ipf/template/tag/sql.php b/ipf/template/tag/sql.php
new file mode 100644 (file)
index 0000000..9251fce
--- /dev/null
@@ -0,0 +1,28 @@
+<?php
+
+class IPF_Template_Tag_Sql extends IPF_Template_Tag
+{
+    function start()
+    {
+        $profiler = IPF_Project::getInstance()->sqlProfiler;
+        if ($profiler!==null){
+            echo '<div style="padding:10px; margin:10px; background:#eee; border:1px dashed #888;"><h3>Sql Debug</h3><div style="color:#888">set <i>debug</i> to false in settings project for disable sql profiler</div>';
+            $time = 0;
+            foreach ($profiler as $event) {
+                $time += $event->getElapsedSecs();
+                $name = $event->getName();
+                if ($name=='fetch' || $name=='prepare' || $name=='connect')
+                    continue;
+                echo "<br>\n<b>" . $name . "</b> " . sprintf("%f", $event->getElapsedSecs()) . "<br>\n";
+                echo $event->getQuery() . "<br>\n";
+                $params = $event->getParams();
+                if( ! empty($params)) {
+                    var_dump($params);
+                    print "<br>\n";
+                }
+            }
+            echo "<br>\n<b>Total time:</b> " . $time  . " (without prepare and fetch event)<br>\n";
+            echo '</div>';
+        }
+    }
+}