Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 24 additions & 5 deletions docs/book/sql.md
Original file line number Diff line number Diff line change
Expand Up @@ -251,11 +251,11 @@ $select->from('foo')->where(['x = 5', 'y = z']);
If you provide an associative array with string keys, any value with a string
key will be cast as follows:

PHP value | Predicate type
--------- | --------------
`null` | `Predicate\IsNull`
`array` | `Predicate\In`
`string` | `Predicate\Operator`, where the key is the identifier.
| PHP value | Predicate type |
|-----------|--------------------------------------------------------|
| `null` | `Predicate\IsNull` |
| `array` | `Predicate\In` |
| `string` | `Predicate\Operator`, where the key is the identifier. |

As an example:

Expand Down Expand Up @@ -642,6 +642,25 @@ class Expression implements ExpressionInterface, PredicateInterface
}
```

Expression parameters can be supplied either as a single scalar, an array of values, or as an array of value/types for more granular escaping.

```php
$select
->from('foo')
->columns([
new Expression(
'(COUNT(?) + ?) AS ?',
[
['some_column' => ExpressionInterface::TYPE_IDENTIFIER],
[5 => ExpressionInterface::TYPE_VALUE],
['bar' => ExpressionInterface::TYPE_IDENTIFIER],
],
),
]);

// Produces SELECT (COUNT("some_column") + '5') AS "bar" FROM "foo"
```

### isNull($identifier)

```php
Expand Down
128 changes: 30 additions & 98 deletions src/Adapter/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

namespace Laminas\Db\Adapter;

use InvalidArgumentException;
use Exception as PhpException;
use Laminas\Db\ResultSet;

use function func_get_args;
use function in_array;
use function is_array;
use function is_bool;
use function is_string;
use function strpos;
use function str_starts_with;
use function strtolower;

/**
Expand Down Expand Up @@ -43,24 +43,15 @@ class Adapter implements AdapterInterface, Profiler\ProfilerAwareInterface
/** @var Platform\PlatformInterface */
protected $platform;

/** @var Profiler\ProfilerInterface */
protected $profiler;
protected Profiler\ProfilerInterface $profiler;

/** @var ResultSet\ResultSetInterface */
protected $queryResultSetPrototype;
protected ResultSet\ResultSetInterface $queryResultSetPrototype;

