Passed
Pull Request — master (#14)
by Wilmer
25:49 queued 10:47
created

PDO   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 67
rs 10
c 0
b 0
f 0
wmc 7

5 Methods

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