Wrapper for insert() and lastInsertId()

Do not use this with the pgsql driver. It is extremely unreliable.

@param  string $table
@param  array  $map
@param  string $sequenceName (optional)
@return string
@throws Issues\QueryError
@throws \Exception
495455
public function insertReturnId(string $table, array $map, string $sequenceName = '')
    {
        if ($this->dbEngine === 'pgsql') {
            throw new \Exception(
                'Do not use insertReturnId() with PostgreSQL. Use insertGet() instead, ' .
                'with an explicit column name rather than a sequence name.'
            );
        }
        if (!$this->insert($table, $map)) {
            throw new Issues\QueryError('Could not insert a new row into ' . $table . '.');
        }
        if ($sequenceName) {
            return $this->lastInsertId($sequenceName);
        }
        return $this->lastInsertId();
    }
1362566
public function insert(string $table, array $map): int
    {
        if (!empty($map)) {
            if (!$this->is1DArray($map)) {
                throw new Issues\MustBeOneDimensionalArray(
                    'Only one-dimensional arrays are allowed.'
                );
            }
        }

        list($queryString, $values) = $this->buildInsertQueryBoolSafe(
            $table,
            $map
        );
        /** @var string $queryString */
        /** @var array $values */

        return (int) $this->safeQuery(
            (string) $queryString,
            $values,
            \PDO::FETCH_BOTH,
            true
        );
    }
930597
public function insertGetId(array $values, $sequence = null)
    {
        $sql = $this->grammar->compileInsertGetId($this, $values, $sequence);

        $values = $this->cleanBindings($values);

        return $this->processor->processInsertGetId($this, $sql, $values, $sequence);
    }
1168154
public function lastInsertId($table = null, $column = null)
    {
        $row = null;
        if ($column && $this->columnCount()) {
            $row = $this->fetch(static::FETCH_TYPE_ASSOC);
        }
        if (isset($row[$column])) {
            return $row[$column];
        }

        return $this->_driver->lastInsertId($table, $column);
    }
351649
public function insert($table, array $data)
    {
        $nameFields = join(',', array_keys($data));
        $preparedValues = $this->prepareValues($data);
        $keyValues = join(",", array_keys($preparedValues));
        $queryString = "INSERT INTO {$table} ({$nameFields}) VALUES ({$keyValues});";

        $this->query($queryString, $preparedValues);

        return $this->getInsertId();
    }
407404
public function processInsertGetId(Builder $query, $sql, $values, $sequence = null)
    {
        $query->getConnection()->insert($sql, $values);

        return $query->getConnection()->lastInsertId();
    }
821579
public function insertId($table, $data)
    {
        try {
            $this->insert($table, $data);
            return $this->sql->lastInsertId();
        } catch (\Exception $e) {
            Throw $e;
        }
    }
448960
public function insertPDO($table, array $data = array())
    {
        if (count($data) === 0) {
            return false;
        }
        $q = "INSERT INTO `$table` ";
        $v = '';
        $n = '';

        foreach ($data as $key => $val) {
            $n .= "`$key`, ";
            if (strtolower($val) === 'now()') {
                $v .= 'NOW(), ';
                unset($data[$key]);
            } else {
                $v .= '?, ';
            }
        }
        $q .= '(' . rtrim($n, ', ') . ') VALUES (' . rtrim($v, ', ') . ');';
        try {
            $this->_queryId = self::$_linkId->prepare($q);

            $i = 1;
            foreach ($data as $key => $val) {
                $type = $this->getPDOConstantType($val);
                $this->_queryId->bindValue($i, $val, $type);
                ++$i;
            }
            $this->_queryId->execute();
            return self::$_linkId->lastInsertId();
        } catch (PDOException $e) {
            $this->_error = $e->getMessage();
            return false;
        }
    }
754389
public function insertGetId(array $values, $sequence = null)
    {
        $result = $this->getBuilder()->insertGetId($values, $sequence);

        // Reset query builder
        $this->resetBuilder();

        return $result;
    }
1069240
public function getSequenceName(Table $table)
    {
        $result = null;
        if ($table->getIdMethod() == IdMethod::NATIVE) {
            $idMethodParams = $table->getIdMethodParameters();
            if (empty($idMethodParams)) {
                $result = null;
                // We're going to ignore a check for max length (mainly
                // because I'm not sure how Postgres would handle this w/ SERIAL anyway)
                foreach ($table->getColumns() as $col) {
                    if ($col->isAutoIncrement()) {
                        $result = $table->getName() . '_' . $col->getName() . '_seq';
                        break; // there's only one auto-increment column allowed
                    }
                }
            } else {
                $result = $idMethodParams[0]->getValue();
            }
        }

        return $result;
    }
245791
public function processInsertGetId(Builder $query, $sql, $values, $sequence = null)
    {
        $connection = $query->getConnection();

        $connection->insert($sql, $values);

        if ($connection->getConfig('odbc') === true) {
            $id = $this->processInsertGetIdForOdbc($connection);
        } else {
            $id = $connection->getPdo()->lastInsertId();
        }

        return is_numeric($id) ? (int) $id : $id;
    }
212200
public function insert($table, array $values = null)
    {
        $builder         = new Insert($this->driver);
        $this->lastQuery = $builder;

        if ($values) {
            $builder->values($values);
        }

        return $builder->into($table);
    }
32448
public function insertGetId(array $values, $sequence = null)
    {
        $this->compileWheres();
        $result = $this->query->insert($values);

        if (0 == (int) $result['errors']) {
            if (isset($values['id'])) {
                return $values['id'];
            }

            // Return id
            return current($result['generated_keys']);
        }
    }
47605
public function lastInsertId($tableName = null, $primaryKey = null)
    {
        $this->_connect();

         if ($tableName !== null) {
            $sequenceName = $tableName;
            if ($primaryKey) {
                $sequenceName .= "_$primaryKey";
            }
            $sequenceName .= '_seq';
            return $this->lastSequenceId($sequenceName);
        }

        $id = $this->getConnection()->lastInsertId();

        return $id;
    }
786114
public function insert ($table_name, $columns) {
		$column_keys = array();
		foreach ($columns as $key => $value) {
			$column_keys[$key] = ":" . $key;
		}

		$query = "INSERT INTO " . $table_name . " (" . implode(", ", array_keys($column_keys)) . ") VALUES (" . implode(", ", array_values($column_keys)) . ")";
		$this -> query($query, $columns, false);
		return $this -> getLastInsertedID();
	}
654968
public function insert($model, $values, $map = null)
    {
        if (is_string($values)) {
            $values = [
                $values => $map,
            ];
            $map = null;
        }

        $this->adapt($model, $this->profile($values));

        $fieldsArray = [];
        $tokensArray = [];
        $valuesArray = [];

        foreach ($values as $field => $value) {
            $field = isset($map[$field]) ? $map[$field] : $field;
            $token = ':'.$field;
            $fieldsArray[] = '`'.$field.'`';
            $tokensArray[] = $token;
            $valuesArray[$token] = $value;
        }

        $fields = implode(',', $fieldsArray);
        $tokens = implode(',', $tokensArray);
        $table = $this->getPrefix($model);
        $sql = "INSERT INTO `{$table}` ({$fields}) VALUES ({$tokens})";
        $this->execute($sql, $valuesArray);

        return $this->getLastId();
    }
1176613
public function getLastInsertId($tableName, $cle)
  {
    $last_insert_id = $this->cnx->lastInsertId($tableName.'_'.$cle.'_seq');
    return $last_insert_id;
  }