PluginProbe
Visualizer – Tables & Charts Manager with Built-in AI Generator / 3.3.2
Visualizer – Tables & Charts Manager with Built-in AI Generator v3.3.2
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.3.2, at classes/Visualizer/Source/Query.php

201 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 * Constructor.
40 *
41 * @access public
42 * @param string $query The query.
43 */
44 public function __construct( $query = null ) {
45 $this->_query = $query;
46 }
47
48 /**
49 * Fetches information from source, parses it and builds series and data arrays.
50 *
51 * @param bool $as_html Should the result be fetched as an HTML table or as an object.
52 * @param bool $results_as_numeric_array Should the result be fetched as ARRAY_N instead of ARRAY_A.
53 * @param bool $raw_results Should the result be returned without processing.
54 * @access public
55 * @return boolean TRUE on success, otherwise FALSE.
56 */
57 public function fetch( $as_html = false, $results_as_numeric_array = false, $raw_results = false ) {
58 if ( empty( $this->_query ) ) {
59 return false;
60 }
61
62 // only select queries allowed.
63 if ( preg_match( '/^\s*(insert|delete|update|replace|create|alter|drop|truncate)\s/i', $this->_query ) ) {
64 $this->_error = __( 'Only SELECT queries are allowed', 'visualizer' );
65 return false;
66 }
67
68 // impose a limit if no limit clause is provided.
69 if ( strpos( strtolower( $this->_query ), ' limit ' ) === false ) {
70 $this->_query .= ' LIMIT ' . apply_filters( 'visualizer_sql_query_limit', 1000 );
71 }
72
73 global $wpdb;
74 $wpdb->hide_errors();
75 // @codingStandardsIgnoreStart
76 $rows = $wpdb->get_results( $this->_query, $results_as_numeric_array ? ARRAY_N : ARRAY_A );
77 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__ );
78 // @codingStandardsIgnoreEnd
79 $wpdb->show_errors();
80
81 if ( $raw_results ) {
82 return $rows;
83 }
84
85 if ( $rows ) {
86 $results = array();
87 $headers = array();
88 if ( $rows ) {
89 $row_num = 0;
90 foreach ( $rows as $row ) {
91 $result = array();
92 $col_num = 0;
93 foreach ( $row as $k => $v ) {
94 $result[] = $v;
95 if ( 0 === $row_num ) {
96 $headers[] = array( 'type' => $this->get_col_type( $col_num++ ), 'label' => $k );
97 }
98 }
99 $results[] = $result;
100 $row_num++;
101 }
102 }
103
104 if ( $as_html ) {
105 return $this->html( $headers, $results );
106 }
107 return $this->object( $headers, $results );
108 }
109
110 $this->_error = $wpdb->last_error;
111 return null;
112 }
113
114 /**
115 * Get the data type of the column.
116 *
117 * @param int $col_num The column index in the fetched result set.
118 * @access private
119 * @return int
120 */
121 private function get_col_type( $col_num ) {
122 global $wpdb;
123 switch ( $wpdb->get_col_info( 'type', $col_num ) ) {
124 case 0:
125 case 5:
126 case 4:
127 case 9:
128 case 3:
129 case 2:
130 case 246:
131 case 8:
132 // numeric.
133 return 'number';
134 case 10:
135 case 12:
136 case 14:
137 // date.
138 return 'date';
139 }
140 return 'string';
141 }
142
143 /**
144 * Returns the HTML output.
145 *
146 * @param array $headers The headers of the result set.
147 * @param array $results The data of the result set.
148 * @access private
149 * @return string
150 */
151 private function html( $headers, $results ) {
152 return Visualizer_Render_Layout::show( 'db-wizard-results', $headers, $results );
153 }
154
155 /**
156 * Sets the series and data.
157 *
158 * @param array $headers The headers of the result set.
159 * @param array $results The data of the result set.
160 * @access private
161 * @return bool
162 */
163 private function object( $headers, $results ) {
164 $series = array();
165 foreach ( $headers as $header ) {
166 $series[] = $header;
167 }
168 $this->_series = $series;
169
170 $data = array();
171 foreach ( $results as $row ) {
172 $data[] = $this->_normalizeData( $row );
173 }
174 $this->_data = $data;
175
176 return true;
177 }
178
179 /**
180 * Returns the final query.
181 *
182 * @access public
183 * @return string
184 */
185 public function get_query() {
186 return $this->_query;
187 }
188
189 /**
190 * Returns source name.
191 *
192 * @since 1.0.0
193 *
194 * @access public
195 * @return string The name of source.
196 */
197 public function getSourceName() {
198 return __CLASS__;
199 }
200 }
201