insert() public method

Inserts a row into a table.
public insert ( string $sql, array | string $arg1 = null, string $arg2 = null, string $pk = null, integer $idValue = null, string $sequenceName = null ) : integer
$sql string SQL statement.
$arg1 array | string Either an array of bound parameters or a query name.
$arg2 string If $arg1 contains bound parameters, the query name.
$pk string The primary key column.
$idValue integer The primary key value. This parameter is required if the primary key is inserted manually.
$sequenceName string The sequence name.
return integer Last inserted ID.
コード例 #1
0
ファイル: Pgsql.php プロジェクト: jubinpatel/horde
 /**
  * Inserts a row into a table.
  *
  * @param string $sql           SQL statement.
  * @param array|string $arg1    Either an array of bound parameters or a
  *                              query name.
  * @param string $arg2          If $arg1 contains bound parameters, the
  *                              query name.
  * @param string $pk            The primary key column.
  * @param integer $idValue      The primary key value. This parameter is
  *                              required if the primary key is inserted
  *                              manually.
  * @param string $sequenceName  The sequence name.
  *
  * @return integer  Last inserted ID.
  * @throws Horde_Db_Exception
  */
 public function insert($sql, $arg1 = null, $arg2 = null, $pk = null, $idValue = null, $sequenceName = null)
 {
     // Extract the table from the insert sql. Yuck.
     $temp = explode(' ', trim($sql), 4);
     $table = str_replace('"', '', $temp[2]);
     // Try an insert with 'returning id' if available (PG >= 8.2)
     if ($this->supportsInsertWithReturning()) {
         if (!$pk) {
             list($pk, $sequenceName) = $this->pkAndSequenceFor($table);
         }
         if ($pk) {
             $id = $this->selectValue($sql . ' RETURNING ' . $this->quoteColumnName($pk), $arg1, $arg2);
             $this->resetPkSequence($table, $pk, $sequenceName);
             return $id;
         }
     }
     // If neither pk nor sequence name is given, look them up.
     if (!($pk || $sequenceName)) {
         list($pk, $sequenceName) = $this->pkAndSequenceFor($table);
     }
     // Otherwise, insert then grab last_insert_id.
     if ($insertId = parent::insert($sql, $arg1, $arg2, $pk, $idValue, $sequenceName)) {
         $this->resetPkSequence($table, $pk, $sequenceName);
         return $insertId;
     }
     // If a pk is given, fallback to default sequence name.
     // Don't fetch last insert id for a table without a pk.
     if ($pk && ($sequenceName || ($sequenceName = $this->defaultSequenceName($table, $pk)))) {
         $this->resetPkSequence($table, $pk, $sequenceName);
         return $this->_lastInsertId($table, $sequenceName);
     }
 }