diff --git a/src/Database/Connections/MySqlConnection.php b/src/Database/Connections/MySqlConnection.php index 24b87b664..dfa741419 100644 --- a/src/Database/Connections/MySqlConnection.php +++ b/src/Database/Connections/MySqlConnection.php @@ -7,6 +7,7 @@ use Illuminate\Database\Schema\MySqlBuilder; use Illuminate\Database\Schema\MySqlSchemaState; use Illuminate\Filesystem\Filesystem; +use Exception; use PDO; /** @@ -14,6 +15,76 @@ */ class MySqlConnection extends Connection { + /** + * The last inserted ID generated by the server + * + * @var string|int|null + */ + protected $lastInsertId; + + /** + * Run an insert statement against the database. + * + * @param string $query + * @param array $bindings + * @param string|null $sequence + * @return bool + */ + public function insert($query, $bindings = [], $sequence = null) + { + return $this->run($query, $bindings, function ($query, $bindings) use ($sequence) { + if ($this->pretending()) { + return true; + } + + $statement = $this->getPdo()->prepare($query); + + $this->bindValues($statement, $this->prepareBindings($bindings)); + + $this->recordsHaveBeenModified(); + + $result = $statement->execute(); + + $this->lastInsertId = $this->getPdo()->lastInsertId($sequence); + + return $result; + }); + } + + /** + * Get the last insert ID. + * + * @return int + */ + public function getLastInsertId() + { + return $this->lastInsertId; + } + + /** + * Escape a binary value for safe SQL embedding. + * + * @param string $value + * @return string + */ + protected function escapeBinary($value) + { + $hex = bin2hex($value); + + return "x'{$hex}'"; + } + + /** + * Determine if the given database exception was caused by a unique constraint violation. + * + * @param \Exception $exception + * @return bool + */ + protected function isUniqueConstraintError(Exception $exception) + { + return boolval(preg_match('#Integrity constraint violation: 1062#i', $exception->getMessage())); + } + /** * Determine if the connected database is a MariaDB database. * @@ -35,7 +106,7 @@ protected function getDefaultQueryGrammar() if (method_exists($grammar, 'setConnection')) { $grammar->setConnection($this); } - + return $this->withTablePrefix($grammar); }