FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
SolidDbManager.php
1 <?php
2 /*
3 Copyright (C) 2014, Siemens AG
4 Authors: Steffen Weber, Andreas Würl
5 
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 version 2 as published by the Free Software Foundation.
9 
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14 
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 */
19 
20 namespace Fossology\Lib\Db;
21 
22 use Monolog\Logger;
23 
25 {
26  function __construct(Logger $logger)
27  {
28  parent::__construct($logger);
29  }
30 
36  public function prepare($statementName, $sqlStatement)
37  {
38  if (array_key_exists($statementName, $this->preparedStatements)) {
39  if ($this->preparedStatements[$statementName] !== $sqlStatement) {
40  throw new \Exception("Existing Statement mismatch: $statementName");
41  }
42  return;
43  }
44  $this->cumulatedTime[$statementName] = 0;
45  $this->queryCount[$statementName] = 0;
46  $this->preparedStatements[$statementName] = $sqlStatement;
47  }
48 
55  public function execute($statementName, $params = array())
56  {
57  if (! array_key_exists($statementName, $this->preparedStatements)) {
58  throw new \Exception("Unknown Statement");
59  }
60  $startTime = microtime(true);
61  $statement = $this->evaluateStatement($statementName, $params);
62  $res = $this->dbDriver->query($statement);
63  $execTime = microtime(true) - $startTime;
64  $this->collectStatistics($statementName, $execTime);
65  $this->logger->addDebug("execution of '$statementName' took " . $this->formatMilliseconds($execTime));
66  $this->checkResult($res, "$statementName :: $statement");
67  return $res;
68  }
69 
76  private function evaluateStatement($statementName, $params)
77  {
78  $sql = $this->preparedStatements[$statementName];
79  $cnt = 0;
80  foreach ($params as $var) {
81  $cnt++;
82  if ($var === null) {
83  throw new \Exception('given argument for $' . $cnt . ' is null');
84  }
85  if (is_bool($var)) {
86  $masked = $this->dbDriver->booleanToDb($var);
87  } else if (is_numeric($var)) {
88  $masked = $var;
89  } else {
90  $masked = "'". $this->dbDriver->escapeString($var)."'";
91  }
92  $sqlRep = preg_replace('/(\$'.$cnt.')([^\d]|$)/', "$masked$2", $sql);
93  if ($sqlRep == $sql) {
94  throw new \Exception('$' . $cnt . ' not found in prepared statement');
95  }
96  $sql = $sqlRep;
97  }
98  if (preg_match('/(\$[\d]+)([^\d]|$)/', $sql, $match)) {
99  $this->logger->addDebug($match[1]." in '$statementName not resolved");
100  }
101  return $sql;
102  }
103 }
execute($statementName, $params=array())
prepare($statementName, $sqlStatement)
checkResult($result, $sqlStatement="")
Check the result for unexpected errors. If found, treat them as fatal.
Definition: DbManager.php:127
evaluateStatement($statementName, $params)
collectStatistics($statementName, $execTime)
Definition: DbManager.php:285