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

195 lines 5.3 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 // impose a limit if no limit clause is provided.
63 if ( strpos( strtolower( $this->_query ), ' limit ' ) === false ) {
64 $this->_query .= ' LIMIT ' . apply_filters( 'visualizer_sql_query_limit', 1000 );
65 }
66
67 global $wpdb;
68 $wpdb->hide_errors();
69 // @codingStandardsIgnoreStart
70 $rows = $wpdb->get_results( $this->_query, $results_as_numeric_array ? ARRAY_N : ARRAY_A );
71 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__ );
72 // @codingStandardsIgnoreEnd
73 $wpdb->show_errors();
74
75 if ( $raw_results ) {
76 return $rows;
77 }
78
79 if ( $rows ) {
80 $results = array();
81 $headers = array();
82 if ( $rows ) {
83 $row_num = 0;
84 foreach ( $rows as $row ) {
85 $result = array();
86 $col_num = 0;
87 foreach ( $row as $k => $v ) {
88 $result[] = $v;
89 if ( 0 === $row_num ) {
90 $headers[] = array( 'type' => $this->get_col_type( $col_num++ ), 'label' => $k );
91 }
92 }
93 $results[] = $result;
94 $row_num++;
95 }
96 }
97
98 if ( $as_html ) {
99 return $this->html( $headers, $results );
100 }
101 return $this->object( $headers, $results );
102 }
103
104 $this->_error = $wpdb->last_error;
105 return null;
106 }
107
108 /**
109 * Get the data type of the column.
110 *
111 * @param int $col_num The column index in the fetched result set.
112 * @access private
113 * @return int
114 */
115 private function get_col_type( $col_num ) {
116 global $wpdb;
117 switch ( $wpdb->get_col_info( 'type', $col_num ) ) {
118 case 0:
119 case 5:
120 case 4:
121 case 9:
122 case 3:
123 case 2:
124 case 246:
125 case 8:
126 // numeric.
127 return 'number';
128 case 10:
129 case 12:
130 case 14:
131 // date.
132 return 'date';
133 }
134 return 'string';
135 }
136
137 /**
138 * Returns the HTML output.
139 *
140 * @param array $headers The headers of the result set.
141 * @param array $results The data of the result set.
142 * @access private
143 * @return string
144 */
145 private function html( $headers, $results ) {
146 return Visualizer_Render_Layout::show( 'db-wizard-results', $headers, $results );
147 }
148
149 /**
150 * Sets the series and data.
151 *
152 * @param array $headers The headers of the result set.
153 * @param array $results The data of the result set.
154 * @access private
155 * @return bool
156 */
157 private function object( $headers, $results ) {
158 $series = array();
159 foreach ( $headers as $header ) {
160 $series[] = $header;
161 }
162 $this->_series = $series;
163
164 $data = array();
165 foreach ( $results as $row ) {
166 $data[] = $this->_normalizeData( $row );
167 }
168 $this->_data = $data;
169
170 return true;
171 }
172
173 /**
174 * Returns the final query.
175 *
176 * @access public
177 * @return string
178 */
179 public function get_query() {
180 return $this->_query;
181 }
182
183 /**
184 * Returns source name.
185 *
186 * @since 1.0.0
187 *
188 * @access public
189 * @return string The name of source.
190 */
191 public function getSourceName() {
192 return __CLASS__;
193 }
194 }
195