Completed
Push — master ( f39055...5439cc )
by Restu
12:12
created

Database::clearUsingModel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
1
<?php
2
namespace JayaCode\Framework\Core\Database;
3
4
use JayaCode\Framework\Core\Database\Connector\Connector;
5
use JayaCode\Framework\Core\Database\Connector\ConnectorMySql;
6
use JayaCode\Framework\Core\Database\Query\Grammar\GrammarMySql;
7
use JayaCode\Framework\Core\Database\Query\Query;
8
use PDO;
9
10
/**
11
 * Class Database
12
 * @package JayaCode\Framework\Core\Database
13
 */
14
class Database
15
{
16
    /**
17
     * @var Connector
18
     */
19
    protected $connector;
20
21
    /**
22
     * @var \PDO
23
     */
24
    protected $pdo;
25
26
    /**
27
     * @var \PDOStatement
28
     */
29
    protected $statement;
30
31
    /**
32
     * @var Query
33
     */
34
    protected $query;
35
36
    /**
37
     * @var
38
     */
39
    protected $grammar;
40
41
    /**
42
     * @var array
43
     */
44
    protected $driver = [
45
        "mysql" => [
46
            "connector" => ConnectorMySql::class,
47
            "grammar" => GrammarMySql::class,
48
        ]
49
    ];
50
51
    /**
52
     * @var array
53
     */
54
    protected $config = [
55
        "driver" => "mysql",
56
57
        "host" => "",
58
        "username" => "",
59
        "password" => "",
60
61
        "dbname" => "",
62
        "options" => [
63
            PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
64
            PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
65
        ]
66
    ];
67
68
    /**
69
     * @var string
70
     */
71
    protected $model = null;
72
73
    /**
74
     * @param $config
75
     */
76
    public function __construct($config)
77
    {
78
        $this->config = array_merge($this->config, $config);
79
80
        $this->initialize();
81
82
        $this->createConnection();
83
    }
84
85
    /**
86
     * @param $config
87
     * @return static
88
     */
89
    public static function create($config)
90
    {
91
        return new static($config);
92
    }
93
94
    /**
95
     *
96
     */
97
    protected function initialize()
98
    {
99
        $connectorClass = $this->driver[$this->config["driver"]]["connector"];
100
        $this->connector = new $connectorClass();
101
102
        $grammarClass = $this->driver[$this->config["driver"]]["grammar"];
103
        $this->grammar = new $grammarClass();
104
105
        $this->query = new Query();
106
    }
107
108
    /**
109
     * @return PDO
110
     */
111
    public function createConnection()
112
    {
113
        $this->pdo = $this->connector->connect($this->config);
114
        return $this->pdo;
115
    }
116
117
    /**
118
     * @param $query
119
     * @param null $params
120
     * @return $this
121
     */
122
    public function sql($query, $params = null)
123
    {
124
        $this->query = $this->query->sql($query, $params);
125
        return $this;
126
    }
127
128
    /**
129
     * @param $table
130
     * @return $this
131
     */
132
    public function table($table)
133
    {
134
        $this->query->setTable($table);
135
        return $this;
136
    }
137
138
    /**
139
     * @param null $columns
140
     * @return $this
141
     */
142
    public function select($columns = null)
143
    {
144
        $this->query->select($columns);
145
        return $this;
146
    }
147
148
    /**
149
     * @param $query
150
     * @param string $type
151
     * @return Query
152
     */
153
    public function whereQ($query, $type = "AND")
154
    {
155
        $this->query->whereQ($query, $type);
156
        return $this;
157
    }
158
159
    /**
160
     * @param $column
161
     * @param $value
162
     * @param string $operator
163
     * @param string $type
164
     * @return $this
165
     */
166
    public function where($column, $value, $operator = "=", $type = "AND")
167
    {
168
        $this->query->where($column, $value, $operator, $type);
169
        return $this;
170
    }
171
172
    /**
173
     * @param $column
174
     * @param $value
175
     * @param string $operator
176
     * @return $this
177
     */
178
    public function andWhere($column, $value, $operator = "=")
179
    {
180
        $this->query->andWhere($column, $value, $operator);
181
        return $this;
182
    }
183
184
    /**
185
     * @param $column
186
     * @param $value
187
     * @param string $operator
188
     * @return $this
189
     */
190
    public function orWhere($column, $value, $operator = "=")
191
    {
192
        $this->query->orWhere($column, $value, $operator);
193
        return $this;
194
    }
195
196
    /**
197
     * @param $column
198
     * @param $value
199
     * @param string $type
200
     * @return $this
201
     */
202
    public function like($column, $value, $type = "AND")
203
    {
204
        $this->query->like($column, $value, $type);
205
        return $this;
206
    }
207
208
    /**
209
     * @param $column
210
     * @param $value
211
     * @return $this
212
     */
213
    public function andLike($column, $value)
214
    {
215
        $this->query->andLike($column, $value);
216
        return $this;
217
    }
218
219
    /**
220
     * @param $column
221
     * @param $value
222
     * @return $this
223
     */
224
    public function orLike($column, $value)
225
    {
226
        $this->query->orLike($column, $value);
227
        return $this;
228
    }
229
230
    /**
231
     * @param $column
232
     * @param $value
233
     * @param string $type
234
     * @return $this
235
     */
236
    public function between($column, $value, $type = "AND")
237
    {
238
        $this->query->between($column, $value, $type);
239
        return $this;
240
    }
241
242
    /**
243
     * @param $column
244
     * @param $value
245
     * @return $this
246
     */
247
    public function andBetween($column, $value)
248
    {
249
        $this->query->andBetween($column, $value);
250
        return $this;
251
    }
252
253
    /**
254
     * @param $column
255
     * @param $value
256
     * @return $this
257
     */
258
    public function orBetween($column, $value)
259
    {
260
        $this->query->orBetween($column, $value);
261
        return $this;
262
    }
263
264
    /**
265
     * @return $this
266
     * @throws \Exception
267
     */
268
    public function execute()
269
    {
270
        $qArr = $this->query->build($this->grammar);
271
272
        $this->statement = $this->pdo->prepare($qArr[0]);
273
274
        if (!$this->statement->execute($qArr[1])) {
275
            throw new \Exception(join(". ", $this->statement->errorInfo()));
276
        }
277
278
        return $this;
279
    }
280
281
    /**
282
     * @return array
283
     * @throws \Exception
284
     */
285
    public function all()
286
    {
287
        $this->executeIfNotAlreadyExecutedPreviously();
288
289
        if ($this->model) {
290
            $dataModel = array();
291
            while ($model = $this->get()) {
292
                $dataModel[] = $model;
293
            }
294
295
            $this->clear();
296
297
            return $dataModel;
298
        }
299
        return $this->statement->fetchAll();
300
    }
301
302
    /**
303
     * @return mixed
304
     */
305
    public function first()
306
    {
307
        // TODO: use limit after query builder limit finish
308
        $data = $this->get();
309
        $this->clear();
310
        return $data;
311
    }
312
313
    /**
314
     * @return mixed
315
     * @throws \Exception
316
     */
317
    protected function get()
318
    {
319
        $this->executeIfNotAlreadyExecutedPreviously();
320
321
        if ($this->model) {
322
            $data = $this->statement->fetch();
323
            return $data?new $this->model($data, false):$data;
324
        }
325
326
        return $this->statement->fetch();
327
    }
328
329
    /**
330
     * @throws \Exception
331
     */
332
    protected function executeIfNotAlreadyExecutedPreviously()
333
    {
334
        if (!$this->statement) {
335
            $this->execute();
336
        }
337
    }
338
339
    /**
340
     * Clear statement PDO and query builder
341
     */
342
    public function clear()
343
    {
344
        $this->statement = null;
345
        $this->clearUsingModel();
346
347
        $this->query->clear();
348
    }
349
350
    /**
351
     *
352
     */
353
    public function clearUsingModel()
354
    {
355
        $this->model = null;
356
    }
357
358
    /**
359
     * @return string
360
     */
361
    public function getModel()
362
    {
363
        return $this->model;
364
    }
365
366
    /**
367
     * @param string $model
368
     * @param null $table
369
     * @throws \Exception
370
     */
371
    public function setModel($model, $table = null)
372
    {
373
        if (!class_exists($model)) {
374
            throw new \Exception("class {$model} is not exist");
375
        }
376
377
        $this->clear();
378
379
        if ($table) {
380
            $this->table($table);
381
        }
382
        $this->model = $model;
383
    }
384
}
385