9 namespace Fossology\Lib\Db;
15 function __construct(Logger $logger)
17 parent::__construct($logger);
25 public function prepare($statementName, $sqlStatement)
27 if (array_key_exists($statementName, $this->preparedStatements)) {
28 if ($this->preparedStatements[$statementName] !== $sqlStatement) {
29 throw new \Exception(
"Existing Statement mismatch: $statementName");
33 $this->cumulatedTime[$statementName] = 0;
34 $this->queryCount[$statementName] = 0;
35 $this->preparedStatements[$statementName] = $sqlStatement;
44 public function execute($statementName, $params = array())
46 if (! array_key_exists($statementName, $this->preparedStatements)) {
47 throw new \Exception(
"Unknown Statement");
49 $startTime = microtime(
true);
51 $res = $this->dbDriver->query($statement);
52 $execTime = microtime(
true) - $startTime;
54 $this->logger->debug(
"execution of '$statementName' took " . $this->
formatMilliseconds($execTime));
55 $this->
checkResult($res,
"$statementName :: $statement");
67 $sql = $this->preparedStatements[$statementName];
69 foreach ($params as $var) {
72 throw new \Exception(
'given argument for $' . $cnt .
' is null');
75 $masked = $this->dbDriver->booleanToDb($var);
76 }
else if (is_numeric($var)) {
79 $masked =
"'". $this->dbDriver->escapeString($var).
"'";
81 $sqlRep = preg_replace(
'/(\$'.$cnt.
')([^\d]|$)/',
"$masked$2", $sql);
82 if ($sqlRep == $sql) {
83 throw new \Exception(
'$' . $cnt .
' not found in prepared statement');
87 if (preg_match(
'/(\$[\d]+)([^\d]|$)/', $sql, $match)) {
88 $this->logger->debug($match[1].
" in '$statementName not resolved");
checkResult($result, $sqlStatement="")
Check the result for unexpected errors. If found, treat them as fatal.
formatMilliseconds($seconds)
collectStatistics($statementName, $execTime)
evaluateStatement($statementName, $params)
execute($statementName, $params=array())
prepare($statementName, $sqlStatement)