3 * This class is responsible for shuttling SQL queries to the audit table. All
4 * work is delegated to the existing SQL auditing function `auditSQLEvent` in
7 * Copyright (C) 2016 Matthew Vita <matthewvita48@gmail.com>
9 * LICENSE: This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://opensource.org/licenses/gpl-license.php>.
21 * @author Matthew Vita <matthewvita48@gmail.com>
22 * @link http://www.open-emr.org
25 namespace common\database
;
27 use \Doctrine\DBAL\Logging\SQLLogger
;
29 final class Auditor
implements SQLLogger
{
31 * Executed SQL queries with the following keys in each inner object:
33 * `params`: The optional param values for the current SQL query.
34 * `sql`: The raw SQL for the current SQL query (shows `?` for params if given).
36 * @note a query will be removed from the dictionary once complete.
38 public $queries = array();
41 * Index of the current query in the $queries dictionary.
43 public $currentQueryIndex = 0;
46 * Logger for noting sql query information.
51 * Default constructor. This is here for completeness, it is
52 * essentially a no-op.
54 public function __construct() {
55 $this->logger
= new \common\logging\
Logger("\common\database\Auditor");
59 * Intercepts the SQL query to be performed by the ORM.
61 * @param $sql the raw SQL (shows `?` for params if given).
62 * @param $params the optional param values for the SQL.
63 * @param $types the optional param types.
65 public function startQuery($sql, array $params = null, array $types = null) {
66 $this->queries
[++
$this->currentQueryIndex
] = array('sql' => $sql, 'params' => $params);
67 $this->logger
->trace("sql: " . $sql);
71 * Triggers when an SQL query has been performed and sends
72 * the information to the audit table.
74 * @note this is only called if the query succeeded.
75 * @note the query is from the dictionary once complete at this point.
77 public function stopQuery() {
78 $sql = $this->queries
[$this->currentQueryIndex
]['sql'];
79 $params = $this->queries
[$this->currentQueryIndex
]['params'];
80 auditSQLEvent($sql, true, $params);
81 unset($this->queries
[$this->currentQueryIndex
]);