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