connection.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. return $statement->fetchAll(PDO::FETCH_CLASS, 'stdClass');
  128. }
  129. /**
  130. * Execute a SQL UPDATE query and return the affected row count.
  131. *
  132. * @param string $sql
  133. * @param array $bindings
  134. * @return int
  135. */
  136. public function update($sql, $bindings = array())
  137. {
  138. list($statement, $result) = $this->execute($sql, $bindings);
  139. return $statement->rowCount();
  140. }
  141. /**
  142. * Execute a SQL DELETE query and return the affected row count.
  143. *
  144. * @param string $sql
  145. * @param array $bindings
  146. * @return int
  147. */
  148. public function delete($sql, $bindings = array())
  149. {
  150. list($statement, $result) = $this->execute($sql, $bindings);
  151. return $statement->rowCount();
  152. }
  153. /**
  154. * Execute an SQL query and return the boolean result of the PDO statement.
  155. *
  156. * @param string $sql
  157. * @param array $bindings
  158. * @return bool
  159. */
  160. public function statement($sql, $bindings = array())
  161. {
  162. list($statement, $result) = $this->execute($sql, $bindings);
  163. return $result;
  164. }
  165. /**
  166. * Execute a SQL query against the connection.
  167. *
  168. * The PDO statement and boolean result will be return in an array.
  169. *
  170. * @param string $sql
  171. * @param array $bindings
  172. * @return array
  173. */
  174. protected function execute($sql, $bindings = array())
  175. {
  176. $bindings = (array) $bindings;
  177. // Since expressions are injected into the query as strings, we need to
  178. // remove them from the array of bindings. After we have removed them,
  179. // we'll reset the array so there aren't gaps in the keys.
  180. $bindings = array_values(array_filter($bindings, function($binding)
  181. {
  182. return ! $binding instanceof Expression;
  183. }));
  184. $sql = $this->grammar()->shortcut($sql, $bindings);
  185. $statement = $this->pdo->prepare($sql);
  186. // Every query is timed so that we can log the executinon time along
  187. // with the query SQL and array of bindings. This should be make it
  188. // convenient for the developer to profile performance.
  189. $time = microtime(true);
  190. $result = $statement->execute($bindings);
  191. $time = number_format((microtime(true) - $time) * 1000, 2);
  192. // Once we have execute the query, we log the SQL, bindings, and
  193. // execution time in a static array that is accessed by all of
  194. // the connections used by the application.
  195. if (Config::get('database.profile'))
  196. {
  197. $this->log($sql, $bindings, $time);
  198. }
  199. return array($statement, $result);
  200. }
  201. /**
  202. * Log the query and fire the core query event.
  203. *
  204. * @param string $sql
  205. * @param array $bindings
  206. * @param int $time
  207. * @return void
  208. */
  209. protected function log($sql, $bindings, $time)
  210. {
  211. Event::fire('query', array($sql, $bindings, $time));
  212. static::$queries[] = compact('sql', 'bindings', 'time');
  213. }
  214. /**
  215. * Get the driver name for the database connection.
  216. *
  217. * @return string
  218. */
  219. public function driver()
  220. {
  221. return $this->pdo->getAttribute(PDO::ATTR_DRIVER_NAME);
  222. }
  223. /**
  224. * Magic Method for dynamically beginning queries on database tables.
  225. */
  226. public function __call($method, $parameters)
  227. {
  228. return $this->table($method);
  229. }
  230. }