database.php 3.9 KB

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