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