connection.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. <?php namespace Laravel\Database; use PDO, PDOStatement, Laravel\Config, Laravel\Event;
  2. class Connection {
  3. /**
  4. * The raw PDO connection instance.
  5. *
  6. * @var PDO
  7. */
  8. public $pdo;
  9. /**
  10. * The connection configuration array.
  11. *
  12. * @var array
  13. */
  14. public $config;
  15. /**
  16. * The query grammar instance for the connection.
  17. *
  18. * @var Grammars\Grammar
  19. */
  20. protected $grammar;
  21. /**
  22. * All of the queries that have been executed on all connections.
  23. *
  24. * @var array
  25. */
  26. public static $queries = array();
  27. /**
  28. * Create a new database connection instance.
  29. *
  30. * @param PDO $pdo
  31. * @param array $config
  32. * @return void
  33. */
  34. public function __construct(PDO $pdo, $config)
  35. {
  36. $this->pdo = $pdo;
  37. $this->config = $config;
  38. }
  39. /**
  40. * Begin a fluent query against a table.
  41. *
  42. * <code>
  43. * // Start a fluent query against the "users" table
  44. * $query = DB::connection()->table('users');
  45. *
  46. * // Start a fluent query against the "users" table and get all the users
  47. * $users = DB::connection()->table('users')->get();
  48. * </code>
  49. *
  50. * @param string $table
  51. * @return Query
  52. */
  53. public function table($table)
  54. {
  55. return new Query($this, $this->grammar(), $table);
  56. }
  57. /**
  58. * Create a new query grammar for the connection.
  59. *
  60. * @return Query\Grammars\Grammar
  61. */
  62. protected function grammar()
  63. {
  64. if (isset($this->grammar)) return $this->grammar;
  65. switch (isset($this->config['grammar']) ? $this->config['grammar'] : $this->driver())
  66. {
  67. case 'mysql':
  68. return $this->grammar = new Query\Grammars\MySQL($this);
  69. case 'sqlsrv':
  70. return $this->grammar = new Query\Grammars\SQLServer($this);
  71. default:
  72. return $this->grammar = new Query\Grammars\Grammar($this);
  73. }
  74. }
  75. /**
  76. * Execute a SQL query against the connection and return a single column result.
  77. *
  78. * <code>
  79. * // Get the total number of rows on a table
  80. * $count = DB::connection()->only('select count(*) from users');
  81. *
  82. * // Get the sum of payment amounts from a table
  83. * $sum = DB::connection()->only('select sum(amount) from payments')
  84. * </code>
  85. *
  86. * @param string $sql
  87. * @param array $bindings
  88. * @return mixed
  89. */
  90. public function only($sql, $bindings = array())
  91. {
  92. $results = (array) $this->first($sql, $bindings);
  93. return reset($results);
  94. }
  95. /**
  96. * Execute a SQL query against the connection and return the first result.
  97. *
  98. * <code>
  99. * // Execute a query against the database connection
  100. * $user = DB::connection()->first('select * from users');
  101. *
  102. * // Execute a query with bound parameters
  103. * $user = DB::connection()->first('select * from users where id = ?', array($id));
  104. * </code>
  105. *
  106. * @param string $sql
  107. * @param array $bindings
  108. * @return object
  109. */
  110. public function first($sql, $bindings = array())
  111. {
  112. if (count($results = $this->query($sql, $bindings)) > 0)
  113. {
  114. return $results[0];
  115. }
  116. }
  117. /**
  118. * Execute a SQL query and return an array of StdClass objects.
  119. *
  120. * @param string $sql
  121. * @param array $bindings
  122. * @return array
  123. */
  124. public function query($sql, $bindings = array())
  125. {
  126. list($statement, $result) = $this->execute($sql, $bindings);
  127. // The result we return depends on the type of query executed against the
  128. // database. On SELECT clauses, we will return the result set, for update
  129. // and deletes we will return the affected row count. And for all other
  130. // queries we'll just return the boolean result.
  131. if (stripos($sql, 'select') === 0)
  132. {
  133. return $statement->fetchAll(PDO::FETCH_CLASS, 'stdClass');
  134. }
  135. elseif (stripos($sql, 'update') === 0 or stripos($sql, 'delete') === 0)
  136. {
  137. return $statement->rowCount();
  138. }
  139. else
  140. {
  141. return $result;
  142. }
  143. }
  144. /**
  145. * Execute a SQL query against the connection.
  146. *
  147. * The PDO statement and boolean result will be return in an array.
  148. *
  149. * @param string $sql
  150. * @param array $bindings
  151. * @return array
  152. */
  153. protected function execute($sql, $bindings = array())
  154. {
  155. $bindings = (array) $bindings;
  156. // Since expressions are injected into the query as strings, we need to
  157. // remove them from the array of bindings. After we have removed them,
  158. // we'll reset the array so there aren't gaps in the keys.
  159. $bindings = array_values(array_filter($bindings, function($binding)
  160. {
  161. return ! $binding instanceof Expression;
  162. }));
  163. $sql = $this->grammar()->shortcut($sql, $bindings);
  164. // Each database operation is wrapped in a try / catch so we can wrap
  165. // any database exceptions in our custom exception class, which will
  166. // set the message to include the SQL and query bindings.
  167. try
  168. {
  169. $statement = $this->pdo->prepare($sql);
  170. $start = microtime(true);
  171. $result = $statement->execute($bindings);
  172. }
  173. // If an exception occurs, we'll pass it into our custom exception
  174. // and set the message to include the SQL and query bindings so
  175. // debugging is much easier on the developer.
  176. catch (\Exception $exception)
  177. {
  178. $exception = new Exception($sql, $bindings, $exception);
  179. throw $exception;
  180. }
  181. // Once we have execute the query, we log the SQL, bindings, and
  182. // execution time in a static array that is accessed by all of
  183. // the connections actively being used by the application.
  184. if (Config::get('database.profile'))
  185. {
  186. $this->log($sql, $bindings, $start);
  187. }
  188. return array($statement, $result);
  189. }
  190. /**
  191. * Log the query and fire the core query event.
  192. *
  193. * @param string $sql
  194. * @param array $bindings
  195. * @param int $start
  196. * @return void
  197. */
  198. protected function log($sql, $bindings, $start)
  199. {
  200. $time = number_format((microtime(true) - $start) * 1000, 2);
  201. Event::fire('laravel.query', array($sql, $bindings, $time));
  202. static::$queries[] = compact('sql', 'bindings', 'time');
  203. }
  204. /**
  205. * Get the driver name for the database connection.
  206. *
  207. * @return string
  208. */
  209. public function driver()
  210. {
  211. return $this->pdo->getAttribute(PDO::ATTR_DRIVER_NAME);
  212. }
  213. /**
  214. * Magic Method for dynamically beginning queries on database tables.
  215. */
  216. public function __call($method, $parameters)
  217. {
  218. return $this->table($method);
  219. }
  220. }