OSDN Git Service

make query string mandatory first parameter in Iterators\Query constructor
authorscribu <mail@scribu.net>
Sun, 14 Apr 2013 23:37:32 +0000 (02:37 +0300)
committerscribu <mail@scribu.net>
Sun, 14 Apr 2013 23:37:37 +0000 (02:37 +0300)
see #390

php/WP_CLI/Iterators/Query.php
php/WP_CLI/Iterators/Table.php

index 1954586..523e300 100644 (file)
@@ -24,24 +24,19 @@ class Query implements \Iterator {
         *
         * This will loop over all users, but will retrieve them 100 by 100:
         * <code>
-        * foreach( new Iterators\Query( array( 'query' => 'SELECT * FROM users', 'limit' => 100 ) ) as $user ) {
+        * foreach( new Iterators\Query( 'SELECT * FROM users', 100 ) as $user ) {
         *     tickle( $user );
         * }
         * </code>
         *
-        *
-        * @param array $args Supported arguments:
-        *              query – the query as a string. It shouldn't include any LIMIT clauses
-        *              limit – (optional) how many rows to retrieve at once, default value is 500
+        * @param string $query The query as a string. It shouldn't include any LIMIT clauses
+        * @param number $limit How many rows to retrieve at once; default value is 500 (optional)
         */
-       function __construct( $args = array() ) {
+       public function __construct( $query, $limit = 500 ) {
+               $this->query = $query;
+               $this->limit = $limit;
+
                $this->db = $GLOBALS['wpdb'];
-               foreach( $args as $key => $value ) {
-                       $this->$key = $value;
-               }
-               if ( !$this->query ) {
-                       throw new InvalidArgumentException( 'Missing query argument.' );
-               }
        }
 
        function load_items_from_db() {
index eade2ca..825bf26 100644 (file)
@@ -52,11 +52,10 @@ class Table extends Query {
                $conditions = self::build_where_conditions( $args['where'] );
                $where_sql = $conditions? " WHERE $conditions" : '';
                $query = "SELECT $fields FROM $table $where_sql";
-               $parent_args = compact( 'query' );
-               if ( isset( $args['limit'] ) ) {
-                       $parent_args['limit'] = $args['limit'];
-               }
-               parent::__construct( $parent_args );
+
+               $limit = isset( $args['limit'] ) ? $args['limit'] : 500;
+
+               parent::__construct( $query, $limit );
        }
 
        static function build_fields( $fields ) {