Skip to content

Commit

Permalink
Merge pull request #3 from pvsaintpe/fix-procedure
Browse files Browse the repository at this point in the history
add call procedure + insert from select
  • Loading branch information
pvsaintpe committed Mar 4, 2019
2 parents 31a64d9 + 333ad42 commit 5ce0ca7
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions components/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -396,4 +396,63 @@ public function cloneTable($tableName, $sourceTable, $sourceSchema = null)
}
$this->execute("CREATE TABLE `{$tableName}` LIKE {$source}");
}

/**
* Call procedure
* @param $name
* @param array $params
* @param int|null $fetchMode
* @throws
*/
public function call($name, array $params = [], $fetchMode = null)
{
$arguments = join(', :', array_keys($params));
$this->execute("CALL $name(:{$arguments})", $params);
}

/**
* @param string $table
* @param array $where
* @param array $replace
* @param array $remove
* @throws
*/
public function insertFrom($table, $where, $replace, $remove = [])
{
$alias = 'w';

/** @var TableSchema $tableSchema */
$tableSchema = $this->getTableSchema($table);

$columns = array_diff($tableSchema->getColumnNames(), $remove);

$values = [];
foreach ($columns as $column) {
if (array_key_exists($column, $replace)) {
$values[] = $this->quoteValue($replace[$column]);
continue;
}
$values[] = $alias . '.' . $column;
}

$conditions = [];
foreach ($where as $attribute => $condition) {
if (!is_array($condition)) {
$conditions[] = $alias . '.' . $attribute . ' = ' . $this->quoteValue($condition);
} else {
if (count($condition) > 0) {
$conditions[] = $alias . '.' . $attribute . ' IN (' . implode(', ', $condition) . ')';
}
}
}

$sql = "
INSERT INTO `{$table}` (" . join(', ', $columns) . ")
SELECT " . join(', ', $values) . "
FROM `{$table}` `{$alias}`
WHERE " . join(' AND ', $conditions) . "
";

$this->execute($sql);
}
}

0 comments on commit 5ce0ca7

Please sign in to comment.