FOSSology  4.4.0
Open Source License Compliance by Open Source Software
ModernDbManager.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2014 Siemens AG
4  Authors: Steffen Weber, Andreas Würl
5 
6  SPDX-License-Identifier: GPL-2.0-only
7 */
8 
9 namespace Fossology\Lib\Db;
10 
11 use Monolog\Logger;
12 
14 {
15  function __construct(Logger $logger)
16  {
17  parent::__construct($logger);
18  }
19 
25  public function prepare($statementName, $sqlStatement)
26  {
27  if (array_key_exists($statementName, $this->preparedStatements)) {
28  if ($this->preparedStatements[$statementName] !== $sqlStatement) {
29  throw new \Exception("Existing Statement mismatch: $statementName");
30  }
31  return;
32  }
33  $startTime = microtime($get_as_float = true);
34  $res = $this->dbDriver->prepare($statementName, $sqlStatement);
35  $this->cumulatedTime[$statementName] = microtime($get_as_float = true) - $startTime;
36  $this->queryCount[$statementName] = 0;
37  $this->logger->debug("prepare '$statementName' took " . sprintf("%0.3fms", 1000 * $this->cumulatedTime[$statementName]));
38  $this->checkResult($res, "$sqlStatement -- $statementName");
39  $this->preparedStatements[$statementName] = $sqlStatement;
40  }
41 
48  public function execute($statementName, $params = array())
49  {
50  if (! array_key_exists($statementName, $this->preparedStatements)) {
51  throw new \Exception("Unknown Statement");
52  }
53  $startTime = microtime($get_as_float = true);
54  $res = $this->dbDriver->execute($statementName, $params);
55  $execTime = microtime($get_as_float = true) - $startTime;
56  $this->collectStatistics($statementName, $execTime);
57  $this->checkResult($res, "$statementName: " . $this->preparedStatements[$statementName] . ' -- -- ' . print_r($params, true));
58  return $res;
59  }
60 }
checkResult($result, $sqlStatement="")
Check the result for unexpected errors. If found, treat them as fatal.
Definition: DbManager.php:116
collectStatistics($statementName, $execTime)
Definition: DbManager.php:274
prepare($statementName, $sqlStatement)
execute($statementName, $params=array())