|
22 | 22 | use Google\Cloud\Core\Exception\ServiceException; |
23 | 23 | use Google\Cloud\Spanner\Timestamp; |
24 | 24 | use Google\Cloud\Spanner\V1\DirectedReadOptions\ReplicaSelection\Type as ReplicaType; |
| 25 | +use Google\Cloud\Spanner\V1\ReadRequest\LockHint; |
| 26 | +use Google\Cloud\Spanner\V1\ReadRequest\OrderBy; |
25 | 27 |
|
26 | 28 | /** |
27 | 29 | * @group spanner |
@@ -450,6 +452,67 @@ public function testRunTransactionILBWithMultipleOperations() |
450 | 452 | $this->assertEquals([1], $res->rowCounts()); |
451 | 453 | } |
452 | 454 |
|
| 455 | + public function testOrderByOnTransaction() |
| 456 | + { |
| 457 | + $db = self::$database; |
| 458 | + $transaction = $db->transaction(); |
| 459 | + $limit = 10; |
| 460 | + |
| 461 | + $db->insertBatch(self::TEST_TABLE_NAME, $this->getMultipleRows($limit)); |
| 462 | + |
| 463 | + $options = [ |
| 464 | + 'orderBy' => OrderBy::ORDER_BY_PRIMARY_KEY, |
| 465 | + 'limit' => $limit, |
| 466 | + ]; |
| 467 | + |
| 468 | + $rows = iterator_to_array( |
| 469 | + $transaction->read( |
| 470 | + self::TEST_TABLE_NAME, |
| 471 | + new KeySet([ |
| 472 | + 'all' => true, |
| 473 | + ]), |
| 474 | + array_keys(self::$row), |
| 475 | + $options, |
| 476 | + )->rows() |
| 477 | + ); |
| 478 | + |
| 479 | + $this->assertEquals($limit, count($rows)); |
| 480 | + $transaction->rollback(); |
| 481 | + |
| 482 | + for ($i = 0; $i < count($rows) - 1; $i++) { |
| 483 | + $this->assertLessThanOrEqual( |
| 484 | + $rows[$i + 1]['id'], |
| 485 | + $rows[$i]['id'], |
| 486 | + 'The array is not sorted by id in ascending order.' |
| 487 | + ); |
| 488 | + } |
| 489 | + } |
| 490 | + |
| 491 | + public function testLockHintOnTransaction() |
| 492 | + { |
| 493 | + $db = self::$database; |
| 494 | + $transaction = $db->transaction(); |
| 495 | + $limit = 10; |
| 496 | + |
| 497 | + $db->insertBatch(self::TEST_TABLE_NAME, $this->getMultipleRows($limit)); |
| 498 | + |
| 499 | + $options = [ |
| 500 | + 'lockHint' => LockHint::LOCK_HINT_EXCLUSIVE, |
| 501 | + 'limit' => $limit, |
| 502 | + ]; |
| 503 | + |
| 504 | + $rows = $transaction->read( |
| 505 | + self::TEST_TABLE_NAME, |
| 506 | + new KeySet([ |
| 507 | + 'all' => true |
| 508 | + ]), |
| 509 | + array_keys(self::$row), |
| 510 | + $options, |
| 511 | + )->rows(); |
| 512 | + |
| 513 | + $this->assertEquals($limit, count(iterator_to_array($rows))); |
| 514 | + } |
| 515 | + |
453 | 516 | public function getDirectedReadOptions() |
454 | 517 | { |
455 | 518 | return |
@@ -503,4 +566,21 @@ private function getRow() |
503 | 566 |
|
504 | 567 | return $res->rows()->current(); |
505 | 568 | } |
| 569 | + |
| 570 | + private function getMultipleRows(int $total) |
| 571 | + { |
| 572 | + $rows = []; |
| 573 | + |
| 574 | + // Keeping the < total as the setup already inserts one. |
| 575 | + // if $total is 10, then we will generate 9 rows. |
| 576 | + for ($i = 0; $i < $total; $i++) { |
| 577 | + $rows[] = [ |
| 578 | + 'id' => rand(1, 346464), |
| 579 | + 'name' => uniqid(self::TESTING_PREFIX), |
| 580 | + 'birthday' => new Date(new \DateTime('2000-01-01')) |
| 581 | + ]; |
| 582 | + } |
| 583 | + |
| 584 | + return $rows; |
| 585 | + } |
506 | 586 | } |
0 commit comments