Passed
Push — master ( 999eec...31351a )
by Aimeos
05:04
created

DBAL::affectedRows()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 6
rs 10
1
<?php
2
3
/**
4
 * @license LGPLv3, https://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2021
6
 * @package MW
7
 * @subpackage DB
8
 */
9
10
11
namespace Aimeos\MW\DB\Result;
12
13
14
/**
15
 * Database result set object for DBAL connections.
16
 *
17
 * @package MW
18
 * @subpackage DB
19
 */
20
class DBAL extends \Aimeos\MW\DB\Result\Base implements \Aimeos\MW\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
		$this->result->free();
42
	}
43
44
45
	/**
46
	 * Returns the number of rows affected by a INSERT, UPDATE or DELETE statement.
47
	 *
48
	 * @return int Number of touched records
49
	 * @throws \Aimeos\MW\DB\Exception if an error occured in the unterlying driver
50
	 */
51
	public function affectedRows() : int
52
	{
53
		try {
54
			return $this->result->rowCount();
55
		} catch( \Doctrine\DBAL\Driver\Exception $e ) {
56
			throw new \Aimeos\MW\DB\Exception( $e->getMessage(), $e->getCode(), $e->errorInfo );
0 ignored issues
show
Bug introduced by
Accessing errorInfo on the interface Doctrine\DBAL\Driver\Exception suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
57
		}
58
	}
59
60
61
	/**
62
	 * Retrieves the next row from database result set.
63
	 *
64
	 * @param int $style The data can be returned as associative or numerical array
65
	 * @return array|null Numeric or associative array of columns returned by the database or null if no more rows are available
66
	 * @throws \Aimeos\MW\DB\Exception if an error occured in the unterlying driver or the fetch style is unknown
67
	 */
68
	public function fetch( int $style = \Aimeos\MW\DB\Result\Base::FETCH_ASSOC ) : ?array
69
	{
70
		try
71
		{
72
			if( $style === \Aimeos\MW\DB\Result\Base::FETCH_NUM ) {
73
				return $this->result->fetchNumeric() ?: null;
74
			} else {
75
				return $this->result->fetchAssociative() ?: null;
76
			}
77
		}
78
		catch( \Doctrine\DBAL\Driver\Exception $e )
79
		{
80
			throw new \Aimeos\MW\DB\Exception( $e->getMessage(), $e->getCode(), $e->errorInfo );
0 ignored issues
show
Bug introduced by
Accessing errorInfo on the interface Doctrine\DBAL\Driver\Exception suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
81
		}
82
	}
83
84
85
	/**
86
	 * Cleans up pending database result sets.
87
	 *
88
	 * @return \Aimeos\MW\DB\Result\Iface Result instance for method chaining
89
	 * @throws \Aimeos\MW\DB\Exception if an error occured in the unterlying driver
90
	 */
91
	public function finish() : Iface
92
	{
93
		try {
94
			$this->result->free();
95
		} catch( \Doctrine\DBAL\Driver\Exception $e ) {
96
			throw new \Aimeos\MW\DB\Exception( $e->getMessage(), $e->getCode(), $e->errorInfo );
0 ignored issues
show
Bug introduced by
Accessing errorInfo on the interface Doctrine\DBAL\Driver\Exception suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
97
		}
98
99
		return $this;
100
	}
101
102
103
	/**
104
	 * Retrieves the next database result set.
105
	 *
106
	 * @return bool True if another result is available, false if not
107
	 */
108
	public function nextResult() : bool
109
	{
110
		return false;
111
	}
112
}
113