@@ -25,7 +25,7 @@ |
||
| 25 | 25 | */ |
| 26 | 26 | public static function __callStatic($method, $args) |
| 27 | 27 | { |
| 28 | - if (!static::$queryBuilderInstance) { |
|
| 28 | + if ( ! static::$queryBuilderInstance) { |
|
| 29 | 29 | static::$queryBuilderInstance = new QueryBuilderHandler(); |
| 30 | 30 | } |
| 31 | 31 | |
@@ -12,36 +12,36 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | class AliasFacade |
| 14 | 14 | { |
| 15 | - /** |
|
| 16 | - * @var QueryBuilderHandler|null |
|
| 17 | - */ |
|
| 18 | - protected static $queryBuilderInstance; |
|
| 15 | + /** |
|
| 16 | + * @var QueryBuilderHandler|null |
|
| 17 | + */ |
|
| 18 | + protected static $queryBuilderInstance; |
|
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * @param string $method |
|
| 22 | - * @param mixed[] $args |
|
| 23 | - * |
|
| 24 | - * @return mixed |
|
| 25 | - */ |
|
| 26 | - public static function __callStatic($method, $args) |
|
| 27 | - { |
|
| 28 | - if (!static::$queryBuilderInstance) { |
|
| 29 | - static::$queryBuilderInstance = new QueryBuilderHandler(); |
|
| 30 | - } |
|
| 20 | + /** |
|
| 21 | + * @param string $method |
|
| 22 | + * @param mixed[] $args |
|
| 23 | + * |
|
| 24 | + * @return mixed |
|
| 25 | + */ |
|
| 26 | + public static function __callStatic($method, $args) |
|
| 27 | + { |
|
| 28 | + if (!static::$queryBuilderInstance) { |
|
| 29 | + static::$queryBuilderInstance = new QueryBuilderHandler(); |
|
| 30 | + } |
|
| 31 | 31 | |
| 32 | - // Call the non-static method from the class instance |
|
| 33 | - $callable = [static::$queryBuilderInstance, $method]; |
|
| 32 | + // Call the non-static method from the class instance |
|
| 33 | + $callable = [static::$queryBuilderInstance, $method]; |
|
| 34 | 34 | |
| 35 | - return is_callable($callable) |
|
| 36 | - ? call_user_func_array($callable, $args) |
|
| 37 | - : null; |
|
| 38 | - } |
|
| 35 | + return is_callable($callable) |
|
| 36 | + ? call_user_func_array($callable, $args) |
|
| 37 | + : null; |
|
| 38 | + } |
|
| 39 | 39 | |
| 40 | - /** |
|
| 41 | - * @param QueryBuilderHandler $queryBuilderInstance |
|
| 42 | - */ |
|
| 43 | - public static function setQueryBuilderInstance($queryBuilderInstance): void |
|
| 44 | - { |
|
| 45 | - static::$queryBuilderInstance = $queryBuilderInstance; |
|
| 46 | - } |
|
| 40 | + /** |
|
| 41 | + * @param QueryBuilderHandler $queryBuilderInstance |
|
| 42 | + */ |
|
| 43 | + public static function setQueryBuilderInstance($queryBuilderInstance): void |
|
| 44 | + { |
|
| 45 | + static::$queryBuilderInstance = $queryBuilderInstance; |
|
| 46 | + } |
|
| 47 | 47 | } |
@@ -7,168 +7,168 @@ |
||
| 7 | 7 | |
| 8 | 8 | class Binding |
| 9 | 9 | { |
| 10 | - public const STRING = '%s'; |
|
| 11 | - public const BOOL = '%d'; |
|
| 12 | - public const INT = '%d'; |
|
| 13 | - public const FLOAT = '%f'; |
|
| 14 | - public const JSON = '%s'; |
|
| 15 | - public const RAW = ':RAW'; |
|
| 16 | - |
|
| 17 | - /** |
|
| 18 | - * Holds the value to bind with |
|
| 19 | - * |
|
| 20 | - * @var mixed |
|
| 21 | - */ |
|
| 22 | - protected $value; |
|
| 23 | - |
|
| 24 | - /** |
|
| 25 | - * Denotes the type |
|
| 26 | - * |
|
| 27 | - * @var string|null |
|
| 28 | - */ |
|
| 29 | - protected $type; |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * Denotes if the field is a RAW value |
|
| 33 | - * |
|
| 34 | - * @var bool |
|
| 35 | - */ |
|
| 36 | - protected $isRaw = false; |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * @param mixed $value |
|
| 40 | - * @param string|null $type |
|
| 41 | - */ |
|
| 42 | - public function __construct($value, ?string $type = null) |
|
| 43 | - { |
|
| 44 | - $this->verifyType($type); |
|
| 45 | - $this->value = $value; |
|
| 46 | - $this->type = $type; |
|
| 47 | - if (self::RAW === $type) { |
|
| 48 | - $this->isRaw = true; |
|
| 49 | - } |
|
| 50 | - } |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * Creates a binding for a String |
|
| 54 | - * |
|
| 55 | - * @param mixed $value |
|
| 56 | - * @return self |
|
| 57 | - */ |
|
| 58 | - public static function asString($value): self |
|
| 59 | - { |
|
| 60 | - return new Binding($value, self::STRING); |
|
| 61 | - } |
|
| 62 | - |
|
| 63 | - /** |
|
| 64 | - * Creates a binding for a Float |
|
| 65 | - * |
|
| 66 | - * @param mixed $value |
|
| 67 | - * @return self |
|
| 68 | - */ |
|
| 69 | - public static function asFloat($value): self |
|
| 70 | - { |
|
| 71 | - return new Binding($value, self::FLOAT); |
|
| 72 | - } |
|
| 73 | - |
|
| 74 | - /** |
|
| 75 | - * Creates a binding for a Int |
|
| 76 | - * |
|
| 77 | - * @param mixed $value |
|
| 78 | - * @return self |
|
| 79 | - */ |
|
| 80 | - public static function asInt($value): self |
|
| 81 | - { |
|
| 82 | - return new Binding($value, self::INT); |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - /** |
|
| 86 | - * Creates a binding for a Bool |
|
| 87 | - * |
|
| 88 | - * @param mixed $value |
|
| 89 | - * @return self |
|
| 90 | - */ |
|
| 91 | - public static function asBool($value): self |
|
| 92 | - { |
|
| 93 | - return new Binding($value, self::BOOL); |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - /** |
|
| 97 | - * Creates a binding for a JSON |
|
| 98 | - * |
|
| 99 | - * @param mixed $value |
|
| 100 | - * @return self |
|
| 101 | - */ |
|
| 102 | - public static function asJSON($value): self |
|
| 103 | - { |
|
| 104 | - return new Binding($value, self::JSON); |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - /** |
|
| 108 | - * Creates a binding for a Raw |
|
| 109 | - * |
|
| 110 | - * @param mixed $value |
|
| 111 | - * @return self |
|
| 112 | - */ |
|
| 113 | - public static function asRaw($value): self |
|
| 114 | - { |
|
| 115 | - return new Binding($value, self::RAW); |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - /** |
|
| 119 | - * Verifies that the passed type is allowed |
|
| 120 | - * |
|
| 121 | - * @param string|null $type |
|
| 122 | - * @return void |
|
| 123 | - * @throws Exception if not a valid type. |
|
| 124 | - */ |
|
| 125 | - protected function verifyType(?string $type): void |
|
| 126 | - { |
|
| 127 | - $validTypes = [self::STRING, self::BOOL, self::FLOAT, self::INT, self::JSON, self::RAW]; |
|
| 128 | - if (null !== $type && !in_array($type, $validTypes, true)) { |
|
| 129 | - throw new Exception("{$type} is not a valid type to use for Bindings.", 1); |
|
| 130 | - } |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - /** |
|
| 134 | - * Checks if we have a type that will bind. |
|
| 135 | - * |
|
| 136 | - * @return bool |
|
| 137 | - */ |
|
| 138 | - public function hasTypeDefined(): bool |
|
| 139 | - { |
|
| 140 | - return !\in_array($this->type, [null, self::RAW], true); |
|
| 141 | - } |
|
| 142 | - |
|
| 143 | - /** |
|
| 144 | - * Returns the bindings values |
|
| 145 | - * |
|
| 146 | - * @return string|int|float|bool|Raw|null |
|
| 147 | - */ |
|
| 148 | - public function getValue() |
|
| 149 | - { |
|
| 150 | - return ! $this->hasTypeDefined() |
|
| 151 | - ? new Raw($this->value) |
|
| 152 | - : $this->value; |
|
| 153 | - } |
|
| 154 | - |
|
| 155 | - /** |
|
| 156 | - * Gets the types format Conversion Specifier |
|
| 157 | - * |
|
| 158 | - * @return string|null |
|
| 159 | - */ |
|
| 160 | - public function getType(): ?string |
|
| 161 | - { |
|
| 162 | - return $this->type; |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - /** |
|
| 166 | - * Get denotes if the field is a RAW value |
|
| 167 | - * |
|
| 168 | - * @return bool |
|
| 169 | - */ |
|
| 170 | - public function isRaw(): bool |
|
| 171 | - { |
|
| 172 | - return $this->isRaw; |
|
| 173 | - } |
|
| 10 | + public const STRING = '%s'; |
|
| 11 | + public const BOOL = '%d'; |
|
| 12 | + public const INT = '%d'; |
|
| 13 | + public const FLOAT = '%f'; |
|
| 14 | + public const JSON = '%s'; |
|
| 15 | + public const RAW = ':RAW'; |
|
| 16 | + |
|
| 17 | + /** |
|
| 18 | + * Holds the value to bind with |
|
| 19 | + * |
|
| 20 | + * @var mixed |
|
| 21 | + */ |
|
| 22 | + protected $value; |
|
| 23 | + |
|
| 24 | + /** |
|
| 25 | + * Denotes the type |
|
| 26 | + * |
|
| 27 | + * @var string|null |
|
| 28 | + */ |
|
| 29 | + protected $type; |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * Denotes if the field is a RAW value |
|
| 33 | + * |
|
| 34 | + * @var bool |
|
| 35 | + */ |
|
| 36 | + protected $isRaw = false; |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * @param mixed $value |
|
| 40 | + * @param string|null $type |
|
| 41 | + */ |
|
| 42 | + public function __construct($value, ?string $type = null) |
|
| 43 | + { |
|
| 44 | + $this->verifyType($type); |
|
| 45 | + $this->value = $value; |
|
| 46 | + $this->type = $type; |
|
| 47 | + if (self::RAW === $type) { |
|
| 48 | + $this->isRaw = true; |
|
| 49 | + } |
|
| 50 | + } |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * Creates a binding for a String |
|
| 54 | + * |
|
| 55 | + * @param mixed $value |
|
| 56 | + * @return self |
|
| 57 | + */ |
|
| 58 | + public static function asString($value): self |
|
| 59 | + { |
|
| 60 | + return new Binding($value, self::STRING); |
|
| 61 | + } |
|
| 62 | + |
|
| 63 | + /** |
|
| 64 | + * Creates a binding for a Float |
|
| 65 | + * |
|
| 66 | + * @param mixed $value |
|
| 67 | + * @return self |
|
| 68 | + */ |
|
| 69 | + public static function asFloat($value): self |
|
| 70 | + { |
|
| 71 | + return new Binding($value, self::FLOAT); |
|
| 72 | + } |
|
| 73 | + |
|
| 74 | + /** |
|
| 75 | + * Creates a binding for a Int |
|
| 76 | + * |
|
| 77 | + * @param mixed $value |
|
| 78 | + * @return self |
|
| 79 | + */ |
|
| 80 | + public static function asInt($value): self |
|
| 81 | + { |
|
| 82 | + return new Binding($value, self::INT); |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + /** |
|
| 86 | + * Creates a binding for a Bool |
|
| 87 | + * |
|
| 88 | + * @param mixed $value |
|
| 89 | + * @return self |
|
| 90 | + */ |
|
| 91 | + public static function asBool($value): self |
|
| 92 | + { |
|
| 93 | + return new Binding($value, self::BOOL); |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + /** |
|
| 97 | + * Creates a binding for a JSON |
|
| 98 | + * |
|
| 99 | + * @param mixed $value |
|
| 100 | + * @return self |
|
| 101 | + */ |
|
| 102 | + public static function asJSON($value): self |
|
| 103 | + { |
|
| 104 | + return new Binding($value, self::JSON); |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + /** |
|
| 108 | + * Creates a binding for a Raw |
|
| 109 | + * |
|
| 110 | + * @param mixed $value |
|
| 111 | + * @return self |
|
| 112 | + */ |
|
| 113 | + public static function asRaw($value): self |
|
| 114 | + { |
|
| 115 | + return new Binding($value, self::RAW); |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + /** |
|
| 119 | + * Verifies that the passed type is allowed |
|
| 120 | + * |
|
| 121 | + * @param string|null $type |
|
| 122 | + * @return void |
|
| 123 | + * @throws Exception if not a valid type. |
|
| 124 | + */ |
|
| 125 | + protected function verifyType(?string $type): void |
|
| 126 | + { |
|
| 127 | + $validTypes = [self::STRING, self::BOOL, self::FLOAT, self::INT, self::JSON, self::RAW]; |
|
| 128 | + if (null !== $type && !in_array($type, $validTypes, true)) { |
|
| 129 | + throw new Exception("{$type} is not a valid type to use for Bindings.", 1); |
|
| 130 | + } |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + /** |
|
| 134 | + * Checks if we have a type that will bind. |
|
| 135 | + * |
|
| 136 | + * @return bool |
|
| 137 | + */ |
|
| 138 | + public function hasTypeDefined(): bool |
|
| 139 | + { |
|
| 140 | + return !\in_array($this->type, [null, self::RAW], true); |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + /** |
|
| 144 | + * Returns the bindings values |
|
| 145 | + * |
|
| 146 | + * @return string|int|float|bool|Raw|null |
|
| 147 | + */ |
|
| 148 | + public function getValue() |
|
| 149 | + { |
|
| 150 | + return ! $this->hasTypeDefined() |
|
| 151 | + ? new Raw($this->value) |
|
| 152 | + : $this->value; |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + /** |
|
| 156 | + * Gets the types format Conversion Specifier |
|
| 157 | + * |
|
| 158 | + * @return string|null |
|
| 159 | + */ |
|
| 160 | + public function getType(): ?string |
|
| 161 | + { |
|
| 162 | + return $this->type; |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + /** |
|
| 166 | + * Get denotes if the field is a RAW value |
|
| 167 | + * |
|
| 168 | + * @return bool |
|
| 169 | + */ |
|
| 170 | + public function isRaw(): bool |
|
| 171 | + { |
|
| 172 | + return $this->isRaw; |
|
| 173 | + } |
|
| 174 | 174 | } |
@@ -125,7 +125,7 @@ discard block |
||
| 125 | 125 | protected function verifyType(?string $type): void |
| 126 | 126 | { |
| 127 | 127 | $validTypes = [self::STRING, self::BOOL, self::FLOAT, self::INT, self::JSON, self::RAW]; |
| 128 | - if (null !== $type && !in_array($type, $validTypes, true)) { |
|
| 128 | + if (null !== $type && ! in_array($type, $validTypes, true)) { |
|
| 129 | 129 | throw new Exception("{$type} is not a valid type to use for Bindings.", 1); |
| 130 | 130 | } |
| 131 | 131 | } |
@@ -137,7 +137,7 @@ discard block |
||
| 137 | 137 | */ |
| 138 | 138 | public function hasTypeDefined(): bool |
| 139 | 139 | { |
| 140 | - return !\in_array($this->type, [null, self::RAW], true); |
|
| 140 | + return ! \in_array($this->type, [null, self::RAW], true); |
|
| 141 | 141 | } |
| 142 | 142 | |
| 143 | 143 | /** |
@@ -64,7 +64,7 @@ |
||
| 64 | 64 | } |
| 65 | 65 | |
| 66 | 66 | // Preserve the first database connection with a static property |
| 67 | - if (!static::$storedConnection) { |
|
| 67 | + if ( ! static::$storedConnection) { |
|
| 68 | 68 | static::$storedConnection = $this; |
| 69 | 69 | } |
| 70 | 70 | } |
@@ -11,198 +11,198 @@ |
||
| 11 | 11 | |
| 12 | 12 | class Connection |
| 13 | 13 | { |
| 14 | - /** Config keys */ |
|
| 15 | - public const CLONE_WPDB = 'clone_wpdb'; |
|
| 16 | - public const PREFIX = 'prefix'; |
|
| 17 | - public const SHOW_ERRORS = 'show_errors'; |
|
| 18 | - public const USE_WPDB_PREFIX = 'use_wpdb_prefix'; |
|
| 19 | - |
|
| 20 | - /** |
|
| 21 | - * @var Container |
|
| 22 | - */ |
|
| 23 | - protected $container; |
|
| 24 | - |
|
| 25 | - /** |
|
| 26 | - * @var string |
|
| 27 | - */ |
|
| 28 | - protected $adapter; |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * @var array<string, mixed> |
|
| 32 | - */ |
|
| 33 | - protected $adapterConfig; |
|
| 34 | - |
|
| 35 | - /** |
|
| 36 | - * @var wpdb |
|
| 37 | - */ |
|
| 38 | - protected $dbInstance; |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * @var Connection|null |
|
| 42 | - */ |
|
| 43 | - protected static $storedConnection; |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * @var EventHandler |
|
| 47 | - */ |
|
| 48 | - protected $eventHandler; |
|
| 49 | - |
|
| 50 | - /** |
|
| 51 | - * @param wpdb $wpdb |
|
| 52 | - * @param array<string, mixed> $adapterConfig |
|
| 53 | - * @param string|null $alias |
|
| 54 | - * @param Container|null $container |
|
| 55 | - */ |
|
| 56 | - public function __construct( |
|
| 57 | - wpdb $wpdb, |
|
| 58 | - array $adapterConfig = [], |
|
| 59 | - ?string $alias = null, |
|
| 60 | - ?Container $container = null |
|
| 61 | - ) { |
|
| 62 | - $this->setAdapterConfig($adapterConfig); |
|
| 63 | - $this->dbInstance = $this->configureWpdb($wpdb); |
|
| 64 | - |
|
| 65 | - $this->container = $container ?? new Container(); |
|
| 66 | - $this->eventHandler = $this->container->build(EventHandler::class); |
|
| 67 | - |
|
| 68 | - if ($alias) { |
|
| 69 | - $this->createAlias($alias); |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - // Preserve the first database connection with a static property |
|
| 73 | - if (!static::$storedConnection) { |
|
| 74 | - static::$storedConnection = $this; |
|
| 75 | - } |
|
| 76 | - } |
|
| 77 | - |
|
| 78 | - /** |
|
| 79 | - * Configures the instance of WPDB based on adaptor config values. |
|
| 80 | - * |
|
| 81 | - * @param \wpdb $wpdb |
|
| 82 | - * @return \wpdb |
|
| 83 | - */ |
|
| 84 | - protected function configureWpdb(wpdb $wpdb): wpdb |
|
| 85 | - { |
|
| 86 | - // Maybe clone instance. |
|
| 87 | - if ( |
|
| 88 | - array_key_exists(self::CLONE_WPDB, $this->adapterConfig) |
|
| 89 | - && true === $this->adapterConfig[self::CLONE_WPDB] |
|
| 90 | - ) { |
|
| 91 | - $wpdb = clone $wpdb; |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - // Maybe set the prefix to WPDB's. |
|
| 95 | - if ( |
|
| 96 | - array_key_exists(self::USE_WPDB_PREFIX, $this->adapterConfig) |
|
| 97 | - && 0 < \mb_strlen($this->adapterConfig[self::USE_WPDB_PREFIX]) |
|
| 98 | - ) { |
|
| 99 | - $this->adapterConfig[self::PREFIX] = $wpdb->prefix; |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - // Maybe configure errors |
|
| 103 | - if (array_key_exists(self::SHOW_ERRORS, $this->adapterConfig)) { |
|
| 104 | - // Based in its value. |
|
| 105 | - if (true === (bool) $this->adapterConfig[self::SHOW_ERRORS]) { |
|
| 106 | - $wpdb->show_errors(true); |
|
| 107 | - $wpdb->suppress_errors(false); |
|
| 108 | - } else { |
|
| 109 | - $wpdb->show_errors(false); |
|
| 110 | - $wpdb->suppress_errors(true); |
|
| 111 | - } |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - return $wpdb; |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - /** |
|
| 118 | - * Create an easily accessible query builder alias |
|
| 119 | - * |
|
| 120 | - * @param string $alias |
|
| 121 | - */ |
|
| 122 | - public function createAlias(string $alias): void |
|
| 123 | - { |
|
| 124 | - class_alias(AliasFacade::class, $alias); |
|
| 125 | - $builder = $this->container->build(QueryBuilderHandler::class, [$this]); |
|
| 126 | - AliasFacade::setQueryBuilderInstance($builder); |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - /** |
|
| 130 | - * Returns an instance of Query Builder |
|
| 131 | - */ |
|
| 132 | - public function getQueryBuilder(): QueryBuilderHandler |
|
| 133 | - { |
|
| 134 | - return $this->container->build(QueryBuilderHandler::class, [$this]); |
|
| 135 | - } |
|
| 136 | - |
|
| 137 | - /** |
|
| 138 | - * @param wpdb $wpdb |
|
| 139 | - * |
|
| 140 | - * @return $this |
|
| 141 | - */ |
|
| 142 | - public function setDbInstance(wpdb $wpdb) |
|
| 143 | - { |
|
| 144 | - $this->dbInstance = $wpdb; |
|
| 145 | - |
|
| 146 | - return $this; |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - /** |
|
| 150 | - * @return wpdb |
|
| 151 | - */ |
|
| 152 | - public function getDbInstance() |
|
| 153 | - { |
|
| 154 | - return $this->dbInstance; |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - /** |
|
| 158 | - * @param array<string, mixed> $adapterConfig |
|
| 159 | - * |
|
| 160 | - * @return $this |
|
| 161 | - */ |
|
| 162 | - public function setAdapterConfig(array $adapterConfig) |
|
| 163 | - { |
|
| 164 | - $this->adapterConfig = $adapterConfig; |
|
| 165 | - |
|
| 166 | - return $this; |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - /** |
|
| 170 | - * @return array<string, mixed> |
|
| 171 | - */ |
|
| 172 | - public function getAdapterConfig() |
|
| 173 | - { |
|
| 174 | - return $this->adapterConfig; |
|
| 175 | - } |
|
| 176 | - |
|
| 177 | - /** |
|
| 178 | - * @return Container |
|
| 179 | - */ |
|
| 180 | - public function getContainer() |
|
| 181 | - { |
|
| 182 | - return $this->container; |
|
| 183 | - } |
|
| 184 | - |
|
| 185 | - /** |
|
| 186 | - * @return EventHandler |
|
| 187 | - */ |
|
| 188 | - public function getEventHandler() |
|
| 189 | - { |
|
| 190 | - return $this->eventHandler; |
|
| 191 | - } |
|
| 192 | - |
|
| 193 | - /** |
|
| 194 | - * Returns the initial instance created. |
|
| 195 | - * |
|
| 196 | - * @return Connection |
|
| 197 | - * |
|
| 198 | - * @throws Exception If connection not already established |
|
| 199 | - */ |
|
| 200 | - public static function getStoredConnection() |
|
| 201 | - { |
|
| 202 | - if (null === static::$storedConnection) { |
|
| 203 | - throw new Exception('No initial instance of Connection created'); |
|
| 204 | - } |
|
| 205 | - |
|
| 206 | - return static::$storedConnection; |
|
| 207 | - } |
|
| 14 | + /** Config keys */ |
|
| 15 | + public const CLONE_WPDB = 'clone_wpdb'; |
|
| 16 | + public const PREFIX = 'prefix'; |
|
| 17 | + public const SHOW_ERRORS = 'show_errors'; |
|
| 18 | + public const USE_WPDB_PREFIX = 'use_wpdb_prefix'; |
|
| 19 | + |
|
| 20 | + /** |
|
| 21 | + * @var Container |
|
| 22 | + */ |
|
| 23 | + protected $container; |
|
| 24 | + |
|
| 25 | + /** |
|
| 26 | + * @var string |
|
| 27 | + */ |
|
| 28 | + protected $adapter; |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * @var array<string, mixed> |
|
| 32 | + */ |
|
| 33 | + protected $adapterConfig; |
|
| 34 | + |
|
| 35 | + /** |
|
| 36 | + * @var wpdb |
|
| 37 | + */ |
|
| 38 | + protected $dbInstance; |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * @var Connection|null |
|
| 42 | + */ |
|
| 43 | + protected static $storedConnection; |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * @var EventHandler |
|
| 47 | + */ |
|
| 48 | + protected $eventHandler; |
|
| 49 | + |
|
| 50 | + /** |
|
| 51 | + * @param wpdb $wpdb |
|
| 52 | + * @param array<string, mixed> $adapterConfig |
|
| 53 | + * @param string|null $alias |
|
| 54 | + * @param Container|null $container |
|
| 55 | + */ |
|
| 56 | + public function __construct( |
|
| 57 | + wpdb $wpdb, |
|
| 58 | + array $adapterConfig = [], |
|
| 59 | + ?string $alias = null, |
|
| 60 | + ?Container $container = null |
|
| 61 | + ) { |
|
| 62 | + $this->setAdapterConfig($adapterConfig); |
|
| 63 | + $this->dbInstance = $this->configureWpdb($wpdb); |
|
| 64 | + |
|
| 65 | + $this->container = $container ?? new Container(); |
|
| 66 | + $this->eventHandler = $this->container->build(EventHandler::class); |
|
| 67 | + |
|
| 68 | + if ($alias) { |
|
| 69 | + $this->createAlias($alias); |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + // Preserve the first database connection with a static property |
|
| 73 | + if (!static::$storedConnection) { |
|
| 74 | + static::$storedConnection = $this; |
|
| 75 | + } |
|
| 76 | + } |
|
| 77 | + |
|
| 78 | + /** |
|
| 79 | + * Configures the instance of WPDB based on adaptor config values. |
|
| 80 | + * |
|
| 81 | + * @param \wpdb $wpdb |
|
| 82 | + * @return \wpdb |
|
| 83 | + */ |
|
| 84 | + protected function configureWpdb(wpdb $wpdb): wpdb |
|
| 85 | + { |
|
| 86 | + // Maybe clone instance. |
|
| 87 | + if ( |
|
| 88 | + array_key_exists(self::CLONE_WPDB, $this->adapterConfig) |
|
| 89 | + && true === $this->adapterConfig[self::CLONE_WPDB] |
|
| 90 | + ) { |
|
| 91 | + $wpdb = clone $wpdb; |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + // Maybe set the prefix to WPDB's. |
|
| 95 | + if ( |
|
| 96 | + array_key_exists(self::USE_WPDB_PREFIX, $this->adapterConfig) |
|
| 97 | + && 0 < \mb_strlen($this->adapterConfig[self::USE_WPDB_PREFIX]) |
|
| 98 | + ) { |
|
| 99 | + $this->adapterConfig[self::PREFIX] = $wpdb->prefix; |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + // Maybe configure errors |
|
| 103 | + if (array_key_exists(self::SHOW_ERRORS, $this->adapterConfig)) { |
|
| 104 | + // Based in its value. |
|
| 105 | + if (true === (bool) $this->adapterConfig[self::SHOW_ERRORS]) { |
|
| 106 | + $wpdb->show_errors(true); |
|
| 107 | + $wpdb->suppress_errors(false); |
|
| 108 | + } else { |
|
| 109 | + $wpdb->show_errors(false); |
|
| 110 | + $wpdb->suppress_errors(true); |
|
| 111 | + } |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + return $wpdb; |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + /** |
|
| 118 | + * Create an easily accessible query builder alias |
|
| 119 | + * |
|
| 120 | + * @param string $alias |
|
| 121 | + */ |
|
| 122 | + public function createAlias(string $alias): void |
|
| 123 | + { |
|
| 124 | + class_alias(AliasFacade::class, $alias); |
|
| 125 | + $builder = $this->container->build(QueryBuilderHandler::class, [$this]); |
|
| 126 | + AliasFacade::setQueryBuilderInstance($builder); |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + /** |
|
| 130 | + * Returns an instance of Query Builder |
|
| 131 | + */ |
|
| 132 | + public function getQueryBuilder(): QueryBuilderHandler |
|
| 133 | + { |
|
| 134 | + return $this->container->build(QueryBuilderHandler::class, [$this]); |
|
| 135 | + } |
|
| 136 | + |
|
| 137 | + /** |
|
| 138 | + * @param wpdb $wpdb |
|
| 139 | + * |
|
| 140 | + * @return $this |
|
| 141 | + */ |
|
| 142 | + public function setDbInstance(wpdb $wpdb) |
|
| 143 | + { |
|
| 144 | + $this->dbInstance = $wpdb; |
|
| 145 | + |
|
| 146 | + return $this; |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + /** |
|
| 150 | + * @return wpdb |
|
| 151 | + */ |
|
| 152 | + public function getDbInstance() |
|
| 153 | + { |
|
| 154 | + return $this->dbInstance; |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + /** |
|
| 158 | + * @param array<string, mixed> $adapterConfig |
|
| 159 | + * |
|
| 160 | + * @return $this |
|
| 161 | + */ |
|
| 162 | + public function setAdapterConfig(array $adapterConfig) |
|
| 163 | + { |
|
| 164 | + $this->adapterConfig = $adapterConfig; |
|
| 165 | + |
|
| 166 | + return $this; |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + /** |
|
| 170 | + * @return array<string, mixed> |
|
| 171 | + */ |
|
| 172 | + public function getAdapterConfig() |
|
| 173 | + { |
|
| 174 | + return $this->adapterConfig; |
|
| 175 | + } |
|
| 176 | + |
|
| 177 | + /** |
|
| 178 | + * @return Container |
|
| 179 | + */ |
|
| 180 | + public function getContainer() |
|
| 181 | + { |
|
| 182 | + return $this->container; |
|
| 183 | + } |
|
| 184 | + |
|
| 185 | + /** |
|
| 186 | + * @return EventHandler |
|
| 187 | + */ |
|
| 188 | + public function getEventHandler() |
|
| 189 | + { |
|
| 190 | + return $this->eventHandler; |
|
| 191 | + } |
|
| 192 | + |
|
| 193 | + /** |
|
| 194 | + * Returns the initial instance created. |
|
| 195 | + * |
|
| 196 | + * @return Connection |
|
| 197 | + * |
|
| 198 | + * @throws Exception If connection not already established |
|
| 199 | + */ |
|
| 200 | + public static function getStoredConnection() |
|
| 201 | + { |
|
| 202 | + if (null === static::$storedConnection) { |
|
| 203 | + throw new Exception('No initial instance of Connection created'); |
|
| 204 | + } |
|
| 205 | + |
|
| 206 | + return static::$storedConnection; |
|
| 207 | + } |
|
| 208 | 208 | } |
@@ -4,19 +4,19 @@ |
||
| 4 | 4 | |
| 5 | 5 | class NestedCriteria extends QueryBuilderHandler |
| 6 | 6 | { |
| 7 | - /** |
|
| 8 | - * @param string|Raw $key |
|
| 9 | - * @param string|mixed|null $operator Can be used as value, if 3rd arg not passed |
|
| 10 | - * @param mixed|null $value |
|
| 11 | - * @param string $joiner |
|
| 12 | - * |
|
| 13 | - * @return $this |
|
| 14 | - */ |
|
| 15 | - protected function whereHandler($key, $operator = null, $value = null, $joiner = 'AND') |
|
| 16 | - { |
|
| 17 | - $key = $this->addTablePrefix($key); |
|
| 18 | - $this->statements['criteria'][] = compact('key', 'operator', 'value', 'joiner'); |
|
| 7 | + /** |
|
| 8 | + * @param string|Raw $key |
|
| 9 | + * @param string|mixed|null $operator Can be used as value, if 3rd arg not passed |
|
| 10 | + * @param mixed|null $value |
|
| 11 | + * @param string $joiner |
|
| 12 | + * |
|
| 13 | + * @return $this |
|
| 14 | + */ |
|
| 15 | + protected function whereHandler($key, $operator = null, $value = null, $joiner = 'AND') |
|
| 16 | + { |
|
| 17 | + $key = $this->addTablePrefix($key); |
|
| 18 | + $this->statements['criteria'][] = compact('key', 'operator', 'value', 'joiner'); |
|
| 19 | 19 | |
| 20 | - return $this; |
|
| 21 | - } |
|
| 20 | + return $this; |
|
| 21 | + } |
|
| 22 | 22 | } |
@@ -49,9 +49,9 @@ discard block |
||
| 49 | 49 | */ |
| 50 | 50 | public function select(array $statements): array |
| 51 | 51 | { |
| 52 | - if (!array_key_exists('tables', $statements)) { |
|
| 52 | + if ( ! array_key_exists('tables', $statements)) { |
|
| 53 | 53 | throw new Exception('No table specified.', 3); |
| 54 | - } elseif (!array_key_exists('selects', $statements)) { |
|
| 54 | + } elseif ( ! array_key_exists('selects', $statements)) { |
|
| 55 | 55 | $statements['selects'][] = '*'; |
| 56 | 56 | } |
| 57 | 57 | |
@@ -65,7 +65,7 @@ discard block |
||
| 65 | 65 | // Group bys |
| 66 | 66 | $groupBys = ''; |
| 67 | 67 | if (isset($statements['groupBys']) && $groupBys = $this->arrayStr($statements['groupBys'], ', ')) { |
| 68 | - $groupBys = 'GROUP BY ' . $groupBys; |
|
| 68 | + $groupBys = 'GROUP BY '.$groupBys; |
|
| 69 | 69 | } |
| 70 | 70 | |
| 71 | 71 | // Order bys |
@@ -76,17 +76,17 @@ discard block |
||
| 76 | 76 | if ($field instanceof Closure) { |
| 77 | 77 | continue; |
| 78 | 78 | } |
| 79 | - $orderBys .= $field . ' ' . $orderBy['type'] . ', '; |
|
| 79 | + $orderBys .= $field.' '.$orderBy['type'].', '; |
|
| 80 | 80 | } |
| 81 | 81 | |
| 82 | 82 | if ($orderBys = trim($orderBys, ', ')) { |
| 83 | - $orderBys = 'ORDER BY ' . $orderBys; |
|
| 83 | + $orderBys = 'ORDER BY '.$orderBys; |
|
| 84 | 84 | } |
| 85 | 85 | } |
| 86 | 86 | |
| 87 | 87 | // Limit and offset |
| 88 | - $limit = isset($statements['limit']) ? 'LIMIT ' . (int) $statements['limit'] : ''; |
|
| 89 | - $offset = isset($statements['offset']) ? 'OFFSET ' . (int) $statements['offset'] : ''; |
|
| 88 | + $limit = isset($statements['limit']) ? 'LIMIT '.(int) $statements['limit'] : ''; |
|
| 89 | + $offset = isset($statements['offset']) ? 'OFFSET '.(int) $statements['offset'] : ''; |
|
| 90 | 90 | |
| 91 | 91 | // Having |
| 92 | 92 | list($havingCriteria, $havingBindings) = $this->buildCriteriaWithType($statements, 'havings', 'HAVING'); |
@@ -96,7 +96,7 @@ discard block |
||
| 96 | 96 | |
| 97 | 97 | /** @var string[] */ |
| 98 | 98 | $sqlArray = [ |
| 99 | - 'SELECT' . (isset($statements['distinct']) ? ' DISTINCT' : ''), |
|
| 99 | + 'SELECT'.(isset($statements['distinct']) ? ' DISTINCT' : ''), |
|
| 100 | 100 | $selects, |
| 101 | 101 | 'FROM', |
| 102 | 102 | $tables, |
@@ -130,7 +130,7 @@ discard block |
||
| 130 | 130 | public function criteriaOnly(array $statements, bool $bindValues = true): array |
| 131 | 131 | { |
| 132 | 132 | $sql = $bindings = []; |
| 133 | - if (!isset($statements['criteria'])) { |
|
| 133 | + if ( ! isset($statements['criteria'])) { |
|
| 134 | 134 | return compact('sql', 'bindings'); |
| 135 | 135 | } |
| 136 | 136 | |
@@ -152,7 +152,7 @@ discard block |
||
| 152 | 152 | */ |
| 153 | 153 | private function doInsert(array $statements, array $data, string $type): array |
| 154 | 154 | { |
| 155 | - if (!isset($statements['tables'])) { |
|
| 155 | + if ( ! isset($statements['tables'])) { |
|
| 156 | 156 | throw new Exception('No table specified', 3); |
| 157 | 157 | } |
| 158 | 158 | |
@@ -173,20 +173,20 @@ discard block |
||
| 173 | 173 | if ($value instanceof Raw) { |
| 174 | 174 | $values[] = $this->parseRaw($value); |
| 175 | 175 | } elseif ($isBindings) { |
| 176 | - $values[] = $value->getType(); |
|
| 176 | + $values[] = $value->getType(); |
|
| 177 | 177 | $bindings[] = $value->getValue(); |
| 178 | 178 | } else { |
| 179 | - $values[] = $this->inferType($value); |
|
| 179 | + $values[] = $this->inferType($value); |
|
| 180 | 180 | $bindings[] = $value; |
| 181 | 181 | } |
| 182 | 182 | } |
| 183 | 183 | |
| 184 | 184 | $sqlArray = [ |
| 185 | - $type . ' INTO', |
|
| 185 | + $type.' INTO', |
|
| 186 | 186 | $this->wrapSanitizer($table), |
| 187 | - '(' . $this->arrayStr($keys, ',') . ')', |
|
| 187 | + '('.$this->arrayStr($keys, ',').')', |
|
| 188 | 188 | 'VALUES', |
| 189 | - '(' . $this->arrayStr($values, ',') . ')', |
|
| 189 | + '('.$this->arrayStr($values, ',').')', |
|
| 190 | 190 | ]; |
| 191 | 191 | |
| 192 | 192 | if (isset($statements['onduplicate'])) { |
@@ -194,7 +194,7 @@ discard block |
||
| 194 | 194 | throw new Exception('No data given.', 4); |
| 195 | 195 | } |
| 196 | 196 | list($updateStatement, $updateBindings) = $this->getUpdateStatement($statements['onduplicate']); |
| 197 | - $sqlArray[] = 'ON DUPLICATE KEY UPDATE ' . $updateStatement; |
|
| 197 | + $sqlArray[] = 'ON DUPLICATE KEY UPDATE '.$updateStatement; |
|
| 198 | 198 | $bindings = array_merge($bindings, $updateBindings); |
| 199 | 199 | } |
| 200 | 200 | |
@@ -302,12 +302,12 @@ discard block |
||
| 302 | 302 | } |
| 303 | 303 | |
| 304 | 304 | if ($value instanceof Raw) { |
| 305 | - $statement .= $this->stringifyValue($this->wrapSanitizer($key)) . '=' . $value . ','; |
|
| 305 | + $statement .= $this->stringifyValue($this->wrapSanitizer($key)).'='.$value.','; |
|
| 306 | 306 | } elseif ($isBindings) { |
| 307 | - $statement .= $this->stringifyValue($this->wrapSanitizer($key)) . sprintf('=%s,', $value->getType()); |
|
| 307 | + $statement .= $this->stringifyValue($this->wrapSanitizer($key)).sprintf('=%s,', $value->getType()); |
|
| 308 | 308 | $bindings[] = $value->getValue(); |
| 309 | 309 | } else { |
| 310 | - $statement .= $this->stringifyValue($this->wrapSanitizer($key)) . sprintf('=%s,', $this->inferType($value)); |
|
| 310 | + $statement .= $this->stringifyValue($this->wrapSanitizer($key)).sprintf('=%s,', $this->inferType($value)); |
|
| 311 | 311 | $bindings[] = $value; |
| 312 | 312 | } |
| 313 | 313 | } |
@@ -329,7 +329,7 @@ discard block |
||
| 329 | 329 | */ |
| 330 | 330 | public function update($statements, array $data) |
| 331 | 331 | { |
| 332 | - if (!isset($statements['tables'])) { |
|
| 332 | + if ( ! isset($statements['tables'])) { |
|
| 333 | 333 | throw new Exception('No table specified', 3); |
| 334 | 334 | } elseif (count($data) < 1) { |
| 335 | 335 | throw new Exception('No data given.', 4); |
@@ -344,12 +344,12 @@ discard block |
||
| 344 | 344 | list($whereCriteria, $whereBindings) = $this->buildCriteriaWithType($statements, 'wheres', 'WHERE'); |
| 345 | 345 | |
| 346 | 346 | // Limit |
| 347 | - $limit = isset($statements['limit']) ? 'LIMIT ' . $statements['limit'] : ''; |
|
| 347 | + $limit = isset($statements['limit']) ? 'LIMIT '.$statements['limit'] : ''; |
|
| 348 | 348 | |
| 349 | 349 | $sqlArray = [ |
| 350 | 350 | 'UPDATE', |
| 351 | 351 | $this->wrapSanitizer($table), |
| 352 | - 'SET ' . $updateStatement, |
|
| 352 | + 'SET '.$updateStatement, |
|
| 353 | 353 | $whereCriteria, |
| 354 | 354 | $limit, |
| 355 | 355 | ]; |
@@ -372,7 +372,7 @@ discard block |
||
| 372 | 372 | */ |
| 373 | 373 | public function delete($statements) |
| 374 | 374 | { |
| 375 | - if (!isset($statements['tables'])) { |
|
| 375 | + if ( ! isset($statements['tables'])) { |
|
| 376 | 376 | throw new Exception('No table specified', 3); |
| 377 | 377 | } |
| 378 | 378 | |
@@ -387,7 +387,7 @@ discard block |
||
| 387 | 387 | list($whereCriteria, $whereBindings) = $this->buildCriteriaWithType($statements, 'wheres', 'WHERE'); |
| 388 | 388 | |
| 389 | 389 | // Limit |
| 390 | - $limit = isset($statements['limit']) ? 'LIMIT ' . $statements['limit'] : ''; |
|
| 390 | + $limit = isset($statements['limit']) ? 'LIMIT '.$statements['limit'] : ''; |
|
| 391 | 391 | |
| 392 | 392 | $sqlArray = ['DELETE FROM', $table, $whereCriteria]; |
| 393 | 393 | $sql = $this->concatenateQuery($sqlArray); |
@@ -409,11 +409,11 @@ discard block |
||
| 409 | 409 | { |
| 410 | 410 | $str = ''; |
| 411 | 411 | foreach ($pieces as $key => $piece) { |
| 412 | - if (!is_int($key)) { |
|
| 413 | - $piece = $key . ' AS ' . $piece; |
|
| 412 | + if ( ! is_int($key)) { |
|
| 413 | + $piece = $key.' AS '.$piece; |
|
| 414 | 414 | } |
| 415 | 415 | |
| 416 | - $str .= $piece . $glue; |
|
| 416 | + $str .= $piece.$glue; |
|
| 417 | 417 | } |
| 418 | 418 | |
| 419 | 419 | return trim($str, $glue); |
@@ -430,7 +430,7 @@ discard block |
||
| 430 | 430 | { |
| 431 | 431 | $str = ''; |
| 432 | 432 | foreach ($pieces as $piece) { |
| 433 | - $str = trim($str) . ' ' . trim($piece); |
|
| 433 | + $str = trim($str).' '.trim($piece); |
|
| 434 | 434 | } |
| 435 | 435 | |
| 436 | 436 | return trim($str); |
@@ -445,7 +445,7 @@ discard block |
||
| 445 | 445 | public function getType($value): string |
| 446 | 446 | { |
| 447 | 447 | return $value instanceof Binding && $value->getType() !== null |
| 448 | - ? $value->getType() : $this->inferType($value) ; |
|
| 448 | + ? $value->getType() : $this->inferType($value); |
|
| 449 | 449 | } |
| 450 | 450 | |
| 451 | 451 | /** |
@@ -488,7 +488,7 @@ discard block |
||
| 488 | 488 | |
| 489 | 489 | |
| 490 | 490 | $bindings = array_map([$this, 'getValue'], $bindings); |
| 491 | - $query = $this->connection->getDbInstance()->prepare($query, $bindings) ; |
|
| 491 | + $query = $this->connection->getDbInstance()->prepare($query, $bindings); |
|
| 492 | 492 | return is_string($query) ? $query : ''; |
| 493 | 493 | } |
| 494 | 494 | |
@@ -529,10 +529,10 @@ discard block |
||
| 529 | 529 | // Merge the bindings we get from nestedCriteria object |
| 530 | 530 | $bindings = array_merge($bindings, $queryObject->getBindings()); |
| 531 | 531 | // Append the sql we get from the nestedCriteria object |
| 532 | - $criteria .= $statement['joiner'] . ' (' . $queryObject->getSql() . ') '; |
|
| 532 | + $criteria .= $statement['joiner'].' ('.$queryObject->getSql().') '; |
|
| 533 | 533 | } elseif (is_array($value)) { |
| 534 | 534 | // where_in or between like query |
| 535 | - $criteria .= $statement['joiner'] . ' ' . $key . ' ' . $statement['operator']; |
|
| 535 | + $criteria .= $statement['joiner'].' '.$key.' '.$statement['operator']; |
|
| 536 | 536 | |
| 537 | 537 | switch ($statement['operator']) { |
| 538 | 538 | case 'BETWEEN': |
@@ -565,7 +565,7 @@ discard block |
||
| 565 | 565 | } |
| 566 | 566 | |
| 567 | 567 | $valuePlaceholder = trim($valuePlaceholder, ', '); |
| 568 | - $criteria .= ' (' . $valuePlaceholder . ') '; |
|
| 568 | + $criteria .= ' ('.$valuePlaceholder.') '; |
|
| 569 | 569 | break; |
| 570 | 570 | } |
| 571 | 571 | } elseif ($value instanceof Raw) { |
@@ -573,20 +573,20 @@ discard block |
||
| 573 | 573 | $criteria .= "{$statement['joiner']} {$key} {$statement['operator']} $value "; |
| 574 | 574 | } else { |
| 575 | 575 | // Usual where like criteria |
| 576 | - if (!$bindValues) { |
|
| 576 | + if ( ! $bindValues) { |
|
| 577 | 577 | // Specially for joins |
| 578 | 578 | // We are not binding values, lets sanitize then |
| 579 | 579 | $value = $this->stringifyValue($this->wrapSanitizer($value)) ?? ''; |
| 580 | - $criteria .= $statement['joiner'] . ' ' . $key . ' ' . $statement['operator'] . ' ' . $value . ' '; |
|
| 580 | + $criteria .= $statement['joiner'].' '.$key.' '.$statement['operator'].' '.$value.' '; |
|
| 581 | 581 | } elseif ($statement['key'] instanceof Raw) { |
| 582 | - $criteria .= $statement['joiner'] . ' ' . $key . ' '; |
|
| 582 | + $criteria .= $statement['joiner'].' '.$key.' '; |
|
| 583 | 583 | $bindings = array_merge($bindings, $statement['key']->getBindings()); |
| 584 | 584 | } else { |
| 585 | 585 | // For wheres |
| 586 | 586 | $bindings[] = $this->getValue($value); |
| 587 | 587 | |
| 588 | - $criteria .= $statement['joiner'] . ' ' . $key . ' ' . $statement['operator'] . ' ' |
|
| 589 | - . $this->getType($value) . ' '; |
|
| 588 | + $criteria .= $statement['joiner'].' '.$key.' '.$statement['operator'].' ' |
|
| 589 | + . $this->getType($value).' '; |
|
| 590 | 590 | } |
| 591 | 591 | } |
| 592 | 592 | } |
@@ -641,7 +641,7 @@ discard block |
||
| 641 | 641 | |
| 642 | 642 | foreach ($valueArr as $key => $subValue) { |
| 643 | 643 | // Don't wrap if we have *, which is not a usual field |
| 644 | - $valueArr[$key] = '*' == trim($subValue) ? $subValue : $this->sanitizer . $subValue . $this->sanitizer; |
|
| 644 | + $valueArr[$key] = '*' == trim($subValue) ? $subValue : $this->sanitizer.$subValue.$this->sanitizer; |
|
| 645 | 645 | } |
| 646 | 646 | |
| 647 | 647 | // Join these back with "." and return |
@@ -668,7 +668,7 @@ discard block |
||
| 668 | 668 | list($criteria, $bindings) = $this->buildCriteria($statements[$key], $bindValues); |
| 669 | 669 | |
| 670 | 670 | if ($criteria) { |
| 671 | - $criteria = $type . ' ' . $criteria; |
|
| 671 | + $criteria = $type.' '.$criteria; |
|
| 672 | 672 | } |
| 673 | 673 | } |
| 674 | 674 | |
@@ -686,7 +686,7 @@ discard block |
||
| 686 | 686 | { |
| 687 | 687 | $sql = ''; |
| 688 | 688 | |
| 689 | - if (!array_key_exists('joins', $statements) || !is_array($statements['joins'])) { |
|
| 689 | + if ( ! array_key_exists('joins', $statements) || ! is_array($statements['joins'])) { |
|
| 690 | 690 | return $sql; |
| 691 | 691 | } |
| 692 | 692 | |
@@ -694,7 +694,7 @@ discard block |
||
| 694 | 694 | if (is_array($joinArr['table'])) { |
| 695 | 695 | $mainTable = $this->stringifyValue($this->wrapSanitizer($joinArr['table'][0])); |
| 696 | 696 | $aliasTable = $this->stringifyValue($this->wrapSanitizer($joinArr['table'][1])); |
| 697 | - $table = $mainTable . ' AS ' . $aliasTable; |
|
| 697 | + $table = $mainTable.' AS '.$aliasTable; |
|
| 698 | 698 | } else { |
| 699 | 699 | $table = $joinArr['table'] instanceof Raw |
| 700 | 700 | ? $this->parseRaw($joinArr['table']) |
@@ -17,708 +17,708 @@ |
||
| 17 | 17 | |
| 18 | 18 | class WPDBAdapter |
| 19 | 19 | { |
| 20 | - /** |
|
| 21 | - * @var string |
|
| 22 | - */ |
|
| 23 | - protected $sanitizer = ''; |
|
| 24 | - |
|
| 25 | - /** |
|
| 26 | - * @var \Pixie\Connection |
|
| 27 | - */ |
|
| 28 | - protected $connection; |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * @var \Viocon\Container |
|
| 32 | - */ |
|
| 33 | - protected $container; |
|
| 34 | - |
|
| 35 | - public function __construct(Connection $connection) |
|
| 36 | - { |
|
| 37 | - $this->connection = $connection; |
|
| 38 | - $this->container = $this->connection->getContainer(); |
|
| 39 | - } |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * Build select query string and bindings |
|
| 43 | - * |
|
| 44 | - * @param array<string|Closure, mixed|mixed[]> $statements |
|
| 45 | - * |
|
| 46 | - * @throws Exception |
|
| 47 | - * |
|
| 48 | - * @return array{sql:string,bindings:mixed[]} |
|
| 49 | - */ |
|
| 50 | - public function select(array $statements): array |
|
| 51 | - { |
|
| 52 | - if (!array_key_exists('tables', $statements)) { |
|
| 53 | - throw new Exception('No table specified.', 3); |
|
| 54 | - } elseif (!array_key_exists('selects', $statements)) { |
|
| 55 | - $statements['selects'][] = '*'; |
|
| 56 | - } |
|
| 57 | - |
|
| 58 | - // From |
|
| 59 | - $tables = $this->arrayStr($statements['tables'], ', '); |
|
| 60 | - // Select |
|
| 61 | - $selects = $this->arrayStr($statements['selects'], ', '); |
|
| 62 | - |
|
| 63 | - // Wheres |
|
| 64 | - list($whereCriteria, $whereBindings) = $this->buildCriteriaWithType($statements, 'wheres', 'WHERE'); |
|
| 65 | - |
|
| 66 | - // Group bys |
|
| 67 | - $groupBys = ''; |
|
| 68 | - if (isset($statements['groupBys']) && $groupBys = $this->arrayStr($statements['groupBys'], ', ')) { |
|
| 69 | - $groupBys = 'GROUP BY ' . $groupBys; |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - // Order bys |
|
| 73 | - $orderBys = ''; |
|
| 74 | - if (isset($statements['orderBys']) && is_array($statements['orderBys'])) { |
|
| 75 | - foreach ($statements['orderBys'] as $orderBy) { |
|
| 76 | - $field = $this->wrapSanitizer($orderBy['field']); |
|
| 77 | - if ($field instanceof Closure) { |
|
| 78 | - continue; |
|
| 79 | - } |
|
| 80 | - $orderBys .= $field . ' ' . $orderBy['type'] . ', '; |
|
| 81 | - } |
|
| 82 | - |
|
| 83 | - if ($orderBys = trim($orderBys, ', ')) { |
|
| 84 | - $orderBys = 'ORDER BY ' . $orderBys; |
|
| 85 | - } |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - // Limit and offset |
|
| 89 | - $limit = isset($statements['limit']) ? 'LIMIT ' . (int) $statements['limit'] : ''; |
|
| 90 | - $offset = isset($statements['offset']) ? 'OFFSET ' . (int) $statements['offset'] : ''; |
|
| 91 | - |
|
| 92 | - // Having |
|
| 93 | - list($havingCriteria, $havingBindings) = $this->buildCriteriaWithType($statements, 'havings', 'HAVING'); |
|
| 94 | - |
|
| 95 | - // Joins |
|
| 96 | - $joinString = $this->buildJoin($statements); |
|
| 97 | - |
|
| 98 | - /** @var string[] */ |
|
| 99 | - $sqlArray = [ |
|
| 100 | - 'SELECT' . (isset($statements['distinct']) ? ' DISTINCT' : ''), |
|
| 101 | - $selects, |
|
| 102 | - 'FROM', |
|
| 103 | - $tables, |
|
| 104 | - $joinString, |
|
| 105 | - $whereCriteria, |
|
| 106 | - $groupBys, |
|
| 107 | - $havingCriteria, |
|
| 108 | - $orderBys, |
|
| 109 | - $limit, |
|
| 110 | - $offset, |
|
| 111 | - ]; |
|
| 112 | - |
|
| 113 | - $sql = $this->concatenateQuery($sqlArray); |
|
| 114 | - |
|
| 115 | - $bindings = array_merge( |
|
| 116 | - $whereBindings, |
|
| 117 | - $havingBindings |
|
| 118 | - ); |
|
| 119 | - |
|
| 120 | - return compact('sql', 'bindings'); |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - /** |
|
| 124 | - * Build just criteria part of the query |
|
| 125 | - * |
|
| 126 | - * @param array<string|Closure, mixed|mixed[]> $statements |
|
| 127 | - * @param bool $bindValues |
|
| 128 | - * |
|
| 129 | - * @return array{sql:string[]|string, bindings:array<mixed>} |
|
| 130 | - */ |
|
| 131 | - public function criteriaOnly(array $statements, bool $bindValues = true): array |
|
| 132 | - { |
|
| 133 | - $sql = $bindings = []; |
|
| 134 | - if (!isset($statements['criteria'])) { |
|
| 135 | - return compact('sql', 'bindings'); |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - list($sql, $bindings) = $this->buildCriteria($statements['criteria'], $bindValues); |
|
| 139 | - |
|
| 140 | - return compact('sql', 'bindings'); |
|
| 141 | - } |
|
| 142 | - |
|
| 143 | - /** |
|
| 144 | - * Build a generic insert/ignore/replace query |
|
| 145 | - * |
|
| 146 | - * @param array<string|Closure, mixed|mixed[]> $statements |
|
| 147 | - * @param array<string, mixed> $data |
|
| 148 | - * @param string $type |
|
| 149 | - * |
|
| 150 | - * @return array{sql:string, bindings:mixed[]} |
|
| 151 | - * |
|
| 152 | - * @throws Exception |
|
| 153 | - */ |
|
| 154 | - private function doInsert(array $statements, array $data, string $type): array |
|
| 155 | - { |
|
| 156 | - if (!isset($statements['tables'])) { |
|
| 157 | - throw new Exception('No table specified', 3); |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - $table = end($statements['tables']); |
|
| 161 | - |
|
| 162 | - $bindings = $keys = $values = []; |
|
| 163 | - |
|
| 164 | - foreach ($data as $key => $value) { |
|
| 165 | - $keys[] = $key; |
|
| 166 | - |
|
| 167 | - // Handle value as bindings |
|
| 168 | - $isBindings = $value instanceof Binding; |
|
| 169 | - // If this is a raw binding, extract the Raw and replace value. |
|
| 170 | - if ($isBindings && $value->isRaw()) { |
|
| 171 | - $value = $value->getValue(); |
|
| 172 | - } |
|
| 173 | - |
|
| 174 | - if ($value instanceof Raw) { |
|
| 175 | - $values[] = $this->parseRaw($value); |
|
| 176 | - } elseif ($isBindings) { |
|
| 177 | - $values[] = $value->getType(); |
|
| 178 | - $bindings[] = $value->getValue(); |
|
| 179 | - } else { |
|
| 180 | - $values[] = $this->inferType($value); |
|
| 181 | - $bindings[] = $value; |
|
| 182 | - } |
|
| 183 | - } |
|
| 184 | - |
|
| 185 | - $sqlArray = [ |
|
| 186 | - $type . ' INTO', |
|
| 187 | - $this->wrapSanitizer($table), |
|
| 188 | - '(' . $this->arrayStr($keys, ',') . ')', |
|
| 189 | - 'VALUES', |
|
| 190 | - '(' . $this->arrayStr($values, ',') . ')', |
|
| 191 | - ]; |
|
| 192 | - |
|
| 193 | - if (isset($statements['onduplicate'])) { |
|
| 194 | - if (count($statements['onduplicate']) < 1) { |
|
| 195 | - throw new Exception('No data given.', 4); |
|
| 196 | - } |
|
| 197 | - list($updateStatement, $updateBindings) = $this->getUpdateStatement($statements['onduplicate']); |
|
| 198 | - $sqlArray[] = 'ON DUPLICATE KEY UPDATE ' . $updateStatement; |
|
| 199 | - $bindings = array_merge($bindings, $updateBindings); |
|
| 200 | - } |
|
| 201 | - |
|
| 202 | - $sql = $this->concatenateQuery($this->stringifyValues($sqlArray)); |
|
| 203 | - |
|
| 204 | - return compact('sql', 'bindings'); |
|
| 205 | - } |
|
| 206 | - |
|
| 207 | - /** |
|
| 208 | - * Attempts to stringify an array of values. |
|
| 209 | - * |
|
| 210 | - * @param array<string|int, string|Closure> $values |
|
| 211 | - * |
|
| 212 | - * @return string[] |
|
| 213 | - */ |
|
| 214 | - protected function stringifyValues(array $values): array |
|
| 215 | - { |
|
| 216 | - return array_filter(array_map([$this, 'stringifyValue'], $values)); |
|
| 217 | - } |
|
| 218 | - |
|
| 219 | - /** |
|
| 220 | - * Attempts to stringify a single of values. |
|
| 221 | - * |
|
| 222 | - * @param string|Closure|Raw $value |
|
| 223 | - * |
|
| 224 | - * @return string|null |
|
| 225 | - */ |
|
| 226 | - protected function stringifyValue($value): ?string |
|
| 227 | - { |
|
| 228 | - if ($value instanceof Closure) { |
|
| 229 | - $value = $value(); |
|
| 230 | - |
|
| 231 | - return is_string($value) ? $value : null; |
|
| 232 | - } |
|
| 233 | - |
|
| 234 | - if ($value instanceof Raw) { |
|
| 235 | - return $this->parseRaw($value); |
|
| 236 | - } |
|
| 237 | - |
|
| 238 | - return $value; |
|
| 239 | - } |
|
| 240 | - |
|
| 241 | - /** |
|
| 242 | - * Build Insert query |
|
| 243 | - * |
|
| 244 | - * @param array<string|Closure, mixed|mixed[]> $statements |
|
| 245 | - * @param array<string, mixed> $data $data |
|
| 246 | - * |
|
| 247 | - * @return array{sql:string, bindings:mixed[]} |
|
| 248 | - * |
|
| 249 | - * @throws Exception |
|
| 250 | - */ |
|
| 251 | - public function insert($statements, array $data) |
|
| 252 | - { |
|
| 253 | - return $this->doInsert($statements, $data, 'INSERT'); |
|
| 254 | - } |
|
| 255 | - |
|
| 256 | - /** |
|
| 257 | - * Build Insert Ignore query |
|
| 258 | - * |
|
| 259 | - * @param array<string|Closure, mixed|mixed[]> $statements |
|
| 260 | - * @param array<string, mixed> $data $data |
|
| 261 | - * |
|
| 262 | - * @return array{sql:string, bindings:mixed[]} |
|
| 263 | - * |
|
| 264 | - * @throws Exception |
|
| 265 | - */ |
|
| 266 | - public function insertIgnore($statements, array $data) |
|
| 267 | - { |
|
| 268 | - return $this->doInsert($statements, $data, 'INSERT IGNORE'); |
|
| 269 | - } |
|
| 270 | - |
|
| 271 | - /** |
|
| 272 | - * Build Insert Ignore query |
|
| 273 | - * |
|
| 274 | - * @param array<string|Closure, mixed|mixed[]> $statements |
|
| 275 | - * @param array<string, mixed> $data $data |
|
| 276 | - * |
|
| 277 | - * @return array{sql:string, bindings:mixed[]} |
|
| 278 | - * |
|
| 279 | - * @throws Exception |
|
| 280 | - */ |
|
| 281 | - public function replace($statements, array $data) |
|
| 282 | - { |
|
| 283 | - return $this->doInsert($statements, $data, 'REPLACE'); |
|
| 284 | - } |
|
| 285 | - |
|
| 286 | - /** |
|
| 287 | - * Build fields assignment part of SET ... or ON DUBLICATE KEY UPDATE ... statements |
|
| 288 | - * |
|
| 289 | - * @param array<string, mixed> $data |
|
| 290 | - * |
|
| 291 | - * @return array{0:string,1:mixed[]} |
|
| 292 | - */ |
|
| 293 | - private function getUpdateStatement(array $data): array |
|
| 294 | - { |
|
| 295 | - $bindings = []; |
|
| 296 | - $statement = ''; |
|
| 297 | - |
|
| 298 | - foreach ($data as $key => $value) { |
|
| 299 | - $isBindings = $value instanceof Binding; |
|
| 300 | - // If this is a raw binding, extract the Raw and replace value. |
|
| 301 | - if ($isBindings && $value->isRaw()) { |
|
| 302 | - $value = $value->getValue(); |
|
| 303 | - } |
|
| 304 | - |
|
| 305 | - if ($value instanceof Raw) { |
|
| 306 | - $statement .= $this->stringifyValue($this->wrapSanitizer($key)) . '=' . $value . ','; |
|
| 307 | - } elseif ($isBindings) { |
|
| 308 | - $statement .= $this->stringifyValue($this->wrapSanitizer($key)) . sprintf('=%s,', $value->getType()); |
|
| 309 | - $bindings[] = $value->getValue(); |
|
| 310 | - } else { |
|
| 311 | - $statement .= $this->stringifyValue($this->wrapSanitizer($key)) . sprintf('=%s,', $this->inferType($value)); |
|
| 312 | - $bindings[] = $value; |
|
| 313 | - } |
|
| 314 | - } |
|
| 315 | - |
|
| 316 | - $statement = trim($statement, ','); |
|
| 317 | - |
|
| 318 | - return [$statement, $bindings]; |
|
| 319 | - } |
|
| 320 | - |
|
| 321 | - /** |
|
| 322 | - * Build update query |
|
| 323 | - * |
|
| 324 | - * @param array<string|Closure, mixed|mixed[]> $statements |
|
| 325 | - * @param array<string, mixed> $data |
|
| 326 | - * |
|
| 327 | - * @return array{sql:string, bindings:mixed[]} |
|
| 328 | - * |
|
| 329 | - * @throws Exception |
|
| 330 | - */ |
|
| 331 | - public function update($statements, array $data) |
|
| 332 | - { |
|
| 333 | - if (!isset($statements['tables'])) { |
|
| 334 | - throw new Exception('No table specified', 3); |
|
| 335 | - } elseif (count($data) < 1) { |
|
| 336 | - throw new Exception('No data given.', 4); |
|
| 337 | - } |
|
| 338 | - |
|
| 339 | - $table = end($statements['tables']); |
|
| 340 | - |
|
| 341 | - // Update statement |
|
| 342 | - list($updateStatement, $bindings) = $this->getUpdateStatement($data); |
|
| 343 | - |
|
| 344 | - // Wheres |
|
| 345 | - list($whereCriteria, $whereBindings) = $this->buildCriteriaWithType($statements, 'wheres', 'WHERE'); |
|
| 346 | - |
|
| 347 | - // Limit |
|
| 348 | - $limit = isset($statements['limit']) ? 'LIMIT ' . $statements['limit'] : ''; |
|
| 349 | - |
|
| 350 | - $sqlArray = [ |
|
| 351 | - 'UPDATE', |
|
| 352 | - $this->wrapSanitizer($table), |
|
| 353 | - 'SET ' . $updateStatement, |
|
| 354 | - $whereCriteria, |
|
| 355 | - $limit, |
|
| 356 | - ]; |
|
| 357 | - |
|
| 358 | - $sql = $this->concatenateQuery($this->stringifyValues($sqlArray)); |
|
| 359 | - |
|
| 360 | - $bindings = array_merge($bindings, $whereBindings); |
|
| 361 | - |
|
| 362 | - return compact('sql', 'bindings'); |
|
| 363 | - } |
|
| 364 | - |
|
| 365 | - /** |
|
| 366 | - * Build delete query |
|
| 367 | - * |
|
| 368 | - * @param array<string|Closure, mixed|mixed[]> $statements |
|
| 369 | - * |
|
| 370 | - * @return array{sql:string, bindings:mixed[]} |
|
| 371 | - * |
|
| 372 | - * @throws Exception |
|
| 373 | - */ |
|
| 374 | - public function delete($statements) |
|
| 375 | - { |
|
| 376 | - if (!isset($statements['tables'])) { |
|
| 377 | - throw new Exception('No table specified', 3); |
|
| 378 | - } |
|
| 379 | - |
|
| 380 | - $table = end($statements['tables']); |
|
| 381 | - // Ensure table name is a string |
|
| 382 | - $table = $this->stringifyValue($this->wrapSanitizer($table)); |
|
| 383 | - if (null === $table) { |
|
| 384 | - throw new Exception('Table must be a valid string.', 5); |
|
| 385 | - } |
|
| 386 | - |
|
| 387 | - // Wheres |
|
| 388 | - list($whereCriteria, $whereBindings) = $this->buildCriteriaWithType($statements, 'wheres', 'WHERE'); |
|
| 389 | - |
|
| 390 | - // Limit |
|
| 391 | - $limit = isset($statements['limit']) ? 'LIMIT ' . $statements['limit'] : ''; |
|
| 392 | - |
|
| 393 | - $sqlArray = ['DELETE FROM', $table, $whereCriteria]; |
|
| 394 | - $sql = $this->concatenateQuery($sqlArray); |
|
| 395 | - $bindings = $whereBindings; |
|
| 396 | - |
|
| 397 | - return compact('sql', 'bindings'); |
|
| 398 | - } |
|
| 399 | - |
|
| 400 | - /** |
|
| 401 | - * Array concatenating method, like implode. |
|
| 402 | - * But it does wrap sanitizer and trims last glue |
|
| 403 | - * |
|
| 404 | - * @param array<string|int, string> $pieces |
|
| 405 | - * @param string $glue |
|
| 406 | - * |
|
| 407 | - * @return string |
|
| 408 | - */ |
|
| 409 | - protected function arrayStr(array $pieces, string $glue): string |
|
| 410 | - { |
|
| 411 | - $str = ''; |
|
| 412 | - foreach ($pieces as $key => $piece) { |
|
| 413 | - if (!is_int($key)) { |
|
| 414 | - $piece = $key . ' AS ' . $piece; |
|
| 415 | - } |
|
| 416 | - |
|
| 417 | - $str .= $piece . $glue; |
|
| 418 | - } |
|
| 419 | - |
|
| 420 | - return trim($str, $glue); |
|
| 421 | - } |
|
| 422 | - |
|
| 423 | - /** |
|
| 424 | - * Join different part of queries with a space. |
|
| 425 | - * |
|
| 426 | - * @param array<string|int, string> $pieces |
|
| 427 | - * |
|
| 428 | - * @return string |
|
| 429 | - */ |
|
| 430 | - protected function concatenateQuery(array $pieces): string |
|
| 431 | - { |
|
| 432 | - $str = ''; |
|
| 433 | - foreach ($pieces as $piece) { |
|
| 434 | - $str = trim($str) . ' ' . trim($piece); |
|
| 435 | - } |
|
| 436 | - |
|
| 437 | - return trim($str); |
|
| 438 | - } |
|
| 439 | - |
|
| 440 | - /** |
|
| 441 | - * Gets the type of a value, either from a binding or infered |
|
| 442 | - * |
|
| 443 | - * @param mixed $value |
|
| 444 | - * @return string |
|
| 445 | - */ |
|
| 446 | - public function getType($value): string |
|
| 447 | - { |
|
| 448 | - return $value instanceof Binding && $value->getType() !== null |
|
| 449 | - ? $value->getType() : $this->inferType($value) ; |
|
| 450 | - } |
|
| 451 | - |
|
| 452 | - /** |
|
| 453 | - * Get the value from a possible Bindings object. |
|
| 454 | - * |
|
| 455 | - * @param mixed $value |
|
| 456 | - * @return mixed |
|
| 457 | - */ |
|
| 458 | - public function getValue($value) |
|
| 459 | - { |
|
| 460 | - return $value instanceof Binding ? $value->getValue() : $value; |
|
| 461 | - } |
|
| 462 | - |
|
| 463 | - /** |
|
| 464 | - * Attempts to parse a raw query, if bindings are defined then they will be bound first. |
|
| 465 | - * |
|
| 466 | - * @param Raw $raw |
|
| 467 | - * @requires string |
|
| 468 | - */ |
|
| 469 | - public function parseRaw(Raw $raw): string |
|
| 470 | - { |
|
| 471 | - $bindings = $raw->getBindings(); |
|
| 472 | - return 0 === count($bindings) |
|
| 473 | - ? (string) $raw |
|
| 474 | - : $this->interpolateQuery($raw->getValue(), $bindings); |
|
| 475 | - } |
|
| 476 | - |
|
| 477 | - /** |
|
| 478 | - * Interpolates a query |
|
| 479 | - * |
|
| 480 | - * @param string $query |
|
| 481 | - * @param array<mixed> $bindings |
|
| 482 | - * @return string |
|
| 483 | - */ |
|
| 484 | - public function interpolateQuery(string $query, array $bindings = []): string |
|
| 485 | - { |
|
| 486 | - if (0 === count($bindings)) { |
|
| 487 | - return $query; |
|
| 488 | - } |
|
| 489 | - |
|
| 490 | - |
|
| 491 | - $bindings = array_map([$this, 'getValue'], $bindings); |
|
| 492 | - $query = $this->connection->getDbInstance()->prepare($query, $bindings) ; |
|
| 493 | - return is_string($query) ? $query : ''; |
|
| 494 | - } |
|
| 495 | - |
|
| 496 | - /** |
|
| 497 | - * Build generic criteria string and bindings from statements, like "a = b and c = ?" |
|
| 498 | - * |
|
| 499 | - * @param array<string|Closure, mixed|mixed[]> $statements |
|
| 500 | - * @param bool $bindValues |
|
| 501 | - * |
|
| 502 | - * @return array{0:string,1:string[]} |
|
| 503 | - */ |
|
| 504 | - protected function buildCriteria(array $statements, bool $bindValues = true): array |
|
| 505 | - { |
|
| 506 | - $criteria = ''; |
|
| 507 | - $bindings = []; |
|
| 508 | - foreach ($statements as $statement) { |
|
| 509 | - $key = $statement['key']; |
|
| 510 | - $value = $statement['value']; |
|
| 511 | - |
|
| 512 | - // If the value is a Raw Binding, cast to raw |
|
| 513 | - if ($value instanceof Binding && Binding::RAW === $value->getType()) { |
|
| 514 | - /** @var Raw */ |
|
| 515 | - $value = $value->getValue(); |
|
| 516 | - } |
|
| 517 | - |
|
| 518 | - if (is_null($value) && $key instanceof Closure) { |
|
| 519 | - // We have a closure, a nested criteria |
|
| 520 | - |
|
| 521 | - // Build a new NestedCriteria class, keep it by reference so any changes made |
|
| 522 | - // in the closure should reflect here |
|
| 523 | - $nestedCriteria = $this->container->build(NestedCriteria::class, [$this->connection]); |
|
| 524 | - |
|
| 525 | - $nestedCriteria = &$nestedCriteria; |
|
| 526 | - // Call the closure with our new nestedCriteria object |
|
| 527 | - $key($nestedCriteria); |
|
| 528 | - // Get the criteria only query from the nestedCriteria object |
|
| 529 | - $queryObject = $nestedCriteria->getQuery('criteriaOnly', true); |
|
| 530 | - // Merge the bindings we get from nestedCriteria object |
|
| 531 | - $bindings = array_merge($bindings, $queryObject->getBindings()); |
|
| 532 | - // Append the sql we get from the nestedCriteria object |
|
| 533 | - $criteria .= $statement['joiner'] . ' (' . $queryObject->getSql() . ') '; |
|
| 534 | - } elseif (is_array($value)) { |
|
| 535 | - // where_in or between like query |
|
| 536 | - $criteria .= $statement['joiner'] . ' ' . $key . ' ' . $statement['operator']; |
|
| 537 | - |
|
| 538 | - switch ($statement['operator']) { |
|
| 539 | - case 'BETWEEN': |
|
| 540 | - $bindings = array_merge($bindings, $statement['value']); |
|
| 541 | - $criteria .= sprintf( |
|
| 542 | - ' %s AND %s ', |
|
| 543 | - $this->getType($value[0]), |
|
| 544 | - $this->getType($value[1]) |
|
| 545 | - ); |
|
| 546 | - |
|
| 547 | - // Maybe cast the values bindings. |
|
| 548 | - $value[0] = $this->getValue($value[0]); |
|
| 549 | - $value[1] = $this->getValue($value[1]); |
|
| 550 | - break; |
|
| 551 | - default: |
|
| 552 | - $valuePlaceholder = ''; |
|
| 553 | - foreach ($statement['value'] as $subValue) { |
|
| 554 | - // Get its value. |
|
| 555 | - if ($this->getValue($subValue) instanceof Raw) { |
|
| 556 | - /** @var Raw $subValue */ |
|
| 557 | - $subValue = $this->getValue($subValue); |
|
| 558 | - $valuePlaceholder .= sprintf('%s, ', $this->parseRaw($subValue)); |
|
| 559 | - continue; |
|
| 560 | - } |
|
| 561 | - |
|
| 562 | - |
|
| 563 | - // Add in format placeholders. |
|
| 564 | - $valuePlaceholder .= sprintf('%s, ', $this->getType($subValue)); // glynn |
|
| 565 | - $bindings[] = $this->getValue($subValue); |
|
| 566 | - } |
|
| 567 | - |
|
| 568 | - $valuePlaceholder = trim($valuePlaceholder, ', '); |
|
| 569 | - $criteria .= ' (' . $valuePlaceholder . ') '; |
|
| 570 | - break; |
|
| 571 | - } |
|
| 572 | - } elseif ($value instanceof Raw) { |
|
| 573 | - $value = $this->parseRaw($value); |
|
| 574 | - $criteria .= "{$statement['joiner']} {$key} {$statement['operator']} $value "; |
|
| 575 | - } else { |
|
| 576 | - // Usual where like criteria |
|
| 577 | - if (!$bindValues) { |
|
| 578 | - // Specially for joins |
|
| 579 | - // We are not binding values, lets sanitize then |
|
| 580 | - $value = $this->stringifyValue($this->wrapSanitizer($value)) ?? ''; |
|
| 581 | - $criteria .= $statement['joiner'] . ' ' . $key . ' ' . $statement['operator'] . ' ' . $value . ' '; |
|
| 582 | - } elseif ($statement['key'] instanceof Raw) { |
|
| 583 | - $criteria .= $statement['joiner'] . ' ' . $key . ' '; |
|
| 584 | - $bindings = array_merge($bindings, $statement['key']->getBindings()); |
|
| 585 | - } else { |
|
| 586 | - // For wheres |
|
| 587 | - $bindings[] = $this->getValue($value); |
|
| 588 | - |
|
| 589 | - $criteria .= $statement['joiner'] . ' ' . $key . ' ' . $statement['operator'] . ' ' |
|
| 590 | - . $this->getType($value) . ' '; |
|
| 591 | - } |
|
| 592 | - } |
|
| 593 | - } |
|
| 594 | - |
|
| 595 | - // Clear all white spaces, and, or from beginning and white spaces from ending |
|
| 596 | - $criteria = preg_replace('/^(\s?AND ?|\s?OR ?)|\s$/i', '', $criteria); |
|
| 597 | - |
|
| 598 | - return [$criteria ?? '', $bindings]; |
|
| 599 | - } |
|
| 600 | - |
|
| 601 | - /** |
|
| 602 | - * Asserts the types place holder based on its value |
|
| 603 | - * |
|
| 604 | - * @param mixed $value |
|
| 605 | - * |
|
| 606 | - * @return string |
|
| 607 | - */ |
|
| 608 | - public function inferType($value): string |
|
| 609 | - { |
|
| 610 | - switch (true) { |
|
| 611 | - case is_string($value): |
|
| 612 | - return '%s'; |
|
| 613 | - case \is_int($value): |
|
| 614 | - case is_bool($value): |
|
| 615 | - return '%d'; |
|
| 616 | - case is_float($value): |
|
| 617 | - return '%f'; |
|
| 618 | - default: |
|
| 619 | - return ''; |
|
| 620 | - } |
|
| 621 | - } |
|
| 622 | - |
|
| 623 | - /** |
|
| 624 | - * Wrap values with adapter's sanitizer like, '`' |
|
| 625 | - * |
|
| 626 | - * @param string|Raw|Closure $value |
|
| 627 | - * |
|
| 628 | - * @return string|Closure |
|
| 629 | - */ |
|
| 630 | - public function wrapSanitizer($value) |
|
| 631 | - { |
|
| 632 | - // Its a raw query, just cast as string, object has __toString() |
|
| 633 | - if ($value instanceof Raw) { |
|
| 634 | - return $this->parseRaw($value); |
|
| 635 | - } elseif ($value instanceof Closure) { |
|
| 636 | - return $value; |
|
| 637 | - } |
|
| 638 | - |
|
| 639 | - // Separate our table and fields which are joined with a ".", |
|
| 640 | - // like my_table.id |
|
| 641 | - $valueArr = explode('.', $value, 2); |
|
| 642 | - |
|
| 643 | - foreach ($valueArr as $key => $subValue) { |
|
| 644 | - // Don't wrap if we have *, which is not a usual field |
|
| 645 | - $valueArr[$key] = '*' == trim($subValue) ? $subValue : $this->sanitizer . $subValue . $this->sanitizer; |
|
| 646 | - } |
|
| 647 | - |
|
| 648 | - // Join these back with "." and return |
|
| 649 | - return implode('.', $valueArr); |
|
| 650 | - } |
|
| 651 | - |
|
| 652 | - /** |
|
| 653 | - * Build criteria string and binding with various types added, like WHERE and Having |
|
| 654 | - * |
|
| 655 | - * @param array<string|Closure, mixed|mixed[]> $statements |
|
| 656 | - * @param string $key |
|
| 657 | - * @param string $type |
|
| 658 | - * @param bool $bindValues |
|
| 659 | - * |
|
| 660 | - * @return array{0:string, 1:string[]} |
|
| 661 | - */ |
|
| 662 | - protected function buildCriteriaWithType(array $statements, string $key, string $type, bool $bindValues = true) |
|
| 663 | - { |
|
| 664 | - $criteria = ''; |
|
| 665 | - $bindings = []; |
|
| 666 | - |
|
| 667 | - if (isset($statements[$key])) { |
|
| 668 | - // Get the generic/adapter agnostic criteria string from parent |
|
| 669 | - list($criteria, $bindings) = $this->buildCriteria($statements[$key], $bindValues); |
|
| 670 | - |
|
| 671 | - if ($criteria) { |
|
| 672 | - $criteria = $type . ' ' . $criteria; |
|
| 673 | - } |
|
| 674 | - } |
|
| 675 | - |
|
| 676 | - // Remove any multiple whitespace. |
|
| 677 | - $criteria = (string) preg_replace('!\s+!', ' ', $criteria); |
|
| 678 | - |
|
| 679 | - return [$criteria, $bindings]; |
|
| 680 | - } |
|
| 681 | - |
|
| 682 | - /** |
|
| 683 | - * Build join string |
|
| 684 | - * |
|
| 685 | - * @param array<string|Closure, mixed|mixed[]> $statements |
|
| 686 | - * |
|
| 687 | - * @return string |
|
| 688 | - */ |
|
| 689 | - protected function buildJoin(array $statements): string |
|
| 690 | - { |
|
| 691 | - $sql = ''; |
|
| 692 | - |
|
| 693 | - if (!array_key_exists('joins', $statements) || !is_array($statements['joins'])) { |
|
| 694 | - return $sql; |
|
| 695 | - } |
|
| 696 | - |
|
| 697 | - foreach ($statements['joins'] as $joinArr) { |
|
| 698 | - if (is_array($joinArr['table'])) { |
|
| 699 | - $mainTable = $this->stringifyValue($this->wrapSanitizer($joinArr['table'][0])); |
|
| 700 | - $aliasTable = $this->stringifyValue($this->wrapSanitizer($joinArr['table'][1])); |
|
| 701 | - $table = $mainTable . ' AS ' . $aliasTable; |
|
| 702 | - } else { |
|
| 703 | - $table = $joinArr['table'] instanceof Raw |
|
| 704 | - ? $this->parseRaw($joinArr['table']) |
|
| 705 | - : $this->wrapSanitizer($joinArr['table']); |
|
| 706 | - } |
|
| 707 | - $joinBuilder = $joinArr['joinBuilder']; |
|
| 708 | - |
|
| 709 | - /** @var string[] */ |
|
| 710 | - $sqlArr = [ |
|
| 711 | - $sql, |
|
| 712 | - strtoupper($joinArr['type']), |
|
| 713 | - 'JOIN', |
|
| 714 | - $table, |
|
| 715 | - 'ON', |
|
| 716 | - $joinBuilder->getQuery('criteriaOnly', false)->getSql(), |
|
| 717 | - ]; |
|
| 718 | - |
|
| 719 | - $sql = $this->concatenateQuery($sqlArr); |
|
| 720 | - } |
|
| 721 | - |
|
| 722 | - return $sql; |
|
| 723 | - } |
|
| 20 | + /** |
|
| 21 | + * @var string |
|
| 22 | + */ |
|
| 23 | + protected $sanitizer = ''; |
|
| 24 | + |
|
| 25 | + /** |
|
| 26 | + * @var \Pixie\Connection |
|
| 27 | + */ |
|
| 28 | + protected $connection; |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * @var \Viocon\Container |
|
| 32 | + */ |
|
| 33 | + protected $container; |
|
| 34 | + |
|
| 35 | + public function __construct(Connection $connection) |
|
| 36 | + { |
|
| 37 | + $this->connection = $connection; |
|
| 38 | + $this->container = $this->connection->getContainer(); |
|
| 39 | + } |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * Build select query string and bindings |
|
| 43 | + * |
|
| 44 | + * @param array<string|Closure, mixed|mixed[]> $statements |
|
| 45 | + * |
|
| 46 | + * @throws Exception |
|
| 47 | + * |
|
| 48 | + * @return array{sql:string,bindings:mixed[]} |
|
| 49 | + */ |
|
| 50 | + public function select(array $statements): array |
|
| 51 | + { |
|
| 52 | + if (!array_key_exists('tables', $statements)) { |
|
| 53 | + throw new Exception('No table specified.', 3); |
|
| 54 | + } elseif (!array_key_exists('selects', $statements)) { |
|
| 55 | + $statements['selects'][] = '*'; |
|
| 56 | + } |
|
| 57 | + |
|
| 58 | + // From |
|
| 59 | + $tables = $this->arrayStr($statements['tables'], ', '); |
|
| 60 | + // Select |
|
| 61 | + $selects = $this->arrayStr($statements['selects'], ', '); |
|
| 62 | + |
|
| 63 | + // Wheres |
|
| 64 | + list($whereCriteria, $whereBindings) = $this->buildCriteriaWithType($statements, 'wheres', 'WHERE'); |
|
| 65 | + |
|
| 66 | + // Group bys |
|
| 67 | + $groupBys = ''; |
|
| 68 | + if (isset($statements['groupBys']) && $groupBys = $this->arrayStr($statements['groupBys'], ', ')) { |
|
| 69 | + $groupBys = 'GROUP BY ' . $groupBys; |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + // Order bys |
|
| 73 | + $orderBys = ''; |
|
| 74 | + if (isset($statements['orderBys']) && is_array($statements['orderBys'])) { |
|
| 75 | + foreach ($statements['orderBys'] as $orderBy) { |
|
| 76 | + $field = $this->wrapSanitizer($orderBy['field']); |
|
| 77 | + if ($field instanceof Closure) { |
|
| 78 | + continue; |
|
| 79 | + } |
|
| 80 | + $orderBys .= $field . ' ' . $orderBy['type'] . ', '; |
|
| 81 | + } |
|
| 82 | + |
|
| 83 | + if ($orderBys = trim($orderBys, ', ')) { |
|
| 84 | + $orderBys = 'ORDER BY ' . $orderBys; |
|
| 85 | + } |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + // Limit and offset |
|
| 89 | + $limit = isset($statements['limit']) ? 'LIMIT ' . (int) $statements['limit'] : ''; |
|
| 90 | + $offset = isset($statements['offset']) ? 'OFFSET ' . (int) $statements['offset'] : ''; |
|
| 91 | + |
|
| 92 | + // Having |
|
| 93 | + list($havingCriteria, $havingBindings) = $this->buildCriteriaWithType($statements, 'havings', 'HAVING'); |
|
| 94 | + |
|
| 95 | + // Joins |
|
| 96 | + $joinString = $this->buildJoin($statements); |
|
| 97 | + |
|
| 98 | + /** @var string[] */ |
|
| 99 | + $sqlArray = [ |
|
| 100 | + 'SELECT' . (isset($statements['distinct']) ? ' DISTINCT' : ''), |
|
| 101 | + $selects, |
|
| 102 | + 'FROM', |
|
| 103 | + $tables, |
|
| 104 | + $joinString, |
|
| 105 | + $whereCriteria, |
|
| 106 | + $groupBys, |
|
| 107 | + $havingCriteria, |
|
| 108 | + $orderBys, |
|
| 109 | + $limit, |
|
| 110 | + $offset, |
|
| 111 | + ]; |
|
| 112 | + |
|
| 113 | + $sql = $this->concatenateQuery($sqlArray); |
|
| 114 | + |
|
| 115 | + $bindings = array_merge( |
|
| 116 | + $whereBindings, |
|
| 117 | + $havingBindings |
|
| 118 | + ); |
|
| 119 | + |
|
| 120 | + return compact('sql', 'bindings'); |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + /** |
|
| 124 | + * Build just criteria part of the query |
|
| 125 | + * |
|
| 126 | + * @param array<string|Closure, mixed|mixed[]> $statements |
|
| 127 | + * @param bool $bindValues |
|
| 128 | + * |
|
| 129 | + * @return array{sql:string[]|string, bindings:array<mixed>} |
|
| 130 | + */ |
|
| 131 | + public function criteriaOnly(array $statements, bool $bindValues = true): array |
|
| 132 | + { |
|
| 133 | + $sql = $bindings = []; |
|
| 134 | + if (!isset($statements['criteria'])) { |
|
| 135 | + return compact('sql', 'bindings'); |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + list($sql, $bindings) = $this->buildCriteria($statements['criteria'], $bindValues); |
|
| 139 | + |
|
| 140 | + return compact('sql', 'bindings'); |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + /** |
|
| 144 | + * Build a generic insert/ignore/replace query |
|
| 145 | + * |
|
| 146 | + * @param array<string|Closure, mixed|mixed[]> $statements |
|
| 147 | + * @param array<string, mixed> $data |
|
| 148 | + * @param string $type |
|
| 149 | + * |
|
| 150 | + * @return array{sql:string, bindings:mixed[]} |
|
| 151 | + * |
|
| 152 | + * @throws Exception |
|
| 153 | + */ |
|
| 154 | + private function doInsert(array $statements, array $data, string $type): array |
|
| 155 | + { |
|
| 156 | + if (!isset($statements['tables'])) { |
|
| 157 | + throw new Exception('No table specified', 3); |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + $table = end($statements['tables']); |
|
| 161 | + |
|
| 162 | + $bindings = $keys = $values = []; |
|
| 163 | + |
|
| 164 | + foreach ($data as $key => $value) { |
|
| 165 | + $keys[] = $key; |
|
| 166 | + |
|
| 167 | + // Handle value as bindings |
|
| 168 | + $isBindings = $value instanceof Binding; |
|
| 169 | + // If this is a raw binding, extract the Raw and replace value. |
|
| 170 | + if ($isBindings && $value->isRaw()) { |
|
| 171 | + $value = $value->getValue(); |
|
| 172 | + } |
|
| 173 | + |
|
| 174 | + if ($value instanceof Raw) { |
|
| 175 | + $values[] = $this->parseRaw($value); |
|
| 176 | + } elseif ($isBindings) { |
|
| 177 | + $values[] = $value->getType(); |
|
| 178 | + $bindings[] = $value->getValue(); |
|
| 179 | + } else { |
|
| 180 | + $values[] = $this->inferType($value); |
|
| 181 | + $bindings[] = $value; |
|
| 182 | + } |
|
| 183 | + } |
|
| 184 | + |
|
| 185 | + $sqlArray = [ |
|
| 186 | + $type . ' INTO', |
|
| 187 | + $this->wrapSanitizer($table), |
|
| 188 | + '(' . $this->arrayStr($keys, ',') . ')', |
|
| 189 | + 'VALUES', |
|
| 190 | + '(' . $this->arrayStr($values, ',') . ')', |
|
| 191 | + ]; |
|
| 192 | + |
|
| 193 | + if (isset($statements['onduplicate'])) { |
|
| 194 | + if (count($statements['onduplicate']) < 1) { |
|
| 195 | + throw new Exception('No data given.', 4); |
|
| 196 | + } |
|
| 197 | + list($updateStatement, $updateBindings) = $this->getUpdateStatement($statements['onduplicate']); |
|
| 198 | + $sqlArray[] = 'ON DUPLICATE KEY UPDATE ' . $updateStatement; |
|
| 199 | + $bindings = array_merge($bindings, $updateBindings); |
|
| 200 | + } |
|
| 201 | + |
|
| 202 | + $sql = $this->concatenateQuery($this->stringifyValues($sqlArray)); |
|
| 203 | + |
|
| 204 | + return compact('sql', 'bindings'); |
|
| 205 | + } |
|
| 206 | + |
|
| 207 | + /** |
|
| 208 | + * Attempts to stringify an array of values. |
|
| 209 | + * |
|
| 210 | + * @param array<string|int, string|Closure> $values |
|
| 211 | + * |
|
| 212 | + * @return string[] |
|
| 213 | + */ |
|
| 214 | + protected function stringifyValues(array $values): array |
|
| 215 | + { |
|
| 216 | + return array_filter(array_map([$this, 'stringifyValue'], $values)); |
|
| 217 | + } |
|
| 218 | + |
|
| 219 | + /** |
|
| 220 | + * Attempts to stringify a single of values. |
|
| 221 | + * |
|
| 222 | + * @param string|Closure|Raw $value |
|
| 223 | + * |
|
| 224 | + * @return string|null |
|
| 225 | + */ |
|
| 226 | + protected function stringifyValue($value): ?string |
|
| 227 | + { |
|
| 228 | + if ($value instanceof Closure) { |
|
| 229 | + $value = $value(); |
|
| 230 | + |
|
| 231 | + return is_string($value) ? $value : null; |
|
| 232 | + } |
|
| 233 | + |
|
| 234 | + if ($value instanceof Raw) { |
|
| 235 | + return $this->parseRaw($value); |
|
| 236 | + } |
|
| 237 | + |
|
| 238 | + return $value; |
|
| 239 | + } |
|
| 240 | + |
|
| 241 | + /** |
|
| 242 | + * Build Insert query |
|
| 243 | + * |
|
| 244 | + * @param array<string|Closure, mixed|mixed[]> $statements |
|
| 245 | + * @param array<string, mixed> $data $data |
|
| 246 | + * |
|
| 247 | + * @return array{sql:string, bindings:mixed[]} |
|
| 248 | + * |
|
| 249 | + * @throws Exception |
|
| 250 | + */ |
|
| 251 | + public function insert($statements, array $data) |
|
| 252 | + { |
|
| 253 | + return $this->doInsert($statements, $data, 'INSERT'); |
|
| 254 | + } |
|
| 255 | + |
|
| 256 | + /** |
|
| 257 | + * Build Insert Ignore query |
|
| 258 | + * |
|
| 259 | + * @param array<string|Closure, mixed|mixed[]> $statements |
|
| 260 | + * @param array<string, mixed> $data $data |
|
| 261 | + * |
|
| 262 | + * @return array{sql:string, bindings:mixed[]} |
|
| 263 | + * |
|
| 264 | + * @throws Exception |
|
| 265 | + */ |
|
| 266 | + public function insertIgnore($statements, array $data) |
|
| 267 | + { |
|
| 268 | + return $this->doInsert($statements, $data, 'INSERT IGNORE'); |
|
| 269 | + } |
|
| 270 | + |
|
| 271 | + /** |
|
| 272 | + * Build Insert Ignore query |
|
| 273 | + * |
|
| 274 | + * @param array<string|Closure, mixed|mixed[]> $statements |
|
| 275 | + * @param array<string, mixed> $data $data |
|
| 276 | + * |
|
| 277 | + * @return array{sql:string, bindings:mixed[]} |
|
| 278 | + * |
|
| 279 | + * @throws Exception |
|
| 280 | + */ |
|
| 281 | + public function replace($statements, array $data) |
|
| 282 | + { |
|
| 283 | + return $this->doInsert($statements, $data, 'REPLACE'); |
|
| 284 | + } |
|
| 285 | + |
|
| 286 | + /** |
|
| 287 | + * Build fields assignment part of SET ... or ON DUBLICATE KEY UPDATE ... statements |
|
| 288 | + * |
|
| 289 | + * @param array<string, mixed> $data |
|
| 290 | + * |
|
| 291 | + * @return array{0:string,1:mixed[]} |
|
| 292 | + */ |
|
| 293 | + private function getUpdateStatement(array $data): array |
|
| 294 | + { |
|
| 295 | + $bindings = []; |
|
| 296 | + $statement = ''; |
|
| 297 | + |
|
| 298 | + foreach ($data as $key => $value) { |
|
| 299 | + $isBindings = $value instanceof Binding; |
|
| 300 | + // If this is a raw binding, extract the Raw and replace value. |
|
| 301 | + if ($isBindings && $value->isRaw()) { |
|
| 302 | + $value = $value->getValue(); |
|
| 303 | + } |
|
| 304 | + |
|
| 305 | + if ($value instanceof Raw) { |
|
| 306 | + $statement .= $this->stringifyValue($this->wrapSanitizer($key)) . '=' . $value . ','; |
|
| 307 | + } elseif ($isBindings) { |
|
| 308 | + $statement .= $this->stringifyValue($this->wrapSanitizer($key)) . sprintf('=%s,', $value->getType()); |
|
| 309 | + $bindings[] = $value->getValue(); |
|
| 310 | + } else { |
|
| 311 | + $statement .= $this->stringifyValue($this->wrapSanitizer($key)) . sprintf('=%s,', $this->inferType($value)); |
|
| 312 | + $bindings[] = $value; |
|
| 313 | + } |
|
| 314 | + } |
|
| 315 | + |
|
| 316 | + $statement = trim($statement, ','); |
|
| 317 | + |
|
| 318 | + return [$statement, $bindings]; |
|
| 319 | + } |
|
| 320 | + |
|
| 321 | + /** |
|
| 322 | + * Build update query |
|
| 323 | + * |
|
| 324 | + * @param array<string|Closure, mixed|mixed[]> $statements |
|
| 325 | + * @param array<string, mixed> $data |
|
| 326 | + * |
|
| 327 | + * @return array{sql:string, bindings:mixed[]} |
|
| 328 | + * |
|
| 329 | + * @throws Exception |
|
| 330 | + */ |
|
| 331 | + public function update($statements, array $data) |
|
| 332 | + { |
|
| 333 | + if (!isset($statements['tables'])) { |
|
| 334 | + throw new Exception('No table specified', 3); |
|
| 335 | + } elseif (count($data) < 1) { |
|
| 336 | + throw new Exception('No data given.', 4); |
|
| 337 | + } |
|
| 338 | + |
|
| 339 | + $table = end($statements['tables']); |
|
| 340 | + |
|
| 341 | + // Update statement |
|
| 342 | + list($updateStatement, $bindings) = $this->getUpdateStatement($data); |
|
| 343 | + |
|
| 344 | + // Wheres |
|
| 345 | + list($whereCriteria, $whereBindings) = $this->buildCriteriaWithType($statements, 'wheres', 'WHERE'); |
|
| 346 | + |
|
| 347 | + // Limit |
|
| 348 | + $limit = isset($statements['limit']) ? 'LIMIT ' . $statements['limit'] : ''; |
|
| 349 | + |
|
| 350 | + $sqlArray = [ |
|
| 351 | + 'UPDATE', |
|
| 352 | + $this->wrapSanitizer($table), |
|
| 353 | + 'SET ' . $updateStatement, |
|
| 354 | + $whereCriteria, |
|
| 355 | + $limit, |
|
| 356 | + ]; |
|
| 357 | + |
|
| 358 | + $sql = $this->concatenateQuery($this->stringifyValues($sqlArray)); |
|
| 359 | + |
|
| 360 | + $bindings = array_merge($bindings, $whereBindings); |
|
| 361 | + |
|
| 362 | + return compact('sql', 'bindings'); |
|
| 363 | + } |
|
| 364 | + |
|
| 365 | + /** |
|
| 366 | + * Build delete query |
|
| 367 | + * |
|
| 368 | + * @param array<string|Closure, mixed|mixed[]> $statements |
|
| 369 | + * |
|
| 370 | + * @return array{sql:string, bindings:mixed[]} |
|
| 371 | + * |
|
| 372 | + * @throws Exception |
|
| 373 | + */ |
|
| 374 | + public function delete($statements) |
|
| 375 | + { |
|
| 376 | + if (!isset($statements['tables'])) { |
|
| 377 | + throw new Exception('No table specified', 3); |
|
| 378 | + } |
|
| 379 | + |
|
| 380 | + $table = end($statements['tables']); |
|
| 381 | + // Ensure table name is a string |
|
| 382 | + $table = $this->stringifyValue($this->wrapSanitizer($table)); |
|
| 383 | + if (null === $table) { |
|
| 384 | + throw new Exception('Table must be a valid string.', 5); |
|
| 385 | + } |
|
| 386 | + |
|
| 387 | + // Wheres |
|
| 388 | + list($whereCriteria, $whereBindings) = $this->buildCriteriaWithType($statements, 'wheres', 'WHERE'); |
|
| 389 | + |
|
| 390 | + // Limit |
|
| 391 | + $limit = isset($statements['limit']) ? 'LIMIT ' . $statements['limit'] : ''; |
|
| 392 | + |
|
| 393 | + $sqlArray = ['DELETE FROM', $table, $whereCriteria]; |
|
| 394 | + $sql = $this->concatenateQuery($sqlArray); |
|
| 395 | + $bindings = $whereBindings; |
|
| 396 | + |
|
| 397 | + return compact('sql', 'bindings'); |
|
| 398 | + } |
|
| 399 | + |
|
| 400 | + /** |
|
| 401 | + * Array concatenating method, like implode. |
|
| 402 | + * But it does wrap sanitizer and trims last glue |
|
| 403 | + * |
|
| 404 | + * @param array<string|int, string> $pieces |
|
| 405 | + * @param string $glue |
|
| 406 | + * |
|
| 407 | + * @return string |
|
| 408 | + */ |
|
| 409 | + protected function arrayStr(array $pieces, string $glue): string |
|
| 410 | + { |
|
| 411 | + $str = ''; |
|
| 412 | + foreach ($pieces as $key => $piece) { |
|
| 413 | + if (!is_int($key)) { |
|
| 414 | + $piece = $key . ' AS ' . $piece; |
|
| 415 | + } |
|
| 416 | + |
|
| 417 | + $str .= $piece . $glue; |
|
| 418 | + } |
|
| 419 | + |
|
| 420 | + return trim($str, $glue); |
|
| 421 | + } |
|
| 422 | + |
|
| 423 | + /** |
|
| 424 | + * Join different part of queries with a space. |
|
| 425 | + * |
|
| 426 | + * @param array<string|int, string> $pieces |
|
| 427 | + * |
|
| 428 | + * @return string |
|
| 429 | + */ |
|
| 430 | + protected function concatenateQuery(array $pieces): string |
|
| 431 | + { |
|
| 432 | + $str = ''; |
|
| 433 | + foreach ($pieces as $piece) { |
|
| 434 | + $str = trim($str) . ' ' . trim($piece); |
|
| 435 | + } |
|
| 436 | + |
|
| 437 | + return trim($str); |
|
| 438 | + } |
|
| 439 | + |
|
| 440 | + /** |
|
| 441 | + * Gets the type of a value, either from a binding or infered |
|
| 442 | + * |
|
| 443 | + * @param mixed $value |
|
| 444 | + * @return string |
|
| 445 | + */ |
|
| 446 | + public function getType($value): string |
|
| 447 | + { |
|
| 448 | + return $value instanceof Binding && $value->getType() !== null |
|
| 449 | + ? $value->getType() : $this->inferType($value) ; |
|
| 450 | + } |
|
| 451 | + |
|
| 452 | + /** |
|
| 453 | + * Get the value from a possible Bindings object. |
|
| 454 | + * |
|
| 455 | + * @param mixed $value |
|
| 456 | + * @return mixed |
|
| 457 | + */ |
|
| 458 | + public function getValue($value) |
|
| 459 | + { |
|
| 460 | + return $value instanceof Binding ? $value->getValue() : $value; |
|
| 461 | + } |
|
| 462 | + |
|
| 463 | + /** |
|
| 464 | + * Attempts to parse a raw query, if bindings are defined then they will be bound first. |
|
| 465 | + * |
|
| 466 | + * @param Raw $raw |
|
| 467 | + * @requires string |
|
| 468 | + */ |
|
| 469 | + public function parseRaw(Raw $raw): string |
|
| 470 | + { |
|
| 471 | + $bindings = $raw->getBindings(); |
|
| 472 | + return 0 === count($bindings) |
|
| 473 | + ? (string) $raw |
|
| 474 | + : $this->interpolateQuery($raw->getValue(), $bindings); |
|
| 475 | + } |
|
| 476 | + |
|
| 477 | + /** |
|
| 478 | + * Interpolates a query |
|
| 479 | + * |
|
| 480 | + * @param string $query |
|
| 481 | + * @param array<mixed> $bindings |
|
| 482 | + * @return string |
|
| 483 | + */ |
|
| 484 | + public function interpolateQuery(string $query, array $bindings = []): string |
|
| 485 | + { |
|
| 486 | + if (0 === count($bindings)) { |
|
| 487 | + return $query; |
|
| 488 | + } |
|
| 489 | + |
|
| 490 | + |
|
| 491 | + $bindings = array_map([$this, 'getValue'], $bindings); |
|
| 492 | + $query = $this->connection->getDbInstance()->prepare($query, $bindings) ; |
|
| 493 | + return is_string($query) ? $query : ''; |
|
| 494 | + } |
|
| 495 | + |
|
| 496 | + /** |
|
| 497 | + * Build generic criteria string and bindings from statements, like "a = b and c = ?" |
|
| 498 | + * |
|
| 499 | + * @param array<string|Closure, mixed|mixed[]> $statements |
|
| 500 | + * @param bool $bindValues |
|
| 501 | + * |
|
| 502 | + * @return array{0:string,1:string[]} |
|
| 503 | + */ |
|
| 504 | + protected function buildCriteria(array $statements, bool $bindValues = true): array |
|
| 505 | + { |
|
| 506 | + $criteria = ''; |
|
| 507 | + $bindings = []; |
|
| 508 | + foreach ($statements as $statement) { |
|
| 509 | + $key = $statement['key']; |
|
| 510 | + $value = $statement['value']; |
|
| 511 | + |
|
| 512 | + // If the value is a Raw Binding, cast to raw |
|
| 513 | + if ($value instanceof Binding && Binding::RAW === $value->getType()) { |
|
| 514 | + /** @var Raw */ |
|
| 515 | + $value = $value->getValue(); |
|
| 516 | + } |
|
| 517 | + |
|
| 518 | + if (is_null($value) && $key instanceof Closure) { |
|
| 519 | + // We have a closure, a nested criteria |
|
| 520 | + |
|
| 521 | + // Build a new NestedCriteria class, keep it by reference so any changes made |
|
| 522 | + // in the closure should reflect here |
|
| 523 | + $nestedCriteria = $this->container->build(NestedCriteria::class, [$this->connection]); |
|
| 524 | + |
|
| 525 | + $nestedCriteria = &$nestedCriteria; |
|
| 526 | + // Call the closure with our new nestedCriteria object |
|
| 527 | + $key($nestedCriteria); |
|
| 528 | + // Get the criteria only query from the nestedCriteria object |
|
| 529 | + $queryObject = $nestedCriteria->getQuery('criteriaOnly', true); |
|
| 530 | + // Merge the bindings we get from nestedCriteria object |
|
| 531 | + $bindings = array_merge($bindings, $queryObject->getBindings()); |
|
| 532 | + // Append the sql we get from the nestedCriteria object |
|
| 533 | + $criteria .= $statement['joiner'] . ' (' . $queryObject->getSql() . ') '; |
|
| 534 | + } elseif (is_array($value)) { |
|
| 535 | + // where_in or between like query |
|
| 536 | + $criteria .= $statement['joiner'] . ' ' . $key . ' ' . $statement['operator']; |
|
| 537 | + |
|
| 538 | + switch ($statement['operator']) { |
|
| 539 | + case 'BETWEEN': |
|
| 540 | + $bindings = array_merge($bindings, $statement['value']); |
|
| 541 | + $criteria .= sprintf( |
|
| 542 | + ' %s AND %s ', |
|
| 543 | + $this->getType($value[0]), |
|
| 544 | + $this->getType($value[1]) |
|
| 545 | + ); |
|
| 546 | + |
|
| 547 | + // Maybe cast the values bindings. |
|
| 548 | + $value[0] = $this->getValue($value[0]); |
|
| 549 | + $value[1] = $this->getValue($value[1]); |
|
| 550 | + break; |
|
| 551 | + default: |
|
| 552 | + $valuePlaceholder = ''; |
|
| 553 | + foreach ($statement['value'] as $subValue) { |
|
| 554 | + // Get its value. |
|
| 555 | + if ($this->getValue($subValue) instanceof Raw) { |
|
| 556 | + /** @var Raw $subValue */ |
|
| 557 | + $subValue = $this->getValue($subValue); |
|
| 558 | + $valuePlaceholder .= sprintf('%s, ', $this->parseRaw($subValue)); |
|
| 559 | + continue; |
|
| 560 | + } |
|
| 561 | + |
|
| 562 | + |
|
| 563 | + // Add in format placeholders. |
|
| 564 | + $valuePlaceholder .= sprintf('%s, ', $this->getType($subValue)); // glynn |
|
| 565 | + $bindings[] = $this->getValue($subValue); |
|
| 566 | + } |
|
| 567 | + |
|
| 568 | + $valuePlaceholder = trim($valuePlaceholder, ', '); |
|
| 569 | + $criteria .= ' (' . $valuePlaceholder . ') '; |
|
| 570 | + break; |
|
| 571 | + } |
|
| 572 | + } elseif ($value instanceof Raw) { |
|
| 573 | + $value = $this->parseRaw($value); |
|
| 574 | + $criteria .= "{$statement['joiner']} {$key} {$statement['operator']} $value "; |
|
| 575 | + } else { |
|
| 576 | + // Usual where like criteria |
|
| 577 | + if (!$bindValues) { |
|
| 578 | + // Specially for joins |
|
| 579 | + // We are not binding values, lets sanitize then |
|
| 580 | + $value = $this->stringifyValue($this->wrapSanitizer($value)) ?? ''; |
|
| 581 | + $criteria .= $statement['joiner'] . ' ' . $key . ' ' . $statement['operator'] . ' ' . $value . ' '; |
|
| 582 | + } elseif ($statement['key'] instanceof Raw) { |
|
| 583 | + $criteria .= $statement['joiner'] . ' ' . $key . ' '; |
|
| 584 | + $bindings = array_merge($bindings, $statement['key']->getBindings()); |
|
| 585 | + } else { |
|
| 586 | + // For wheres |
|
| 587 | + $bindings[] = $this->getValue($value); |
|
| 588 | + |
|
| 589 | + $criteria .= $statement['joiner'] . ' ' . $key . ' ' . $statement['operator'] . ' ' |
|
| 590 | + . $this->getType($value) . ' '; |
|
| 591 | + } |
|
| 592 | + } |
|
| 593 | + } |
|
| 594 | + |
|
| 595 | + // Clear all white spaces, and, or from beginning and white spaces from ending |
|
| 596 | + $criteria = preg_replace('/^(\s?AND ?|\s?OR ?)|\s$/i', '', $criteria); |
|
| 597 | + |
|
| 598 | + return [$criteria ?? '', $bindings]; |
|
| 599 | + } |
|
| 600 | + |
|
| 601 | + /** |
|
| 602 | + * Asserts the types place holder based on its value |
|
| 603 | + * |
|
| 604 | + * @param mixed $value |
|
| 605 | + * |
|
| 606 | + * @return string |
|
| 607 | + */ |
|
| 608 | + public function inferType($value): string |
|
| 609 | + { |
|
| 610 | + switch (true) { |
|
| 611 | + case is_string($value): |
|
| 612 | + return '%s'; |
|
| 613 | + case \is_int($value): |
|
| 614 | + case is_bool($value): |
|
| 615 | + return '%d'; |
|
| 616 | + case is_float($value): |
|
| 617 | + return '%f'; |
|
| 618 | + default: |
|
| 619 | + return ''; |
|
| 620 | + } |
|
| 621 | + } |
|
| 622 | + |
|
| 623 | + /** |
|
| 624 | + * Wrap values with adapter's sanitizer like, '`' |
|
| 625 | + * |
|
| 626 | + * @param string|Raw|Closure $value |
|
| 627 | + * |
|
| 628 | + * @return string|Closure |
|
| 629 | + */ |
|
| 630 | + public function wrapSanitizer($value) |
|
| 631 | + { |
|
| 632 | + // Its a raw query, just cast as string, object has __toString() |
|
| 633 | + if ($value instanceof Raw) { |
|
| 634 | + return $this->parseRaw($value); |
|
| 635 | + } elseif ($value instanceof Closure) { |
|
| 636 | + return $value; |
|
| 637 | + } |
|
| 638 | + |
|
| 639 | + // Separate our table and fields which are joined with a ".", |
|
| 640 | + // like my_table.id |
|
| 641 | + $valueArr = explode('.', $value, 2); |
|
| 642 | + |
|
| 643 | + foreach ($valueArr as $key => $subValue) { |
|
| 644 | + // Don't wrap if we have *, which is not a usual field |
|
| 645 | + $valueArr[$key] = '*' == trim($subValue) ? $subValue : $this->sanitizer . $subValue . $this->sanitizer; |
|
| 646 | + } |
|
| 647 | + |
|
| 648 | + // Join these back with "." and return |
|
| 649 | + return implode('.', $valueArr); |
|
| 650 | + } |
|
| 651 | + |
|
| 652 | + /** |
|
| 653 | + * Build criteria string and binding with various types added, like WHERE and Having |
|
| 654 | + * |
|
| 655 | + * @param array<string|Closure, mixed|mixed[]> $statements |
|
| 656 | + * @param string $key |
|
| 657 | + * @param string $type |
|
| 658 | + * @param bool $bindValues |
|
| 659 | + * |
|
| 660 | + * @return array{0:string, 1:string[]} |
|
| 661 | + */ |
|
| 662 | + protected function buildCriteriaWithType(array $statements, string $key, string $type, bool $bindValues = true) |
|
| 663 | + { |
|
| 664 | + $criteria = ''; |
|
| 665 | + $bindings = []; |
|
| 666 | + |
|
| 667 | + if (isset($statements[$key])) { |
|
| 668 | + // Get the generic/adapter agnostic criteria string from parent |
|
| 669 | + list($criteria, $bindings) = $this->buildCriteria($statements[$key], $bindValues); |
|
| 670 | + |
|
| 671 | + if ($criteria) { |
|
| 672 | + $criteria = $type . ' ' . $criteria; |
|
| 673 | + } |
|
| 674 | + } |
|
| 675 | + |
|
| 676 | + // Remove any multiple whitespace. |
|
| 677 | + $criteria = (string) preg_replace('!\s+!', ' ', $criteria); |
|
| 678 | + |
|
| 679 | + return [$criteria, $bindings]; |
|
| 680 | + } |
|
| 681 | + |
|
| 682 | + /** |
|
| 683 | + * Build join string |
|
| 684 | + * |
|
| 685 | + * @param array<string|Closure, mixed|mixed[]> $statements |
|
| 686 | + * |
|
| 687 | + * @return string |
|
| 688 | + */ |
|
| 689 | + protected function buildJoin(array $statements): string |
|
| 690 | + { |
|
| 691 | + $sql = ''; |
|
| 692 | + |
|
| 693 | + if (!array_key_exists('joins', $statements) || !is_array($statements['joins'])) { |
|
| 694 | + return $sql; |
|
| 695 | + } |
|
| 696 | + |
|
| 697 | + foreach ($statements['joins'] as $joinArr) { |
|
| 698 | + if (is_array($joinArr['table'])) { |
|
| 699 | + $mainTable = $this->stringifyValue($this->wrapSanitizer($joinArr['table'][0])); |
|
| 700 | + $aliasTable = $this->stringifyValue($this->wrapSanitizer($joinArr['table'][1])); |
|
| 701 | + $table = $mainTable . ' AS ' . $aliasTable; |
|
| 702 | + } else { |
|
| 703 | + $table = $joinArr['table'] instanceof Raw |
|
| 704 | + ? $this->parseRaw($joinArr['table']) |
|
| 705 | + : $this->wrapSanitizer($joinArr['table']); |
|
| 706 | + } |
|
| 707 | + $joinBuilder = $joinArr['joinBuilder']; |
|
| 708 | + |
|
| 709 | + /** @var string[] */ |
|
| 710 | + $sqlArr = [ |
|
| 711 | + $sql, |
|
| 712 | + strtoupper($joinArr['type']), |
|
| 713 | + 'JOIN', |
|
| 714 | + $table, |
|
| 715 | + 'ON', |
|
| 716 | + $joinBuilder->getQuery('criteriaOnly', false)->getSql(), |
|
| 717 | + ]; |
|
| 718 | + |
|
| 719 | + $sql = $this->concatenateQuery($sqlArr); |
|
| 720 | + } |
|
| 721 | + |
|
| 722 | + return $sql; |
|
| 723 | + } |
|
| 724 | 724 | } |
@@ -4,43 +4,43 @@ |
||
| 4 | 4 | |
| 5 | 5 | class JoinBuilder extends QueryBuilderHandler |
| 6 | 6 | { |
| 7 | - /** |
|
| 8 | - * @param string|Raw $key |
|
| 9 | - * @param string|null $operator |
|
| 10 | - * @param mixed $value |
|
| 11 | - * |
|
| 12 | - * @return static |
|
| 13 | - */ |
|
| 14 | - public function on($key, ?string $operator, $value): self |
|
| 15 | - { |
|
| 16 | - return $this->joinHandler($key, $operator, $value, 'AND'); |
|
| 17 | - } |
|
| 7 | + /** |
|
| 8 | + * @param string|Raw $key |
|
| 9 | + * @param string|null $operator |
|
| 10 | + * @param mixed $value |
|
| 11 | + * |
|
| 12 | + * @return static |
|
| 13 | + */ |
|
| 14 | + public function on($key, ?string $operator, $value): self |
|
| 15 | + { |
|
| 16 | + return $this->joinHandler($key, $operator, $value, 'AND'); |
|
| 17 | + } |
|
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * @param string|Raw $key |
|
| 21 | - * @param string|null $operator |
|
| 22 | - * @param mixed $value |
|
| 23 | - * |
|
| 24 | - * @return static |
|
| 25 | - */ |
|
| 26 | - public function orOn($key, ?string $operator, $value): self |
|
| 27 | - { |
|
| 28 | - return $this->joinHandler($key, $operator, $value, 'OR'); |
|
| 29 | - } |
|
| 19 | + /** |
|
| 20 | + * @param string|Raw $key |
|
| 21 | + * @param string|null $operator |
|
| 22 | + * @param mixed $value |
|
| 23 | + * |
|
| 24 | + * @return static |
|
| 25 | + */ |
|
| 26 | + public function orOn($key, ?string $operator, $value): self |
|
| 27 | + { |
|
| 28 | + return $this->joinHandler($key, $operator, $value, 'OR'); |
|
| 29 | + } |
|
| 30 | 30 | |
| 31 | - /** |
|
| 32 | - * @param string|Raw $key |
|
| 33 | - * @param string|null $operator |
|
| 34 | - * @param mixed $value |
|
| 35 | - * |
|
| 36 | - * @return static |
|
| 37 | - */ |
|
| 38 | - protected function joinHandler($key, ?string $operator = null, $value = null, string $joiner = 'AND'): self |
|
| 39 | - { |
|
| 40 | - $key = $this->addTablePrefix($key); |
|
| 41 | - $value = $this->addTablePrefix($value); |
|
| 42 | - $this->statements['criteria'][] = compact('key', 'operator', 'value', 'joiner'); |
|
| 31 | + /** |
|
| 32 | + * @param string|Raw $key |
|
| 33 | + * @param string|null $operator |
|
| 34 | + * @param mixed $value |
|
| 35 | + * |
|
| 36 | + * @return static |
|
| 37 | + */ |
|
| 38 | + protected function joinHandler($key, ?string $operator = null, $value = null, string $joiner = 'AND'): self |
|
| 39 | + { |
|
| 40 | + $key = $this->addTablePrefix($key); |
|
| 41 | + $value = $this->addTablePrefix($value); |
|
| 42 | + $this->statements['criteria'][] = compact('key', 'operator', 'value', 'joiner'); |
|
| 43 | 43 | |
| 44 | - return $this; |
|
| 45 | - } |
|
| 44 | + return $this; |
|
| 45 | + } |
|
| 46 | 46 | } |
@@ -6,73 +6,72 @@ |
||
| 6 | 6 | |
| 7 | 7 | class QueryObject |
| 8 | 8 | { |
| 9 | - /** |
|
| 10 | - * @var string |
|
| 11 | - */ |
|
| 12 | - protected $sql; |
|
| 9 | + /** |
|
| 10 | + * @var string |
|
| 11 | + */ |
|
| 12 | + protected $sql; |
|
| 13 | 13 | |
| 14 | - /** |
|
| 15 | - * @var mixed[] |
|
| 16 | - */ |
|
| 17 | - protected $bindings = []; |
|
| 14 | + /** |
|
| 15 | + * @var mixed[] |
|
| 16 | + */ |
|
| 17 | + protected $bindings = []; |
|
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * @var wpdb |
|
| 21 | - */ |
|
| 22 | - protected $dbInstance; |
|
| 19 | + /** |
|
| 20 | + * @var wpdb |
|
| 21 | + */ |
|
| 22 | + protected $dbInstance; |
|
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * @param string $sql |
|
| 26 | - * @param mixed[] $bindings |
|
| 27 | - * @param wpdb $dbInstance |
|
| 28 | - */ |
|
| 29 | - public function __construct(string $sql, array $bindings, wpdb $dbInstance) |
|
| 30 | - { |
|
| 31 | - $this->sql = (string)$sql; |
|
| 32 | - $this->bindings = $bindings; |
|
| 33 | - $this->dbInstance = $dbInstance; |
|
| 34 | - } |
|
| 24 | + /** |
|
| 25 | + * @param string $sql |
|
| 26 | + * @param mixed[] $bindings |
|
| 27 | + * @param wpdb $dbInstance |
|
| 28 | + */ |
|
| 29 | + public function __construct(string $sql, array $bindings, wpdb $dbInstance) |
|
| 30 | + { |
|
| 31 | + $this->sql = (string)$sql; |
|
| 32 | + $this->bindings = $bindings; |
|
| 33 | + $this->dbInstance = $dbInstance; |
|
| 34 | + } |
|
| 35 | 35 | |
| 36 | - /** |
|
| 37 | - * @return string |
|
| 38 | - */ |
|
| 39 | - public function getSql() |
|
| 40 | - { |
|
| 41 | - return $this->sql; |
|
| 42 | - } |
|
| 36 | + /** |
|
| 37 | + * @return string |
|
| 38 | + */ |
|
| 39 | + public function getSql() |
|
| 40 | + { |
|
| 41 | + return $this->sql; |
|
| 42 | + } |
|
| 43 | 43 | |
| 44 | - /** |
|
| 45 | - * @return mixed[] |
|
| 46 | - */ |
|
| 47 | - public function getBindings() |
|
| 48 | - { |
|
| 49 | - return $this->bindings; |
|
| 50 | - } |
|
| 44 | + /** |
|
| 45 | + * @return mixed[] |
|
| 46 | + */ |
|
| 47 | + public function getBindings() |
|
| 48 | + { |
|
| 49 | + return $this->bindings; |
|
| 50 | + } |
|
| 51 | 51 | |
| 52 | - /** |
|
| 53 | - * Get the raw/bound sql |
|
| 54 | - * |
|
| 55 | - * @return string |
|
| 56 | - */ |
|
| 57 | - public function getRawSql() |
|
| 58 | - { |
|
| 59 | - return $this->interpolateQuery($this->sql, $this->bindings); |
|
| 60 | - } |
|
| 52 | + /** |
|
| 53 | + * Get the raw/bound sql |
|
| 54 | + * |
|
| 55 | + * @return string |
|
| 56 | + */ |
|
| 57 | + public function getRawSql() |
|
| 58 | + { |
|
| 59 | + return $this->interpolateQuery($this->sql, $this->bindings); |
|
| 60 | + } |
|
| 61 | 61 | |
| 62 | - /** |
|
| 63 | - * Uses WPDB::prepare() to interpolate the query passed. |
|
| 62 | + /** |
|
| 63 | + * Uses WPDB::prepare() to interpolate the query passed. |
|
| 64 | + * |
|
| 65 | + * @param string $query The sql query with parameter placeholders |
|
| 66 | + * @param mixed[] $params The array of substitution parameters |
|
| 67 | + * |
|
| 68 | + * @return string The interpolated query |
|
| 69 | + */ |
|
| 70 | + protected function interpolateQuery($query, $params): string |
|
| 71 | + { |
|
| 72 | + // Only call this when we have valid params (avoids wpdb::prepare() incorrectly called error) |
|
| 73 | + $value = empty($params) ? $query : $this->dbInstance->prepare($query, $params); |
|
| 64 | 74 | |
| 65 | - * |
|
| 66 | - * @param string $query The sql query with parameter placeholders |
|
| 67 | - * @param mixed[] $params The array of substitution parameters |
|
| 68 | - * |
|
| 69 | - * @return string The interpolated query |
|
| 70 | - */ |
|
| 71 | - protected function interpolateQuery($query, $params): string |
|
| 72 | - { |
|
| 73 | - // Only call this when we have valid params (avoids wpdb::prepare() incorrectly called error) |
|
| 74 | - $value = empty($params) ? $query : $this->dbInstance->prepare($query, $params); |
|
| 75 | - |
|
| 76 | - return is_string($value) ? $value : ''; |
|
| 77 | - } |
|
| 75 | + return is_string($value) ? $value : ''; |
|
| 76 | + } |
|
| 78 | 77 | } |
@@ -28,7 +28,7 @@ |
||
| 28 | 28 | */ |
| 29 | 29 | public function __construct(string $sql, array $bindings, wpdb $dbInstance) |
| 30 | 30 | { |
| 31 | - $this->sql = (string)$sql; |
|
| 31 | + $this->sql = (string) $sql; |
|
| 32 | 32 | $this->bindings = $bindings; |
| 33 | 33 | $this->dbInstance = $dbInstance; |
| 34 | 34 | } |
@@ -4,51 +4,51 @@ |
||
| 4 | 4 | |
| 5 | 5 | class Raw |
| 6 | 6 | { |
| 7 | - /** |
|
| 8 | - * @var string |
|
| 9 | - */ |
|
| 10 | - protected $value; |
|
| 7 | + /** |
|
| 8 | + * @var string |
|
| 9 | + */ |
|
| 10 | + protected $value; |
|
| 11 | 11 | |
| 12 | - /** |
|
| 13 | - * @var mixed[] |
|
| 14 | - */ |
|
| 15 | - protected $bindings; |
|
| 12 | + /** |
|
| 13 | + * @var mixed[] |
|
| 14 | + */ |
|
| 15 | + protected $bindings; |
|
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * @param string $value |
|
| 19 | - * @param mixed|mixed[] $bindings |
|
| 20 | - */ |
|
| 21 | - public function __construct($value, $bindings = []) |
|
| 22 | - { |
|
| 23 | - $this->value = (string)$value; |
|
| 24 | - $this->bindings = (array)$bindings; |
|
| 25 | - } |
|
| 17 | + /** |
|
| 18 | + * @param string $value |
|
| 19 | + * @param mixed|mixed[] $bindings |
|
| 20 | + */ |
|
| 21 | + public function __construct($value, $bindings = []) |
|
| 22 | + { |
|
| 23 | + $this->value = (string)$value; |
|
| 24 | + $this->bindings = (array)$bindings; |
|
| 25 | + } |
|
| 26 | 26 | |
| 27 | - /** |
|
| 28 | - * Returns the current bindings |
|
| 29 | - * |
|
| 30 | - * @return mixed[] |
|
| 31 | - */ |
|
| 32 | - public function getBindings(): array |
|
| 33 | - { |
|
| 34 | - return $this->bindings; |
|
| 35 | - } |
|
| 27 | + /** |
|
| 28 | + * Returns the current bindings |
|
| 29 | + * |
|
| 30 | + * @return mixed[] |
|
| 31 | + */ |
|
| 32 | + public function getBindings(): array |
|
| 33 | + { |
|
| 34 | + return $this->bindings; |
|
| 35 | + } |
|
| 36 | 36 | |
| 37 | - /** |
|
| 38 | - * Returns the current value held. |
|
| 39 | - * |
|
| 40 | - * @return string |
|
| 41 | - */ |
|
| 42 | - public function getValue(): string |
|
| 43 | - { |
|
| 44 | - return (string) $this->value; |
|
| 45 | - } |
|
| 37 | + /** |
|
| 38 | + * Returns the current value held. |
|
| 39 | + * |
|
| 40 | + * @return string |
|
| 41 | + */ |
|
| 42 | + public function getValue(): string |
|
| 43 | + { |
|
| 44 | + return (string) $this->value; |
|
| 45 | + } |
|
| 46 | 46 | |
| 47 | - /** |
|
| 48 | - * @return string |
|
| 49 | - */ |
|
| 50 | - public function __toString() |
|
| 51 | - { |
|
| 52 | - return (string)$this->value; |
|
| 53 | - } |
|
| 47 | + /** |
|
| 48 | + * @return string |
|
| 49 | + */ |
|
| 50 | + public function __toString() |
|
| 51 | + { |
|
| 52 | + return (string)$this->value; |
|
| 53 | + } |
|
| 54 | 54 | } |
@@ -20,8 +20,8 @@ discard block |
||
| 20 | 20 | */ |
| 21 | 21 | public function __construct($value, $bindings = []) |
| 22 | 22 | { |
| 23 | - $this->value = (string)$value; |
|
| 24 | - $this->bindings = (array)$bindings; |
|
| 23 | + $this->value = (string) $value; |
|
| 24 | + $this->bindings = (array) $bindings; |
|
| 25 | 25 | } |
| 26 | 26 | |
| 27 | 27 | /** |
@@ -49,6 +49,6 @@ discard block |
||
| 49 | 49 | */ |
| 50 | 50 | public function __toString() |
| 51 | 51 | { |
| 52 | - return (string)$this->value; |
|
| 52 | + return (string) $this->value; |
|
| 53 | 53 | } |
| 54 | 54 | } |
@@ -4,25 +4,25 @@ |
||
| 4 | 4 | |
| 5 | 5 | class Transaction extends QueryBuilderHandler |
| 6 | 6 | { |
| 7 | - /** |
|
| 8 | - * Commit the database changes |
|
| 9 | - * |
|
| 10 | - * @throws TransactionHaltException |
|
| 11 | - */ |
|
| 12 | - public function commit(): void |
|
| 13 | - { |
|
| 14 | - $this->dbInstance->query('COMMIT'); |
|
| 15 | - throw new TransactionHaltException(); |
|
| 16 | - } |
|
| 7 | + /** |
|
| 8 | + * Commit the database changes |
|
| 9 | + * |
|
| 10 | + * @throws TransactionHaltException |
|
| 11 | + */ |
|
| 12 | + public function commit(): void |
|
| 13 | + { |
|
| 14 | + $this->dbInstance->query('COMMIT'); |
|
| 15 | + throw new TransactionHaltException(); |
|
| 16 | + } |
|
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * Rollback the database changes |
|
| 20 | - * |
|
| 21 | - * @throws TransactionHaltException |
|
| 22 | - */ |
|
| 23 | - public function rollback(): void |
|
| 24 | - { |
|
| 25 | - $this->dbInstance->query('ROLLBACK'); |
|
| 26 | - throw new TransactionHaltException(); |
|
| 27 | - } |
|
| 18 | + /** |
|
| 19 | + * Rollback the database changes |
|
| 20 | + * |
|
| 21 | + * @throws TransactionHaltException |
|
| 22 | + */ |
|
| 23 | + public function rollback(): void |
|
| 24 | + { |
|
| 25 | + $this->dbInstance->query('ROLLBACK'); |
|
| 26 | + throw new TransactionHaltException(); |
|
| 27 | + } |
|
| 28 | 28 | } |