PluginProbe
Visualizer – Tables & Charts Manager with Built-in AI Generator / 3.1.3
Visualizer – Tables & Charts Manager with Built-in AI Generator v3.1.3
4.0.7 4.0.6 4.0.5 4.0.4 4.0.3 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.1 3.1.2 3.1.3 3.10.0 3.10.1 3.10.10 3.10.11 3.10.12 3.10.13 3.10.14 3.10.15 3.10.2 3.10.3 3.10.4 All 148 releases
visualizer / classes / Visualizer / Source / Query.php

Query.php in Visualizer – Tables & Charts Manager with Built-in AI Generator 3.1.3, at classes/Visualizer/Source/Query.php

213 lines 5.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 // +----------------------------------------------------------------------+
4 // | Copyright 2013 Madpixels (email : visualizer@madpixels.net) |
5 // +----------------------------------------------------------------------+
6 // | This program is free software; you can redistribute it and/or modify |
7 // | it under the terms of the GNU General Public License, version 2, as |
8 // | published by the Free Software Foundation. |
9 // | |
10 // | This program is distributed in the hope that it will be useful, |
11 // | but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 // | GNU General Public License for more details. |
14 // | |
15 // | You should have received a copy of the GNU General Public License |
16 // | along with this program; if not, write to the Free Software |
17 // | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, |
18 // | MA 02110-1301 USA |
19 // +----------------------------------------------------------------------+
20 // | Author: Eugene Manuilov <eugene@manuilov.org> |
21 // +----------------------------------------------------------------------+
22 /**
23 * Source manager for query builder.
24 *
25 * @category Visualizer
26 * @package Source
27 */
28 class Visualizer_Source_Query extends Visualizer_Source {
29
30 /**
31 * The query.
32 *
33 * @access protected
34 * @var string
35 */
36 protected $_query;
37
38 /**
39 * The error message.
40 *
41 * @access protected
42 * @var string
43 */
44 protected $_error;
45
46 /**
47 * Constructor.
48 *
49 * @access public
50 * @param string $query The query.
51 */
52 public function __construct( $query = null ) {
53 $this->_query = $query;
54 }
55
56 /**
57 * Fetches information from source, parses it and builds series and data arrays.
58 *
59 * @param bool $as_html Should the result be fetched as an HTML table or as an object.
60 * @param bool $results_as_numeric_array Should the result be fetched as ARRAY_N instead of ARRAY_A.
61 * @param bool $raw_results Should the result be returned without processing.
62 * @access public
63 * @return boolean TRUE on success, otherwise FALSE.
64 */
65 public function fetch( $as_html = false, $results_as_numeric_array = false, $raw_results = false ) {
66 if ( empty( $this->_query ) ) {
67 return false;
68 }
69
70 // impose a limit if no limit clause is provided.
71 if ( strpos( strtolower( $this->_query ), ' limit ' ) === false ) {
72 $this->_query .= ' LIMIT ' . apply_filters( 'visualizer_sql_query_limit', 1000 );
73 }
74
75 global $wpdb;
76 $wpdb->hide_errors();
77 // @codingStandardsIgnoreStart
78 $rows = $wpdb->get_results( $this->_query, $results_as_numeric_array ? ARRAY_N : ARRAY_A );
79 do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'Firing query %s to get results %s with error %s', $this->_query, print_r( $rows, true ), print_r( $wpdb->last_error, true ) ), 'debug', __FILE__, __LINE__ );
80 // @codingStandardsIgnoreEnd
81 $wpdb->show_errors();
82
83 if ( $raw_results ) {
84 return $rows;
85 }
86
87 if ( $rows ) {
88 $results = array();
89 $headers = array();
90 if ( $rows ) {
91 $row_num = 0;
92 foreach ( $rows as $row ) {
93 $result = array();
94 $col_num = 0;
95 foreach ( $row as $k => $v ) {
96 $result[] = $v;
97 if ( 0 === $row_num ) {
98 $headers[] = array( 'type' => $this->get_col_type( $col_num++ ), 'label' => $k );
99 }
100 }
101 $results[] = $result;
102 $row_num++;
103 }
104 }
105
106 if ( $as_html ) {
107 return $this->html( $headers, $results );
108 }
109 return $this->object( $headers, $results );
110 }
111
112 $this->_error = $wpdb->last_error;
113 return null;
114 }
115
116 /**
117 * Get the data type of the column.
118 *
119 * @param int $col_num The column index in the fetched result set.
120 * @access private
121 * @return int
122 */
123 private function get_col_type( $col_num ) {
124 global $wpdb;
125 switch ( $wpdb->get_col_info( 'type', $col_num ) ) {
126 case 0:
127 case 5:
128 case 4:
129 case 9:
130 case 3:
131 case 2:
132 case 246:
133 case 8:
134 // numeric.
135 return 'number';
136 case 10:
137 case 12:
138 case 14:
139 // date.
140 return 'date';
141 }
142 return 'string';
143 }
144
145 /**
146 * Returns the HTML output.
147 *
148 * @param array $headers The headers of the result set.
149 * @param array $results The data of the result set.
150 * @access private
151 * @return string
152 */
153 private function html( $headers, $results ) {
154 return Visualizer_Render_Layout::show( 'db-wizard-results', $headers, $results );
155 }
156
157 /**
158 * Sets the series and data.
159 *
160 * @param array $headers The headers of the result set.
161 * @param array $results The data of the result set.
162 * @access private
163 * @return bool
164 */
165 private function object( $headers, $results ) {
166 $series = array();
167 foreach ( $headers as $header ) {
168 $series[] = $header;
169 }
170 $this->_series = $series;
171
172 $data = array();
173 foreach ( $results as $row ) {
174 $data[] = $this->_normalizeData( $row );
175 }
176 $this->_data = $data;
177
178 return true;
179 }
180
181 /**
182 * Returns the error, if any.
183 *
184 * @access public
185 * @return string
186 */
187 public function get_error() {
188 return $this->_error;
189 }
190
191 /**
192 * Returns the final query.
193 *
194 * @access public
195 * @return string
196 */
197 public function get_query() {
198 return $this->_query;
199 }
200
201 /**
202 * Returns source name.
203 *
204 * @since 1.0.0
205 *
206 * @access public
207 * @return string The name of source.
208 */
209 public function getSourceName() {
210 return __CLASS__;
211 }
212 }
213