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
+ """
* @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 );
}
/**