Exceptions
1 year ago
Connection.php
1 year ago
ConvertParameters.php
1 year ago
Driver.php
1 year ago
Result.php
2 months ago
Statement.php
1 year ago
index.php
1 year ago
Statement.php
74 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\Doctrine\WPDB; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Doctrine\WPDB\Exceptions\NotSupportedException; |
| 9 | use MailPoetVendor\Doctrine\DBAL\Driver\Result; |
| 10 | use MailPoetVendor\Doctrine\DBAL\Driver\Statement as StatementInterface; |
| 11 | use MailPoetVendor\Doctrine\DBAL\ParameterType; |
| 12 | use MailPoetVendor\Doctrine\DBAL\SQL\Parser; |
| 13 | |
| 14 | class Statement implements StatementInterface { |
| 15 | private Connection $connection; |
| 16 | private Parser $parser; |
| 17 | private string $sql; |
| 18 | private array $params = []; |
| 19 | |
| 20 | public function __construct( |
| 21 | Connection $connection, |
| 22 | string $sql |
| 23 | ) { |
| 24 | $this->connection = $connection; |
| 25 | $this->parser = new Parser(false); |
| 26 | $this->sql = $sql; |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * @param string|int $param |
| 31 | * @param mixed $value |
| 32 | * @param int $type |
| 33 | * @return true |
| 34 | */ |
| 35 | public function bindValue($param, $value, $type = ParameterType::STRING) { |
| 36 | $this->params[$param] = [$param, $value, $type]; |
| 37 | return true; |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * @param string|int $param |
| 42 | * @param mixed $variable |
| 43 | * @param int $type |
| 44 | * @param int|null $length |
| 45 | * @return true |
| 46 | */ |
| 47 | public function bindParam($param, &$variable, $type = ParameterType::STRING, $length = null) { |
| 48 | throw new NotSupportedException( |
| 49 | 'Statement::bindParam() is deprecated in Doctrine and not implemented in WPDB driver. Use Statement::bindValue() instead.' |
| 50 | ); |
| 51 | } |
| 52 | |
| 53 | public function execute($params = null): Result { |
| 54 | if ($params !== null) { |
| 55 | throw new NotSupportedException( |
| 56 | 'Statement::execute() with parameters is deprecated in Doctrine and not implemented in WPDB driver. Use Statement::bindValue() instead.' |
| 57 | ); |
| 58 | } |
| 59 | |
| 60 | // Convert '?' parameters to WPDB format (sprintf-like: '%s', '%d', ...), |
| 61 | // and add support for named parameters that are not supported by mysqli. |
| 62 | $visitor = new ConvertParameters($this->params); |
| 63 | $this->parser->parse($this->sql, $visitor); |
| 64 | $sql = $visitor->getSQL(); |
| 65 | $values = $visitor->getValues(); |
| 66 | |
| 67 | global $wpdb; |
| 68 | $query = count($values) > 0 |
| 69 | ? $wpdb->prepare($sql, $values) // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
| 70 | : $sql; |
| 71 | return $this->connection->query($query); |
| 72 | } |
| 73 | } |
| 74 |