1
|
|
|
<?php |
2
|
|
|
namespace WebServCo\Framework\Traits; |
3
|
|
|
|
4
|
|
|
use WebServCo\Framework\AbstractDatabase as Db; |
5
|
|
|
|
6
|
|
|
trait DatabaseTrait |
7
|
|
|
{ |
8
|
|
|
use DatabaseAddQueryTrait; |
9
|
|
|
|
10
|
|
|
abstract public function getColumn($query, $params = [], $columnNumber = 0); |
11
|
|
|
abstract public function query($query, $values = []); |
12
|
|
|
|
13
|
|
|
final public function insert($tableName, $addData = [], $updateData = []) |
14
|
|
|
{ |
15
|
|
|
return $this->add(Db::QUERY_TYPE_INSERT, $tableName, $addData, $updateData); |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
final public function insertIgnore($tableName, $data = []) |
19
|
|
|
{ |
20
|
|
|
return $this->add(Db::QUERY_TYPE_INSERT_IGNORE, $tableName, $data); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
final public function replace($tableName, $data = []) |
24
|
|
|
{ |
25
|
|
|
return $this->add(Db::QUERY_TYPE_REPLACE, $tableName, $data); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
final protected function add($queryType, $tableName, $addData = [], $updateData = []) |
29
|
|
|
{ |
30
|
|
|
if (empty($tableName) || empty($addData)) { |
31
|
|
|
throw new \WebServCo\Framework\Exceptions\ApplicationException('No data specified'); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
$query = $this->generateAddQuery($queryType, $tableName, $addData, $updateData); |
35
|
|
|
|
36
|
|
|
$queryData = []; |
37
|
|
|
foreach ($addData as $item) { |
38
|
|
|
$queryData[] = $item; |
39
|
|
|
} |
40
|
|
|
foreach ($updateData as $item) { |
41
|
|
|
$queryData[] = $item; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
return $this->query($query, $queryData); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
final public function valueExists($table, $field, $value) |
48
|
|
|
{ |
49
|
|
|
return (bool) $this->getColumn( |
50
|
|
|
sprintf( |
51
|
|
|
"SELECT 1 FROM %s WHERE %s = ? LIMIT 1", |
52
|
|
|
$this->escapeIdentifier($table), |
53
|
|
|
$this->escapeIdentifier($field) |
54
|
|
|
), |
55
|
|
|
[$value] |
56
|
|
|
); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
final public function tableExists($table, $database = null) |
60
|
|
|
{ |
61
|
|
|
$name = $this->escapeIdentifier($table); |
62
|
|
|
if (!empty($database)) { |
63
|
|
|
$name = sprintf('%s.%s', $this->escapeIdentifier($database), $this->escapeIdentifier($table)); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
try { |
67
|
|
|
$this->query(sprintf('SELECT 1 FROM %s LIMIT 1', $name)); |
68
|
|
|
return true; |
69
|
|
|
//} catch (\Exception $e) { |
|
|
|
|
70
|
|
|
//return false; |
71
|
|
|
} catch (\WebServCo\Framework\Exceptions\DatabaseException $e) { |
72
|
|
|
return false; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.