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

When execute command `seed:run`, insert all data to a table in a bulk. #1148

Merged
merged 7 commits into from Aug 31, 2017
@@ -287,6 +287,15 @@ public function fetchAll($sql);
*/
public function insert(Table $table, $row);
/**
* Inserts data into a table in a bulk.
*
* @param Table $table where to insert data
* @param array $rows
* @return void
*/
public function bulkinsert(Table $table, $rows);
/**
* Quotes a table name for use in a query.
*
@@ -183,6 +183,14 @@ public function insert(Table $table, $row)
$this->getAdapter()->insert($table, $row);
}
/**
* {@inheritdoc}
*/
public function bulkinsert(Table $table, $rows)
{
$this->getAdapter()->bulkinsert($table, $rows);
}
/**
* {@inheritdoc}
*/
@@ -167,6 +167,39 @@ public function insert(Table $table, $row)
$stmt = $this->getConnection()->prepare($sql);
$stmt->execute(array_values($row));
}
/**
* {@inheritdoc}
*/
public function bulkinsert(Table $table, $rows)
{
$sql = sprintf(
"INSERT INTO %s ",
$this->quoteTableName($table->getName())
);
$current = current($rows);
$keys = array_keys($current);
$sql .= "(". implode(', ', array_map(array($this, 'quoteColumnName'), $keys)) . ") VALUES";
$vals = array();
foreach ($rows as $row) {
foreach($row as $v) {
$vals[] = $v;
}
}
$count_keys = count($keys);
$query = "(" . implode(', ', array_fill(0, $count_keys, '?')) . ")";
$count_vars = count($rows);
$queries = array_fill(0, $count_vars, $query);
$sql .= implode(',', $queries);
$stmt = $this->getConnection()->prepare($sql);
$stmt->execute($vals);
}
/**
* {@inheritdoc}
*/
@@ -248,6 +248,17 @@ public function insert(Table $table, $row)
parent::insert($adapterTable, $row);
}
/**
* {@inheritdoc}
*/
public function bulkinsert(Table $table, $rows)
{
$adapterTable = clone $table;
$adapterTableName = $this->getAdapterTableName($table->getName());
$adapterTable->setName($adapterTableName);
parent::bulkinsert($adapterTable, $rows);
}
/**
* Gets the table prefix.
*
@@ -113,6 +113,19 @@ public function insert(Table $table, $row)
$end();
}
/**
* {@inheritdoc}
*
* @return void
*/
public function bulkinsert(Table $table, $rows)
{
$end = $this->startCommandTimer();
$this->writeCommand('bulkinsert', [$table->getName()]);
parent::bulkinsert($table, $rows);
$end();
}
/**
* {@inheritdoc}
*/
View
@@ -647,8 +647,28 @@ public function update()
*/
public function saveData()
{
foreach ($this->getData() as $row) {
$this->getAdapter()->insert($this, $row);
$rows = $this->getData();
if (empty($rows)) {
return;
}
$bulk = true;
$row = current($rows);
$c = array_keys($row);
foreach($this->getData() as $row) {
$k = array_keys($row);
if ($k != $c) {
$bulk = false;
break;
}
}
if ($bulk) {
$this->getAdapter()->bulkinsert($this, $this->getData());
} else {
foreach ($this->getData() as $row) {
$this->getAdapter()->insert($this, $row);
}
}
}
@@ -1082,6 +1082,42 @@ public function testHasColumnReservedName()
$this->assertTrue($tableQuoted->hasColumn('value'));
}
public function testBulkInsertData()
{
$data = array(
array(
'column1' => 'value1',
'column2' => 1,
),
array(
'column1' => 'value2',
'column2' => 2,
),
array(
'column1' => 'value3',
'column2' => 3,
)
);
$table = new \Phinx\Db\Table('table1', array(), $this->adapter);
$table->addColumn('column1', 'string')
->addColumn('column2', 'integer')
->addColumn('column3', 'string', array('default' => 'test'))
->insert($data);
$this->adapter->createTable($table);
$this->adapter->bulkinsert($table, $table->getData());
$table->reset();
$rows = $this->adapter->fetchAll('SELECT * FROM table1');
$this->assertEquals('value1', $rows[0]['column1']);
$this->assertEquals('value2', $rows[1]['column1']);
$this->assertEquals('value3', $rows[2]['column1']);
$this->assertEquals(1, $rows[0]['column2']);
$this->assertEquals(2, $rows[1]['column2']);
$this->assertEquals(3, $rows[2]['column2']);
$this->assertEquals('test', $rows[0]['column3']);
$this->assertEquals('test', $rows[2]['column3']);
}
public function testInsertData()
{
$data = array(
@@ -374,7 +374,7 @@ public function testRenameColumnIsCaseSensitive()
$this->assertFalse($this->adapter->hasColumn('t', 'columnOne'));
$this->assertTrue($this->adapter->hasColumn('t', 'columnTwo'));
}
public function testRenamingANonExistentColumn()
{
$table = new \Phinx\Db\Table('t', array(), $this->adapter);
@@ -937,6 +937,32 @@ public function testTimestampWithTimezone()
}
}
public function testBulkInsertData()
{
$table = new \Phinx\Db\Table('table1', array(), $this->adapter);
$table->addColumn('column1', 'string')
->addColumn('column2', 'integer')
->insert(array(
array(
'column1' => 'value1',
'column2' => 1
),
array(
'column1' => 'value2',
'column2' => 2
)
));
$this->adapter->createTable($table);
$this->adapter->bulkinsert($table, $table->getData());
$table->reset();
$rows = $this->adapter->fetchAll('SELECT * FROM table1');
$this->assertEquals('value1', $rows[0]['column1']);
$this->assertEquals('value2', $rows[1]['column1']);
$this->assertEquals(1, $rows[0]['column2']);
$this->assertEquals(2, $rows[1]['column2']);
}
public function testInsertData()
{
$table = new \Phinx\Db\Table('table1', array(), $this->adapter);
@@ -646,6 +646,49 @@ public function testAddIndexTwoTablesSameIndex()
$this->assertTrue($table2->hasIndex('email'));
}
public function testBulkInsertData()
{
$table = new \Phinx\Db\Table('table1', array(), $this->adapter);
$table->addColumn('column1', 'string')
->addColumn('column2', 'integer')
->insert(array(
array(
'column1' => 'value1',
'column2' => 1,
),
array(
'column1' => 'value2',
'column2' => 2,
)
))
->insert(
array(
'column1' => 'value3',
'column2' => 3,
)
)
->insert(
array(
'column1' => '\'value4\'',
'column2' => null,
)
);
$this->adapter->createTable($table);
$this->adapter->bulkinsert($table, $table->getData());
$table->reset();
$rows = $this->adapter->fetchAll('SELECT * FROM table1');
$this->assertEquals('value1', $rows[0]['column1']);
$this->assertEquals('value2', $rows[1]['column1']);
$this->assertEquals('value3', $rows[2]['column1']);
$this->assertEquals('\'value4\'', $rows[3]['column1']);
$this->assertEquals(1, $rows[0]['column2']);
$this->assertEquals(2, $rows[1]['column2']);
$this->assertEquals(3, $rows[2]['column2']);
$this->assertEquals(null, $rows[3]['column2']);
}
public function testInsertData()
{
$table = new \Phinx\Db\Table('table1', array(), $this->adapter);
@@ -687,6 +730,26 @@ public function testInsertData()
$this->assertEquals(null, $rows[3]['column2']);
}
public function testBulkInsertDataEnum()
{
$table = new \Phinx\Db\Table('table1', array(), $this->adapter);
$table->addColumn('column1', 'enum', array('values' => ['a', 'b', 'c']))
->addColumn('column2', 'enum', array('values' => ['a', 'b', 'c'], 'null' => true))
->addColumn('column3', 'enum', array('values' => ['a', 'b', 'c'], 'default' => 'c'))
->insert(array(
'column1' => 'a',
));
$this->adapter->createTable($table);
$this->adapter->bulkinsert($table, $table->getData());
$table->reset();
$rows = $this->adapter->fetchAll('SELECT * FROM table1');
$this->assertEquals('a', $rows[0]['column1']);
$this->assertEquals(null, $rows[0]['column2']);
$this->assertEquals('c', $rows[0]['column3']);
}
public function testInsertDataEnum()
{
$table = new \Phinx\Db\Table('table1', array(), $this->adapter);
@@ -666,6 +666,41 @@ public function testForignKeysArePropertlyEscaped()
$this->assertTrue($foreign->hasForeignKey('user'));
}
public function testBulkInsertData()
{
$table = new \Phinx\Db\Table('table1', array(), $this->adapter);
$table->addColumn('column1', 'string')
->addColumn('column2', 'integer')
->insert(array(
array(
'column1' => 'value1',
'column2' => 1,
),
array(
'column1' => 'value2',
'column2' => 2,
)
))
->insert(
array(
'column1' => 'value3',
'column2' => 3,
)
);
$this->adapter->createTable($table);
$this->adapter->bulkinsert($table, $table->getData());
$table->reset();
$rows = $this->adapter->fetchAll('SELECT * FROM table1');
$this->assertEquals('value1', $rows[0]['column1']);
$this->assertEquals('value2', $rows[1]['column1']);
$this->assertEquals('value3', $rows[2]['column1']);
$this->assertEquals(1, $rows[0]['column2']);
$this->assertEquals(2, $rows[1]['column2']);
$this->assertEquals(3, $rows[2]['column2']);
}
public function testInsertData()
{
$table = new \Phinx\Db\Table('table1', array(), $this->adapter);
@@ -341,7 +341,7 @@ public function testInsertData()
$this->mock
->expects($this->once())
->method('insert')
->method('bulkinsert')
->with($this->callback(
function ($table) {
return $table->getName() == 'pre_table_suf';
@@ -257,9 +257,9 @@ public function testInsertSaveData()
],
];
$adapterStub->expects($this->exactly(4))
->method('insert')
->with($table, $this->logicalOr($data[0], $data[1], $moreData[0], $moreData[1]));
$adapterStub->expects($this->exactly(1))
->method('bulkinsert')
->with($table, array($data[0], $data[1], $moreData[0], $moreData[1]));
$table->insert($data)
->insert($moreData)
@@ -175,7 +175,7 @@ public function testInsert()
->setConstructorArgs([[]])
->getMock();
$adapterStub->expects($this->once())
->method('insert');
->method('bulkinsert');
$table = new Table('testdb', [], $adapterStub);
ProTip! Use n and p to navigate between commits in a pull request.