|
1 | 1 | <?php |
2 | 2 |
|
3 | | -declare(strict_types=1); |
| 3 | +namespace Laminas\Db\Adapter\Mysql; |
4 | 4 |
|
5 | | -namespace Laminas\Db\Mysql; |
6 | | - |
7 | | -use Laminas\Db\Adapter\AbstractAdapter; |
8 | | -use Laminas\Db\Adapter\Profiler; |
9 | 5 | use Laminas\Db\Adapter\Driver\DriverInterface; |
10 | | -use Laminas\Db\Adapter\Platform; |
11 | | -use Laminas\Db\Exception; |
12 | | -use Laminas\Db\ResultSet; |
| 6 | +use Laminas\Db\Adapter\Platform\PlatformInterface; |
| 7 | +use Laminas\Db\Adapter\AbstractAdapter; |
| 8 | +use Laminas\Db\Adapter\Exception; |
| 9 | + |
| 10 | +use function is_string; |
| 11 | +use function str_starts_with; |
| 12 | +use function strtolower; |
13 | 13 |
|
14 | 14 | /** |
15 | | - * @property DriverInterface $driver |
| 15 | + * @property Driver\DriverInterface $driver |
16 | 16 | * @property Platform\PlatformInterface $platform |
17 | 17 | */ |
18 | 18 | class Adapter extends AbstractAdapter |
19 | 19 | { |
20 | | - /** @var DriverInterface */ |
21 | | - protected $driver; |
22 | | - |
23 | | - /** @var Platform\PlatformInterface */ |
24 | | - protected $platform; |
25 | | - |
26 | | - /** |
27 | | - * @deprecated |
28 | | - * |
29 | | - * @var Driver\StatementInterface |
30 | | - */ |
31 | | - protected $lastPreparedStatement; |
32 | | - /** |
33 | | - * @param DriverInterface|array $driver |
34 | | - * @throws Exception\InvalidArgumentException |
35 | | - */ |
36 | | - public function __construct( |
37 | | - array $parameters, |
38 | | - DriverInterface $driver, |
39 | | - Platform\PlatformInterface $platform, |
40 | | - ?ResultSet\ResultSetInterface $queryResultPrototype = null, |
41 | | - ?Profiler\ProfilerInterface $profiler = null |
42 | | - ) { |
43 | | - |
44 | | - if ($parameters !== []) { |
45 | | - if ($profiler === null && isset($parameters['profiler'])) { |
46 | | - $profiler = $this->createProfiler($parameters); |
47 | | - } |
48 | | - } |
49 | | - |
50 | | - $driver->checkEnvironment(); |
51 | | - $this->driver = $driver; |
52 | | - |
53 | | - $this->platform = $platform; |
54 | | - $this->queryResultSetPrototype = $queryResultPrototype ?: new ResultSet\ResultSet(); |
55 | | - |
56 | | - if ($profiler) { |
57 | | - $this->setProfiler($profiler); |
| 20 | + public function getCurrentSchema(): string |
| 21 | + { |
| 22 | + return $this->driver->getConnection()->getCurrentSchema(); |
| 23 | + } |
| 24 | + |
| 25 | + protected function createDriver(array $parameters): DriverInterface |
| 26 | + { |
| 27 | + if (! isset($parameters['driver'])) { |
| 28 | + throw new Exception\InvalidArgumentException( |
| 29 | + __FUNCTION__ . ' expects a "driver" key to be present inside the parameters' |
| 30 | + ); |
| 31 | + } |
| 32 | + |
| 33 | + if ($parameters['driver'] instanceof DriverInterface) { |
| 34 | + return $parameters['driver']; |
| 35 | + } |
| 36 | + |
| 37 | + if (! is_string($parameters['driver'])) { |
| 38 | + throw new Exception\InvalidArgumentException( |
| 39 | + __FUNCTION__ . ' expects a "driver" to be a string or instance of DriverInterface' |
| 40 | + ); |
| 41 | + } |
| 42 | + |
| 43 | + $options = []; |
| 44 | + if (isset($parameters['options'])) { |
| 45 | + $options = (array) $parameters['options']; |
| 46 | + unset($parameters['options']); |
58 | 47 | } |
| 48 | + |
| 49 | + $driverName = strtolower($parameters['driver']); |
| 50 | + switch ($driverName) { |
| 51 | + case 'mysqli': |
| 52 | + $driver = new Driver\Mysqli\Mysqli($parameters, null, null, $options); |
| 53 | + break; |
| 54 | + case 'pdo': |
| 55 | + default: |
| 56 | + if ($driverName === 'pdo' || str_starts_with($driverName, 'pdo')) { |
| 57 | + $driver = new Driver\Pdo\Pdo($parameters); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + if (! isset($driver) || ! $driver instanceof DriverInterface) { |
| 62 | + throw new Exception\InvalidArgumentException('DriverInterface expected'); |
| 63 | + } |
| 64 | + |
| 65 | + return $driver; |
| 66 | + } |
| 67 | + |
| 68 | + protected function createPlatform(array $parameters): PlatformInterface |
| 69 | + { |
| 70 | + if (isset($parameters['platform'])) { |
| 71 | + $platformName = $parameters['platform']; |
| 72 | + } elseif ($this->driver instanceof DriverInterface) { |
| 73 | + $platformName = $this->driver->getDatabasePlatformName(); |
| 74 | + } else { |
| 75 | + throw new Exception\InvalidArgumentException( |
| 76 | + 'A platform could not be determined from the provided configuration' |
| 77 | + ); |
| 78 | + } |
| 79 | + |
| 80 | + // currently only supported by the IbmDb2 & Oracle concrete implementations |
| 81 | + //$options = $parameters['platform_options'] ?? []; |
| 82 | + |
| 83 | + // mysqli or pdo_mysql driver |
| 84 | + if ($this->driver instanceof Driver\Mysqli\Mysqli || $this->driver instanceof Driver\Pdo\Pdo) { |
| 85 | + $driver = $this->driver; |
| 86 | + } else { |
| 87 | + $driver = null; |
| 88 | + } |
| 89 | + |
| 90 | + return new Platform\Mysql($driver); |
59 | 91 | } |
60 | 92 | } |
0 commit comments