PostgresTest.php 38 KB

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