packages/ztd-query-core/tests/Fake/ExceptionThrowingRewriter.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Fake;
6
7use Throwable;
8use ZtdQuery\Rewrite\MultiRewritePlan;
9use ZtdQuery\Rewrite\RewritePlan;
10use ZtdQuery\Rewrite\SqlRewriter;
11use ZtdQuery\Sql\TransactionStatement;
12
13/**
14 * Rewriter that throws an exception when rewrite() is called.
15 */
16final class ExceptionThrowingRewriter implements SqlRewriter
17{
18    /**
19     * Transaction statement.
20     *
21     * @param string $sql
22     * @return ?TransactionStatement
23     */
24    public function transactionStatement(string $sql): ?TransactionStatement
25    {
26        return null;
27    }
28
29    private Throwable $exception;
30
31    /**
32     * Binds the instance to what it will work from.
33     *
34     * @param Throwable $exception
35     */
36    public function __construct(Throwable $exception)
37    {
38        $this->exception = $exception;
39    }
40
41    /**
42     * Rewrite.
43     *
44     * @param string $sql
45     * @return RewritePlan
46     */
47    public function rewrite(string $sql): RewritePlan
48    {
49        throw $this->exception;
50    }
51
52    /**
53     * Split statements.
54     *
55     * @param string $sql
56     */
57    public function splitStatements(string $sql): array
58    {
59        return [$sql];
60    }
61
62    /**
63     * Rewrite multiple.
64     *
65     * @param string $sql
66     * @return MultiRewritePlan
67     */
68    public function rewriteMultiple(string $sql): MultiRewritePlan
69    {
70        throw $this->exception;
71    }
72
73    /**
74     * Empty result select.
75     *
76     * @return string
77     */
78    public function emptyResultSelect(): string
79    {
80        return 'SELECT 1 WHERE FALSE';
81    }
82}
83