-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuilderContainer.php
More file actions
103 lines (89 loc) · 2.37 KB
/
Copy pathBuilderContainer.php
File metadata and controls
103 lines (89 loc) · 2.37 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php
require_once __DIR__ . '/vendor/autoload.php';
use NilPortugues\Sql\QueryBuilder\Builder\GenericBuilder;
class BuilderContainer{
private $builder;
private $query;
private $modelObject;
private $tableName;
public function __construct($modelObject, $tableName){
$this->builder = new GenericBuilder();
$this->tableName = $tableName;
$this->modelObject = $modelObject;
}
public function select(){
$this->query = $this->builder->select()->setTable($this->tableName);
return $this;
}
public function insert(){
$this->query = $this->builder->insert()->setTable($this->tableName);
return $this;
}
public function update(){
$this->query = $this->builder->update()->setTable($this->tableName);
return $this;
}
public function delete(){
$this->query = $this->builder->delete()->setTable($this->tableName);
return $this;
}
public function setValues($values){
$this->query->setValues($values);
return $this;
}
public function getQueryString(){
$query = $this->builder->write($this->query);
$values = $this->builder->getValues();
$query = str_replace($this->tableName . '.', '', $query);
foreach($values as $key => $value){
switch (gettype($value)) {
case 'string':
$value = '"' . addslashes($value) . '"';
break;
case 'NULL':
$value = '';
break;
case 'integer':
break;
case 'object':
switch (get_class($value)) {
case 'DateTime':
$value = '"' . $value->format('Y-m-d H:i:s') . '"';
break;
default:
throw new Exception("Trying to insert unknown object type into query", 1);
break;
}
break;
default:
$value = addslashes($value);
break;
}
$pos = strpos($query, $key);
if ($pos !== false) {
$query = substr_replace($query, $value, $pos, strlen($key));
}
}
return $query;
}
public function get(){
$query = $this->getQueryString();
return $this->modelObject::get($query);
}
public function setColumns($columns){
$this->query->setColumns($columns);
return $this;
}
public function where(){
$this->query->where();
return $this;
}
public function equals($column, $value){
$this->query->where()->equals($column, $value)->end();
return $this;
}
public function end(){
$this->query->end();
return $this;
}
}