connection.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <?php namespace Laravel\Database; use PDO, PDOStatement;
  2. class Connection {
  3. /**
  4. * The connection configuration array.
  5. *
  6. * @var array
  7. */
  8. protected $config;
  9. /**
  10. * The query grammar instance for the connection.
  11. *
  12. * @var Grammars\Grammar
  13. */
  14. protected $grammar;
  15. /**
  16. * The raw PDO connection instance.
  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. * 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. * @param string $table
  43. * @return Query
  44. */
  45. public function table($table)
  46. {
  47. return new Query($this, $this->grammar(), $table);
  48. }
  49. /**
  50. * Create a new query grammar for the connection.
  51. *
  52. * @return Grammars\Grammar
  53. */
  54. protected function grammar()
  55. {
  56. if (isset($this->grammar)) return $this->grammar;
  57. switch (isset($this->config['grammar']) ? $this->config['grammar'] : $this->driver())
  58. {
  59. case 'mysql':
  60. return $this->grammar = new Grammars\MySQL;
  61. default:
  62. return $this->grammar = new Grammars\Grammar;
  63. }
  64. }
  65. /**
  66. * Execute a SQL query against the connection and return a single column result.
  67. *
  68. * <code>
  69. * // Get the total number of rows on a table
  70. * $count = DB::connection()->only('select count(*) from users');
  71. *
  72. * // Get the sum of payment amounts from a table
  73. * $sum = DB::connection()->only('select sum(amount) from payments')
  74. * </code>
  75. *
  76. * @param string $sql
  77. * @param array $bindings
  78. * @return mixed
  79. */
  80. public function only($sql, $bindings = array())
  81. {
  82. $result = (array) $this->first($sql, $bindings);
  83. return reset($result);
  84. }
  85. /**
  86. * Execute a SQL query against the connection and return the first result.
  87. *
  88. * <code>
  89. * // Execute a query against the database connection
  90. * $user = DB::connection()->first('select * from users');
  91. *
  92. * // Execute a query with bound parameters
  93. * $user = DB::connection()->first('select * from users where id = ?', array($id));
  94. * </code>
  95. *
  96. * @param string $sql
  97. * @param array $bindings
  98. * @return object
  99. */
  100. public function first($sql, $bindings = array())
  101. {
  102. if (count($results = $this->query($sql, $bindings)) > 0) return $results[0];
  103. }
  104. /**
  105. * Execute a SQL query against the connection.
  106. *
  107. * The method returns the following based on query type:
  108. *
  109. * SELECT -> Array of stdClasses
  110. * UPDATE -> Number of rows affected.
  111. * DELETE -> Number of Rows affected.
  112. * ELSE -> Boolean true / false depending on success.
  113. *
  114. * <code>
  115. * // Execute a query against the database connection
  116. * $users = DB::connection()->query('select * from users');
  117. *
  118. * // Execute a query with bound parameters
  119. * $user = DB::connection()->query('select * from users where id = ?', array($id));
  120. * </code>
  121. *
  122. * @param string $sql
  123. * @param array $bindings
  124. * @return mixed
  125. */
  126. public function query($sql, $bindings = array())
  127. {
  128. // Remove expressions from the bindings since they injected into
  129. // the query as raw strings and are not bound parameters.
  130. foreach ($bindings as $key => $value)
  131. {
  132. if ($value instanceof Expression) unset($bindings[$key]);
  133. }
  134. $sql = $this->transform(trim($sql), $bindings);
  135. $this->queries[] = compact('sql', 'bindings');
  136. return $this->execute($this->pdo->prepare($sql), $bindings);
  137. }
  138. /**
  139. * Transform an SQL query into an executable query.
  140. *
  141. * Laravel provides a convenient short-cut when writing raw queries for
  142. * handling cumbersome "where in" statements. This method will transform
  143. * those segments into their full SQL counterparts.
  144. *
  145. * @param string $sql
  146. * @param array $bindings
  147. * @return string
  148. */
  149. protected function transform($sql, $bindings)
  150. {
  151. if (strpos($sql, '(...)') === false) return $sql;
  152. for ($i = 0; $i < count($bindings); $i++)
  153. {
  154. // If the binding is an array, we can assume it is being used to fill
  155. // a "where in" condition, so we will replace the next place-holder
  156. // in the query with the correct number of parameters based on the
  157. // number of elements in this binding.
  158. if (is_array($bindings[$i]))
  159. {
  160. $parameters = implode(', ', array_fill(0, count($bindings[$i]), '?'));
  161. $sql = preg_replace('~\(\.\.\.\)~', "({$parameters})", $sql, 1);
  162. }
  163. }
  164. return $sql;
  165. }
  166. /**
  167. * Execute a prepared PDO statement and return the appropriate results.
  168. *
  169. * @param PDOStatement $statement
  170. * @param array $results
  171. * @return mixed
  172. */
  173. protected function execute(PDOStatement $statement, $bindings)
  174. {
  175. $result = $statement->execute($bindings);
  176. if (strpos(strtoupper($statement->queryString), 'SELECT') === 0)
  177. {
  178. return $statement->fetchAll(PDO::FETCH_CLASS, 'stdClass');
  179. }
  180. elseif (strpos(strtoupper($statement->queryString), 'INSERT') === 0)
  181. {
  182. return $result;
  183. }
  184. return $statement->rowCount();
  185. }
  186. /**
  187. * Get the driver name for the database connection.
  188. *
  189. * @return string
  190. */
  191. public function driver()
  192. {
  193. return $this->pdo->getAttribute(PDO::ATTR_DRIVER_NAME);
  194. }
  195. /**
  196. * Magic Method for dynamically beginning queries on database tables.
  197. */
  198. public function __call($method, $parameters)
  199. {
  200. return $this->table($method);
  201. }
  202. }