1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10:
11:
12:
13:
14: 15: 16: 17: 18: 19:
20: class DibiEvent
21: {
22:
23: const CONNECT = 1,
24: SELECT = 4,
25: INSERT = 8,
26: DELETE = 16,
27: UPDATE = 32,
28: QUERY = 60,
29: BEGIN = 64,
30: COMMIT = 128,
31: ROLLBACK = 256,
32: TRANSACTION = 448,
33: ALL = 1023;
34:
35:
36: public $connection;
37:
38:
39: public $type;
40:
41:
42: public $sql;
43:
44:
45: public $result;
46:
47:
48: public $time;
49:
50:
51: public $count;
52:
53:
54: public $source;
55:
56:
57:
58: public function __construct(DibiConnection $connection, $type, $sql = NULL)
59: {
60: $this->connection = $connection;
61: $this->type = $type;
62: $this->sql = trim($sql);
63: $this->time = -microtime(TRUE);
64:
65: if ($type === self::QUERY && preg_match('#(SELECT|UPDATE|INSERT|DELETE)#iA', $this->sql, $matches)) {
66: static $types = array(
67: 'SELECT' => self::SELECT, 'UPDATE' => self::UPDATE,
68: 'INSERT' => self::INSERT, 'DELETE' => self::DELETE,
69: );
70: $this->type = $types[strtoupper($matches[1])];
71: }
72:
73: $rc = new ReflectionClass('dibi');
74: $dibiDir = dirname($rc->getFileName()) . DIRECTORY_SEPARATOR;
75: foreach (debug_backtrace(FALSE) as $row) {
76: if (isset($row['file']) && is_file($row['file']) && strpos($row['file'], $dibiDir) !== 0) {
77: $this->source = array($row['file'], (int) $row['line']);
78: break;
79: }
80: }
81:
82: dibi::$elapsedTime = FALSE;
83: }
84:
85:
86:
87: public function done($result = NULL)
88: {
89: $this->result = $result;
90: try {
91: $this->count = $result instanceof DibiResult ? count($result) : NULL;
92: } catch (DibiException $e) {
93: $this->count = NULL;
94: }
95:
96: $this->time += microtime(TRUE);
97: dibi::$elapsedTime = $this->time;
98: dibi::$totalTime += $this->time;
99: return $this;
100: }
101:
102: }
103: