Skip to content

Commit a4b314a

Browse files
committed
Update DB.php
1 parent cfa1965 commit a4b314a

1 file changed

Lines changed: 59 additions & 101 deletions

File tree

src/DB.php

Lines changed: 59 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ class DB
2121

2222
private $params = array();
2323

24-
protected $pk = null;
25-
2624
private $rowCount = null;
2725

2826
private $select = null;
2927

28+
private $sth = null;
29+
3030
protected $table = null;
3131

3232
private $where = array();
@@ -102,14 +102,10 @@ protected function buildWhere(): string
102102
$tmp[] = sprintf("%s %s %s",
103103
$item['column'],
104104
$item['operator'],
105-
":where_$i"
106-
/*$item['value'] instanceof Expression
107-
? $item['value']
108-
: "'" . $item['value'] . "'"*/);
105+
":where_$i");
109106
$this->param(":where_$i", $item['value']);
110107
} else {
111108
$tmp[] = $item['value'];
112-
//$tmp[] = ":where_$i";
113109
}
114110

115111
$i += 1;
@@ -167,28 +163,17 @@ public function count(): ?int
167163

168164
$statement .= " LIMIT 1;";
169165

170-
$sth = $this->dbh->prepare($statement);
171-
if (!$sth)
166+
if (!$this->fire($statement))
172167
{
173168
return false;
174169
}
175170

176-
if (!$sth->execute($this->params))
177-
{
178-
return false;
179-
}
180-
181-
if ($this->debug)
182-
{
183-
echo '<pre>';
184-
$sth->debugDumpParams();
185-
echo '</pre>';
186-
}
171+
$this->dump();
187172

188-
return $sth->fetchColumn();
173+
return $this->sth->fetchColumn();
189174
}
190175

191-
public function debug(bool $value=null): DB
176+
public function debug(bool $value): DB
192177
{
193178
$this->debug = $value;
194179

@@ -225,103 +210,79 @@ public function delete($modifiers=null): ?int
225210
$statement .= sprintf(" LIMIT %u;", $this->rowCount);
226211
}
227212

228-
$sth = $this->dbh->prepare($statement);
229-
if (!$sth)
213+
if (!$this->fire($statement))
230214
{
231215
return false;
232216
}
233217

234-
if (!$sth->execute($this->params))
235-
{
236-
return false;
237-
}
218+
$this->dump();
238219

220+
return $this->sth->rowCount();
221+
}
222+
223+
protected function dump()
224+
{
239225
if ($this->debug)
240226
{
241227
echo '<pre>';
242-
$sth->debugDumpParams();
228+
$this->sth->debugDumpParams();
243229
echo '</pre>';
244230
}
245-
246-
return $sth->rowCount();
247231
}
248-
232+
249233
public function find($value): ?array
250234
{
251-
$this->where($this->pk, $value);
235+
$this->where('id', $value);
252236
$this->limit(1, 0);
253237

254-
$statement = $this->buildSelect();
255-
256-
$sth = $this->dbh->prepare($statement);
257-
if (!$sth)
238+
if (!$this->fire($this->buildSelect()))
258239
{
259240
return false;
260241
}
261242

262-
if (!$sth->execute($this->params))
243+
$this->dump();
244+
245+
return $this->sth->fetch(\PDO::FETCH_ASSOC);
246+
}
247+
248+
protected function fire(string $statement): bool
249+
{
250+
$this->sth = $this->dbh->prepare($statement);
251+
if (!$this->sth)
263252
{
264253
return false;
265254
}
266255

267-
if ($this->debug)
256+
if (!$this->sth->execute($this->params))
268257
{
269-
echo '<pre>';
270-
$sth->debugDumpParams();
271-
echo '</pre>';
258+
return false;
272259
}
273260

274-
return $sth->fetch(\PDO::FETCH_ASSOC);
261+
return true;
275262
}
276263

