Skip to content

Commit fce482a

Browse files
committed
Add support for specifying custom SQL statement
1 parent e09d152 commit fce482a

File tree

1 file changed

+23
-8
lines changed

1 file changed

+23
-8
lines changed

src/transform.php

+23-8
Original file line numberDiff line numberDiff line change
@@ -145,13 +145,11 @@ function writeSchema($conn, $tables) {
145145
info('Wrote /result/combined_schema.json');
146146
}
147147

148-
function dumpTable($conn, $adapter, $table) {
149-
info('Dumping ' . $table);
150-
151-
$query = $conn->query($adapter->selectSql($table));
148+
function dumpSql($conn, $sql, $filename) {
149+
$query = $conn->query($sql);
152150
$query->execute();
153151

154-
$json = new CompressingJsonStreamFileWriter('/result/data/' . $table . '.json.gz');
152+
$json = new CompressingJsonStreamFileWriter($filename);
155153
$json->writer->arrayBegin();
156154

157155
$rowCount = 0;
@@ -169,6 +167,12 @@ function dumpTable($conn, $adapter, $table) {
169167
info('Wrote ' . $rowCount . ' rows to ' . $json->getPath());
170168
}
171169

170+
function dumpTable($conn, $adapter, $table) {
171+
info('Dumping ' . $table);
172+
173+
dumpSql($conn, $adapter->selectSql($table), '/result/data/' . $table . '.json.gz');
174+
}
175+
172176
info($username !== '' ? 'Using username: ' . $username : 'Username: (no username)');
173177
info($password !== '' ? 'Using password: ********' : 'Password: (no password)');
174178
info('Connecting to DSN ' . $dsn);
@@ -205,8 +209,19 @@ function dumpTable($conn, $adapter, $table) {
205209
info('Skipping schema fetch - only know how to do it for MySQL');
206210
}
207211

208-
foreach($tables as $table) {
209-
dumpTable($conn, $adapter, $table);
212+
$customSql = getenv('SQL');
213+
214+
if ($customSql) {
215+
info("Custom SQL statement specified: $customSql");
216+
217+
dumpSql($conn, $customSql, '/result/data/custom_' . time() . '.json.gz');
218+
}
219+
else {
220+
foreach($tables as $table) {
221+
dumpTable($conn, $adapter, $table);
222+
}
223+
224+
info('Exported ' . count($tables) . ' tables');
210225
}
211226

212-
info('Done, exported ' . count($tables) . ' tables');
227+
info('Done');

0 commit comments

Comments
 (0)