Skip to content

Commit 715dd1d

Browse files
Merge pull request #10 from SarafApp/feature/stream-raw
feat: added raw stream that returns ReadableStreamInterface
2 parents 9493104 + 6dcdde8 commit 715dd1d

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

src/QueryBuilder/Core/DBFactory.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,28 @@ public function streamQuery(string $query): StreamEventHandler
183183
return $connection->streamQuery($query);
184184
}
185185

186+
/**
187+
* @throws \Saraf\QB\QueryBuilder\Exceptions\DBFactoryException
188+
*/
189+
public function streamQueryRaw(string $query): ReadableStreamInterface
190+
{
191+
$isWrite = true;
192+
if (str_starts_with(strtolower($query), "select")
193+
|| str_starts_with(strtolower($query), "show")
194+
) $isWrite = false;
195+
196+
$bestConnections = $this->getBestConnection();
197+
198+
$connection = $isWrite
199+
? $this->writeConnections[$bestConnections['write']]
200+
: $this->readConnections[$bestConnections['read']];
201+
202+
if (!($connection instanceof DBWorker))
203+
throw new DBFactoryException("Connections Not Instance of Worker / Restart App");
204+
205+
return $connection->streamQueryRaw($query);
206+
}
207+
186208
/**
187209
* @throws DBFactoryException
188210
*/

src/QueryBuilder/Core/DBWorker.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ public function streamQuery(string $query): StreamEventHandler
4040
}));
4141
}
4242

43+
public function streamQueryRaw(string $query): ReadableStreamInterface
44+
{
45+
return $this->connection->queryStream($query);
46+
}
47+
4348
protected function handleResult(QueryResult $result): array
4449
{
4550
if (!is_null($result->resultRows)) {

src/QueryBuilder/Core/EQuery.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ public function stream(): StreamEventHandler
3636
return $this->factory->streamQuery($this->query);
3737
}
3838

39+
public function streamRaw(): ReadableStreamInterface
40+
{
41+
return $this->factory->streamQueryRaw($this->query);
42+
}
43+
3944
public function getQuery(): Promise
4045
{
4146
return new Promise(function (callable $resolve) {

0 commit comments

Comments
 (0)