Skip to content

Commit a006890

Browse files
committed
Extract is_in_transaction helper from Collection class
1 parent c6aec73 commit a006890

File tree

2 files changed

+42
-40
lines changed

2 files changed

+42
-40
lines changed

src/Collection.php

Lines changed: 26 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ public function aggregate(array $pipeline, array $options = [])
209209
if ( ! isset($options['readConcern']) &&
210210
! ($hasOutStage && $this->readConcern->getLevel() === ReadConcern::MAJORITY) &&
211211
\MongoDB\server_supports_feature($server, self::$wireVersionForReadConcern) &&
212-
! $this->isInTransaction($options)) {
212+
! \MongoDB\is_in_transaction($options)) {
213213
$options['readConcern'] = $this->readConcern;
214214
}
215215

@@ -220,7 +220,7 @@ public function aggregate(array $pipeline, array $options = [])
220220
if ($hasOutStage &&
221221
! isset($options['writeConcern']) &&
222222
\MongoDB\server_supports_feature($server, self::$wireVersionForWritableCommandWriteConcern) &&
223-
! $this->isInTransaction($options)) {
223+
! \MongoDB\is_in_transaction($options)) {
224224
$options['writeConcern'] = $this->writeConcern;
225225
}
226226

@@ -242,7 +242,7 @@ public function aggregate(array $pipeline, array $options = [])
242242
*/
243243
public function bulkWrite(array $operations, array $options = [])
244244
{
245-
if ( ! isset($options['writeConcern']) && ! $this->isInTransaction($options)) {
245+
if ( ! isset($options['writeConcern']) && ! \MongoDB\is_in_transaction($options)) {
246246
$options['writeConcern'] = $this->writeConcern;
247247
}
248248

@@ -274,7 +274,7 @@ public function count($filter = [], array $options = [])
274274

275275
$server = $this->manager->selectServer($options['readPreference']);
276276

277-
if ( ! isset($options['readConcern']) && \MongoDB\server_supports_feature($server, self::$wireVersionForReadConcern) && ! $this->isInTransaction($options)) {
277+
if ( ! isset($options['readConcern']) && \MongoDB\server_supports_feature($server, self::$wireVersionForReadConcern) && ! \MongoDB\is_in_transaction($options)) {
278278
$options['readConcern'] = $this->readConcern;
279279
}
280280

@@ -303,7 +303,7 @@ public function countDocuments($filter = [], array $options = [])
303303

304304
$server = $this->manager->selectServer($options['readPreference']);
305305

306-
if ( ! isset($options['readConcern']) && \MongoDB\server_supports_feature($server, self::$wireVersionForReadConcern) && ! $this->isInTransaction($options)) {
306+
if ( ! isset($options['readConcern']) && \MongoDB\server_supports_feature($server, self::$wireVersionForReadConcern) && ! \MongoDB\is_in_transaction($options)) {
307307
$options['readConcern'] = $this->readConcern;
308308
}
309309

@@ -365,7 +365,7 @@ public function createIndexes(array $indexes, array $options = [])
365365
{
366366
$server = $this->manager->selectServer(new ReadPreference(ReadPreference::RP_PRIMARY));
367367

368-
if ( ! isset($options['writeConcern']) && \MongoDB\server_supports_feature($server, self::$wireVersionForWritableCommandWriteConcern) && ! $this->isInTransaction($options)) {
368+
if ( ! isset($options['writeConcern']) && \MongoDB\server_supports_feature($server, self::$wireVersionForWritableCommandWriteConcern) && ! \MongoDB\is_in_transaction($options)) {
369369
$options['writeConcern'] = $this->writeConcern;
370370
}
371371

@@ -388,7 +388,7 @@ public function createIndexes(array $indexes, array $options = [])
388388
*/
389389
public function deleteMany($filter, array $options = [])
390390
{
391-
if ( ! isset($options['writeConcern']) && ! $this->isInTransaction($options)) {
391+
if ( ! isset($options['writeConcern']) && ! \MongoDB\is_in_transaction($options)) {
392392
$options['writeConcern'] = $this->writeConcern;
393393
}
394394

@@ -412,7 +412,7 @@ public function deleteMany($filter, array $options = [])
412412
*/
413413
public function deleteOne($filter, array $options = [])
414414
{
415-
if ( ! isset($options['writeConcern']) && ! $this->isInTransaction($options)) {
415+
if ( ! isset($options['writeConcern']) && ! \MongoDB\is_in_transaction($options)) {
416416
$options['writeConcern'] = $this->writeConcern;
417417
}
418418

@@ -443,7 +443,7 @@ public function distinct($fieldName, $filter = [], array $options = [])
443443

444444
$server = $this->manager->selectServer($options['readPreference']);
445445

446-
if ( ! isset($options['readConcern']) && \MongoDB\server_supports_feature($server, self::$wireVersionForReadConcern) && ! $this->isInTransaction($options)) {
446+
if ( ! isset($options['readConcern']) && \MongoDB\server_supports_feature($server, self::$wireVersionForReadConcern) && ! \MongoDB\is_in_transaction($options)) {
447447
$options['readConcern'] = $this->readConcern;
448448
}
449449

@@ -470,7 +470,7 @@ public function drop(array $options = [])
470470

471471
$server = $this->manager->selectServer(new ReadPreference(ReadPreference::RP_PRIMARY));
472472

473-
if ( ! isset($options['writeConcern']) && \MongoDB\server_supports_feature($server, self::$wireVersionForWritableCommandWriteConcern) && ! $this->isInTransaction($options)) {
473+
if ( ! isset($options['writeConcern']) && \MongoDB\server_supports_feature($server, self::$wireVersionForWritableCommandWriteConcern) && ! \MongoDB\is_in_transaction($options)) {
474474
$options['writeConcern'] = $this->writeConcern;
475475
}
476476

@@ -504,7 +504,7 @@ public function dropIndex($indexName, array $options = [])
504504

505505
$server = $this->manager->selectServer(new ReadPreference(ReadPreference::RP_PRIMARY));
506506

507-
if ( ! isset($options['writeConcern']) && \MongoDB\server_supports_feature($server, self::$wireVersionForWritableCommandWriteConcern) && ! $this->isInTransaction($options)) {
507+
if ( ! isset($options['writeConcern']) && \MongoDB\server_supports_feature($server, self::$wireVersionForWritableCommandWriteConcern) && ! \MongoDB\is_in_transaction($options)) {
508508
$options['writeConcern'] = $this->writeConcern;
509509
}
510510

@@ -531,7 +531,7 @@ public function dropIndexes(array $options = [])
531531

532532
$server = $this->manager->selectServer(new ReadPreference(ReadPreference::RP_PRIMARY));
533533

534-
if ( ! isset($options['writeConcern']) && \MongoDB\server_supports_feature($server, self::$wireVersionForWritableCommandWriteConcern) && ! $this->isInTransaction($options)) {
534+
if ( ! isset($options['writeConcern']) && \MongoDB\server_supports_feature($server, self::$wireVersionForWritableCommandWriteConcern) && ! \MongoDB\is_in_transaction($options)) {
535535
$options['writeConcern'] = $this->writeConcern;
536536
}
537537

@@ -559,7 +559,7 @@ public function estimatedDocumentCount(array $options = [])
559559

560560
$server = $this->manager->selectServer($options['readPreference']);
561561

562-
if ( ! isset($options['readConcern']) && \MongoDB\server_supports_feature($server, self::$wireVersionForReadConcern) && ! $this->isInTransaction($options)) {
562+
if ( ! isset($options['readConcern']) && \MongoDB\server_supports_feature($server, self::$wireVersionForReadConcern) && ! \MongoDB\is_in_transaction($options)) {
563563
$options['readConcern'] = $this->readConcern;
564564
}
565565

@@ -617,7 +617,7 @@ public function find($filter = [], array $options = [])
617617

618618
$server = $this->manager->selectServer($options['readPreference']);
619619

620-
if ( ! isset($options['readConcern']) && \MongoDB\server_supports_feature($server, self::$wireVersionForReadConcern) && ! $this->isInTransaction($options)) {
620+
if ( ! isset($options['readConcern']) && \MongoDB\server_supports_feature($server, self::$wireVersionForReadConcern) && ! \MongoDB\is_in_transaction($options)) {
621621
$options['readConcern'] = $this->readConcern;
622622
}
623623

@@ -650,7 +650,7 @@ public function findOne($filter = [], array $options = [])
650650

651651
$server = $this->manager->selectServer($options['readPreference']);
652652

653-
if ( ! isset($options['readConcern']) && \MongoDB\server_supports_feature($server, self::$wireVersionForReadConcern) && ! $this->isInTransaction($options)) {
653+
if ( ! isset($options['readConcern']) && \MongoDB\server_supports_feature($server, self::$wireVersionForReadConcern) && ! \MongoDB\is_in_transaction($options)) {
654654
$options['readConcern'] = $this->readConcern;
655655
}
656656

@@ -682,7 +682,7 @@ public function findOneAndDelete($filter, array $options = [])
682682
{
683683
$server = $this->manager->selectServer(new ReadPreference(ReadPreference::RP_PRIMARY));
684684

685-
if ( ! isset($options['writeConcern']) && \MongoDB\server_supports_feature($server, self::$wireVersionForFindAndModifyWriteConcern) && ! $this->isInTransaction($options)) {
685+
if ( ! isset($options['writeConcern']) && \MongoDB\server_supports_feature($server, self::$wireVersionForFindAndModifyWriteConcern) && ! \MongoDB\is_in_transaction($options)) {
686686
$options['writeConcern'] = $this->writeConcern;
687687
}
688688

@@ -719,7 +719,7 @@ public function findOneAndReplace($filter, $replacement, array $options = [])
719719
{
720720
$server = $this->manager->selectServer(new ReadPreference(ReadPreference::RP_PRIMARY));
721721

722-
if ( ! isset($options['writeConcern']) && \MongoDB\server_supports_feature($server, self::$wireVersionForFindAndModifyWriteConcern) && ! $this->isInTransaction($options)) {
722+
if ( ! isset($options['writeConcern']) && \MongoDB\server_supports_feature($server, self::$wireVersionForFindAndModifyWriteConcern) && ! \MongoDB\is_in_transaction($options)) {
723723
$options['writeConcern'] = $this->writeConcern;
724724
}
725725

@@ -756,7 +756,7 @@ public function findOneAndUpdate($filter, $update, array $options = [])
756756
{
757757
$server = $this->manager->selectServer(new ReadPreference(ReadPreference::RP_PRIMARY));
758758

759-
if ( ! isset($options['writeConcern']) && \MongoDB\server_supports_feature($server, self::$wireVersionForFindAndModifyWriteConcern) && ! $this->isInTransaction($options)) {
759+
if ( ! isset($options['writeConcern']) && \MongoDB\server_supports_feature($server, self::$wireVersionForFindAndModifyWriteConcern) && ! \MongoDB\is_in_transaction($options)) {
760760
$options['writeConcern'] = $this->writeConcern;
761761
}
762762

@@ -865,7 +865,7 @@ public function getWriteConcern()
865865
*/
866866
public function insertMany(array $documents, array $options = [])
867867
{
868-
if ( ! isset($options['writeConcern']) && ! $this->isInTransaction($options)) {
868+
if ( ! isset($options['writeConcern']) && ! \MongoDB\is_in_transaction($options)) {
869869
$options['writeConcern'] = $this->writeConcern;
870870
}
871871

@@ -888,7 +888,7 @@ public function insertMany(array $documents, array $options = [])
888888
*/
889889
public function insertOne($document, array $options = [])
890890
{
891-
if ( ! isset($options['writeConcern']) && ! $this->isInTransaction($options)) {
891+
if ( ! isset($options['writeConcern']) && ! \MongoDB\is_in_transaction($options)) {
892892
$options['writeConcern'] = $this->writeConcern;
893893
}
894894

@@ -950,15 +950,15 @@ public function mapReduce(JavascriptInterface $map, JavascriptInterface $reduce,
950950
*
951951
* A read concern is also not compatible with transactions.
952952
*/
953-
if ( ! isset($options['readConcern']) && ! ($hasOutputCollection && $this->readConcern->getLevel() === ReadConcern::MAJORITY) && \MongoDB\server_supports_feature($server, self::$wireVersionForReadConcern) && ! $this->isInTransaction($options)) {
953+
if ( ! isset($options['readConcern']) && ! ($hasOutputCollection && $this->readConcern->getLevel() === ReadConcern::MAJORITY) && \MongoDB\server_supports_feature($server, self::$wireVersionForReadConcern) && ! \MongoDB\is_in_transaction($options)) {
954954
$options['readConcern'] = $this->readConcern;
955955
}
956956

957957
if ( ! isset($options['typeMap'])) {
958958
$options['typeMap'] = $this->typeMap;
959959
}
960960

961-
if ( ! isset($options['writeConcern']) && \MongoDB\server_supports_feature($server, self::$wireVersionForWritableCommandWriteConcern) && ! $this->isInTransaction($options)) {
961+
if ( ! isset($options['writeConcern']) && \MongoDB\server_supports_feature($server, self::$wireVersionForWritableCommandWriteConcern) && ! \MongoDB\is_in_transaction($options)) {
962962
$options['writeConcern'] = $this->writeConcern;
963963
}
964964

@@ -982,7 +982,7 @@ public function mapReduce(JavascriptInterface $map, JavascriptInterface $reduce,
982982
*/
983983
public function replaceOne($filter, $replacement, array $options = [])
984984
{
985-
if ( ! isset($options['writeConcern']) && ! $this->isInTransaction($options)) {
985+
if ( ! isset($options['writeConcern']) && ! \MongoDB\is_in_transaction($options)) {
986986
$options['writeConcern'] = $this->writeConcern;
987987
}
988988

@@ -1007,7 +1007,7 @@ public function replaceOne($filter, $replacement, array $options = [])
10071007
*/
10081008
public function updateMany($filter, $update, array $options = [])
10091009
{
1010-
if ( ! isset($options['writeConcern']) && ! $this->isInTransaction($options)) {
1010+
if ( ! isset($options['writeConcern']) && ! \MongoDB\is_in_transaction($options)) {
10111011
$options['writeConcern'] = $this->writeConcern;
10121012
}
10131013

@@ -1032,7 +1032,7 @@ public function updateMany($filter, $update, array $options = [])
10321032
*/
10331033
public function updateOne($filter, $update, array $options = [])
10341034
{
1035-
if ( ! isset($options['writeConcern']) && ! $this->isInTransaction($options)) {
1035+
if ( ! isset($options['writeConcern']) && ! \MongoDB\is_in_transaction($options)) {
10361036
$options['writeConcern'] = $this->writeConcern;
10371037
}
10381038

@@ -1066,7 +1066,7 @@ public function watch(array $pipeline = [], array $options = [])
10661066
* related to change streams being unsupported instead of an
10671067
* UnsupportedException regarding use of the "readConcern" option from
10681068
* the Aggregate operation class. */
1069-
if ( ! isset($options['readConcern']) && \MongoDB\server_supports_feature($server, self::$wireVersionForReadConcern) && ! $this->isInTransaction($options)) {
1069+
if ( ! isset($options['readConcern']) && \MongoDB\server_supports_feature($server, self::$wireVersionForReadConcern) && ! \MongoDB\is_in_transaction($options)) {
10701070
$options['readConcern'] = $this->readConcern;
10711071
}
10721072

@@ -1098,18 +1098,4 @@ public function withOptions(array $options = [])
10981098

10991099
return new Collection($this->manager, $this->databaseName, $this->collectionName, $options);
11001100
}
1101-
1102-
/**
1103-
* Returns whether we are currently in a transaction
1104-
*
1105-
* @param array $options Command options
1106-
* @return bool
1107-
*/
1108-
private function isInTransaction(array $options)
1109-
{
1110-
if (isset($options['session']) && $options['session'] instanceof \MongoDB\Driver\Session && $options['session']->isInTransaction()) {
1111-
return true;
1112-
}
1113-
return false;
1114-
}
11151101
}

src/functions.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,22 @@ function is_first_key_operator($document)
110110
return (isset($firstKey[0]) && $firstKey[0] === '$');
111111
}
112112

113+
/**
114+
* Returns whether we are currently in a transaction
115+
*
116+
* @internal
117+
* @param array $options Command options
118+
* @return bool
119+
*/
120+
function is_in_transaction(array $options)
121+
{
122+
if (isset($options['session']) && $options['session'] instanceof \MongoDB\Driver\Session && $options['session']->isInTransaction()) {
123+
return true;
124+
}
125+
126+
return false;
127+
}
128+
113129
/**
114130
* Return whether the aggregation pipeline ends with an $out operator.
115131
*

0 commit comments

Comments
 (0)