Completed
Push — master ( 542fab...77f05a )
by Restu
11:03
created

Database::andLike()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 3
c 1
b 0
f 1
nc 1
nop 2
dl 0
loc 5
rs 9.4285
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
     * @param $config
70
     */
71
    public function __construct($config)
72
    {
73
        $this->config = array_merge($this->config, $config);
74
75
        $this->initialize();
76
77
        $this->createConnection();
78
    }
79
80
    /**
81
     * @param $config
82
     * @return static
83
     */
84
    public static function create($config)
85
    {
86
        return new static($config);
87
    }
88
89
    /**
90
     *
91
     */
92
    protected function initialize()
93
    {
94
        $connectorClass = $this->driver[$this->config["driver"]]["connector"];
95
        $this->connector = new $connectorClass();
96
97
        $grammarClass = $this->driver[$this->config["driver"]]["grammar"];
98
        $this->grammar = new $grammarClass();
99
100
        $this->query = new Query();
101
    }
102
103
    /**
104
     * @return PDO
105
     */
106
    public function createConnection()
107
    {
108
        $this->pdo = $this->connector->connect($this->config);
109
        return $this->pdo;
110
    }
111
112
    /**
113
     * @param $query
114
     * @param null $params
115
     * @return $this
116
     */
117
    public function sql($query, $params = null)
118
    {
119
        $this->query = $this->query->sql($query, $params);
120
        return $this;
121
    }
122
123
    /**
124
     * @param $table
125
     * @return $this
126
     */
127
    public function table($table)
128
    {
129
        $this->query->setTable($table);
130
        return $this;
131
    }
132
133
    /**
134
     * @param null $columns
135
     * @return $this
136
     */
137
    public function select($columns = null)
138
    {
139
        $this->query->select($columns);
140
        return $this;
141
    }
142
143
    /**
144
     * @param $query
145
     * @param string $type
146
     * @return Query
147
     */
148
    public function whereQ($query, $type = "AND")
149
    {
150
        $this->query->whereQ($query, $type);
151
        return $this;
152
    }
153
154
    /**
155
     * @param $column
156
     * @param $value
157
     * @param string $operator
158
     * @param string $type
159
     * @return $this
160
     */
161
    public function where($column, $value, $operator = "=", $type = "AND")
162
    {
163
        $this->query->where($column, $value, $operator, $type);
164
        return $this;
165
    }
166
167
    /**
168
     * @param $column
169
     * @param $value
170
     * @param string $operator
171
     * @return $this
172
     */
173
    public function andWhere($column, $value, $operator = "=")
174
    {
175
        $this->query->andWhere($column, $value, $operator);
176
        return $this;
177
    }
178
179
    /**
180
     * @param $column
181
     * @param $value
182
     * @param string $operator
183
     * @return $this
184
     */
185
    public function orWhere($column, $value, $operator = "=")
186
    {
187
        $this->query->orWhere($column, $value, $operator);
188
        return $this;
189
    }
190
191
    /**
192
     * @param $column
193
     * @param $value
194
     * @param string $type
195
     * @return $this
196
     */
197
    public function like($column, $value, $type = "AND")
198
    {
199
        $this->query->like($column, $value, $type);
200
        return $this;
201
    }
202
203
    /**
204
     * @param $column
205
     * @param $value
206
     * @return $this
207
     */
208
    public function andLike($column, $value)
209
    {
210
        $this->query->andLike($column, $value);
211
        return $this;
212
    }
213
214
    /**
215
     * @param $column
216
     * @param $value
217
     * @return $this
218
     */
219
    public function orLike($column, $value)
220
    {
221
        $this->query->orLike($column, $value);
222
        return $this;
223
    }
224
225
    /**
226
     * @return $this
227
     * @throws \Exception
228
     */
229
    public function execute()
230
    {
231
        $qArr = $this->query->build($this->grammar);
232
233
        $this->statement = $this->pdo->prepare($qArr[0]);
234
235
        if (!$this->statement->execute($qArr[1])) {
236
            throw new \Exception(join(". ", $this->statement->errorInfo()));
237
        }
238
239
        return $this;
240
    }
241
242
    /**
243
     * @return array
244
     * @throws \Exception
245
     */
246
    public function all()
247
    {
248
        if (!$this->statement) {
249
            $this->execute();
250
        }
251
        return $this->statement->fetchAll();
252
    }
253
254
    /**
255
     * @return mixed
256
     * @throws \Exception
257
     */
258
    public function get()
259
    {
260
        if (!$this->statement) {
261
            $this->execute();
262
        }
263
        return $this->statement->fetch();
264
    }
265
}
266