connection.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php namespace Laravel\Database;
  2. class Connection {
  3. /**
  4. * The connection name.
  5. *
  6. * @var string
  7. */
  8. public $name;
  9. /**
  10. * The connection configuration.
  11. *
  12. * @var array
  13. */
  14. public $config;
  15. /**
  16. * The PDO connection.
  17. *
  18. * @var PDO
  19. */
  20. public $pdo;
  21. /**
  22. * All of the queries that have been executed on the connection.
  23. *
  24. * @var array
  25. */
  26. public $queries = array();
  27. /**
  28. * The database connector instance.
  29. *
  30. * @var Connector
  31. */
  32. private $connector;
  33. /**
  34. * Create a new Connection instance.
  35. *
  36. * @param string $name
  37. * @param array $config
  38. * @param Connector $connector
  39. * @return void
  40. */
  41. public function __construct($name, $config, Connector $connector)
  42. {
  43. $this->name = $name;
  44. $this->config = $config;
  45. $this->connector = $connector;
  46. }
  47. /**
  48. * Establish the PDO connection for the connection instance.
  49. *
  50. * @return void
  51. */
  52. public function connect()
  53. {
  54. $this->pdo = $this->connector->connect($this->config);
  55. }
  56. /**
  57. * Determine if a PDO connection has been established for the connection.
  58. *
  59. * @return bool
  60. */
  61. public function connected()
  62. {
  63. return ! is_null($this->pdo);
  64. }
  65. /**
  66. * Execute a SQL query against the connection and return a scalar result.
  67. *
  68. * @param string $sql
  69. * @param array $bindings
  70. * @return mixed
  71. */
  72. public function scalar($sql, $bindings = array())
  73. {
  74. $result = (array) $this->first($sql, $bindings);
  75. return (strpos(strtolower($sql), 'select count') === 0) ? (int) reset($result) : (float) reset($result);
  76. }
  77. /**
  78. * Execute a SQL query against the connection and return the first result.
  79. *
  80. * @param string $sql
  81. * @param array $bindings
  82. * @return object
  83. */
  84. public function first($sql, $bindings = array())
  85. {
  86. return (count($results = $this->query($sql, $bindings)) > 0) ? $results[0] : null;
  87. }
  88. /**
  89. * Execute a SQL query against the connection.
  90. *
  91. * The method returns the following based on query type:
  92. *
  93. * SELECT -> Array of stdClasses
  94. * UPDATE -> Number of rows affected.
  95. * DELETE -> Number of Rows affected.
  96. * ELSE -> Boolean true / false depending on success.
  97. *
  98. * @param string $sql
  99. * @param array $bindings
  100. * @return mixed
  101. */
  102. public function query($sql, $bindings = array())
  103. {
  104. if ( ! $this->connected()) $this->connect();
  105. $this->queries[] = compact('sql', 'bindings');
  106. return $this->execute($this->pdo->prepare(trim($sql)), $bindings);
  107. }
  108. /**
  109. * Execute a prepared PDO statement and return the appropriate results.
  110. *
  111. * @param PDOStatement $statement
  112. * @param array $results
  113. * @return mixed
  114. */
  115. private function execute(\PDOStatement $statement, $bindings)
  116. {
  117. $result = $statement->execute($bindings);
  118. if (strpos(strtoupper($statement->queryString), 'SELECT') === 0)
  119. {
  120. return $statement->fetchAll(\PDO::FETCH_CLASS, 'stdClass');
  121. }
  122. elseif (strpos(strtoupper($statement->queryString), 'INSERT') === 0)
  123. {
  124. return $result;
  125. }
  126. return $statement->rowCount();
  127. }
  128. /**
  129. * Begin a fluent query against a table.
  130. *
  131. * @param string $table
  132. * @return Query
  133. */
  134. public function table($table)
  135. {
  136. return Query\Factory::make($table, $this, Query\Compiler\Factory::make($this));
  137. }
  138. /**
  139. * Get the driver name for the database connection.
  140. *
  141. * @return string
  142. */
  143. public function driver()
  144. {
  145. if ( ! $this->connected()) $this->connect();
  146. return $this->pdo->getAttribute(\PDO::ATTR_DRIVER_NAME);
  147. }
  148. }