OSDN Git Service

db query: select database
authorscribu <mail@scribu.net>
Mon, 29 Apr 2013 18:08:34 +0000 (21:08 +0300)
committerscribu <mail@scribu.net>
Mon, 29 Apr 2013 18:10:27 +0000 (21:10 +0300)
Also, let WP_CLI::launch() proxy STDIN, instead of reading it
explicitly.

features/db.feature
php/commands/db.php

index 30630fa..ca32411 100644 (file)
@@ -24,6 +24,24 @@ Feature: Perform database operations
     Then it should run without errors
     And STDOUT should not be empty
 
-    When I run `wp db query 'SELECT COUNT(*) FROM wp_posts'`
+  Scenario: DB Query
+    Given a WP install
+
+    When I run `wp db query 'SELECT COUNT(*) as total FROM wp_posts'`
+    Then it should run without errors
+    And STDOUT should contain:
+    """
+    total
+    """
+
+    Given a debug.sql file:
+    """
+    SELECT COUNT(*) as total FROM wp_posts
+    """
+
+    When I run `wp db query < debug.sql`
     Then it should run without errors
-    And STDOUT should match '%d'
+    And STDOUT should contain:
+    """
+    total
+    """
index e19cc14..662306d 100644 (file)
@@ -85,13 +85,14 @@ class DB_Command extends WP_CLI_Command {
         * @synopsis [<sql>]
         */
        function query( $args ) {
-               if ( empty( $args ) ) {
-                       $query = file_get_contents( 'php://stdin' );
-               } else {
-                       list( $query ) = $args;
+               $cmd = '--host=%s --user=%s --database=%s';
+               $cmd = Utils\esc_cmd( $cmd, DB_HOST, DB_USER, DB_NAME );
+
+               if ( !empty( $args ) ) {
+                       $cmd .= Utils\esc_cmd( ' --execute=%s', $args[0] );
                }
 
-               self::run_query( $query );
+               self::run( 'mysql', $cmd );
        }
 
        /**