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