AbstractPdo::getTables()
last analyzed

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
nc 1
dl 0
loc 1
c 0
b 0
f 0
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 Psr\Log\LoggerInterface;
15
use \PDO;
16
use function Terah\Assert\Assert;
17
use Psr\Log\LoggerAwareTrait;
18
use Terah\FluentPdoModel\Column;
19
use Terah\FluentPdoModel\ForeignKey;
20
use Terah\RedisCache\RedisCacheTrait;
21
/**
22
 * Class AbstractPdo
23
 *
24
 * @package Terah\FluentPdoModel\Drivers
25
 * @author  Terry Cullen - [email protected]
26
 */
27
abstract class AbstractPdo extends PDO implements DriverInterface
28
{
29
    use RedisCacheTrait;
30
    use LoggerAwareTrait;
31
32
    /** @var array  */
33
    protected $_config              = [];
34
35
    /** @var int  */
36
    protected $_transactionCount    = 0;
37
38
    /** @var bool  */
39
    protected $_supportsColumnMeta  = false;
40
41
    /**
42
     * @return LoggerInterface
43
     */
44
    public function getLogger() : LoggerInterface
45
    {
46
        return $this->logger;
47
    }
48
49
    /**
50
     * @param array  $config
51
     * @param string $dsn
52
     * @return AbstractPdo
53
     */
54
    public function setConfig(array $config=[], string $dsn='') : AbstractPdo
55
    {
56
        if ( $dsn )
57
        {
58
            $driver                 = strpos($dsn, ':') !== false ? substr($dsn, 0, strpos($dsn, ':')) : $dsn;
59
            $driver                 = ucfirst(strtolower($driver));
60
            $dsn                    = preg_replace("/^{$driver}:/i", '', $dsn);
61
            $parts                  = explode(';', $dsn);
62
            foreach ( $parts as $conf )
63
            {
64
                $conf                   = explode('=', $conf);
65
                $config[$conf[0]]       = isset($conf[1]) ? $conf[1] : true;
66
            }
67
            $config['driver']       = $driver;
68
        }
69
        $this->_config          = $config;
70
71
        return $this;
72
    }
73
74
    /**
75
     * @param string $key
76
     * @return mixed
77
     */
78
    public function getConfig(string $key)
79
    {
80
        return isset($this->_config[$key]) ? $this->_config[$key] : null;
81
    }
82
83
    /**
84
     * @return array
85
     */
86
    public function getAllConfig() : array
87
    {
88
        return $this->_config;
89
    }
90
91
    /**
92
     * @return bool
93
     */
94
    public function logQueries() : bool
95
    {
96
        return !empty( $this->_config['log_queries'] ) && $this->_config['log_queries'] ? true : false;
97
    }
98
99
    /**
100
     * @return int
101
     */
102
    public function getTransactionDepth() : int
103
    {
104
        return $this->_transactionCount;
105
    }
106
107
    /**
108
     * @return bool
109
     */
110
    public function beginTransaction() : bool
111
    {
112
        $this->_transactionCount++;
113
        if ( $this->_transactionCount === 1 )
114
        {
115
            return parent::beginTransaction();
116
        }
117
118
        return true;
119
    }
120
121
    /**
122
     * @return bool
123
     */
124
    public function commit() : bool
125
    {
126
        $this->_transactionCount--;
127
        $this->_transactionCount = $this->_transactionCount < 0 ? 0 : $this->_transactionCount;
128
        if ( $this->_transactionCount === 0 )
129
        {
130
            return parent::commit();
131
        }
132
133
        return true;
134
    }
135
136
    /**
137
     * @return bool
138
     */
139
    public function rollback() : bool
140
    {
141
        if ( $this->_transactionCount > 0 )
142
        {
143
            $this->_transactionCount = 0;
144
145
            return parent::rollBack();
146
        }
147
148
        return true;
149
    }
150
151
    /**
152
     * @param string $query
153
     * @param integer $limit
154
     * @param integer $offset
155
     * @return string
156
     */
157
    public function setLimit(string $query, int $limit=0, int $offset=0) : string
158
    {
159
        Assert($query)->string()->notEmpty();
160
        Assert($limit)->unsignedInt();
161
        Assert($offset)->unsignedInt();
162
        if ( $limit )
163
        {
164
            $query .= " LIMIT {$limit}";
165
        }
166
        if ( $offset )
167
        {
168
            $query .= " OFFSET {$offset}";
169
        }
170
171
        return $query;
172
    }
173
174
    /**
175
     * @return bool
176
     */
177
    public function supportsColumnMeta() : bool
178
    {
179
        return $this->_supportsColumnMeta;
180
    }
181
182
    /**
183
     * @param string $table
184
     * @param string $column
185
     * @return string
186
     */
187
    abstract public function getFieldComment(string $table, string $column) : string;
188
189
    /**
190
     * @param bool $include_views
191
     * @return array
192
     */
193
    abstract public function getTables(bool $include_views=false) : array;
194
195
    /**
196
     * @param bool $include_views
197
     * @param string $table
198
     * @return Column[][]
199
     */
200
    abstract public function getColumns(bool $include_views=false, string $table='') : array;
201
202
    /**
203
     * @param string $table
204
     * @return ForeignKey[][]
205
     */
206
    abstract public function getForeignKeys(string $table='') : array;
207
208
209
    /**
210
     * @param bool $include_views
211
     * @param string $table
212
     * @return array
213
     */
214
    abstract public function getTableCounts(bool $include_views=false, string $table='') : array;
215
}