/** * 执行事务 * * @param \Closure $callback * @return mixed * * @throws \Exception|\Throwable */ public function transaction(Closure $callback) { if ($this->getDriverName() == 'sqlsrv') { return parent::transaction($callback); } $this->getPdo()->exec('BEGIN TRAN'); try { $result = $callback($this); $this->getPdo()->exec('COMMIT TRAN'); } catch (Exception $e) { $this->getPdo()->exec('ROLLBACK TRAN'); throw $e; } catch (Throwable $e) { $this->getPdo()->exec('ROLLBACK TRAN'); throw $e; } return $result; }
/** * 运行查询连接器 * * @return array */ protected function runSelect() { return $this->connection->select($this->toSql(), $this->getBindings(), !$this->useWritePdo); }
/** * 准备数据库链接实例 * * @param \CatLib\Database\Connections\Connection $connection * @return \CatLib\Database\Connections\Connection */ protected function prepare(Connection $connection) { if (isset($this->app['env']['database']['fetch'])) { $connection->setFetchMode($this->app['env']['database']['fetch']); } if ($this->app->isExists('events')) { $connection->setEventDispatcher($this->app['events']); } $connection->setReconnector(function ($connection) { $this->reconnect($connection->getName()); }); return $connection; }
/** * 创建新的事件实例 * * @param \CatLib\Database\Connections\Connection $connection * @return void */ public function __construct($connection) { $this->connection = $connection; $this->connectionName = $connection->getName(); }