277264
public function first(): ?array
278265
{
279-
$statement = $this->buildSelect();
280-
281-
$sth = $this->dbh->prepare($statement);
282-
if (!$sth)
283-
{
284-
return false;
285-
}
286-
287-
if (!$sth->execute($this->params))
266+
if (!$this->fire($this->buildSelect()))
288267
{
289268
return false;
290269
}
291270

292-
if ($this->debug)
293-
{
294-
echo '<pre>';
295-
$sth->debugDumpParams();
296-
echo '</pre>';
297-
}
271+
$this->dump();
298272

299-
return $sth->fetch(\PDO::FETCH_ASSOC);
273+
return $this->sth->fetch(\PDO::FETCH_ASSOC);
300274
}
301275

302276
public function get(): ?array
303277
{
304-
$statement = $this->buildSelect();
305-
306-
$sth = $this->dbh->prepare($statement);
307-
if (!$sth)
278+
if (!$this->fire($this->buildSelect()))
308279
{
309280
return false;
310281
}
311282

312-
if (!$sth->execute($this->params))
313-
{
314-
return false;
315-
}
316-
317-
if ($this->debug)
318-
{
319-
echo '<pre>';
320-
$sth->debugDumpParams();
321-
echo '</pre>';
322-
}
283+
$this->dump();
323284

324-
return $sth->fetchAll(\PDO::FETCH_ASSOC);
285+
return $this->sth->fetchAll(\PDO::FETCH_ASSOC);
325286
}
326287

327288
public function groupBy(string $value=null): DB
@@ -369,18 +330,12 @@ public function insert(array $data, $modifiers=null): ?int
369330

370331
$statement .= " SET " . join(", ", $tmp);
371332

372-
$sth = $this->dbh->prepare($statement);
373-
if (!$sth)
333+
if (!$this->fire($statement))
374334
{
375335
return false;
376336
}
377337

378-
if (!$sth->execute($this->params))
379-
{
380-
return false;
381-
}
382-
383-
return $sth->rowCount()
338+
return $this->sth->rowCount()
384339
? $this->dbh->lastInsertId()
385340
: false;
386341
}
@@ -457,6 +412,7 @@ public function reset(): DB
457412
$this->params = array();
458413
$this->rowCount = null;
459414
$this->select = null;
415+
$this->sth = null;
460416
$this->where = array();
461417

462418
return $this;
@@ -469,13 +425,11 @@ public function select(string $value=null): DB
469425
return $this;
470426
}
471427

472-
public static function table(string $value): DB
428+
public function table(string $value): DB
473429
{
474-
$inst = new self();
475-
476-
$inst->table = $value;
430+
$this->table = $value;
477431

478-
return $inst;
432+
return $this;
479433
}
480434

481435
public function truncate(): bool
@@ -533,27 +487,27 @@ public function update(array $data, $modifiers=null): int
533487
$statement .= sprintf(" LIMIT %u;", $this->rowCount);
534488
}
535489

536-
$sth = $this->dbh->prepare($statement);
537-
if (!$sth)
490+
if (!$this->fire($statement))
538491
{
539492
return false;
540493
}
541494

542-
if (!$sth->execute($this->params))
543-
{
544-
return false;
545-
}
495+
$this->dump();
546496

547-
if ($this->debug)
548-
{
549-
echo '<pre>';
550-
$sth->debugDumpParams();
551-
echo '</pre>';
497+
return $this->sth->rowCount();
552498
}
553499

554-
return $sth->rowCount();
500+
public function value(string $column): string
501+
{
502+
$row = $this->first();
503+
if (!$row)
504+
{
505+
return false;
555506
}
556-
507+
508+
return array_key_exists($column, $row) ? $row[$column] : NULL;
509+
}
510+
557511
public function where(): DB
558512
{
559513
switch (func_num_args())
@@ -573,6 +527,10 @@ public function where(): DB
573527
$operator = func_get_arg(1);
574528
$value = func_get_arg(2);
575529
break;
530+
default:
531+
$column = null;
532+
$operator = null;
533+
$value = null;
576534
}
577535

578536
$this->where[] = array(

0 commit comments

Comments
 (0)