/**
* Convert the values into a SQL string with placeholders.
*
* @param \Cake\Database\ValueBinder $generator Placeholder generator object
* @return string
*/
public function sql(ValueBinder $generator)
{
if (empty($this->_values) && empty($this->_query)) {
return '';
}
$i = 0;
$defaults = array_fill_keys($this->_columns, null);
foreach ($this->_values as $row) {
$row = array_merge($defaults, $row);
foreach ($row as $column => $value) {
$type = $this->typeMap()->type($column);
$generator->bind($i++, $value, $type);
}
}
if ($this->query()) {
return ' ' . $this->query()->sql($generator);
}
$placeholders = [];
$numColumns = count($this->_columns);
$rowPlaceholders = implode(', ', array_fill(0, $numColumns, '?'));
$placeholders = array_fill(0, count($this->_values), $rowPlaceholders);
return sprintf(' VALUES (%s)', implode('), (', $placeholders));
}