Skip to content

Commit 3cfb3ed

Browse files
committed
Works well enough to drive a Metadata instance but I am sure there are many bugs.
Signed-off-by: Joey Smith <jsmith@webinertia.net> Signed-off-by: Joey Smith <jsmith@webinertia.net>
1 parent 553e1ec commit 3cfb3ed

15 files changed

+1417
-368
lines changed

src/Adapter.php

Lines changed: 8 additions & 331 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,28 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace Laminas\Db\Mysql;
46

5-
use InvalidArgumentException;
67
use Laminas\Db\Adapter\AbstractAdapter;
7-
use Laminas\Db\Adapter\AdapterInterface;
8-
use Laminas\Db\Adapter\ParameterContainer;
9-
use Laminas\Db\Platform;
108
use Laminas\Db\Adapter\Profiler;
11-
use Laminas\Db\Driver\DriverInterface;
12-
use Laminas\Db\Driver\Pdo\Pdo;
13-
use Laminas\Db\Driver\ResultInterface;
9+
use Laminas\Db\Adapter\Driver\DriverInterface;
10+
use Laminas\Db\Adapter\Platform;
1411
use Laminas\Db\Exception;
1512
use Laminas\Db\ResultSet;
1613

17-
use function func_get_args;
18-
use function in_array;
19-
use function is_array;
20-
use function is_bool;
21-
use function is_string;
22-
use function strpos;
23-
use function strtolower;
24-
2514
/**
2615
* @property DriverInterface $driver
2716
* @property Platform\PlatformInterface $platform
2817
*/
2918
class Adapter extends AbstractAdapter
3019
{
31-
/**
32-
* Query Mode Constants
33-
*/
34-
public const QUERY_MODE_EXECUTE = 'execute';
35-
public const QUERY_MODE_PREPARE = 'prepare';
36-
37-
/**
38-
* Prepare Type Constants
39-
*/
40-
public const PREPARE_TYPE_POSITIONAL = 'positional';
41-
public const PREPARE_TYPE_NAMED = 'named';
42-
43-
public const FUNCTION_FORMAT_PARAMETER_NAME = 'formatParameterName';
44-
public const FUNCTION_QUOTE_IDENTIFIER = 'quoteIdentifier';
45-
public const FUNCTION_QUOTE_VALUE = 'quoteValue';
46-
47-
public const VALUE_QUOTE_SEPARATOR = 'quoteSeparator';
48-
4920
/** @var DriverInterface */
5021
protected $driver;
5122

5223
/** @var Platform\PlatformInterface */
5324
protected $platform;
5425

55-
/** @var Profiler\ProfilerInterface */
56-
protected $profiler;
57-
58-
/** @var ResultSet\ResultSetInterface */
59-
protected $queryResultSetPrototype;
60-
6126
/**
6227
* @deprecated
6328
*
@@ -69,315 +34,27 @@ class Adapter extends AbstractAdapter
6934
* @throws Exception\InvalidArgumentException
7035
*/
7136
public function __construct(
72-
$driver,
73-
?Platform\PlatformInterface $platform = null,
37+
array $parameters,
38+
DriverInterface $driver,
39+
Platform\PlatformInterface $platform,
7440
?ResultSet\ResultSetInterface $queryResultPrototype = null,
7541
?Profiler\ProfilerInterface $profiler = null
7642
) {
77-
// first argument can be an array of parameters
78-
$parameters = [];
7943

80-
if (is_array($driver)) {
81-
$parameters = $driver;
44+
if ($parameters !== []) {
8245
if ($profiler === null && isset($parameters['profiler'])) {
8346
$profiler = $this->createProfiler($parameters);
8447
}
85-
$driver = $this->createDriver($parameters);
86-
} elseif (! $driver instanceof DriverInterface) {
87-
throw new Exception\InvalidArgumentException(
88-
'The supplied or instantiated driver object does not implement ' . DriverInterface::class
89-
);
9048
}
9149

9250
$driver->checkEnvironment();
9351
$this->driver = $driver;
9452

95-
if ($platform === null) {
96-
$platform = $this->createPlatform($parameters);
97-
}
98-
9953
$this->platform = $platform;
10054
$this->queryResultSetPrototype = $queryResultPrototype ?: new ResultSet\ResultSet();
10155

10256
if ($profiler) {
10357
$this->setProfiler($profiler);
10458
}
10559
}
106-
107-
/**
108-
* @return $this Provides a fluent interface
109-
*/
110-
public function setProfiler(Profiler\ProfilerInterface $profiler)
111-
{
112-
$this->profiler = $profiler;
113-
if ($this->driver instanceof Profiler\ProfilerAwareInterface) {
114-
$this->driver->setProfiler($profiler);
115-
}
116-
return $this;
117-
}
118-
119-
/**
120-
* @return null|Profiler\ProfilerInterface
121-
*/
122-
public function getProfiler()
123-
{
124-
return $this->profiler;
125-
}
126-
127-
/**
128-
* getDriver()
129-
*
130-
* @throws Exception\RuntimeException
131-
* @return DriverInterface
132-
*/
133-
public function getDriver()
134-
{
135-
if ($this->driver === null) {
136-
throw new Exception\RuntimeException('Driver has not been set or configured for this adapter.');
137-
}
138-
return $this->driver;
139-
}
140-
141-
/**
142-
* @return Platform\PlatformInterface
143-
*/
144-
public function getPlatform()
145-
{
146-
return $this->platform;
147-
}
148-
149-
/**
150-
* @return ResultSet\ResultSetInterface
151-
*/
152-
public function getQueryResultSetPrototype()
153-
{
154-
return $this->queryResultSetPrototype;
155-
}
156-
157-
/** @return string */
158-
public function getCurrentSchema()
159-
{
160-
return $this->driver->getConnection()->getCurrentSchema();
161-
}
162-
163-
/**
164-
* query() is a convenience function
165-
*
166-
* @param string $sql
167-
* @param string|array|ParameterContainer $parametersOrQueryMode
168-
* @throws Exception\InvalidArgumentException
169-
* @return Driver\StatementInterface|ResultSet\ResultSet
170-
*/
171-
public function query(
172-
$sql,
173-
$parametersOrQueryMode = self::QUERY_MODE_PREPARE,
174-
?ResultSet\ResultSetInterface $resultPrototype = null
175-
) {
176-
if (
177-
is_string($parametersOrQueryMode)
178-
&& in_array($parametersOrQueryMode, [self::QUERY_MODE_PREPARE, self::QUERY_MODE_EXECUTE])
179-
) {
180-
$mode = $parametersOrQueryMode;
181-
$parameters = null;
182-
} elseif (is_array($parametersOrQueryMode) || $parametersOrQueryMode instanceof ParameterContainer) {
183-
$mode = self::QUERY_MODE_PREPARE;
184-
$parameters = $parametersOrQueryMode;
185-
} else {
186-
throw new Exception\InvalidArgumentException(
187-
'Parameter 2 to this method must be a flag, an array, or ParameterContainer'
188-
);
189-
}
190-
191-
if ($mode === self::QUERY_MODE_PREPARE) {
192-
$lastPreparedStatement = $this->driver->createStatement($sql);
193-
$lastPreparedStatement->prepare();
194-
if (is_array($parameters) || $parameters instanceof ParameterContainer) {
195-
if (is_array($parameters)) {
196-
$lastPreparedStatement->setParameterContainer(new ParameterContainer($parameters));
197-
} else {
198-
$lastPreparedStatement->setParameterContainer($parameters);
199-
}
200-
$result = $lastPreparedStatement->execute();
201-
} else {
202-
return $lastPreparedStatement;
203-
}
204-
} else {
205-
$result = $this->driver->getConnection()->execute($sql);
206-
}
207-
208-
if ($result instanceof ResultInterface && $result->isQueryResult()) {
209-
$resultSet = $resultPrototype ?? $this->queryResultSetPrototype;
210-
$resultSetCopy = clone $resultSet;
211-
212-
$resultSetCopy->initialize($result);
213-
214-
return $resultSetCopy;
215-
}
216-
217-
return $result;
218-
}
219-
220-
/**
221-
* Create statement
222-
*
223-
* @param string $initialSql
224-
* @param null|ParameterContainer|array $initialParameters
225-
* @return Driver\StatementInterface
226-
*/
227-
public function createStatement($initialSql = null, $initialParameters = null)
228-
{
229-
$statement = $this->driver->createStatement($initialSql);
230-
if (
231-
$initialParameters === null
232-
|| ! $initialParameters instanceof ParameterContainer
233-
&& is_array($initialParameters)
234-
) {
235-
$initialParameters = new ParameterContainer(is_array($initialParameters) ? $initialParameters : []);
236-
}
237-
$statement->setParameterContainer($initialParameters);
238-
return $statement;
239-
}
240-
241-
public function getHelpers()
242-
{
243-
$functions = [];
244-
$platform = $this->platform;
245-
foreach (func_get_args() as $arg) {
246-
switch ($arg) {
247-
case self::FUNCTION_QUOTE_IDENTIFIER:
248-
$functions[] = function ($value) use ($platform) {
249-
return $platform->quoteIdentifier($value);
250-
};
251-
break;
252-
case self::FUNCTION_QUOTE_VALUE:
253-
$functions[] = function ($value) use ($platform) {
254-
return $platform->quoteValue($value);
255-
};
256-
break;
257-
}
258-
}
259-
}
260-
261-
/**
262-
* @param string $name
263-
* @throws Exception\InvalidArgumentException
264-
* @return DriverInterface|Platform\PlatformInterface
265-
*/
266-
// public function __get($name)
267-
// {
268-
// switch (strtolower($name)) {
269-
// case 'driver':
270-
// return $this->driver;
271-
// case 'platform':
272-
// return $this->platform;
273-
// default:
274-
// throw new Exception\InvalidArgumentException('Invalid magic property on adapter');
275-
// }
276-
// }
277-
278-
/**
279-
* @param array $parameters
280-
* @return DriverInterface
281-
* @throws InvalidArgumentException
282-
* @throws Exception\InvalidArgumentException
283-
*/
284-
protected function createDriver($parameters)
285-
{
286-
if (! isset($parameters['driver'])) {
287-
throw new Exception\InvalidArgumentException(
288-
__FUNCTION__ . ' expects a "driver" key to be present inside the parameters'
289-
);
290-
}
291-
292-
if ($parameters['driver'] instanceof DriverInterface) {
293-
return $parameters['driver'];
294-
}
295-
296-
if (! is_string($parameters['driver'])) {
297-
throw new Exception\InvalidArgumentException(
298-
__FUNCTION__ . ' expects a "driver" to be a string or instance of DriverInterface'
299-
);
300-
}
301-
302-
$options = [];
303-
if (isset($parameters['options'])) {
304-
$options = (array) $parameters['options'];
305-
unset($parameters['options']);
306-
}
307-
308-
$driverName = strtolower($parameters['driver']);
309-
switch ($driverName) {
310-
case 'mysqli':
311-
$driver = new Driver\Mysqli\Driver($parameters, null, null, $options);
312-
break;
313-
case 'pdo':
314-
default:
315-
if ($driverName === 'pdo' || strpos($driverName, 'pdo') === 0) {
316-
$driver = new Pdo($parameters);
317-
}
318-
}
319-
320-
if (! isset($driver) || ! $driver instanceof DriverInterface) {
321-
throw new Exception\InvalidArgumentException('DriverInterface expected');
322-
}
323-
324-
return $driver;
325-
}
326-
327-
/**
328-
* todo: replace with factory
329-
* @param array $parameters
330-
* @return Platform\PlatformInterface
331-
*/
332-
protected function createPlatform(array $parameters)
333-
{
334-
if (isset($parameters['platform'])) {
335-
$platformName = $parameters['platform'];
336-
} elseif ($this->driver instanceof DriverInterface) {
337-
$platformName = $this->driver->getDatabasePlatformName(DriverInterface::NAME_FORMAT_CAMELCASE);
338-
} else {
339-
throw new Exception\InvalidArgumentException(
340-
'A platform could not be determined from the provided configuration'
341-
);
342-
}
343-
344-
// currently only supported by the IbmDb2 & Oracle concrete implementations
345-
$options = $parameters['platform_options'] ?? [];
346-
347-
348-
// todo: replace with factory
349-
switch ($platformName) {
350-
case 'Mysql':
351-
// mysqli or pdo_mysql driver
352-
if ($this->driver instanceof Driver\Mysqli\Driver || $this->driver instanceof Pdo) {
353-
$driver = $this->driver;
354-
} else {
355-
$driver = null;
356-
}
357-
return new Platform\Mysql($driver);
358-
default:
359-
return new Platform\Sql92();
360-
}
361-
}
362-
363-
/**
364-
* todo: remains in abstract adapter
365-
* @param array $parameters
366-
* @return Profiler\ProfilerInterface
367-
* @throws Exception\InvalidArgumentException
368-
*/
369-
// protected function createProfiler($parameters)
370-
// {
371-
// if ($parameters['profiler'] instanceof Profiler\ProfilerInterface) {
372-
// return $parameters['profiler'];
373-
// }
374-
375-
// if (is_bool($parameters['profiler'])) {
376-
// return $parameters['profiler'] === true ? new Profiler\Profiler() : null;
377-
// }
378-
379-
// throw new Exception\InvalidArgumentException(
380-
// '"profiler" parameter must be an instance of ProfilerInterface or a boolean'
381-
// );
382-
// }
38360
}

0 commit comments

Comments
 (0)