connection.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. * All of the queries that have been executed on the connection.
  11. *
  12. * @var array
  13. */
  14. public $queries = array();
  15. /**
  16. * The connection configuration array.
  17. *
  18. * @var array
  19. */
  20. protected $config;
  21. /**
  22. * The query grammar instance for the connection.
  23. *
  24. * @var Grammars\Grammar
  25. */
  26. protected $grammar;
  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)
  103. {
  104. return $results[0];
  105. }
  106. }
  107. /**
  108. * Execute a SQL query against the connection.
  109. *
  110. * The method returns the following based on query type:
  111. *
  112. * SELECT -> Array of stdClasses
  113. * UPDATE -> Number of rows affected.
  114. * DELETE -> Number of Rows affected.
  115. * ELSE -> Boolean true / false depending on success.
  116. *
  117. * <code>
  118. * // Execute a query against the database connection
  119. * $users = DB::connection()->query('select * from users');
  120. *
  121. * // Execute a query with bound parameters
  122. * $user = DB::connection()->query('select * from users where id = ?', array($id));
  123. * </code>
  124. *
  125. * @param string $sql
  126. * @param array $bindings
  127. * @return mixed
  128. */
  129. public function query($sql, $bindings = array())
  130. {
  131. foreach ($bindings as $key => $value)
  132. {
  133. if ($value instanceof Expression) unset($bindings[$key]);
  134. }
  135. $sql = $this->transform($sql, $bindings);
  136. $this->queries[] = compact('sql', 'bindings');
  137. return $this->execute($this->pdo->prepare($sql), $bindings);
  138. }
  139. /**
  140. * Transform an SQL query into an executable query.
  141. *
  142. * Laravel provides a convenient short-cut when writing raw queries for
  143. * handling cumbersome "where in" statements. This method will transform
  144. * those segments into their full SQL counterparts.
  145. *
  146. * @param string $sql
  147. * @param array $bindings
  148. * @return string
  149. */
  150. protected function transform($sql, $bindings)
  151. {
  152. if (strpos($sql, '(...)') !== false)
  153. {
  154. for ($i = 0; $i < count($bindings); $i++)
  155. {
  156. // If the binding is an array, we can assume it is being used to fill
  157. // a "where in" condition, so we will replace the next place-holder
  158. // in the query with the correct number of parameters based on the
  159. // number of elements in this binding.
  160. if (is_array($bindings[$i]))
  161. {
  162. $parameters = implode(', ', array_fill(0, count($bindings[$i]), '?'));
  163. $sql = preg_replace('~\(\.\.\.\)~', "({$parameters})", $sql, 1);
  164. }
  165. }
  166. }
  167. return trim($sql);
  168. }
  169. /**
  170. * Execute a prepared PDO statement and return the appropriate results.
  171. *
  172. * @param PDOStatement $statement
  173. * @param array $results
  174. * @return mixed
  175. */
  176. protected function execute(PDOStatement $statement, $bindings)
  177. {
  178. $result = $statement->execute($bindings);
  179. $sql = strtoupper($statement->queryString);
  180. if (strpos($sql, 'SELECT') === 0)
  181. {
  182. return $statement->fetchAll(PDO::FETCH_CLASS, 'stdClass');
  183. }
  184. elseif (strpos($sql, 'UPDATE') === 0 or strpos($sql, 'DELETE') === 0)
  185. {
  186. return $statement->rowCount();
  187. }
  188. else
  189. {
  190. return $result;
  191. }
  192. }
  193. /**
  194. * Get the driver name for the database connection.
  195. *
  196. * @return string
  197. */
  198. public function driver()
  199. {
  200. return $this->pdo->getAttribute(PDO::ATTR_DRIVER_NAME);
  201. }
  202. /**
  203. * Magic Method for dynamically beginning queries on database tables.
  204. */
  205. public function __call($method, $parameters)
  206. {
  207. return $this->table($method);
  208. }
  209. }