| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace Yiisoft\Db\Tests\Support; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Yiisoft\Cache\ArrayCache; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Yiisoft\Cache\Cache; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Yiisoft\Cache\CacheInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Yiisoft\Db\Cache\QueryCache; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Yiisoft\Db\Cache\SchemaCache; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Yiisoft\Db\Driver\PDO\ConnectionPDOInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Yiisoft\Db\Exception\Exception; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Yiisoft\Db\Exception\InvalidConfigException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use function explode; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use function file_get_contents; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use function preg_replace; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use function str_replace; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use function trim; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | final class DbHelper | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 23 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |     public static function getCache(): CacheInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |         return new Cache(new ArrayCache()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     public static function getQueryCache(): QueryCache | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         return new QueryCache(self::getCache()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     public static function getSchemaCache(): SchemaCache | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         return new SchemaCache(self::getCache()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * Loads the fixture into the database. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * @throws Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * @throws InvalidConfigException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     public static function loadFixture(ConnectionPDOInterface $db, string $fixture): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         $db->open(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         if ($db->getName() === 'oci') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             [$drops, $creates] = explode('/* STATEMENTS */', file_get_contents($fixture), 2); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |             [$statements, $triggers, $data] = explode('/* TRIGGERS */', $creates, 3); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |             $lines = array_merge( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |                 explode('--', $drops), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |                 explode(';', $statements), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |                 explode('/', $triggers), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |                 explode(';', $data) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |             $lines = explode(';', file_get_contents($fixture)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         foreach ($lines as $line) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             if (trim($line) !== '') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |                 $db->getPDO()?->exec($line); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      * Adjust dbms specific escaping. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      * @param string $sql string SQL statement to adjust. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      * @param string $driverName string DBMS name. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     public static function replaceQuotes(string $sql, string $driverName): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         return match ($driverName) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             'mysql', 'sqlite' => str_replace(['[[', ']]'], '`', $sql), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             'oci' => str_replace(['[[', ']]'], '"', $sql), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |             'pgsql' => str_replace(['\\[', '\\]'], ['[', ']'], preg_replace('/(\[\[)|((?<!(\[))]])/', '"', $sql)), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |             'db', 'sqlsrv' => str_replace(['[[', ']]'], ['[', ']'], $sql), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |             default => $sql, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         }; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 87 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 88 |  |  |  |