BP_Recursive_Query::get_sql_for_query( array $query, int $depth )
Generate SQL clauses for a single query array.
Description
If nested subqueries are found, this method recurses the tree to produce the properly nested SQL.
Subclasses generally do not need to call this method. It is invoked automatically from get_sql_clauses().
Parameters
- $query
-
(Required) Query to parse.
- $depth
-
(Optional) Number of tree levels deep we currently are. Used to calculate indentation.
Return
(array)
Source
File: bp-core/classes/class-bp-recursive-query.php
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 | protected function get_sql_for_query( $query , $depth = 0 ) { $sql_chunks = array ( 'join' => array (), 'where' => array (), ); $sql = array ( 'join' => '' , 'where' => '' , ); $indent = '' ; for ( $i = 0; $i < $depth ; $i ++ ) { $indent .= "\t" ; } foreach ( $query as $key => $clause ) { if ( 'relation' === $key ) { $relation = $query [ 'relation' ]; } elseif ( is_array ( $clause ) ) { // This is a first-order clause if ( $this ->is_first_order_clause( $clause ) ) { $clause_sql = $this ->get_sql_for_clause( $clause , $query ); $where_count = count ( $clause_sql [ 'where' ] ); if ( ! $where_count ) { $sql_chunks [ 'where' ][] = '' ; } elseif ( 1 === $where_count ) { $sql_chunks [ 'where' ][] = $clause_sql [ 'where' ][0]; } else { $sql_chunks [ 'where' ][] = '( ' . implode( ' AND ' , $clause_sql [ 'where' ] ) . ' )' ; } $sql_chunks [ 'join' ] = array_merge ( $sql_chunks [ 'join' ], $clause_sql [ 'join' ] ); // This is a subquery } else { $clause_sql = $this ->get_sql_for_query( $clause , $depth + 1 ); $sql_chunks [ 'where' ][] = $clause_sql [ 'where' ]; $sql_chunks [ 'join' ][] = $clause_sql [ 'join' ]; } } } // Filter empties $sql_chunks [ 'join' ] = array_filter ( $sql_chunks [ 'join' ] ); $sql_chunks [ 'where' ] = array_filter ( $sql_chunks [ 'where' ] ); if ( empty ( $relation ) ) { $relation = 'AND' ; } if ( ! empty ( $sql_chunks [ 'join' ] ) ) { $sql [ 'join' ] = implode( ' ' , array_unique ( $sql_chunks [ 'join' ] ) ); } if ( ! empty ( $sql_chunks [ 'where' ] ) ) { $sql [ 'where' ] = '( ' . "\n\t" . $indent . implode( ' ' . "\n\t" . $indent . $relation . ' ' . "\n\t" . $indent , $sql_chunks [ 'where' ] ) . "\n" . $indent . ')' . "\n" ; } return $sql ; } |
Changelog
Version | Description |
---|---|
BuddyPress 2.2.0 | Introduced. |
Questions?
We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.