Passed
Push — master ( add693...b324e9 )
by Wilmer
13:51
created

PDO::getAttribute()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 3
nop 1
dl 0
loc 10
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Mssql\Pdo;
6
7
/**
8
 * This is an extension of the default PDO class of MSSQL and DBLIB drivers. It provides workarounds for improperly
9
 * implemented functionalities of the MSSQL and DBLIB drivers.
10
 */
11
final class PDO extends \PDO
12
{
13
    /**
14
     * Returns value of the last inserted ID.
15
     *
16
     * @param string|null $sequence the sequence name. Defaults to null.
17
     *
18
     * @return int last inserted ID value.
19
     */
20
    public function lastInsertId($sequence = null): int
21
    {
22
        return $this->query('SELECT CAST(COALESCE(SCOPE_IDENTITY(), @@IDENTITY) AS bigint)')->fetchColumn();
23
    }
24
25
    /**
26
     * Starts a transaction. It is necessary to override PDO's method as MSSQL PDO driver does not natively support
27
     * transactions.
28
     *
29
     * @return bool the result of a transaction start.
30
     */
31
    public function beginTransaction(): bool
32
    {
33
        $this->exec('BEGIN TRANSACTION');
34
35
        return true;
36
    }
37
38
    /**
39
     * Commits a transaction. It is necessary to override PDO's method as MSSQL PDO driver does not natively support
40
     * transactions.
41
     *
42
     * @return bool the result of a transaction commit.
43
     */
44
    public function commit(): bool
45
    {
46
        $this->exec('COMMIT TRANSACTION');
47
48
        return true;
49
    }
50
51
    /**
52
     * Rollbacks a transaction. It is necessary to override PDO's method as MSSQL PDO driver does not natively support
53
     * transactions.
54
     *
55
     * @return bool the result of a transaction roll back.
56
     */
57
    public function rollBack(): bool
58
    {
59
        $this->exec('ROLLBACK TRANSACTION');
60
61
        return true;
62
    }
63
64
    /**
65
     * Retrieve a database connection attribute.
66
     *
67
     * It is necessary to override PDO's method as some MSSQL PDO driver (e.g. dblib) does not
68
     * support getting attributes.
69
     *
70
     * @param int $attribute One of the PDO::ATTR_* constants.
71
     *
72
     * @return mixed A successful call returns the value of the requested PDO attribute.
73
     * An unsuccessful call returns null.
74
     */
75
    public function getAttribute($attribute)
76
    {
77
        try {
78
            return parent::getAttribute($attribute);
79
        } catch (\PDOException $e) {
80
            switch ($attribute) {
81
                case self::ATTR_SERVER_VERSION:
82
                    return $this->query("SELECT CAST(SERVERPROPERTY('productversion') AS VARCHAR)")->fetchColumn();
83
                default:
84
                    throw $e;
85
            }
86
        }
87
    }
88
}
89