Completed
Push — master ( dd6108...435347 )
by Patrick
02:41
created

SQLDataTable   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 83
c 0
b 0
f 0
rs 10
wmc 17
lcom 1
cbo 2

8 Methods

Rating   Name   Duplication   Size   Complexity  
A get_primary_key() 0 9 2
A create() 0 4 1
A delete() 0 9 2
A __construct() 0 5 1
B count() 0 17 5
A update() 0 5 1
A raw_query() 0 4 1
A read() 0 13 4
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();
0 ignored issues
show
Bug introduced by
The method to_sql_string cannot be called on $filter (of type boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
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();
0 ignored issues
show
Bug introduced by
The method to_sql_string cannot be called on $filter (of type boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
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);
0 ignored issues
show
Bug introduced by
It seems like $select defined by parameter $select on line 47 can also be of type boolean; however, Data\SQLDataSet::read() does only seem to accept string, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
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
/* vim: set tabstop=4 shiftwidth=4 expandtab: */
88