database.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php namespace Laravel;
  2. use Closure;
  3. use Laravel\Database\Expression;
  4. use Laravel\Database\Connection;
  5. class Database {
  6. /**
  7. * The established database connections.
  8. *
  9. * @var array
  10. */
  11. public static $connections = array();
  12. /**
  13. * The third-party driver registrar.
  14. *
  15. * @var array
  16. */
  17. public static $registrar = array();
  18. /**
  19. * Get a database connection.
  20. *
  21. * If no database name is specified, the default connection will be returned.
  22. *
  23. * <code>
  24. * // Get the default database connection for the application
  25. * $connection = DB::connection();
  26. *
  27. * // Get a specific connection by passing the connection name
  28. * $connection = DB::connection('mysql');
  29. * </code>
  30. *
  31. * @param string $connection
  32. * @return Database\Connection
  33. */
  34. public static function connection($connection = null)
  35. {
  36. if (is_null($connection)) $connection = Config::get('database.default');
  37. if ( ! isset(static::$connections[$connection]))
  38. {
  39. $config = Config::get("database.connections.{$connection}");
  40. if (is_null($config))
  41. {
  42. throw new \Exception("Database connection is not defined for [$connection].");
  43. }
  44. static::$connections[$connection] = new Connection(static::connect($config), $config);
  45. }
  46. return static::$connections[$connection];
  47. }
  48. /**
  49. * Get a PDO database connection for a given database configuration.
  50. *
  51. * @param array $config
  52. * @return PDO
  53. */
  54. protected static function connect($config)
  55. {
  56. return static::connector($config['driver'])->connect($config);
  57. }
  58. /**
  59. * Create a new database connector instance.
  60. *
  61. * @param string $driver
  62. * @return Database\Connectors\Connector
  63. */
  64. protected static function connector($driver)
  65. {
  66. if (isset(static::$registrar[$driver]))
  67. {
  68. $resolver = static::$registrar[$driver]['connector'];
  69. return $resolver();
  70. }
  71. switch ($driver)
  72. {
  73. case 'sqlite':
  74. return new Database\Connectors\SQLite;
  75. case 'mysql':
  76. return new Database\Connectors\MySQL;
  77. case 'pgsql':
  78. return new Database\Connectors\Postgres;
  79. case 'sqlsrv':
  80. return new Database\Connectors\SQLServer;
  81. default:
  82. throw new \Exception("Database driver [$driver] is not supported.");
  83. }
  84. }
  85. /**
  86. * Begin a fluent query against a table.
  87. *
  88. * @param string $table
  89. * @param string $connection
  90. * @return Database\Query
  91. */
  92. public static function table($table, $connection = null)
  93. {
  94. return static::connection($connection)->table($table);
  95. }
  96. /**
  97. * Create a new database expression instance.
  98. *
  99. * Database expressions are used to inject raw SQL into a fluent query.
  100. *
  101. * @param string $value
  102. * @return Expression
  103. */
  104. public static function raw($value)
  105. {
  106. return new Expression($value);
  107. }
  108. /**
  109. * Escape a string for usage in a query.
  110. *
  111. * This uses the correct quoting mechanism for the default database connection.
  112. *
  113. * @param string $value
  114. * @return string
  115. */
  116. public static function escape($value)
  117. {
  118. return static::connection()->pdo->quote($value);
  119. }
  120. /**
  121. * Get the profiling data for all queries.
  122. *
  123. * @return array
  124. */
  125. public static function profile()
  126. {
  127. return Database\Connection::$queries;
  128. }
  129. /**
  130. * Get the last query that was executed.
  131. *
  132. * Returns false if no queries have been executed yet.
  133. *
  134. * @return string
  135. */
  136. public static function last_query()
  137. {
  138. return end(Database\Connection::$queries);
  139. }
  140. /**
  141. * Register a database connector and grammars.
  142. *
  143. * @param string $name
  144. * @param Closure $connector
  145. * @param Closure $query
  146. * @param Closure $schema
  147. * @return void
  148. */
  149. public static function extend($name, Closure $connector, $query = null, $schema = null)
  150. {
  151. if (is_null($query)) $query = '\Laravel\Database\Query\Grammars\Grammar';
  152. static::$registrar[$name] = compact('connector', 'query', 'schema');
  153. }
  154. /**
  155. * Magic Method for calling methods on the default database connection.
  156. *
  157. * <code>
  158. * // Get the driver name for the default database connection
  159. * $driver = DB::driver();
  160. *
  161. * // Execute a fluent query on the default database connection
  162. * $users = DB::table('users')->get();
  163. * </code>
  164. */
  165. public static function __callStatic($method, $parameters)
  166. {
  167. return call_user_func_array(array(static::connection(), $method), $parameters);
  168. }
  169. }