Passed
Push — master ( a4dbe3...db2213 )
by Aimeos
02:07
created

DBAL::fetch()   B

Complexity

Conditions 8
Paths 8

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 11
c 2
b 0
f 0
dl 0
loc 21
rs 8.4444
cc 8
nc 8
nop 1
1
<?php
2
3
/**
4
 * @license LGPLv3, https://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2021-2022
6
 * @package Base
7
 * @subpackage DB
8
 */
9
10
11
namespace Aimeos\Base\DB\Result;
12
13
14
/**
15
 * Database result set object for DBAL connections.
16
 *
17
 * @package Base
18
 * @subpackage DB
19
 */
20
class DBAL extends \Aimeos\Base\DB\Result\Base implements \Aimeos\Base\DB\Result\Iface
21
{
22
	private $result;
23
24
25
	/**
26
	 * Initializes the result object.
27
	 *
28
	 * @param \Doctrine\DBAL\Driver\Statement|\Doctrine\DBAL\Driver\Result $result Result object created by DBAL
29
	 */
30
	public function __construct( $result )
31
	{
32
		$this->result = $result;
33
	}
34
35
36
	/**
37
	 * Clears the result set if anything is left.
38
	 */
39
	public function __destruct()
40
	{
41
		$class = '\Doctrine\DBAL\Driver\Result';
42
43
		if( $this->result instanceof $class ) {
44
			$this->result->free();
0 ignored issues
show
Bug introduced by
The method free() does not exist on Doctrine\DBAL\Driver\Statement. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

44
			$this->result->/** @scrutinizer ignore-call */ 
45
                  free();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
45
		}
46
47
		$class = '\Doctrine\DBAL\Driver\Statement';
48
49
		if( $this->result instanceof $class ) {
50
			$this->result->closeCursor();
0 ignored issues
show
Bug introduced by
The method closeCursor() does not exist on Doctrine\DBAL\Driver\Result. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

50
			$this->result->/** @scrutinizer ignore-call */ 
51
                  closeCursor();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method closeCursor() does not exist on Doctrine\DBAL\Driver\Statement. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

50
			$this->result->/** @scrutinizer ignore-call */ 
51
                  closeCursor();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
51
		}
52
	}
53
54
55
	/**
56
	 * Returns the number of rows affected by a INSERT, UPDATE or DELETE statement.
57
	 *
58
	 * @return int Number of touched records
59
	 * @throws \Aimeos\Base\DB\Exception if an error occured in the unterlying driver
60
	 */
61
	public function affectedRows() : int
62
	{
63
		try {
64
			return $this->result->rowCount();
0 ignored issues
show
Bug introduced by
The method rowCount() does not exist on Doctrine\DBAL\Driver\Statement. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

64
			return $this->result->/** @scrutinizer ignore-call */ rowCount();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
65
		} catch( \Doctrine\DBAL\Driver\Exception $e ) {
66
			throw new \Aimeos\Base\DB\Exception( $e->getMessage(), $e->getCode() );
67
		}
68
	}
69
70
71
	/**
72
	 * Retrieves the next row from database result set.
73
	 *
74
	 * @param int $style The data can be returned as associative or numerical array
75
	 * @return array|null Numeric or associative array of columns returned by the database or null if no more rows are available
76
	 * @throws \Aimeos\Base\DB\Exception if an error occured in the unterlying driver or the fetch style is unknown
77
	 */
78
	public function fetch( int $style = \Aimeos\Base\DB\Result\Base::FETCH_ASSOC ) : ?array
79
	{
80
		try
81
		{
82
			$class = '\Doctrine\DBAL\Driver\Statement';
83
84
			if( $this->result instanceof $class )
85
			{
86
				$fetch = $style === \Aimeos\Base\DB\Result\Base::FETCH_NUM ? \PDO::FETCH_NUM : \PDO::FETCH_ASSOC;
87
				return $this->result->fetch( $fetch ) ?: null;
0 ignored issues
show
Bug introduced by
The method fetch() does not exist on Doctrine\DBAL\Driver\Statement. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

87
				return $this->result->/** @scrutinizer ignore-call */ fetch( $fetch ) ?: null;

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method fetch() does not exist on Doctrine\DBAL\Driver\Result. Did you maybe mean fetchOne()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

87
				return $this->result->/** @scrutinizer ignore-call */ fetch( $fetch ) ?: null;

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
88
			}
89
90
			if( $style === \Aimeos\Base\DB\Result\Base::FETCH_NUM ) {
91
				return $this->result->fetchNumeric() ?: null;
0 ignored issues
show
Bug introduced by
The method fetchNumeric() does not exist on Doctrine\DBAL\Driver\Statement. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

91
				return $this->result->/** @scrutinizer ignore-call */ fetchNumeric() ?: null;

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
92
			} else {
93
				return $this->result->fetchAssociative() ?: null;
0 ignored issues
show
Bug introduced by
The method fetchAssociative() does not exist on Doctrine\DBAL\Driver\Statement. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

93
				return $this->result->/** @scrutinizer ignore-call */ fetchAssociative() ?: null;

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
94
			}
95
		}
96
		catch( \Doctrine\DBAL\Driver\Exception $e )
97
		{
98
			throw new \Aimeos\Base\DB\Exception( $e->getMessage(), $e->getCode() );
99
		}
100
	}
101
102
103
	/**
104
	 * Cleans up pending database result sets.
105
	 *
106
	 * @return \Aimeos\Base\DB\Result\Iface Result instance for method chaining
107
	 * @throws \Aimeos\Base\DB\Exception if an error occured in the unterlying driver
108
	 */
109
	public function finish() : Iface
110
	{
111
		try
112
		{
113
			$class = '\Doctrine\DBAL\Driver\Result';
114
115
			if( $this->result instanceof $class ) {
116
				$this->result->free();
117
			}
118
119
			$class = '\Doctrine\DBAL\Driver\Statement';
120
121
			if( $this->result instanceof $class ) {
122
				$this->result->closeCursor();
123
			}
124
		}
125
		catch( \Doctrine\DBAL\Driver\Exception $e )
126
		{
127
			throw new \Aimeos\Base\DB\Exception( $e->getMessage(), $e->getCode() );
128
		}
129
130
		return $this;
131
	}
132
133
134
	/**
135
	 * Retrieves the next database result set.
136
	 *
137
	 * @return bool True if another result is available, false if not
138
	 */
139
	public function nextResult() : bool
140
	{
141
		return false;
142
	}
143
}
144