Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow where clause #233

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 43 additions & 39 deletions src/Orangehill/Iseed/Iseed.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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');
Expand All @@ -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);
Expand Down Expand Up @@ -127,19 +130,21 @@ 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, null, null, 'or');
}

if($orderBy) {
if ($orderBy) {
$result = $result->orderBy($orderBy, $direction);
}

Expand All @@ -152,7 +157,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)
Expand Down Expand Up @@ -180,17 +185,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);
Expand All @@ -211,13 +216,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)
Expand Down Expand Up @@ -285,8 +290,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)
Expand All @@ -296,7 +301,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)
Expand Down Expand Up @@ -328,8 +333,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;
}
Expand All @@ -349,8 +353,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)
{
Expand All @@ -363,8 +367,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)
{
Expand Down Expand Up @@ -392,7 +396,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)
Expand Down
24 changes: 19 additions & 5 deletions src/Orangehill/Iseed/IseedCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -25,7 +29,7 @@ class IseedCommand extends Command
/**
* Create a new command instance.
*
* @return \Orangehill\Iseed\IseedCommand
* @return IseedCommand
*/
public function __construct()
{
Expand Down Expand Up @@ -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'));
Expand All @@ -66,6 +71,13 @@ 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 ($where && count($tables) > 1) {
throw new LogicException('please specify only one table when using where clause');
}

if ($max < 1) {
$max = null;
Expand All @@ -92,7 +104,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,
Expand All @@ -107,7 +119,8 @@ public function fire()
$dumpAuto,
$indexed,
$orderBy,
$direction
$direction,
$where
),
$table
);
Expand Down Expand Up @@ -160,7 +173,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),
Expand All @@ -172,6 +185,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),
);
}

Expand Down Expand Up @@ -200,7 +214,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.");
}

Expand Down