Passed
Pull Request — master (#51)
by Arman
04:26
created

IdiormDbal::deleteAll()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
/**
4
 * Quantum PHP Framework
5
 *
6
 * An open source software development framework for PHP
7
 *
8
 * @package Quantum
9
 * @author Arman Ag. <[email protected]>
10
 * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org)
11
 * @link http://quantum.softberg.org/
12
 * @since 2.6.0
13
 */
14
15
namespace Quantum\Libraries\Database\Idiorm;
16
17
use Quantum\Libraries\Database\Idiorm\Statements\Criteria;
18
use Quantum\Libraries\Database\Idiorm\Statements\Reducer;
19
use Quantum\Libraries\Database\Idiorm\Statements\Result;
20
use Quantum\Libraries\Database\Idiorm\Statements\Model;
21
use Quantum\Libraries\Database\Idiorm\Statements\Query;
22
use Quantum\Libraries\Database\Idiorm\Statements\Join;
23
use Quantum\Libraries\Database\DbalInterface;
24
use Quantum\Exceptions\DatabaseException;
25
use PDO;
26
use ORM;
27
28
29
/**
30
 * Class IdiormDbal
31
 * @package Quantum\Libraries\Database
32
 */
33
class IdiormDbal implements DbalInterface, RelationalInterface
34
{
35
36
    use Model;
37
    use Result;
38
    use Criteria;
39
    use Reducer;
40
    use Join;
41
    use Query;
42
43
    /**
44
     * Type array
45
     */
46
    const TYPE_ARRAY = 1;
47
48
    /**
49
     * Type object
50
     */
51
    const TYPE_OBJECT = 2;
52
53
    /**
54
     * Default charset
55
     */
56
    const DEFAULT_CHARSET = 'utf8';
57
58
    /**
59
     * The database table associated with model
60
     * @var string
61
     */
62
    private $table;
63
64
    /**
65
     * Id column of table
66
     * @var string
67
     */
68
    private $idColumn;
69
70
    /**
71
     * Foreign keys
72
     * @var array
73
     */
74
    private $foreignKeys = [];
75
76
    /**
77
     * Idiorm Patch object
78
     * @var \Quantum\Libraries\Database\Idiorm\IdiormPatch
79
     */
80
    private $ormPatch = null;
0 ignored issues
show
introduced by
The private property $ormPatch is not used, and could be removed.
Loading history...
81
82
    /**
83
     * ORM Model
84
     * @var object
85
     */
86
    private $ormModel;
87
88
    /**
89
     * Active connection
90
     * @var array|null
91
     */
92
    private static $connection = null;
93
94
    /**
95
     * Operators map
96
     * @var string[]
97
     */
98
    private $operators = [
0 ignored issues
show
introduced by
The private property $operators is not used, and could be removed.
Loading history...
99
        '=' => 'where_equal',
100
        '!=' => 'where_not_equal',
101
        '>' => 'where_gt',
102
        '>=' => 'where_gte',
103
        '<' => 'where_lt',
104
        '<=' => 'where_lte',
105
        'IN' => 'where_in',
106
        'NOT IN' => 'where_not_in',
107
        'LIKE' => 'where_like',
108
        'NOT LIKE' => 'where_not_like',
109
        'NULL' => 'where_null',
110
        'NOT NULL' => 'where_not_null',
111
        '#=#' => null,
112
    ];
113
114
    /**
115
     * ORM Class
116
     * @var string
117
     */
118
    private static $ormClass = ORM::class;
119
120
    /**
121
     * Class constructor
122
     * @param string $table
123
     * @param string $idColumn
124
     * @param array $foreignKeys
125
     */
126
    public function __construct(string $table, string $idColumn = 'id', array $foreignKeys = [])
127
    {
128
        $this->table = $table;
129
        $this->idColumn = $idColumn;
130
        $this->foreignKeys = $foreignKeys;
131
    }
132
133
    /**
134
     * @inheritDoc
135
     */
136
    public static function connect(array $config)
137
    {
138
        $configuration = [
139
            'connection_string' => self::buildConnectionString($config),
140
            'logging' => config()->get('debug', false),
141
            'error_mode' => PDO::ERRMODE_EXCEPTION,
142
        ];
143
144
        if ($config['driver'] == 'mysql' || $config['driver'] == 'pgsql') {
145
            $configuration = array_merge($configuration, [
146
                'username' => $config['username'] ?? null,
147
                'password' => $config['password'] ?? null,
148
                'driver_options' => [
149
                    PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES ' . ($config['charset'] ?? self::DEFAULT_CHARSET)
150
                ]
151
            ]);
152
153
        }
154
155
        (self::$ormClass)::configure($configuration);
156
157
        self::$connection = (self::$ormClass)::get_config();
158
    }
159
160
    /**
161
     * @inheritDoc
162
     */
163
    public static function getConnection(): ?array
164
    {
165
        return self::$connection;
166
    }
167
168
    /**
169
     * @inheritDoc
170
     */
171
    public static function disconnect()
172
    {
173
        self::$connection = null;
174
        (self::$ormClass)::reset_db();
175
    }
176
177
    /**
178
     * @inheritDoc
179
     */
180
    public function getTable(): string
181
    {
182
        return $this->table;
183
    }
184
185
    /**
186
     * Gets the ORM model
187
     * @return \ORM
188
     * @throws \Quantum\Exceptions\DatabaseException
189
     */
190
    public function getOrmModel(): ORM
191
    {
192
        if (!$this->ormModel) {
193
            if (!self::getConnection()) {
194
                throw DatabaseException::missingConfig();
195
            }
196
197
            $this->ormModel = (self::$ormClass)::for_table($this->table)->use_id_column($this->idColumn);
198
        }
199
200
        return $this->ormModel;
201
    }
202
203
    /**
204
     * @param \ORM $ormModel
205
     */
206
    protected function updateOrmModel(ORM $ormModel)
207
    {
208
        $this->ormModel = $ormModel;
209
    }
210
211
    /**
212
     * Builds connection string
213
     * @param array $connectionDetails
214
     * @return string
215
     */
216
    protected static function buildConnectionString(array $connectionDetails): string
217
    {
218
        $connectionString = $connectionDetails['driver'] . ':';
219
220
        switch ($connectionDetails['driver']) {
221
            case 'sqlite':
222
                $connectionString .= $connectionDetails['database'];
223
                break;
224
            case 'mysql':
225
            case 'pgsql':
226
                $connectionString .= 'host=' . $connectionDetails['host'] . ';';
227
228
                if (isset($connectionDetails['port'])) {
229
                    $connectionString .= 'post=' . $connectionDetails['port'] . ';';
230
                }
231
232
                $connectionString .= 'dbname=' . $connectionDetails['dbname'] . ';';
233
234
                if (isset($connectionDetails['charset'])) {
235
                    $connectionString .= 'charset=' . $connectionDetails['charset'] . ';';
236
                }
237
                break;
238
        }
239
240
        return $connectionString;
241
    }
242
243
}
244