packages/ztd-query-core/tests/Fake/FixedRewriter.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Fake;
6
7use ZtdQuery\Rewrite\MultiRewritePlan;
8use ZtdQuery\Rewrite\RewritePlan;
9use ZtdQuery\Rewrite\SqlRewriter;
10use ZtdQuery\Sql\TransactionStatement;
11
12/**
13 * A rewriter that answers the same plan whatever it is given.
14 *
15 * A test about what the session does with a plan has no interest in how the
16 * plan was arrived at.
17 */
18final class FixedRewriter implements SqlRewriter
19{
20 /**
21 * Transaction statement.
22 *
23 * @param string $sql
24 * @return ?TransactionStatement
25 */
26 public function transactionStatement(string $sql): ?TransactionStatement
27 {
28 return null;
29 }
30
31 /**
32 * Predefined plan to return from rewrite().
33 *
34 * @var RewritePlan
35 */
36 private RewritePlan $plan;
37
38 /**
39 * Binds the instance to what it will work from.
40 *
41 * @param RewritePlan $plan
42 */
43 public function __construct(RewritePlan $plan)
44 {
45 $this->plan = $plan;
46 }
47
48 /**
49 * Rewrite.
50 *
51 * @param string $sql
52 * @return RewritePlan
53 */
54 public function rewrite(string $sql): RewritePlan
55 {
56 return $this->plan;
57 }
58
59 /**
60 * Split statements.
61 *
62 * @param string $sql
63 */
64 public function splitStatements(string $sql): array
65 {
66 return [$sql];
67 }
68
69 /**
70 * Rewrite multiple.
71 *
72 * @param string $sql
73 * @return MultiRewritePlan
74 */
75 public function rewriteMultiple(string $sql): MultiRewritePlan
76 {
77 return new MultiRewritePlan([$this->plan]);
78 }
79
80 /**
81 * Empty result select.
82 *
83 * @return string
84 */
85 public function emptyResultSelect(): string
86 {
87 return 'SELECT 1 WHERE FALSE';
88 }
89}
90