Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Flow\PostgreSql\AST\Transformers;

use Flow\PostgreSql\Exception\InvalidExplainConfigException;
use Flow\PostgreSql\QueryBuilder\Utility\ExplainFormat;

final readonly class ExplainConfig
Expand All @@ -20,6 +21,7 @@ public function __construct(
public bool $wal = false,
public ExplainFormat $format = ExplainFormat::JSON,
) {
$this->validate();
}

public static function forAnalysis() : self
Expand Down Expand Up @@ -53,4 +55,325 @@ public static function forEstimate() : self
format: ExplainFormat::JSON,
);
}

public function withAnalyze() : self
{
return new self(
analyze: true,
verbose: $this->verbose,
costs: $this->costs,
buffers: $this->buffers,
timing: $this->timing,
summary: $this->summary,
memory: $this->memory,
settings: $this->settings,
wal: $this->wal,
format: $this->format,
);
}

public function withBuffers() : self
{
return new self(
analyze: $this->analyze,
verbose: $this->verbose,
costs: $this->costs,
buffers: true,
timing: $this->timing,
summary: $this->summary,
memory: $this->memory,
settings: $this->settings,
wal: $this->wal,
format: $this->format,
);
}

public function withCosts() : self
{
return new self(
analyze: $this->analyze,
verbose: $this->verbose,
costs: true,
buffers: $this->buffers,
timing: $this->timing,
summary: $this->summary,
memory: $this->memory,
settings: $this->settings,
wal: $this->wal,
format: $this->format,
);
}

public function withFormat(ExplainFormat $format) : self
{
return new self(
analyze: $this->analyze,
verbose: $this->verbose,
costs: $this->costs,
buffers: $this->buffers,
timing: $this->timing,
summary: $this->summary,
memory: $this->memory,
settings: $this->settings,
wal: $this->wal,
format: $format,
);
}

public function withMemory() : self
{
return new self(
analyze: $this->analyze,
verbose: $this->verbose,
costs: $this->costs,
buffers: $this->buffers,
timing: $this->timing,
summary: $this->summary,
memory: true,
settings: $this->settings,
wal: $this->wal,
format: $this->format,
);
}

public function withoutAnalyze() : self
{
return new self(
analyze: false,
verbose: $this->verbose,
costs: $this->costs,
buffers: false,
timing: false,
summary: $this->summary,
memory: $this->memory,
settings: $this->settings,
wal: false,
format: $this->format,
);
}

public function withoutBuffers() : self
{
return new self(
analyze: $this->analyze,
verbose: $this->verbose,
costs: $this->costs,
buffers: false,
timing: $this->timing,
summary: $this->summary,
memory: $this->memory,
settings: $this->settings,
wal: $this->wal,
format: $this->format,
);
}

public function withoutCosts() : self
{
return new self(
analyze: $this->analyze,
verbose: $this->verbose,
costs: false,
buffers: $this->buffers,
timing: $this->timing,
summary: $this->summary,
memory: $this->memory,
settings: $this->settings,
wal: $this->wal,
format: $this->format,
);
}

public function withoutMemory() : self
{
return new self(
analyze: $this->analyze,
verbose: $this->verbose,
costs: $this->costs,
buffers: $this->buffers,
timing: $this->timing,
summary: $this->summary,
memory: false,
settings: $this->settings,
wal: $this->wal,
format: $this->format,
);
}

public function withoutSettings() : self
{
return new self(
analyze: $this->analyze,
verbose: $this->verbose,
costs: $this->costs,
buffers: $this->buffers,
timing: $this->timing,
summary: $this->summary,
memory: $this->memory,
settings: false,
wal: $this->wal,
format: $this->format,
);
}

public function withoutSummary() : self
{
return new self(
analyze: $this->analyze,
verbose: $this->verbose,
costs: $this->costs,
buffers: $this->buffers,
timing: $this->timing,
summary: false,
memory: $this->memory,
settings: $this->settings,
wal: $this->wal,
format: $this->format,
);
}

