Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
01/24/2015 10:25:19 PM (11 years ago)
Author:
djpaul
Message:

Correct indentation in BP_Recursive_Query

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-core/bp-core-classes.php

    r9351 r9404  
    27592759abstract class BP_Recursive_Query {
    27602760
    2761         /**
    2762          * Query arguments passed to the constructor.
    2763          *
    2764          * @since BuddyPress (2.2.0)
    2765          * @access public
    2766          * @var array
    2767          */
    2768         public $queries = array();
    2769 
    2770         /**
    2771          * Generate SQL clauses to be appended to a main query.
    2772          *
    2773          * Extending classes should call this method from within a publicly
    2774          * accessible get_sql() method, and manipulate the SQL as necessary.
    2775          * For example, {@link BP_XProfile_Query::get_sql()} is merely a wrapper for
    2776          * get_sql_clauses(), while {@link BP_Activity_Query::get_sql()} discards
    2777          * the empty 'join' clause, and only passes the 'where' clause.
    2778          *
    2779          * @since BuddyPress (2.2.0)
    2780          * @access protected
    2781          *
    2782          * @param  string $primary_table
    2783          * @param  string $primary_id_column
    2784          * @return array
    2785          */
    2786         protected function get_sql_clauses() {
    2787                 $sql = $this->get_sql_for_query( $this->queries );
    2788 
    2789                 if ( ! empty( $sql['where'] ) ) {
    2790                         $sql['where'] = ' AND ' . "\n" . $sql['where'] . "\n";
    2791                 }
    2792 
    2793                 return $sql;
    2794         }
    2795 
    2796         /**
    2797          * Generate SQL clauses for a single query array.
    2798          *
    2799          * If nested subqueries are found, this method recurses the tree to
    2800          * produce the properly nested SQL.
    2801          *
    2802          * Subclasses generally do not need to call this method. It is invoked
    2803          * automatically from get_sql_clauses().
    2804          *
    2805          * @since BuddyPress (2.2.0)
    2806          * @access protected
    2807          *
    2808          * @param  array $query Query to parse.
    2809          * @param  int   $depth Optional. Number of tree levels deep we
    2810          *                      currently are. Used to calculate indentation.
    2811          * @return array
    2812          */
    2813         protected function get_sql_for_query( $query, $depth = 0 ) {
    2814                 $sql_chunks = array(
    2815                         'join'  => array(),
    2816                         'where' => array(),
    2817                 );
    2818 
    2819                 $sql = array(
    2820                         'join'  => '',
    2821                         'where' => '',
    2822                 );
    2823 
    2824                 $indent = '';
    2825                 for ( $i = 0; $i < $depth; $i++ ) {
    2826                         $indent .= "\t";
    2827                 }
    2828 
    2829                 foreach ( $query as $key => $clause ) {
    2830                         if ( 'relation' === $key ) {
    2831                                 $relation = $query['relation'];
    2832                         } elseif ( is_array( $clause ) ) {
    2833                                 // This is a first-order clause
    2834                                 if ( $this->is_first_order_clause( $clause ) ) {
    2835                                         $clause_sql = $this->get_sql_for_clause( $clause, $query );
    2836 
    2837                                         $where_count = count( $clause_sql['where'] );
    2838                                         if ( ! $where_count ) {
    2839                                                 $sql_chunks['where'][] = '';
    2840                                         } elseif ( 1 === $where_count ) {
    2841                                                 $sql_chunks['where'][] = $clause_sql['where'][0];
    2842                                         } else {
    2843                                                 $sql_chunks['where'][] = '( ' . implode( ' AND ', $clause_sql['where'] ) . ' )';
    2844                                         }
    2845 
    2846                                         $sql_chunks['join'] = array_merge( $sql_chunks['join'], $clause_sql['join'] );
    2847                                 // This is a subquery
    2848                                 } else {
    2849                                         $clause_sql = $this->get_sql_for_query( $clause, $depth + 1 );
    2850 
    2851                                         $sql_chunks['where'][] = $clause_sql['where'];
    2852                                         $sql_chunks['join'][]  = $clause_sql['join'];
    2853 
    2854                                 }
    2855                         }
    2856                 }
    2857 
    2858                 // Filter empties
    2859                 $sql_chunks['join']  = array_filter( $sql_chunks['join'] );
    2860                 $sql_chunks['where'] = array_filter( $sql_chunks['where'] );
    2861 
    2862                 if ( empty( $relation ) ) {
    2863                         $relation = 'AND';
    2864                 }
    2865 
    2866                 if ( ! empty( $sql_chunks['join'] ) ) {
    2867                         $sql['join'] = implode( ' ', array_unique( $sql_chunks['join'] ) );
    2868                 }
    2869 
    2870                 if ( ! empty( $sql_chunks['where'] ) ) {
    2871                         $sql['where'] = '( ' . "\n\t" . $indent . implode( ' ' . "\n\t" . $indent . $relation . ' ' . "\n\t" . $indent, $sql_chunks['where'] ) . "\n" . $indent . ')' . "\n";
    2872                 }
    2873 
    2874                 return $sql;
    2875         }
     2761        /**
     2762         * Query arguments passed to the constructor.
     2763         *
     2764         * @since BuddyPress (2.2.0)
     2765         * @access public
     2766         * @var array
     2767         */
     2768        public $queries = array();
     2769
     2770        /**
     2771         * Generate SQL clauses to be appended to a main query.
     2772         *
     2773         * Extending classes should call this method from within a publicly
     2774         * accessible get_sql() method, and manipulate the SQL as necessary.
     2775         * For example, {@link BP_XProfile_Query::get_sql()} is merely a wrapper for
     2776         * get_sql_clauses(), while {@link BP_Activity_Query::get_sql()} discards
     2777         * the empty 'join' clause, and only passes the 'where' clause.
     2778         *
     2779         * @since BuddyPress (2.2.0)
     2780         * @access protected
     2781         *
     2782         * @param  string $primary_table
     2783         * @param  string $primary_id_column
     2784         * @return array
     2785         */
     2786        protected function get_sql_clauses() {
     2787                $sql = $this->get_sql_for_query( $this->queries );
     2788
     2789                if ( ! empty( $sql['where'] ) ) {
     2790                        $sql['where'] = ' AND ' . "\n" . $sql['where'] . "\n";
     2791                }
     2792
     2793                return $sql;
     2794        }
     2795
     2796        /**
     2797         * Generate SQL clauses for a single query array.
     2798         *
     2799         * If nested subqueries are found, this method recurses the tree to
     2800         * produce the properly nested SQL.
     2801         *
     2802         * Subclasses generally do not need to call this method. It is invoked
     2803         * automatically from get_sql_clauses().
     2804         *
     2805         * @since BuddyPress (2.2.0)
     2806         * @access protected
     2807         *
     2808         * @param  array $query Query to parse.
     2809         * @param  int   $depth Optional. Number of tree levels deep we
     2810         *                      currently are. Used to calculate indentation.
     2811         * @return array
     2812         */
     2813        protected function get_sql_for_query( $query, $depth = 0 ) {
     2814                $sql_chunks = array(
     2815                        'join'  => array(),
     2816                        'where' => array(),
     2817                );
     2818
     2819                $sql = array(
     2820                        'join'  => '',
     2821                        'where' => '',
     2822                );
     2823
     2824                $indent = '';
     2825                for ( $i = 0; $i < $depth; $i++ ) {
     2826                        $indent .= "\t";
     2827                }
     2828
     2829                foreach ( $query as $key => $clause ) {
     2830                        if ( 'relation' === $key ) {
     2831                                $relation = $query['relation'];
     2832                        } elseif ( is_array( $clause ) ) {
     2833                                // This is a first-order clause
     2834                                if ( $this->is_first_order_clause( $clause ) ) {
     2835                                        $clause_sql = $this->get_sql_for_clause( $clause, $query );
     2836
     2837                                        $where_count = count( $clause_sql['where'] );
     2838                                        if ( ! $where_count ) {
     2839                                                $sql_chunks['where'][] = '';
     2840                                        } elseif ( 1 === $where_count ) {
     2841                                                $sql_chunks['where'][] = $clause_sql['where'][0];
     2842                                        } else {
     2843                                                $sql_chunks['where'][] = '( ' . implode( ' AND ', $clause_sql['where'] ) . ' )';
     2844                                        }
     2845
     2846                                        $sql_chunks['join'] = array_merge( $sql_chunks['join'], $clause_sql['join'] );
     2847                                // This is a subquery
     2848                                } else {
     2849                                        $clause_sql = $this->get_sql_for_query( $clause, $depth + 1 );
     2850
     2851                                        $sql_chunks['where'][] = $clause_sql['where'];
     2852                                        $sql_chunks['join'][]  = $clause_sql['join'];
     2853                                }
     2854                        }
     2855                }
     2856
     2857                // Filter empties
     2858                $sql_chunks['join']  = array_filter( $sql_chunks['join'] );
     2859                $sql_chunks['where'] = array_filter( $sql_chunks['where'] );
     2860
     2861                if ( empty( $relation ) ) {
     2862                        $relation = 'AND';
     2863                }
     2864
     2865                if ( ! empty( $sql_chunks['join'] ) ) {
     2866                        $sql['join'] = implode( ' ', array_unique( $sql_chunks['join'] ) );
     2867                }
     2868
     2869                if ( ! empty( $sql_chunks['where'] ) ) {
     2870                        $sql['where'] = '( ' . "\n\t" . $indent . implode( ' ' . "\n\t" . $indent . $relation . ' ' . "\n\t" . $indent, $sql_chunks['where'] ) . "\n" . $indent . ')' . "\n";
     2871                }
     2872
     2873                return $sql;
     2874        }
    28762875
    28772876        /**
     
    29462945        }
    29472946
    2948         /**
    2949         * Generate JOIN and WHERE clauses for a first-order clause.
    2950         *
    2951         * Must be overridden in a subclass.
    2952         *
    2953         * @since BuddyPress (2.2.0)
    2954         * @access protected
    2955         *
    2956         * @param  array $clause       Array of arguments belonging to the clause.
    2957         * @param  array $parent_query Parent query to which the clause belongs.
    2958         * @return array {
    2959         *     @type array $join  Array of subclauses for the JOIN statement.
    2960         *     @type array $where Array of subclauses for the WHERE statement.
    2961         * }
    2962         */
    2963         abstract protected function get_sql_for_clause( $clause, $parent_query );
    2964 
    2965         /**
    2966         * Determine whether a clause is first-order.
    2967         *
    2968         * Must be overridden in a subclass.
    2969         *
    2970         * @since BuddyPress (2.2.0)
    2971         * @access protected
    2972         *
    2973         * @param  array $q Clause to check.
    2974         * @return bool
    2975         */
    2976         abstract protected function is_first_order_clause( $query );
     2947        /**
     2948        * Generate JOIN and WHERE clauses for a first-order clause.
     2949        *
     2950        * Must be overridden in a subclass.
     2951        *
     2952        * @since BuddyPress (2.2.0)
     2953        * @access protected
     2954        *
     2955        * @param  array $clause       Array of arguments belonging to the clause.
     2956        * @param  array $parent_query Parent query to which the clause belongs.
     2957        * @return array {
     2958        *     @type array $join  Array of subclauses for the JOIN statement.
     2959        *     @type array $where Array of subclauses for the WHERE statement.
     2960        * }
     2961        */
     2962        abstract protected function get_sql_for_clause( $clause, $parent_query );
     2963
     2964        /**
     2965        * Determine whether a clause is first-order.
     2966        *
     2967        * Must be overridden in a subclass.
     2968        *
     2969        * @since BuddyPress (2.2.0)
     2970        * @access protected
     2971        *
     2972        * @param  array $q Clause to check.
     2973        * @return bool
     2974        */
     2975        abstract protected function is_first_order_clause( $query );
    29772976}
Note: See TracChangeset for help on using the changeset viewer.