PluginProbe
WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards / 5.5.82
WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards v5.5.82
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.82, at WPDataAccess/Utilities/WPDA_Reverse_Engineering.php

272 lines 7.0 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 /* translators: %s = database name */
58 wp_die( sprintf( esc_attr__( 'ERROR - Remote database %s not available', 'wp-data-access' ), esc_attr( $this->schema_name ) ) );
59 }
60 }
61
62 /**
63 * Get table info in Data Designer format
64 *
65 * @param string $design_mode Possible values are: Basic or Advanced
66 *
67 * @return array Database table in Data Designer format
68 *
69 * @since 2.0.13
70 */
71 public function get_designer_format( $design_mode ) {
72 $table_structure = array();
73
74 $tab = $this->get_table_info();
75 $rows = $this->get_table_columns();
76 $idxs = $this->get_table_indexes();
77
78 $idx_current = '';
79 $idx_current_unique = '';
80 $idx_current_columns = '';
81 $idx_array = array();
82 $fulltext_support = get_option( 'wpda_fulltext_support' );
83 foreach ( $idxs as $idx ) {
84 if ( $idx['index_name'] !== $idx_current ) {
85 if ( $idx_current_columns !== '' ) {
86 $idx_array[] = array(
87 'index_name' => $idx_current,
88 'unique' => $idx_current_unique,
89 'column_names' => rtrim( $idx_current_columns, ',' ),
90 );
91 }
92 $idx_current = $idx['index_name'];
93 }
94 if ( 'on' === $fulltext_support ) {
95 if ( 'FULLTEXT' === $idx['index_type'] ) {
96 $idx_current_unique = 'FULLTEXT';
97 } else {
98 $idx_current_unique = $idx['non_unique'] === '1' ? 'No' : 'Yes';
99 }
100 } else {
101 $idx_current_unique = $idx['non_unique'] === '1' ? 'No' : 'Yes';
102 }
103 $idx_current_columns = $idx['column_name'] . ',';
104 }
105 if ( $idx_current !== '' ) {
106 $idx_array[] = array(
107 'index_name' => $idx_current,
108 'unique' => $idx_current_unique,
109 'column_names' => rtrim( $idx_current_columns, ',' ),
110 );
111 }
112
113 foreach ( $rows as $row ) {
114 $obj = (object) null;
115 $obj->column_name = $row['column_name'];
116
117 $obj->data_type = $row['data_type'];
118 if ( stripos( $row['column_type'], 'unsigned' ) !== false ) {
119 $obj->type_attribute = 'unsigned';
120 } elseif ( stripos( $row['column_type'], 'unsigned zerofill' ) !== false ) {
121 $obj->type_attribute = 'unsigned zerofill';
122 } else {
123 $obj->type_attribute = '';
124 }
125
126 $obj->key = 'PRI' === $row['column_key'] ? 'Yes' : 'No';
127 $obj->mandatory = 'YES' === $row['is_nullable'] ? 'No' : 'Yes';
128
129 $obj->max_length = '';
130 $max_length_start = strpos( $row['column_type'], '(' );
131 if (
132 false !== $max_length_start &&
133 'enum' !== $row['data_type'] &&
134 'set' !== $row['data_type']
135 ) {
136 // Get max length from column_type
137 $max_length_end = strpos( $row['column_type'], ')' );
138 if ( false != $max_length_end ) {
139 $obj->max_length = substr( $row['column_type'], $max_length_start + 1, $max_length_end - $max_length_start - 1 );
140 } elseif ( null !== $row['character_maximum_length'] ) {
141 $obj->max_length = $row['character_maximum_length'];
142 } elseif ( null !== $row['numeric_precision'] ) {
143 $obj->max_length = $row['numeric_precision'];
144 if ( null !== $row['numeric_scale'] ) {
145 $obj->max_length .= ",{$row['numeric_scale']}";
146 }
147 }
148 }
149
150 $obj->extra = $row['extra'];
151 if ( '' !== $row['column_default'] && null !== $row['column_default'] ) {
152 $simple_data_type = WPDA::get_type( $obj->data_type );
153 if ( 'number' === $simple_data_type || 'date' === $simple_data_type ) {
154 $obj->default = $row['column_default'];
155 } else {
156 $obj->default = "'{$row['column_default']}'";
157 }
158 } else {
159 $obj->default = '';
160 }
161
162 $obj->list = '';
163 if ( 'enum' === $row['data_type'] ) {
164 $obj->list = substr( substr( $row['column_type'], 5 ), 0, - 1 );
165 } elseif ( 'set' === $row['data_type'] ) {
166 $obj->list = substr( substr( $row['column_type'], 4 ), 0, - 1 );
167 }
168
169 $table_structure[] = $obj;
170 }
171
172 return array(
173 'design_mode' => $design_mode,
174 'engine' => $tab[0]['engine'],
175 'collation' => $tab[0]['table_collation'],
176 'table' => $table_structure,
177 'indexes' => $idx_array,
178 );
179 }
180
181 /**
182 * Get database table info
183 *
184 * @return array Table info
185 *
186 * @since 2.0.13
187 */
188 protected function get_table_info() {
189 $query = $this->wpdadb->prepare(
190 '
191 SELECT engine AS engine,
192 table_collation AS table_collation
193 FROM information_schema.tables
194 WHERE table_schema = %s
195 AND table_name = %s
196 ',
197 array(
198 $this->wpdadb->dbname,
199 $this->table_name,
200 )
201 );
202
203 return $this->wpdadb->get_results( $query, 'ARRAY_A' );
204 }
205
206 /**
207 * Get database column info
208 *
209 * @return array Table column info
210 *
211 * @since 2.0.13
212 */
213 protected function get_table_columns() {
214 $query = $this->wpdadb->prepare(
215 '
216 SELECT column_name AS column_name,
217 data_type AS data_type,
218 column_type AS column_type,
219 is_nullable AS is_nullable,
220 column_default AS column_default,
221 column_key AS column_key,
222 extra AS extra,
223 character_maximum_length AS character_maximum_length,
224 numeric_precision AS numeric_precision,
225 numeric_scale AS numeric_scale
226 FROM information_schema.columns
227 WHERE table_schema = %s
228 AND table_name = %s
229 ORDER BY ordinal_position
230 ',
231 array(
232 $this->wpdadb->dbname,
233 $this->table_name,
234 )
235 );
236
237 return $this->wpdadb->get_results( $query, 'ARRAY_A' );
238 }
239
240 /**
241 * Get database index info
242 *
243 * @return array Index info
244 *
245 * @since 2.0.13
246 */
247 protected function get_table_indexes() {
248 $query = $this->wpdadb->prepare(
249 "
250 SELECT index_name AS index_name,
251 column_name AS column_name,
252 non_unique AS non_unique,
253 index_type AS index_type
254 FROM information_schema.statistics
255 WHERE table_schema = %s
256 AND table_name = %s
257 AND index_name != 'PRIMARY'
258 ORDER BY index_name, column_name, seq_in_index
259 ",
260 array(
261 $this->wpdadb->dbname,
262 $this->table_name,
263 )
264 );
265
266 return $this->wpdadb->get_results( $query, 'ARRAY_A' );
267 }
268
269 }
270
271 }
272