TemporaryTableTest.php 3.95 KB
Newer Older
1 2 3 4
<?php

namespace Doctrine\Tests\DBAL\Functional;

jeroendedauw's avatar
jeroendedauw committed
5 6
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Types\Type;
7 8 9 10 11 12 13

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

        }
    }

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

    /**
     * @group DDC-1337
     * @return void
     */
34
    public function testDropTemporaryTableNotAutoCommitTransaction()
35
    {
36 37 38
        if ($this->_conn->getDatabasePlatform()->getName() == 'sqlanywhere' ||
            $this->_conn->getDatabasePlatform()->getName() == 'oracle') {
            $this->markTestSkipped("Test does not work on Oracle and SQL Anywhere.");
39 40
        }

41 42
        $platform = $this->_conn->getDatabasePlatform();
        $columnDefinitions = array("id" => array("type" => Type::getType("integer"), "notnull" => true));
43
        $tempTable = $platform->getTemporaryTableName("my_temporary");
44

45
        $createTempTableSQL = $platform->getCreateTemporaryTableSnippetSQL() . ' ' . $tempTable . ' ('
46
                . $platform->getColumnDeclarationListSQL($columnDefinitions) . ')';
47
        $this->_conn->executeUpdate($createTempTableSQL);
48

49
        $table = new Table("nontemporary");
50
        $table->addColumn("id", "integer");
51
        $table->setPrimaryKey(array('id'));
52

53 54 55
        foreach ($platform->getCreateTableSQL($table) AS $sql) {
            $this->_conn->executeQuery($sql);
        }
56

57
        $this->_conn->beginTransaction();
58
        $this->_conn->insert("nontemporary", array("id" => 1));
59
        $this->_conn->exec($platform->getDropTemporaryTableSQL($tempTable));
60
        $this->_conn->insert("nontemporary", array("id" => 2));
61 62 63

        $this->_conn->rollback();

64 65
        $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.");
66 67 68 69 70 71
    }

    /**
     * @group DDC-1337
     * @return void
     */
72
    public function testCreateTemporaryTableNotAutoCommitTransaction()
73
    {
74 75 76
        if ($this->_conn->getDatabasePlatform()->getName() == 'sqlanywhere' ||
            $this->_conn->getDatabasePlatform()->getName() == 'oracle') {
            $this->markTestSkipped("Test does not work on Oracle and SQL Anywhere.");
77 78
        }

79 80
        $platform = $this->_conn->getDatabasePlatform();
        $columnDefinitions = array("id" => array("type" => Type::getType("integer"), "notnull" => true));
81
        $tempTable = $platform->getTemporaryTableName("my_temporary");
82

83
        $createTempTableSQL = $platform->getCreateTemporaryTableSnippetSQL() . ' ' . $tempTable . ' ('
84 85
                . $platform->getColumnDeclarationListSQL($columnDefinitions) . ')';

86
        $table = new Table("nontemporary");
87
        $table->addColumn("id", "integer");
88
        $table->setPrimaryKey(array('id'));
89

90 91 92
        foreach ($platform->getCreateTableSQL($table) AS $sql) {
            $this->_conn->executeQuery($sql);
        }
93

94
        $this->_conn->beginTransaction();
95
        $this->_conn->insert("nontemporary", array("id" => 1));
96

97 98
        $this->_conn->exec($createTempTableSQL);
        $this->_conn->insert("nontemporary", array("id" => 2));
99 100 101 102 103 104

        $this->_conn->rollback();

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

106 107
        }

108 109
        $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.");
110 111
    }
}