99namespace LeanMapperQuery ;
1010
1111use LeanMapper ;
12- use LeanMapperQuery \Exception \InvalidArgumentException ;
13- use LeanMapperQuery \Exception \InvalidRelationshipException ;
14- use LeanMapperQuery \Exception \InvalidStateException ;
15- use LeanMapperQuery \Exception \MemberAccessException ;
16- use LeanMapperQuery \Exception \NonExistingMethodException ;
1712use LeanMapper \Fluent ;
1813use LeanMapper \IMapper ;
1914use LeanMapper \ImplicitFilters ;
2015use LeanMapper \Reflection \Property ;
2116use LeanMapper \Relationship ;
2217use LeanMapper \Result ;
18+ use LeanMapperQuery \Exception \InvalidArgumentException ;
19+ use LeanMapperQuery \Exception \InvalidRelationshipException ;
20+ use LeanMapperQuery \Exception \InvalidStateException ;
21+ use LeanMapperQuery \Exception \MemberAccessException ;
22+ use LeanMapperQuery \Exception \NonExistingMethodException ;
2323
2424/**
2525 * @author Michal Bohuslávek
@@ -52,12 +52,12 @@ class Query implements IQuery
5252 * @var array
5353 */
5454 private static $ placeholders = [
55- 'string ' => '%s ' ,
56- 'boolean ' => '%b ' ,
57- 'integer ' => '%i ' ,
58- 'float ' => '%f ' ,
55+ 'string ' => '%s ' ,
56+ 'boolean ' => '%b ' ,
57+ 'integer ' => '%i ' ,
58+ 'float ' => '%f ' ,
5959 'DateTime ' => '%t ' ,
60- 'Date ' => '%d ' ,
60+ 'Date ' => '%d ' ,
6161 ];
6262
6363 ////////////////////////////////////////////////////
@@ -487,8 +487,17 @@ public function applyQuery(Fluent $fluent, IMapper $mapper, QueryTarget\ITarget
487487 return $ fluent ;
488488 }
489489
490+ private static $ fluentCache = [];
491+
490492 private function apply (Fluent $ fluent , IMapper $ mapper , $ sourceTableName = null )
491493 {
494+ if ($ this ->hash !== null ) {
495+ $ sum = hash_final ($ this ->hash );
496+ if (array_key_exists ($ sum , self ::$ fluentCache )) {
497+ return self ::$ fluentCache [$ sum ];
498+ }
499+ }
500+
492501 // NOTE:
493502 // $fluent is expected to have called method Fluent::from
494503 // with pure table name as an argument. For example:
@@ -543,6 +552,10 @@ private function apply(Fluent $fluent, IMapper $mapper, $sourceTableName = null)
543552 call_user_func_array ([$ this , $ method ], $ args );
544553 }
545554
555+ if ($ this ->hash !== null ) {
556+ self ::$ fluentCache [$ sum ] = $ fluent ;
557+ }
558+
546559 // Reset fluent.
547560 $ this ->fluent = null ;
548561 return $ fluent ;
@@ -556,6 +569,8 @@ public function getStrategy()
556569 return empty ($ this ->limitQueue ) ? Result::STRATEGY_IN : Result::STRATEGY_UNION ;
557570 }
558571
572+ private $ hash = null ;
573+
559574 /**
560575 * Enqueues command.
561576 * @param string $name Command name
@@ -570,6 +585,12 @@ public function __call($name, array $args)
570585 throw new NonExistingMethodException ("Command ' $ name' doesn't exist. To register this command there should be defined protected method " . get_called_class () . ":: $ method. " );
571586 }
572587
588+ if ($ this ->hash === null ) {
589+ $ this ->hash = hash_init ('sha1 ' );
590+ }
591+
592+ hash_update ($ this ->hash , $ method . json_encode ($ args ));
593+
573594 switch ($ name ) {
574595 case 'limit ' :
575596 case 'offset ' :
0 commit comments