From 673e034094e0b05dd6eb3bb6b7e880c5f06f6170 Mon Sep 17 00:00:00 2001 From: Vayner Date: Sat, 25 Nov 2023 20:14:46 +0300 Subject: [PATCH 1/3] feat: introduce where clause --- src/Orangehill/Iseed/Iseed.php | 83 ++++++++++++++------------- src/Orangehill/Iseed/IseedCommand.php | 21 +++++-- 2 files changed, 60 insertions(+), 44 deletions(-) diff --git a/src/Orangehill/Iseed/Iseed.php b/src/Orangehill/Iseed/Iseed.php index 20e5c8e..eeb1595 100644 --- a/src/Orangehill/Iseed/Iseed.php +++ b/src/Orangehill/Iseed/Iseed.php @@ -2,9 +2,10 @@ namespace Orangehill\Iseed; +use DB; use Illuminate\Filesystem\Filesystem; use Illuminate\Support\Composer; -use Illuminate\Support\Facades\Config; +use Schema; class Iseed { @@ -51,17 +52,19 @@ public function readStubFile($file) /** * Generates a seed file. - * @param string $table - * @param string $prefix - * @param string $suffix - * @param string $database - * @param int $max - * @param string $prerunEvent - * @param string $postunEvent + * @param string $table + * @param string $prefix + * @param string $suffix + * @param string $database + * @param int $max + * @param string $prerunEvent + * @param string $postunEvent * @return bool * @throws Orangehill\Iseed\TableNotFoundException */ - public function generateSeed($table, $prefix=null, $suffix=null, $database = null, $max = 0, $chunkSize = 0, $exclude = null, $prerunEvent = null, $postrunEvent = null, $dumpAuto = true, $indexed = true, $orderBy = null, $direction = 'ASC') + public function generateSeed($table, $prefix = null, $suffix = null, $database = null, $max = 0, $chunkSize = 0, $exclude = null, + $prerunEvent = null, $postrunEvent = null, $dumpAuto = true, $indexed = true, $orderBy = null, $direction = 'ASC', + $where = null) { if (!$database) { $database = config('database.default'); @@ -75,7 +78,7 @@ public function generateSeed($table, $prefix=null, $suffix=null, $database = nul } // Get the data - $data = $this->getData($table, $max, $exclude, $orderBy, $direction); + $data = $this->getData($table, $max, $exclude, $orderBy, $direction, $where); // Repack the data $dataArray = $this->repackSeedData($data); @@ -127,19 +130,22 @@ public function getSeedPath() /** * Get the Data - * @param string $table + * @param string $table * @return Array */ - public function getData($table, $max, $exclude = null, $orderBy = null, $direction = 'ASC') + public function getData($table, $max, $exclude = null, $orderBy = null, $direction = 'ASC', $where = null) { - $result = \DB::connection($this->databaseName)->table($table); - + $result = DB::connection($this->databaseName)->table($table); if (!empty($exclude)) { - $allColumns = \DB::connection($this->databaseName)->getSchemaBuilder()->getColumnListing($table); + $allColumns = DB::connection($this->databaseName)->getSchemaBuilder()->getColumnListing($table); $result = $result->select(array_diff($allColumns, $exclude)); } + if ($where) { + $result = $result->where($where); + } - if($orderBy) { + dd($result->toSql()); + if ($orderBy) { $result = $result->orderBy($orderBy, $direction); } @@ -152,7 +158,7 @@ public function getData($table, $max, $exclude = null, $orderBy = null, $directi /** * Repacks data read from the database - * @param array|object $data + * @param array|object $data * @return array */ public function repackSeedData($data) @@ -180,17 +186,17 @@ public function repackSeedData($data) */ public function hasTable($table) { - return \Schema::connection($this->databaseName)->hasTable($table); + return Schema::connection($this->databaseName)->hasTable($table); } /** * Generates a seed class name (also used as a filename) - * @param string $table - * @param string $prefix - * @param string $suffix + * @param string $table + * @param string $prefix + * @param string $suffix * @return string */ - public function generateClassName($table, $prefix=null, $suffix=null) + public function generateClassName($table, $prefix = null, $suffix = null) { $tableString = ''; $tableName = explode('_', $table); @@ -211,13 +217,13 @@ public function getStubPath() /** * Populate the place-holders in the seed stub. - * @param string $class - * @param string $stub - * @param string $table - * @param string $data - * @param int $chunkSize - * @param string $prerunEvent - * @param string $postunEvent + * @param string $class + * @param string $stub + * @param string $table + * @param string $data + * @param int $chunkSize + * @param string $prerunEvent + * @param string $postunEvent * @return string */ public function populateStub($class, $stub, $table, $data, $chunkSize = null, $prerunEvent = null, $postrunEvent = null, $indexed = true) @@ -285,8 +291,8 @@ public function populateStub($class, $stub, $table, $data, $chunkSize = null, $p /** * Create the full path name to the seed file. - * @param string $name - * @param string $path + * @param string $name + * @param string $path * @return string */ public function getPath($name, $path) @@ -296,7 +302,7 @@ public function getPath($name, $path) /** * Prettify a var_export of an array - * @param array $array + * @param array $array * @return string */ protected function prettifyArray($array, $indexed = true) @@ -328,8 +334,7 @@ protected function prettifyArray($array, $indexed = true) //skip character right after an escape \ if ($lines[$i][$j] == '\\') { $j++; - } - //check string open/end + } //check string open/end else if ($lines[$i][$j] == '\'') { $inString = !$inString; } @@ -349,8 +354,8 @@ protected function prettifyArray($array, $indexed = true) /** * Adds new lines to the passed content variable reference. * - * @param string $content - * @param int $numberOfLines + * @param string $content + * @param int $numberOfLines */ private function addNewLines(&$content, $numberOfLines = 1) { @@ -363,8 +368,8 @@ private function addNewLines(&$content, $numberOfLines = 1) /** * Adds indentation to the passed content reference. * - * @param string $content - * @param int $numberOfIndents + * @param string $content + * @param int $numberOfIndents */ private function addIndent(&$content, $numberOfIndents = 1) { @@ -392,7 +397,7 @@ public function cleanSection() /** * Updates the DatabaseSeeder file's run method (kudoz to: https://github.com/JeffreyWay/Laravel-4-Generators) - * @param string $className + * @param string $className * @return bool */ public function updateDatabaseSeederRunMethod($className) diff --git a/src/Orangehill/Iseed/IseedCommand.php b/src/Orangehill/Iseed/IseedCommand.php index 58517ec..aefdcb9 100755 --- a/src/Orangehill/Iseed/IseedCommand.php +++ b/src/Orangehill/Iseed/IseedCommand.php @@ -2,9 +2,13 @@ namespace Orangehill\Iseed; +use Config; +use File; use Illuminate\Console\Command; +use Schema; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputOption; +use Symfony\Component\Mime\Exception\LogicException; class IseedCommand extends Command { @@ -25,7 +29,7 @@ class IseedCommand extends Command /** * Create a new command instance. * - * @return \Orangehill\Iseed\IseedCommand + * @return IseedCommand */ public function __construct() { @@ -55,6 +59,7 @@ public function fire() } $tables = explode(",", $this->argument('tables')); + $max = intval($this->option('max')); $chunkSize = intval($this->option('chunksize')); $exclude = explode(",", $this->option('exclude')); @@ -66,6 +71,10 @@ public function fire() $direction = $this->option('direction'); $prefix = $this->option('classnameprefix'); $suffix = $this->option('classnamesuffix'); + $where = json_decode($this->option('where'), true); + if ($this->option('where') && !$where) { + throw new LogicException('where clause json malformed'); + } if ($max < 1) { $max = null; @@ -92,7 +101,7 @@ public function fire() list($fileName, $className) = $this->generateFileName($table, $prefix, $suffix); // if file does not exist or force option is turned on generate seeder - if (!\File::exists($fileName) || $this->option('force')) { + if (!File::exists($fileName) || $this->option('force')) { $this->printResult( app('iseed')->generateSeed( $table, @@ -107,7 +116,8 @@ public function fire() $dumpAuto, $indexed, $orderBy, - $direction + $direction, + $where ), $table ); @@ -160,7 +170,7 @@ protected function getOptions() return array( array('clean', null, InputOption::VALUE_NONE, 'clean iseed section', null), array('force', null, InputOption::VALUE_NONE, 'force overwrite of all existing seed classes', null), - array('database', null, InputOption::VALUE_OPTIONAL, 'database connection', \Config::get('database.default')), + array('database', null, InputOption::VALUE_OPTIONAL, 'database connection', Config::get('database.default')), array('max', null, InputOption::VALUE_OPTIONAL, 'max number of rows', null), array('chunksize', null, InputOption::VALUE_OPTIONAL, 'size of data chunks for each insert query', null), array('exclude', null, InputOption::VALUE_OPTIONAL, 'exclude columns', null), @@ -172,6 +182,7 @@ protected function getOptions() array('direction', null, InputOption::VALUE_OPTIONAL, 'orderby direction', null), array('classnameprefix', null, InputOption::VALUE_OPTIONAL, 'prefix for class and file name', null), array('classnamesuffix', null, InputOption::VALUE_OPTIONAL, 'suffix for class and file name', null), + array('where', null, InputOption::VALUE_OPTIONAL, 'where calause', null), ); } @@ -200,7 +211,7 @@ protected function printResult($successful, $table) */ protected function generateFileName($table, $prefix=null, $suffix=null) { - if (!\Schema::connection($this->option('database') ? $this->option('database') : config('database.default'))->hasTable($table)) { + if (!Schema::connection($this->option('database') ? $this->option('database') : config('database.default'))->hasTable($table)) { throw new TableNotFoundException("Table $table was not found."); } From d136ead27d664c4bece5d300569f4775146d42e8 Mon Sep 17 00:00:00 2001 From: Vayner Date: Sat, 25 Nov 2023 20:19:04 +0300 Subject: [PATCH 2/3] fix: apply for one table only --- src/Orangehill/Iseed/Iseed.php | 1 - src/Orangehill/Iseed/IseedCommand.php | 3 +++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Orangehill/Iseed/Iseed.php b/src/Orangehill/Iseed/Iseed.php index eeb1595..be33f7c 100644 --- a/src/Orangehill/Iseed/Iseed.php +++ b/src/Orangehill/Iseed/Iseed.php @@ -144,7 +144,6 @@ public function getData($table, $max, $exclude = null, $orderBy = null, $directi $result = $result->where($where); } - dd($result->toSql()); if ($orderBy) { $result = $result->orderBy($orderBy, $direction); } diff --git a/src/Orangehill/Iseed/IseedCommand.php b/src/Orangehill/Iseed/IseedCommand.php index aefdcb9..60f70be 100755 --- a/src/Orangehill/Iseed/IseedCommand.php +++ b/src/Orangehill/Iseed/IseedCommand.php @@ -75,6 +75,9 @@ public function fire() if ($this->option('where') && !$where) { throw new LogicException('where clause json malformed'); } + if ($where && count($tables) > 1) { + throw new LogicException('please specify only one table when using where clause'); + } if ($max < 1) { $max = null; From 9b0bd06234b505152c3423df1b71f567db5618bb Mon Sep 17 00:00:00 2001 From: Vayner Date: Sat, 25 Nov 2023 22:27:13 +0300 Subject: [PATCH 3/3] feat: or logics (e.g [["id", 1], ["id", 2]]) --- src/Orangehill/Iseed/Iseed.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Orangehill/Iseed/Iseed.php b/src/Orangehill/Iseed/Iseed.php index be33f7c..61a868b 100644 --- a/src/Orangehill/Iseed/Iseed.php +++ b/src/Orangehill/Iseed/Iseed.php @@ -141,7 +141,7 @@ public function getData($table, $max, $exclude = null, $orderBy = null, $directi $result = $result->select(array_diff($allColumns, $exclude)); } if ($where) { - $result = $result->where($where); + $result = $result->where($where, null, null, 'or'); } if ($orderBy) {