buildInsertQuery() public method

public buildInsertQuery ( )
Beispiel #1
0
	/**
	 * Inserts row in a table.
	 * @param  array|\Traversable|Selection array($column => $value)|\Traversable|Selection for INSERT ... SELECT
	 * @return IRow|int|bool Returns IRow or number of affected rows for Selection or table without primary key
	 */
	public function insert($data)
	{
		if ($data instanceof self) {
			$data = new Nette\Database\SqlLiteral($data->getSql(), $data->getSqlBuilder()->getParameters());

		} elseif ($data instanceof \Traversable) {
			$data = iterator_to_array($data);
		}

		$return = $this->connection->query($this->sqlBuilder->buildInsertQuery(), $data);
		$this->loadRefCache();

		if ($data instanceof Nette\Database\SqlLiteral || $this->primary === NULL) {
			unset($this->refCache['referencing'][$this->getGeneralCacheKey()][$this->getSpecificCacheKey()]);
			return $return->getRowCount();
		}

		$primaryKey = $this->connection->getInsertId($this->getPrimarySequence());
		if ($primaryKey === FALSE) {
			unset($this->refCache['referencing'][$this->getGeneralCacheKey()][$this->getSpecificCacheKey()]);
			return $return->getRowCount();
		}

		if (is_array($this->getPrimary())) {
			$primaryKey = array();

			foreach ((array) $this->getPrimary() as $key) {
				if (!isset($data[$key])) {
					return $data;
				}

				$primaryKey[$key] = $data[$key];
			}
			if (count($primaryKey) === 1) {
				$primaryKey = reset($primaryKey);
			}
		}

		$row = $this->createSelectionInstance()
			->select('*')
			->wherePrimary($primaryKey)
			->fetch();

		if ($this->rows !== NULL) {
			if ($signature = $row->getSignature(FALSE)) {
				$this->rows[$signature] = $row;
				$this->data[$signature] = $row;
			} else {
				$this->rows[] = $row;
				$this->data[] = $row;
			}
		}

		return $row;
	}
Beispiel #2
0
 /**
  * Inserts row in a table.
  * @param  array|\Traversable|Selection array($column => $value)|\Traversable|Selection for INSERT ... SELECT
  * @return IRow|int|bool Returns IRow or number of affected rows for Selection or table without primary key
  */
 public function insert($data)
 {
     if ($data instanceof self) {
         $return = $this->context->queryArgs($this->sqlBuilder->buildInsertQuery() . ' ' . $data->getSql(), $data->getSqlBuilder()->getParameters());
     } else {
         if ($data instanceof \Traversable) {
             $data = iterator_to_array($data);
         }
         $return = $this->context->query($this->sqlBuilder->buildInsertQuery() . ' ?values', $data);
     }
     $this->loadRefCache();
     if ($data instanceof self || $this->primary === NULL) {
         unset($this->refCache['referencing'][$this->getGeneralCacheKey()][$this->getSpecificCacheKey()]);
         return $return->getRowCount();
     }
     $primarySequenceName = $this->getPrimarySequence();
     $primaryKey = $this->context->getInsertId(!empty($primarySequenceName) ? $this->context->getConnection()->getSupplementalDriver()->delimite($primarySequenceName) : $primarySequenceName);
     if ($primaryKey === FALSE) {
         unset($this->refCache['referencing'][$this->getGeneralCacheKey()][$this->getSpecificCacheKey()]);
         return $return->getRowCount();
     }
     if (is_array($this->getPrimary())) {
         $primaryKey = array();
         foreach ((array) $this->getPrimary() as $key) {
             if (!isset($data[$key])) {
                 return $data;
             }
             $primaryKey[$key] = $data[$key];
         }
         if (count($primaryKey) === 1) {
             $primaryKey = reset($primaryKey);
         }
     }
     $row = $this->createSelectionInstance()->select('*')->wherePrimary($primaryKey)->fetch();
     if ($this->rows !== NULL) {
         if ($signature = $row->getSignature(FALSE)) {
             $this->rows[$signature] = $row;
             $this->data[$signature] = $row;
         } else {
             $this->rows[] = $row;
             $this->data[] = $row;
         }
     }
     return $row;
 }
 /**
  * Inserts row in a table.
  *
  * @param  mixed array($column => $value)|Traversable for single row insert or Selection|string for INSERT ... SELECT
  *
  * @return ActiveRow or FALSE in case of an error or number of affected rows for INSERT ... SELECT
  */
 public function insert($data)
 {
     if ($data instanceof Selection) {
         $data = $data->getSql();
     } elseif ($data instanceof \Traversable) {
         $data = iterator_to_array($data);
     }
     $return = $this->connection->query($this->sqlBuilder->buildInsertQuery(), $data);
     $this->checkReferenced = true;
     if (!is_array($data)) {
         return $return->rowCount();
     }
     if (!isset($data[$this->primary]) && ($id = $this->connection->lastInsertId())) {
         $data[$this->primary] = $id;
         return $this->rows[$id] = $this->createRow($data);
     } else {
         return $this->createRow($data);
     }
 }
 /**
  * Inserts row in a table.
  * @param  mixed array($column => $value)|Traversable for single row insert or Selection|string for INSERT ... SELECT
  * @return ActiveRow or FALSE in case of an error or number of affected rows for INSERT ... SELECT
  */
 public function insert($data)
 {
     if ($data instanceof Selection) {
         $data = $data->getSql();
     } elseif ($data instanceof \Traversable) {
         $data = iterator_to_array($data);
     }
     $return = $this->connection->query($this->sqlBuilder->buildInsertQuery(), $data);
     $this->checkReferenced = TRUE;
     if (!is_array($data)) {
         return $return->rowCount();
     }
     if (!is_array($this->primary) && !isset($data[$this->primary]) && ($id = $this->connection->lastInsertId($this->getPrimarySequence()))) {
         $data[$this->primary] = $id;
     }
     $row = $this->createRow($data);
     if ($signature = $row->getSignature(FALSE)) {
         $this->rows[$signature] = $row;
     }
     return $row;
 }