Completed
Push — master ( 72a69a...90f27b )
by Mikael
01:59
created

DatabaseDebug::loadHistory()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 10
ccs 0
cts 9
cp 0
rs 9.4285
cc 2
eloc 7
nc 2
nop 0
crap 6
1
<?php
2
3
namespace Anax\Database;
4
5
/**
6
 * Database wrapper, provides a database API on top of PHP PDO for
7
 * enhancing the API and dealing with error reporting and tracking.
8
 */
9
class DatabaseDebug extends Database
10
{
11
    /**
12
     * @var integer $numQueries count all queries made
13
     * @var array   $queries    save all queries for debugging
14
     * @var array   $params     Save all parameters for debugging
15
     */
16
    private static $numQueries = 0;
17
    private static $queries = [];
18
    private static $params = [];
19
20
21
22
    /**
23
     * Connect to the database.
24
     *
25
     * @return self
26
     */
27
    public function connect()
28
    {
29
        if ($this->options['verbose']) {
0 ignored issues
show
Bug introduced by
The property options cannot be accessed from this context as it is declared private in class Anax\Database\Database.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
30
            echo "<p>Connecting to dsn:<br><code>" . $this->options['dsn'] . "</code>";
0 ignored issues
show
Bug introduced by
The property options cannot be accessed from this context as it is declared private in class Anax\Database\Database.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
31
        }
32
33
        $this->loadHistory();
34
        return parent::connect();
35
    }
36
37
38
39
    /**
40
     * Load query-history from session if available.
41
     *
42
     * @return int number of database queries made.
43
     */
44
    public function loadHistory()
45
    {
46
        $key = $this->options['session_key'];
0 ignored issues
show
Bug introduced by
The property options cannot be accessed from this context as it is declared private in class Anax\Database\Database.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
47
        if (isset($_SESSION['CDatabase'])) {
48
            self::$numQueries = $_SESSION[$key]['numQueries'];
49
            self::$queries    = $_SESSION[$key]['queries'];
50
            self::$params     = $_SESSION[$key]['params'];
51
            unset($_SESSION[$key]);
52
        }
53
    }
54
55
56
57
    /**
58
     * Save query-history in session, useful as a flashmemory when redirecting to another page.
59
     *
60
     * @param string $extra enables to save some extra debug information.
61
     *
62
     * @return void
63
     */
64
    public function saveHistory($extra = null)
65
    {
66
        if (!is_null($extra)) {
67
            self::$queries[] = $extra;
68
            self::$params[] = null;
69
        }
70
71
        self::$queries[] = 'Saved query-history to session.';
72
        self::$params[] = null;
73
74
        $key = $this->options['session_key'];
0 ignored issues
show
Bug introduced by
The property options cannot be accessed from this context as it is declared private in class Anax\Database\Database.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
75
        $_SESSION[$key]['numQueries'] = self::$numQueries;
76
        $_SESSION[$key]['queries']    = self::$queries;
77
        $_SESSION[$key]['params']     = self::$params;
78
    }
79
80
81
82
    /**
83
     * Get how many queries have been processed.
84
     *
85
     * @return int number of database queries made.
86
     */
87
    public function getNumQueries()
88
    {
89
        return self::$numQueries;
90
    }
91
92
93
94
    /**
95
     * Get all the queries that have been processed.
96
     *
97
     * @return array with queries.
98
     */
99
    public function getQueries()
100
    {
101
        return [self::$queries, self::$params];
102
    }
103
104
105
106
    /**
107
     * Get a HTML representation of all queries made, for debugging
108
     * and analysing purpose.
109
     *
110
     * @return string with HTML.
111
     */
112
    public function dump()
113
    {
114
        $html  = '<p><i>You have made ' . self::$numQueries . ' database queries.</i></p><pre>';
115
        
116
        foreach (self::$queries as $key => $val) {
117
            $params = empty(self::$params[$key])
118
                ? null
119
                : htmlentities(print_r(self::$params[$key], 1), null, 'UTF-8') . '<br/><br/>';
120
            $html .= htmlentities($val, null, 'UTF-8') . '<br/><br/>' . $params;
121
        }
122
        
123
        return $html . '</pre>';
124
    }
125
126
127
128
    /**
129
     * Execute a SQL-query and ignore the resultset.
130
     *
131
     * @param string $query  the SQL statement
132
     * @param array  $params the params array
133
     *
134
     * @throws Exception when failing to prepare question.
135
     *
136
     * @return boolean returns TRUE on success or FALSE on failure.
137
     */
138
    public function execute($query, $params = [])
139
    {
140
        self::$queries[] = $query;
141
        self::$params[]  = $params;
142
        self::$numQueries++;
143
144
        if ($this->options['verbose']) {
0 ignored issues
show
Bug introduced by
The property options cannot be accessed from this context as it is declared private in class Anax\Database\Database.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
145
            echo "<p>Num query = "
146
                . self::$numQueries
147
                . "</p><p>Query = </p><pre>"
148
                . htmlentities($query)
149
                . "</pre>"
150
                . (empty($params)
151
                    ? null
152
                    : "<p>Params:</p><pre>" . htmlentities(print_r($params, 1)) . "</pre>"
153
                );
154
        }
155
156
        return parent::execute($query, $params);
157
    }
158
}
159