Example #1
0
 public function rollbackAll()
 {
     if (null === $this->current_name) {
         return;
     }
     $this->names = [];
     $this->current_name = null;
     $this->yo_pdo->query('ROLLBACK');
 }
Example #2
0
    /**
     * @return bool
     */
    public function hasSchema()
    {
        $sql = <<<SQL
SELECT 1 AS schema_exists
FROM pg_tables
WHERE schemaname = 'migrations'
    AND tablename = 'migrations'
SQL;
        $row = $this->yo_pdo->query($sql)->fetch();
        if ($row) {
            return (bool) $row['schema_exists'];
        }
        return false;
    }
Example #3
0
    /**
     * @param YoPdo $yo_pdo
     * @param string $table_name
     * @param array $expected_results
     * @return array
     */
    public function assertResults(YoPdo $yo_pdo, $table_name, array $expected_results)
    {
        $column_sql = '';
        if ($expected_results && reset($expected_results)) {
            $column_sql = ', ' . implode(', ', array_keys(reset($expected_results)));
        }
        $sql = <<<SQL
SELECT id {$column_sql}
FROM {$table_name}
ORDER BY id
SQL;
        $result = $yo_pdo->query($sql);
        while ($row = $result->fetch()) {
            if (!array_key_exists('id', $row)) {
                $this->test_case->assertTrue(false, "Field 'id' not found in row.");
            } elseif (!array_key_exists($row['id'], $expected_results)) {
                $this->test_case->assertTrue(false, "Row with key '{$row['id']}' not found in expected results.");
            } else {
                $expected_result = $expected_results[$row['id']];
                $expected_result['id'] = $row['id'];
                if (array_diff_assoc($expected_result, $row) || array_diff_assoc($row, $expected_result)) {
                    $this->test_case->assertEquals($expected_result, $row);
                }
                unset($expected_results[$row['id']]);
            }
        }
        $this->test_case->assertEmpty($expected_results);
    }
Example #4
0
    /**
     * @covers ::query
     * @covers ::<private>
     * @dataProvider dbProvider
     * @param YoPdo $yo_pdo
     */
    public function testASimpleQueryCanBeRan(YoPdo $yo_pdo)
    {
        $sql = <<<SQL
SELECT :param_a AS col UNION
SELECT :param_b AS col UNION
SELECT :last_param AS col
ORDER BY col
SQL;
        $result = $yo_pdo->query($sql, array('param_a' => 1, 'param_b' => 2, 'last_param' => 3));
        $count = 1;
        while ($row = $result->fetch()) {
            $this->assertEquals($count, $row['col']);
            ++$count;
        }
    }
Example #5
0
    /**
     * @param array $additional_records
     */
    public function insertRecords(array $additional_records = [])
    {
        $this->bufferRecords($additional_records);
        if (!count($this->records)) {
            return;
        }
        $table_name = $this->table_name;
        $columns = implode(', ', $this->columns);
        $placeholders = $this->getRecordLines(count($this->records));
        $flattened_records = $this->flattenRecords($this->records);
        $sql = <<<SQL
INSERT INTO {$table_name}
({$columns})
VALUES
{$placeholders}
SQL;
        $this->yo_pdo->query($sql, $flattened_records);
        $this->records = [];
    }