Categories
4 weeks ago
Coupons
4 weeks ago
Customers
2 months ago
Downloads
1 year ago
Export
3 years ago
Import
3 years ago
Orders
4 weeks ago
PerformanceIndicators
9 months ago
Products
4 weeks ago
Revenue
3 months ago
Stock
4 months ago
Taxes
4 weeks ago
Variations
4 weeks ago
Cache.php
4 years ago
Controller.php
11 months ago
DataStore.php
4 weeks ago
DataStoreInterface.php
4 years ago
ExportableInterface.php
4 years ago
ExportableTraits.php
4 years ago
FilteredGetDataTrait.php
1 year ago
GenericController.php
1 year ago
GenericQuery.php
1 year ago
GenericStatsController.php
1 year ago
OrderAwareControllerTrait.php
1 year ago
ParameterException.php
4 years ago
Query.php
1 year ago
Segmenter.php
1 year ago
SqlQuery.php
3 years ago
StatsDataStoreTrait.php
1 year ago
TimeInterval.php
2 years ago
SqlQuery.php
230 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Admin\API\Reports\SqlQuery class file. |
| 4 | */ |
| 5 | |
| 6 | namespace Automattic\WooCommerce\Admin\API\Reports; |
| 7 | |
| 8 | if ( ! defined( 'ABSPATH' ) ) { |
| 9 | exit; |
| 10 | } |
| 11 | |
| 12 | /** |
| 13 | * Admin\API\Reports\SqlQuery: Common parent for manipulating SQL query clauses. |
| 14 | */ |
| 15 | class SqlQuery { |
| 16 | /** |
| 17 | * List of SQL clauses. |
| 18 | * |
| 19 | * @var array |
| 20 | */ |
| 21 | private $sql_clauses = array( |
| 22 | 'select' => array(), |
| 23 | 'from' => array(), |
| 24 | 'left_join' => array(), |
| 25 | 'join' => array(), |
| 26 | 'right_join' => array(), |
| 27 | 'where' => array(), |
| 28 | 'where_time' => array(), |
| 29 | 'group_by' => array(), |
| 30 | 'having' => array(), |
| 31 | 'limit' => array(), |
| 32 | 'order_by' => array(), |
| 33 | 'union' => array(), |
| 34 | ); |
| 35 | /** |
| 36 | * SQL clause merge filters. |
| 37 | * |
| 38 | * @var array |
| 39 | */ |
| 40 | private $sql_filters = array( |
| 41 | 'where' => array( |
| 42 | 'where', |
| 43 | 'where_time', |
| 44 | ), |
| 45 | 'join' => array( |
| 46 | 'right_join', |
| 47 | 'join', |
| 48 | 'left_join', |
| 49 | ), |
| 50 | ); |
| 51 | /** |
| 52 | * Data store context used to pass to filters. |
| 53 | * |
| 54 | * @var string |
| 55 | */ |
| 56 | protected $context; |
| 57 | |
| 58 | /** |
| 59 | * Constructor. |
| 60 | * |
| 61 | * @param string $context Optional context passed to filters. Default empty string. |
| 62 | */ |
| 63 | public function __construct( $context = '' ) { |
| 64 | $this->context = $context; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Add a SQL clause to be included when get_data is called. |
| 69 | * |
| 70 | * @param string $type Clause type. |
| 71 | * @param string $clause SQL clause. |
| 72 | */ |
| 73 | public function add_sql_clause( $type, $clause ) { |
| 74 | if ( isset( $this->sql_clauses[ $type ] ) && ! empty( $clause ) ) { |
| 75 | $this->sql_clauses[ $type ][] = $clause; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Get SQL clause by type. |
| 81 | * |
| 82 | * @param string $type Clause type. |
| 83 | * @param string $handling Whether to filter the return value (filtered|unfiltered). Default unfiltered. |
| 84 | * |
| 85 | * @return string SQL clause. |
| 86 | */ |
| 87 | protected function get_sql_clause( $type, $handling = 'unfiltered' ) { |
| 88 | if ( ! isset( $this->sql_clauses[ $type ] ) ) { |
| 89 | return ''; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Default to bypassing filters for clause retrieval internal to data stores. |
| 94 | * The filters are applied when the full SQL statement is retrieved. |
| 95 | */ |
| 96 | if ( 'unfiltered' === $handling ) { |
| 97 | return implode( ' ', $this->sql_clauses[ $type ] ); |
| 98 | } |
| 99 | |
| 100 | if ( isset( $this->sql_filters[ $type ] ) ) { |
| 101 | $clauses = array(); |
| 102 | foreach ( $this->sql_filters[ $type ] as $subset ) { |
| 103 | $clauses = array_merge( $clauses, $this->sql_clauses[ $subset ] ); |
| 104 | } |
| 105 | } else { |
| 106 | $clauses = $this->sql_clauses[ $type ]; |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Filter SQL clauses by type and context. |
| 111 | * |
| 112 | * @param array $clauses The original arguments for the request. |
| 113 | * @param string $context The data store context. |
| 114 | */ |
| 115 | $clauses = apply_filters( "woocommerce_analytics_clauses_{$type}", $clauses, $this->context ); |
| 116 | /** |
| 117 | * Filter SQL clauses by type and context. |
| 118 | * |
| 119 | * @param array $clauses The original arguments for the request. |
| 120 | */ |
| 121 | $clauses = apply_filters( "woocommerce_analytics_clauses_{$type}_{$this->context}", $clauses ); |
| 122 | return implode( ' ', $clauses ); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Clear SQL clauses by type. |
| 127 | * |
| 128 | * @param string|array $types Clause type. |
| 129 | */ |
| 130 | protected function clear_sql_clause( $types ) { |
| 131 | foreach ( (array) $types as $type ) { |
| 132 | if ( isset( $this->sql_clauses[ $type ] ) ) { |
| 133 | $this->sql_clauses[ $type ] = array(); |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Replace strings within SQL clauses by type. |
| 140 | * |
| 141 | * @param string $type Clause type. |
| 142 | * @param string $search String to search for. |
| 143 | * @param string $replace Replacement string. |
| 144 | */ |
| 145 | protected function str_replace_clause( $type, $search, $replace ) { |
| 146 | if ( isset( $this->sql_clauses[ $type ] ) ) { |
| 147 | foreach ( $this->sql_clauses[ $type ] as $key => $sql ) { |
| 148 | $this->sql_clauses[ $type ][ $key ] = str_replace( $search, $replace, $sql ); |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Get the full SQL statement. |
| 155 | * |
| 156 | * @return string |
| 157 | */ |
| 158 | public function get_query_statement() { |
| 159 | $join = $this->get_sql_clause( 'join', 'filtered' ); |
| 160 | $where = $this->get_sql_clause( 'where', 'filtered' ); |
| 161 | $group_by = $this->get_sql_clause( 'group_by', 'filtered' ); |
| 162 | $having = $this->get_sql_clause( 'having', 'filtered' ); |
| 163 | $order_by = $this->get_sql_clause( 'order_by', 'filtered' ); |
| 164 | $union = $this->get_sql_clause( 'union', 'filtered' ); |
| 165 | |
| 166 | $statement = ''; |
| 167 | |
| 168 | $statement .= " |
| 169 | SELECT |
| 170 | {$this->get_sql_clause( 'select', 'filtered' )} |
| 171 | FROM |
| 172 | {$this->get_sql_clause( 'from', 'filtered' )} |
| 173 | {$join} |
| 174 | WHERE |
| 175 | 1=1 |
| 176 | {$where} |
| 177 | "; |
| 178 | |
| 179 | if ( ! empty( $group_by ) ) { |
| 180 | $statement .= " |
| 181 | GROUP BY |
| 182 | {$group_by} |
| 183 | "; |
| 184 | if ( ! empty( $having ) ) { |
| 185 | $statement .= " |
| 186 | HAVING |
| 187 | 1=1 |
| 188 | {$having} |
| 189 | "; |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | if ( ! empty( $union ) ) { |
| 194 | $statement .= " |
| 195 | UNION |
| 196 | {$union} |
| 197 | "; |
| 198 | } |
| 199 | |
| 200 | if ( ! empty( $order_by ) ) { |
| 201 | $statement .= " |
| 202 | ORDER BY |
| 203 | {$order_by} |
| 204 | "; |
| 205 | } |
| 206 | |
| 207 | return $statement . $this->get_sql_clause( 'limit', 'filtered' ); |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * Reinitialize the clause array. |
| 212 | */ |
| 213 | public function clear_all_clauses() { |
| 214 | $this->sql_clauses = array( |
| 215 | 'select' => array(), |
| 216 | 'from' => array(), |
| 217 | 'left_join' => array(), |
| 218 | 'join' => array(), |
| 219 | 'right_join' => array(), |
| 220 | 'where' => array(), |
| 221 | 'where_time' => array(), |
| 222 | 'group_by' => array(), |
| 223 | 'having' => array(), |
| 224 | 'limit' => array(), |
| 225 | 'order_by' => array(), |
| 226 | 'union' => array(), |
| 227 | ); |
| 228 | } |
| 229 | } |
| 230 |