• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

letsdrink / ouzo / 11274835883

10 Oct 2024 01:16PM UTC coverage: 85.99% (-0.02%) from 86.005%
11274835883

push

github

web-flow
Add an option to truncate bound values (#323)

6 of 7 new or added lines in 1 file covered. (85.71%)

1 existing line in 1 file now uncovered.

4990 of 5803 relevant lines covered (85.99%)

101.7 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.15
/src/Ouzo/Core/Db/StatementExecutor.php
1
<?php
2
/*
3
 * Copyright (c) Ouzo contributors, https://github.com/letsdrink/ouzo
4
 * This file is made available under the MIT License (view the LICENSE file for more information).
5
 */
6

7
namespace Ouzo\Db;
8

9
use Closure;
10
use Ouzo\Config;
11
use Ouzo\Logger\Backtrace;
12
use Ouzo\Logger\Logger;
13
use Ouzo\Utilities\Objects;
14
use PDO;
15
use PDOStatement;
16

17
class StatementExecutor
18
{
19
    private string $humanizedSql;
20

21
    private function __construct(
22
        private PDO $dbHandle,
23
        private string $sql,
24
        private array $boundValues,
25
        private PDOExecutor $pdoExecutor
26
    )
27
    {
28
        $this->humanizedSql = QueryHumanizer::humanize($sql);
490✔
29
    }
30

31
    private function executeWithStats(Closure $afterCallback): mixed
32
    {
33
        return Stats::trace($this->humanizedSql, $this->boundValues, fn() => $this->internalExecute($afterCallback));
490✔
34
    }
35

36
    private function internalExecute(Closure $afterCallback): mixed
37
    {
38
        $pdoStatement = $this->createPdoStatement();
490✔
39
        $result = call_user_func($afterCallback, $pdoStatement);
478✔
40
        $pdoStatement->closeCursor();
478✔
41
        return $result;
478✔
42
    }
43

44
    public function execute(): mixed
45
    {
46
        return $this->executeWithStats(fn(PDOStatement $pdoStatement) => $pdoStatement->rowCount());
470✔
47
    }
48

49
    public function executeAndFetch(string $function, string $fetchStyle): mixed
50
    {
51
        return $this->executeWithStats(fn($pdoStatement) => $pdoStatement->$function($fetchStyle));
433✔
52
    }
53

54
    public function fetch(int $fetchMode = PDO::FETCH_ASSOC): mixed
55
    {
56
        return $this->executeAndFetch('fetch', $fetchMode);
8✔
57
    }
58

59
    public function fetchAll(int $fetchMode = PDO::FETCH_ASSOC): mixed
60
    {
61
        return $this->executeAndFetch('fetchAll', $fetchMode);
20✔
62
    }
63

64
    public static function prepare(PDO $dbHandle, string $sql, array $boundValues, array $options): StatementExecutor
65
    {
66
        $pdoExecutor = PDOExecutor::newInstance($options);
490✔
67
        return new StatementExecutor($dbHandle, $sql, $boundValues, $pdoExecutor);
490✔
68
    }
69

70
    public function fetchIterator(array $options = []): StatementIterator
71
    {
72
        return Stats::trace($this->humanizedSql, $this->boundValues, function () use ($options) {
9✔
73
            $pdoStatement = $this->createPdoStatement($options);
9✔
74
            return new StatementIterator($pdoStatement);
9✔
75
        });
9✔
76
    }
77

78
    public function createPdoStatement(array $options = []): PDOStatement
79
    {
80
        $sqlString = $this->prepareSqlString();
490✔
81

82
        $callingClass = Backtrace::getCallingClass();
490✔
83
        Logger::getLogger(__CLASS__)->info("From: %s Query: %s", [$callingClass, $sqlString]);
490✔
84

85
        return $this->pdoExecutor->createPDOStatement($this->dbHandle, $this->sql, $this->boundValues, $sqlString, $options);
490✔
86
    }
87

88
    private function prepareSqlString(): string
89
    {
90
        $truncateLimit = Config::getValue('db', 'truncate_bound_values_string_limit');
490✔
91

92
        $boundValuesAsString = Objects::toString($this->boundValues);
490✔
93
        $boundValuesAsStringLength = mb_strlen($boundValuesAsString);
490✔
94
        if (!is_null($truncateLimit) && $boundValuesAsStringLength > $truncateLimit) {
490✔
NEW
95
            $boundValuesAsString = trim(mb_substr($boundValuesAsString, 0, $truncateLimit)) . "...\"] (truncated from {$boundValuesAsStringLength})";
×
96
        }
97

98
        return sprintf("%s with params: %s", $this->humanizedSql, $boundValuesAsString);
490✔
99
    }
100
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc