From 1eab339347add11a6b4f38c966ed7baaac1384e7 Mon Sep 17 00:00:00 2001 From: Christopher Vagnetoft Date: Fri, 17 Dec 2021 01:48:57 +0100 Subject: [PATCH] Updated PDO shell plugin * Added .save and .show commands to PDO shell plugin --- plugins/com.noccy.pdo.shell/README.md | 39 +++++++++++ .../com.noccy.pdo.shell/Shell/PdoShell.php | 66 +++++++++++++++++++ 2 files changed, 105 insertions(+) create mode 100644 plugins/com.noccy.pdo.shell/README.md diff --git a/plugins/com.noccy.pdo.shell/README.md b/plugins/com.noccy.pdo.shell/README.md new file mode 100644 index 0000000..e8a9086 --- /dev/null +++ b/plugins/com.noccy.pdo.shell/README.md @@ -0,0 +1,39 @@ +# PDO Shell Plugin + +This is a plugin to interactively work with PDO. + +## Usage + +The simplest use is invoking the shell directly: + + $ spark pdo:shell + PDO[db]> + +You are presented with a prompt containing the string "PDO", follwed by the currently +selected resource in brackets. Commands start with a period (`.`) and you can get a +list of the valid commands with `.help`. + +To run a query, you can either run it directly: + + PDO[db]> SELECT id FROM users WHERE username='bob' + +Or let the shell do the escaping for you: + + PDO[db]> .query "SELECT id FROM users WHERE username=?" bob + +You can also read the commands from a file and have them executed as if they were +entered into the shell: + + $ spark pdo:shell -r sparkrc + +### Selecting the resource + +This can be done from the command line: + + $ spark pdo:shell --db otherdb + +Or from within the shell: + + PDO[db]> .select otherdb + PDO[otherdb]> + diff --git a/plugins/com.noccy.pdo.shell/Shell/PdoShell.php b/plugins/com.noccy.pdo.shell/Shell/PdoShell.php index c70778f..45206ca 100644 --- a/plugins/com.noccy.pdo.shell/Shell/PdoShell.php +++ b/plugins/com.noccy.pdo.shell/Shell/PdoShell.php @@ -25,6 +25,8 @@ class PdoShell { private array $options = []; + private ?array $lastQuery = null; + private array $defaultOptions = [ 'output' => 'table', 'table.maxwidth' => 40, @@ -98,6 +100,12 @@ class PdoShell { { switch ($command) { + case '.save': + $this->doSaveCommand($args); + break; + case '.show': + $this->doShowCommand($args); + break; case '.select': $this->doSelectCommand($args); break; @@ -127,7 +135,9 @@ class PdoShell { { $cmds = [ '.help' => "Show this help", + '.save FILE' => "Save the last query and result to a .json file", '.select RES' => "Select the database resource to query", + '.show [FILE]' => "Show the last query, or one saved to file", '.set [KEY [VALUE]]' => "Set a configuration value", '.var [NAME [VALUE]]' => "Set a variable, or show variable value", '.exit|.quit' => "Exit the shell", @@ -139,6 +149,54 @@ class PdoShell { } } + private function doSaveCommand(array $args) + { + if (empty($this->lastQuery)) { + $this->output->writeln("No last query to save!"); + return; + } + $filename = array_shift($args); + if (empty($filename)) { + for($n=0;$n<999;$n++) { + $filename = sprintf("query.%03d.json", $n); + if (!file_exists($filename)) break; + } + } + file_put_contents($filename, json_encode($this->lastQuery, JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES)); + $this->output->writeln("Wrote {$filename}"); + } + + private function doShowCommand(array $args) + { + $file = array_shift($args); + if ($file) { + if (!file_exists($file)) { + $this->output->writeln("File not found: {$file}"); + return; + } + $json = json_decode(file_get_contents($file), true); + $query = $json['query']??'?'; + $res = $json['result']??[]; + } else { + $query = $this->lastQuery['query']; + $res = $this->lastQuery['result']; + } + + $this->output->writeln("{$query}"); + + switch ($this->options['output']) { + case 'table': + $this->dumpQueryTable($res); + break; + case 'vertical': + $this->dumpQueryVertical($res); + break; + default: + print_r($res); + } + + } + private function doSetCommand(array $args) { $varname = array_shift($args); @@ -221,6 +279,14 @@ class PdoShell { return; } + $this->lastQuery = [ + 'timestamp' => microtime(true), + 'resource' => $this->resource, + 'query' => $query, + 'params' => $params, + 'result' => $res + ]; + switch ($this->options['output']) { case 'table': $this->dumpQueryTable($res);