packages/ztd-query-postgres/fuzz/Input/InsertLiterals.php

1<?php
2
3declare(strict_types=1);
4
5namespace Fuzz\Input;
6
7use ZtdQuery\Schema\TableDefinition;
8
9/**
10 * Creates reproducible PostgreSQL pipeline fixtures.
11 */
12final class InsertLiterals
13{
14    /**
15     * Supplies a seeded value generator.
16     */
17    public function __construct(private readonly \Faker\Generator $faker)
18    {
19    }
20
21    /**
22     * Build a VALUES clause with placeholder literals for all columns.
23     */
24    public function buildInsertValues(TableDefinition $definition): string
25    {
26        $values = [];
27        foreach ($definition->columns as $col) {
28            $type = strtoupper($definition->columnTypes[$col] ?? 'TEXT');
29            $baseType = preg_replace('/\(.*\)/', '', $type);
30            $baseType = trim($baseType ?? $type);
31            $values[] = match (true) {
32                in_array($baseType, ['INT', 'INT2', 'INT4', 'INT8', 'INTEGER', 'SMALLINT', 'BIGINT', 'SERIAL', 'SMALLSERIAL', 'BIGSERIAL'], true) => (string) $this->faker->numberBetween(1, 9999),
33                in_array($baseType, ['REAL', 'FLOAT4', 'DOUBLE PRECISION', 'FLOAT8', 'DECIMAL', 'NUMERIC'], true) => (string) round($this->faker->randomFloat(2, 0, 999), 2),
34                in_array($baseType, ['BOOLEAN', 'BOOL'], true) => $this->faker->boolean() ? 'TRUE' : 'FALSE',
35                default => "'" . str_replace("'", "''", $this->faker->word()) . "'",
36            };
37        }
38        return implode(', ', $values);
39    }
40}
41