-
Notifications
You must be signed in to change notification settings - Fork 1
/
dataconnector.php
130 lines (112 loc) · 3.08 KB
/
dataconnector.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
/*
* ----------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* <[email protected]> wrote this file. As long as you retain this notice you
* can do whatever you want with this stuff. If we meet some day, and you think
* this stuff is worth it, you can buy me a beer in return. Mattias Nyberg
* ----------------------------------------------------------------------------
*/
/*
* Data connector
*/
class DataConnector
{
function __construct($dbh, $table_name)
{
$this->dbh = $dbh;
$this->table_name = $table_name;
$this->error = array();
}
public function errors()
{
return $this->error;
}
protected function add_error($error)
{
array_push($this->error, $error);
}
protected function selective_item_update($data, $id)
{
if(!$this->connected_or_error()) return FALSE;
$query_string = '';
$query_data = array();
$first = ' ';
foreach($data as $key => $item)
{
if($item !== NULL)
{
$query_string .= $first .$key. '=:' .$key;
$query_data[$key] = $item;
$first = ', ';
}
}
$query_data['id'] = $id;
$stmt = $this->dbh->prepare('UPDATE ' .$this->table_name. ' SET ' .$query_string. ' WHERE id=:id');
$result = $stmt->execute($query_data);
if($result)
{
$stmt->closeCursor();
return TRUE;
}
return FALSE;
}
public function get_complete_item($id)
{
return $this->get_limited_item($id, '*');
}
protected function get_limited_item($id, $values, $get_disabled = TRUE)
{
if(!$this->connected_or_error()) return FALSE;
$disabled_string = $get_disabled ? '' : ' AND disabled=FALSE';
$select_string = is_array($values) ? implode(',', $values) : $values;
$stmt = $this->dbh->prepare('SELECT ' .$select_string. ' FROM ' .$this->table_name. ' WHERE id=:id' .$disabled_string);
$result = $stmt->execute(array('id'=>$id));
if($result)
{
$item = $stmt->fetch();
$stmt->closeCursor();
return $item;
}
return FALSE;
}
protected function connected_or_error()
{
if($this->dbh == null)
{
throw new Exception('Database not connected.');
return FALSE;
}
return TRUE;
}
protected function get_limited_list($items, $start, $limit, $get_disabled = TRUE)
{
if(!$this->connected_or_error()) return FALSE;
$disabled_string = $get_disabled ? '' : ' WHERE disabled=FALSE';
$select_string = is_array($items) ? implode(',', $items) : $items;
$limit_string = ($limit > 0) ? ' LIMIT ' .$start. ',' .$limit : '';
$stmt = $this->dbh->prepare('SELECT ' .$select_string. ' FROM ' .$this->table_name .$disabled_string. ' ORDER BY id DESC ' .$limit_string);
$result = $stmt->execute();
if($result)
{
$item = $stmt->fetchAll();
$stmt->closeCursor();
return $item;
}
return FALSE;
}
public function count_items()
{
if(!$this->connected_or_error()) return FALSE;
$stmt = $this->dbh->prepare('SELECT COUNT(*) FROM ' .$this->table_name. ' WHERE disabled=FALSE');
$result = $stmt->execute();
if($result !== FALSE)
{
$count = $item = $stmt->fetch();
$stmt->closeCursor();
return $count[0];
}
return FALSE;
}
}
?>