| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * Phossa Project | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * PHP version 5.4 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * @category  Library | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * @package   Phossa2\Query | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * @copyright Copyright (c) 2016 phossa.com | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * @license   http://mit-license.org/ MIT License | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * @link      http://www.phossa.com/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | /*# declare(strict_types=1); */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | namespace Phossa2\Query\Traits\Clause; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Phossa2\Query\Interfaces\Clause\UnionInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * UnionTrait | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * Implementation of UnionInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  * @package Phossa2\Query | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  * @author  Hong Zhang <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  * @see     UnionInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  * @version 2.0.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  * @since   2.0.0 added | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | trait UnionTrait | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     use AbstractTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * 0 NO, 1 YES, 2 UNION ALL | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * @var    int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * @access protected | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     protected $is_union = UnionInterface::UNION_NOT; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * {@inheritDoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     public function union()/*# : SelectStatementInterface */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         $this->is_union = UnionInterface::UNION_YES; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         return $this->getBuilder()->select()->table('')->setPrevious($this); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      * {@inheritDoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     public function unionAll()/*# : SelectStatementInterface */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         $this->is_union = UnionInterface::UNION_ALL; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         return $this->getBuilder()->select()->table('')->setPrevious($this); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * Override `getStatement()` in StatementAbstract | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * {@inheritDoc} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 63 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |     public function getStatement(array $settings = [])/*# : string */ | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |         // combine settings | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |         $settings = $this->combineSettings($settings); | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |         // statements | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |         $sql = []; | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |         // build previous statement | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |         if ($this->hasPrevious()) { | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |             $sql[] = $this->getPrevious()->getStatement($settings); | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |         // build current sql | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |         $sql[] = $this->buildSql($settings); | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |         // replace with ?, :name or real values | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |         return $this->bindValues(join($settings['seperator'], $sql), $settings); | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      * Build UNION/UNION ALL | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      * @param  string $prefix | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |      * @param  array $settings | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |      * @access protected | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     protected function buildUnion( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         /*# string */ $prefix, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         array $settings | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     )/*# : string */ { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         switch ($this->is_union) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |             case UnionInterface::UNION_YES: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |                 return $settings['seperator'] . 'UNION'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |             case UnionInterface::UNION_ALL: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |                 return $settings['seperator'] . 'UNION ALL'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |             default: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |                 return $prefix; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 105 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 106 |  |  |  |