1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace database; |
5
|
|
|
|
6
|
|
|
use database\DB; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class Read |
10
|
|
|
* @package src |
11
|
|
|
*/ |
12
|
|
|
class Read |
13
|
|
|
{ |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var |
17
|
|
|
*/ |
18
|
|
|
private $db; |
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var |
21
|
|
|
*/ |
22
|
|
|
private $table; |
23
|
|
|
/** |
24
|
|
|
* @var |
25
|
|
|
*/ |
26
|
|
|
private $read; |
27
|
|
|
/** |
28
|
|
|
* @var |
29
|
|
|
*/ |
30
|
|
|
private $statement; |
31
|
|
|
/** |
32
|
|
|
* @var |
33
|
|
|
*/ |
34
|
|
|
private $terms; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var \PDOStatement |
38
|
|
|
*/ |
39
|
|
|
private $select; |
40
|
|
|
|
41
|
|
|
private $query; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var |
45
|
|
|
*/ |
46
|
|
|
private $limit; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var |
50
|
|
|
*/ |
51
|
|
|
private $offset; |
52
|
|
|
|
53
|
|
|
private $page; |
54
|
|
|
private $link; |
|
|
|
|
55
|
|
|
private $rows; |
|
|
|
|
56
|
|
|
private $maxLinks; |
|
|
|
|
57
|
|
|
private $first; |
|
|
|
|
58
|
|
|
private $last; |
|
|
|
|
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @var |
62
|
|
|
*/ |
63
|
|
|
private $result; |
64
|
|
|
|
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Read constructor. |
68
|
|
|
*/ |
69
|
|
|
public function __construct() |
70
|
|
|
{ |
71
|
|
|
|
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @return mixed |
76
|
|
|
*/ |
77
|
|
|
public function getResult() |
78
|
|
|
{ |
79
|
|
|
return $this->result; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param $table |
84
|
|
|
* @param null $parse |
|
|
|
|
85
|
|
|
* @return array |
86
|
|
|
*/ |
87
|
|
|
|
88
|
|
|
public function query(string $query, string $parse = null) |
89
|
|
|
{ |
90
|
|
|
if ($parse) { |
91
|
|
|
parse_str($parse, $this->statement); |
92
|
|
|
|
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$this->query = $query; |
96
|
|
|
$this->read = DB::connect()->prepare($this->query); |
97
|
|
|
$this->read->execute($this->statement); |
98
|
|
|
$this->result = $this->read->fetchAll(\PDO::FETCH_OBJ); |
99
|
|
|
return $this; |
|
|
|
|
100
|
|
|
|
101
|
|
|
|
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function all($table) |
105
|
|
|
{ |
106
|
|
|
$this->table = (string)$table; |
107
|
|
|
|
108
|
|
|
|
109
|
|
|
$this->read = DB::connect()->prepare("SELECT * FROM {$this->table}"); |
110
|
|
|
$this->read->execute(); |
111
|
|
|
return $this->result = $this->read->fetchAll(\PDO::FETCH_OBJ); |
112
|
|
|
return $this; |
|
|
|
|
113
|
|
|
|
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param string $table |
118
|
|
|
* @param string $terms |
119
|
|
|
* @param string $parse |
120
|
|
|
* @return mixed |
121
|
|
|
*/ |
122
|
|
|
public function find(string $table, string $terms, string $parse) |
123
|
|
|
{ |
124
|
|
|
$this->table = $table; |
125
|
|
|
$this->terms = $terms; |
126
|
|
|
|
127
|
|
|
if ($parse) { |
128
|
|
|
parse_str($parse, $this->statement); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
try { |
132
|
|
|
$this->read = DB::connect()->prepare("SELECT * FROM {$this->table} {$this->terms}"); |
133
|
|
|
$this->read->execute($this->statement); |
134
|
|
|
return $this->result = $this->read->fetchAll(\PDO::FETCH_OBJ); |
135
|
|
|
} catch (\PDOException $e) { |
136
|
|
|
echo $e->getMessage() . " in " . $e->getFile(); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
return $this; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public function getRowCount() |
143
|
|
|
{ |
144
|
|
|
return $this->select->rowCount(); |
145
|
|
|
return $this; |
|
|
|
|
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public function page(int $pag = null, int $limit) |
149
|
|
|
{ |
150
|
|
|
$this->page = ($pag ? $pag : 1); |
151
|
|
|
$this->limit = $limit; |
152
|
|
|
$this->offset = ($this->page * $this->limit) - $this->limit; |
153
|
|
|
|
154
|
|
|
try { |
155
|
|
|
|
156
|
|
|
$this->read = DB::connect()->prepare("SELECT * FROM {$this->table} LIMIT {$this->limit} OFFSET {$this->offset}"); |
157
|
|
|
$this->read->execute(); |
158
|
|
|
$this->result = $this->read->fetchAll(\PDO::FETCH_OBJ); |
159
|
|
|
}catch (\PDOException $e){ |
160
|
|
|
echo $e->getMessage() . " in " . $e->getFile(); |
161
|
|
|
|
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
|
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
|
168
|
|
|
} |