Skip to content

Commit e425904

Browse files
authored
Merge pull request #26 from Automattic/add/dryer-fetch-results
DRYer code for fetching results
2 parents 7d186ae + 7a56a9a commit e425904

File tree

6 files changed

+22
-53
lines changed

6 files changed

+22
-53
lines changed

src/ThriftSQL.php

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,6 @@ abstract class ThriftSQL {
77
*/
88
abstract public function connect();
99

10-
/**
11-
* The simplest use case; takes a query string executes it synchronously and
12-
* returns the entire result set after collecting it from the server.
13-
*
14-
* @param string $queryStr
15-
* @return array
16-
* @throws \ThriftSQL\Exception
17-
*/
18-
abstract public function queryAndFetchAll( $queryStr );
19-
2010
/**
2111
* Sends a query string for execution on the server and returns a
2212
* ThriftSQLQuery object for fetching the results manually.
@@ -32,6 +22,25 @@ abstract public function query( $queryStr );
3222
*/
3323
abstract public function disconnect();
3424

25+
/**
26+
* The simplest use case; takes a query string executes it synchronously and
27+
* returns the entire result set after collecting it from the server.
28+
*
29+
* @param string $queryStr
30+
* @return array
31+
* @throws \ThriftSQL\Exception
32+
*/
33+
public function queryAndFetchAll( $queryStr ) {
34+
$iterator = $this->getIterator( $queryStr );
35+
36+
$resultTuples = array();
37+
foreach( $iterator as $rowNum => $row ) {
38+
$resultTuples[] = $row;
39+
}
40+
41+
return $resultTuples;
42+
}
43+
3544
/**
3645
* Gets a memory efficient iterator that you can use in a foreach loop.
3746
*

src/ThriftSQL/Hive.php

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -84,25 +84,6 @@ public function query( $queryStr ) {
8484
return new \ThriftSQL\HiveQuery( $response, $this->_client );
8585
}
8686

87-
public function queryAndFetchAll( $queryStr ) {
88-
try {
89-
$query = $this->query( $queryStr );
90-
$query->wait();
91-
// Collect results
92-
$resultTuples = array();
93-
do {
94-
$responseTuples = $query->fetch(100);
95-
// No more data we're done
96-
if ( empty( $responseTuples ) ) {
97-
return $resultTuples;
98-
}
99-
$resultTuples = array_merge( $resultTuples, $responseTuples );
100-
} while (true);
101-
} catch( Exception $e ) {
102-
throw new \ThriftSQL\Exception( $e->getMessage() );
103-
}
104-
}
105-
10687
public function disconnect() {
10788
// Close session if we have one
10889
if ( null !== $this->_sessionHandle ) {

src/ThriftSQL/HiveQuery.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public function wait() {
4848
}
4949

5050
$this->_ready = true;
51+
return $this;
5152
}
5253

5354
public function fetch( $maxRows ) {

src/ThriftSQL/Impala.php

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ public function __construct( $host, $port = 21000, $username = null, $password =
2020
}
2121

2222
public function connect() {
23-
2423
// Check if we have already connected
2524
if ( null !== $this->_client ) {
2625
return $this;
@@ -47,7 +46,6 @@ public function connect() {
4746
}
4847

4948
return $this;
50-
5149
}
5250

5351
public function query( $queryStr ) {
@@ -58,26 +56,7 @@ public function query( $queryStr ) {
5856
}
5957
}
6058

61-
public function queryAndFetchAll( $queryStr ) {
62-
try {
63-
$query = $this->query( $queryStr );
64-
$query->wait();
65-
$result = array();
66-
do {
67-
$rows = $query->fetch( 100 );
68-
if ( empty( $rows ) ) {
69-
break;
70-
}
71-
$result = array_merge( $result, $rows );
72-
} while ( true );
73-
return $result;
74-
} catch( Exception $e ) {
75-
throw new \ThriftSQL\Exception( $e->getMessage() );
76-
}
77-
}
78-
7959
public function disconnect() {
80-
8160
// Clear out the client
8261
$this->_client = null;
8362

@@ -86,6 +65,5 @@ public function disconnect() {
8665
$this->_transport->close();
8766
}
8867
$this->_transport = null;
89-
9068
}
9169
}

src/ThriftSQL/ImpalaQuery.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public function wait() {
5252
} while (true);
5353

5454
$this->_ready = true;
55+
return $this;
5556
}
5657

5758
public function fetch( $maxRows ) {

src/ThriftSQL/Utils/Iterator.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,8 @@ public function rewind() {
9696
}
9797
$this->runCount++;
9898

99-
$this->thriftSQLQuery = $this->thriftSQL->query( $this->queryStr );
10099
$this->buffer = array();
101100
$this->location = 0;
102-
$this->thriftSQLQuery->wait();
101+
$this->thriftSQLQuery = $this->thriftSQL->query( $this->queryStr )->wait();
103102
}
104103
}

0 commit comments

Comments
 (0)