1
|
|
|
<?php |
2
|
|
|
namespace Data; |
3
|
|
|
|
4
|
|
|
class SQLDataTable extends DataTable |
5
|
|
|
{ |
6
|
|
|
protected $dataset; |
7
|
|
|
protected $tablename; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @param SQLDataSet $dataset The dataset to create this datatable in |
11
|
|
|
* @param string $tablename The name of the table in the dataset |
12
|
|
|
*/ |
13
|
|
|
public function __construct($dataset, $tablename) |
14
|
|
|
{ |
15
|
|
|
$this->dataset = $dataset; |
16
|
|
|
$this->tablename = $tablename; |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
public function get_primary_key() |
20
|
|
|
{ |
21
|
|
|
$res = $this->dataset->raw_query("SHOW INDEX FROM $this->tablename WHERE Key_name='PRIMARY'"); |
22
|
|
|
if($res === false) |
23
|
|
|
{ |
24
|
|
|
return false; |
25
|
|
|
} |
26
|
|
|
return $res[0]['Column_name']; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function count($filter = false) |
30
|
|
|
{ |
31
|
|
|
$where = false; |
32
|
|
|
if($filter !== false) |
33
|
|
|
{ |
34
|
|
|
$where = $filter->to_sql_string(); |
|
|
|
|
35
|
|
|
} |
36
|
|
|
$ret = $this->dataset->read($this->tablename, $where, 'COUNT(*)'); |
37
|
|
|
if($ret === false || !isset($ret[0]) || !isset($ret[0]['COUNT(*)'])) |
38
|
|
|
{ |
39
|
|
|
return false; |
40
|
|
|
} |
41
|
|
|
else |
42
|
|
|
{ |
43
|
|
|
return $ret[0]['COUNT(*)']; |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function read($filter = false, $select = false, $count = false, $skip = false, $sort = false, $params = false) |
48
|
|
|
{ |
49
|
|
|
$where = false; |
50
|
|
|
if($filter !== false) |
51
|
|
|
{ |
52
|
|
|
$where = $filter->to_sql_string(); |
|
|
|
|
53
|
|
|
} |
54
|
|
|
if($select !== false && is_array($select)) |
55
|
|
|
{ |
56
|
|
|
$select = implode(',', $select); |
57
|
|
|
} |
58
|
|
|
return $this->dataset->read($this->tablename, $where, $select, $count, $skip, $sort); |
|
|
|
|
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function update($filter, $data) |
62
|
|
|
{ |
63
|
|
|
$where = $filter->to_sql_string(); |
64
|
|
|
return $this->dataset->update($this->tablename, $where, $data); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function create($data) |
68
|
|
|
{ |
69
|
|
|
return $this->dataset->create($this->tablename, $data); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function delete($filter) |
73
|
|
|
{ |
74
|
|
|
$where = false; |
75
|
|
|
if($filter !== false) |
76
|
|
|
{ |
77
|
|
|
$where = $filter->to_sql_string(); |
78
|
|
|
} |
79
|
|
|
return $this->dataset->delete($this->tablename, $where); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function raw_query($sql) |
83
|
|
|
{ |
84
|
|
|
return $this->dataset->raw_query($sql); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function getLastError() |
88
|
|
|
{ |
89
|
|
|
return $this->dataset->getLastError(); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
/* vim: set tabstop=4 shiftwidth=4 expandtab: */ |
93
|
|
|
|
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.