-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquerybuilder.php
88 lines (69 loc) · 2.02 KB
/
querybuilder.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?php
class QueryBuilder {
private $select;
private $from;
private $where = [];
// id > 1 AND name LIKE %doe% AND age > 18
private $limit;
private $orderBy;
function select($fields) {
$this->select = $fields;
return $this;
}
function from($table) {
$this->from = $table;
return $this;
}
function where($field, $operator, $value) {
$this->where[] = "{$field} {$operator} '{$value}'"; // id > 1
return $this;
}
function limit($limit) {
$this->limit = $limit;
return $this;
}
function orderBy($field, $order = 'ASC') {
$this->orderBy = "{$field} {$order}";
return $this;
}
function get() {
$query = "";
$query .= "SELECT {$this->select} ";
$query .= "FROM {$this->from} ";
if ($this->where) {
$conditions = implode(' AND ', $this->where);
$query .= "WHERE {$conditions} ";
}
if ($this->orderBy) {
$query .= "ORDER BY {$this->orderBy} ";
}
if ($this->limit) {
$query .= "LIMIT {$this->limit} ";
}
return $query;
}
}
// $qb = new QueryBuilder;
// // $query = $qb->select('name, roll, email')->from('users')->where('id','>','1')->where('name','LIKE','%doe%')->limit(10)->get();
// $query = $qb->select('name, roll, email')
// ->from('users')
// ->where('id', '>', '1')
// ->where('name', 'LIKE', '%doe%')
// ->orderBy('name', 'DESC')
// ->limit(10)->get();
// echo $query;
$connection = new PDO('mysql:host=127.0.0.1;dbname=Chinook', 'root', '');
$qb = new QueryBuilder;
$query = $qb->select('*')
->limit(10)
->orderBy('Title', 'DESC')
->where('id', '>', '1')
->from('Albums')
// ->where('Title', 'LIKE', 'U%')
->get();
echo $query;
$statement = $connection->prepare($query);
$statement->execute();
$result = $statement->fetchAll(PDO::FETCH_OBJ);
print_r($result);
//SELECT name FROM users WHERE id > 1 AND name LIKE '%doe%' LIMIT 10