BP_XProfile_Query:: get_sql_for_clause (数组美元的条款,数组parent_query美元)
为一阶查询子句生成SQL JOIN和WHERE子句。
描述
“一阶”意味着它是一个带有“字段”或“值”的数组。
参数
- 美元的条款
-
(数组)(必需)查询子句。
- parent_query美元
-
(数组)(必需)父查询数组。
返回
(数组)数组,包含要附加到一阶查询的JOIN和WHERE SQL子句。
- “加入”
(字符串)添加到主JOIN子句的SQL片段。 - “在那里”
(字符串)附加到主WHERE子句的SQL片段。
源
文件:bp-xprofile /类/ class-bp-xprofile-query.php
Public function get_sql_for_clause(&$子句,$parent_query) {global $wpdb;$ sql_chunks =阵列(',' = >数组(),“加入”= >数组());If (isset($clause['compare']) {$clause['compare'] = strtoupper($clause['compare']);} else {$clause['compare'] = isset($clause['value']) && is_array($clause['value']) ?“在”:“=”;} if (!In_array ($clause['compare'], array('=', '!=', '>', '>=', '<', '<=', ' 像”、“不喜欢”、“在”、“不是”、“之间”、“之间”、“存在”、“不存在”、“正则表达式”、“不REGEXP”、“RLIKE ' ) ) ) { $ 条款(“比较”)=“=”;} $field_compare = $clause['compare'];//首先构建JOIN子句,如果需要的话。 $join = ''; $data_table = buddypress()->profile->table_name_data; // We prefer to avoid joins if possible. Look for an existing join compatible with this clause. $alias = $this->find_compatible_table_alias( $clause, $parent_query ); if ( false === $alias ) { $i = count( $this->table_aliases ); $alias = $i ? 'xpq' . $i : $data_table; // JOIN clauses for NOT EXISTS have their own syntax. if ( 'NOT EXISTS' === $field_compare ) { $join .= " LEFT JOIN $data_table"; $join .= $i ? " AS $alias" : ''; $join .= $wpdb->prepare( " ON ($this->primary_table.$this->primary_id_column = $alias.user_id AND $alias.field_id = %d )", $clause['field'] ); // All other JOIN clauses. } else { $join .= " INNER JOIN $data_table"; $join .= $i ? " AS $alias" : ''; $join .= " ON ( $this->primary_table.$this->primary_id_column = $alias.user_id )"; } $this->table_aliases[] = $alias; $sql_chunks['join'][] = $join; } // Save the alias to this clause, for future siblings to find. $clause['alias'] = $alias; // Next, build the WHERE clause. $where = ''; // Field_id. if ( array_key_exists( 'field', $clause ) ) { // Convert field name to ID if necessary. if ( ! is_numeric( $clause['field'] ) ) { $clause['field'] = xprofile_get_field_id_from_name( $clause['field'] ); } // NOT EXISTS has its own syntax. if ( 'NOT EXISTS' === $field_compare ) { $sql_chunks['where'][] = $alias . '.user_id IS NULL'; } else { $sql_chunks['where'][] = $wpdb->prepare( "$alias.field_id = %d", $clause['field'] ); } } // Value. if ( array_key_exists( 'value', $clause ) ) { $field_value = $clause['value']; $field_type = $this->get_cast_for_type( isset( $clause['type'] ) ? $clause['type'] : '' ); if ( in_array( $field_compare, array( 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN' ) ) ) { if ( ! is_array( $field_value ) ) { $field_value = preg_split( '/[,\s]+/', $field_value ); } } else { $field_value = trim( $field_value ); } switch ( $field_compare ) { case 'IN' : case 'NOT IN' : $field_compare_string = '(' . substr( str_repeat( ',%s', count( $field_value ) ), 1 ) . ')'; $where = $wpdb->prepare( $field_compare_string, $field_value ); break; case 'BETWEEN' : case 'NOT BETWEEN' : $field_value = array_slice( $field_value, 0, 2 ); $where = $wpdb->prepare( '%s AND %s', $field_value ); break; case 'LIKE' : case 'NOT LIKE' : $field_value = '%' . bp_esc_like( $field_value ) . '%'; $where = $wpdb->prepare( '%s', $field_value ); break; default : $where = $wpdb->prepare( '%s', $field_value ); break; } if ( $where ) { $sql_chunks['where'][] = "CAST($alias.value AS {$field_type}) {$field_compare} {$where}"; } } /* * Multiple WHERE clauses (`field` and `value` pairs) should be joined in parentheses. */ if ( 1 < count( $sql_chunks['where'] ) ) { $sql_chunks['where'] = array( '( ' . implode( ' AND ', $sql_chunks['where'] ) . ' )' ); } return $sql_chunks; }
更新日志
版本 | 描述 |
---|---|
BuddyPress 2.2.0 | 介绍了。 |