| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace ByJG\AnyDataset\Database; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use ByJG\AnyDataset\ConnectionManagement; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use ByJG\AnyDataset\Exception\DatabaseException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use ByJG\AnyDataset\Exception\DatasetException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use ByJG\AnyDataset\Exception\NotAvailableException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use ByJG\AnyDataset\Repository\SQLRelayIterator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | class DbSqlRelayDriver implements DbDriverInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |      * Enter description here... | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      * @var ConnectionManagement | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     protected $connectionManagement; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     /** Used for SQL Relay connections * */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     protected $conn; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     protected $transaction = false; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |     public function __construct($connMngt) | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |         $this->connectionManagement = $connMngt; | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |         $this->conn = sqlrcon_alloc( | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |             $this->connectionManagement->getServer(), | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |             $this->connectionManagement->getPort(), | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |             $this->connectionManagement->getExtraParam("unixsocket"), | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |             $this->connectionManagement->getUsername(), | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |             $this->connectionManagement->getPassword(), | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |             0, | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |             1 | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |         ); | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |         sqlrcon_autoCommitOn($this->conn); | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     public function __destruct() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         if (!is_null($this->conn)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |             sqlrcon_free($this->conn); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     protected function getSQLRelayCursor($sql, $array = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         $cur = sqlrcur_alloc($this->conn); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         if ($array) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |             list($sql, $array) = SqlBind::parseSQL($this->connectionManagement, $sql, $array); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |             sqlrcur_prepareQuery($cur, $sql); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             $bindCount = 1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |             foreach ($array as $key => $value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |                 $field = strval($bindCount ++); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |                 sqlrcur_inputBind($cur, $field, $value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             $success = sqlrcur_executeQuery($cur); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             sqlrcon_endSession($this->conn); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             $success = sqlrcur_sendQuery($cur, $sql); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             sqlrcon_endSession($this->conn); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         if (!$success) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             throw new DatasetException(sqlrcur_errorMessage($cur)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         sqlrcur_lowerCaseColumnNames($cur); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         return $cur; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     public function getIterator($sql, $array = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         $cur = $this->getSQLRelayCursor($sql, $array); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         $iterator = new SQLRelayIterator($cur); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         return $iterator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     public function getScalar($sql, $array = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         $cur = $this->getSQLRelayCursor($sql, $array); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         $scalar = sqlrcur_getField($cur, 0, 0); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         sqlrcur_free($cur); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         return $scalar; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     public function getAllFields($tablename) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         $cur = sqlrcur_alloc($this->conn); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         $success = sqlrcur_sendQuery( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |             $cur, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |             SqlHelper::createSafeSQL("select * from :table", array(":table" => $tablename)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         sqlrcon_endSession($cur); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         if (!$success) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |             throw new DatasetException(sqlrcur_errorMessage($cur)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         $fields = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         $colCount = sqlrcur_colCount($cur); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         for ($col = 0; $col < $colCount; $col++) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |             $fields[] = strtolower(sqlrcur_getColumnName($cur, $col)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         sqlrcur_free($cur); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         return $fields; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |     public function beginTransaction() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         $this->transaction = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         sqlrcon_autoCommitOff($this->conn); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 123 |  | View Code Duplication |     public function commitTransaction() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |         if ($this->transaction) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |             $this->transaction = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |             $ret = sqlrcon_commit($this->conn); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |             if ($ret === 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |                 throw new DatabaseException('Commit failed'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |             } elseif ($ret === -1) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |                 throw new DatabaseException('An error occurred. Commit failed'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |             sqlrcon_autoCommitOn($this->conn); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 139 |  | View Code Duplication |     public function rollbackTransaction() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |         if ($this->transaction) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |             $this->transaction = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |             $ret = sqlrcon_rollback($this->conn); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |             if ($ret === 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |                 throw new DatabaseException('Commit failed'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |             } elseif ($ret === -1) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |                 throw new DatabaseException('An error occurred. Commit failed'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |             sqlrcon_autoCommitOn($this->conn); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |     public function executeSql($sql, $array = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |         $cur = $this->getSQLRelayCursor($sql, $array); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |         sqlrcur_free($cur); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |         return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |     public function getDbConnection() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |         return $this->conn; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |     public function getAttribute($name) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |         throw new NotAvailableException('Method not implemented for SQL Relay Driver'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |     public function setAttribute($name, $value) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |         throw new NotAvailableException('Method not implemented for SQL Relay Driver'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 180 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 181 |  |  |  | 
            
                        
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.