public function testDBAL() { $connectionParams = ['dbname' => $this->conf['dbname'], 'user' => $this->conf['username'], 'password' => $this->conf['password'], 'host' => $this->conf['host'], 'driver' => 'pdo_pgsql']; $conn = DriverManager::getConnection($connectionParams); $conn->exec(file_get_contents(__DIR__ . '/fixtures/createTable.sql')); $sth = $conn->query("SELECT * FROM PUBLIC.TBUPSERTEXAMPLE"); $result = $sth->fetchAll(); $this->assertEquals(0, count($result)); $conn->transactional(function ($conn) { Upsert::createFromDBAL($conn)->exec('PUBLIC.TBUPSERTEXAMPLE', ['key1' => 'key1', 'key2' => 'key2', 'key3' => 'key3', 'key4' => 'key4'], ['value1' => 'value1', 'value2' => 'value2', 'value3' => 'value3', 'value4' => null, 'value5' => 'value5']); }); $sth = $conn->query("SELECT * FROM PUBLIC.TBUPSERTEXAMPLE"); $result = $sth->fetchAll(); $this->assertEquals('value1', $result[0]['value1']); $this->assertEquals('value2', $result[0]['value2']); $this->assertEquals('value3', $result[0]['value3']); $this->assertEquals(null, $result[0]['value4']); $this->assertEquals('value5', $result[0]['value5']); $conn->transactional(function ($conn) { Upsert::createFromDBAL($conn)->exec('PUBLIC.TBUPSERTEXAMPLE', ['key1' => 'key1', 'key2' => 'key2', 'key3' => 'key3', 'key4' => 'key4'], ['value1' => 'value1', 'value2' => 'value2', 'value3' => 'value3', 'value4' => 'value4', 'value5' => 'value5']); }); $sth = $conn->query("SELECT * FROM PUBLIC.TBUPSERTEXAMPLE"); $result = $sth->fetchAll(); $this->assertEquals('value1', $result[0]['value1']); $this->assertEquals('value2', $result[0]['value2']); $this->assertEquals('value3', $result[0]['value3']); $this->assertEquals('value4', $result[0]['value4']); $this->assertEquals('value5', $result[0]['value5']); $conn->exec(file_get_contents(__DIR__ . '/fixtures/dropTable.sql')); }
<?php include __DIR__ . '/../vendor/autoload.php'; use Doctrine\DBAL\DriverManager; use G\SqlUtils\Upsert; $connectionParams = ['dbname' => 'gonzalo', 'user' => 'username', 'password' => 'password', 'host' => 'localhost', 'driver' => 'pdo_pgsql']; $dbh = DriverManager::getConnection($connectionParams); $dbh->transactional(function ($conn) { Upsert::createFromDBAL($conn)->exec('public.tbupsertexample', ['KEY1' => 'key1', 'KEY2' => 'key2', 'KEY3' => 'key3', 'KEY4' => 'key4'], ['VALUE1' => 'value1', 'VALUE2' => 'value2', 'VALUE3' => 'value3', 'VALUE4' => null, 'VALUE5' => 'value5']); });
<?php include __DIR__ . '/../vendor/autoload.php'; use G\SqlUtils\Upsert; $conn = new PDO('pgsql:dbname=gonzalo;host=localhost', 'username', 'password'); $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $conn->beginTransaction(); try { Upsert::createFromPDO($conn)->exec('PUBLIC.TBUPSERTEXAMPLE', ['KEY1' => 'key1', 'KEY2' => 'key2', 'KEY3' => 'key3', 'KEY4' => 'key4'], ['VALUE1' => 'value1', 'VALUE2' => 'value2', 'VALUE3' => 'value3', 'VALUE4' => 'value4', 'VALUE5' => 'value5']); $conn->commit(); } catch (Exception $e) { $conn->rollback(); throw $e; }