-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy path03-export-helpers.php
More file actions
164 lines (130 loc) · 4.36 KB
/
03-export-helpers.php
File metadata and controls
164 lines (130 loc) · 4.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php
/**
* Export Helpers Examples
*
* Demonstrates how to export database results to various formats (JSON, CSV, XML)
* using the helper methods: Db::toJson(), Db::toCsv(), Db::toXml()
*/
require_once __DIR__ . '/../../vendor/autoload.php';
use tommyknocker\pdodb\PdoDb;
use tommyknocker\pdodb\helpers\Db;
// Initialize database connection
$driver = getenv('PDODB_DRIVER') ?: 'sqlite';
$config = match ($driver) {
'mysql' => require __DIR__ . '/../config.mysql.php',
'pgsql' => require __DIR__ . '/../config.pgsql.php',
default => require __DIR__ . '/../config.sqlite.php',
};
$db = new PdoDb($driver, $config);
echo "=== Export Helpers Examples ===\n\n";
// Create and populate test table using fluent API (cross-dialect)
$schema = $db->schema();
$schema->dropTableIfExists('export_test');
$autoIncrement = $driver === 'pgsql' ? 'SERIAL PRIMARY KEY' : 'INT AUTO_INCREMENT PRIMARY KEY';
$db->rawQuery('
CREATE TABLE export_test (
id '. $autoIncrement .',
name VARCHAR(255),
email VARCHAR(255),
age INT,
city VARCHAR(255)
)
');
$db->find()->table('export_test')->insertMulti([
['name' => 'Alice Johnson', 'email' => 'alice@example.com', 'age' => 30, 'city' => 'New York'],
['name' => 'Bob Smith', 'email' => 'bob@example.com', 'age' => 25, 'city' => 'Los Angeles'],
['name' => 'Carol Davis', 'email' => 'carol@example.com', 'age' => 28, 'city' => 'Chicago'],
]);
// Get data from database
$data = $db->find()->from('export_test')->get();
echo "Total records: " . count($data) . "\n\n";
// Example 1: Export to JSON
echo "=== 1. JSON Export ===\n";
$json = Db::toJson($data);
echo "Basic JSON export:\n";
echo $json . "\n\n";
// JSON with custom flags
$jsonCompact = Db::toJson($data, 0); // No pretty print
echo "Compact JSON (no pretty print):\n";
echo $jsonCompact . "\n\n";
// Example 2: Export to CSV
echo "=== 2. CSV Export ===\n";
$csv = Db::toCsv($data);
echo "CSV export:\n";
echo $csv . "\n\n";
// CSV with custom delimiter
$csvCustom = Db::toCsv($data, ';');
echo "CSV with semicolon delimiter:\n";
echo $csvCustom . "\n\n";
// Example 3: Export to XML
echo "=== 3. XML Export ===\n";
$xml = Db::toXml($data);
echo "XML export:\n";
echo $xml . "\n\n";
// XML with custom element names
$xmlCustom = Db::toXml($data, 'users', 'user');
echo "XML with custom elements (<users>, <user>):\n";
echo $xmlCustom . "\n\n";
// Example 4: Export specific fields
echo "=== 4. Export Specific Fields ===\n";
$filteredData = $db->find()
->from('export_test')
->select(['name', 'email'])
->get();
$jsonFiltered = Db::toJson($filteredData);
echo "JSON export (name and email only):\n";
echo $jsonFiltered . "\n\n";
// Example 5: Export with conditions
echo "=== 5. Export Filtered Data ===\n";
$adults = $db->find()
->from('export_test')
->where('age', 27, '>')
->get();
echo "Records with age > 27:\n";
echo Db::toJson($adults) . "\n\n";
// Example 6: Save to file
echo "=== 6. Save to Files ===\n";
$outputDir = __DIR__ . '/output';
if (!is_dir($outputDir)) {
mkdir($outputDir, 0755, true);
}
file_put_contents($outputDir . '/users.json', Db::toJson($data));
file_put_contents($outputDir . '/users.csv', Db::toCsv($data));
file_put_contents($outputDir . '/users.xml', Db::toXml($data));
echo "Files saved to: $outputDir/\n";
echo "- users.json\n";
echo "- users.csv\n";
echo "- users.xml\n\n";
// Example 7: Export empty result set
echo "=== 7. Export Empty Data ===\n";
$empty = $db->find()->from('export_test')->where('id', 9999)->get();
echo "JSON of empty array: " . Db::toJson($empty) . "\n";
echo "CSV of empty array: '" . Db::toCsv($empty) . "'\n";
echo "XML of empty array: " . Db::toXml($empty) . "\n\n";
// Example 8: Export with nested data
echo "=== 8. Export Complex Data ===\n";
$complexData = [
[
'id' => 1,
'name' => 'Alice',
'tags' => ['php', 'mysql', 'docker'],
'metadata' => [
'location' => 'USA',
'status' => 'active'
]
],
[
'id' => 2,
'name' => 'Bob',
'tags' => ['python', 'postgresql'],
'metadata' => [
'location' => 'UK',
'status' => 'pending'
]
]
];
$jsonComplex = Db::toJson($complexData);
echo "JSON with nested arrays/objects:\n";
echo $jsonComplex . "\n\n";
echo "=== All Examples Completed ===\n";
echo "Check output/ directory for generated files.\n";