Make WordPress Core

Ticket #26114: 26114.diff

File 26114.diff, 7.5 KB (added by dougwollison, 13 years ago)

Initial patch: adds auto_prepare, get_(var/row/col/results)_prepared methods and updates query()

  • src/wp-includes/wp-db.php

     
    1515define( 'EZSQL_VERSION', 'WP1.25' );
    1616
    1717/**
     18 * @since ?.? Added random string to prevent collisions in wpdb::auto_prepare()
    1819 * @since 0.71
    1920 */
    20 define( 'OBJECT', 'OBJECT', true );
     21define( 'OBJECT', 'OBJECT.' . md5( microtime() ), true );
    2122
    2223/**
     24 * @since ?.? Added random string to prevent collisions in wpdb::auto_prepare()
    2325 * @since 2.5.0
    2426 */
    25 define( 'OBJECT_K', 'OBJECT_K' );
     27define( 'OBJECT_K', 'OBJECT_K.' . md5( microtime() ) );
    2628
    2729/**
     30 * @since ?.? Added random string to prevent collisions in wpdb::auto_prepare()
    2831 * @since 0.71
    2932 */
    30 define( 'ARRAY_A', 'ARRAY_A' );
     33define( 'ARRAY_A', 'ARRAY_A.' . md5( microtime() ) );
    3134
    3235/**
     36 * @since ?.? Added random string to prevent collisions in wpdb::auto_prepare()
    3337 * @since 0.71
    3438 */
    35 define( 'ARRAY_N', 'ARRAY_N' );
     39define( 'ARRAY_N', 'ARRAY_N.' . md5( microtime() ) );
    3640
    3741/**
    3842 * WordPress Database Access Abstraction Object
    … …  
    10071011        }
    10081012
    10091013        /**
     1014         * Takes the query and arguments passed to the calling function, calling wpdb::prepare if needed.
     1015         *
     1016         * This will update the passed $query and $output variables based on what's found in $args.
     1017         * If passed the arguments that don't match one of the output flags, and pass it along to
     1018         * wpdb::prepare() to modify the $query arg before it gets used in the calling function.
     1019         *
     1020         * @since ?.?
     1021         *
     1022         * @uses OBJECT For output method detection.
     1023         * @uses OBJECT_K For output method detection.
     1024         * @uses ARRAY_A For output method detection.
     1025         * @uses ARRAY_N For output method detection.
     1026         * @uses wpdb::prepare() To prepare the query using the found prepare arguments.
     1027         *
     1028         * @param array  $args    The arguments passed to the calling function.
     1029         * @param string $query   The query that was passed to the calling function.
     1030         * @param string &$output The output format the calling function should use (by reference).
     1031         *
     1032         * @return null|false|string The result of wpdb::prepare(), null if no query is passed.
     1033         */
     1034        private function auto_prepare( $args = array(), $query = null, &$output = OBJECT ) {
     1035                if ( is_null( $query ) ) {
     1036                        return;
     1037                }
     1038
     1039                if ( count( $args ) > 1 ) {
     1040                        // The first argument is the $query, extract it.
     1041                        $query = array_shift( $args );
     1042
     1043                        // Test if the first or last argument matches one of the output flags.
     1044                        // These flags have been updated to random values, to avoid colisions
     1045                        // with actual prepare arguments.
     1046                        $outputs = array( OBJECT, OBJECT_K, ARRAY_A, ARRAY_N );
     1047                        if ( in_array( $args[0], $outputs, true ) ) {
     1048                                $output = array_shift( $args );
     1049                        } elseif ( in_array( end( $args ), $outputs, true ) ) {
     1050                                $output = array_pop( $args );
     1051                        }
     1052
     1053                        // Check if theres is only one argument, and that
     1054                        // it's the array of prepare values (it could happen),
     1055                        // make it the $args array if so.
     1056                        if ( count( $args ) == 1 && is_array( $args[0] ) ) {
     1057                                $args = $args[0];
     1058                        }
     1059
     1060                        // Make sure there are remaining arguments to use.
     1061                        if ( count( $args ) > 0 ) {
     1062                                // Call wpdb::prepare() and pass it's result to $query
     1063                                $query = $this->prepare( $query, $args );
     1064                        }
     1065                }
     1066        }
     1067
     1068        /**
    10101069         * Print SQL/DB error.
    10111070         *
    10121071         * @since 0.71
    … …  
    11721231         *
    11731232         * More information can be found on the codex page.
    11741233         *
     1234         * @since ?.? Added auto_prepare usage.
    11751235         * @since 0.71
    11761236         *
    11771237         * @param string $query Database query
    … …  
    11801240        function query( $query ) {
    11811241                if ( ! $this->ready )
    11821242                        return false;
     1243
     1244                // Try auto_prepare if other arguments were passed.
     1245                if( func_num_args() > 1 ) {
     1246                        $query = $this->auto_prepare( func_get_args(), $query );
     1247                }
     1248
    11831249                /**
    11841250                 * Filter the database query.
    11851251                 *
    … …  
    14521518        }
    14531519
    14541520        /**
     1521         * Retrieve the first selected value from the database.
     1522         *
     1523         * Auto prepares and calls wpdb::get_var().
     1524         *
     1525         * @since ?.?
     1526         *
     1527         * @uses wpdb::auto_prepare() If other arguments are passed to it.
     1528         * @uses wpdb::get_var() To execute the auto prepared query.
     1529         *
     1530         * @param string $query   SQL query.
     1531         * @param mixed  $args... The output, and/or the variables to pass through wpdb::auto_prepare().
     1532         *
     1533         * @return string|null Database query result (as string), or null on failure.
     1534         */
     1535        function get_var_prepared( $query ){
     1536                $this->func_call = "\$db->get_var_prepared(\"$query\", ".implode(', ', func_get_args()).")";
     1537
     1538                if( func_num_args() > 1 ) {
     1539                        $query = $this->auto_prepare( func_get_args(), $query );
     1540                }
     1541
     1542                return $this->get_var( $query );
     1543        }
     1544
     1545        /**
    14551546         * Retrieve one row from the database.
    14561547         *
    14571548         * Executes a SQL query and returns the row from the SQL result.
    … …  
    14861577        }
    14871578
    14881579        /**
     1580         * Retrieve the first selected row from the database.
     1581         *
     1582         * Auto prepares and calls wpdb::get_row().
     1583         *
     1584         * @since ?.?
     1585         *
     1586         * @uses wpdb::auto_prepare() If other arguments are passed to it.
     1587         * @uses wpdb::get_row() To execute the auto prepared query.
     1588         *
     1589         * @param string $query   SQL query.
     1590         * @param mixed  $args... The output, and/or the variables to pass through wpdb::auto_prepare().
     1591         *
     1592         * @return mixed Database query result in format specified by $output or null on failure.
     1593         */
     1594        function get_row_prepared( $query ){
     1595                $this->func_call = "\$db->get_row_prepared(\"$query\", ".implode(', ', func_get_args()).")";
     1596
     1597                if( func_num_args() > 1 ) {
     1598                        $query = $this->auto_prepare( func_get_args(), $query, $output );
     1599                }
     1600
     1601                return $this->get_row( $query, $output );
     1602        }
     1603
     1604        /**
    14891605         * Retrieve one column from the database.
    14901606         *
    14911607         * Executes a SQL query and returns the column from the SQL result.
    … …  
    15111627        }
    15121628
    15131629        /**
     1630         * Retrieve the first selected column from the database.
     1631         *
     1632         * Auto prepares and calls wpdb::get_col().
     1633         *
     1634         * @since ?.?
     1635         *
     1636         * @uses wpdb::auto_prepare() If other arguments are passed to it.
     1637         * @uses wpdb::get_col() To execute the auto prepared query.
     1638         *
     1639         * @param string $query   SQL query.
     1640         * @param mixed  $args... The output, and/or the variables to pass through wpdb::auto_prepare().
     1641         *
     1642         * @return mixed Database query results. Array indexed from 0 by SQL result row number.
     1643         */
     1644        function get_col_prepared( $query ){
     1645                $this->func_call = "\$db->get_col_prepared(\"$query\", ".implode(', ', func_get_args()).")";
     1646
     1647                if( func_num_args() > 1 ) {
     1648                        $query = $this->auto_prepare( func_get_args(), $query );
     1649                }
     1650
     1651                return $this->get_col( $query );
     1652        }
     1653
     1654        /**
    15141655         * Retrieve an entire SQL result set from the database (i.e., many rows)
    15151656         *
    15161657         * Executes a SQL query and returns the entire SQL result.
    … …  
    15641705        }
    15651706
    15661707        /**
     1708         * Retrieve the results from the database.
     1709         *
     1710         * Auto prepares and calls wpdb::get_results().
     1711         *
     1712         * @since ?.?
     1713         *
     1714         * @uses wpdb::auto_prepare() If other arguments are passed to it.
     1715         * @uses wpdb::get_results() To execute the auto prepared query.
     1716         *
     1717         * @param string $query   SQL query.
     1718         * @param mixed  $args... The output, and/or the variables to pass through wpdb::auto_prepare().
     1719         *
     1720         * @return mixed Database query results.
     1721         */
     1722        function get_results_prepared( $query ){
     1723                $this->func_call = "\$db->get_results_prepared(\"$query\", ".implode(', ', func_get_args()).")";
     1724
     1725                if( func_num_args() > 1 ) {
     1726                        $query = $this->auto_prepare( func_get_args(), $query, $output );
     1727                }
     1728
     1729                // Pass to get_results
     1730                return $this->get_results( $query, $output );
     1731        }
     1732
     1733        /**
    15671734         * Load the column metadata from the last query.
    15681735         *
    15691736         * @since 3.5.0