1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Doctrine\Tests\DBAL\Functional; |
4
|
|
|
|
5
|
|
|
use Doctrine\DBAL\ParameterType; |
6
|
|
|
use Doctrine\DBAL\Types\Type; |
7
|
|
|
use Doctrine\Tests\DbalFunctionalTestCase; |
8
|
|
|
use Doctrine\Tests\TestUtil; |
9
|
|
|
use function array_filter; |
10
|
|
|
use function strtolower; |
11
|
|
|
|
12
|
|
|
class WriteTest extends DbalFunctionalTestCase |
13
|
|
|
{ |
14
|
|
|
protected function setUp() |
15
|
|
|
{ |
16
|
|
|
parent::setUp(); |
17
|
|
|
|
18
|
|
|
$this->createTable('write_table'); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
private function createTable($tableName) |
22
|
|
|
{ |
23
|
|
|
$table = new \Doctrine\DBAL\Schema\Table($tableName); |
24
|
|
|
$table->addColumn('id', 'integer', array('autoincrement' => true)); |
25
|
|
|
$table->addColumn('test_int', 'integer'); |
26
|
|
|
$table->addColumn('test_string', 'string', array('notnull' => false)); |
27
|
|
|
$table->setPrimaryKey(array('id')); |
28
|
|
|
|
29
|
|
|
$this->_conn->getSchemaManager()->createTable($table); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
protected function tearDown() |
33
|
|
|
{ |
34
|
|
|
$this->_conn->getSchemaManager()->dropTable('write_table'); |
35
|
|
|
|
36
|
|
|
parent::tearDown(); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @group DBAL-80 |
41
|
|
|
*/ |
42
|
|
|
public function testExecuteUpdateFirstTypeIsNull() |
43
|
|
|
{ |
44
|
|
|
$sql = "INSERT INTO write_table (test_string, test_int) VALUES (?, ?)"; |
45
|
|
|
$this->_conn->executeUpdate($sql, ['text', 1111], [null, ParameterType::INTEGER]); |
46
|
|
|
|
47
|
|
|
$sql = "SELECT * FROM write_table WHERE test_string = ? AND test_int = ?"; |
48
|
|
|
self::assertTrue((bool) $this->_conn->fetchColumn($sql, ['text', 1111])); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function testExecuteUpdate() |
52
|
|
|
{ |
53
|
|
|
$sql = "INSERT INTO write_table (test_int) VALUES ( " . $this->_conn->quote(1) . ")"; |
54
|
|
|
$affected = $this->_conn->executeUpdate($sql); |
55
|
|
|
|
56
|
|
|
self::assertEquals(1, $affected, "executeUpdate() should return the number of affected rows!"); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function testExecuteUpdateWithTypes() |
60
|
|
|
{ |
61
|
|
|
$sql = "INSERT INTO write_table (test_int, test_string) VALUES (?, ?)"; |
62
|
|
|
$affected = $this->_conn->executeUpdate( |
63
|
|
|
$sql, |
64
|
|
|
[1, 'foo'], |
65
|
|
|
[ParameterType::INTEGER, ParameterType::STRING] |
66
|
|
|
); |
67
|
|
|
|
68
|
|
|
self::assertEquals(1, $affected, "executeUpdate() should return the number of affected rows!"); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function testPrepareRowCountReturnsAffectedRows() |
72
|
|
|
{ |
73
|
|
|
$sql = "INSERT INTO write_table (test_int, test_string) VALUES (?, ?)"; |
74
|
|
|
$stmt = $this->_conn->prepare($sql); |
75
|
|
|
|
76
|
|
|
$stmt->bindValue(1, 1); |
77
|
|
|
$stmt->bindValue(2, "foo"); |
78
|
|
|
$stmt->execute(); |
79
|
|
|
|
80
|
|
|
self::assertEquals(1, $stmt->rowCount()); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function testPrepareWithPdoTypes() |
84
|
|
|
{ |
85
|
|
|
$sql = "INSERT INTO write_table (test_int, test_string) VALUES (?, ?)"; |
86
|
|
|
$stmt = $this->_conn->prepare($sql); |
87
|
|
|
|
88
|
|
|
$stmt->bindValue(1, 1, ParameterType::INTEGER); |
89
|
|
|
$stmt->bindValue(2, 'foo', ParameterType::STRING); |
90
|
|
|
$stmt->execute(); |
91
|
|
|
|
92
|
|
|
self::assertEquals(1, $stmt->rowCount()); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function testPrepareWithDbalTypes() |
96
|
|
|
{ |
97
|
|
|
$sql = "INSERT INTO write_table (test_int, test_string) VALUES (?, ?)"; |
98
|
|
|
$stmt = $this->_conn->prepare($sql); |
99
|
|
|
|
100
|
|
|
$stmt->bindValue(1, 1, Type::getType('integer')); |
101
|
|
|
$stmt->bindValue(2, "foo", Type::getType('string')); |
102
|
|
|
$stmt->execute(); |
103
|
|
|
|
104
|
|
|
self::assertEquals(1, $stmt->rowCount()); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function testPrepareWithDbalTypeNames() |
108
|
|
|
{ |
109
|
|
|
$sql = "INSERT INTO write_table (test_int, test_string) VALUES (?, ?)"; |
110
|
|
|
$stmt = $this->_conn->prepare($sql); |
111
|
|
|
|
112
|
|
|
$stmt->bindValue(1, 1, 'integer'); |
113
|
|
|
$stmt->bindValue(2, "foo", 'string'); |
114
|
|
|
$stmt->execute(); |
115
|
|
|
|
116
|
|
|
self::assertEquals(1, $stmt->rowCount()); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function insertRows() |
120
|
|
|
{ |
121
|
|
|
self::assertEquals(1, $this->_conn->insert('write_table', array('test_int' => 1, 'test_string' => 'foo'))); |
122
|
|
|
self::assertEquals(1, $this->_conn->insert('write_table', array('test_int' => 2, 'test_string' => 'bar'))); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function testInsert() |
126
|
|
|
{ |
127
|
|
|
$this->insertRows(); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
public function testDelete() |
131
|
|
|
{ |
132
|
|
|
$this->insertRows(); |
133
|
|
|
|
134
|
|
|
self::assertEquals(1, $this->_conn->delete('write_table', array('test_int' => 2))); |
135
|
|
|
self::assertCount(1, $this->_conn->fetchAll('SELECT * FROM write_table')); |
136
|
|
|
|
137
|
|
|
self::assertEquals(1, $this->_conn->delete('write_table', array('test_int' => 1))); |
138
|
|
|
self::assertCount(0, $this->_conn->fetchAll('SELECT * FROM write_table')); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
public function testUpdate() |
142
|
|
|
{ |
143
|
|
|
$this->insertRows(); |
144
|
|
|
|
145
|
|
|
self::assertEquals(1, $this->_conn->update('write_table', array('test_string' => 'bar'), array('test_string' => 'foo'))); |
146
|
|
|
self::assertEquals(2, $this->_conn->update('write_table', array('test_string' => 'baz'), array('test_string' => 'bar'))); |
147
|
|
|
self::assertEquals(0, $this->_conn->update('write_table', array('test_string' => 'baz'), array('test_string' => 'bar'))); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @group DBAL-445 |
152
|
|
|
*/ |
153
|
|
|
public function testInsertWithKeyValueTypes() |
154
|
|
|
{ |
155
|
|
|
$testString = new \DateTime('2013-04-14 10:10:10'); |
156
|
|
|
|
157
|
|
|
$this->_conn->insert( |
158
|
|
|
'write_table', |
159
|
|
|
array('test_int' => '30', 'test_string' => $testString), |
160
|
|
|
array('test_string' => 'datetime', 'test_int' => 'integer') |
161
|
|
|
); |
162
|
|
|
|
163
|
|
|
$data = $this->_conn->fetchColumn('SELECT test_string FROM write_table WHERE test_int = 30'); |
164
|
|
|
|
165
|
|
|
self::assertEquals($testString->format($this->_conn->getDatabasePlatform()->getDateTimeFormatString()), $data); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @group DBAL-445 |
170
|
|
|
*/ |
171
|
|
|
public function testUpdateWithKeyValueTypes() |
172
|
|
|
{ |
173
|
|
|
$testString = new \DateTime('2013-04-14 10:10:10'); |
174
|
|
|
|
175
|
|
|
$this->_conn->insert( |
176
|
|
|
'write_table', |
177
|
|
|
array('test_int' => '30', 'test_string' => $testString), |
178
|
|
|
array('test_string' => 'datetime', 'test_int' => 'integer') |
179
|
|
|
); |
180
|
|
|
|
181
|
|
|
$testString = new \DateTime('2013-04-15 10:10:10'); |
182
|
|
|
|
183
|
|
|
$this->_conn->update( |
184
|
|
|
'write_table', |
185
|
|
|
array('test_string' => $testString), |
186
|
|
|
array('test_int' => '30'), |
187
|
|
|
array('test_string' => 'datetime', 'test_int' => 'integer') |
188
|
|
|
); |
189
|
|
|
|
190
|
|
|
$data = $this->_conn->fetchColumn('SELECT test_string FROM write_table WHERE test_int = 30'); |
191
|
|
|
|
192
|
|
|
self::assertEquals($testString->format($this->_conn->getDatabasePlatform()->getDateTimeFormatString()), $data); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* @group DBAL-445 |
197
|
|
|
*/ |
198
|
|
|
public function testDeleteWithKeyValueTypes() |
199
|
|
|
{ |
200
|
|
|
$val = new \DateTime('2013-04-14 10:10:10'); |
201
|
|
|
$this->_conn->insert( |
202
|
|
|
'write_table', |
203
|
|
|
array('test_int' => '30', 'test_string' => $val), |
204
|
|
|
array('test_string' => 'datetime', 'test_int' => 'integer') |
205
|
|
|
); |
206
|
|
|
|
207
|
|
|
$this->_conn->delete('write_table', array('test_int' => 30, 'test_string' => $val), array('test_string' => 'datetime', 'test_int' => 'integer')); |
208
|
|
|
|
209
|
|
|
$data = $this->_conn->fetchColumn('SELECT test_string FROM write_table WHERE test_int = 30'); |
210
|
|
|
|
211
|
|
|
self::assertFalse($data); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
public function testEmptyIdentityInsert() |
215
|
|
|
{ |
216
|
|
|
$table = new \Doctrine\DBAL\Schema\Table('test_empty_identity'); |
217
|
|
|
$table->addColumn('id', 'integer', array('autoincrement' => true)); |
218
|
|
|
$table->setPrimaryKey(array('id')); |
219
|
|
|
|
220
|
|
|
$this->_conn->getSchemaManager()->createTable($table); |
221
|
|
|
|
222
|
|
|
$sql = $this->_conn->getDatabasePlatform()->getEmptyIdentityInsertSQL('test_empty_identity', 'id'); |
223
|
|
|
|
224
|
|
|
self::assertSame(1, $this->_conn->exec($sql)); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* @group DBAL-2688 |
229
|
|
|
*/ |
230
|
|
|
public function testUpdateWhereIsNull() |
231
|
|
|
{ |
232
|
|
|
$this->_conn->insert( |
233
|
|
|
'write_table', |
234
|
|
|
['test_int' => '30', 'test_string' => null], |
235
|
|
|
['test_string' => 'string', 'test_int' => 'integer'] |
236
|
|
|
); |
237
|
|
|
|
238
|
|
|
$data = $this->_conn->fetchAll('SELECT * FROM write_table WHERE test_int = 30'); |
239
|
|
|
|
240
|
|
|
self::assertCount(1, $data); |
241
|
|
|
|
242
|
|
|
$this->_conn->update('write_table', ['test_int' => 10], ['test_string' => null], ['test_string' => 'string', 'test_int' => 'integer']); |
243
|
|
|
|
244
|
|
|
$data = $this->_conn->fetchAll('SELECT * FROM write_table WHERE test_int = 30'); |
245
|
|
|
|
246
|
|
|
self::assertCount(0, $data); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
public function testDeleteWhereIsNull() |
250
|
|
|
{ |
251
|
|
|
$this->_conn->insert( |
252
|
|
|
'write_table', |
253
|
|
|
['test_int' => '30', 'test_string' => null], |
254
|
|
|
['test_string' => 'string', 'test_int' => 'integer'] |
255
|
|
|
); |
256
|
|
|
|
257
|
|
|
$data = $this->_conn->fetchAll('SELECT * FROM write_table WHERE test_int = 30'); |
258
|
|
|
|
259
|
|
|
self::assertCount(1, $data); |
260
|
|
|
|
261
|
|
|
$this->_conn->delete('write_table', ['test_string' => null], ['test_string' => 'string']); |
262
|
|
|
|
263
|
|
|
$data = $this->_conn->fetchAll('SELECT * FROM write_table WHERE test_int = 30'); |
264
|
|
|
|
265
|
|
|
self::assertCount(0, $data); |
266
|
|
|
} |
267
|
|
|
} |
268
|
|
|
|