connection.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. <?php namespace Laravel\Database; use PDO, PDOStatement;
  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. protected $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;
  69. case 'sqlsrv':
  70. return $this->grammar = new Query\Grammars\SQLServer;
  71. default:
  72. return $this->grammar = new Query\Grammars\Grammar;
  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. // Since expressions are injected into the query as strings, we need to
  177. // remove them from the array of bindings. After we have removed them,
  178. // we'll reset the array so there aren't gaps in the keys.
  179. $bindings = array_values(array_filter($bindings, function($binding)
  180. {
  181. return ! $binding instanceof Expression;
  182. }));
  183. $sql = $this->transform($sql, $bindings);
  184. $statement = $this->pdo->prepare($sql);
  185. // Every query is timed so that we can log the executinon time along
  186. // with the query SQL and array of bindings. This should be make it
  187. // convenient for the developer to profile the application's query
  188. // performance to diagnose bottlenecks.
  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. This allows us to
  195. // review all of the executed SQL.
  196. static::$queries[] = compact('sql', 'bindings', 'time');
  197. return array($statement, $result);
  198. }
  199. /**
  200. * Transform an SQL query into an executable query.
  201. *
  202. * @param string $sql
  203. * @param array $bindings
  204. * @return string
  205. */
  206. protected function transform($sql, $bindings)
  207. {
  208. // Laravel provides an easy short-cut notation for writing raw
  209. // WHERE IN statements. If (...) is in the query, it will be
  210. // replaced with the correct number of parameters based on
  211. // the bindings for the query.
  212. if (strpos($sql, '(...)') !== false)
  213. {
  214. for ($i = 0; $i < count($bindings); $i++)
  215. {
  216. // If the binding is an array, we can assume it is being used
  217. // to fill a "where in" condition, so we'll replace the next
  218. // place-holder in the SQL query with the correct number of
  219. // parameters based on the elements in the binding.
  220. if (is_array($bindings[$i]))
  221. {
  222. $parameters = implode(', ', array_fill(0, count($bindings[$i]), '?'));
  223. $sql = preg_replace('~\(\.\.\.\)~', "({$parameters})", $sql, 1);
  224. }
  225. }
  226. }
  227. return trim($sql);
  228. }
  229. /**
  230. * Get the driver name for the database connection.
  231. *
  232. * @return string
  233. */
  234. public function driver()
  235. {
  236. return $this->pdo->getAttribute(PDO::ATTR_DRIVER_NAME);
  237. }
  238. /**
  239. * Magic Method for dynamically beginning queries on database tables.
  240. */
  241. public function __call($method, $parameters)
  242. {
  243. return $this->table($method);
  244. }
  245. }