Guest User

connection.php

a guest
Nov 22nd, 2021
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 23.75 KB | None | 0 0
  1. <?php namespace Illuminate\Database;
  2.  
  3. use PDO;
  4. use Closure;
  5. use DateTime;
  6. use Exception;
  7. use LogicException;
  8. use RuntimeException;
  9. use Illuminate\Contracts\Events\Dispatcher;
  10. use Illuminate\Database\Query\Processors\Processor;
  11. use Doctrine\DBAL\Connection as DoctrineConnection;
  12.  
  13. class Connection implements ConnectionInterface {
  14.  
  15. /**
  16. * The active PDO connection.
  17. *
  18. * @var PDO
  19. */
  20. protected $pdo;
  21.  
  22. /**
  23. * The active PDO connection used for reads.
  24. *
  25. * @var PDO
  26. */
  27. protected $readPdo;
  28.  
  29. /**
  30. * The reconnector instance for the connection.
  31. *
  32. * @var callable
  33. */
  34. protected $reconnector;
  35.  
  36. /**
  37. * The query grammar implementation.
  38. *
  39. * @var \Illuminate\Database\Query\Grammars\Grammar
  40. */
  41. protected $queryGrammar;
  42.  
  43. /**
  44. * The schema grammar implementation.
  45. *
  46. * @var \Illuminate\Database\Schema\Grammars\Grammar
  47. */
  48. protected $schemaGrammar;
  49.  
  50. /**
  51. * The query post processor implementation.
  52. *
  53. * @var \Illuminate\Database\Query\Processors\Processor
  54. */
  55. protected $postProcessor;
  56.  
  57. /**
  58. * The event dispatcher instance.
  59. *
  60. * @var \Illuminate\Contracts\Events\Dispatcher
  61. */
  62. protected $events;
  63.  
  64. /**
  65. * The default fetch mode of the connection.
  66. *
  67. * @var int
  68. */
  69. protected $fetchMode = PDO::FETCH_ASSOC;
  70.  
  71. /**
  72. * The number of active transactions.
  73. *
  74. * @var int
  75. */
  76. protected $transactions = 0;
  77.  
  78. /**
  79. * All of the queries run against the connection.
  80. *
  81. * @var array
  82. */
  83. protected $queryLog = array();
  84.  
  85. /**
  86. * Indicates whether queries are being logged.
  87. *
  88. * @var bool
  89. */
  90. protected $loggingQueries = false;
  91.  
  92. /**
  93. * Indicates if the connection is in a "dry run".
  94. *
  95. * @var bool
  96. */
  97. protected $pretending = false;
  98.  
  99. /**
  100. * The name of the connected database.
  101. *
  102. * @var string
  103. */
  104. protected $database;
  105.  
  106. /**
  107. * The table prefix for the connection.
  108. *
  109. * @var string
  110. */
  111. protected $tablePrefix = '';
  112.  
  113. /**
  114. * The database connection configuration options.
  115. *
  116. * @var array
  117. */
  118. protected $config = array();
  119.  
  120. /**
  121. * Create a new database connection instance.
  122. *
  123. * @param \PDO $pdo
  124. * @param string $database
  125. * @param string $tablePrefix
  126. * @param array $config
  127. * @return void
  128. */
  129. public function __construct(PDO $pdo, $database = '', $tablePrefix = '', array $config = array())
  130. {
  131. $this->pdo = $pdo;
  132.  
  133. // First we will setup the default properties. We keep track of the DB
  134. // name we are connected to since it is needed when some reflective
  135. // type commands are run such as checking whether a table exists.
  136. $this->database = $database;
  137.  
  138. $this->tablePrefix = $tablePrefix;
  139.  
  140. $this->config = $config;
  141.  
  142. // We need to initialize a query grammar and the query post processors
  143. // which are both very important parts of the database abstractions
  144. // so we initialize these to their default values while starting.
  145. $this->useDefaultQueryGrammar();
  146.  
  147. $this->useDefaultPostProcessor();
  148. }
  149.  
  150. /**
  151. * Set the query grammar to the default implementation.
  152. *
  153. * @return void
  154. */
  155. public function useDefaultQueryGrammar()
  156. {
  157. $this->queryGrammar = $this->getDefaultQueryGrammar();
  158. }
  159.  
  160. /**
  161. * Get the default query grammar instance.
  162. *
  163. * @return \Illuminate\Database\Query\Grammars\Grammar
  164. */
  165. protected function getDefaultQueryGrammar()
  166. {
  167. return new Query\Grammars\Grammar;
  168. }
  169.  
  170. /**
  171. * Set the schema grammar to the default implementation.
  172. *
  173. * @return void
  174. */
  175. public function useDefaultSchemaGrammar()
  176. {
  177. $this->schemaGrammar = $this->getDefaultSchemaGrammar();
  178. }
  179.  
  180. /**
  181. * Get the default schema grammar instance.
  182. *
  183. * @return \Illuminate\Database\Schema\Grammars\Grammar
  184. */
  185. protected function getDefaultSchemaGrammar() {}
  186.  
  187. /**
  188. * Set the query post processor to the default implementation.
  189. *
  190. * @return void
  191. */
  192. public function useDefaultPostProcessor()
  193. {
  194. $this->postProcessor = $this->getDefaultPostProcessor();
  195. }
  196.  
  197. /**
  198. * Get the default post processor instance.
  199. *
  200. * @return \Illuminate\Database\Query\Processors\Processor
  201. */
  202. protected function getDefaultPostProcessor()
  203. {
  204. return new Query\Processors\Processor;
  205. }
  206.  
  207. /**
  208. * Get a schema builder instance for the connection.
  209. *
  210. * @return \Illuminate\Database\Schema\Builder
  211. */
  212. public function getSchemaBuilder()
  213. {
  214. if (is_null($this->schemaGrammar)) { $this->useDefaultSchemaGrammar(); }
  215.  
  216. return new Schema\Builder($this);
  217. }
  218.  
  219. /**
  220. * Begin a fluent query against a database table.
  221. *
  222. * @param string $table
  223. * @return \Illuminate\Database\Query\Builder
  224. */
  225. public function table($table)
  226. {
  227. $processor = $this->getPostProcessor();
  228.  
  229. $query = new Query\Builder($this, $this->getQueryGrammar(), $processor);
  230.  
  231. return $query->from($table);
  232. }
  233.  
  234. /**
  235. * Get a new raw query expression.
  236. *
  237. * @param mixed $value
  238. * @return \Illuminate\Database\Query\Expression
  239. */
  240. public function raw($value)
  241. {
  242. return new Query\Expression($value);
  243. }
  244.  
  245. /**
  246. * Run a select statement and return a single result.
  247. *
  248. * @param string $query
  249. * @param array $bindings
  250. * @return mixed
  251. */
  252. public function selectOne($query, $bindings = array())
  253. {
  254. $records = $this->select($query, $bindings);
  255.  
  256. return count($records) > 0 ? reset($records) : null;
  257. }
  258.  
  259. /**
  260. * Run a select statement against the database.
  261. *
  262. * @param string $query
  263. * @param array $bindings
  264. * @return array
  265. */
  266. public function selectFromWriteConnection($query, $bindings = array())
  267. {
  268. return $this->select($query, $bindings, false);
  269. }
  270.  
  271. /**
  272. * Run a select statement against the database.
  273. *
  274. * @param string $query
  275. * @param array $bindings
  276. * @param bool $useReadPdo
  277. * @return array
  278. */
  279. public function select($query, $bindings = array(), $useReadPdo = true)
  280. {
  281. return $this->run($query, $bindings, function($me, $query, $bindings) use ($useReadPdo)
  282. {
  283. if ($me->pretending()) return array();
  284.  
  285. // For select statements, we'll simply execute the query and return an array
  286. // of the database result set. Each element in the array will be a single
  287. // row from the database table, and will either be an array or objects.
  288. $statement = $this->getPdoForSelect($useReadPdo)->prepare($query);
  289.  
  290. $statement->execute($me->prepareBindings($bindings));
  291.  
  292. return $statement->fetchAll($me->getFetchMode());
  293. });
  294. }
  295.  
  296. /**
  297. * Get the PDO connection to use for a select query.
  298. *
  299. * @param bool $useReadPdo
  300. * @return \PDO
  301. */
  302. protected function getPdoForSelect($useReadPdo = true)
  303. {
  304. return $useReadPdo ? $this->getReadPdo() : $this->getPdo();
  305. }
  306.  
  307. /**
  308. * Run an insert statement against the database.
  309. *
  310. * @param string $query
  311. * @param array $bindings
  312. * @return bool
  313. */
  314. public function insert($query, $bindings = array())
  315. {
  316. return $this->statement($query, $bindings);
  317. }
  318.  
  319. /**
  320. * Run an update statement against the database.
  321. *
  322. * @param string $query
  323. * @param array $bindings
  324. * @return int
  325. */
  326. public function update($query, $bindings = array())
  327. {
  328. return $this->affectingStatement($query, $bindings);
  329. }
  330.  
  331. /**
  332. * Run a delete statement against the database.
  333. *
  334. * @param string $query
  335. * @param array $bindings
  336. * @return int
  337. */
  338. public function delete($query, $bindings = array())
  339. {
  340. return $this->affectingStatement($query, $bindings);
  341. }
  342.  
  343. /**
  344. * Execute an SQL statement and return the boolean result.
  345. *
  346. * @param string $query
  347. * @param array $bindings
  348. * @return bool
  349. */
  350. public function statement($query, $bindings = array())
  351. {
  352. return $this->run($query, $bindings, function($me, $query, $bindings)
  353. {
  354. if ($me->pretending()) return true;
  355.  
  356. $bindings = $me->prepareBindings($bindings);
  357.  
  358. return $me->getPdo()->prepare($query)->execute($bindings);
  359. });
  360. }
  361.  
  362. /**
  363. * Run an SQL statement and get the number of rows affected.
  364. *
  365. * @param string $query
  366. * @param array $bindings
  367. * @return int
  368. */
  369. public function affectingStatement($query, $bindings = array())
  370. {
  371. return $this->run($query, $bindings, function($me, $query, $bindings)
  372. {
  373. if ($me->pretending()) return 0;
  374.  
  375. // For update or delete statements, we want to get the number of rows affected
  376. // by the statement and return that back to the developer. We'll first need
  377. // to execute the statement and then we'll use PDO to fetch the affected.
  378. $statement = $me->getPdo()->prepare($query);
  379.  
  380. $statement->execute($me->prepareBindings($bindings));
  381.  
  382. return $statement->rowCount();
  383. });
  384. }
  385.  
  386. /**
  387. * Run a raw, unprepared query against the PDO connection.
  388. *
  389. * @param string $query
  390. * @return bool
  391. */
  392. public function unprepared($query)
  393. {
  394. return $this->run($query, array(), function($me, $query)
  395. {
  396. if ($me->pretending()) return true;
  397.  
  398. return (bool) $me->getPdo()->exec($query);
  399. });
  400. }
  401.  
  402. /**
  403. * Prepare the query bindings for execution.
  404. *
  405. * @param array $bindings
  406. * @return array
  407. */
  408. public function prepareBindings(array $bindings)
  409. {
  410. $grammar = $this->getQueryGrammar();
  411.  
  412. foreach ($bindings as $key => $value)
  413. {
  414. // We need to transform all instances of the DateTime class into an actual
  415. // date string. Each query grammar maintains its own date string format
  416. // so we'll just ask the grammar for the format to get from the date.
  417. if ($value instanceof DateTime)
  418. {
  419. $bindings[$key] = $value->format($grammar->getDateFormat());
  420. }
  421. elseif ($value === false)
  422. {
  423. $bindings[$key] = 0;
  424. }
  425. }
  426.  
  427. return $bindings;
  428. }
  429.  
  430. /**
  431. * Execute a Closure within a transaction.
  432. *
  433. * @param \Closure $callback
  434. * @return mixed
  435. *
  436. * @throws \Exception
  437. */
  438. public function transaction(Closure $callback)
  439. {
  440. $this->beginTransaction();
  441.  
  442. // We'll simply execute the given callback within a try / catch block
  443. // and if we catch any exception we can rollback the transaction
  444. // so that none of the changes are persisted to the database.
  445. try
  446. {
  447. $result = $callback($this);
  448.  
  449. $this->commit();
  450. }
  451.  
  452. // If we catch an exception, we will roll back so nothing gets messed
  453. // up in the database. Then we'll re-throw the exception so it can
  454. // be handled how the developer sees fit for their applications.
  455. catch (Exception $e)
  456. {
  457. $this->rollBack();
  458.  
  459. throw $e;
  460. }
  461.  
  462. return $result;
  463. }
  464.  
  465. /**
  466. * Start a new database transaction.
  467. *
  468. * @return void
  469. */
  470. public function beginTransaction()
  471. {
  472. ++$this->transactions;
  473.  
  474. if ($this->transactions == 1)
  475. {
  476. $this->pdo->beginTransaction();
  477. }
  478.  
  479. $this->fireConnectionEvent('beganTransaction');
  480. }
  481.  
  482. /**
  483. * Commit the active database transaction.
  484. *
  485. * @return void
  486. */
  487. public function commit()
  488. {
  489. if ($this->transactions == 1) $this->pdo->commit();
  490.  
  491. --$this->transactions;
  492.  
  493. $this->fireConnectionEvent('committed');
  494. }
  495.  
  496. /**
  497. * Rollback the active database transaction.
  498. *
  499. * @return void
  500. */
  501. public function rollBack()
  502. {
  503. if ($this->transactions == 1)
  504. {
  505. $this->transactions = 0;
  506.  
  507. $this->pdo->rollBack();
  508. }
  509. else
  510. {
  511. --$this->transactions;
  512. }
  513.  
  514. $this->fireConnectionEvent('rollingBack');
  515. }
  516.  
  517. /**
  518. * Get the number of active transactions.
  519. *
  520. * @return int
  521. */
  522. public function transactionLevel()
  523. {
  524. return $this->transactions;
  525. }
  526.  
  527. /**
  528. * Execute the given callback in "dry run" mode.
  529. *
  530. * @param \Closure $callback
  531. * @return array
  532. */
  533. public function pretend(Closure $callback)
  534. {
  535. $loggingQueries = $this->loggingQueries;
  536.  
  537. $this->enableQueryLog();
  538.  
  539. $this->pretending = true;
  540.  
  541. $this->queryLog = [];
  542.  
  543. // Basically to make the database connection "pretend", we will just return
  544. // the default values for all the query methods, then we will return an
  545. // array of queries that were "executed" within the Closure callback.
  546. $callback($this);
  547.  
  548. $this->pretending = false;
  549.  
  550. $this->loggingQueries = $loggingQueries;
  551.  
  552. return $this->queryLog;
  553. }
  554.  
  555. /**
  556. * Run a SQL statement and log its execution context.
  557. *
  558. * @param string $query
  559. * @param array $bindings
  560. * @param \Closure $callback
  561. * @return mixed
  562. *
  563. * @throws \Illuminate\Database\QueryException
  564. */
  565. protected function run($query, $bindings, Closure $callback)
  566. {
  567. $this->reconnectIfMissingConnection();
  568.  
  569. $start = microtime(true);
  570.  
  571. // Here we will run this query. If an exception occurs we'll determine if it was
  572. // caused by a connection that has been lost. If that is the cause, we'll try
  573. // to re-establish connection and re-run the query with a fresh connection.
  574. try
  575. {
  576. $result = $this->runQueryCallback($query, $bindings, $callback);
  577. }
  578. catch (QueryException $e)
  579. {
  580. $result = $this->tryAgainIfCausedByLostConnection(
  581. $e, $query, $bindings, $callback
  582. );
  583. }
  584.  
  585. // Once we have run the query we will calculate the time that it took to run and
  586. // then log the query, bindings, and execution time so we will report them on
  587. // the event that the developer needs them. We'll log time in milliseconds.
  588. $time = $this->getElapsedTime($start);
  589.  
  590. $this->logQuery($query, $bindings, $time);
  591.  
  592. return $result;
  593. }
  594.  
  595. /**
  596. * Run a SQL statement.
  597. *
  598. * @param string $query
  599. * @param array $bindings
  600. * @param \Closure $callback
  601. * @return mixed
  602. *
  603. * @throws \Illuminate\Database\QueryException
  604. */
  605. protected function runQueryCallback($query, $bindings, Closure $callback)
  606. {
  607. // To execute the statement, we'll simply call the callback, which will actually
  608. // run the SQL against the PDO connection. Then we can calculate the time it
  609. // took to execute and log the query SQL, bindings and time in our memory.
  610. try
  611. {
  612. $result = $callback($this, $query, $bindings);
  613. }
  614.  
  615. // If an exception occurs when attempting to run a query, we'll format the error
  616. // message to include the bindings with SQL, which will make this exception a
  617. // lot more helpful to the developer instead of just the database's errors.
  618. catch (Exception $e)
  619. {
  620. throw new QueryException(
  621. $query, $this->prepareBindings($bindings), $e
  622. );
  623. }
  624.  
  625. return $result;
  626. }
  627.  
  628. /**
  629. * Handle a query exception that occurred during query execution.
  630. *
  631. * @param \Illuminate\Database\QueryException $e
  632. * @param string $query
  633. * @param array $bindings
  634. * @param \Closure $callback
  635. * @return mixed
  636. *
  637. * @throws \Illuminate\Database\QueryException
  638. */
  639. protected function tryAgainIfCausedByLostConnection(QueryException $e, $query, $bindings, Closure $callback)
  640. {
  641. if ($this->causedByLostConnection($e))
  642. {
  643. $this->reconnect();
  644.  
  645. return $this->runQueryCallback($query, $bindings, $callback);
  646. }
  647.  
  648. throw $e;
  649. }
  650.  
  651. /**
  652. * Determine if the given exception was caused by a lost connection.
  653. *
  654. * @param \Illuminate\Database\QueryException $e
  655. * @return bool
  656. */
  657. protected function causedByLostConnection(QueryException $e)
  658. {
  659. $message = $e->getPrevious()->getMessage();
  660.  
  661. return str_contains($message, [
  662. 'server has gone away',
  663. 'no connection to the server',
  664. 'Lost connection',
  665. ]);
  666. }
  667.  
  668. /**
  669. * Disconnect from the underlying PDO connection.
  670. *
  671. * @return void
  672. */
  673. public function disconnect()
  674. {
  675. $this->setPdo(null)->setReadPdo(null);
  676. }
  677.  
  678. /**
  679. * Reconnect to the database.
  680. *
  681. * @return void
  682. *
  683. * @throws \LogicException
  684. */
  685. public function reconnect()
  686. {
  687. if (is_callable($this->reconnector))
  688. {
  689. return call_user_func($this->reconnector, $this);
  690. }
  691.  
  692. throw new LogicException("Lost connection and no reconnector available.");
  693. }
  694.  
  695. /**
  696. * Reconnect to the database if a PDO connection is missing.
  697. *
  698. * @return void
  699. */
  700. protected function reconnectIfMissingConnection()
  701. {
  702. if (is_null($this->getPdo()) || is_null($this->getReadPdo()))
  703. {
  704. $this->reconnect();
  705. }
  706. }
  707.  
  708. /**
  709. * Log a query in the connection's query log.
  710. *
  711. * @param string $query
  712. * @param array $bindings
  713. * @param float|null $time
  714. * @return void
  715. */
  716. public function logQuery($query, $bindings, $time = null)
  717. {
  718. if (isset($this->events))
  719. {
  720. $this->events->fire('illuminate.query', array($query, $bindings, $time, $this->getName()));
  721. }
  722.  
  723. if ( ! $this->loggingQueries) return;
  724.  
  725. $this->queryLog[] = compact('query', 'bindings', 'time');
  726. }
  727.  
  728. /**
  729. * Register a database query listener with the connection.
  730. *
  731. * @param \Closure $callback
  732. * @return void
  733. */
  734. public function listen(Closure $callback)
  735. {
  736. if (isset($this->events))
  737. {
  738. $this->events->listen('illuminate.query', $callback);
  739. }
  740. }
  741.  
  742. /**
  743. * Fire an event for this connection.
  744. *
  745. * @param string $event
  746. * @return void
  747. */
  748. protected function fireConnectionEvent($event)
  749. {
  750. if (isset($this->events))
  751. {
  752. $this->events->fire('connection.'.$this->getName().'.'.$event, $this);
  753. }
  754. }
  755.  
  756. /**
  757. * Get the elapsed time since a given starting point.
  758. *
  759. * @param int $start
  760. * @return float
  761. */
  762. protected function getElapsedTime($start)
  763. {
  764. return round((microtime(true) - $start) * 1000, 2);
  765. }
  766.  
  767. /**
  768. * Get a Doctrine Schema Column instance.
  769. *
  770. * @param string $table
  771. * @param string $column
  772. * @return \Doctrine\DBAL\Schema\Column
  773. */
  774. public function getDoctrineColumn($table, $column)
  775. {
  776. $schema = $this->getDoctrineSchemaManager();
  777.  
  778. return $schema->listTableDetails($table)->getColumn($column);
  779. }
  780.  
  781. /**
  782. * Get the Doctrine DBAL schema manager for the connection.
  783. *
  784. * @return \Doctrine\DBAL\Schema\AbstractSchemaManager
  785. */
  786. public function getDoctrineSchemaManager()
  787. {
  788. return $this->getDoctrineDriver()->getSchemaManager($this->getDoctrineConnection());
  789. }
  790.  
  791. /**
  792. * Get the Doctrine DBAL database connection instance.
  793. *
  794. * @return \Doctrine\DBAL\Connection
  795. */
  796. public function getDoctrineConnection()
  797. {
  798. $driver = $this->getDoctrineDriver();
  799.  
  800. $data = array('pdo' => $this->pdo, 'dbname' => $this->getConfig('database'));
  801.  
  802. return new DoctrineConnection($data, $driver);
  803. }
  804.  
  805. /**
  806. * Get the current PDO connection.
  807. *
  808. * @return \PDO
  809. */
  810. public function getPdo()
  811. {
  812. return $this->pdo;
  813. }
  814.  
  815. /**
  816. * Get the current PDO connection used for reading.
  817. *
  818. * @return \PDO
  819. */
  820. public function getReadPdo()
  821. {
  822. if ($this->transactions >= 1) return $this->getPdo();
  823.  
  824. return $this->readPdo ?: $this->pdo;
  825. }
  826.  
  827. /**
  828. * Set the PDO connection.
  829. *
  830. * @param \PDO|null $pdo
  831. * @return $this
  832. */
  833. public function setPdo($pdo)
  834. {
  835. if ($this->transactions >= 1)
  836. throw new RuntimeException("Can't swap PDO instance while within transaction.");
  837.  
  838. $this->pdo = $pdo;
  839.  
  840. return $this;
  841. }
  842.  
  843. /**
  844. * Set the PDO connection used for reading.
  845. *
  846. * @param \PDO|null $pdo
  847. * @return $this
  848. */
  849. public function setReadPdo($pdo)
  850. {
  851. $this->readPdo = $pdo;
  852.  
  853. return $this;
  854. }
  855.  
  856. /**
  857. * Set the reconnect instance on the connection.
  858. *
  859. * @param callable $reconnector
  860. * @return $this
  861. */
  862. public function setReconnector(callable $reconnector)
  863. {
  864. $this->reconnector = $reconnector;
  865.  
  866. return $this;
  867. }
  868.  
  869. /**
  870. * Get the database connection name.
  871. *
  872. * @return string|null
  873. */
  874. public function getName()
  875. {
  876. return $this->getConfig('name');
  877. }
  878.  
  879. /**
  880. * Get an option from the configuration options.
  881. *
  882. * @param string $option
  883. * @return mixed
  884. */
  885. public function getConfig($option)
  886. {
  887. return array_get($this->config, $option);
  888. }
  889.  
  890. /**
  891. * Get the PDO driver name.
  892. *
  893. * @return string
  894. */
  895. public function getDriverName()
  896. {
  897. return $this->pdo->getAttribute(\PDO::ATTR_DRIVER_NAME);
  898. }
  899.  
  900. /**
  901. * Get the query grammar used by the connection.
  902. *
  903. * @return \Illuminate\Database\Query\Grammars\Grammar
  904. */
  905. public function getQueryGrammar()
  906. {
  907. return $this->queryGrammar;
  908. }
  909.  
  910. /**
  911. * Set the query grammar used by the connection.
  912. *
  913. * @param \Illuminate\Database\Query\Grammars\Grammar
  914. * @return void
  915. */
  916. public function setQueryGrammar(Query\Grammars\Grammar $grammar)
  917. {
  918. $this->queryGrammar = $grammar;
  919. }
  920.  
  921. /**
  922. * Get the schema grammar used by the connection.
  923. *
  924. * @return \Illuminate\Database\Schema\Grammars\Grammar
  925. */
  926. public function getSchemaGrammar()
  927. {
  928. return $this->schemaGrammar;
  929. }
  930.  
  931. /**
  932. * Set the schema grammar used by the connection.
  933. *
  934. * @param \Illuminate\Database\Schema\Grammars\Grammar
  935. * @return void
  936. */
  937. public function setSchemaGrammar(Schema\Grammars\Grammar $grammar)
  938. {
  939. $this->schemaGrammar = $grammar;
  940. }
  941.  
  942. /**
  943. * Get the query post processor used by the connection.
  944. *
  945. * @return \Illuminate\Database\Query\Processors\Processor
  946. */
  947. public function getPostProcessor()
  948. {
  949. return $this->postProcessor;
  950. }
  951.  
  952. /**
  953. * Set the query post processor used by the connection.
  954. *
  955. * @param \Illuminate\Database\Query\Processors\Processor
  956. * @return void
  957. */
  958. public function setPostProcessor(Processor $processor)
  959. {
  960. $this->postProcessor = $processor;
  961. }
  962.  
  963. /**
  964. * Get the event dispatcher used by the connection.
  965. *
  966. * @return \Illuminate\Contracts\Events\Dispatcher
  967. */
  968. public function getEventDispatcher()
  969. {
  970. return $this->events;
  971. }
  972.  
  973. /**
  974. * Set the event dispatcher instance on the connection.
  975. *
  976. * @param \Illuminate\Contracts\Events\Dispatcher
  977. * @return void
  978. */
  979. public function setEventDispatcher(Dispatcher $events)
  980. {
  981. $this->events = $events;
  982. }
  983.  
  984. /**
  985. * Determine if the connection in a "dry run".
  986. *
  987. * @return bool
  988. */
  989. public function pretending()
  990. {
  991. return $this->pretending === true;
  992. }
  993.  
  994. /**
  995. * Get the default fetch mode for the connection.
  996. *
  997. * @return int
  998. */
  999. public function getFetchMode()
  1000. {
  1001. return $this->fetchMode;
  1002. }
  1003.  
  1004. /**
  1005. * Set the default fetch mode for the connection.
  1006. *
  1007. * @param int $fetchMode
  1008. * @return int
  1009. */
  1010. public function setFetchMode($fetchMode)
  1011. {
  1012. $this->fetchMode = $fetchMode;
  1013. }
  1014.  
  1015. /**
  1016. * Get the connection query log.
  1017. *
  1018. * @return array
  1019. */
  1020. public function getQueryLog()
  1021. {
  1022. return $this->queryLog;
  1023. }
  1024.  
  1025. /**
  1026. * Clear the query log.
  1027. *
  1028. * @return void
  1029. */
  1030. public function flushQueryLog()
  1031. {
  1032. $this->queryLog = array();
  1033. }
  1034.  
  1035. /**
  1036. * Enable the query log on the connection.
  1037. *
  1038. * @return void
  1039. */
  1040. public function enableQueryLog()
  1041. {
  1042. $this->loggingQueries = true;
  1043. }
  1044.  
  1045. /**
  1046. * Disable the query log on the connection.
  1047. *
  1048. * @return void
  1049. */
  1050. public function disableQueryLog()
  1051. {
  1052. $this->loggingQueries = false;
  1053. }
  1054.  
  1055. /**
  1056. * Determine whether we're logging queries.
  1057. *
  1058. * @return bool
  1059. */
  1060. public function logging()
  1061. {
  1062. return $this->loggingQueries;
  1063. }
  1064.  
  1065. /**
  1066. * Get the name of the connected database.
  1067. *
  1068. * @return string
  1069. */
  1070. public function getDatabaseName()
  1071. {
  1072. return $this->database;
  1073. }
  1074.  
  1075. /**
  1076. * Set the name of the connected database.
  1077. *
  1078. * @param string $database
  1079. * @return string
  1080. */
  1081. public function setDatabaseName($database)
  1082. {
  1083. $this->database = $database;
  1084. }
  1085.  
  1086. /**
  1087. * Get the table prefix for the connection.
  1088. *
  1089. * @return string
  1090. */
  1091. public function getTablePrefix()
  1092. {
  1093. return $this->tablePrefix;
  1094. }
  1095.  
  1096. /**
  1097. * Set the table prefix in use by the connection.
  1098. *
  1099. * @param string $prefix
  1100. * @return void
  1101. */
  1102. public function setTablePrefix($prefix)
  1103. {
  1104. $this->tablePrefix = $prefix;
  1105.  
  1106. $this->getQueryGrammar()->setTablePrefix($prefix);
  1107. }
  1108.  
  1109. /**
  1110. * Set the table prefix and return the grammar.
  1111. *
  1112. * @param \Illuminate\Database\Grammar $grammar
  1113. * @return \Illuminate\Database\Grammar
  1114. */
  1115. public function withTablePrefix(Grammar $grammar)
  1116. {
  1117. $grammar->setTablePrefix($this->tablePrefix);
  1118.  
  1119. return $grammar;
  1120. }
  1121.  
  1122. }
  1123.  
Advertisement
Add Comment
Please, Sign In to add comment