Commit 25bf486b by Carsten Brandt

save some function calls on AR insert

parent 0a709b10
...@@ -282,7 +282,7 @@ class ActiveRecord extends BaseActiveRecord ...@@ -282,7 +282,7 @@ class ActiveRecord extends BaseActiveRecord
*/ */
public static function getTableSchema() public static function getTableSchema()
{ {
$schema = static::getDb()->getTableSchema(static::tableName()); $schema = static::getDb()->getSchema()->getTableSchema(static::tableName());
if ($schema !== null) { if ($schema !== null) {
return $schema; return $schema;
} else { } else {
......
...@@ -131,7 +131,8 @@ class QueryBuilder extends \yii\base\Object ...@@ -131,7 +131,8 @@ class QueryBuilder extends \yii\base\Object
*/ */
public function insert($table, $columns, &$params) public function insert($table, $columns, &$params)
{ {
if (($tableSchema = $this->db->getTableSchema($table)) !== null) { $schema = $this->db->getSchema();
if (($tableSchema = $schema->getTableSchema($table)) !== null) {
$columnSchemas = $tableSchema->columns; $columnSchemas = $tableSchema->columns;
} else { } else {
$columnSchemas = []; $columnSchemas = [];
...@@ -139,7 +140,7 @@ class QueryBuilder extends \yii\base\Object ...@@ -139,7 +140,7 @@ class QueryBuilder extends \yii\base\Object
$names = []; $names = [];
$placeholders = []; $placeholders = [];
foreach ($columns as $name => $value) { foreach ($columns as $name => $value) {
$names[] = $this->db->quoteColumnName($name); $names[] = $schema->quoteColumnName($name);
if ($value instanceof Expression) { if ($value instanceof Expression) {
$placeholders[] = $value->expression; $placeholders[] = $value->expression;
foreach ($value->params as $n => $v) { foreach ($value->params as $n => $v) {
...@@ -152,7 +153,7 @@ class QueryBuilder extends \yii\base\Object ...@@ -152,7 +153,7 @@ class QueryBuilder extends \yii\base\Object
} }
} }
return 'INSERT INTO ' . $this->db->quoteTableName($table) return 'INSERT INTO ' . $schema->quoteTableName($table)
. ' (' . implode(', ', $names) . ') VALUES (' . ' (' . implode(', ', $names) . ') VALUES ('
. implode(', ', $placeholders) . ')'; . implode(', ', $placeholders) . ')';
} }
...@@ -178,7 +179,8 @@ class QueryBuilder extends \yii\base\Object ...@@ -178,7 +179,8 @@ class QueryBuilder extends \yii\base\Object
*/ */
public function batchInsert($table, $columns, $rows) public function batchInsert($table, $columns, $rows)
{ {
if (($tableSchema = $this->db->getTableSchema($table)) !== null) { $schema = $this->db->getSchema();
if (($tableSchema = $schema->getTableSchema($table)) !== null) {
$columnSchemas = $tableSchema->columns; $columnSchemas = $tableSchema->columns;
} else { } else {
$columnSchemas = []; $columnSchemas = [];
...@@ -192,7 +194,7 @@ class QueryBuilder extends \yii\base\Object ...@@ -192,7 +194,7 @@ class QueryBuilder extends \yii\base\Object
$value = $columnSchemas[$columns[$i]]->dbTypecast($value); $value = $columnSchemas[$columns[$i]]->dbTypecast($value);
} }
if (is_string($value)) { if (is_string($value)) {
$value = $this->db->quoteValue($value); $value = $schema->quoteValue($value);
} elseif ($value === false) { } elseif ($value === false) {
$value = 0; $value = 0;
} elseif ($value === null) { } elseif ($value === null) {
...@@ -204,10 +206,10 @@ class QueryBuilder extends \yii\base\Object ...@@ -204,10 +206,10 @@ class QueryBuilder extends \yii\base\Object
} }
foreach ($columns as $i => $name) { foreach ($columns as $i => $name) {
$columns[$i] = $this->db->quoteColumnName($name); $columns[$i] = $schema->quoteColumnName($name);
} }
return 'INSERT INTO ' . $this->db->quoteTableName($table) return 'INSERT INTO ' . $schema->quoteTableName($table)
. ' (' . implode(', ', $columns) . ') VALUES ' . implode(', ', $values); . ' (' . implode(', ', $columns) . ') VALUES ' . implode(', ', $values);
} }
......
...@@ -128,8 +128,8 @@ class QueryBuilder extends \yii\db\QueryBuilder ...@@ -128,8 +128,8 @@ class QueryBuilder extends \yii\db\QueryBuilder
public function checkIntegrity($check = true, $schema = '', $table = '') public function checkIntegrity($check = true, $schema = '', $table = '')
{ {
$enable = $check ? 'ENABLE' : 'DISABLE'; $enable = $check ? 'ENABLE' : 'DISABLE';
$schema = $schema ? $schema : $this->db->schema->defaultSchema; $schema = $schema ? $schema : $this->db->getSchema()->defaultSchema;
$tableNames = $table ? [$table] : $this->db->schema->getTableNames($schema); $tableNames = $table ? [$table] : $this->db->getSchema()->getTableNames($schema);
$command = ''; $command = '';
foreach ($tableNames as $tableName) { foreach ($tableNames as $tableName) {
...@@ -165,7 +165,8 @@ class QueryBuilder extends \yii\db\QueryBuilder ...@@ -165,7 +165,8 @@ class QueryBuilder extends \yii\db\QueryBuilder
*/ */
public function batchInsert($table, $columns, $rows) public function batchInsert($table, $columns, $rows)
{ {
if (($tableSchema = $this->db->getTableSchema($table)) !== null) { $schema = $this->db->getSchema();
if (($tableSchema = $schema->getTableSchema($table)) !== null) {
$columnSchemas = $tableSchema->columns; $columnSchemas = $tableSchema->columns;
} else { } else {
$columnSchemas = []; $columnSchemas = [];
...@@ -179,7 +180,7 @@ class QueryBuilder extends \yii\db\QueryBuilder ...@@ -179,7 +180,7 @@ class QueryBuilder extends \yii\db\QueryBuilder
$value = $columnSchemas[$columns[$i]]->dbTypecast($value); $value = $columnSchemas[$columns[$i]]->dbTypecast($value);
} }
if (is_string($value)) { if (is_string($value)) {
$value = $this->db->quoteValue($value); $value = $schema->quoteValue($value);
} elseif ($value === true) { } elseif ($value === true) {
$value = 'TRUE'; $value = 'TRUE';
} elseif ($value === false) { } elseif ($value === false) {
...@@ -193,10 +194,10 @@ class QueryBuilder extends \yii\db\QueryBuilder ...@@ -193,10 +194,10 @@ class QueryBuilder extends \yii\db\QueryBuilder
} }
foreach ($columns as $i => $name) { foreach ($columns as $i => $name) {
$columns[$i] = $this->db->quoteColumnName($name); $columns[$i] = $schema->quoteColumnName($name);
} }
return 'INSERT INTO ' . $this->db->quoteTableName($table) return 'INSERT INTO ' . $schema->quoteTableName($table)
. ' (' . implode(', ', $columns) . ') VALUES ' . implode(', ', $values); . ' (' . implode(', ', $columns) . ') VALUES ' . implode(', ', $values);
} }
} }
...@@ -70,7 +70,8 @@ class QueryBuilder extends \yii\db\QueryBuilder ...@@ -70,7 +70,8 @@ class QueryBuilder extends \yii\db\QueryBuilder
return parent::batchInsert($table, $columns, $rows); return parent::batchInsert($table, $columns, $rows);
} }
if (($tableSchema = $this->db->getTableSchema($table)) !== null) { $schema = $this->db->getSchema();
if (($tableSchema = $schema->getTableSchema($table)) !== null) {
$columnSchemas = $tableSchema->columns; $columnSchemas = $tableSchema->columns;
} else { } else {
$columnSchemas = []; $columnSchemas = [];
...@@ -84,7 +85,7 @@ class QueryBuilder extends \yii\db\QueryBuilder ...@@ -84,7 +85,7 @@ class QueryBuilder extends \yii\db\QueryBuilder
$value = $columnSchemas[$columns[$i]]->dbTypecast($value); $value = $columnSchemas[$columns[$i]]->dbTypecast($value);
} }
if (is_string($value)) { if (is_string($value)) {
$value = $this->db->quoteValue($value); $value = $schema->quoteValue($value);
} elseif ($value === false) { } elseif ($value === false) {
$value = 0; $value = 0;
} elseif ($value === null) { } elseif ($value === null) {
...@@ -96,10 +97,10 @@ class QueryBuilder extends \yii\db\QueryBuilder ...@@ -96,10 +97,10 @@ class QueryBuilder extends \yii\db\QueryBuilder
} }
foreach ($columns as $i => $name) { foreach ($columns as $i => $name) {
$columns[$i] = $this->db->quoteColumnName($name); $columns[$i] = $schema->quoteColumnName($name);
} }
return 'INSERT INTO ' . $this->db->quoteTableName($table) return 'INSERT INTO ' . $schema->quoteTableName($table)
. ' (' . implode(', ', $columns) . ') SELECT ' . implode(' UNION SELECT ', $values); . ' (' . implode(', ', $columns) . ') SELECT ' . implode(' UNION SELECT ', $values);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment