クラス WP_Meta_Query::get_sql()
2014/12/25
WP_Meta_Query::get_sql()
定義ファイル :/blog/wp-includes/meta.php 3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 | public function get_sql( $type, $primary_table, $primary_id_column, $context = null ) { global $wpdb; if ( ! $meta_table = _get_meta_table( $type ) ) return false; $meta_id_column = sanitize_key( $type . '_id' ); $join = array(); $where = array(); $key_only_queries = array(); $queries = array(); // Split out the queries with empty arrays as value foreach ( $this->queries as $k => $q ) { if ( isset( $q['value'] ) && is_array( $q['value'] ) && empty( $q['value'] ) ) { $key_only_queries[$k] = $q; unset( $this->queries[$k] ); } } // Split out the meta_key only queries (we can only do this for OR) if ( 'OR' == $this->relation ) { foreach ( $this->queries as $k => $q ) { if ( ( empty( $q['compare'] ) || 'NOT EXISTS' != $q['compare'] ) && ! array_key_exists( 'value', $q ) && ! empty( $q['key'] ) ) $key_only_queries[$k] = $q; else $queries[$k] = $q; } } else { $queries = $this->queries; } // Specify all the meta_key only queries in one go if ( $key_only_queries ) { $join[] = "INNER JOIN $meta_table ON $primary_table.$primary_id_column = $meta_table.$meta_id_column"; foreach ( $key_only_queries as $key => $q ) $where["key-only-$key"] = $wpdb->prepare( "$meta_table.meta_key = %s", trim( $q['key'] ) ); } foreach ( $queries as $k => $q ) { $meta_key = isset( $q['key'] ) ? trim( $q['key'] ) : ''; $meta_type = $this->get_cast_for_type( isset( $q['type'] ) ? $q['type'] : '' ); if ( array_key_exists( 'value', $q ) && is_null( $q['value'] ) ) $q['value'] = ''; $meta_value = isset( $q['value'] ) ? $q['value'] : null; if ( isset( $q['compare'] ) ) $meta_compare = strtoupper( $q['compare'] ); else $meta_compare = is_array( $meta_value ) ? 'IN' : '='; if ( ! in_array( $meta_compare, array( '=', '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN', 'NOT EXISTS', 'REGEXP', 'NOT REGEXP', 'RLIKE' ) ) ) $meta_compare = '='; $i = count( $join ); $alias = $i ? 'mt' . $i : $meta_table; if ( 'NOT EXISTS' == $meta_compare ) { $join[$i] = "LEFT JOIN $meta_table"; $join[$i] .= $i ? " AS $alias" : ''; $join[$i] .= " ON ($primary_table.$primary_id_column = $alias.$meta_id_column AND $alias.meta_key = '$meta_key')"; $where[$k] = ' ' . $alias . '.' . $meta_id_column . ' IS NULL'; continue; } $join[$i] = "INNER JOIN $meta_table"; $join[$i] .= $i ? " AS $alias" : ''; $join[$i] .= " ON ($primary_table.$primary_id_column = $alias.$meta_id_column)"; $where[$k] = ''; if ( !empty( $meta_key ) ) $where[$k] = $wpdb->prepare( "$alias.meta_key = %s", $meta_key ); if ( is_null( $meta_value ) ) { if ( empty( $where[$k] ) ) unset( $join[$i] ); continue; } if ( in_array( $meta_compare, array( 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN' ) ) ) { if ( ! is_array( $meta_value ) ) $meta_value = preg_split( '/[,\s]+/', $meta_value ); if ( empty( $meta_value ) ) { unset( $join[$i] ); continue; } } else { $meta_value = trim( $meta_value ); } if ( 'IN' == substr( $meta_compare, -2) ) { $meta_compare_string = '(' . substr( str_repeat( ',%s', count( $meta_value ) ), 1 ) . ')'; } elseif ( 'BETWEEN' == substr( $meta_compare, -7) ) { $meta_value = array_slice( $meta_value, 0, 2 ); $meta_compare_string = '%s AND %s'; } elseif ( 'LIKE' == $meta_compare || 'NOT LIKE' == $meta_compare ) { $meta_value = '%' . $wpdb->esc_like( $meta_value ) . '%'; $meta_compare_string = '%s'; } else { $meta_compare_string = '%s'; } if ( ! empty( $where[$k] ) ) $where[$k] .= ' AND '; $where[$k] = ' (' . $where[$k] . $wpdb->prepare( "CAST($alias.meta_value AS {$meta_type}) {$meta_compare} {$meta_compare_string})", $meta_value ); } $where = array_filter( $where ); if ( empty( $where ) ) $where = ''; else $where = ' AND (' . implode( "\n{$this->relation} ", $where ) . ' )'; $join = implode( "\n", $join ); if ( ! empty( $join ) ) $join = ' ' . $join; return apply_filters_ref_array( 'get_meta_sql', array( compact( 'join', 'where' ), $this->queries, $type, $primary_table, $primary_id_column, $context ) ); } } |