PostgresTest.php 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036
  1. <?php
  2. /**
  3. * DboPostgresTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice.
  12. *
  13. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://cakephp.org CakePHP(tm) Project
  15. * @package Cake.Test.Case.Model.Datasource.Database
  16. * @since CakePHP(tm) v 1.2.0
  17. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  18. */
  19. App::uses('Model', 'Model');
  20. App::uses('AppModel', 'Model');
  21. App::uses('Postgres', 'Model/Datasource/Database');
  22. require_once dirname(dirname(dirname(__FILE__))) . DS . 'models.php';
  23. /**
  24. * DboPostgresTestDb class
  25. *
  26. * @package Cake.Test.Case.Model.Datasource.Database
  27. */
  28. class DboPostgresTestDb extends Postgres {
  29. /**
  30. * simulated property
  31. *
  32. * @var array
  33. */
  34. public $simulated = array();
  35. /**
  36. * execute method
  37. *
  38. * @param mixed $sql
  39. * @return void
  40. */
  41. protected function _execute($sql, $params = array(), $prepareOptions = array()) {
  42. $this->simulated[] = $sql;
  43. return null;
  44. }
  45. /**
  46. * getLastQuery method
  47. *
  48. * @return void
  49. */
  50. public function getLastQuery() {
  51. return $this->simulated[count($this->simulated) - 1];
  52. }
  53. }
  54. /**
  55. * PostgresTestModel class
  56. *
  57. * @package Cake.Test.Case.Model.Datasource.Database
  58. */
  59. class PostgresTestModel extends Model {
  60. /**
  61. * useTable property
  62. *
  63. * @var boolean
  64. */
  65. public $useTable = false;
  66. /**
  67. * belongsTo property
  68. *
  69. * @var array
  70. */
  71. public $belongsTo = array(
  72. 'PostgresClientTestModel' => array(
  73. 'foreignKey' => 'client_id'
  74. )
  75. );
  76. /**
  77. * find method
  78. *
  79. * @param mixed $conditions
  80. * @param mixed $fields
  81. * @param mixed $order
  82. * @param mixed $recursive
  83. * @return void
  84. */
  85. public function find($conditions = null, $fields = null, $order = null, $recursive = null) {
  86. return $conditions;
  87. }
  88. /**
  89. * findAll method
  90. *
  91. * @param mixed $conditions
  92. * @param mixed $fields
  93. * @param mixed $order
  94. * @param mixed $recursive
  95. * @return void
  96. */
  97. public function findAll($conditions = null, $fields = null, $order = null, $recursive = null) {
  98. return $conditions;
  99. }
  100. /**
  101. * schema method
  102. *
  103. * @return void
  104. */
  105. public function schema($field = false) {
  106. return array(
  107. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  108. 'client_id' => array('type' => 'integer', 'null' => '', 'default' => '0', 'length' => '11'),
  109. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  110. 'login' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  111. 'passwd' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '255'),
  112. 'addr_1' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '255'),
  113. 'addr_2' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '25'),
  114. 'zip_code' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'),
  115. 'city' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'),
  116. 'country' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'),
  117. 'phone' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'),
  118. 'fax' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'),
  119. 'url' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '255'),
  120. 'email' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'),
  121. 'comments' => array('type' => 'text', 'null' => '1', 'default' => '', 'length' => ''),
  122. 'last_login' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => ''),
  123. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  124. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  125. );
  126. }
  127. }
  128. /**
  129. * PostgresClientTestModel class
  130. *
  131. * @package Cake.Test.Case.Model.Datasource.Database
  132. */
  133. class PostgresClientTestModel extends Model {
  134. /**
  135. * useTable property
  136. *
  137. * @var boolean
  138. */
  139. public $useTable = false;
  140. /**
  141. * schema method
  142. *
  143. * @return void
  144. */
  145. public function schema($field = false) {
  146. return array(
  147. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8', 'key' => 'primary'),
  148. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  149. 'email' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'),
  150. 'created' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => ''),
  151. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  152. );
  153. }
  154. }
  155. /**
  156. * PostgresTest class
  157. *
  158. * @package Cake.Test.Case.Model.Datasource.Database
  159. */
  160. class PostgresTest extends CakeTestCase {
  161. /**
  162. * Do not automatically load fixtures for each test, they will be loaded manually
  163. * using CakeTestCase::loadFixtures
  164. *
  165. * @var boolean
  166. */
  167. public $autoFixtures = false;
  168. /**
  169. * Fixtures
  170. *
  171. * @var object
  172. */
  173. public $fixtures = array('core.user', 'core.binary_test', 'core.comment', 'core.article',
  174. 'core.tag', 'core.articles_tag', 'core.attachment', 'core.person', 'core.post', 'core.author',
  175. 'core.datatype',
  176. );
  177. /**
  178. * Actual DB connection used in testing
  179. *
  180. * @var DboSource
  181. */
  182. public $Dbo = null;
  183. /**
  184. * Simulated DB connection used in testing
  185. *
  186. * @var DboSource
  187. */
  188. public $Dbo2 = null;
  189. /**
  190. * Sets up a Dbo class instance for testing
  191. *
  192. */
  193. public function setUp() {
  194. parent::setUp();
  195. Configure::write('Cache.disable', true);
  196. $this->Dbo = ConnectionManager::getDataSource('test');
  197. $this->skipIf(!($this->Dbo instanceof Postgres));
  198. $this->Dbo2 = new DboPostgresTestDb($this->Dbo->config, false);
  199. $this->model = new PostgresTestModel();
  200. }
  201. /**
  202. * Sets up a Dbo class instance for testing
  203. *
  204. */
  205. public function tearDown() {
  206. parent::tearDown();
  207. Configure::write('Cache.disable', false);
  208. unset($this->Dbo2);
  209. }
  210. /**
  211. * Test field quoting method
  212. *
  213. */
  214. public function testFieldQuoting() {
  215. $fields = array(
  216. '"PostgresTestModel"."id" AS "PostgresTestModel__id"',
  217. '"PostgresTestModel"."client_id" AS "PostgresTestModel__client_id"',
  218. '"PostgresTestModel"."name" AS "PostgresTestModel__name"',
  219. '"PostgresTestModel"."login" AS "PostgresTestModel__login"',
  220. '"PostgresTestModel"."passwd" AS "PostgresTestModel__passwd"',
  221. '"PostgresTestModel"."addr_1" AS "PostgresTestModel__addr_1"',
  222. '"PostgresTestModel"."addr_2" AS "PostgresTestModel__addr_2"',
  223. '"PostgresTestModel"."zip_code" AS "PostgresTestModel__zip_code"',
  224. '"PostgresTestModel"."city" AS "PostgresTestModel__city"',
  225. '"PostgresTestModel"."country" AS "PostgresTestModel__country"',
  226. '"PostgresTestModel"."phone" AS "PostgresTestModel__phone"',
  227. '"PostgresTestModel"."fax" AS "PostgresTestModel__fax"',
  228. '"PostgresTestModel"."url" AS "PostgresTestModel__url"',
  229. '"PostgresTestModel"."email" AS "PostgresTestModel__email"',
  230. '"PostgresTestModel"."comments" AS "PostgresTestModel__comments"',
  231. '"PostgresTestModel"."last_login" AS "PostgresTestModel__last_login"',
  232. '"PostgresTestModel"."created" AS "PostgresTestModel__created"',
  233. '"PostgresTestModel"."updated" AS "PostgresTestModel__updated"'
  234. );
  235. $result = $this->Dbo->fields($this->model);
  236. $expected = $fields;
  237. $this->assertEquals($expected, $result);
  238. $result = $this->Dbo->fields($this->model, null, 'PostgresTestModel.*');
  239. $expected = $fields;
  240. $this->assertEquals($expected, $result);
  241. $result = $this->Dbo->fields($this->model, null, array('*', 'AnotherModel.id', 'AnotherModel.name'));
  242. $expected = array_merge($fields, array(
  243. '"AnotherModel"."id" AS "AnotherModel__id"',
  244. '"AnotherModel"."name" AS "AnotherModel__name"'));
  245. $this->assertEquals($expected, $result);
  246. $result = $this->Dbo->fields($this->model, null, array('*', 'PostgresClientTestModel.*'));
  247. $expected = array_merge($fields, array(
  248. '"PostgresClientTestModel"."id" AS "PostgresClientTestModel__id"',
  249. '"PostgresClientTestModel"."name" AS "PostgresClientTestModel__name"',
  250. '"PostgresClientTestModel"."email" AS "PostgresClientTestModel__email"',
  251. '"PostgresClientTestModel"."created" AS "PostgresClientTestModel__created"',
  252. '"PostgresClientTestModel"."updated" AS "PostgresClientTestModel__updated"'));
  253. $this->assertEquals($expected, $result);
  254. }
  255. /**
  256. * testColumnParsing method
  257. *
  258. * @return void
  259. */
  260. public function testColumnParsing() {
  261. $this->assertEquals('text', $this->Dbo2->column('text'));
  262. $this->assertEquals('date', $this->Dbo2->column('date'));
  263. $this->assertEquals('boolean', $this->Dbo2->column('boolean'));
  264. $this->assertEquals('string', $this->Dbo2->column('character varying'));
  265. $this->assertEquals('time', $this->Dbo2->column('time without time zone'));
  266. $this->assertEquals('datetime', $this->Dbo2->column('timestamp without time zone'));
  267. $result = $this->Dbo2->column('bigint');
  268. $expected = 'biginteger';
  269. $this->assertEquals($expected, $result);
  270. }
  271. /**
  272. * testValueQuoting method
  273. *
  274. * @return void
  275. */
  276. public function testValueQuoting() {
  277. $this->assertEquals("1.200000", $this->Dbo->value(1.2, 'float'));
  278. $this->assertEquals("'1,2'", $this->Dbo->value('1,2', 'float'));
  279. $this->assertEquals("0", $this->Dbo->value('0', 'integer'));
  280. $this->assertEquals('NULL', $this->Dbo->value('', 'integer'));
  281. $this->assertEquals('NULL', $this->Dbo->value('', 'float'));
  282. $this->assertEquals("NULL", $this->Dbo->value('', 'integer', false));
  283. $this->assertEquals("NULL", $this->Dbo->value('', 'float', false));
  284. $this->assertEquals("'0.0'", $this->Dbo->value('0.0', 'float'));
  285. $this->assertEquals("'TRUE'", $this->Dbo->value('t', 'boolean'));
  286. $this->assertEquals("'FALSE'", $this->Dbo->value('f', 'boolean'));
  287. $this->assertEquals("'TRUE'", $this->Dbo->value(true));
  288. $this->assertEquals("'FALSE'", $this->Dbo->value(false));
  289. $this->assertEquals("'t'", $this->Dbo->value('t'));
  290. $this->assertEquals("'f'", $this->Dbo->value('f'));
  291. $this->assertEquals("'TRUE'", $this->Dbo->value('true', 'boolean'));
  292. $this->assertEquals("'FALSE'", $this->Dbo->value('false', 'boolean'));
  293. $this->assertEquals("'FALSE'", $this->Dbo->value('', 'boolean'));
  294. $this->assertEquals("'FALSE'", $this->Dbo->value(0, 'boolean'));
  295. $this->assertEquals("'TRUE'", $this->Dbo->value(1, 'boolean'));
  296. $this->assertEquals("'TRUE'", $this->Dbo->value('1', 'boolean'));
  297. $this->assertEquals("NULL", $this->Dbo->value(null, 'boolean'));
  298. $this->assertEquals("NULL", $this->Dbo->value(array()));
  299. }
  300. /**
  301. * test that localized floats don't cause trouble.
  302. *
  303. * @return void
  304. */
  305. public function testLocalizedFloats() {
  306. $restore = setlocale(LC_NUMERIC, 0);
  307. $this->skipIf(setlocale(LC_NUMERIC, 'de_DE') === false, "The German locale isn't available.");
  308. $result = $this->db->value(3.141593, 'float');
  309. $this->assertEquals("3.141593", $result);
  310. $result = $this->db->value(3.14);
  311. $this->assertEquals("3.140000", $result);
  312. setlocale(LC_NUMERIC, $restore);
  313. }
  314. /**
  315. * test that date and time columns do not generate errors with null and nullish values.
  316. *
  317. * @return void
  318. */
  319. public function testDateAndTimeAsNull() {
  320. $this->assertEquals('NULL', $this->Dbo->value(null, 'date'));
  321. $this->assertEquals('NULL', $this->Dbo->value('', 'date'));
  322. $this->assertEquals('NULL', $this->Dbo->value('', 'datetime'));
  323. $this->assertEquals('NULL', $this->Dbo->value(null, 'datetime'));
  324. $this->assertEquals('NULL', $this->Dbo->value('', 'timestamp'));
  325. $this->assertEquals('NULL', $this->Dbo->value(null, 'timestamp'));
  326. $this->assertEquals('NULL', $this->Dbo->value('', 'time'));
  327. $this->assertEquals('NULL', $this->Dbo->value(null, 'time'));
  328. }
  329. /**
  330. * Tests that different Postgres boolean 'flavors' are properly returned as native PHP booleans
  331. *
  332. * @return void
  333. */
  334. public function testBooleanNormalization() {
  335. $this->assertEquals(true, $this->Dbo2->boolean('t', false));
  336. $this->assertEquals(true, $this->Dbo2->boolean('true', false));
  337. $this->assertEquals(true, $this->Dbo2->boolean('TRUE', false));
  338. $this->assertEquals(true, $this->Dbo2->boolean(true, false));
  339. $this->assertEquals(true, $this->Dbo2->boolean(1, false));
  340. $this->assertEquals(true, $this->Dbo2->boolean(" ", false));
  341. $this->assertEquals(false, $this->Dbo2->boolean('f', false));
  342. $this->assertEquals(false, $this->Dbo2->boolean('false', false));
  343. $this->assertEquals(false, $this->Dbo2->boolean('FALSE', false));
  344. $this->assertEquals(false, $this->Dbo2->boolean(false, false));
  345. $this->assertEquals(false, $this->Dbo2->boolean(0, false));
  346. $this->assertEquals(false, $this->Dbo2->boolean('', false));
  347. }
  348. /**
  349. * test that default -> false in schemas works correctly.
  350. *
  351. * @return void
  352. */
  353. public function testBooleanDefaultFalseInSchema() {
  354. $this->loadFixtures('Datatype');
  355. $model = new Model(array('name' => 'Datatype', 'table' => 'datatypes', 'ds' => 'test'));
  356. $model->create();
  357. $this->assertSame(false, $model->data['Datatype']['bool']);
  358. }
  359. /**
  360. * testLastInsertIdMultipleInsert method
  361. *
  362. * @return void
  363. */
  364. public function testLastInsertIdMultipleInsert() {
  365. $this->loadFixtures('User');
  366. $db1 = ConnectionManager::getDataSource('test');
  367. $table = $db1->fullTableName('users', false);
  368. $password = '5f4dcc3b5aa765d61d8327deb882cf99';
  369. $db1->execute(
  370. "INSERT INTO {$table} (\"user\", password) VALUES ('mariano', '{$password}')"
  371. );
  372. $this->assertEquals(5, $db1->lastInsertId($table));
  373. $db1->execute("INSERT INTO {$table} (\"user\", password) VALUES ('hoge', '{$password}')");
  374. $this->assertEquals(6, $db1->lastInsertId($table));
  375. }
  376. /**
  377. * Tests that column types without default lengths in $columns do not have length values
  378. * applied when generating schemas.
  379. *
  380. * @return void
  381. */
  382. public function testColumnUseLength() {
  383. $result = array('name' => 'foo', 'type' => 'string', 'length' => 100, 'default' => 'FOO');
  384. $expected = '"foo" varchar(100) DEFAULT \'FOO\'';
  385. $this->assertEquals($expected, $this->Dbo->buildColumn($result));
  386. $result = array('name' => 'foo', 'type' => 'text', 'length' => 100, 'default' => 'FOO');
  387. $expected = '"foo" text DEFAULT \'FOO\'';
  388. $this->assertEquals($expected, $this->Dbo->buildColumn($result));
  389. }
  390. /**
  391. * Tests that binary data is escaped/unescaped properly on reads and writes
  392. *
  393. * @return void
  394. */
  395. public function testBinaryDataIntegrity() {
  396. $this->loadFixtures('BinaryTest');
  397. $data = '%PDF-1.3
  398. %ƒÂÚÂÎßÛ†–ƒ∆
  399. 4 0 obj
  400. << /Length 5 0 R /Filter /FlateDecode >>
  401. stream
  402. xµYMì€∆Ω„WÃ%)nï0¯îâ-«é]Q"πXµáÿ•Ip - P V,]Ú#c˚ˇ‰ut¥†∏Ti9 Ü=”›Ø_˜4>à∑‚Épcé¢Pxæ®2q\'
  403. 1UªbU ᡒ+ö«√[ıµ⁄ão"R∑"HiGæä€(å≠≈^Ãøsm?YlƒÃõªfi‹âEÚB&‚Î◊7bÒ^¸m°÷˛?2±Øs“fiu#®U√ˇú÷g¥C;ä")n})JºIÔ3ËSnÑÎ¥≤ıD∆¢∂Msx1üèG˚±Œ™⁄>¶ySïufØ ˝¸?UπÃã√6flÌÚC=øK?˝…s
  404. ˛§¯ˇ:-˜ò7€ÓFæ∂∑Õ˛∆“V’>ılflëÅd«ÜQdI ›ÎB%W¿ΩıÉn~h vêCS>«é˛(ØôK!€¡zB!√
  405. [œÜ"ûß ·iH¸[Àºæ∑¯¡L,ÀÚAlS∫ˆ=∫Œ≤cÄr&ˆÈ:√ÿ£˚È«4fl•À]vc›bÅôÿî=siXe4/¡p]ã]ôÆIœ™ Ωflà_ƒ‚G?«7 ùÿ ı¯K4ïIpV◊÷·\'éµóªÚæ>î
  406. ;›sú!2fl¬F•/f∑j£
  407. dw"IÊÜπ<ôÿˆ%IG1ytÛDflXg|Éòa§˜}C˛¿ÿe°G´Ú±jÍm~¿/∂hã<#-¥•ıùe87€t˜õ6w}´{æ
  408. m‹ê– ∆¡ 6⁄\
  409. rAÀBùZ3aË‚r$G·$ó0Ñ üâUY4È™¡%C∑Ÿ2rc<Iõ-cï.
  410. [ŒöâFA†É‡+QglMÉîÉÄúÌ|¸»#x7¥«MgVÎ-GGÚ• I?Á‘”Lzw∞pHů◊nefqCî.nÕeè∆ÿÛy¡˙fb≤üŒHÜAëÕNq=´@ ’cQdÖúAÉIqñŸ˘+2&∏ Àù.gÅ‚ƒœ3EPƒOi—‰:>ÍCäı
  411. =Õec=ëR˝”eñ=<V$ì˙+x+¢ïÒÕ<àeWå»–˚∫Õ d§&£àf ]fPA´âtënöå∏◊ó „Ë@∆≠K´÷˘}a_CI˚©yòHg,ôSSVìBƒl4 L.ÈY…á,2∂íäÙ.$ó¸CäŸ*€óy
  412. π?G,_√·ÆÎç=^Vkvo±ó{§ƒ2»±¨Ïüo»ëD-ãé fió¥cVÙ\'™G~\'p¢%* ã˚÷
  413. ªºnh˚ºO^∏…®[Ó“‚ÅfıÌ≥∫F!Eœ(π∑T6`¬tΩÆ0ì»rTÎ`»Ñ«
  414. ]≈åp˝)=¿Ô0∆öVÂmˇˆ„ø~¯ÁÔ∏b*fc»‡Îı„Ú}∆tœs∂Y∫ÜaÆ˙X∏~<ÿ·Ù vé1‹p¿TD∆ÔîÄ“úhˆ*Ú€îe)K –p¨ÚJ3Ÿ∞ã>ÊuNê°“√Ü ‹Ê9iÙ0˙AAEÍ ˙`∂£\'ûce•åƒX›ŸÁ´1SK{qdá"tÏ[wQ#SµBe∞∑µó…ÌV`B"Ñ≥„!è_Óφ-º*ºú¿Ë0ˆeê∂´ë+HFj…‡zvHÓN|ÔL÷ûñ3õÜ$z%sá…pÎóV38âs Çoµ•ß3†<9B·¨û~¢3)ÂxóÿÁCÕòÆ ∫Í=»ÿSπS;∆~±êÆTEp∑óÈ÷ÀuìDHÈ $ÉõæÜjû§"≤ÃONM®RËíRr{õS ∏Ê™op±W;ÂUÔ P∫kÔˇflTæ∑óflË” ÆC©Ô[≥◊HÁ˚¨hê"ÆbF?ú%h˙ˇ4xèÕ(ó2ÙáíM])Ñd|=fë-cI0ñL¢kÖêk‰Rƒ«ıÄWñ8mO3∏&√æËX¯Hó—ì]yF2»–˜ádàà‡‹Çο„≥7mªHAS∑¶.;Œx(1} _kd©.fidç48M\'àáªCp^Krí<ɉXÓıïl!Ì$N<ı∞B»G]…∂Ó¯>˛ÔbõÒπÀ•:ôO<j∂™œ%âÏ—>@È$pÖu‹Ê´-QqV ?V≥JÆÍqÛX8(lπï@zgÖ}Fe<ˇ‡Sñ“ÿ˜ê?6‡L∫Oß~µ –?ËeäÚ®YîÕ =Ü=¢DÁu*GvBk;)L¬N«î:flö∂≠ÇΩq„Ñm하Ë∂‚"û≥§:±≤i^ΩÑ!)Wıyŧô á„RÄ÷Òôc’≠—s™rı‚Pdêãh˘ßHVç5fifiÈF€çÌÛuçÖ/M=gëµ±ÿGû1coÔuñæ‘z®. õ∑7ÉÏÜÆ,°’H†ÍÉÌ∂7e º® íˆ⁄◊øNWK”ÂYµ‚ñé;µ¶gV-fl>µtË¥áßN2 ¯¶BaP-)eW.àôt^∏1›C∑Ö?L„&”5’4jvã–ªZ ÷+4% ´0l…»ú^°´© ûiπ∑é®óܱÒÿ‰ïˆÌ–dˆ◊Æ19rQ=Í|ı•rMæ¬;ò‰Y‰é9.” ‹˝V«ã¯∏,+ë®j*¡·/';
  415. $model = new AppModel(array('name' => 'BinaryTest', 'ds' => 'test'));
  416. $model->save(compact('data'));
  417. $result = $model->find('first');
  418. $this->assertEquals($data, $result['BinaryTest']['data']);
  419. }
  420. /**
  421. * Tests passing PostgreSQL regular expression operators when building queries
  422. *
  423. * @return void
  424. */
  425. public function testRegexpOperatorConditionsParsing() {
  426. $this->assertSame(' WHERE "name" ~ \'[a-z_]+\'', $this->Dbo->conditions(array('name ~' => '[a-z_]+')));
  427. $this->assertSame(' WHERE "name" ~* \'[a-z_]+\'', $this->Dbo->conditions(array('name ~*' => '[a-z_]+')));
  428. $this->assertSame(' WHERE "name" !~ \'[a-z_]+\'', $this->Dbo->conditions(array('name !~' => '[a-z_]+')));
  429. $this->assertSame(' WHERE "name" !~* \'[a-z_]+\'', $this->Dbo->conditions(array('name !~*' => '[a-z_]+')));
  430. }
  431. /**
  432. * Tests the syntax of generated schema indexes
  433. *
  434. * @return void
  435. */
  436. public function testSchemaIndexSyntax() {
  437. $schema = new CakeSchema();
  438. $schema->tables = array('i18n' => array(
  439. 'id' => array(
  440. 'type' => 'integer', 'null' => false, 'default' => null,
  441. 'length' => 10, 'key' => 'primary'
  442. ),
  443. 'locale' => array('type' => 'string', 'null' => false, 'length' => 6, 'key' => 'index'),
  444. 'model' => array('type' => 'string', 'null' => false, 'key' => 'index'),
  445. 'foreign_key' => array(
  446. 'type' => 'integer', 'null' => false, 'length' => 10, 'key' => 'index'
  447. ),
  448. 'field' => array('type' => 'string', 'null' => false, 'key' => 'index'),
  449. 'content' => array('type' => 'text', 'null' => true, 'default' => null),
  450. 'indexes' => array(
  451. 'PRIMARY' => array('column' => 'id', 'unique' => 1),
  452. 'locale' => array('column' => 'locale', 'unique' => 0),
  453. 'model' => array('column' => 'model', 'unique' => 0),
  454. 'row_id' => array('column' => 'foreign_key', 'unique' => 0),
  455. 'field' => array('column' => 'field', 'unique' => 0)
  456. )
  457. ));
  458. $result = $this->Dbo->createSchema($schema);
  459. $this->assertNotRegExp('/^CREATE INDEX(.+);,$/', $result);
  460. }
  461. /**
  462. * testCakeSchema method
  463. *
  464. * Test that schema generated postgresql queries are valid. ref #5696
  465. * Check that the create statement for a schema generated table is the same as the original sql
  466. *
  467. * @return void
  468. */
  469. public function testCakeSchema() {
  470. $db1 = ConnectionManager::getDataSource('test');
  471. $db1->cacheSources = false;
  472. $db1->rawQuery('CREATE TABLE ' . $db1->fullTableName('datatype_tests') . ' (
  473. id serial NOT NULL,
  474. "varchar" character varying(40) NOT NULL,
  475. "full_length" character varying NOT NULL,
  476. "huge_int" bigint NOT NULL,
  477. "timestamp" timestamp without time zone,
  478. "date" date,
  479. CONSTRAINT test_data_types_pkey PRIMARY KEY (id)
  480. )');
  481. $schema = new CakeSchema(array('connection' => 'test'));
  482. $result = $schema->read(array(
  483. 'connection' => 'test',
  484. 'models' => array('DatatypeTest')
  485. ));
  486. $schema->tables = array(
  487. 'datatype_tests' => $result['tables']['missing']['datatype_tests']
  488. );
  489. $result = $db1->createSchema($schema, 'datatype_tests');
  490. $this->assertNotRegExp('/timestamp DEFAULT/', $result);
  491. $this->assertRegExp('/\"full_length\"\s*text\s.*,/', $result);
  492. $this->assertContains('timestamp ,', $result);
  493. $this->assertContains('"huge_int" bigint NOT NULL,', $result);
  494. $db1->query('DROP TABLE ' . $db1->fullTableName('datatype_tests'));
  495. $db1->query($result);
  496. $result2 = $schema->read(array(
  497. 'connection' => 'test',
  498. 'models' => array('DatatypeTest')
  499. ));
  500. $schema->tables = array('datatype_tests' => $result2['tables']['missing']['datatype_tests']);
  501. $result2 = $db1->createSchema($schema, 'datatype_tests');
  502. $this->assertEquals($result, $result2);
  503. $db1->query('DROP TABLE ' . $db1->fullTableName('datatype_tests'));
  504. }
  505. /**
  506. * Test index generation from table info.
  507. *
  508. * @return void
  509. */
  510. public function testIndexGeneration() {
  511. $name = $this->Dbo->fullTableName('index_test', false, false);
  512. $this->Dbo->query('CREATE TABLE ' . $name . ' ("id" serial NOT NULL PRIMARY KEY, "bool" integer, "small_char" varchar(50), "description" varchar(40) )');
  513. $this->Dbo->query('CREATE INDEX pointless_bool ON ' . $name . '("bool")');
  514. $this->Dbo->query('CREATE UNIQUE INDEX char_index ON ' . $name . '("small_char")');
  515. $expected = array(
  516. 'PRIMARY' => array('unique' => true, 'column' => 'id'),
  517. 'pointless_bool' => array('unique' => false, 'column' => 'bool'),
  518. 'char_index' => array('unique' => true, 'column' => 'small_char'),
  519. );
  520. $result = $this->Dbo->index($name);
  521. $this->Dbo->query('DROP TABLE ' . $name);
  522. $this->assertEquals($expected, $result);
  523. $name = $this->Dbo->fullTableName('index_test_2', false, false);
  524. $this->Dbo->query('CREATE TABLE ' . $name . ' ("id" serial NOT NULL PRIMARY KEY, "bool" integer, "small_char" varchar(50), "description" varchar(40) )');
  525. $this->Dbo->query('CREATE UNIQUE INDEX multi_col ON ' . $name . '("small_char", "bool")');
  526. $expected = array(
  527. 'PRIMARY' => array('unique' => true, 'column' => 'id'),
  528. 'multi_col' => array('unique' => true, 'column' => array('small_char', 'bool')),
  529. );
  530. $result = $this->Dbo->index($name);
  531. $this->Dbo->query('DROP TABLE ' . $name);
  532. $this->assertEquals($expected, $result);
  533. }
  534. /**
  535. * Test the alterSchema capabilities of postgres
  536. *
  537. * @return void
  538. */
  539. public function testAlterSchema() {
  540. $Old = new CakeSchema(array(
  541. 'connection' => 'test',
  542. 'name' => 'AlterPosts',
  543. 'alter_posts' => array(
  544. 'id' => array('type' => 'integer', 'key' => 'primary'),
  545. 'author_id' => array('type' => 'integer', 'null' => false),
  546. 'title' => array('type' => 'string', 'null' => true),
  547. 'body' => array('type' => 'text'),
  548. 'published' => array('type' => 'string', 'length' => 1, 'default' => 'N'),
  549. 'created' => array('type' => 'datetime'),
  550. 'updated' => array('type' => 'datetime'),
  551. )
  552. ));
  553. $this->Dbo->query($this->Dbo->createSchema($Old));
  554. $New = new CakeSchema(array(
  555. 'connection' => 'test',
  556. 'name' => 'AlterPosts',
  557. 'alter_posts' => array(
  558. 'id' => array('type' => 'integer', 'key' => 'primary'),
  559. 'author_id' => array('type' => 'integer', 'null' => true),
  560. 'title' => array('type' => 'string', 'null' => false, 'default' => 'my title'),
  561. 'body' => array('type' => 'string', 'length' => 500),
  562. 'status' => array('type' => 'integer', 'length' => 3, 'default' => 1),
  563. 'created' => array('type' => 'datetime'),
  564. 'updated' => array('type' => 'datetime'),
  565. )
  566. ));
  567. $this->Dbo->query($this->Dbo->alterSchema($New->compare($Old), 'alter_posts'));
  568. $model = new CakeTestModel(array('table' => 'alter_posts', 'ds' => 'test'));
  569. $result = $model->schema();
  570. $this->assertTrue(isset($result['status']));
  571. $this->assertFalse(isset($result['published']));
  572. $this->assertEquals('string', $result['body']['type']);
  573. $this->assertEquals(1, $result['status']['default']);
  574. $this->assertEquals(true, $result['author_id']['null']);
  575. $this->assertEquals(false, $result['title']['null']);
  576. $this->Dbo->query($this->Dbo->dropSchema($New));
  577. $New = new CakeSchema(array(
  578. 'connection' => 'test_suite',
  579. 'name' => 'AlterPosts',
  580. 'alter_posts' => array(
  581. 'id' => array('type' => 'string', 'length' => 36, 'key' => 'primary'),
  582. 'author_id' => array('type' => 'integer', 'null' => false),
  583. 'title' => array('type' => 'string', 'null' => true),
  584. 'body' => array('type' => 'text'),
  585. 'published' => array('type' => 'string', 'length' => 1, 'default' => 'N'),
  586. 'created' => array('type' => 'datetime'),
  587. 'updated' => array('type' => 'datetime'),
  588. )
  589. ));
  590. $result = $this->Dbo->alterSchema($New->compare($Old), 'alter_posts');
  591. $this->assertNotRegExp('/varchar\(36\) NOT NULL/i', $result);
  592. }
  593. /**
  594. * Test the alter index capabilities of postgres
  595. *
  596. * @return void
  597. */
  598. public function testAlterIndexes() {
  599. $this->Dbo->cacheSources = false;
  600. $schema1 = new CakeSchema(array(
  601. 'name' => 'AlterTest1',
  602. 'connection' => 'test',
  603. 'altertest' => array(
  604. 'id' => array('type' => 'integer', 'null' => false, 'default' => 0),
  605. 'name' => array('type' => 'string', 'null' => false, 'length' => 50),
  606. 'group1' => array('type' => 'integer', 'null' => true),
  607. 'group2' => array('type' => 'integer', 'null' => true)
  608. )
  609. ));
  610. $this->Dbo->rawQuery($this->Dbo->createSchema($schema1));
  611. $schema2 = new CakeSchema(array(
  612. 'name' => 'AlterTest2',
  613. 'connection' => 'test',
  614. 'altertest' => array(
  615. 'id' => array('type' => 'integer', 'null' => false, 'default' => 0),
  616. 'name' => array('type' => 'string', 'null' => false, 'length' => 50),
  617. 'group1' => array('type' => 'integer', 'null' => true),
  618. 'group2' => array('type' => 'integer', 'null' => true),
  619. 'indexes' => array(
  620. 'name_idx' => array('unique' => false, 'column' => 'name'),
  621. 'group_idx' => array('unique' => false, 'column' => 'group1'),
  622. 'compound_idx' => array('unique' => false, 'column' => array('group1', 'group2')),
  623. 'PRIMARY' => array('unique' => true, 'column' => 'id')
  624. )
  625. )
  626. ));
  627. $this->Dbo->query($this->Dbo->alterSchema($schema2->compare($schema1)));
  628. $indexes = $this->Dbo->index('altertest');
  629. $this->assertEquals($schema2->tables['altertest']['indexes'], $indexes);
  630. // Change three indexes, delete one and add another one
  631. $schema3 = new CakeSchema(array(
  632. 'name' => 'AlterTest3',
  633. 'connection' => 'test',
  634. 'altertest' => array(
  635. 'id' => array('type' => 'integer', 'null' => false, 'default' => 0),
  636. 'name' => array('type' => 'string', 'null' => false, 'length' => 50),
  637. 'group1' => array('type' => 'integer', 'null' => true),
  638. 'group2' => array('type' => 'integer', 'null' => true),
  639. 'indexes' => array(
  640. 'name_idx' => array('unique' => true, 'column' => 'name'),
  641. 'group_idx' => array('unique' => false, 'column' => 'group2'),
  642. 'compound_idx' => array('unique' => false, 'column' => array('group2', 'group1')),
  643. 'another_idx' => array('unique' => false, 'column' => array('group1', 'name')))
  644. )));
  645. $this->Dbo->query($this->Dbo->alterSchema($schema3->compare($schema2)));
  646. $indexes = $this->Dbo->index('altertest');
  647. $this->assertEquals($schema3->tables['altertest']['indexes'], $indexes);
  648. // Compare us to ourself.
  649. $this->assertEquals(array(), $schema3->compare($schema3));
  650. // Drop the indexes
  651. $this->Dbo->query($this->Dbo->alterSchema($schema1->compare($schema3)));
  652. $indexes = $this->Dbo->index('altertest');
  653. $this->assertEquals(array(), $indexes);
  654. $this->Dbo->query($this->Dbo->dropSchema($schema1));
  655. }
  656. /**
  657. * Test the alterSchema RENAME statements
  658. *
  659. * @return void
  660. */
  661. public function testAlterSchemaRenameTo() {
  662. $query = $this->Dbo->alterSchema(array(
  663. 'posts' => array(
  664. 'change' => array(
  665. 'title' => array('name' => 'subject', 'type' => 'string', 'null' => false)
  666. )
  667. )
  668. ));
  669. $this->assertContains('RENAME "title" TO "subject";', $query);
  670. $this->assertContains('ALTER COLUMN "subject" TYPE', $query);
  671. $this->assertNotContains(";\n\tALTER COLUMN \"subject\" TYPE", $query);
  672. $this->assertNotContains('ALTER COLUMN "title" TYPE "subject"', $query);
  673. }
  674. /**
  675. * Test it is possible to use virtual field with postgresql
  676. *
  677. * @return void
  678. */
  679. public function testVirtualFields() {
  680. $this->loadFixtures('Article', 'Comment', 'User', 'Attachment', 'Tag', 'ArticlesTag');
  681. $Article = new Article;
  682. $Article->virtualFields = array(
  683. 'next_id' => 'Article.id + 1',
  684. 'complex' => 'Article.title || Article.body',
  685. 'functional' => 'COALESCE(User.user, Article.title)',
  686. 'subquery' => 'SELECT count(*) FROM ' . $Article->Comment->table
  687. );
  688. $result = $Article->find('first');
  689. $this->assertEquals(2, $result['Article']['next_id']);
  690. $this->assertEquals($result['Article']['complex'], $result['Article']['title'] . $result['Article']['body']);
  691. $this->assertEquals($result['Article']['functional'], $result['User']['user']);
  692. $this->assertEquals(6, $result['Article']['subquery']);
  693. }
  694. /**
  695. * Test that virtual fields work with SQL constants
  696. *
  697. * @return void
  698. */
  699. public function testVirtualFieldAsAConstant() {
  700. $this->loadFixtures('Article', 'Comment');
  701. $Article = ClassRegistry::init('Article');
  702. $Article->virtualFields = array(
  703. 'empty' => "NULL",
  704. 'number' => 43,
  705. 'truth' => 'TRUE'
  706. );
  707. $result = $Article->find('first');
  708. $this->assertNull($result['Article']['empty']);
  709. $this->assertTrue($result['Article']['truth']);
  710. $this->assertEquals(43, $result['Article']['number']);
  711. }
  712. /**
  713. * Tests additional order options for postgres
  714. *
  715. * @return void
  716. */
  717. public function testOrderAdditionalParams() {
  718. $result = $this->Dbo->order(array('title' => 'DESC NULLS FIRST', 'body' => 'DESC'));
  719. $expected = ' ORDER BY "title" DESC NULLS FIRST, "body" DESC';
  720. $this->assertEquals($expected, $result);
  721. }
  722. /**
  723. * Test it is possible to do a SELECT COUNT(DISTINCT Model.field)
  724. * query in postgres and it gets correctly quoted
  725. *
  726. * @return void
  727. */
  728. public function testQuoteDistinctInFunction() {
  729. $this->loadFixtures('Article');
  730. $Article = new Article;
  731. $result = $this->Dbo->fields($Article, null, array('COUNT(DISTINCT Article.id)'));
  732. $expected = array('COUNT(DISTINCT "Article"."id")');
  733. $this->assertEquals($expected, $result);
  734. $result = $this->Dbo->fields($Article, null, array('COUNT(DISTINCT id)'));
  735. $expected = array('COUNT(DISTINCT "id")');
  736. $this->assertEquals($expected, $result);
  737. $result = $this->Dbo->fields($Article, null, array('COUNT(DISTINCT FUNC(id))'));
  738. $expected = array('COUNT(DISTINCT FUNC("id"))');
  739. $this->assertEquals($expected, $result);
  740. }
  741. /**
  742. * test that saveAll works even with conditions that lack a model name.
  743. *
  744. * @return void
  745. */
  746. public function testUpdateAllWithNonQualifiedConditions() {
  747. $this->loadFixtures('Article');
  748. $Article = new Article();
  749. $result = $Article->updateAll(array('title' => "'Awesome'"), array('title' => 'Third Article'));
  750. $this->assertTrue($result);
  751. $result = $Article->find('count', array(
  752. 'conditions' => array('Article.title' => 'Awesome')
  753. ));
  754. $this->assertEquals(1, $result, 'Article count is wrong or fixture has changed.');
  755. }
  756. /**
  757. * test alterSchema on two tables.
  758. *
  759. * @return void
  760. */
  761. public function testAlteringTwoTables() {
  762. $schema1 = new CakeSchema(array(
  763. 'name' => 'AlterTest1',
  764. 'connection' => 'test',
  765. 'altertest' => array(
  766. 'id' => array('type' => 'integer', 'null' => false, 'default' => 0),
  767. 'name' => array('type' => 'string', 'null' => false, 'length' => 50),
  768. ),
  769. 'other_table' => array(
  770. 'id' => array('type' => 'integer', 'null' => false, 'default' => 0),
  771. 'name' => array('type' => 'string', 'null' => false, 'length' => 50),
  772. )
  773. ));
  774. $schema2 = new CakeSchema(array(
  775. 'name' => 'AlterTest1',
  776. 'connection' => 'test',
  777. 'altertest' => array(
  778. 'id' => array('type' => 'integer', 'null' => false, 'default' => 0),
  779. 'field_two' => array('type' => 'string', 'null' => false, 'length' => 50),
  780. ),
  781. 'other_table' => array(
  782. 'id' => array('type' => 'integer', 'null' => false, 'default' => 0),
  783. 'field_two' => array('type' => 'string', 'null' => false, 'length' => 50),
  784. )
  785. ));
  786. $result = $this->db->alterSchema($schema2->compare($schema1));
  787. $this->assertEquals(2, substr_count($result, 'field_two'), 'Too many fields');
  788. $this->assertFalse(strpos(';ALTER', $result), 'Too many semi colons');
  789. }
  790. /**
  791. * test encoding setting.
  792. *
  793. * @return void
  794. */
  795. public function testEncoding() {
  796. $result = $this->Dbo->setEncoding('UTF8');
  797. $this->assertTrue($result);
  798. $result = $this->Dbo->getEncoding();
  799. $this->assertEquals('UTF8', $result);
  800. $result = $this->Dbo->setEncoding('EUC_JP'); /* 'EUC_JP' is right character code name in PostgreSQL */
  801. $this->assertTrue($result);
  802. $result = $this->Dbo->getEncoding();
  803. $this->assertEquals('EUC_JP', $result);
  804. }
  805. /**
  806. * Test truncate with a mock.
  807. *
  808. * @return void
  809. */
  810. public function testTruncateStatements() {
  811. $this->loadFixtures('Article', 'User');
  812. $db = ConnectionManager::getDatasource('test');
  813. $schema = $db->config['schema'];
  814. $Article = new Article();
  815. $this->Dbo = $this->getMock('Postgres', array('execute'), array($db->config));
  816. $this->Dbo->expects($this->at(0))->method('execute')
  817. ->with("DELETE FROM \"$schema\".\"articles\"");
  818. $this->Dbo->truncate($Article);
  819. $this->Dbo->expects($this->at(0))->method('execute')
  820. ->with("DELETE FROM \"$schema\".\"articles\"");
  821. $this->Dbo->truncate('articles');
  822. // #2355: prevent duplicate prefix
  823. $this->Dbo->config['prefix'] = 'tbl_';
  824. $Article->tablePrefix = 'tbl_';
  825. $this->Dbo->expects($this->at(0))->method('execute')
  826. ->with("DELETE FROM \"$schema\".\"tbl_articles\"");
  827. $this->Dbo->truncate($Article);
  828. $this->Dbo->expects($this->at(0))->method('execute')
  829. ->with("DELETE FROM \"$schema\".\"tbl_articles\"");
  830. $this->Dbo->truncate('articles');
  831. }
  832. /**
  833. * Test nested transaction
  834. *
  835. * @return void
  836. */
  837. public function testNestedTransaction() {
  838. $this->Dbo->useNestedTransactions = true;
  839. $this->skipIf($this->Dbo->nestedTransactionSupported() === false, 'The Postgres server do not support nested transaction');
  840. $this->loadFixtures('Article');
  841. $model = new Article();
  842. $model->hasOne = $model->hasMany = $model->belongsTo = $model->hasAndBelongsToMany = array();
  843. $model->cacheQueries = false;
  844. $this->Dbo->cacheMethods = false;
  845. $this->assertTrue($this->Dbo->begin());
  846. $this->assertNotEmpty($model->read(null, 1));
  847. $this->assertTrue($this->Dbo->begin());
  848. $this->assertTrue($model->delete(1));
  849. $this->assertEmpty($model->read(null, 1));
  850. $this->assertTrue($this->Dbo->rollback());
  851. $this->assertNotEmpty($model->read(null, 1));
  852. $this->assertTrue($this->Dbo->begin());
  853. $this->assertTrue($model->delete(1));
  854. $this->assertEmpty($model->read(null, 1));
  855. $this->assertTrue($this->Dbo->commit());
  856. $this->assertEmpty($model->read(null, 1));
  857. $this->assertTrue($this->Dbo->rollback());
  858. $this->assertNotEmpty($model->read(null, 1));
  859. }
  860. public function testResetSequence() {
  861. $model = new Article();
  862. $table = $this->Dbo->fullTableName($model, false);
  863. $fields = array(
  864. 'id', 'user_id', 'title', 'body', 'published',
  865. );
  866. $values = array(
  867. array(1, 1, 'test', 'first post', false),
  868. array(2, 1, 'test 2', 'second post post', false),
  869. );
  870. $this->Dbo->insertMulti($table, $fields, $values);
  871. $sequence = $this->Dbo->getSequence($table);
  872. $result = $this->Dbo->rawQuery("SELECT nextval('$sequence')");
  873. $original = $result->fetch(PDO::FETCH_ASSOC);
  874. $this->assertTrue($this->Dbo->resetSequence($table, 'id'));
  875. $result = $this->Dbo->rawQuery("SELECT currval('$sequence')");
  876. $new = $result->fetch(PDO::FETCH_ASSOC);
  877. $this->assertTrue($new['currval'] > $original['nextval'], 'Sequence did not update');
  878. }
  879. public function testSettings() {
  880. Configure::write('Cache.disable', true);
  881. $this->Dbo = ConnectionManager::getDataSource('test');
  882. $this->skipIf(!($this->Dbo instanceof Postgres));
  883. $config2 = $this->Dbo->config;
  884. $config2['settings']['datestyle'] = 'sql, dmy';
  885. ConnectionManager::create('test2', $config2);
  886. $dbo2 = new Postgres($config2, true);
  887. $expected = array(array('r' => date('d/m/Y')));
  888. $r = $dbo2->fetchRow('SELECT now()::date AS "r"');
  889. $this->assertEquals($expected, $r);
  890. $dbo2->execute('SET DATESTYLE TO ISO');
  891. $dbo2->disconnect();
  892. }
  893. /**
  894. * Test the limit function.
  895. *
  896. * @return void
  897. */
  898. public function testLimit() {
  899. $db = $this->Dbo;
  900. $result = $db->limit('0');
  901. $this->assertNull($result);
  902. $result = $db->limit('10');
  903. $this->assertEquals(' LIMIT 10', $result);
  904. $result = $db->limit('FARTS', 'BOOGERS');
  905. $this->assertEquals(' LIMIT 0 OFFSET 0', $result);
  906. $result = $db->limit(20, 10);
  907. $this->assertEquals(' LIMIT 20 OFFSET 10', $result);
  908. $result = $db->limit(10, 300000000000000000000000000000);
  909. $scientificNotation = sprintf('%.1E', 300000000000000000000000000000);
  910. $this->assertNotContains($scientificNotation, $result);
  911. }
  912. }