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