1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by IntelliJ IDEA. |
4
|
|
|
* User: inji |
5
|
|
|
* Date: 07.01.2018 |
6
|
|
|
* Time: 16:39 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Inji\Db\InjiStorage; |
10
|
|
|
|
11
|
|
|
use \Inji\Tools; |
12
|
|
|
|
13
|
|
|
class Query implements \Inji\Db\DriverQuery { |
14
|
|
|
/** |
15
|
|
|
* @var \Inji\Db\InjiStorage |
16
|
|
|
*/ |
17
|
|
|
public $connection; |
18
|
|
|
public $whereArray = []; |
19
|
|
|
public $table = []; |
20
|
|
|
public $dbOptions = []; |
21
|
|
|
|
22
|
|
|
public function __construct($connection) { |
23
|
|
|
$this->connection = $connection; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function setDbOption($name, $value) { |
27
|
|
|
$this->dbOptions[$name] = $value; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
View Code Duplication |
public function where($col, $value = true, $comparision = '=', $concatenation = 'AND') { |
31
|
|
|
if (is_array($col) && !Tools::isAssoc($col)) { |
32
|
|
|
$this->whereArray[] = $col; |
33
|
|
|
} else { |
34
|
|
|
$this->whereArray[] = [$col, $value, $comparision, $concatenation]; |
35
|
|
|
} |
36
|
|
|
return $this; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function setTable($tableName) { |
40
|
|
|
$this->table = $tableName; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function select($tableName = null) { |
44
|
|
|
if (!$tableName) { |
|
|
|
|
45
|
|
|
$tableName = $this->table; |
46
|
|
|
} |
47
|
|
|
return new Result($this->connection->getItems($tableName, $this->whereArray, $this->dbOptions), $this); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function insert(string $tableName, array $values) { |
51
|
|
|
if (!$tableName) { |
52
|
|
|
$tableName = $this->table; |
53
|
|
|
} |
54
|
|
|
return $this->connection->addItem($tableName, $values, $this->dbOptions); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function update(string $tableName, array $values) { |
58
|
|
|
if (!$tableName) { |
59
|
|
|
$tableName = $this->table; |
60
|
|
|
} |
61
|
|
|
return $this->connection->updateItems($tableName, $this->whereArray, $values, $this->dbOptions); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function delete(string $tableName='') { |
65
|
|
|
if (!$tableName) { |
66
|
|
|
$tableName = $this->table; |
67
|
|
|
} |
68
|
|
|
return $this->connection->deleteItems($tableName, $this->whereArray, $this->dbOptions); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
} |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: