DataExporterWithORM
in package
Database data exporter using ORM QueryBuilder for SQL injection protection
This class exports database data to various formats (SQL, JSON, CSV, YAML) with full support for WHERE conditions using parameterized queries.
Table of Contents
Properties
- $_adapter : AdapterInterface
- $_environment : string
- $_migrationTable : string
- $_options : array<string|int, mixed>
- $_pdo : PDO
Methods
- __construct() : mixed
- Constructor
- __destruct() : mixed
- Destructor - ensure adapter is disconnected
- disconnect() : void
- Disconnect from database
- exportToFile() : string|false
- Export data to file
- getTableRowCount() : int
- Get row count for a table using parameterized query
- escapeValue() : string
- Escape value for SQL
- exportToCsvDirectory() : string|false
- Export to CSV format (directory with one file per table)
- exportToJson() : string
- Export to JSON format
- exportToSql() : string
- Export to SQL format
- exportToYaml() : string
- Export to YAML format
- formatBooleanLiteral() : string
- Format boolean value as SQL literal
- getTableData() : array<string|int, mixed>
- Get data from a table using QueryBuilder
- getTableSchema() : string
- Get table schema
- getTablesToExport() : array<string|int, mixed>
- Get list of tables to export
- hasLeadingZeros() : bool
- Check if a value has leading zeros that would be lost if treated as numeric
- parseWhereClause() : array<string|int, mixed>
- Parse a WHERE clause string into parameterized SQL
- quoteIdentifier() : string
- Quote a database identifier (table or column name)
Properties
$_adapter
private
AdapterInterface
$_adapter
$_environment
private
string
$_environment
$_migrationTable
private
string
$_migrationTable
$_options
private
array<string|int, mixed>
$_options
$_pdo
private
PDO
$_pdo
Methods
__construct()
Constructor
public
__construct(Config $config, string $environment, string $migrationTable[, array<string|int, mixed> $options = [] ]) : mixed
Parameters
- $config : Config
-
Phinx configuration
- $environment : string
-
Environment name
- $migrationTable : string
-
Migration table name
- $options : array<string|int, mixed> = []
-
Export options
__destruct()
Destructor - ensure adapter is disconnected
public
__destruct() : mixed
disconnect()
Disconnect from database
public
disconnect() : void
exportToFile()
Export data to file
public
exportToFile(string $outputPath) : string|false
Parameters
- $outputPath : string
-
Output file path
Return values
string|false —Actual output path or false on failure
getTableRowCount()
Get row count for a table using parameterized query
public
getTableRowCount(string $table) : int
Parameters
- $table : string
-
Table name
Return values
int —Row count
escapeValue()
Escape value for SQL
private
escapeValue(mixed $value) : string
Parameters
- $value : mixed
-
Value to escape
Return values
string —Escaped value
exportToCsvDirectory()
Export to CSV format (directory with one file per table)
private
exportToCsvDirectory(string $directory, array<string|int, mixed> $tables) : string|false
Parameters
- $directory : string
-
Output directory
- $tables : array<string|int, mixed>
-
Tables to export
Return values
string|false —Directory path or false on failure
exportToJson()
Export to JSON format
private
exportToJson(array<string|int, mixed> $tables) : string
Parameters
- $tables : array<string|int, mixed>
-
Tables to export
Return values
string —JSON content
exportToSql()
Export to SQL format
private
exportToSql(array<string|int, mixed> $tables) : string
Parameters
- $tables : array<string|int, mixed>
-
Tables to export
Return values
string —SQL content
exportToYaml()
Export to YAML format
private
exportToYaml(array<string|int, mixed> $tables) : string
Parameters
- $tables : array<string|int, mixed>
-
Tables to export
Return values
string —YAML content
formatBooleanLiteral()
Format boolean value as SQL literal
private
formatBooleanLiteral(bool $value) : string
PostgreSQL requires TRUE/FALSE literals for boolean columns. Other databases accept 1/0 for booleans.
Parameters
- $value : bool
-
Boolean value to format
Return values
string —SQL literal representation
getTableData()
Get data from a table using QueryBuilder
private
getTableData(string $table) : array<string|int, mixed>
Parameters
- $table : string
-
Table name
Return values
array<string|int, mixed> —Table data
getTableSchema()
Get table schema
private
getTableSchema(string $table) : string
Parameters
- $table : string
-
Table name
Return values
string —CREATE TABLE statement
getTablesToExport()
Get list of tables to export
private
getTablesToExport() : array<string|int, mixed>
Return values
array<string|int, mixed> —Table names
hasLeadingZeros()
Check if a value has leading zeros that would be lost if treated as numeric
private
hasLeadingZeros(mixed $value) : bool
Parameters
- $value : mixed
-
Value to check
Return values
bool —True if the value has leading zeros that need preservation
parseWhereClause()
Parse a WHERE clause string into parameterized SQL
private
parseWhereClause(string $whereClause) : array<string|int, mixed>
This is a thin wrapper that delegates to the centralized SqlWhereValidator for validation and uses proven parsing logic from DataExporter.
Converts "status = 'active' AND age > 18" into parameterized SQL with bindings. Supports:
- Comparison operators: =, !=, <>, <, >, <=, >=, LIKE, NOT LIKE
- IN/NOT IN operators: status IN ('active', 'pending')
- NULL checks: deleted_at IS NULL, active IS NOT NULL
- SQL-escaped quotes: name = 'O''Brien' is correctly parsed as O'Brien
Parameters
- $whereClause : string
-
The WHERE clause to parse
Tags
Return values
array<string|int, mixed> —Array with 'sql' and 'bindings' keys
quoteIdentifier()
Quote a database identifier (table or column name)
private
quoteIdentifier(string $identifier) : string
Parameters
- $identifier : string
-
The identifier to quote
Return values
string —The properly quoted identifier for the current adapter