TemporaryTableTest.php 3.48 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
<?php

namespace Doctrine\Tests\DBAL\Functional;

use \Doctrine\DBAL\Schema\Table;
use \Doctrine\DBAL\Schema\Column;
use \Doctrine\DBAL\Types\Type;

class TemporaryTableTest extends \Doctrine\Tests\DbalFunctionalTestCase
{
    public function setUp()
    {
        parent::setUp();
        try {
15 16 17 18 19 20 21 22
            $this->_conn->exec($this->_conn->getDatabasePlatform()->getDropTableSQL("nontemporary"));
        } catch(\Exception $e) {

        }
    }

    public function tearDown()
    {
23 24 25 26 27
        if ($this->_conn) {
            try {
                $tempTable = $this->_conn->getDatabasePlatform()->getTemporaryTableName("temporary");
                $this->_conn->exec($this->_conn->getDatabasePlatform()->getDropTemporaryTableSQL($tempTable));
            } catch(\Exception $e) { }
28 29 30 31 32 33 34
        }
    }

    /**
     * @group DDC-1337
     * @return void
     */
35
    public function testDropTemporaryTableNotAutoCommitTransaction()
36 37 38 39 40
    {
        $platform = $this->_conn->getDatabasePlatform();
        $columnDefinitions = array("id" => array("type" => Type::getType("integer"), "notnull" => true));
        $tempTable = $platform->getTemporaryTableName("temporary");

41
        $createTempTableSQL = $platform->getCreateTemporaryTableSnippetSQL() . ' ' . $tempTable . ' ('
42
                . $platform->getColumnDeclarationListSQL($columnDefinitions) . ')';
43
        $this->_conn->executeUpdate($createTempTableSQL);
44

45
        $table = new Table("nontemporary");
46
        $table->addColumn("id", "integer");
47
        $table->setPrimaryKey(array('id'));
48

49 50 51
        foreach ($platform->getCreateTableSQL($table) AS $sql) {
            $this->_conn->executeQuery($sql);
        }
52

53
        $this->_conn->beginTransaction();
54
        $this->_conn->insert("nontemporary", array("id" => 1));
55
        $this->_conn->exec($platform->getDropTemporaryTableSQL($tempTable));
56
        $this->_conn->insert("nontemporary", array("id" => 2));
57 58 59

        $this->_conn->rollback();

60 61
        $rows = $this->_conn->fetchAll('SELECT * FROM nontemporary');
        $this->assertEquals(array(), $rows, "In an event of an error this result has one row, because of an implicit commit.");
62 63 64 65 66 67
    }

    /**
     * @group DDC-1337
     * @return void
     */
68
    public function testCreateTemporaryTableNotAutoCommitTransaction()
69 70 71 72 73
    {
        $platform = $this->_conn->getDatabasePlatform();
        $columnDefinitions = array("id" => array("type" => Type::getType("integer"), "notnull" => true));
        $tempTable = $platform->getTemporaryTableName("temporary");

74
        $createTempTableSQL = $platform->getCreateTemporaryTableSnippetSQL() . ' ' . $tempTable . ' ('
75 76
                . $platform->getColumnDeclarationListSQL($columnDefinitions) . ')';

77
        $table = new Table("nontemporary");
78
        $table->addColumn("id", "integer");
79
        $table->setPrimaryKey(array('id'));
80

81 82 83
        foreach ($platform->getCreateTableSQL($table) AS $sql) {
            $this->_conn->executeQuery($sql);
        }
84

85
        $this->_conn->beginTransaction();
86
        $this->_conn->insert("nontemporary", array("id" => 1));
87

88 89
        $this->_conn->exec($createTempTableSQL);
        $this->_conn->insert("nontemporary", array("id" => 2));
90 91 92 93 94 95

        $this->_conn->rollback();

        try {
            $this->_conn->exec($platform->getDropTemporaryTableSQL($tempTable));
        } catch(\Exception $e) {
96

97 98
        }

99 100
        $rows = $this->_conn->fetchAll('SELECT * FROM nontemporary');
        $this->assertEquals(array(), $rows, "In an event of an error this result has one row, because of an implicit commit.");
101 102
    }
}