db-command icon indicating copy to clipboard operation
db-command copied to clipboard

Added "db get-rows" command

Open petenelson opened this issue 6 years ago • 9 comments

This code addresses #145 by adding a get-rows command to allow custom DB queries to get results formatted in a list, CSV, JSON, or a single column that can be piped to other commands.

petenelson avatar Nov 04 '19 21:11 petenelson

At first I thought this PR was good for merging, but then I realized that it deviates considerably from #145 .

I think this should be implemented as a [--format=<format>] flag to the existing wp db query:

  • stick with using mysql directly, not loading WP for this.
  • strip --format=<format> from the arguments that are sent to mysql
  • only allow select queries when --format=<format> was specified
  • collect results from mysql and then pass them to the requested formatter

Are you up for another stab at this?

schlessera avatar Nov 12 '19 02:11 schlessera

This was the part I'm not quite clear on how to implement collect results from mysql and then pass them to the requested formatter which is why I went with this get-rows route. Would the ob_start/ob_get_clean pickup the terminal output from the run_mysql_command() function?

petenelson avatar Nov 12 '19 17:11 petenelson

Aha, think I found what's needed, passing descriptors and reading from pipes. Making some progress on this.

petenelson avatar Nov 22 '19 20:11 petenelson

Something like this, but it will take an update to the run_mysql_command() in Utils in order to pass an arg to be able to capture the output.

$descriptors = [
	[ 'pipe', 'r' ],
	[ 'pipe', 'w' ],
	[ 'pipe', 'w' ],
];

$run_args = [ 'return_results' => true ];

$results = self::run( '/usr/bin/env mysql --no-defaults --no-auto-rehash --skip-column-names', $assoc_args, $descriptors, $run_args );

$results = array_filter( explode( PHP_EOL, $results ) );

I'll need to get the main wp-cli project setup locally for development in order to submit a PR for the run_mysql_command() update.

petenelson avatar Nov 22 '19 20:11 petenelson

@petenelson Looks like you've got ti figured out. Let me know if you need assistance with building something like the above.

schlessera avatar Dec 13 '19 08:12 schlessera

This will be greatly simplified by https://github.com/wp-cli/wp-cli/pull/5384.

schlessera avatar Apr 17 '20 03:04 schlessera

@petenelson Are you still interested in working on this?

It should be more straight-forward now, I've adapted Utils\ run_mysql_command(), so that if you pass false as the fourth argument, it will return an array with [ $stdout, $sterr, $exit_code ].

schlessera avatar Apr 17 '20 03:04 schlessera

Hey @petenelson,

Gentle ping to see if you're still up for giving this another try...

No worries at all if you can't find the time or energy, I would just appreciate knowing about that to decide what to do with the PR.

schlessera avatar Dec 08 '20 21:12 schlessera

Probably not right now. I'd need to get all of the development environment up and running again.

petenelson avatar Dec 08 '20 22:12 petenelson

Proceeding with https://github.com/wp-cli/wp-cli/issues/5594 for this repository. I've captured this PR to https://gist.github.com/danielbachhuber/6924f9d9aa78fa3b2092943943bf45b7 in case this PR is auto-closed or broken in some way.

danielbachhuber avatar Nov 18 '22 15:11 danielbachhuber