1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58:
<?php
namespace CzProject\SqlGenerator\Statements;
use CzProject\SqlGenerator\Helpers;
use CzProject\SqlGenerator\IDriver;
use CzProject\SqlGenerator\IStatement;
class Insert implements IStatement
{
private $tableName;
private $data;
public function __construct($tableName, array $data)
{
$this->tableName = $tableName;
$this->data = $data;
}
public function toSql(IDriver $driver)
{
$output = 'INSERT INTO ' . $driver->escapeIdentifier($this->tableName);
$output .= ' (';
$output .= implode(', ', array_map(array($driver, 'escapeIdentifier'), array_keys($this->data)));
$output .= ")\nVALUES (";
$fields = count($this->data);
foreach ($this->data as $value) {
$output .= Helpers::formatValue($value, $driver);
$fields--;
if ($fields > 0) {
$output .= ', ';
}
}
$output .= ');';
return $output;
}
}