# trinity-backup/2.0.5/src/Database/QueryBuilder.php

Trinity Backup – Backup, Migrate, Restore, Clone &amp; Schedule Backups, version 2.0.5. 60 lines.

- Page: https://pluginprobe.com/plugins/trinity-backup/2.0.5/code/src/Database/QueryBuilder.php
- Raw: https://pluginprobe.com/plugins/trinity-backup/2.0.5/raw/src/Database/QueryBuilder.php
- Modified: 2026-01-29T16:15:16+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/trinity-backup/2.0.5/code/src/Database/QueryBuilder.php#L10-L20`.

```php
<?php

declare(strict_types=1);

namespace TrinityBackup\Database;

if (!\defined('ABSPATH')) {
    exit;
}

final class QueryBuilder
{
    /** @param array<string, mixed> $row */
    public function buildInsert(string $table, array $row, callable $escaper): string
    {
        $columns = [];
        $values = [];

        foreach ($row as $column => $value) {
            $columns[] = $this->escapeIdentifier((string) $column);
            $values[] = $this->formatValue($value, $escaper);
        }

        $columnList = implode(', ', $columns);
        $valueList = implode(', ', $values);

        // Use INSERT INTO - tables are dropped and recreated before import
        // so there are no conflicts with existing data
        return sprintf(
            "INSERT INTO %s (%s) VALUES (%s);\n",
            $this->escapeIdentifier($table),
            $columnList,
            $valueList
        );
    }

    private function formatValue(mixed $value, callable $escaper): string
    {
        if ($value === null) {
            return 'NULL';
        }

        if (is_bool($value)) {
            return $value ? '1' : '0';
        }

        if (is_int($value) || is_float($value)) {
            return (string) $value;
        }

        $escaped = $escaper((string) $value);
        return "'" . $escaped . "'";
    }

    private function escapeIdentifier(string $identifier): string
    {
        return '`' . str_replace('`', '``', $identifier) . '`';
    }
}

```