/**
* @deprecated
*
* @var Driver\StatementInterface
*/
protected $lastPreparedStatement;
/**
* @param Driver\DriverInterface|array $driver
* @throws Exception\InvalidArgumentException
*/
public function __construct(
$driver,
Driver\DriverInterface|array $driver,
?Platform\PlatformInterface $platform = null,
?ResultSet\ResultSetInterface $queryResultPrototype = null,
?Profiler\ProfilerInterface $profiler = null
Expand Down Expand Up @@ -98,7 +89,7 @@ public function __construct(
/**
* @return $this Provides a fluent interface
*/
public function setProfiler(Profiler\ProfilerInterface $profiler)
public function setProfiler(Profiler\ProfilerInterface $profiler): self
{
$this->profiler = $profiler;
if ($this->driver instanceof Profiler\ProfilerAwareInterface) {
Expand All @@ -107,63 +98,48 @@ public function setProfiler(Profiler\ProfilerInterface $profiler)
return $this;
}

/**
* @return null|Profiler\ProfilerInterface
*/
public function getProfiler()
public function getProfiler(): ?Profiler\ProfilerInterface
{
return $this->profiler;
}

/**
* getDriver()
*
* @throws Exception\RuntimeException
* @return Driver\DriverInterface
*/
public function getDriver()
public function getDriver(): Driver\DriverInterface|array
{
if ($this->driver === null) {
throw new Exception\RuntimeException('Driver has not been set or configured for this adapter.');
}
return $this->driver;
}

/**
* @return Platform\PlatformInterface
*/
public function getPlatform()
public function getPlatform(): ?Platform\PlatformInterface
{
return $this->platform;
}

/**
* @return ResultSet\ResultSetInterface
*/
public function getQueryResultSetPrototype()
public function getQueryResultSetPrototype(): ResultSet\ResultSetInterface
{
return $this->queryResultSetPrototype;
}

/** @return string */
public function getCurrentSchema()
public function getCurrentSchema(): string
{
return $this->driver->getConnection()->getCurrentSchema();
}

/**
* query() is a convenience function
*
* @param string $sql
* @param string|array|ParameterContainer $parametersOrQueryMode
* @throws Exception\InvalidArgumentException
* @return Driver\StatementInterface|ResultSet\ResultSet
* @throws PhpException
*/
public function query(
$sql,
$parametersOrQueryMode = self::QUERY_MODE_PREPARE,
string $sql,
ParameterContainer|array|string $parametersOrQueryMode = self::QUERY_MODE_PREPARE,
?ResultSet\ResultSetInterface $resultPrototype = null
) {
): Driver\StatementInterface|ResultSet\ResultSet|Driver\ResultInterface {
if (
is_string($parametersOrQueryMode)
&& in_array($parametersOrQueryMode, [self::QUERY_MODE_PREPARE, self::QUERY_MODE_EXECUTE])
Expand Down Expand Up @@ -210,13 +186,11 @@ public function query(

/**
* Create statement
*
* @param string $initialSql
* @param null|ParameterContainer|array $initialParameters
* @return Driver\StatementInterface
*/
public function createStatement($initialSql = null, $initialParameters = null)
{
public function createStatement(
?string $initialSql = null,
ParameterContainer|array|null $initialParameters = null
): Driver\StatementInterface {
$statement = $this->driver->createStatement($initialSql);
if (
$initialParameters === null
Expand Down Expand Up @@ -250,29 +224,19 @@ public function getHelpers()
}

/**
* @param string $name
* @throws Exception\InvalidArgumentException
* @return Driver\DriverInterface|Platform\PlatformInterface
*/
public function __get($name)
public function __get(string $name)
{
switch (strtolower($name)) {
case 'driver':
return $this->driver;
case 'platform':
return $this->platform;
default:
throw new Exception\InvalidArgumentException('Invalid magic property on adapter');
}
return match (strtolower($name)) {
'driver' => $this->driver,
'platform' => $this->platform,
default => throw new Exception\InvalidArgumentException('Invalid magic property on adapter'),
};
}

/**
* @param array $parameters
* @return Driver\DriverInterface
* @throws InvalidArgumentException
* @throws Exception\InvalidArgumentException
*/
protected function createDriver($parameters)
protected function createDriver(array $parameters): Driver\DriverInterface
{
if (! isset($parameters['driver'])) {
throw new Exception\InvalidArgumentException(
Expand Down Expand Up @@ -315,7 +279,7 @@ protected function createDriver($parameters)
break;
case 'pdo':
default:
if ($driverName === 'pdo' || strpos($driverName, 'pdo') === 0) {
if ($driverName === 'pdo' || str_starts_with($driverName, 'pdo')) {
$driver = new Driver\Pdo\Pdo($parameters);
}
}
Expand All @@ -327,16 +291,12 @@ protected function createDriver($parameters)
return $driver;
}

/**
* @param array $parameters
* @return Platform\PlatformInterface
*/
protected function createPlatform(array $parameters)
protected function createPlatform(array $parameters): Platform\PlatformInterface
{
if (isset($parameters['platform'])) {
$platformName = $parameters['platform'];
} elseif ($this->driver instanceof Driver\DriverInterface) {
$platformName = $this->driver->getDatabasePlatformName(Driver\DriverInterface::NAME_FORMAT_CAMELCASE);
$platformName = $this->driver->getDatabasePlatformName();
} else {
throw new Exception\InvalidArgumentException(
'A platform could not be determined from the provided configuration'
Expand Down Expand Up @@ -387,12 +347,7 @@ protected function createPlatform(array $parameters)
}
}

/**
* @param array $parameters
* @return Profiler\ProfilerInterface
* @throws Exception\InvalidArgumentException
*/
protected function createProfiler($parameters)
protected function createProfiler(array $parameters): ?Profiler\ProfilerInterface
{
if ($parameters['profiler'] instanceof Profiler\ProfilerInterface) {
return $parameters['profiler'];
Expand All @@ -406,27 +361,4 @@ protected function createProfiler($parameters)
'"profiler" parameter must be an instance of ProfilerInterface or a boolean'
);
}

/**
* @deprecated
*
* @param array $parameters
* @return Driver\DriverInterface
* @throws InvalidArgumentException
* @throws Exception\InvalidArgumentException
*/
protected function createDriverFromParameters(array $parameters)
{
return $this->createDriver($parameters);
}

/**
* @deprecated
*
* @return Platform\PlatformInterface
*/
protected function createPlatformFromDriver(Driver\DriverInterface $driver)
{
return $this->createPlatform($driver);
}
}
9 changes: 0 additions & 9 deletions src/Adapter/Driver/Oci8/Statement.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,6 @@ class Statement implements StatementInterface, Profiler\ProfilerAwareInterface
/** @var resource */
protected $resource;

/**
* @internal
* @deprecated
*
* @var bool
*/
public $parametersBound;

/**
* Is prepared
*
Expand Down Expand Up @@ -323,7 +315,6 @@ protected function bindParametersFromContainer()
public function __clone()
{
$this->isPrepared = false;
$this->parametersBound = false;
$this->resource = null;
if ($this->parameterContainer) {
$this->parameterContainer = clone $this->parameterContainer;
Expand Down
Loading