ConnectionTest.php 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice.
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP(tm) Project
  13. * @since 3.0.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Test\TestCase\Database;
  17. use Cake\Cache\Engine\NullEngine;
  18. use Cake\Collection\Collection;
  19. use Cake\Core\App;
  20. use Cake\Database\Connection;
  21. use Cake\Database\Driver;
  22. use Cake\Database\Driver\Mysql;
  23. use Cake\Database\Driver\Sqlite;
  24. use Cake\Database\Driver\Sqlserver;
  25. use Cake\Database\Exception\MissingConnectionException;
  26. use Cake\Database\Exception\MissingDriverException;
  27. use Cake\Database\Exception\MissingExtensionException;
  28. use Cake\Database\Exception\NestedTransactionRollbackException;
  29. use Cake\Database\Log\LoggingStatement;
  30. use Cake\Database\Log\QueryLogger;
  31. use Cake\Database\Schema\CachedCollection;
  32. use Cake\Database\StatementInterface;
  33. use Cake\Datasource\ConnectionManager;
  34. use Cake\Log\Log;
  35. use Cake\TestSuite\TestCase;
  36. use DateTime;
  37. use Exception;
  38. use InvalidArgumentException;
  39. use PDO;
  40. use ReflectionMethod;
  41. use ReflectionProperty;
  42. /**
  43. * Tests Connection class
  44. */
  45. class ConnectionTest extends TestCase
  46. {
  47. /**
  48. * @var array<string>
  49. */
  50. protected array $fixtures = ['core.Things'];
  51. /**
  52. * Where the NestedTransactionRollbackException was created.
  53. *
  54. * @var int
  55. */
  56. protected $rollbackSourceLine = -1;
  57. /**
  58. * Internal states of nested transaction.
  59. *
  60. * @var array
  61. */
  62. protected $nestedTransactionStates = [];
  63. /**
  64. * @var bool
  65. */
  66. protected $logState;
  67. /**
  68. * @var \Cake\Datasource\ConnectionInterface
  69. */
  70. protected $connection;
  71. /**
  72. * @var \Cake\Database\Log\QueryLogger
  73. */
  74. protected $defaultLogger;
  75. public function setUp(): void
  76. {
  77. parent::setUp();
  78. $this->connection = ConnectionManager::get('test');
  79. $this->defaultLogger = $this->connection->getLogger();
  80. $this->logState = $this->connection->isQueryLoggingEnabled();
  81. $this->connection->disableQueryLogging();
  82. static::setAppNamespace();
  83. }
  84. public function tearDown(): void
  85. {
  86. $this->connection->disableSavePoints();
  87. $this->connection->setLogger($this->defaultLogger);
  88. $this->connection->enableQueryLogging($this->logState);
  89. Log::reset();
  90. unset($this->connection);
  91. parent::tearDown();
  92. }
  93. /**
  94. * Auxiliary method to build a mock for a driver so it can be injected into
  95. * the connection object
  96. *
  97. * @return \Cake\Database\Driver|\PHPUnit\Framework\MockObject\MockObject
  98. */
  99. public function getMockFormDriver()
  100. {
  101. $driver = $this->getMockBuilder(Driver::class)->getMock();
  102. $driver->expects($this->once())
  103. ->method('enabled')
  104. ->will($this->returnValue(true));
  105. return $driver;
  106. }
  107. /**
  108. * Tests connecting to database
  109. */
  110. public function testIsConnected(): void
  111. {
  112. $this->connection->connect();
  113. $this->assertTrue($this->connection->isConnected());
  114. }
  115. /**
  116. * Tests creating a connection using no driver throws an exception
  117. */
  118. public function testNoDriver(): void
  119. {
  120. $this->expectException(MissingDriverException::class);
  121. $this->expectExceptionMessage('Database driver could not be found.');
  122. $connection = new Connection([]);
  123. }
  124. /**
  125. * Tests creating a connection using an invalid driver throws an exception
  126. */
  127. public function testEmptyDriver(): void
  128. {
  129. $this->expectException(MissingDriverException::class);
  130. $this->expectExceptionMessage('Database driver could not be found.');
  131. $connection = new Connection(['driver' => false]);
  132. }
  133. /**
  134. * Tests creating a connection using an invalid driver throws an exception
  135. */
  136. public function testMissingDriver(): void
  137. {
  138. $this->expectException(MissingDriverException::class);
  139. $this->expectExceptionMessage('Database driver \Foo\InvalidDriver could not be found.');
  140. $connection = new Connection(['driver' => '\Foo\InvalidDriver']);
  141. }
  142. /**
  143. * Tests trying to use a disabled driver throws an exception
  144. */
  145. public function testDisabledDriver(): void
  146. {
  147. $this->expectException(MissingExtensionException::class);
  148. $this->expectExceptionMessage('Database driver DriverMock cannot be used due to a missing PHP extension or unmet dependency');
  149. $mock = $this->getMockBuilder(Mysql::class)
  150. ->onlyMethods(['enabled'])
  151. ->setMockClassName('DriverMock')
  152. ->getMock();
  153. $connection = new Connection(['driver' => $mock]);
  154. }
  155. /**
  156. * Tests that the `driver` option supports the short classname/plugin syntax.
  157. */
  158. public function testDriverOptionClassNameSupport(): void
  159. {
  160. $connection = new Connection(['driver' => 'TestDriver']);
  161. $this->assertInstanceOf('TestApp\Database\Driver\TestDriver', $connection->getDriver());
  162. $connection = new Connection(['driver' => 'TestPlugin.TestDriver']);
  163. $this->assertInstanceOf('TestPlugin\Database\Driver\TestDriver', $connection->getDriver());
  164. [, $name] = namespaceSplit(get_class($this->connection->getDriver()));
  165. $connection = new Connection(['driver' => $name]);
  166. $this->assertInstanceOf(get_class($this->connection->getDriver()), $connection->getDriver());
  167. }
  168. /**
  169. * Tests that connecting with invalid credentials or database name throws an exception
  170. */
  171. public function testWrongCredentials(): void
  172. {
  173. $config = ConnectionManager::getConfig('test');
  174. $this->skipIf(isset($config['url']), 'Datasource has dsn, skipping.');
  175. $connection = new Connection(['database' => '/dev/nonexistent'] + ConnectionManager::getConfig('test'));
  176. $e = null;
  177. try {
  178. $connection->connect();
  179. } catch (MissingConnectionException $e) {
  180. }
  181. $this->assertNotNull($e);
  182. $this->assertStringStartsWith(
  183. sprintf(
  184. 'Connection to %s could not be established:',
  185. App::shortName(get_class($connection->getDriver()), 'Database/Driver')
  186. ),
  187. $e->getMessage()
  188. );
  189. $this->assertInstanceOf('PDOException', $e->getPrevious());
  190. }
  191. /**
  192. * Tests creation of prepared statements
  193. */
  194. public function testPrepare(): void
  195. {
  196. $sql = 'SELECT 1 + 1';
  197. $result = $this->connection->prepare($sql);
  198. $this->assertInstanceOf('Cake\Database\StatementInterface', $result);
  199. $this->assertEquals($sql, $result->queryString);
  200. $query = $this->connection->newQuery()->select('1 + 1');
  201. $result = $this->connection->prepare($query);
  202. $this->assertInstanceOf('Cake\Database\StatementInterface', $result);
  203. $sql = '#SELECT [`"\[]?1 \+ 1[`"\]]?#';
  204. $this->assertMatchesRegularExpression($sql, $result->queryString);
  205. }
  206. /**
  207. * Tests executing a simple query using bound values
  208. */
  209. public function testExecuteWithArguments(): void
  210. {
  211. $sql = 'SELECT 1 + ?';
  212. $statement = $this->connection->execute($sql, [1], ['integer']);
  213. $this->assertCount(1, $statement);
  214. $result = $statement->fetch();
  215. $this->assertEquals([2], $result);
  216. $statement->closeCursor();
  217. $sql = 'SELECT 1 + ? + ? AS total';
  218. $statement = $this->connection->execute($sql, [2, 3], ['integer', 'integer']);
  219. $this->assertCount(1, $statement);
  220. $result = $statement->fetch('assoc');
  221. $this->assertEquals(['total' => 6], $result);
  222. $statement->closeCursor();
  223. $sql = 'SELECT 1 + :one + :two AS total';
  224. $statement = $this->connection->execute($sql, ['one' => 2, 'two' => 3], ['one' => 'integer', 'two' => 'integer']);
  225. $this->assertCount(1, $statement);
  226. $result = $statement->fetch('assoc');
  227. $statement->closeCursor();
  228. $this->assertEquals(['total' => 6], $result);
  229. }
  230. /**
  231. * Tests executing a query with params and associated types
  232. */
  233. public function testExecuteWithArgumentsAndTypes(): void
  234. {
  235. $sql = "SELECT '2012-01-01' = ?";
  236. $statement = $this->connection->execute($sql, [new DateTime('2012-01-01')], ['date']);
  237. $result = $statement->fetch();
  238. $statement->closeCursor();
  239. $this->assertTrue((bool)$result[0]);
  240. }
  241. /**
  242. * test executing a buffered query interacts with Collection well.
  243. */
  244. public function testBufferedStatementCollectionWrappingStatement(): void
  245. {
  246. $this->skipIf(
  247. !($this->connection->getDriver() instanceof Sqlite),
  248. 'Only required for SQLite driver which does not support buffered results natively'
  249. );
  250. $statement = $this->connection->query('SELECT * FROM things LIMIT 3');
  251. $collection = new Collection($statement);
  252. $result = $collection->extract('id')->toArray();
  253. $this->assertEquals(['1', '2'], $result);
  254. // Check iteration after extraction
  255. $result = [];
  256. foreach ($collection as $v) {
  257. $result[] = $v['id'];
  258. }
  259. $this->assertEquals(['1', '2'], $result);
  260. }
  261. /**
  262. * Tests that passing a unknown value to a query throws an exception
  263. */
  264. public function testExecuteWithMissingType(): void
  265. {
  266. $this->expectException(InvalidArgumentException::class);
  267. $sql = 'SELECT ?';
  268. $statement = $this->connection->execute($sql, [new DateTime('2012-01-01')], ['bar']);
  269. }
  270. /**
  271. * Tests executing a query with no params also works
  272. */
  273. public function testExecuteWithNoParams(): void
  274. {
  275. $sql = 'SELECT 1';
  276. $statement = $this->connection->execute($sql);
  277. $result = $statement->fetch();
  278. $this->assertCount(1, $result);
  279. $this->assertEquals([1], $result);
  280. $statement->closeCursor();
  281. }
  282. /**
  283. * Tests it is possible to insert data into a table using matching types by key name
  284. */
  285. public function testInsertWithMatchingTypes(): void
  286. {
  287. $data = ['id' => '3', 'title' => 'a title', 'body' => 'a body'];
  288. $result = $this->connection->insert(
  289. 'things',
  290. $data,
  291. ['id' => 'integer', 'title' => 'string', 'body' => 'string']
  292. );
  293. $this->assertInstanceOf('Cake\Database\StatementInterface', $result);
  294. $result->closeCursor();
  295. $result = $this->connection->execute('SELECT * from things where id = 3');
  296. $this->assertCount(1, $result);
  297. $row = $result->fetch('assoc');
  298. $result->closeCursor();
  299. $this->assertEquals($data, $row);
  300. }
  301. /**
  302. * Tests it is possible to insert data into a table using matching types by array position
  303. */
  304. public function testInsertWithPositionalTypes(): void
  305. {
  306. $data = ['id' => '3', 'title' => 'a title', 'body' => 'a body'];
  307. $result = $this->connection->insert(
  308. 'things',
  309. $data,
  310. ['integer', 'string', 'string']
  311. );
  312. $result->closeCursor();
  313. $this->assertInstanceOf('Cake\Database\StatementInterface', $result);
  314. $result = $this->connection->execute('SELECT * from things where id = 3');
  315. $this->assertCount(1, $result);
  316. $row = $result->fetch('assoc');
  317. $result->closeCursor();
  318. $this->assertEquals($data, $row);
  319. }
  320. /**
  321. * Tests an statement class can be reused for multiple executions
  322. */
  323. public function testStatementReusing(): void
  324. {
  325. $total = $this->connection->execute('SELECT COUNT(*) AS total FROM things');
  326. $result = $total->fetch('assoc');
  327. $this->assertEquals(2, $result['total']);
  328. $total->closeCursor();
  329. $total->execute();
  330. $result = $total->fetch('assoc');
  331. $this->assertEquals(2, $result['total']);
  332. $total->closeCursor();
  333. $result = $this->connection->execute('SELECT title, body FROM things');
  334. $row = $result->fetch('assoc');
  335. $this->assertSame('a title', $row['title']);
  336. $this->assertSame('a body', $row['body']);
  337. $row = $result->fetch('assoc');
  338. $result->closeCursor();
  339. $this->assertSame('another title', $row['title']);
  340. $this->assertSame('another body', $row['body']);
  341. $result->execute();
  342. $row = $result->fetch('assoc');
  343. $result->closeCursor();
  344. $this->assertSame('a title', $row['title']);
  345. }
  346. /**
  347. * Tests that it is possible to pass PDO constants to the underlying statement
  348. * object for using alternate fetch types
  349. */
  350. public function testStatementFetchObject(): void
  351. {
  352. $statement = $this->connection->execute('SELECT title, body FROM things');
  353. $row = $statement->fetch(PDO::FETCH_OBJ);
  354. $this->assertSame('a title', $row->title);
  355. $this->assertSame('a body', $row->body);
  356. $statement->closeCursor();
  357. }
  358. /**
  359. * Tests rows can be updated without specifying any conditions nor types
  360. */
  361. public function testUpdateWithoutConditionsNorTypes(): void
  362. {
  363. $title = 'changed the title!';
  364. $body = 'changed the body!';
  365. $this->connection->update('things', ['title' => $title, 'body' => $body]);
  366. $result = $this->connection->execute('SELECT * FROM things WHERE title = ? AND body = ?', [$title, $body]);
  367. $this->assertCount(2, $result);
  368. $result->closeCursor();
  369. }
  370. /**
  371. * Tests it is possible to use key => value conditions for update
  372. */
  373. public function testUpdateWithConditionsNoTypes(): void
  374. {
  375. $title = 'changed the title!';
  376. $body = 'changed the body!';
  377. $this->connection->update('things', ['title' => $title, 'body' => $body], ['id' => 2]);
  378. $result = $this->connection->execute('SELECT * FROM things WHERE title = ? AND body = ?', [$title, $body]);
  379. $this->assertCount(1, $result);
  380. $result->closeCursor();
  381. }
  382. /**
  383. * Tests it is possible to use key => value and string conditions for update
  384. */
  385. public function testUpdateWithConditionsCombinedNoTypes(): void
  386. {
  387. $title = 'changed the title!';
  388. $body = 'changed the body!';
  389. $this->connection->update('things', ['title' => $title, 'body' => $body], ['id' => 2, 'body is not null']);
  390. $result = $this->connection->execute('SELECT * FROM things WHERE title = ? AND body = ?', [$title, $body]);
  391. $this->assertCount(1, $result);
  392. $result->closeCursor();
  393. }
  394. /**
  395. * Tests you can bind types to update values
  396. */
  397. public function testUpdateWithTypes(): void
  398. {
  399. $title = 'changed the title!';
  400. $body = new DateTime('2012-01-01');
  401. $values = compact('title', 'body');
  402. $this->connection->update('things', $values, [], ['body' => 'date']);
  403. $result = $this->connection->execute('SELECT * FROM things WHERE title = :title AND body = :body', $values, ['body' => 'date']);
  404. $this->assertCount(2, $result);
  405. $row = $result->fetch('assoc');
  406. $this->assertSame('2012-01-01', $row['body']);
  407. $row = $result->fetch('assoc');
  408. $this->assertSame('2012-01-01', $row['body']);
  409. $result->closeCursor();
  410. }
  411. /**
  412. * Tests you can bind types to update values
  413. */
  414. public function testUpdateWithConditionsAndTypes(): void
  415. {
  416. $title = 'changed the title!';
  417. $body = new DateTime('2012-01-01');
  418. $values = compact('title', 'body');
  419. $this->connection->update('things', $values, ['id' => '1'], ['body' => 'date', 'id' => 'integer']);
  420. $result = $this->connection->execute('SELECT * FROM things WHERE title = :title AND body = :body', $values, ['body' => 'date']);
  421. $this->assertCount(1, $result);
  422. $row = $result->fetch('assoc');
  423. $this->assertSame('2012-01-01', $row['body']);
  424. $result->closeCursor();
  425. }
  426. /**
  427. * Tests delete from table with no conditions
  428. */
  429. public function testDeleteNoConditions(): void
  430. {
  431. $this->connection->delete('things');
  432. $result = $this->connection->execute('SELECT * FROM things');
  433. $this->assertCount(0, $result);
  434. $result->closeCursor();
  435. }
  436. /**
  437. * Tests delete from table with conditions
  438. */
  439. public function testDeleteWithConditions(): void
  440. {
  441. $this->connection->delete('things', ['id' => '1'], ['id' => 'integer']);
  442. $result = $this->connection->execute('SELECT * FROM things');
  443. $this->assertCount(1, $result);
  444. $result->closeCursor();
  445. $this->connection->delete('things', ['id' => '1'], ['id' => 'integer']);
  446. $result = $this->connection->execute('SELECT * FROM things');
  447. $this->assertCount(1, $result);
  448. $result->closeCursor();
  449. $this->connection->delete('things', ['id' => '2'], ['id' => 'integer']);
  450. $result = $this->connection->execute('SELECT * FROM things');
  451. $this->assertCount(0, $result);
  452. $result->closeCursor();
  453. }
  454. /**
  455. * Tests that it is possible to use simple database transactions
  456. */
  457. public function testSimpleTransactions(): void
  458. {
  459. $this->connection->begin();
  460. $this->assertTrue($this->connection->getDriver()->inTransaction());
  461. $this->connection->delete('things', ['id' => 1]);
  462. $this->connection->rollback();
  463. $this->assertFalse($this->connection->getDriver()->inTransaction());
  464. $result = $this->connection->execute('SELECT * FROM things');
  465. $this->assertCount(2, $result);
  466. $result->closeCursor();
  467. $this->connection->begin();
  468. $this->assertTrue($this->connection->getDriver()->inTransaction());
  469. $this->connection->delete('things', ['id' => 1]);
  470. $this->connection->commit();
  471. $this->assertFalse($this->connection->getDriver()->inTransaction());
  472. $result = $this->connection->execute('SELECT * FROM things');
  473. $this->assertCount(1, $result);
  474. }
  475. /**
  476. * Tests that the destructor of Connection generates a warning log
  477. * when transaction is not closed
  478. */
  479. public function testDestructorWithUncommittedTransaction(): void
  480. {
  481. $driver = $this->getMockFormDriver();
  482. $connection = new Connection(['driver' => $driver]);
  483. $connection->begin();
  484. $this->assertTrue($connection->inTransaction());
  485. $logger = $this->createMock('Psr\Log\AbstractLogger');
  486. $logger->expects($this->once())
  487. ->method('log')
  488. ->with('warning', $this->stringContains('The connection is going to be closed'));
  489. Log::setConfig('error', $logger);
  490. // Destroy the connection
  491. unset($connection);
  492. }
  493. /**
  494. * Tests that it is possible to use virtualized nested transaction
  495. * with early rollback algorithm
  496. */
  497. public function testVirtualNestedTransaction(): void
  498. {
  499. //starting 3 virtual transaction
  500. $this->connection->begin();
  501. $this->connection->begin();
  502. $this->connection->begin();
  503. $this->assertTrue($this->connection->getDriver()->inTransaction());
  504. $this->connection->delete('things', ['id' => 1]);
  505. $result = $this->connection->execute('SELECT * FROM things');
  506. $this->assertCount(1, $result);
  507. $this->connection->commit();
  508. $this->assertTrue($this->connection->getDriver()->inTransaction());
  509. $this->connection->rollback();
  510. $this->assertFalse($this->connection->getDriver()->inTransaction());
  511. $result = $this->connection->execute('SELECT * FROM things');
  512. $this->assertCount(2, $result);
  513. }
  514. /**
  515. * Tests that it is possible to use virtualized nested transaction
  516. * with early rollback algorithm
  517. */
  518. public function testVirtualNestedTransaction2(): void
  519. {
  520. //starting 3 virtual transaction
  521. $this->connection->begin();
  522. $this->connection->begin();
  523. $this->connection->begin();
  524. $this->connection->delete('things', ['id' => 1]);
  525. $result = $this->connection->execute('SELECT * FROM things');
  526. $this->assertCount(1, $result);
  527. $this->connection->rollback();
  528. $result = $this->connection->execute('SELECT * FROM things');
  529. $this->assertCount(2, $result);
  530. }
  531. /**
  532. * Tests that it is possible to use virtualized nested transaction
  533. * with early rollback algorithm
  534. */
  535. public function testVirtualNestedTransaction3(): void
  536. {
  537. //starting 3 virtual transaction
  538. $this->connection->begin();
  539. $this->connection->begin();
  540. $this->connection->begin();
  541. $this->connection->delete('things', ['id' => 1]);
  542. $result = $this->connection->execute('SELECT * FROM things');
  543. $this->assertCount(1, $result);
  544. $this->connection->commit();
  545. $this->connection->commit();
  546. $this->connection->commit();
  547. $result = $this->connection->execute('SELECT * FROM things');
  548. $this->assertCount(1, $result);
  549. }
  550. /**
  551. * Tests that it is possible to real use nested transactions
  552. */
  553. public function testSavePoints(): void
  554. {
  555. $this->skipIf(!$this->connection->enableSavePoints(true));
  556. $this->connection->begin();
  557. $this->connection->delete('things', ['id' => 1]);
  558. $result = $this->connection->execute('SELECT * FROM things');
  559. $this->assertCount(1, $result);
  560. $this->connection->begin();
  561. $this->connection->delete('things', ['id' => 2]);
  562. $result = $this->connection->execute('SELECT * FROM things');
  563. $this->assertCount(0, $result);
  564. $this->connection->rollback();
  565. $result = $this->connection->execute('SELECT * FROM things');
  566. $this->assertCount(1, $result);
  567. $this->connection->rollback();
  568. $result = $this->connection->execute('SELECT * FROM things');
  569. $this->assertCount(2, $result);
  570. }
  571. /**
  572. * Tests that it is possible to real use nested transactions
  573. */
  574. public function testSavePoints2(): void
  575. {
  576. $this->skipIf(!$this->connection->enableSavePoints(true));
  577. $this->connection->begin();
  578. $this->connection->delete('things', ['id' => 1]);
  579. $result = $this->connection->execute('SELECT * FROM things');
  580. $this->assertCount(1, $result);
  581. $this->connection->begin();
  582. $this->connection->delete('things', ['id' => 2]);
  583. $result = $this->connection->execute('SELECT * FROM things');
  584. $this->assertCount(0, $result);
  585. $this->connection->rollback();
  586. $result = $this->connection->execute('SELECT * FROM things');
  587. $this->assertCount(1, $result);
  588. $this->connection->commit();
  589. $result = $this->connection->execute('SELECT * FROM things');
  590. $this->assertCount(1, $result);
  591. }
  592. /**
  593. * Tests inTransaction()
  594. */
  595. public function testInTransaction(): void
  596. {
  597. $this->connection->begin();
  598. $this->assertTrue($this->connection->inTransaction());
  599. $this->connection->begin();
  600. $this->assertTrue($this->connection->inTransaction());
  601. $this->connection->commit();
  602. $this->assertTrue($this->connection->inTransaction());
  603. $this->connection->commit();
  604. $this->assertFalse($this->connection->inTransaction());
  605. $this->connection->begin();
  606. $this->assertTrue($this->connection->inTransaction());
  607. $this->connection->begin();
  608. $this->connection->rollback();
  609. $this->assertFalse($this->connection->inTransaction());
  610. }
  611. /**
  612. * Tests inTransaction() with save points
  613. */
  614. public function testInTransactionWithSavePoints(): void
  615. {
  616. $this->skipIf(
  617. $this->connection->getDriver() instanceof Sqlserver,
  618. 'SQLServer fails when this test is included.'
  619. );
  620. $this->skipIf(!$this->connection->enableSavePoints(true));
  621. $this->connection->begin();
  622. $this->assertTrue($this->connection->inTransaction());
  623. $this->connection->begin();
  624. $this->assertTrue($this->connection->inTransaction());
  625. $this->connection->commit();
  626. $this->assertTrue($this->connection->inTransaction());
  627. $this->connection->commit();
  628. $this->assertFalse($this->connection->inTransaction());
  629. $this->connection->begin();
  630. $this->assertTrue($this->connection->inTransaction());
  631. $this->connection->begin();
  632. $this->connection->rollback();
  633. $this->assertTrue($this->connection->inTransaction());
  634. $this->connection->rollback();
  635. $this->assertFalse($this->connection->inTransaction());
  636. }
  637. /**
  638. * Tests connection can quote values to be safely used in query strings
  639. */
  640. public function testQuote(): void
  641. {
  642. $this->skipIf(!$this->connection->supportsQuoting());
  643. $expected = "'2012-01-01'";
  644. $result = $this->connection->quote(new DateTime('2012-01-01'), 'date');
  645. $this->assertEquals($expected, $result);
  646. $expected = "'1'";
  647. $result = $this->connection->quote(1, 'string');
  648. $this->assertEquals($expected, $result);
  649. $expected = "'hello'";
  650. $result = $this->connection->quote('hello', 'string');
  651. $this->assertEquals($expected, $result);
  652. }
  653. /**
  654. * Tests identifier quoting
  655. */
  656. public function testQuoteIdentifier(): void
  657. {
  658. $driver = $this->getMockBuilder('Cake\Database\Driver\Sqlite')
  659. ->onlyMethods(['enabled'])
  660. ->getMock();
  661. $driver->expects($this->once())
  662. ->method('enabled')
  663. ->will($this->returnValue(true));
  664. $connection = new Connection(['driver' => $driver]);
  665. $result = $connection->quoteIdentifier('name');
  666. $expected = '"name"';
  667. $this->assertEquals($expected, $result);
  668. $result = $connection->quoteIdentifier('Model.*');
  669. $expected = '"Model".*';
  670. $this->assertEquals($expected, $result);
  671. $result = $connection->quoteIdentifier('Items.No_ 2');
  672. $expected = '"Items"."No_ 2"';
  673. $this->assertEquals($expected, $result);
  674. $result = $connection->quoteIdentifier('Items.No_ 2 thing');
  675. $expected = '"Items"."No_ 2 thing"';
  676. $this->assertEquals($expected, $result);
  677. $result = $connection->quoteIdentifier('Items.No_ 2 thing AS thing');
  678. $expected = '"Items"."No_ 2 thing" AS "thing"';
  679. $this->assertEquals($expected, $result);
  680. $result = $connection->quoteIdentifier('Items.Item Category Code = :c1');
  681. $expected = '"Items"."Item Category Code" = :c1';
  682. $this->assertEquals($expected, $result);
  683. $result = $connection->quoteIdentifier('MTD()');
  684. $expected = 'MTD()';
  685. $this->assertEquals($expected, $result);
  686. $result = $connection->quoteIdentifier('(sm)');
  687. $expected = '(sm)';
  688. $this->assertEquals($expected, $result);
  689. $result = $connection->quoteIdentifier('name AS x');
  690. $expected = '"name" AS "x"';
  691. $this->assertEquals($expected, $result);
  692. $result = $connection->quoteIdentifier('Model.name AS x');
  693. $expected = '"Model"."name" AS "x"';
  694. $this->assertEquals($expected, $result);
  695. $result = $connection->quoteIdentifier('Function(Something.foo)');
  696. $expected = 'Function("Something"."foo")';
  697. $this->assertEquals($expected, $result);
  698. $result = $connection->quoteIdentifier('Function(SubFunction(Something.foo))');
  699. $expected = 'Function(SubFunction("Something"."foo"))';
  700. $this->assertEquals($expected, $result);
  701. $result = $connection->quoteIdentifier('Function(Something.foo) AS x');
  702. $expected = 'Function("Something"."foo") AS "x"';
  703. $this->assertEquals($expected, $result);
  704. $result = $connection->quoteIdentifier('name-with-minus');
  705. $expected = '"name-with-minus"';
  706. $this->assertEquals($expected, $result);
  707. $result = $connection->quoteIdentifier('my-name');
  708. $expected = '"my-name"';
  709. $this->assertEquals($expected, $result);
  710. $result = $connection->quoteIdentifier('Foo-Model.*');
  711. $expected = '"Foo-Model".*';
  712. $this->assertEquals($expected, $result);
  713. $result = $connection->quoteIdentifier('Team.P%');
  714. $expected = '"Team"."P%"';
  715. $this->assertEquals($expected, $result);
  716. $result = $connection->quoteIdentifier('Team.G/G');
  717. $expected = '"Team"."G/G"';
  718. $this->assertEquals($expected, $result);
  719. $result = $connection->quoteIdentifier('Model.name as y');
  720. $expected = '"Model"."name" AS "y"';
  721. $this->assertEquals($expected, $result);
  722. $result = $connection->quoteIdentifier('nämé');
  723. $expected = '"nämé"';
  724. $this->assertEquals($expected, $result);
  725. $result = $connection->quoteIdentifier('aßa.nämé');
  726. $expected = '"aßa"."nämé"';
  727. $this->assertEquals($expected, $result);
  728. $result = $connection->quoteIdentifier('aßa.*');
  729. $expected = '"aßa".*';
  730. $this->assertEquals($expected, $result);
  731. $result = $connection->quoteIdentifier('Modeß.nämé as y');
  732. $expected = '"Modeß"."nämé" AS "y"';
  733. $this->assertEquals($expected, $result);
  734. $result = $connection->quoteIdentifier('Model.näme Datum as y');
  735. $expected = '"Model"."näme Datum" AS "y"';
  736. $this->assertEquals($expected, $result);
  737. }
  738. /**
  739. * Tests default return vale for logger() function
  740. */
  741. public function testGetLoggerDefault(): void
  742. {
  743. $logger = $this->connection->getLogger();
  744. $this->assertInstanceOf('Cake\Database\Log\QueryLogger', $logger);
  745. $this->assertSame($logger, $this->connection->getLogger());
  746. }
  747. /**
  748. * Tests setting and getting the logger object
  749. */
  750. public function testGetAndSetLogger(): void
  751. {
  752. $logger = new QueryLogger();
  753. $this->connection->setLogger($logger);
  754. $this->assertSame($logger, $this->connection->getLogger());
  755. }
  756. /**
  757. * Tests that statements are decorated with a logger when logQueries is set to true
  758. */
  759. public function testLoggerDecorator(): void
  760. {
  761. $logger = new QueryLogger();
  762. $this->connection->enableQueryLogging(true);
  763. $this->connection->setLogger($logger);
  764. $st = $this->connection->prepare('SELECT 1');
  765. $this->assertInstanceOf(LoggingStatement::class, $st);
  766. $this->assertSame($logger, $st->getLogger());
  767. $this->connection->enableQueryLogging(false);
  768. $st = $this->connection->prepare('SELECT 1');
  769. $this->assertNotInstanceOf('Cake\Database\Log\LoggingStatement', $st);
  770. }
  771. /**
  772. * test enableQueryLogging method
  773. */
  774. public function testEnableQueryLogging(): void
  775. {
  776. $this->connection->enableQueryLogging(true);
  777. $this->assertTrue($this->connection->isQueryLoggingEnabled());
  778. $this->connection->disableQueryLogging();
  779. $this->assertFalse($this->connection->isQueryLoggingEnabled());
  780. }
  781. /**
  782. * Tests that log() function logs to the configured query logger
  783. */
  784. public function testLogFunction(): void
  785. {
  786. Log::setConfig('queries', ['className' => 'Array']);
  787. $this->connection->enableQueryLogging();
  788. $this->connection->log('SELECT 1');
  789. $messages = Log::engine('queries')->read();
  790. $this->assertCount(1, $messages);
  791. $this->assertSame('debug: connection=test duration=0 rows=0 SELECT 1', $messages[0]);
  792. }
  793. /**
  794. * @see https://github.com/cakephp/cakephp/issues/14676
  795. */
  796. public function testLoggerDecoratorDoesNotPrematurelyFetchRecords(): void
  797. {
  798. Log::setConfig('queries', ['className' => 'Array']);
  799. $logger = new QueryLogger();
  800. $this->connection->enableQueryLogging(true);
  801. $this->connection->setLogger($logger);
  802. $st = $this->connection->execute('SELECT * FROM things');
  803. $this->assertInstanceOf(LoggingStatement::class, $st);
  804. $messages = Log::engine('queries')->read();
  805. $this->assertCount(0, $messages);
  806. $expected = [
  807. [1, 'a title', 'a body'],
  808. [2, 'another title', 'another body'],
  809. ];
  810. $results = $st->fetchAll();
  811. $this->assertEquals($expected, $results);
  812. $messages = Log::engine('queries')->read();
  813. $this->assertCount(1, $messages);
  814. $st = $this->connection->execute('SELECT * FROM things WHERE id = 0');
  815. $this->assertSame(0, $st->rowCount());
  816. $messages = Log::engine('queries')->read();
  817. $this->assertCount(2, $messages, 'Select queries without any matching rows should also be logged.');
  818. }
  819. /**
  820. * Tests that begin and rollback are also logged
  821. */
  822. public function testLogBeginRollbackTransaction(): void
  823. {
  824. Log::setConfig('queries', ['className' => 'Array']);
  825. $connection = $this
  826. ->getMockBuilder(Connection::class)
  827. ->onlyMethods(['connect'])
  828. ->disableOriginalConstructor()
  829. ->getMock();
  830. $connection->enableQueryLogging(true);
  831. $driver = $this->getMockFormDriver();
  832. $connection->setDriver($driver);
  833. $connection->begin();
  834. $connection->begin(); //This one will not be logged
  835. $connection->rollback();
  836. $messages = Log::engine('queries')->read();
  837. $this->assertCount(2, $messages);
  838. $this->assertSame('debug: connection= duration=0 rows=0 BEGIN', $messages[0]);
  839. $this->assertSame('debug: connection= duration=0 rows=0 ROLLBACK', $messages[1]);
  840. }
  841. /**
  842. * Tests that commits are logged
  843. */
  844. public function testLogCommitTransaction(): void
  845. {
  846. $driver = $this->getMockFormDriver();
  847. $connection = $this->getMockBuilder(Connection::class)
  848. ->onlyMethods(['connect'])
  849. ->setConstructorArgs([['driver' => $driver]])
  850. ->getMock();
  851. Log::setConfig('queries', ['className' => 'Array']);
  852. $connection->enableQueryLogging(true);
  853. $connection->begin();
  854. $connection->commit();
  855. $messages = Log::engine('queries')->read();
  856. $this->assertCount(2, $messages);
  857. $this->assertSame('debug: connection= duration=0 rows=0 BEGIN', $messages[0]);
  858. $this->assertSame('debug: connection= duration=0 rows=0 COMMIT', $messages[1]);
  859. }
  860. /**
  861. * Tests setting and getting the cacher object
  862. */
  863. public function testGetAndSetCacher(): void
  864. {
  865. $cacher = new NullEngine();
  866. $this->connection->setCacher($cacher);
  867. $this->assertSame($cacher, $this->connection->getCacher());
  868. }
  869. /**
  870. * Tests that the transactional method will start and commit a transaction
  871. * around some arbitrary function passed as argument
  872. */
  873. public function testTransactionalSuccess(): void
  874. {
  875. $driver = $this->getMockFormDriver();
  876. $connection = $this->getMockBuilder(Connection::class)
  877. ->onlyMethods(['connect', 'commit', 'begin'])
  878. ->setConstructorArgs([['driver' => $driver]])
  879. ->getMock();
  880. $connection->expects($this->once())->method('begin');
  881. $connection->expects($this->once())->method('commit');
  882. $result = $connection->transactional(function ($conn) use ($connection) {
  883. $this->assertSame($connection, $conn);
  884. return 'thing';
  885. });
  886. $this->assertSame('thing', $result);
  887. }
  888. /**
  889. * Tests that the transactional method will rollback the transaction if false
  890. * is returned from the callback
  891. */
  892. public function testTransactionalFail(): void
  893. {
  894. $driver = $this->getMockFormDriver();
  895. $connection = $this->getMockBuilder(Connection::class)
  896. ->onlyMethods(['connect', 'commit', 'begin', 'rollback'])
  897. ->setConstructorArgs([['driver' => $driver]])
  898. ->getMock();
  899. $connection->expects($this->once())->method('begin');
  900. $connection->expects($this->once())->method('rollback');
  901. $connection->expects($this->never())->method('commit');
  902. $result = $connection->transactional(function ($conn) use ($connection) {
  903. $this->assertSame($connection, $conn);
  904. return false;
  905. });
  906. $this->assertFalse($result);
  907. }
  908. /**
  909. * Tests that the transactional method will rollback the transaction
  910. * and throw the same exception if the callback raises one
  911. *
  912. * @throws \InvalidArgumentException
  913. */
  914. public function testTransactionalWithException(): void
  915. {
  916. $this->expectException(InvalidArgumentException::class);
  917. $driver = $this->getMockFormDriver();
  918. $connection = $this->getMockBuilder(Connection::class)
  919. ->onlyMethods(['connect', 'commit', 'begin', 'rollback'])
  920. ->setConstructorArgs([['driver' => $driver]])
  921. ->getMock();
  922. $connection->expects($this->once())->method('begin');
  923. $connection->expects($this->once())->method('rollback');
  924. $connection->expects($this->never())->method('commit');
  925. $connection->transactional(function ($conn) use ($connection): void {
  926. $this->assertSame($connection, $conn);
  927. throw new InvalidArgumentException();
  928. });
  929. }
  930. /**
  931. * Tests it is possible to set a schema collection object
  932. */
  933. public function testSetSchemaCollection(): void
  934. {
  935. $driver = $this->getMockFormDriver();
  936. $connection = $this->getMockBuilder(Connection::class)
  937. ->onlyMethods(['connect'])
  938. ->setConstructorArgs([['driver' => $driver]])
  939. ->getMock();
  940. $schema = $connection->getSchemaCollection();
  941. $this->assertInstanceOf('Cake\Database\Schema\Collection', $schema);
  942. $schema = $this->getMockBuilder('Cake\Database\Schema\Collection')
  943. ->setConstructorArgs([$connection])
  944. ->getMock();
  945. $connection->setSchemaCollection($schema);
  946. $this->assertSame($schema, $connection->getSchemaCollection());
  947. }
  948. /**
  949. * Test CachedCollection creation with default and custom cache key prefix.
  950. */
  951. public function testGetCachedCollection(): void
  952. {
  953. $driver = $this->getMockFormDriver();
  954. $connection = $this->getMockBuilder(Connection::class)
  955. ->onlyMethods(['connect'])
  956. ->setConstructorArgs([[
  957. 'driver' => $driver,
  958. 'name' => 'default',
  959. 'cacheMetadata' => true,
  960. ]])
  961. ->getMock();
  962. $schema = $connection->getSchemaCollection();
  963. $this->assertInstanceOf(CachedCollection::class, $schema);
  964. $this->assertSame('default_key', $schema->cacheKey('key'));
  965. $driver = $this->getMockFormDriver();
  966. $connection = $this->getMockBuilder(Connection::class)
  967. ->onlyMethods(['connect'])
  968. ->setConstructorArgs([[
  969. 'driver' => $driver,
  970. 'name' => 'default',
  971. 'cacheMetadata' => true,
  972. 'cacheKeyPrefix' => 'foo',
  973. ]])
  974. ->getMock();
  975. $schema = $connection->getSchemaCollection();
  976. $this->assertInstanceOf(CachedCollection::class, $schema);
  977. $this->assertSame('foo_key', $schema->cacheKey('key'));
  978. }
  979. /**
  980. * Tests that allowed nesting of commit/rollback operations doesn't
  981. * throw any exceptions.
  982. */
  983. public function testNestedTransactionRollbackExceptionNotThrown(): void
  984. {
  985. $this->connection->transactional(function () {
  986. $this->connection->transactional(function () {
  987. return true;
  988. });
  989. return true;
  990. });
  991. $this->assertFalse($this->connection->inTransaction());
  992. $this->connection->transactional(function () {
  993. $this->connection->transactional(function () {
  994. return true;
  995. });
  996. return false;
  997. });
  998. $this->assertFalse($this->connection->inTransaction());
  999. $this->connection->transactional(function () {
  1000. $this->connection->transactional(function () {
  1001. return false;
  1002. });
  1003. return false;
  1004. });
  1005. $this->assertFalse($this->connection->inTransaction());
  1006. }
  1007. /**
  1008. * Tests that not allowed nesting of commit/rollback operations throws
  1009. * a NestedTransactionRollbackException.
  1010. */
  1011. public function testNestedTransactionRollbackExceptionThrown(): void
  1012. {
  1013. $this->rollbackSourceLine = -1;
  1014. $e = null;
  1015. try {
  1016. $this->connection->transactional(function () {
  1017. $this->connection->transactional(function () {
  1018. return false;
  1019. });
  1020. $this->rollbackSourceLine = __LINE__ - 1;
  1021. return true;
  1022. });
  1023. $this->fail('NestedTransactionRollbackException should be thrown');
  1024. } catch (NestedTransactionRollbackException $e) {
  1025. }
  1026. $trace = $e->getTrace();
  1027. $this->assertEquals(__FILE__, $trace[1]['file']);
  1028. $this->assertEquals($this->rollbackSourceLine, $trace[1]['line']);
  1029. }
  1030. /**
  1031. * Tests more detail about that not allowed nesting of rollback/commit
  1032. * operations throws a NestedTransactionRollbackException.
  1033. */
  1034. public function testNestedTransactionStates(): void
  1035. {
  1036. $this->rollbackSourceLine = -1;
  1037. $this->nestedTransactionStates = [];
  1038. $e = null;
  1039. try {
  1040. $this->connection->transactional(function () {
  1041. $this->pushNestedTransactionState();
  1042. $this->connection->transactional(function () {
  1043. return true;
  1044. });
  1045. $this->connection->transactional(function () {
  1046. $this->pushNestedTransactionState();
  1047. $this->connection->transactional(function () {
  1048. return false;
  1049. });
  1050. $this->rollbackSourceLine = __LINE__ - 1;
  1051. $this->pushNestedTransactionState();
  1052. return true;
  1053. });
  1054. $this->connection->transactional(function () {
  1055. return false;
  1056. });
  1057. $this->pushNestedTransactionState();
  1058. return true;
  1059. });
  1060. $this->fail('NestedTransactionRollbackException should be thrown');
  1061. } catch (NestedTransactionRollbackException $e) {
  1062. }
  1063. $this->pushNestedTransactionState();
  1064. $this->assertSame([false, false, true, true, false], $this->nestedTransactionStates);
  1065. $this->assertFalse($this->connection->inTransaction());
  1066. $trace = $e->getTrace();
  1067. $this->assertEquals(__FILE__, $trace[1]['file']);
  1068. $this->assertEquals($this->rollbackSourceLine, $trace[1]['line']);
  1069. }
  1070. /**
  1071. * Helper method to trace nested transaction states.
  1072. */
  1073. public function pushNestedTransactionState(): void
  1074. {
  1075. $method = new ReflectionMethod($this->connection, 'wasNestedTransactionRolledback');
  1076. $method->setAccessible(true);
  1077. $this->nestedTransactionStates[] = $method->invoke($this->connection);
  1078. }
  1079. /**
  1080. * Tests that the connection is restablished whenever it is interrupted
  1081. * after having used the connection at least once.
  1082. */
  1083. public function testAutomaticReconnect(): void
  1084. {
  1085. $conn = clone $this->connection;
  1086. $statement = $conn->query('SELECT 1');
  1087. $statement->execute();
  1088. $statement->closeCursor();
  1089. $prop = new ReflectionProperty($conn, '_driver');
  1090. $prop->setAccessible(true);
  1091. $oldDriver = $prop->getValue($conn);
  1092. $newDriver = $this->getMockBuilder(Driver::class)->getMock();
  1093. $prop->setValue($conn, $newDriver);
  1094. $newDriver->expects($this->exactly(2))
  1095. ->method('prepare')
  1096. ->will($this->onConsecutiveCalls(
  1097. $this->throwException(new Exception('server gone away')),
  1098. $this->returnValue($statement)
  1099. ));
  1100. $res = $conn->query('SELECT 1');
  1101. $this->assertInstanceOf(StatementInterface::class, $res);
  1102. }
  1103. /**
  1104. * Tests that the connection is not restablished whenever it is interrupted
  1105. * inside a transaction.
  1106. */
  1107. public function testNoAutomaticReconnect(): void
  1108. {
  1109. $conn = clone $this->connection;
  1110. $statement = $conn->query('SELECT 1');
  1111. $statement->execute();
  1112. $statement->closeCursor();
  1113. $conn->begin();
  1114. $prop = new ReflectionProperty($conn, '_driver');
  1115. $prop->setAccessible(true);
  1116. $oldDriver = $prop->getValue($conn);
  1117. $newDriver = $this->getMockBuilder(Driver::class)->getMock();
  1118. $prop->setValue($conn, $newDriver);
  1119. $newDriver->expects($this->once())
  1120. ->method('prepare')
  1121. ->will($this->throwException(new Exception('server gone away')));
  1122. try {
  1123. $conn->query('SELECT 1');
  1124. } catch (Exception $e) {
  1125. }
  1126. $this->assertInstanceOf(Exception::class, $e ?? null);
  1127. $prop->setValue($conn, $oldDriver);
  1128. $conn->rollback();
  1129. }
  1130. }