* @package Mammut\DB\Sql */ class Insert extends Query implements iSql { use Targetable; const VALUES = 'values'; protected $columns = array(); protected $values = null; /** * Creates a new INSERT query model * * @param null|string|Table $table */ public function __construct($table = null) { if($table) $this->into($table); } /** * Create INTO clause, alias for the Targetable from() * * @param string|Table $table * @return $this */ public function into($table) { $this->from($table); return $this; } /** * Specify columns * * @param array $columns * @return $this */ public function columns(array $columns) { $this->setup[self::COLUMNS] = $columns; return $this; } /** * Specify values to insert * * @param array|Select $values * @param bool $ignoreKeys * TRUE if the array keys are ignored and only order of elements is important. Setting * this to FALSE also overwrites the currently defined columns. * @throws \InvalidArgumentException * @return $this */ public function values($values, $ignoreKeys = true) { if(!is_array($values) && !$values instanceof Select) throw new \InvalidArgumentException('values() expects an array of values or Mammut\Db\Sql\Select instance'); if($values instanceof Select) { $this->setup[self::VALUES] = $values; return $this; } if (!$ignoreKeys) $this->columns(array_keys($values)); $this->setup[self::VALUES] = array_values($values); return $this; } /** * Create SELECT INTO clause * * @param Select $select * a Select object which should be used as data * @return $this */ public function select(Select $select) { return $this->values($select); } public function getSql(iDialect $dialect = null) { $dialect = $dialect ? $dialect : new \Mammut\DB\Sql\Dialect\Sql92(); $sql = $dialect->getInsertSQL($this->setup); return $sql; } }