@@ -56,19 +56,19 @@ public function setPreserveTypes($preserveTypes = true) {
5656 * @return array[]
5757 */
5858 public function fetchRows (\Closure $ callback = null ) {
59- $ statement = $ this ->createStatement ();
60- $ data = $ statement ->fetchAll (\PDO ::FETCH_ASSOC );
61- if ($ this ->preserveTypes ) {
62- $ columnDefinitions = FieldTypeProvider::getFieldTypes ($ statement );
63- foreach ($ data as &$ row ) {
64- $ row = FieldValueConverter::convertValues ($ row , $ columnDefinitions );
59+ return $ this ->createTempStatement (function (QueryStatement $ statement ) use ($ callback ) {
60+ $ data = $ statement ->fetchAll (\PDO ::FETCH_ASSOC );
61+ if ($ this ->preserveTypes ) {
62+ $ columnDefinitions = FieldTypeProvider::getFieldTypes ($ statement );
63+ foreach ($ data as &$ row ) {
64+ $ row = FieldValueConverter::convertValues ($ row , $ columnDefinitions );
65+ }
6566 }
66- }
67- if ($ callback !== null ) {
68- $ data = array_map ($ callback , $ data );
69- }
70- $ statement ->closeCursor ();
71- return $ data ;
67+ if ($ callback !== null ) {
68+ $ data = array_map ($ callback , $ data );
69+ }
70+ return $ data ;
71+ });
7272 }
7373
7474 /**
@@ -88,39 +88,36 @@ public function fetchRowsLazy(\Closure $callback = null) {
8888 * @return string[]
8989 */
9090 public function fetchRow () {
91- $ statement = $ this ->createStatement ();
92- $ row = $ statement ->fetch (\PDO ::FETCH_ASSOC );
93- if (!is_array ($ row )) {
94- $ statement ->closeCursor ();
95- return array ();
96- }
97- if ($ this ->preserveTypes ) {
98- $ columnDefinitions = FieldTypeProvider::getFieldTypes ($ statement );
99- $ row = FieldValueConverter::convertValues ($ row , $ columnDefinitions );
100- }
101- $ statement ->closeCursor ();
102- return $ row ;
91+ return $ this ->createTempStatement (function (QueryStatement $ statement ) {
92+ $ row = $ statement ->fetch (\PDO ::FETCH_ASSOC );
93+ if (!is_array ($ row )) {
94+ return array ();
95+ }
96+ if ($ this ->preserveTypes ) {
97+ $ columnDefinitions = FieldTypeProvider::getFieldTypes ($ statement );
98+ $ row = FieldValueConverter::convertValues ($ row , $ columnDefinitions );
99+ }
100+ return $ row ;
101+ });
103102 }
104103
105104 /**
106105 * @param bool $treatValueAsArray
107106 * @return mixed[]
108107 */
109108 public function fetchKeyValue ($ treatValueAsArray = false ) {
110- $ rows = $ this ->fetchRows ();
111- $ result = array ();
112- if (!$ treatValueAsArray ) {
113- foreach ($ rows as $ row ) {
114- list ($ key , $ value ) = array_values ($ row );
115- $ result [$ key ] = $ value ;
116- }
117- } else {
118- foreach ($ rows as $ row ) {
119- list ($ key ) = array_values ($ row );
120- $ result [$ key ] = $ row ;
109+ return $ this ->createTempStatement (function (QueryStatement $ statement ) use ($ treatValueAsArray ) {
110+ if ($ treatValueAsArray ) {
111+ $ rows = $ statement ->fetchAll (\PDO ::FETCH_ASSOC );
112+ $ result = array ();
113+ foreach ($ rows as $ row ) {
114+ list ($ key ) = array_values ($ row );
115+ $ result [$ key ] = $ row ;
116+ }
117+ return $ result ;
121118 }
122- }
123- return $ result ;
119+ return $ statement -> fetchAll (\ PDO :: FETCH_KEY_PAIR );
120+ }) ;
124121 }
125122
126123 /**
@@ -148,9 +145,8 @@ public function fetchGroups(array $fields) {
148145 * @return string[]
149146 */
150147 public function fetchArray () {
151- return $ this ->fetchRows (function ($ row ) {
152- reset ($ row );
153- return current ($ row );
148+ return $ this ->createTempStatement (function (QueryStatement $ stmt ) {
149+ return $ stmt ->fetchAll (\PDO ::FETCH_COLUMN );
154150 });
155151 }
156152
@@ -159,16 +155,13 @@ public function fetchArray() {
159155 * @return null|bool|string|int|float
160156 */
161157 public function fetchValue ($ default = null ) {
162- $ statement = $ this ->createStatement ();
163- $ row = $ statement ->fetch (\PDO ::FETCH_ASSOC );
164- $ statement ->closeCursor ();
165- if (!is_array ($ row )) {
158+ return $ this ->createTempStatement (function (QueryStatement $ stmt ) use ($ default ) {
159+ $ result = $ stmt ->fetch (\PDO ::FETCH_NUM );
160+ if ($ result !== false ) {
161+ return $ result [0 ];
162+ }
166163 return $ default ;
167- }
168- if (!count ($ row )) {
169- return null ;
170- }
171- return array_shift ($ row );
164+ });
172165 }
173166
174167 /**
@@ -178,6 +171,24 @@ public function getFoundRows() {
178171 return $ this ->foundRows ;
179172 }
180173
174+ /**
175+ * @param callback $fn
176+ * @return mixed
177+ * @throws \Exception
178+ */
179+ private function createTempStatement ($ fn ) {
180+ $ stmt = $ this ->createStatement ();
181+ $ res = null ;
182+ try {
183+ $ res = call_user_func ($ fn , $ stmt );
184+ } catch (\Exception $ e ) { // PHP 5.4 compatibility
185+ $ stmt ->closeCursor ();
186+ throw $ e ;
187+ }
188+ $ stmt ->closeCursor ();
189+ return $ res ;
190+ }
191+
181192 /**
182193 * @return QueryStatement
183194 */
0 commit comments