forked from Runalyze/Runalyze
/
Inserter.php
183 lines (154 loc) · 3.18 KB
/
Inserter.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
/**
* This file contains class::Insrter
* @package Runalyze\Model
*/
namespace Runalyze\Model;
/**
* Insert object to database
*
* It may be of need to set an object before using prepared statements.
*
* @author Hannes Christiansen
* @package Runalyze\Model
*/
abstract class Inserter {
/**
* PDO
* @var \PDO
*/
protected $PDO;
/**
* Object
* @var \Runalyze\Model\Entity
*/
protected $Object;
/**
* Prepared insert statement
* @var \PDOStatement
*/
protected $Prepared = null;
/**
* @var int
*/
protected $InsertedID = false;
/**
* Construct inserter
* @param \PDO $connection
* @param \Runalyze\Model\Entity $object [optional]
*/
public function __construct(\PDO $connection, Entity $object = null) {
$this->PDO = $connection;
$this->Object = $object;
}
/**
* Tablename without prefix
* @return string
*/
abstract protected function table();
/**
* Keys to insert
* @return array
*/
abstract protected function keys();
/**
* Prepare insert
* @throws \RuntimeException
*/
public function prepare() {
$keys = $this->keys();
if (empty($keys)) {
throw new \RuntimeException('This class does not support prepared inserts.');
}
$this->Prepared = $this->PDO->prepare('
INSERT INTO `'.PREFIX.$this->table().'`
(`'.implode('`,`', $keys).'`)
VALUES (:'.implode(', :', $keys).')
');
}
/**
* Set object
* @param \Runalyze\Model\Entity $object
*/
final public function insert(Entity $object = null) {
if (!is_null($object)) {
$this->Object = $object;
}
$this->before();
$this->runInsert();
$this->after();
}
/**
* Run insert
*/
private function runInsert() {
if (!is_null($this->Prepared)) {
$this->runPreparedInsert();
} else {
$this->runManualInsert();
}
$this->InsertedID = $this->PDO->lastInsertId();
if ($this->Object instanceof EntityWithID) {
$this->Object->setID($this->InsertedID);
}
}
/**
* Run prepared statement
*/
private function runPreparedInsert() {
$values = array();
foreach ($this->keys() as $key) {
$values[':'.$key] = $this->value($key);
}
$this->Prepared->execute($values);
}
/**
* Run manual insert
*/
private function runManualInsert() {
$keys = $this->keys();
$values = array();
foreach ($keys as $key) {
$value = $this->value($key);
$values[] = is_null($value) ? 'NULL' : $this->PDO->quote($value);
}
$this->PDO->exec('
INSERT INTO `'.PREFIX.$this->table().'`
(`'.implode('`,`', $keys).'`)
VALUES ('.implode(',', $values).')
');
}
/**
* Value for key
* @param string $key
* @return string
*/
protected function value($key) {
$value = $this->Object->get($key);
if (is_array($value)) {
return Entity::implode($value);
}
return $value;
}
/**
* Tasks before insertion
*/
protected function before() {
$this->Object->synchronize();
}
/**
* Tasks after insertion
*/
protected function after() {}
/**
* Last inserted ID
* @return int
* @throws \RuntimeException
*/
final public function insertedID() {
if (!($this->Object instanceof EntityWithID)) {
throw new \RuntimeException('Only objects with id serve an inserted id.');
}
return $this->InsertedID;
}
}