database.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. * Get the profiling data for all queries.
  110. *
  111. * @return array
  112. */
  113. public static function profile()
  114. {
  115. return Database\Connection::$queries;
  116. }
  117. /**
  118. * Get the last query that was executed.
  119. *
  120. * Returns false if no queries have been executed yet.
  121. *
  122. * @return string
  123. */
  124. public static function last_query()
  125. {
  126. return end(Database\Connection::$queries);
  127. }
  128. /**
  129. * Register a database connector and grammars.
  130. *
  131. * @param string $name
  132. * @param Closure $connector
  133. * @param Closure $query
  134. * @param Closure $schema
  135. * @return void
  136. */
  137. public static function extend($name, Closure $connector, $query = null, $schema = null)
  138. {
  139. if (is_null($query)) $query = '\Laravel\Database\Query\Grammars\Grammar';
  140. static::$registrar[$name] = compact('connector', 'query', 'schema');
  141. }
  142. /**
  143. * Magic Method for calling methods on the default database connection.
  144. *
  145. * <code>
  146. * // Get the driver name for the default database connection
  147. * $driver = DB::driver();
  148. *
  149. * // Execute a fluent query on the default database connection
  150. * $users = DB::table('users')->get();
  151. * </code>
  152. */
  153. public static function __callStatic($method, $parameters)
  154. {
  155. return call_user_func_array(array(static::connection(), $method), $parameters);
  156. }
  157. }