Completed
Branch BUG-9951-10331-8793-pue-fixes (9f33f1)
by
unknown
26:00 queued 14:50
created

TableAnalysis::showIndexes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 2
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
namespace EventEspresso\core\services\database;
3
4
/**
5
 *
6
 * Class TableAnalysis
7
 *
8
 * For analyzing database tables; should not perform any manipulation, or have
9
 * any EE business logic
10
 *
11
 * @package         Event Espresso
12
 * @subpackage
13
 * @author				Mike Nelson
14
 * @since		 	   $VID:$
15
 *
16
 */
17
class TableAnalysis extends \EE_Base {
18
19
    /**
20
     * The maximum number of characters that can be indexed on a column using utf8mb4 collation,
21
     * see https://events.codebasehq.com/redirect?https://make.wordpress.org/core/2015/04/02/the-utf8mb4-upgrade/
22
     */
23
    const INDEX_COLUMN_SIZE = 191;
24
	/**
25
	 * Returns the table name which will definitely have the wpdb prefix on the front,
26
	 * except if it currently has the wpdb->base_prefix on the front, in which case
27
	 * it will have the wpdb->base_prefix on it
28
	 *
29
	 * @global \wpdb $wpdb
30
	 * @param string $table_name
31
	 * @return string $tableName, having ensured it has the wpdb prefix on the front
32
	 */
33
	public function ensureTableNameHasPrefix( $table_name )
34
	{
35
		global $wpdb;
36
		return strpos( $table_name, $wpdb->base_prefix ) === 0 ? $table_name : $wpdb->prefix . $table_name;
37
	}
38
39
40
41
	/**
42
	 * Indicates whether or not the table has any entries. $table_name can
43
	 * optionally start with $wpdb->prefix or not
44
	 * @global \wpdb $wpdb
45
	 * @param string $table_name
46
	 * @return bool
47
	 */
48
	public function tableIsEmpty( $table_name )
49
	{
50
		global $wpdb;
51
		$table_name = $this->ensureTableNameHasPrefix( $table_name );
52
		if ( $this->tableExists( $table_name ) ) {
53
			$count = $wpdb->get_var( "SELECT COUNT(*) FROM $table_name" );
54
			return absint( $count ) === 0 ? true : false;
55
		}
56
		return false;
57
	}
58
59
60
61
	/**
62
	 * Indicates whether or not the table exists. $table_name can optionally
63
	 * have the $wpdb->prefix on the beginning, or not.
64
	 * @global \wpdb $wpdb
65
	 * @global array EZSQL_Error
66
	 * @param $table_name
67
	 * @return bool
68
	 */
69
	public function tableExists( $table_name )
70
	{
71
		global $wpdb, $EZSQL_ERROR;
72
		$table_name = $this->ensureTableNameHasPrefix( $table_name );
73
		//ignore if this causes an sql error
74
		$old_error = $wpdb->last_error;
75
		$old_suppress_errors = $wpdb->suppress_errors();
76
		$old_show_errors_value = $wpdb->show_errors( FALSE );
77
		$ezsql_error_cache = $EZSQL_ERROR;
78
		$wpdb->get_results( "SELECT * from $table_name LIMIT 1");
79
		$wpdb->show_errors( $old_show_errors_value );
80
		$wpdb->suppress_errors( $old_suppress_errors );
81
		$new_error = $wpdb->last_error;
82
		$wpdb->last_error = $old_error;
83
		$EZSQL_ERROR = $ezsql_error_cache;
84
		//if there was a table doesn't exist error
85
		if( ! empty( $new_error ) ) {
86
			if(
87
				in_array(
88
					\EEH_Activation::last_wpdb_error_code(),
89
					array(
90
						1051, //bad table
91
						1109, //unknown table
92
						117, //no such table
93
					)
94
				)
95
				||
96
				preg_match( '~^Table .* doesn\'t exist~', $new_error ) //in case not using mysql and error codes aren't reliable, just check for this error string
97
			) {
98
				return false;
99
			} else {
100
				//log this because that's weird. Just use the normal PHP error log
101
				error_log(
102
					sprintf(
103
						__( 'Event Espresso error detected when checking if table existed: %1$s (it wasn\'t just that the table didn\'t exist either)', 'event_espresso' ),
104
					$new_error
105
					)
106
				);
107
			}
108
		}
109
		return true;
110
	}
111
112
113
114
    /**
115
     * @param $table_name
116
     * @param $index_name
117
     * @return array of columns used on that index, Each entry is an object with the following properties {
118
     *  @type string Table
119
     *  @type string Non_unique "0" or "1"
120
     *  @type string Key_name
121
     *  @type string Seq_in_index
122
     *  @type string Column_name
123
     *  @type string Collation
124
     *  @type string Cardinality
125
     *  @type string Sub_part on a column, usually this is just the number of characters from this column to use in indexing
126
     *  @type string|null Packed
127
     *  @type string Null
128
     *  @type string Index_type
129
     *  @type string Comment
130
     *  @type string Index_comment
131
     * }
132
     */
133
	public function showIndexes($table_name, $index_name){
134
	    global $wpdb;
135
        $table_name = $this->ensureTableNameHasPrefix($table_name);
136
        $index_exists_query = "SHOW INDEX FROM {$table_name} WHERE Key_name = '{$index_name}'";
137
        return $wpdb->get_results($index_exists_query);
138
    }
139
}
140