public function withoutTiming() : self
{
return new self(
analyze: $this->analyze,
verbose: $this->verbose,
costs: $this->costs,
buffers: $this->buffers,
timing: false,
summary: $this->summary,
memory: $this->memory,
settings: $this->settings,
wal: $this->wal,
format: $this->format,
);
}

public function withoutVerbose() : self
{
return new self(
analyze: $this->analyze,
verbose: false,
costs: $this->costs,
buffers: $this->buffers,
timing: $this->timing,
summary: $this->summary,
memory: $this->memory,
settings: $this->settings,
wal: $this->wal,
format: $this->format,
);
}

public function withoutWal() : self
{
return new self(
analyze: $this->analyze,
verbose: $this->verbose,
costs: $this->costs,
buffers: $this->buffers,
timing: $this->timing,
summary: $this->summary,
memory: $this->memory,
settings: $this->settings,
wal: false,
format: $this->format,
);
}

public function withSettings() : self
{
return new self(
analyze: $this->analyze,
verbose: $this->verbose,
costs: $this->costs,
buffers: $this->buffers,
timing: $this->timing,
summary: $this->summary,
memory: $this->memory,
settings: true,
wal: $this->wal,
format: $this->format,
);
}

public function withSummary() : self
{
return new self(
analyze: $this->analyze,
verbose: $this->verbose,
costs: $this->costs,
buffers: $this->buffers,
timing: $this->timing,
summary: true,
memory: $this->memory,
settings: $this->settings,
wal: $this->wal,
format: $this->format,
);
}

public function withTiming() : self
{
return new self(
analyze: $this->analyze,
verbose: $this->verbose,
costs: $this->costs,
buffers: $this->buffers,
timing: true,
summary: $this->summary,
memory: $this->memory,
settings: $this->settings,
wal: $this->wal,
format: $this->format,
);
}

public function withVerbose() : self
{
return new self(
analyze: $this->analyze,
verbose: true,
costs: $this->costs,
buffers: $this->buffers,
timing: $this->timing,
summary: $this->summary,
memory: $this->memory,
settings: $this->settings,
wal: $this->wal,
format: $this->format,
);
}

public function withWal() : self
{
return new self(
analyze: $this->analyze,
verbose: $this->verbose,
costs: $this->costs,
buffers: $this->buffers,
timing: $this->timing,
summary: $this->summary,
memory: $this->memory,
settings: $this->settings,
wal: true,
format: $this->format,
);
}

private function validate() : void
{
if (!$this->analyze) {
if ($this->buffers) {
throw InvalidExplainConfigException::buffersRequiresAnalyze();
}

if ($this->timing) {
throw InvalidExplainConfigException::timingRequiresAnalyze();
}

if ($this->wal) {
throw InvalidExplainConfigException::walRequiresAnalyze();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,28 +100,22 @@ private function wrapWithExplain(SelectStmt $stmt) : Node

$options[] = $this->createDefElemInt('costs', $this->config->costs ? 1 : 0);

if ($this->config->buffers) {
$options[] = $this->createDefElemInt('buffers', 1);
}

if ($this->config->timing) {
$options[] = $this->createDefElemInt('timing', 1);
}

if ($this->config->summary) {
$options[] = $this->createDefElemInt('summary', 1);
}
if ($this->config->analyze) {
$options[] = $this->createDefElemInt('buffers', $this->config->buffers ? 1 : 0);
$options[] = $this->createDefElemInt('timing', $this->config->timing ? 1 : 0);
$options[] = $this->createDefElemInt('summary', $this->config->summary ? 1 : 0);

if ($this->config->memory) {
$options[] = $this->createDefElemInt('memory', 1);
}
if ($this->config->memory) {
$options[] = $this->createDefElemInt('memory', 1);
}

if ($this->config->settings) {
$options[] = $this->createDefElemInt('settings', 1);
}
if ($this->config->settings) {
$options[] = $this->createDefElemInt('settings', 1);
}

if ($this->config->wal) {
$options[] = $this->createDefElemInt('wal', 1);
if ($this->config->wal) {
$options[] = $this->createDefElemInt('wal', 1);
}
}

$options[] = $this->createDefElemString('format', $this->config->format->value);
Expand Down
Loading
Loading