database.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. return static::$registrar[$driver]['connector']();
  68. }
  69. switch ($driver)
  70. {
  71. case 'sqlite':
  72. return new Database\Connectors\SQLite;
  73. case 'mysql':
  74. return new Database\Connectors\MySQL;
  75. case 'pgsql':
  76. return new Database\Connectors\Postgres;
  77. case 'sqlsrv':
  78. return new Database\Connectors\SQLServer;
  79. default:
  80. throw new \Exception("Database driver [$driver] is not supported.");
  81. }
  82. }
  83. /**
  84. * Begin a fluent query against a table.
  85. *
  86. * @param string $table
  87. * @param string $connection
  88. * @return Database\Query
  89. */
  90. public static function table($table, $connection = null)
  91. {
  92. return static::connection($connection)->table($table);
  93. }
  94. /**
  95. * Create a new database expression instance.
  96. *
  97. * Database expressions are used to inject raw SQL into a fluent query.
  98. *
  99. * @param string $value
  100. * @return Expression
  101. */
  102. public static function raw($value)
  103. {
  104. return new Expression($value);
  105. }
  106. /**
  107. * Get the profiling data for all queries.
  108. *
  109. * @return array
  110. */
  111. public static function profile()
  112. {
  113. return Database\Connection::$queries;
  114. }
  115. /**
  116. * Register a database connector and grammars.
  117. *
  118. * @param string $name
  119. * @param Closure $connector
  120. * @param Closure $query
  121. * @param Closure $schema
  122. * @return void
  123. */
  124. public static function extend($name, Closure $connector, $query = null, $schema = null)
  125. {
  126. if (is_null($query)) $query = '\Laravel\Database\Query\Grammars\Grammar';
  127. static::$registrar[$name] = compact('connector', 'query', 'schema');
  128. }
  129. /**
  130. * Magic Method for calling methods on the default database connection.
  131. *
  132. * <code>
  133. * // Get the driver name for the default database connection
  134. * $driver = DB::driver();
  135. *
  136. * // Execute a fluent query on the default database connection
  137. * $users = DB::table('users')->get();
  138. * </code>
  139. */
  140. public static function __callStatic($method, $parameters)
  141. {
  142. return call_user_func_array(array(static::connection(), $method), $parameters);
  143. }
  144. }