PluginProbe
WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards / 5.5.40
WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards v5.5.40
5.5.84 5.5.83 5.5.82 5.5.81 5.5.80 5.5.79 5.5.77 5.5.76 5.5.75 5.5.73 5.5.72 5.5.22 5.5.23 5.5.29 5.5.3 5.5.31 5.5.32 5.5.34 5.5.35 5.5.36 5.5.37 5.5.4 5.5.40 5.5.41 5.5.42 All 160 releases
wp-data-access / WPDataAccess / Utilities / WPDA_Reverse_Engineering.php

WPDA_Reverse_Engineering.php in WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards 5.5.40, at WPDataAccess/Utilities/WPDA_Reverse_Engineering.php

271 lines 7.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Suppress "error - 0 - No summary was found for this file" on phpdoc generation
5 *
6 * @package WPDataAccess\Utilities
7 */
8
9 namespace WPDataAccess\Utilities {
10
11 use WPDataAccess\Connection\WPDADB;
12 use WPDataAccess\WPDA;
13
14 /**
15 * Class WPDA_Reverse_Engineering
16 *
17 * @author Peter Schulz
18 * @since 2.0.13
19 */
20 class WPDA_Reverse_Engineering {
21
22 /**
23 * Database connection
24 *
25 * @var null
26 */
27 protected $wpdadb = null;
28
29 /**
30 * Database schema name
31 *
32 * @var string
33 */
34 protected $schema_name;
35
36 /**
37 * Database table name
38 *
39 * @var string
40 */
41 protected $table_name;
42
43 /**
44 * WPDA_Reverse_Engineering constructor
45 *
46 * @param string $table_name Database table name
47 * @param string $schema_name Database schema name
48 *
49 * @since 2.0.13
50 */
51 public function __construct( $table_name, $schema_name ) {
52 $this->schema_name = $schema_name;
53 $this->table_name = $table_name;
54
55 $this->wpdadb = WPDADB::get_db_connection( $this->schema_name );
56 if ( null === $this->wpdadb ) {
57 wp_die( sprintf( __( 'ERROR - Remote database %s not available', 'wp-data-access' ), esc_attr( $this->schema_name ) ) );
58 }
59 }
60
61 /**
62 * Get table info in Data Designer format
63 *
64 * @param string $design_mode Possible values are: Basic or Advanced
65 *
66 * @return array Database table in Data Designer format
67 *
68 * @since 2.0.13
69 */
70 public function get_designer_format( $design_mode ) {
71 $table_structure = array();
72
73 $tab = $this->get_table_info();
74 $rows = $this->get_table_columns();
75 $idxs = $this->get_table_indexes();
76
77 $idx_current = '';
78 $idx_current_unique = '';
79 $idx_current_columns = '';
80 $idx_array = array();
81 $fulltext_support = get_option( 'wpda_fulltext_support' );
82 foreach ( $idxs as $idx ) {
83 if ( $idx['index_name'] !== $idx_current ) {
84 if ( $idx_current_columns !== '' ) {
85 $idx_array[] = array(
86 'index_name' => $idx_current,
87 'unique' => $idx_current_unique,
88 'column_names' => rtrim( $idx_current_columns, ',' ),
89 );
90 }
91 $idx_current = $idx['index_name'];
92 }
93 if ( 'on' === $fulltext_support ) {
94 if ( 'FULLTEXT' === $idx['index_type'] ) {
95 $idx_current_unique = 'FULLTEXT';
96 } else {
97 $idx_current_unique = $idx['non_unique'] === '1' ? 'No' : 'Yes';
98 }
99 } else {
100 $idx_current_unique = $idx['non_unique'] === '1' ? 'No' : 'Yes';
101 }
102 $idx_current_columns = $idx['column_name'] . ',';
103 }
104 if ( $idx_current !== '' ) {
105 $idx_array[] = array(
106 'index_name' => $idx_current,
107 'unique' => $idx_current_unique,
108 'column_names' => rtrim( $idx_current_columns, ',' ),
109 );
110 }
111
112 foreach ( $rows as $row ) {
113 $obj = (object) null;
114 $obj->column_name = $row['column_name'];
115
116 $obj->data_type = $row['data_type'];
117 if ( stripos( $row['column_type'], 'unsigned' ) !== false ) {
118 $obj->type_attribute = 'unsigned';
119 } elseif ( stripos( $row['column_type'], 'unsigned zerofill' ) !== false ) {
120 $obj->type_attribute = 'unsigned zerofill';
121 } else {
122 $obj->type_attribute = '';
123 }
124
125 $obj->key = 'PRI' === $row['column_key'] ? 'Yes' : 'No';
126 $obj->mandatory = 'YES' === $row['is_nullable'] ? 'No' : 'Yes';
127
128 $obj->max_length = '';
129 $max_length_start = strpos( $row['column_type'], '(' );
130 if (
131 false !== $max_length_start &&
132 'enum' !== $row['data_type'] &&
133 'set' !== $row['data_type']
134 ) {
135 // Get max length from column_type
136 $max_length_end = strpos( $row['column_type'], ')' );
137 if ( false != $max_length_end ) {
138 $obj->max_length = substr( $row['column_type'], $max_length_start + 1, $max_length_end - $max_length_start - 1 );
139 } elseif ( null !== $row['character_maximum_length'] ) {
140 $obj->max_length = $row['character_maximum_length'];
141 } elseif ( null !== $row['numeric_precision'] ) {
142 $obj->max_length = $row['numeric_precision'];
143 if ( null !== $row['numeric_scale'] ) {
144 $obj->max_length .= ",{$row['numeric_scale']}";
145 }
146 }
147 }
148
149 $obj->extra = $row['extra'];
150 if ( '' !== $row['column_default'] && null !== $row['column_default'] ) {
151 $simple_data_type = WPDA::get_type( $obj->data_type );
152 if ( 'number' === $simple_data_type || 'date' === $simple_data_type ) {
153 $obj->default = $row['column_default'];
154 } else {
155 $obj->default = "'{$row['column_default']}'";
156 }
157 } else {
158 $obj->default = '';
159 }
160
161 $obj->list = '';
162 if ( 'enum' === $row['data_type'] ) {
163 $obj->list = substr( substr( $row['column_type'], 5 ), 0, - 1 );
164 } elseif ( 'set' === $row['data_type'] ) {
165 $obj->list = substr( substr( $row['column_type'], 4 ), 0, - 1 );
166 }
167
168 $table_structure[] = $obj;
169 }
170
171 return array(
172 'design_mode' => $design_mode,
173 'engine' => $tab[0]['engine'],
174 'collation' => $tab[0]['table_collation'],
175 'table' => $table_structure,
176 'indexes' => $idx_array,
177 );
178 }
179
180 /**
181 * Get database table info
182 *
183 * @return array Table info
184 *
185 * @since 2.0.13
186 */
187 protected function get_table_info() {
188 $query = $this->wpdadb->prepare(
189 '
190 SELECT engine AS engine,
191 table_collation AS table_collation
192 FROM information_schema.tables
193 WHERE table_schema = %s
194 AND table_name = %s
195 ',
196 array(
197 $this->wpdadb->dbname,
198 $this->table_name,
199 )
200 );
201
202 return $this->wpdadb->get_results( $query, 'ARRAY_A' ); // phpcs:ignore Standard.Category.SniffName.ErrorCode
203 }
204
205 /**
206 * Get database column info
207 *
208 * @return array Table column info
209 *
210 * @since 2.0.13
211 */
212 protected function get_table_columns() {
213 $query = $this->wpdadb->prepare(
214 '
215 SELECT column_name AS column_name,
216 data_type AS data_type,
217 column_type AS column_type,
218 is_nullable AS is_nullable,
219 column_default AS column_default,
220 column_key AS column_key,
221 extra AS extra,
222 character_maximum_length AS character_maximum_length,
223 numeric_precision AS numeric_precision,
224 numeric_scale AS numeric_scale
225 FROM information_schema.columns
226 WHERE table_schema = %s
227 AND table_name = %s
228 ORDER BY ordinal_position
229 ',
230 array(
231 $this->wpdadb->dbname,
232 $this->table_name,
233 )
234 );
235
236 return $this->wpdadb->get_results( $query, 'ARRAY_A' ); // phpcs:ignore Standard.Category.SniffName.ErrorCode
237 }
238
239 /**
240 * Get database index info
241 *
242 * @return array Index info
243 *
244 * @since 2.0.13
245 */
246 protected function get_table_indexes() {
247 $query = $this->wpdadb->prepare(
248 "
249 SELECT index_name AS index_name,
250 column_name AS column_name,
251 non_unique AS non_unique,
252 index_type AS index_type
253 FROM information_schema.statistics
254 WHERE table_schema = %s
255 AND table_name = %s
256 AND index_name != 'PRIMARY'
257 ORDER BY index_name, column_name, seq_in_index
258 ",
259 array(
260 $this->wpdadb->dbname,
261 $this->table_name,
262 )
263 );
264
265 return $this->wpdadb->get_results( $query, 'ARRAY_A' ); // phpcs:ignore Standard.Category.SniffName.ErrorCode
266 }
267
268 }
269
270 }
271