Completed
Push — master ( 9ab6e9...542fab )
by Restu
14:55
created

Database::whereQ()   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 $table
114
     * @return $this
115
     */
116
    public function table($table)
117
    {
118
        $this->query->setTable($table);
119
        return $this;
120
    }
121
122
    /**
123
     * @param null $columns
124
     * @return $this
125
     */
126
    public function select($columns = null)
127
    {
128
        $this->query->select($columns);
129
        return $this;
130
    }
131
132
    /**
133
     * @param $query
134
     * @param string $type
135
     * @return Query
136
     */
137
    public function whereQ($query, $type = "AND")
138
    {
139
        $this->query->whereQ($query, $type);
140
        return $this;
141
    }
142
143
    /**
144
     * @param $column
145
     * @param $value
146
     * @param string $operator
147
     * @param string $type
148
     * @return $this
149
     */
150
    public function where($column, $value, $operator = "=", $type = "AND")
151
    {
152
        $this->query->where($column, $value, $operator, $type);
153
        return $this;
154
    }
155
156
    /**
157
     * @param $column
158
     * @param $value
159
     * @param string $operator
160
     * @return $this
161
     */
162
    public function andWhere($column, $value, $operator = "=")
163
    {
164
        $this->query->andWhere($column, $value, $operator);
165
        return $this;
166
    }
167
168
    /**
169
     * @param $column
170
     * @param $value
171
     * @param string $operator
172
     * @return $this
173
     */
174
    public function orWhere($column, $value, $operator = "=")
175
    {
176
        $this->query->orWhere($column, $value, $operator);
177
        return $this;
178
    }
179
180
    /**
181
     * @return $this
182
     * @throws \Exception
183
     */
184
    public function execute()
185
    {
186
        $qArr = $this->query->build($this->grammar);
187
188
        $this->statement = $this->pdo->prepare($qArr[0]);
189
190
        if (!$this->statement->execute($qArr[1])) {
191
            throw new \Exception(join(". ", $this->statement->errorInfo()));
192
        }
193
194
        return $this;
195
    }
196
197
    /**
198
     * @return array
199
     * @throws \Exception
200
     */
201
    public function all()
202
    {
203
        if (!$this->statement) {
204
            $this->execute();
205
        }
206
        return $this->statement->fetchAll();
207
    }
208
209
    /**
210
     * @return mixed
211
     * @throws \Exception
212
     */
213
    public function get()
214
    {
215
        if (!$this->statement) {
216
            $this->execute();
217
        }
218
        return $this->statement->fetch();
219
    }
220
}
221