From 19648bf98fc8b8ae135505bf7be2bf1fb4cf6613 Mon Sep 17 00:00:00 2001 From: Panagiotis Moustafellos Date: Sat, 23 Nov 2013 22:52:22 +0200 Subject: [PATCH 1/5] added BatchInsert trait --- framework/yii/db/BatchInsertTrait.php | 66 +++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 framework/yii/db/BatchInsertTrait.php diff --git a/framework/yii/db/BatchInsertTrait.php b/framework/yii/db/BatchInsertTrait.php new file mode 100644 index 00000000000..cfd55ce25eb --- /dev/null +++ b/framework/yii/db/BatchInsertTrait.php @@ -0,0 +1,66 @@ + + * @author Carsten Brandt + * @author Panagiotis Moustafellos + * @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); + } +} \ No newline at end of file From 3f95c0b03ea54648794417971231509c5f7f8d1f Mon Sep 17 00:00:00 2001 From: Panagiotis Moustafellos Date: Sat, 23 Nov 2013 22:53:05 +0200 Subject: [PATCH 2/5] BatchInsertTrait for MySQL --- framework/yii/db/mysql/QueryBuilder.php | 50 ++----------------------- 1 file changed, 3 insertions(+), 47 deletions(-) diff --git a/framework/yii/db/mysql/QueryBuilder.php b/framework/yii/db/mysql/QueryBuilder.php index 50e717ccbe6..be8f4ef54da 100644 --- a/framework/yii/db/mysql/QueryBuilder.php +++ b/framework/yii/db/mysql/QueryBuilder.php @@ -8,6 +8,7 @@ namespace yii\db\mysql; use yii\db\Exception; +use yii\db\BatchInsertTrait; use yii\base\InvalidParamException; /** @@ -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). */ @@ -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); - } } From d1e3a73a4a0892c1ce2d6c4d7eda9b8fb88df0e1 Mon Sep 17 00:00:00 2001 From: Panagiotis Moustafellos Date: Sat, 23 Nov 2013 22:53:54 +0200 Subject: [PATCH 3/5] BatchInsertTrait for CUBRID --- framework/yii/db/cubrid/QueryBuilder.php | 50 ++---------------------- 1 file changed, 3 insertions(+), 47 deletions(-) diff --git a/framework/yii/db/cubrid/QueryBuilder.php b/framework/yii/db/cubrid/QueryBuilder.php index e80e1d616df..b9510fd8f89 100644 --- a/framework/yii/db/cubrid/QueryBuilder.php +++ b/framework/yii/db/cubrid/QueryBuilder.php @@ -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). @@ -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). */ @@ -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); - } } From f700a83f2ddf46880de16bad4e0cbb395c186811 Mon Sep 17 00:00:00 2001 From: Panagiotis Moustafellos Date: Sun, 24 Nov 2013 00:04:47 +0200 Subject: [PATCH 4/5] BatchInsertTrait for PostgreSQL --- framework/yii/db/pgsql/QueryBuilder.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/framework/yii/db/pgsql/QueryBuilder.php b/framework/yii/db/pgsql/QueryBuilder.php index 09a620ddd2e..09dc75c6ad9 100644 --- a/framework/yii/db/pgsql/QueryBuilder.php +++ b/framework/yii/db/pgsql/QueryBuilder.php @@ -8,6 +8,8 @@ namespace yii\db\pgsql; +use yii\db\BatchInsertTrait; + /** * QueryBuilder is the query builder for PostgreSQL databases. * @@ -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). From c4bb12fcc22ebb66e46c6ae515de947aa1484309 Mon Sep 17 00:00:00 2001 From: Panagiotis Moustafellos Date: Sun, 24 Nov 2013 00:57:16 +0200 Subject: [PATCH 5/5] BatchInsertTrait for mssql (2008 and upwards) --- framework/yii/db/mssql/QueryBuilder.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/framework/yii/db/mssql/QueryBuilder.php b/framework/yii/db/mssql/QueryBuilder.php index c9bf7ca7345..909a3472736 100644 --- a/framework/yii/db/mssql/QueryBuilder.php +++ b/framework/yii/db/mssql/QueryBuilder.php @@ -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 + * @author Panagiotis Moustafellos * @since 2.0 */ class QueryBuilder extends \yii\db\QueryBuilder { + use BatchInsertTrait; + /** * @var array mapping from abstract column types (keys) to physical column types (values). */