| 13532 |
anikendra |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* SqlserverTest file
|
|
|
4 |
*
|
|
|
5 |
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
|
|
6 |
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
|
|
7 |
*
|
|
|
8 |
* Licensed under The MIT License
|
|
|
9 |
* For full copyright and license information, please see the LICENSE.txt
|
|
|
10 |
* Redistributions of files must retain the above copyright notice.
|
|
|
11 |
*
|
|
|
12 |
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
|
|
13 |
* @link http://cakephp.org CakePHP(tm) Project
|
|
|
14 |
* @package Cake.Test.Case.Model.Datasource.Database
|
|
|
15 |
* @since CakePHP(tm) v 1.2.0
|
|
|
16 |
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
|
|
17 |
*/
|
|
|
18 |
|
|
|
19 |
App::uses('Model', 'Model');
|
|
|
20 |
App::uses('AppModel', 'Model');
|
|
|
21 |
App::uses('Sqlserver', 'Model/Datasource/Database');
|
|
|
22 |
|
|
|
23 |
require_once dirname(dirname(dirname(__FILE__))) . DS . 'models.php';
|
|
|
24 |
|
|
|
25 |
/**
|
|
|
26 |
* SqlserverTestDb class
|
|
|
27 |
*
|
|
|
28 |
* @package Cake.Test.Case.Model.Datasource.Database
|
|
|
29 |
*/
|
|
|
30 |
class SqlserverTestDb extends Sqlserver {
|
|
|
31 |
|
|
|
32 |
/**
|
|
|
33 |
* simulated property
|
|
|
34 |
*
|
|
|
35 |
* @var array
|
|
|
36 |
*/
|
|
|
37 |
public $simulated = array();
|
|
|
38 |
|
|
|
39 |
/**
|
|
|
40 |
* execute results stack
|
|
|
41 |
*
|
|
|
42 |
* @var array
|
|
|
43 |
*/
|
|
|
44 |
public $executeResultsStack = array();
|
|
|
45 |
|
|
|
46 |
/**
|
|
|
47 |
* execute method
|
|
|
48 |
*
|
|
|
49 |
* @param mixed $sql
|
|
|
50 |
* @param mixed $params
|
|
|
51 |
* @param mixed $prepareOptions
|
|
|
52 |
* @return mixed
|
|
|
53 |
*/
|
|
|
54 |
protected function _execute($sql, $params = array(), $prepareOptions = array()) {
|
|
|
55 |
$this->simulated[] = $sql;
|
|
|
56 |
return empty($this->executeResultsStack) ? null : array_pop($this->executeResultsStack);
|
|
|
57 |
}
|
|
|
58 |
|
|
|
59 |
/**
|
|
|
60 |
* fetchAll method
|
|
|
61 |
*
|
|
|
62 |
* @param mixed $sql
|
|
|
63 |
* @return void
|
|
|
64 |
*/
|
|
|
65 |
protected function _matchRecords(Model $model, $conditions = null) {
|
|
|
66 |
return $this->conditions(array('id' => array(1, 2)));
|
|
|
67 |
}
|
|
|
68 |
|
|
|
69 |
/**
|
|
|
70 |
* getLastQuery method
|
|
|
71 |
*
|
|
|
72 |
* @return string
|
|
|
73 |
*/
|
|
|
74 |
public function getLastQuery() {
|
|
|
75 |
return $this->simulated[count($this->simulated) - 1];
|
|
|
76 |
}
|
|
|
77 |
|
|
|
78 |
/**
|
|
|
79 |
* getPrimaryKey method
|
|
|
80 |
*
|
|
|
81 |
* @param mixed $model
|
|
|
82 |
* @return string
|
|
|
83 |
*/
|
|
|
84 |
public function getPrimaryKey($model) {
|
|
|
85 |
return parent::_getPrimaryKey($model);
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
/**
|
|
|
89 |
* clearFieldMappings method
|
|
|
90 |
*
|
|
|
91 |
* @return void
|
|
|
92 |
*/
|
|
|
93 |
public function clearFieldMappings() {
|
|
|
94 |
$this->_fieldMappings = array();
|
|
|
95 |
}
|
|
|
96 |
|
|
|
97 |
/**
|
|
|
98 |
* describe method
|
|
|
99 |
*
|
|
|
100 |
* @param object $model
|
|
|
101 |
* @return void
|
|
|
102 |
*/
|
|
|
103 |
public function describe($model) {
|
|
|
104 |
return empty($this->describe) ? parent::describe($model) : $this->describe;
|
|
|
105 |
}
|
|
|
106 |
|
|
|
107 |
}
|
|
|
108 |
|
|
|
109 |
/**
|
|
|
110 |
* SqlserverTestModel class
|
|
|
111 |
*
|
|
|
112 |
* @package Cake.Test.Case.Model.Datasource.Database
|
|
|
113 |
*/
|
|
|
114 |
class SqlserverTestModel extends CakeTestModel {
|
|
|
115 |
|
|
|
116 |
/**
|
|
|
117 |
* useTable property
|
|
|
118 |
*
|
|
|
119 |
* @var boolean
|
|
|
120 |
*/
|
|
|
121 |
public $useTable = false;
|
|
|
122 |
|
|
|
123 |
/**
|
|
|
124 |
* _schema property
|
|
|
125 |
*
|
|
|
126 |
* @var array
|
|
|
127 |
*/
|
|
|
128 |
protected $_schema = array(
|
|
|
129 |
'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8', 'key' => 'primary'),
|
|
|
130 |
'client_id' => array('type' => 'integer', 'null' => '', 'default' => '0', 'length' => '11'),
|
|
|
131 |
'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
|
|
|
132 |
'login' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
|
|
|
133 |
'passwd' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '255'),
|
|
|
134 |
'addr_1' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '255'),
|
|
|
135 |
'addr_2' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '25'),
|
|
|
136 |
'zip_code' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'),
|
|
|
137 |
'city' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'),
|
|
|
138 |
'country' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'),
|
|
|
139 |
'phone' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'),
|
|
|
140 |
'fax' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'),
|
|
|
141 |
'url' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '255'),
|
|
|
142 |
'email' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'),
|
|
|
143 |
'comments' => array('type' => 'text', 'null' => '1', 'default' => '', 'length' => ''),
|
|
|
144 |
'last_login' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => ''),
|
|
|
145 |
'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
|
|
|
146 |
'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
|
|
|
147 |
);
|
|
|
148 |
|
|
|
149 |
/**
|
|
|
150 |
* belongsTo property
|
|
|
151 |
*
|
|
|
152 |
* @var array
|
|
|
153 |
*/
|
|
|
154 |
public $belongsTo = array(
|
|
|
155 |
'SqlserverClientTestModel' => array(
|
|
|
156 |
'foreignKey' => 'client_id'
|
|
|
157 |
)
|
|
|
158 |
);
|
|
|
159 |
|
|
|
160 |
/**
|
|
|
161 |
* find method
|
|
|
162 |
*
|
|
|
163 |
* @param mixed $conditions
|
|
|
164 |
* @param mixed $fields
|
|
|
165 |
* @param mixed $order
|
|
|
166 |
* @param mixed $recursive
|
|
|
167 |
* @return void
|
|
|
168 |
*/
|
|
|
169 |
public function find($conditions = null, $fields = null, $order = null, $recursive = null) {
|
|
|
170 |
return $conditions;
|
|
|
171 |
}
|
|
|
172 |
|
|
|
173 |
}
|
|
|
174 |
|
|
|
175 |
/**
|
|
|
176 |
* SqlserverClientTestModel class
|
|
|
177 |
*
|
|
|
178 |
* @package Cake.Test.Case.Model.Datasource.Database
|
|
|
179 |
*/
|
|
|
180 |
class SqlserverClientTestModel extends CakeTestModel {
|
|
|
181 |
|
|
|
182 |
/**
|
|
|
183 |
* useTable property
|
|
|
184 |
*
|
|
|
185 |
* @var boolean
|
|
|
186 |
*/
|
|
|
187 |
public $useTable = false;
|
|
|
188 |
|
|
|
189 |
/**
|
|
|
190 |
* _schema property
|
|
|
191 |
*
|
|
|
192 |
* @var array
|
|
|
193 |
*/
|
|
|
194 |
protected $_schema = array(
|
|
|
195 |
'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8', 'key' => 'primary'),
|
|
|
196 |
'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
|
|
|
197 |
'email' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'),
|
|
|
198 |
'created' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => ''),
|
|
|
199 |
'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
|
|
|
200 |
);
|
|
|
201 |
}
|
|
|
202 |
|
|
|
203 |
/**
|
|
|
204 |
* SqlserverTestResultIterator class
|
|
|
205 |
*
|
|
|
206 |
* @package Cake.Test.Case.Model.Datasource.Database
|
|
|
207 |
*/
|
|
|
208 |
class SqlserverTestResultIterator extends ArrayIterator {
|
|
|
209 |
|
|
|
210 |
/**
|
|
|
211 |
* closeCursor method
|
|
|
212 |
*
|
|
|
213 |
* @return void
|
|
|
214 |
*/
|
|
|
215 |
public function closeCursor() {
|
|
|
216 |
}
|
|
|
217 |
|
|
|
218 |
/**
|
|
|
219 |
* fetch method
|
|
|
220 |
*
|
|
|
221 |
* @return void
|
|
|
222 |
*/
|
|
|
223 |
public function fetch() {
|
|
|
224 |
if (!$this->valid()) {
|
|
|
225 |
return null;
|
|
|
226 |
}
|
|
|
227 |
$current = $this->current();
|
|
|
228 |
$this->next();
|
|
|
229 |
return $current;
|
|
|
230 |
}
|
|
|
231 |
|
|
|
232 |
}
|
|
|
233 |
|
|
|
234 |
/**
|
|
|
235 |
* SqlserverTest class
|
|
|
236 |
*
|
|
|
237 |
* @package Cake.Test.Case.Model.Datasource.Database
|
|
|
238 |
*/
|
|
|
239 |
class SqlserverTest extends CakeTestCase {
|
|
|
240 |
|
|
|
241 |
/**
|
|
|
242 |
* The Dbo instance to be tested
|
|
|
243 |
*
|
|
|
244 |
* @var DboSource
|
|
|
245 |
*/
|
|
|
246 |
public $db = null;
|
|
|
247 |
|
|
|
248 |
/**
|
|
|
249 |
* autoFixtures property
|
|
|
250 |
*
|
|
|
251 |
* @var boolean
|
|
|
252 |
*/
|
|
|
253 |
public $autoFixtures = false;
|
|
|
254 |
|
|
|
255 |
/**
|
|
|
256 |
* fixtures property
|
|
|
257 |
*
|
|
|
258 |
* @var array
|
|
|
259 |
*/
|
|
|
260 |
public $fixtures = array('core.user', 'core.category', 'core.author', 'core.post');
|
|
|
261 |
|
|
|
262 |
/**
|
|
|
263 |
* Sets up a Dbo class instance for testing
|
|
|
264 |
*
|
|
|
265 |
*/
|
|
|
266 |
public function setUp() {
|
|
|
267 |
parent::setUp();
|
|
|
268 |
$this->Dbo = ConnectionManager::getDataSource('test');
|
|
|
269 |
if (!($this->Dbo instanceof Sqlserver)) {
|
|
|
270 |
$this->markTestSkipped('Please configure the test datasource to use SQL Server.');
|
|
|
271 |
}
|
|
|
272 |
$this->db = new SqlserverTestDb($this->Dbo->config);
|
|
|
273 |
$this->model = new SqlserverTestModel();
|
|
|
274 |
}
|
|
|
275 |
|
|
|
276 |
/**
|
|
|
277 |
* tearDown method
|
|
|
278 |
*
|
|
|
279 |
* @return void
|
|
|
280 |
*/
|
|
|
281 |
public function tearDown() {
|
|
|
282 |
parent::tearDown();
|
|
|
283 |
unset($this->Dbo);
|
|
|
284 |
unset($this->model);
|
|
|
285 |
}
|
|
|
286 |
|
|
|
287 |
/**
|
|
|
288 |
* testQuoting method
|
|
|
289 |
*
|
|
|
290 |
* @return void
|
|
|
291 |
*/
|
|
|
292 |
public function testQuoting() {
|
|
|
293 |
$expected = "1.2";
|
|
|
294 |
$result = $this->db->value(1.2, 'float');
|
|
|
295 |
$this->assertSame($expected, $result);
|
|
|
296 |
|
|
|
297 |
$expected = "'1,2'";
|
|
|
298 |
$result = $this->db->value('1,2', 'float');
|
|
|
299 |
$this->assertSame($expected, $result);
|
|
|
300 |
|
|
|
301 |
$expected = 'NULL';
|
|
|
302 |
$result = $this->db->value('', 'integer');
|
|
|
303 |
$this->assertSame($expected, $result);
|
|
|
304 |
|
|
|
305 |
$expected = 'NULL';
|
|
|
306 |
$result = $this->db->value('', 'float');
|
|
|
307 |
$this->assertSame($expected, $result);
|
|
|
308 |
|
|
|
309 |
$expected = "''";
|
|
|
310 |
$result = $this->db->value('', 'binary');
|
|
|
311 |
$this->assertSame($expected, $result);
|
|
|
312 |
|
|
|
313 |
$expected = 'NULL';
|
|
|
314 |
$result = $this->db->value(null, 'string');
|
|
|
315 |
$this->assertSame($expected, $result);
|
|
|
316 |
}
|
|
|
317 |
|
|
|
318 |
/**
|
|
|
319 |
* testFields method
|
|
|
320 |
*
|
|
|
321 |
* @return void
|
|
|
322 |
*/
|
|
|
323 |
public function testFields() {
|
|
|
324 |
$fields = array(
|
|
|
325 |
'[SqlserverTestModel].[id] AS [SqlserverTestModel__id]',
|
|
|
326 |
'[SqlserverTestModel].[client_id] AS [SqlserverTestModel__client_id]',
|
|
|
327 |
'[SqlserverTestModel].[name] AS [SqlserverTestModel__name]',
|
|
|
328 |
'[SqlserverTestModel].[login] AS [SqlserverTestModel__login]',
|
|
|
329 |
'[SqlserverTestModel].[passwd] AS [SqlserverTestModel__passwd]',
|
|
|
330 |
'[SqlserverTestModel].[addr_1] AS [SqlserverTestModel__addr_1]',
|
|
|
331 |
'[SqlserverTestModel].[addr_2] AS [SqlserverTestModel__addr_2]',
|
|
|
332 |
'[SqlserverTestModel].[zip_code] AS [SqlserverTestModel__zip_code]',
|
|
|
333 |
'[SqlserverTestModel].[city] AS [SqlserverTestModel__city]',
|
|
|
334 |
'[SqlserverTestModel].[country] AS [SqlserverTestModel__country]',
|
|
|
335 |
'[SqlserverTestModel].[phone] AS [SqlserverTestModel__phone]',
|
|
|
336 |
'[SqlserverTestModel].[fax] AS [SqlserverTestModel__fax]',
|
|
|
337 |
'[SqlserverTestModel].[url] AS [SqlserverTestModel__url]',
|
|
|
338 |
'[SqlserverTestModel].[email] AS [SqlserverTestModel__email]',
|
|
|
339 |
'[SqlserverTestModel].[comments] AS [SqlserverTestModel__comments]',
|
|
|
340 |
'CONVERT(VARCHAR(20), [SqlserverTestModel].[last_login], 20) AS [SqlserverTestModel__last_login]',
|
|
|
341 |
'[SqlserverTestModel].[created] AS [SqlserverTestModel__created]',
|
|
|
342 |
'CONVERT(VARCHAR(20), [SqlserverTestModel].[updated], 20) AS [SqlserverTestModel__updated]'
|
|
|
343 |
);
|
|
|
344 |
|
|
|
345 |
$result = $this->db->fields($this->model);
|
|
|
346 |
$expected = $fields;
|
|
|
347 |
$this->assertEquals($expected, $result);
|
|
|
348 |
|
|
|
349 |
$this->db->clearFieldMappings();
|
|
|
350 |
$result = $this->db->fields($this->model, null, 'SqlserverTestModel.*');
|
|
|
351 |
$expected = $fields;
|
|
|
352 |
$this->assertEquals($expected, $result);
|
|
|
353 |
|
|
|
354 |
$this->db->clearFieldMappings();
|
|
|
355 |
$result = $this->db->fields($this->model, null, array('*', 'AnotherModel.id', 'AnotherModel.name'));
|
|
|
356 |
$expected = array_merge($fields, array(
|
|
|
357 |
'[AnotherModel].[id] AS [AnotherModel__id]',
|
|
|
358 |
'[AnotherModel].[name] AS [AnotherModel__name]'));
|
|
|
359 |
$this->assertEquals($expected, $result);
|
|
|
360 |
|
|
|
361 |
$this->db->clearFieldMappings();
|
|
|
362 |
$result = $this->db->fields($this->model, null, array('*', 'SqlserverClientTestModel.*'));
|
|
|
363 |
$expected = array_merge($fields, array(
|
|
|
364 |
'[SqlserverClientTestModel].[id] AS [SqlserverClientTestModel__id]',
|
|
|
365 |
'[SqlserverClientTestModel].[name] AS [SqlserverClientTestModel__name]',
|
|
|
366 |
'[SqlserverClientTestModel].[email] AS [SqlserverClientTestModel__email]',
|
|
|
367 |
'CONVERT(VARCHAR(20), [SqlserverClientTestModel].[created], 20) AS [SqlserverClientTestModel__created]',
|
|
|
368 |
'CONVERT(VARCHAR(20), [SqlserverClientTestModel].[updated], 20) AS [SqlserverClientTestModel__updated]'));
|
|
|
369 |
$this->assertEquals($expected, $result);
|
|
|
370 |
}
|
|
|
371 |
|
|
|
372 |
/**
|
|
|
373 |
* testDistinctFields method
|
|
|
374 |
*
|
|
|
375 |
* @return void
|
|
|
376 |
*/
|
|
|
377 |
public function testDistinctFields() {
|
|
|
378 |
$result = $this->db->fields($this->model, null, array('DISTINCT Car.country_code'));
|
|
|
379 |
$expected = array('DISTINCT [Car].[country_code] AS [Car__country_code]');
|
|
|
380 |
$this->assertEquals($expected, $result);
|
|
|
381 |
|
|
|
382 |
$result = $this->db->fields($this->model, null, 'DISTINCT Car.country_code');
|
|
|
383 |
$expected = array('DISTINCT [Car].[country_code] AS [Car__country_code]');
|
|
|
384 |
$this->assertEquals($expected, $result);
|
|
|
385 |
}
|
|
|
386 |
|
|
|
387 |
/**
|
|
|
388 |
* testDistinctWithLimit method
|
|
|
389 |
*
|
|
|
390 |
* @return void
|
|
|
391 |
*/
|
|
|
392 |
public function testDistinctWithLimit() {
|
|
|
393 |
$this->db->read($this->model, array(
|
|
|
394 |
'fields' => array('DISTINCT SqlserverTestModel.city', 'SqlserverTestModel.country'),
|
|
|
395 |
'limit' => 5
|
|
|
396 |
));
|
|
|
397 |
$result = $this->db->getLastQuery();
|
|
|
398 |
$this->assertRegExp('/^SELECT DISTINCT TOP 5/', $result);
|
|
|
399 |
}
|
|
|
400 |
|
|
|
401 |
/**
|
|
|
402 |
* testDescribe method
|
|
|
403 |
*
|
|
|
404 |
* @return void
|
|
|
405 |
*/
|
|
|
406 |
public function testDescribe() {
|
|
|
407 |
$SqlserverTableDescription = new SqlserverTestResultIterator(array(
|
|
|
408 |
(object)array(
|
|
|
409 |
'Default' => '((0))',
|
|
|
410 |
'Field' => 'count',
|
|
|
411 |
'Key' => 0,
|
|
|
412 |
'Length' => '4',
|
|
|
413 |
'Null' => 'NO',
|
|
|
414 |
'Type' => 'integer'
|
|
|
415 |
),
|
|
|
416 |
(object)array(
|
|
|
417 |
'Default' => '',
|
|
|
418 |
'Field' => 'body',
|
|
|
419 |
'Key' => 0,
|
|
|
420 |
'Length' => '-1',
|
|
|
421 |
'Null' => 'YES',
|
|
|
422 |
'Type' => 'nvarchar'
|
|
|
423 |
),
|
|
|
424 |
(object)array(
|
|
|
425 |
'Default' => '',
|
|
|
426 |
'Field' => 'published',
|
|
|
427 |
'Key' => 0,
|
|
|
428 |
'Type' => 'datetime2',
|
|
|
429 |
'Length' => 8,
|
|
|
430 |
'Null' => 'YES',
|
|
|
431 |
'Size' => ''
|
|
|
432 |
),
|
|
|
433 |
(object)array(
|
|
|
434 |
'Default' => '',
|
|
|
435 |
'Field' => 'id',
|
|
|
436 |
'Key' => 1,
|
|
|
437 |
'Type' => 'nchar',
|
|
|
438 |
'Length' => 72,
|
|
|
439 |
'Null' => 'NO',
|
|
|
440 |
'Size' => ''
|
|
|
441 |
),
|
|
|
442 |
(object)array(
|
|
|
443 |
'Default' => null,
|
|
|
444 |
'Field' => 'parent_id',
|
|
|
445 |
'Key' => '0',
|
|
|
446 |
'Type' => 'bigint',
|
|
|
447 |
'Length' => 8,
|
|
|
448 |
'Null' => 'YES',
|
|
|
449 |
'Size' => '0',
|
|
|
450 |
),
|
|
|
451 |
));
|
|
|
452 |
$this->db->executeResultsStack = array($SqlserverTableDescription);
|
|
|
453 |
$dummyModel = $this->model;
|
|
|
454 |
$result = $this->db->describe($dummyModel);
|
|
|
455 |
$expected = array(
|
|
|
456 |
'count' => array(
|
|
|
457 |
'type' => 'integer',
|
|
|
458 |
'null' => false,
|
|
|
459 |
'default' => '0',
|
|
|
460 |
'length' => 4
|
|
|
461 |
),
|
|
|
462 |
'body' => array(
|
|
|
463 |
'type' => 'text',
|
|
|
464 |
'null' => true,
|
|
|
465 |
'default' => null,
|
|
|
466 |
'length' => null
|
|
|
467 |
),
|
|
|
468 |
'published' => array(
|
|
|
469 |
'type' => 'datetime',
|
|
|
470 |
'null' => true,
|
|
|
471 |
'default' => '',
|
|
|
472 |
'length' => null
|
|
|
473 |
),
|
|
|
474 |
'id' => array(
|
|
|
475 |
'type' => 'string',
|
|
|
476 |
'null' => false,
|
|
|
477 |
'default' => '',
|
|
|
478 |
'length' => 36,
|
|
|
479 |
'key' => 'primary'
|
|
|
480 |
),
|
|
|
481 |
'parent_id' => array(
|
|
|
482 |
'type' => 'biginteger',
|
|
|
483 |
'null' => true,
|
|
|
484 |
'default' => null,
|
|
|
485 |
'length' => 8,
|
|
|
486 |
),
|
|
|
487 |
);
|
|
|
488 |
$this->assertEquals($expected, $result);
|
|
|
489 |
$this->assertSame($expected['parent_id'], $result['parent_id']);
|
|
|
490 |
}
|
|
|
491 |
|
|
|
492 |
/**
|
|
|
493 |
* testBuildColumn
|
|
|
494 |
*
|
|
|
495 |
* @return void
|
|
|
496 |
*/
|
|
|
497 |
public function testBuildColumn() {
|
|
|
498 |
$column = array('name' => 'id', 'type' => 'integer', 'null' => false, 'default' => '', 'length' => '8', 'key' => 'primary');
|
|
|
499 |
$result = $this->db->buildColumn($column);
|
|
|
500 |
$expected = '[id] int IDENTITY (1, 1) NOT NULL';
|
|
|
501 |
$this->assertEquals($expected, $result);
|
|
|
502 |
|
|
|
503 |
$column = array('name' => 'client_id', 'type' => 'integer', 'null' => false, 'default' => '0', 'length' => '11');
|
|
|
504 |
$result = $this->db->buildColumn($column);
|
|
|
505 |
$expected = '[client_id] int DEFAULT 0 NOT NULL';
|
|
|
506 |
$this->assertEquals($expected, $result);
|
|
|
507 |
|
|
|
508 |
$column = array('name' => 'client_id', 'type' => 'integer', 'null' => true);
|
|
|
509 |
$result = $this->db->buildColumn($column);
|
|
|
510 |
$expected = '[client_id] int NULL';
|
|
|
511 |
$this->assertEquals($expected, $result);
|
|
|
512 |
|
|
|
513 |
// 'name' => 'type' format for columns
|
|
|
514 |
$column = array('type' => 'integer', 'name' => 'client_id');
|
|
|
515 |
$result = $this->db->buildColumn($column);
|
|
|
516 |
$expected = '[client_id] int NULL';
|
|
|
517 |
$this->assertEquals($expected, $result);
|
|
|
518 |
|
|
|
519 |
$column = array('type' => 'string', 'name' => 'name');
|
|
|
520 |
$result = $this->db->buildColumn($column);
|
|
|
521 |
$expected = '[name] nvarchar(255) NULL';
|
|
|
522 |
$this->assertEquals($expected, $result);
|
|
|
523 |
|
|
|
524 |
$column = array('name' => 'name', 'type' => 'string', 'null' => false, 'default' => '', 'length' => '255');
|
|
|
525 |
$result = $this->db->buildColumn($column);
|
|
|
526 |
$expected = '[name] nvarchar(255) DEFAULT \'\' NOT NULL';
|
|
|
527 |
$this->assertEquals($expected, $result);
|
|
|
528 |
|
|
|
529 |
$column = array('name' => 'name', 'type' => 'string', 'null' => false, 'length' => '255');
|
|
|
530 |
$result = $this->db->buildColumn($column);
|
|
|
531 |
$expected = '[name] nvarchar(255) NOT NULL';
|
|
|
532 |
$this->assertEquals($expected, $result);
|
|
|
533 |
|
|
|
534 |
$column = array('name' => 'name', 'type' => 'string', 'null' => false, 'default' => null, 'length' => '255');
|
|
|
535 |
$result = $this->db->buildColumn($column);
|
|
|
536 |
$expected = '[name] nvarchar(255) NOT NULL';
|
|
|
537 |
$this->assertEquals($expected, $result);
|
|
|
538 |
|
|
|
539 |
$column = array('name' => 'name', 'type' => 'string', 'null' => true, 'default' => null, 'length' => '255');
|
|
|
540 |
$result = $this->db->buildColumn($column);
|
|
|
541 |
$expected = '[name] nvarchar(255) NULL';
|
|
|
542 |
$this->assertEquals($expected, $result);
|
|
|
543 |
|
|
|
544 |
$column = array('name' => 'name', 'type' => 'string', 'null' => true, 'default' => '', 'length' => '255');
|
|
|
545 |
$result = $this->db->buildColumn($column);
|
|
|
546 |
$expected = '[name] nvarchar(255) DEFAULT \'\'';
|
|
|
547 |
$this->assertEquals($expected, $result);
|
|
|
548 |
|
|
|
549 |
$column = array('name' => 'body', 'type' => 'text');
|
|
|
550 |
$result = $this->db->buildColumn($column);
|
|
|
551 |
$expected = '[body] nvarchar(MAX)';
|
|
|
552 |
$this->assertEquals($expected, $result);
|
|
|
553 |
|
|
|
554 |
$column = array(
|
|
|
555 |
'name' => 'checked',
|
|
|
556 |
'type' => 'boolean',
|
|
|
557 |
'length' => 10,
|
|
|
558 |
'default' => '1'
|
|
|
559 |
);
|
|
|
560 |
$result = $this->db->buildColumn($column);
|
|
|
561 |
$expected = "[checked] bit DEFAULT '1'";
|
|
|
562 |
$this->assertEquals($expected, $result);
|
|
|
563 |
|
|
|
564 |
$column = array(
|
|
|
565 |
'name' => 'huge',
|
|
|
566 |
'type' => 'biginteger',
|
|
|
567 |
);
|
|
|
568 |
$result = $this->db->buildColumn($column);
|
|
|
569 |
$expected = "[huge] bigint";
|
|
|
570 |
$this->assertEquals($expected, $result);
|
|
|
571 |
}
|
|
|
572 |
|
|
|
573 |
/**
|
|
|
574 |
* testBuildIndex method
|
|
|
575 |
*
|
|
|
576 |
* @return void
|
|
|
577 |
*/
|
|
|
578 |
public function testBuildIndex() {
|
|
|
579 |
$indexes = array(
|
|
|
580 |
'PRIMARY' => array('column' => 'id', 'unique' => 1),
|
|
|
581 |
'client_id' => array('column' => 'client_id', 'unique' => 1)
|
|
|
582 |
);
|
|
|
583 |
$result = $this->db->buildIndex($indexes, 'items');
|
|
|
584 |
$expected = array(
|
|
|
585 |
'PRIMARY KEY ([id])',
|
|
|
586 |
'ALTER TABLE items ADD CONSTRAINT client_id UNIQUE([client_id]);'
|
|
|
587 |
);
|
|
|
588 |
$this->assertEquals($expected, $result);
|
|
|
589 |
|
|
|
590 |
$indexes = array('client_id' => array('column' => 'client_id'));
|
|
|
591 |
$result = $this->db->buildIndex($indexes, 'items');
|
|
|
592 |
$this->assertSame(array(), $result);
|
|
|
593 |
|
|
|
594 |
$indexes = array('client_id' => array('column' => array('client_id', 'period_id'), 'unique' => 1));
|
|
|
595 |
$result = $this->db->buildIndex($indexes, 'items');
|
|
|
596 |
$expected = array('ALTER TABLE items ADD CONSTRAINT client_id UNIQUE([client_id], [period_id]);');
|
|
|
597 |
$this->assertEquals($expected, $result);
|
|
|
598 |
}
|
|
|
599 |
|
|
|
600 |
/**
|
|
|
601 |
* testUpdateAllSyntax method
|
|
|
602 |
*
|
|
|
603 |
* @return void
|
|
|
604 |
*/
|
|
|
605 |
public function testUpdateAllSyntax() {
|
|
|
606 |
$fields = array('SqlserverTestModel.client_id' => '[SqlserverTestModel].[client_id] + 1');
|
|
|
607 |
$conditions = array('SqlserverTestModel.updated <' => date('2009-01-01 00:00:00'));
|
|
|
608 |
$this->db->update($this->model, $fields, null, $conditions);
|
|
|
609 |
|
|
|
610 |
$result = $this->db->getLastQuery();
|
|
|
611 |
$this->assertNotRegExp('/SqlserverTestModel/', $result);
|
|
|
612 |
$this->assertRegExp('/^UPDATE \[sqlserver_test_models\]/', $result);
|
|
|
613 |
$this->assertRegExp('/SET \[client_id\] = \[client_id\] \+ 1/', $result);
|
|
|
614 |
}
|
|
|
615 |
|
|
|
616 |
/**
|
|
|
617 |
* testGetPrimaryKey method
|
|
|
618 |
*
|
|
|
619 |
* @return void
|
|
|
620 |
*/
|
|
|
621 |
public function testGetPrimaryKey() {
|
|
|
622 |
$schema = $this->model->schema();
|
|
|
623 |
|
|
|
624 |
$this->db->describe = $schema;
|
|
|
625 |
$result = $this->db->getPrimaryKey($this->model);
|
|
|
626 |
$this->assertEquals('id', $result);
|
|
|
627 |
|
|
|
628 |
unset($schema['id']['key']);
|
|
|
629 |
$this->db->describe = $schema;
|
|
|
630 |
$result = $this->db->getPrimaryKey($this->model);
|
|
|
631 |
$this->assertNull($result);
|
|
|
632 |
}
|
|
|
633 |
|
|
|
634 |
/**
|
|
|
635 |
* SQL server < 11 doesn't have proper limit/offset support, test that our hack works.
|
|
|
636 |
*
|
|
|
637 |
* @return void
|
|
|
638 |
*/
|
|
|
639 |
public function testLimitOffsetHack() {
|
|
|
640 |
$this->loadFixtures('Author', 'Post', 'User');
|
|
|
641 |
$query = array(
|
|
|
642 |
'limit' => 2,
|
|
|
643 |
'page' => 1,
|
|
|
644 |
'order' => 'User.user ASC',
|
|
|
645 |
);
|
|
|
646 |
$User = ClassRegistry::init('User');
|
|
|
647 |
$results = $User->find('all', $query);
|
|
|
648 |
|
|
|
649 |
$this->assertEquals(2, count($results));
|
|
|
650 |
$this->assertEquals('garrett', $results[0]['User']['user']);
|
|
|
651 |
$this->assertEquals('larry', $results[1]['User']['user']);
|
|
|
652 |
|
|
|
653 |
$query = array(
|
|
|
654 |
'limit' => 2,
|
|
|
655 |
'page' => 2,
|
|
|
656 |
'order' => 'User.user ASC',
|
|
|
657 |
);
|
|
|
658 |
$User = ClassRegistry::init('User');
|
|
|
659 |
$results = $User->find('all', $query);
|
|
|
660 |
|
|
|
661 |
$this->assertEquals(2, count($results));
|
|
|
662 |
$this->assertFalse(isset($results[0][0]));
|
|
|
663 |
$this->assertEquals('mariano', $results[0]['User']['user']);
|
|
|
664 |
$this->assertEquals('nate', $results[1]['User']['user']);
|
|
|
665 |
}
|
|
|
666 |
|
|
|
667 |
/**
|
|
|
668 |
* Test that the return of stored procedures is honoured
|
|
|
669 |
*
|
|
|
670 |
* @return void
|
|
|
671 |
*/
|
|
|
672 |
public function testStoredProcedureReturn() {
|
|
|
673 |
$sql = <<<SQL
|
|
|
674 |
CREATE PROCEDURE cake_test_procedure
|
|
|
675 |
AS
|
|
|
676 |
BEGIN
|
|
|
677 |
RETURN 2;
|
|
|
678 |
END
|
|
|
679 |
SQL;
|
|
|
680 |
$this->Dbo->execute($sql);
|
|
|
681 |
|
|
|
682 |
$sql = <<<SQL
|
|
|
683 |
DECLARE @return_value int
|
|
|
684 |
EXEC @return_value = [cake_test_procedure]
|
|
|
685 |
SELECT 'value' = @return_value
|
|
|
686 |
SQL;
|
|
|
687 |
$query = $this->Dbo->execute($sql);
|
|
|
688 |
$this->Dbo->execute('DROP PROC cake_test_procedure');
|
|
|
689 |
|
|
|
690 |
$result = $query->fetch();
|
|
|
691 |
$this->assertEquals(2, $result['value']);
|
|
|
692 |
}
|
|
|
693 |
|
|
|
694 |
}
|