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

thecodingmachine / magic-query / 7862450178

11 Feb 2024 01:55PM UTC coverage: 70.298%. First build
7862450178

Pull #87

github

web-flow
Merge 3adfbf0ce into 0c0ab130b
Pull Request #87: Update deps + test on dbal v3

2 of 11 new or added lines in 5 files covered. (18.18%)

1084 of 1542 relevant lines covered (70.3%)

4.76 hits per line

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

0.0
/src/Mouf/Database/QueryWriter/QueryResult.php
1
<?php
2

3
namespace Mouf\Database\QueryWriter;
4

5
use function method_exists;
6
use Mouf\Database\QueryWriter\Utils\DbHelper;
7
use Mouf\Utils\Value\ValueUtils;
8
use SQLParser\Query\Select;
9
use Mouf\Utils\Common\PaginableInterface;
10
use Mouf\Utils\Value\ArrayValueInterface;
11
use Mouf\Utils\Value\ValueInterface;
12
use Doctrine\DBAL\Connection;
13
use Mouf\Utils\Common\SortableInterface;
14
use SQLParser\Node\NodeInterface;
15
use SQLParser\Node\ColRef;
16

17
/**
18
 * A class that can execute a query and expose the results.
19
 *
20
 * @Renderer { "smallLogo":"vendor/mouf/database.querywriter/icons/database_query.png" }
21
 *
22
 * @author David Negrier
23
 */
24
class QueryResult implements ArrayValueInterface, PaginableInterface, SortableInterface
25
{
26
    /**
27
     * The Select statement.
28
     *
29
     * @var Select
30
     */
31
    private $select;
32

33
    /**
34
     * The connection to the database.
35
     *
36
     * @var Connection
37
     */
38
    private $connection;
39

40
    /**
41
     * The list of parameters to apply to the SQL request.
42
     *
43
     * @var array<string, string>|array<string, ValueInterface>|ArrayValueInterface
44
     */
45
    private $parameters = array();
46

47
    /** @var int|null */
48
    private $offset;
49
    /** @var int|null */
50
    private $limit;
51

52
    /**
53
     * @param Select     $select
54
     * @param Connection $connection
55
     */
56
    public function __construct(Select $select, Connection $connection)
57
    {
58
        $this->select = $select;
×
59
        $this->connection = $connection;
×
60
    }
61

62
    /**
63
     * The list of parameters to apply to the SQL request.
64
     *
65
     * @param array<string, string>|array<string, ValueInterface>|ArrayValueInterface $parameters
66
     */
67
    public function setParameters($parameters): void
68
    {
69
        $this->parameters = $parameters;
×
70
    }
71

72
    /**
73
     * (non-PHPdoc).
74
     *
75
     * @see \Mouf\Utils\Value\ArrayValueInterface::val()
76
     */
77
    public function val()
78
    {
79
        $parameters = ValueUtils::val($this->parameters);
×
NEW
80
        $pdoStatement = $this->connection->prepare($this->select->toSql($parameters, $this->connection->getDatabasePlatform()).DbHelper::getFromLimitString($this->offset, $this->limit));
×
81

NEW
82
        return new ResultSet($pdoStatement->getWrappedStatement());
×
83
    }
84

85
    /**
86
     * Returns the SQL for this query-result (without pagination, but with parameters accounted for).
87
     *
88
     * @return string|null
89
     */
90
    public function toSql()
91
    {
92
        $parameters = ValueUtils::val($this->parameters);
×
93

94
        return $this->select->toSql($parameters, $this->connection->getDatabasePlatform());
×
95
    }
96

97
    /**
98
     * Paginates the result set.
99
     *
100
     * @param int $limit
101
     * @param int $offset
102
     */
103
    public function paginate($limit, $offset = 0): void
104
    {
105
        $this->limit = $limit;
×
106
        $this->offset = $offset;
×
107
    }
108

109
    /* (non-PHPdoc)
110
     * @see \Mouf\Utils\Common\SortableInterface::sort()
111
     */
112
    public function sort($key, $direction = SortableInterface::ASC): void
113
    {
114
        $result = $this->findColumnByKey($key);
×
115
        if ($result != null) {
×
116
            $columnObj = clone($result);
×
117
            if (method_exists($columnObj, 'setAlias')) {
×
118
                $columnObj->setAlias(null);
×
119
            }
120
            if (method_exists($columnObj, 'setDirection')) {
×
121
                $columnObj->setDirection($direction);
×
122
            }
123
        } else {
124
            $columnObj = new ColRef();
×
125
            $columnObj->setColumn($key);
×
126
            $columnObj->setDirection($direction);
×
127
        }
128
        $this->select->setOrder(array($columnObj));
×
129
    }
130

131
    /**
132
     * Returns the object representing a column from the key passed in parameter.
133
     * It will first scan the column aliases to find if an alias match the key, then the column names, etc...
134
     * It will throw an exception if the column cannot be found.
135
     *
136
     * @param string $key
137
     *
138
     * @return NodeInterface|null
139
     */
140
    private function findColumnByKey($key): ?NodeInterface
141
    {
142
        $columns = $this->select->getColumns();
×
143
        foreach ($columns as $column) {
×
144
            if (method_exists($column, 'getAlias')) {
×
145
                $alias = $column->getAlias();
×
146
                if ($alias === $key) {
×
147
                    return $column;
×
148
                }
149
            }
150
            if ($column instanceof ColRef) {
×
151
                if ($column->getColumn() === $key) {
×
152
                    return $column;
×
153
                }
154
            }
155
        }
156

157
        return null;
×
158
    }
159
}
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