| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Graze\DataDb\Export\Query; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Graze\DataDb\Dialect\RedshiftDialect; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Graze\DataDb\QueryNode; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Graze\DataDb\QueryNodeInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Graze\DataFile\Format\CsvFormatInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Graze\DataFile\Helper\OptionalLoggerTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Graze\DataFile\Node\FileNodeInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use InvalidArgumentException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Psr\Log\LoggerAwareInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use RuntimeException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | class RedshiftExportQuery extends QueryNode implements LoggerAwareInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     use OptionalLoggerTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      * RedshiftExportQuery constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * @param QueryNodeInterface $base | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * @param FileNodeInterface  $file | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * @param CsvFormatInterface $format | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     public function __construct(QueryNodeInterface $base, FileNodeInterface $file, CsvFormatInterface $format) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         $dialect = $base->getAdapter()->getDialect(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         if (!$dialect instanceof RedshiftDialect) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |             throw new InvalidArgumentException("The supplied base query is must be a redshift query"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         list ($sql, $bind) = $dialect->getExportToCsv($this->getInjectedSql($base), $file, $format); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         parent::__construct($base->getAdapter(), $sql, $bind); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * @param QueryNodeInterface $query | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * @return string | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 43 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 44 |  |  |     private function getInjectedSql(QueryNodeInterface $query) | 
            
                                                        
            
                                    
            
            
                | 45 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 46 |  |  |         $c = 0; | 
            
                                                        
            
                                    
            
            
                | 47 |  |  |         $baseBindings = $query->getBind(); | 
            
                                                        
            
                                    
            
            
                | 48 |  |  |         return preg_replace_callback( | 
            
                                                        
            
                                    
            
            
                | 49 |  |  |             '/\?/', | 
            
                                                        
            
                                    
            
            
                | 50 |  |  |             function ($value) use (&$c, $query, $baseBindings) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 51 |  |  |                 if (!isset($baseBindings[$c])) { | 
            
                                                        
            
                                    
            
            
                | 52 |  |  |                     throw new RuntimeException("Different number of options in query to parameters supplied"); | 
            
                                                        
            
                                    
            
            
                | 53 |  |  |                 } | 
            
                                                        
            
                                    
            
            
                | 54 |  |  |                 return $query->getAdapter()->quoteValue($baseBindings[$c++]); | 
            
                                                        
            
                                    
            
            
                | 55 |  |  |             }, | 
            
                                                        
            
                                    
            
            
                | 56 |  |  |             $query->getSql() | 
            
                                                        
            
                                    
            
            
                | 57 |  |  |         ); | 
            
                                                        
            
                                    
            
            
                | 58 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 59 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 60 |  |  |  | 
            
                        
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.