Skip to content
Closed
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
66 changes: 66 additions & 0 deletions framework/yii/db/BatchInsertTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/

namespace yii\db;

/**
* The BatchInsert trait provides [[batchInsert()]] method for MySQL and CUBRID databases.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @author Carsten Brandt <mail@cebe.cc>
* @author Panagiotis Moustafellos <pmoust@gmail.com>
* @since 2.0
*/
trait BatchInsertTrait
{
/**
* Generates a batch INSERT SQL statement.
* For example,
*
* ~~~
* $connection->createCommand()->batchInsert('tbl_user', ['name', 'age'], [
* ['Tom', 30],
* ['Jane', 20],
* ['Linda', 25],
* ])->execute();
* ~~~
*
* Note that the values in each row must match the corresponding column names.
*
* @param string $table the table that new rows will be inserted into.
* @param array $columns the column names
* @param array $rows the rows to be batch inserted into the table
* @return string the batch INSERT SQL statement
*/
public function batchInsert($table, $columns, $rows)
{
if (($tableSchema = $this->db->getTableSchema($table)) !== null) {
$columnSchemas = $tableSchema->columns;
} else {
$columnSchemas = [];
}

foreach ($columns as $i => $name) {
$columns[$i] = $this->db->quoteColumnName($name);
}

$values = [];
foreach ($rows as $row) {
$vs = [];
foreach ($row as $i => $value) {
if (!is_array($value) && isset($columnSchemas[$columns[$i]])) {
$value = $columnSchemas[$columns[$i]]->typecast($value);
}
$vs[] = is_string($value) ? $this->db->quoteValue($value) : $value;
}
$values[] = '(' . implode(', ', $vs) . ')';
}

return 'INSERT INTO ' . $this->db->quoteTableName($table)
. ' (' . implode(', ', $columns) . ') VALUES ' . implode(', ', $values);
}
}
50 changes: 3 additions & 47 deletions framework/yii/db/cubrid/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace yii\db\cubrid;

use yii\base\InvalidParamException;
use yii\db\BatchInsertTrait;

/**
* QueryBuilder is the query builder for CUBRID databases (version 9.1.x and higher).
Expand All @@ -17,6 +18,8 @@
*/
class QueryBuilder extends \yii\db\QueryBuilder
{
use BatchInsertTrait;

/**
* @var array mapping from abstract column types (keys) to physical column types (values).
*/
Expand Down Expand Up @@ -67,51 +70,4 @@ public function resetSequence($tableName, $value = null)
throw new InvalidParamException("There is not sequence associated with table '$tableName'.");
}
}

/**
* Generates a batch INSERT SQL statement.
* For example,
*
* ~~~
* $connection->createCommand()->batchInsert('tbl_user', ['name', 'age'], [
* ['Tom', 30],
* ['Jane', 20],
* ['Linda', 25],
* ])->execute();
* ~~~
*
* Note that the values in each row must match the corresponding column names.
*
* @param string $table the table that new rows will be inserted into.
* @param array $columns the column names
* @param array $rows the rows to be batch inserted into the table
* @return string the batch INSERT SQL statement
*/
public function batchInsert($table, $columns, $rows)
{
if (($tableSchema = $this->db->getTableSchema($table)) !== null) {
$columnSchemas = $tableSchema->columns;
} else {
$columnSchemas = [];
}

foreach ($columns as $i => $name) {
$columns[$i] = $this->db->quoteColumnName($name);
}

$values = [];
foreach ($rows as $row) {
$vs = [];
foreach ($row as $i => $value) {
if (!is_array($value) && isset($columnSchemas[$columns[$i]])) {
$value = $columnSchemas[$columns[$i]]->typecast($value);
}
$vs[] = is_string($value) ? $this->db->quoteValue($value) : $value;
}
$values[] = '(' . implode(', ', $vs) . ')';
}

return 'INSERT INTO ' . $this->db->quoteTableName($table)
. ' (' . implode(', ', $columns) . ') VALUES ' . implode(', ', $values);
}
}
4 changes: 4 additions & 0 deletions framework/yii/db/mssql/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,20 @@

namespace yii\db\mssql;

use yii\db\BatchInsertTrait;
use yii\base\InvalidParamException;

/**
* QueryBuilder is the query builder for MS SQL Server databases (version 2008 and above).
*
* @author Timur Ruziev <resurtm@gmail.com>
* @author Panagiotis Moustafellos <pmoust@gmail.com>
* @since 2.0
*/
class QueryBuilder extends \yii\db\QueryBuilder
{
use BatchInsertTrait;

/**
* @var array mapping from abstract column types (keys) to physical column types (values).
*/
Expand Down
50 changes: 3 additions & 47 deletions framework/yii/db/mysql/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace yii\db\mysql;

use yii\db\Exception;
use yii\db\BatchInsertTrait;
use yii\base\InvalidParamException;

/**
Expand All @@ -18,6 +19,8 @@
*/
class QueryBuilder extends \yii\db\QueryBuilder
{
use BatchInsertTrait;

/**
* @var array mapping from abstract column types (keys) to physical column types (values).
*/
Expand Down Expand Up @@ -140,51 +143,4 @@ public function checkIntegrity($check = true, $schema = '', $table = '')
{
return 'SET FOREIGN_KEY_CHECKS = ' . ($check ? 1 : 0);
}

/**
* Generates a batch INSERT SQL statement.
* For example,
*
* ~~~
* $connection->createCommand()->batchInsert('tbl_user', ['name', 'age'], [
* ['Tom', 30],
* ['Jane', 20],
* ['Linda', 25],
* ])->execute();
* ~~~
*
* Note that the values in each row must match the corresponding column names.
*
* @param string $table the table that new rows will be inserted into.
* @param array $columns the column names
* @param array $rows the rows to be batch inserted into the table
* @return string the batch INSERT SQL statement
*/
public function batchInsert($table, $columns, $rows)
{
if (($tableSchema = $this->db->getTableSchema($table)) !== null) {
$columnSchemas = $tableSchema->columns;
} else {
$columnSchemas = [];
}

foreach ($columns as $i => $name) {
$columns[$i] = $this->db->quoteColumnName($name);
}

$values = [];
foreach ($rows as $row) {
$vs = [];
foreach ($row as $i => $value) {
if (!is_array($value) && isset($columnSchemas[$columns[$i]])) {
$value = $columnSchemas[$columns[$i]]->typecast($value);
}
$vs[] = is_string($value) ? $this->db->quoteValue($value) : $value;
}
$values[] = '(' . implode(', ', $vs) . ')';
}

return 'INSERT INTO ' . $this->db->quoteTableName($table)
. ' (' . implode(', ', $columns) . ') VALUES ' . implode(', ', $values);
}
}
3 changes: 3 additions & 0 deletions framework/yii/db/pgsql/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

namespace yii\db\pgsql;

use yii\db\BatchInsertTrait;

/**
* QueryBuilder is the query builder for PostgreSQL databases.
*
Expand All @@ -16,6 +18,7 @@
*/
class QueryBuilder extends \yii\db\QueryBuilder
{
use BatchInsertTrait;

/**
* @var array mapping from abstract column types (keys) to physical column types (values).
Expand Down