Skip to content

Commit c629372

Browse files
author
Matthew Wiltzius
committed
Add QueryOptions class
Add getId() and getState() methods to Query class Miscellaneous bugfixes
1 parent f4dba1b commit c629372

File tree

3 files changed

+55
-12
lines changed

3 files changed

+55
-12
lines changed

src/Query.php

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,16 @@ class Query
3333
private int $rows = 100;
3434
private ?string $correlationId = null;
3535

36-
public function __construct(SQLJob $job, string $sql, ?array $options)
36+
public function __construct(SQLJob $job, string $sql, ?QueryOptions $options)
3737
{
3838
$this->job = $job;
3939
$this->sql = $sql;
4040

4141
$options = $options ?? [];
42-
$this->parameters = $options['parameters'] ?? [];
43-
$this->isPrepared = !empty($options['parameters']);
44-
$this->isClCommand = $options['isClCommand'] ?? false;
45-
$this->isTerseResults = $options['isTerseResults'] ?? false;
42+
$this->parameters = $options->parameters ?? [];
43+
$this->isPrepared = !empty($options->parameters);
44+
$this->isClCommand = $options->isClCommand ?? false;
45+
$this->isTerseResults = $options->isTerseResults ?? false;
4646

4747
self::$globalQueryList[] = $this;
4848
}
@@ -86,7 +86,7 @@ public function prepareSqlExecute(): array
8686
}
8787
if (count($errorList) == 0)
8888
$errorList['error'] = "failed to run query for unknown reason";
89-
throw new \RunetimeException(json_encode($errorList));
89+
throw new \RuntimeException(json_encode($errorList));
9090
}
9191

9292
$isDone = (bool)($results['is_done'] ?? false);
@@ -97,7 +97,7 @@ public function prepareSqlExecute(): array
9797
return $results;
9898
}
9999

100-
public function run(int $rows = null): array
100+
public function run(?int $rows = null): array
101101
{
102102
if ($rows == null)
103103
$rows = $this->rows;
@@ -128,7 +128,7 @@ public function run(int $rows = null): array
128128
'parameters' => $this->parameters,
129129
];
130130

131-
$results = executeQuery($queryObject);
131+
$results = $this->executeQuery($queryObject);
132132

133133
$success = (bool)($results['success'] ?? false);
134134
if (!$success && !$this->isClCommand) {
@@ -141,7 +141,7 @@ public function run(int $rows = null): array
141141
}
142142
if (count($errorList) == 0)
143143
$errorList['error'] = "failed to run query for unknown reason";
144-
throw new \RunetimeException(json_encode($errorList));
144+
throw new \RuntimeException(json_encode($errorList));
145145
}
146146

147147
$isDone = (bool)($results['is_done'] ?? false);
@@ -179,7 +179,7 @@ public function fetchMore(int $rows): array
179179
$success = (bool)($results['success'] ?? false);
180180
if (!$success && !$this->isClCommand) {
181181
$this->state = QueryState::ERROR;
182-
throw new \RunetimeException(($results['error'] ?? "Failed to run Query (unknown error)"));
182+
throw new \RuntimeException(($results['error'] ?? "Failed to run Query (unknown error)"));
183183
}
184184

185185
$isDone = (bool)($results['is_done'] ?? false);
@@ -204,4 +204,14 @@ public function close(): array
204204
elseif (!$this->correlationId)
205205
$this->state = QueryState::RUN_DONE;
206206
}
207+
208+
public function getId(): string
209+
{
210+
return $this->correlationId;
211+
}
212+
213+
public function getState(): QueryState
214+
{
215+
return $this->state;
216+
}
207217
}

src/QueryOptions.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php declare(strict_types=1);
2+
3+
/*
4+
* Copyright 2024 IBM
5+
* All Rights Reserved
6+
* Apache License 2.0 ... see LICENSE file
7+
*
8+
* Original Author: Matthew Wiltzius <[email protected]>
9+
*/
10+
11+
namespace Mapepire;
12+
13+
final class QueryOptions
14+
{
15+
public ?bool $isTerseResults = null;
16+
public ?bool $isClCommand = null;
17+
public ?array $parameters = null;
18+
public ?bool $autoClose = null;
19+
20+
public function __construct(?array $data = null) {
21+
$this->isTerseResults = isset($data['isTerseResults']) ? (bool)$data['isTerseResults'] : null;
22+
$this->isClCommand = isset($data['isClCommand']) ? (bool)$data['isClCommand'] : null;
23+
$this->parameters = isset($data['parameters']) ? array_values((array)$data['parameters']) : null;
24+
$this->autoClose = isset($data['autoClose']) ? (bool)$data['autoClose'] : null;
25+
}
26+
27+
public function test() {
28+
print("We're using the correct one!");
29+
}
30+
}

src/SQLJob.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,9 +203,12 @@ public function close(): void
203203
}
204204
}
205205

206-
public function query(string $sql, ?array $options = null): Query
206+
public function query(string $sql, QueryOptions|array|null $options = null): Query
207207
{
208-
return new Query($this, $sql, null);
208+
if(is_array($options)) {
209+
$options = new QueryOptions($options);
210+
}
211+
return new Query($this, $sql, $options);
209212
}
210213

211214
public function queryAndRun(string $sql): string

0 commit comments

Comments
 (0)