Completed
Branch feature/pre-split (92bc7d)
by Anton
04:30
created

SQLServerResult::fetch()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 10
nc 3
nop 3
dl 0
loc 18
rs 8.8571
c 0
b 0
f 0
1
<?php
2
/**
3
 * Spiral Framework.
4
 *
5
 * @license   MIT
6
 * @author    Anton Titov (Wolfy-J)
7
 */
8
9
namespace Spiral\Database\Drivers\SQLServer;
10
11
use Spiral\Database\Query\PDOResult;
12
13
/**
14
 * SQLServer specific result reader, required due server need additional column for sorting in some
15
 * cases.
16
 *
17
 * Attention: spiral_row_number column WILL be included in results generated by fetchAll method but
18
 * will be removed from fetch method automatically.
19
 *
20
 * todo: make sure there is no much inconsistency with SQLServer selections
21
 */
22
class SQLServerResult extends PDOResult
23
{
24
    /**
25
     * Helper column used to create limit, offset statements in older versions of sql server.
26
     */
27
    const ROW_NUMBER_COLUMN = 'spiral_row_number';
28
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function columnCount()
33
    {
34
        return parent::columnCount() + ($this->hasHelpColumn() ? -1 : 0);
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function fetch(
41
        $fetch_style = null,
42
        $cursor_orientation = \PDO::FETCH_ORI_NEXT,
43
        $cursor_offset = 0
44
    ) {
45
        $result = parent::fetch($fetch_style, $cursor_orientation, $cursor_offset);
46
47
        if (!$this->hasHelpColumn() || !is_array($result)) {
48
            return $result;
49
        }
50
51
        if (is_array($result) && $this->hasHelpColumn()) {
52
            //Sorting column is always first in result
53
            array_pop($result);
54
        }
55
56
        return $result;
57
    }
58
59
    /**
60
     * Provides support for old SQLServer versions.
61
     *
62
     * @return bool
63
     */
64
    private function hasHelpColumn()
65
    {
66
        return $this->getColumnMeta($this->countColumns() - 1)['name'] == self::ROW_NUMBER_COLUMN;
67
    }
68
}
69