OciPdo   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 99
rs 10
c 0
b 0
f 0
wmc 14
lcom 0
cbo 4

7 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 15 5
A getTables() 0 4 1
A getColumns() 0 4 1
A getForeignKeys() 0 4 1
A getTableCounts() 0 4 1
A getFieldComment() 0 4 1
A setLimit() 0 18 4
1
<?php declare(strict_types=1);
2
/**
3
 * @package Terah\FluentPdoModel
4
 *
5
 * Licensed under The MIT License
6
 * For full copyright and license information, please see the LICENSE.txt
7
 * Redistributions of files must retain the above copyright notice.
8
 *
9
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
10
 */
11
12
namespace Terah\FluentPdoModel\Drivers;
13
14
use \PDO;
15
use Psr\Log\LoggerInterface;
16
use Psr\Log\NullLogger;
17
use Terah\FluentPdoModel\Column;
18
use Terah\FluentPdoModel\ForeignKey;
19
use Terah\RedisCache\CacheInterface;
20
use Terah\RedisCache\NullCache;
21
use function Terah\Assert\Assert;
22
23
class OciPdo extends AbstractPdo implements DriverInterface
24
{
25
    /**
26
     * @param string          $dsn
27
     * @param string          $username
28
     * @param string          $password
29
     * @param array           $options
30
     * @param LoggerInterface $logger
31
     * @param CacheInterface   $cache
32
     */
33
    public function __construct(string $dsn, string $username='', string $password='', array $options=[], LoggerInterface $logger=null, CacheInterface $cache=null)
34
    {
35
        parent::__construct($dsn, $username, $password, $options);
36
        $this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
37
        $this->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
38
        $this->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
39
        $this->setAttribute(PDO::ATTR_CASE, PDO::CASE_LOWER);
40
        if ( array_key_exists('timeout', $options) && $options['timeout'] )
41
        {
42
            $this->setAttribute(PDO::ATTR_TIMEOUT, (int)$options['timeout']);
43
        }
44
        $this->setConfig($options, $dsn);
45
        $this->setLogger($logger ? $logger : new NullLogger());
46
        $this->setCache($cache ? $cache : new NullCache());
47
    }
48
49
    /**
50
     * @param bool $include_views
51
     * @return array
52
     */
53
    public function getTables(bool $include_views=false) : array
54
    {
55
        return [];
56
    }
57
58
    /**
59
     * @param bool $include_views
60
     * @param string $table
61
     * @return Column[]
62
     */
63
    public function getColumns(bool $include_views=false, string $table='') : array
64
    {
65
        return [];
66
    }
67
68
    /**
69
     * @param string $table
70
     * @return ForeignKey[]
71
     */
72
    public function getForeignKeys(string $table='') : array
73
    {
74
        return [];
75
    }
76
77
    /**
78
     * @param bool|false $include_views
79
     * @param string $table
80
     * @return array
81
     */
82
    public function getTableCounts(bool $include_views=false, string $table='') : array
83
    {
84
        return [];
85
    }
86
87
    /**
88
     * @param string $table
89
     * @param string $column
90
     * @return string
91
     */
92
    public function getFieldComment(string $table, string $column) : string
93
    {
94
        return '';
95
    }
96
97
    /**
98
     * @param string $query
99
     * @param integer $limit
100
     * @param null|integer $offset
101
     * @return string
102
     */
103
    public function setLimit(string $query, int $limit=0, int $offset=0) : string
104
    {
105
        Assert($query)->string()->notEmpty();
106
        Assert($limit)->nullOr()->integer();
107
        Assert($offset)->nullOr()->integer();
108
        if ( $offset )
109
        {
110
            $limit  = $limit ?: 1;
111
            $limit  = $limit + $offset;
112
            return "SELECT * FROM ({$query}) WHERE ROWNUM between {$offset} AND {$limit}";
113
        }
114
        if ( $limit  )
115
        {
116
            return "SELECT * FROM ({$query}) WHERE ROWNUM <= {$limit}";
117
        }
118
119
        return $query;
120
    }
121
}
122