Skip to content

Commit

Permalink
feat: add command sql:to-map for convert insert SQL to k-v map
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jan 29, 2022
1 parent aa956a0 commit a3071c5
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 5 deletions.
48 changes: 45 additions & 3 deletions app/Console/Controller/SqlController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,16 @@
use Inhere\Console\Controller;
use Inhere\Console\IO\Output;
use Inhere\Kite\Console\Component\ContentsAutoReader;
use Inhere\Kite\Helper\AppHelper;
use Inhere\Kite\Lib\Parser\DBTable;
use InvalidArgumentException;
use Toolkit\PFlag\FlagsParser;
use Toolkit\Stdlib\Helper\Assert;
use Toolkit\Stdlib\Json;
use function array_combine;
use function array_map;
use function count;
use function explode;
use function trim;

/**
* Class SqlController
Expand All @@ -32,7 +38,8 @@ class SqlController extends Controller
protected static function commandAliases(): array
{
return [
'md' => ['2md', 'to-md'],
'md' => ['2md', 'to-md'],
'toMap' => ['2map', '2kv', 'tomap', 'to-map'],
];
}

Expand Down Expand Up @@ -75,7 +82,7 @@ public function mdCommand(FlagsParser $fs, Output $output): void
public function fieldsCommand(FlagsParser $fs, Output $output): void
{
$source = $fs->getOpt('source');
$source = AppHelper::tryReadContents($source);
$source = ContentsAutoReader::readFrom($source);

if (!$source) {
throw new InvalidArgumentException('empty source code for convert');
Expand All @@ -88,4 +95,39 @@ public function fieldsCommand(FlagsParser $fs, Output $output): void

$output->aList($mp);
}

/**
* convert insert SQL to k-v map and dump it.
*
* @options
* -s, --source string;The source create SQL for convert. allow: FILEPATH, @clipboard;true
* -o, --output The output target. default is stdout.
*
* @param FlagsParser $fs
* @param Output $output
*/
public function toMapCommand(FlagsParser $fs, Output $output): void
{
$source = $fs->getOpt('source');
$source = ContentsAutoReader::readFrom($source);

$fieldAndVals = explode(' VALUES ', trim($source), 2);
Assert::equals(count($fieldAndVals), 2, "invalid insert SQL: $source");

$fieldStr = trim($fieldAndVals[0]);
$valueStr = trim($fieldAndVals[1]);

$fields = explode(',', trim($fieldStr, ' ()'));
[, $first] = explode('(', $fields[0], 2);
$fields[0] = trim($first);

$fields = array_map(static fn($field) => trim(trim($field), ' `'), $fields);

// split values
$values = explode(', ', trim($valueStr, ' ();'));
$values = array_map(static fn($value) => trim($value), $values);

$map = array_combine($fields, $values);
$output->writeRaw(Json::pretty($map));
}
}
4 changes: 2 additions & 2 deletions app/Console/simple-cmds.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
$app->addCommand('ln', function ($fs) {
vdump($fs);
}, [
'desc' => 'run ln command',
'desc' => 'run ln command',
'options' => [
's, src, source' => 'the source file path',
't, dst, target' => 'the target link file path',
Expand All @@ -31,7 +31,7 @@
}, [
'desc' => 'find bin file path, like system `which`',
'aliases' => ['where', 'whereis'],
'options' => [
'options' => [
'--clean' => 'bool;clean output, only output path.'
],
'arguments' => [
Expand Down

0 comments on commit a3071c5

Please sign in to comment.