1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Phossa Project |
4
|
|
|
* |
5
|
|
|
* PHP version 5.4 |
6
|
|
|
* |
7
|
|
|
* @category Library |
8
|
|
|
* @package Phossa2\Db |
9
|
|
|
* @copyright Copyright (c) 2016 phossa.com |
10
|
|
|
* @license http://mit-license.org/ MIT License |
11
|
|
|
* @link http://www.phossa.com/ |
12
|
|
|
*/ |
13
|
|
|
/*# declare(strict_types=1); */ |
14
|
|
|
|
15
|
|
|
namespace Phossa2\Db\Driver; |
16
|
|
|
|
17
|
|
|
use Phossa2\Db\Message\Message; |
18
|
|
|
use Phossa2\Shared\Base\ObjectAbstract; |
19
|
|
|
use Phossa2\Db\Interfaces\ResultInterface; |
20
|
|
|
use Phossa2\Db\Exception\RuntimeException; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* ResultAbstract |
24
|
|
|
* |
25
|
|
|
* @package Phossa2\Db |
26
|
|
|
* @author Hong Zhang <[email protected]> |
27
|
|
|
* @see ObjectAbstract |
28
|
|
|
* @see ResultInterface |
29
|
|
|
* @version 2.0.0 |
30
|
|
|
* @since 2.0.0 added |
31
|
|
|
*/ |
32
|
|
|
abstract class ResultAbstract extends ObjectAbstract implements ResultInterface |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* Fetched already |
36
|
|
|
* |
37
|
|
|
* @var bool |
38
|
|
|
* @access protected |
39
|
|
|
*/ |
40
|
|
|
protected $fetched = false; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* {@inheritDoc} |
44
|
|
|
*/ |
45
|
|
|
public function isSelect()/*# : bool */ |
46
|
|
|
{ |
47
|
|
|
return 0 !== $this->fieldCount(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* {@inheritDoc} |
52
|
|
|
*/ |
53
|
|
|
public function fetchAll()/*# : array */ |
54
|
|
|
{ |
55
|
|
|
$this->exceptionIfNotSelect(); |
56
|
|
|
$this->exceptionIfFetchedAlready(); |
57
|
|
|
$this->fetched = true; |
58
|
|
|
return $this->realFetchAll(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* {@inheritDoc} |
63
|
|
|
*/ |
64
|
|
|
public function fetchRow(/*# int */ $rowCount = 1)/*# : array */ |
65
|
|
|
{ |
66
|
|
|
$this->exceptionIfNotSelect(); |
67
|
|
|
$this->exceptionIfFetchedAlready(); |
68
|
|
|
$this->fetched = true; |
69
|
|
|
return $this->realFetchRow($rowCount); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* {@inheritDoc} |
74
|
|
|
*/ |
75
|
|
|
public function fetchCol($col = 0, $rowCount = 0)/*# : array */ |
76
|
|
|
{ |
77
|
|
|
$rows = $rowCount ? $this->fetchRow($rowCount) : $this->fetchAll(); |
78
|
|
|
|
79
|
|
|
$cols = $keys = []; |
80
|
|
|
foreach($rows as $row) { |
81
|
|
|
// named or indexed column |
82
|
|
|
if (isset($row[$col])) { |
83
|
|
|
$cols[] = $row[$col]; |
84
|
|
|
|
85
|
|
|
// n'th column |
86
|
|
|
} elseif (is_int($col) && count($row) >= $col) { |
87
|
|
|
if (empty($keys)) { |
88
|
|
|
$keys = array_keys($row); |
89
|
|
|
} |
90
|
|
|
$cols[] = $row[$keys[$col]]; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return $cols; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Throw exception if not select query |
99
|
|
|
* |
100
|
|
|
* @throws RuntimeException if not a select query |
101
|
|
|
* @access protected |
102
|
|
|
*/ |
103
|
|
|
protected function exceptionIfNotSelect() |
104
|
|
|
{ |
105
|
|
|
if (!$this->isSelect()) { |
106
|
|
|
throw new RuntimeException( |
107
|
|
|
Message::get(Message::DB_RESULT_NOT_SELECT), |
108
|
|
|
Message::DB_RESULT_NOT_SELECT |
109
|
|
|
); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Throw exception if fetched already |
115
|
|
|
* |
116
|
|
|
* @throws RuntimeException if fetched already |
117
|
|
|
* @access protected |
118
|
|
|
*/ |
119
|
|
|
protected function exceptionIfFetchedAlready() |
120
|
|
|
{ |
121
|
|
|
if ($this->fetched) { |
122
|
|
|
throw new RuntimeException( |
123
|
|
|
Message::get(Message::DB_RESULT_FETCHED), |
124
|
|
|
Message::DB_RESULT_FETCHED |
125
|
|
|
); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Driver fetch all |
131
|
|
|
* |
132
|
|
|
* @return array |
133
|
|
|
* @access protected |
134
|
|
|
*/ |
135
|
|
|
abstract protected function realFetchAll()/*# : array */; |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Driver fetch row |
139
|
|
|
* |
140
|
|
|
* @param int $rowCount number of rows to fetch |
141
|
|
|
* @return array |
142
|
|
|
* @access protected |
143
|
|
|
*/ |
144
|
|
|
abstract protected function realFetchRow($rowCount)/*# : array */; |
145
|
|
|
} |
146
|
|
|
|