| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Websmurf\LaravelCassandra; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 5 |  |  | class Cassandra | 
            
                                                                        
                            
            
                                    
            
            
                | 6 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 8 |  |  |     /** @var \Cassandra\Cluster */ | 
            
                                                                        
                            
            
                                    
            
            
                | 9 |  |  |     protected $cluster; | 
            
                                                                        
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 11 |  |  |     /** @var  /Cassandra\Session */ | 
            
                                                                        
                            
            
                                    
            
            
                | 12 |  |  |     protected $session; | 
            
                                                                        
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |      * Create a new connection instance with the provided configuration | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 | 3 |  |     public function __construct() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |         // Set up connection details | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 | 3 |  |         $builder = \Cassandra::cluster(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         // Fetch configured port and set it, if it's provided | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 | 3 |  |         $port = config('cassandra.port'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 | 3 |  |         if ( ! empty( $port )) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 | 3 |  |             $builder->withPort($port); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 | 3 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         // Fetch configured default page size and set it, if it's provided | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 | 3 |  |         $defaultPageSize = config('cassandra.defaultPageSize'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 | 3 |  |         if ( ! empty( $defaultPageSize )) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 | 3 |  |             $builder->withDefaultPageSize($defaultPageSize); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 | 3 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         // Fetch configured default consistency level and set it, if it's provided | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 3 |  |         $defaultConsistency = config('cassandra.withDefaultConsistency'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 3 |  |         if ( ! empty( $defaultConsistency )) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 3 |  |             $builder->withDefaultConsistency($defaultConsistency); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 | 3 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         // Set contact end points | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 3 |  |         call_user_func_array([ $builder, "withContactPoints" ], config('cassandra.contactpoints')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         // Connect to cluster | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 3 |  |         $this->cluster = $builder->build(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         // Create a connect to the keyspace on cluster | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 3 |  |         $this->session = $this->cluster->connect(config('cassandra.keyspace')); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 49 | 3 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |      * Create a prepared statement | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |      * @param string                           $cql | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |      * @param \Cassandra\ExecutionOptions|null $options | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |      * @return \Cassandra\PreparedStatement | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 2 |  |     public function prepare($cql, \Cassandra\ExecutionOptions $options = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         // Crazy fall back due to checks in the datastax php library | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 2 |  |         if(is_null($options)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 2 |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 2 |  |             $statement = $this->session->prepare($cql); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 2 |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             $statement = $this->session->prepare($cql, $options); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 2 |  |         return $statement; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 70 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |      * Execute a cassandra query statement | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |      * @param \Cassandra\Statement             $statement | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |      * @param \Cassandra\ExecutionOptions|null $options | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |      * @return \Cassandra\Rows | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 1 |  |     public function execute(\Cassandra\Statement $statement, \Cassandra\ExecutionOptions $options = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         // Crazy fall back due to checks in the datastax php library | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 1 |  |         if(is_null($options)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 1 |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 1 |  |             $rows = $this->session->execute($statement); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 1 |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |             $rows = $this->session->execute($statement, $options); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 | 1 |  |         return $rows; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 92 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 93 |  |  | } |