Passed
Push — fix-array-access ( 050ccc...ecf3a7 )
by Alexander
56:12 queued 49:24
created

DBLibPDO::lastInsertId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
rs 10
c 1
b 0
f 0
1
<?php
2
/**
3
 * @link http://www.yiiframework.com/
4
 * @copyright Copyright (c) 2008 Yii Software LLC
5
 * @license http://www.yiiframework.com/license/
6
 */
7
8
namespace yii\db\mssql;
9
10
/**
11
 * This is an extension of the default PDO class of DBLIB drivers.
12
 * It provides workarounds for improperly implemented functionalities of the DBLIB drivers.
13
 *
14
 * @author Bert Brunekreeft <[email protected]>
15
 * @since 2.0
16
 */
17
class DBLibPDO extends \PDO
18
{
19
    /**
20
     * Returns value of the last inserted ID.
21
     * @param string|null $sequence the sequence name. Defaults to null.
22
     * @return int last inserted ID value.
23
     */
24
    public function lastInsertId($sequence = null)
25
    {
26
        return $this->query('SELECT CAST(COALESCE(SCOPE_IDENTITY(), @@IDENTITY) AS bigint)')->fetchColumn();
27
    }
28
29
    /**
30
     * Retrieve a database connection attribute.
31
     *
32
     * It is necessary to override PDO's method as some MSSQL PDO driver (e.g. dblib) does not
33
     * support getting attributes.
34
     * @param int $attribute One of the PDO::ATTR_* constants.
35
     * @return mixed A successful call returns the value of the requested PDO attribute.
36
     * An unsuccessful call returns null.
37
     */
38
    public function getAttribute($attribute)
39
    {
40
        try {
41
            return parent::getAttribute($attribute);
42
        } catch (\PDOException $e) {
43
            switch ($attribute) {
44
                case self::ATTR_SERVER_VERSION:
45
                    return $this->query("SELECT CAST(SERVERPROPERTY('productversion') AS VARCHAR)")->fetchColumn();
46
                default:
47
                    throw $e;
48
            }
49
        }
50
    }
51
}
52