connection.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php namespace Laravel\Database;
  2. class Connection {
  3. /**
  4. * The connection name.
  5. *
  6. * @var string
  7. */
  8. public $name;
  9. /**
  10. * The connection configuration.
  11. *
  12. * @var array
  13. */
  14. public $config;
  15. /**
  16. * The PDO connection.
  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. * The database connector instance.
  29. *
  30. * @var Connector
  31. */
  32. private $connector;
  33. /**
  34. * Create a new Connection instance.
  35. *
  36. * @param Connector $connector
  37. * @param Query\Factory $factory
  38. * @param Compiler\Factory $compiler
  39. * @param string $name
  40. * @param array $config
  41. * @return void
  42. */
  43. public function __construct(Connector $connector, Query\Factory $query, Query\Compiler\Factory $compiler, $name, $config)
  44. {
  45. $this->name = $name;
  46. $this->query = $query;
  47. $this->config = $config;
  48. $this->compiler = $compiler;
  49. $this->connector = $connector;
  50. }
  51. /**
  52. * Establish the PDO connection for the connection instance.
  53. *
  54. * @return void
  55. */
  56. public function connect()
  57. {
  58. $this->pdo = $this->connector->connect($this->config);
  59. }
  60. /**
  61. * Determine if a PDO connection has been established for the connection.
  62. *
  63. * @return bool
  64. */
  65. public function connected()
  66. {
  67. return ! is_null($this->pdo);
  68. }
  69. /**
  70. * Execute a SQL query against the connection and return a scalar result.
  71. *
  72. * @param string $sql
  73. * @param array $bindings
  74. * @return mixed
  75. */
  76. public function scalar($sql, $bindings = array())
  77. {
  78. $result = (array) $this->first($sql, $bindings);
  79. return (strpos(strtolower(trim($sql)), 'select count') === 0) ? (int) reset($result) : (float) reset($result);
  80. }
  81. /**
  82. * Execute a SQL query against the connection and return the first result.
  83. *
  84. * @param string $sql
  85. * @param array $bindings
  86. * @return object
  87. */
  88. public function first($sql, $bindings = array())
  89. {
  90. return (count($results = $this->query($sql, $bindings)) > 0) ? $results[0] : null;
  91. }
  92. /**
  93. * Execute a SQL query against the connection.
  94. *
  95. * The method returns the following based on query type:
  96. *
  97. * SELECT -> Array of stdClasses
  98. * UPDATE -> Number of rows affected.
  99. * DELETE -> Number of Rows affected.
  100. * ELSE -> Boolean true / false depending on success.
  101. *
  102. * @param string $sql
  103. * @param array $bindings
  104. * @return mixed
  105. */
  106. public function query($sql, $bindings = array())
  107. {
  108. if ( ! $this->connected()) $this->connect();
  109. $this->queries[] = compact('sql', 'bindings');
  110. return $this->execute($this->pdo->prepare(trim($sql)), $bindings);
  111. }
  112. /**
  113. * Execute a prepared PDO statement and return the appropriate results.
  114. *
  115. * @param PDOStatement $statement
  116. * @param array $results
  117. * @return mixed
  118. */
  119. private function execute(\PDOStatement $statement, $bindings)
  120. {
  121. $result = $statement->execute($bindings);
  122. if (strpos(strtoupper($statement->queryString), 'SELECT') === 0)
  123. {
  124. return $statement->fetchAll(\PDO::FETCH_CLASS, 'stdClass');
  125. }
  126. elseif (strpos(strtoupper($statement->queryString), 'INSERT') === 0)
  127. {
  128. return $result;
  129. }
  130. return $statement->rowCount();
  131. }
  132. /**
  133. * Begin a fluent query against a table.
  134. *
  135. * @param string $table
  136. * @return Query
  137. */
  138. public function table($table)
  139. {
  140. return $this->query->make($this, $this->compiler->make($this), $table);
  141. }
  142. /**
  143. * Get the driver name for the database connection.
  144. *
  145. * @return string
  146. */
  147. public function driver()
  148. {
  149. if ( ! $this->connected()) $this->connect();
  150. return $this->pdo->getAttribute(\PDO::ATTR_DRIVER_NAME);
  151. }
  152. /**
  153. * Magic Method for dynamically beginning queries on database tables.
  154. */
  155. public function __call($method, $parameters)
  156. {
  157. return $this->table($method);
  158. }
  159. }