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.1.0 6.2.1 3.1.1 6.2.2 3.1.2 6.2.3 3.2.0 6.3.0 3.2.1 6.3.0-beta.1 3.2.2 6.3.0-rc.1 3.2.3 6.3.0-rc.2 3.2.4 6.3.1 3.2.5 6.3.2 3.2.6 6.4.0 3.3.0 6.4.0-beta.1 3.3.1 6.4.0-rc.1 3.3.2 6.4.1 3.3.2-rc.1 6.4.2 3.3.3 6.5.0 3.3.4 6.5.0-beta.1 3.3.5 6.5.0-rc.1 3.3.6 6.5.0-rc.2 3.4.0 6.5.1 3.4.0-beta.1 6.5.2 3.4.0-rc.2 6.6.0 3.4.1 6.6.0-beta.1 3.4.2 6.6.0-rc.1 3.4.3 6.6.0-rc.2 3.4.4 6.6.1 3.4.5 6.6.2 3.4.6 6.7.0 3.4.7 6.7.0-beta.1 3.4.8 6.7.0-beta.2 3.5.0 6.7.0-rc.1 3.5.0-beta.1 6.7.1 3.5.0-rc.1 6.8.0 3.5.0-rc.2 6.8.0-beta.1 3.5.1 6.8.0-beta.2 3.5.10 6.8.0-rc.1 3.5.2 6.8.1 3.5.3 6.8.2 3.5.4 6.8.3 3.5.5 6.9.0 3.5.6 6.9.0-beta.1 3.5.7 6.9.0-beta.2 3.5.8 6.9.0-rc.1 3.5.9 6.9.1 3.6.0 6.9.2 3.6.0-beta.1 6.9.3 3.6.0-rc.1 6.9.4 3.6.0-rc.2 6.9.5 3.6.0-rc.3 7.0.0 3.6.1 7.0.0-beta.1 3.6.2 7.0.0-beta.2 3.6.3 7.0.0-beta.3 3.6.4 7.0.0-rc.1 3.6.5 7.0.0-rc.2 3.6.6 7.0.1 3.6.7 7.0.2 3.7.0 7.1.0 3.7.0-beta.1 7.1.0-beta.1 3.7.0-rc.1 7.1.0-beta.2 3.7.0-rc.2 7.1.0-rc.1 3.7.1 7.1.0-rc.2 3.7.2 7.1.1
woocommerce / src / Admin / API / Reports / SqlQuery.php
woocommerce / src / Admin / API / Reports Last commit date
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