quodlibetor
quodlibetor

Reputation: 8433

Is there a way to print the sql that would be executed on pdo->commit?

I've got a script that is capable of building up a longish transaction of prepared statements, and I'd like a way to print what is about to be executed.

None of the class methods in PDO look promising.

EDIT: This is for a (hacky) db upgrade script, as a sanity check. Devs are supposed to append to the script as they modify the database.

$commonStmt = $pdo->prepare("INSERT ... VALUES (:a, :b)");
function commonInsert($a, $b) {
    global $commonStmt;
    $commonStmt->bindParam(':a', $a);
    $commonStmt->bindParam(':b', $b);
    $commonStmt->execute();
}


$pdo->beginTransaction();
if ($db_version < $file_version) {
    commonInsert('a', 'b');
}
if ($db_version < $file_version) {
    commonInsert('c', 'd');
}

if ($db_version < $file_version) {
    $stmt = $pdo->prepare('INSERT ...');
    $stmt->execute();
}

if ($db_version < $file_version) {
    $pdo->query('INSERT');
}

// this will only get longer

$pdo->commit();

I could add debug statements (per Tamas' suggestion) to each update, but that seems onerous for a simple sanity check. And the commonInsert case is the one least likely to need it.

Upvotes: 2

Views: 291

Answers (1)

Tom Imrei
Tom Imrei

Reputation: 1544

how about PDOStatement::debugDumpParams?

Upvotes: 1

Related Questions