database.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. * Get a database connection.
  13. *
  14. * If no database name is specified, the default connection will be returned.
  15. *
  16. * <code>
  17. * // Get the default database connection for the application
  18. * $connection = DB::connection();
  19. *
  20. * // Get a specific connection by passing the connection name
  21. * $connection = DB::connection('mysql');
  22. * </code>
  23. *
  24. * @param string $connection
  25. * @return Database\Connection
  26. */
  27. public static function connection($connection = null)
  28. {
  29. if (is_null($connection)) $connection = Config::get('database.default');
  30. if ( ! isset(static::$connections[$connection]))
  31. {
  32. $config = Config::get("database.connections.{$connection}");
  33. if (is_null($config))
  34. {
  35. throw new \Exception("Database connection is not defined for [$connection].");
  36. }
  37. static::$connections[$connection] = new Connection(static::connect($config), $config);
  38. }
  39. return static::$connections[$connection];
  40. }
  41. /**
  42. * Get a PDO database connection for a given database configuration.
  43. *
  44. * @param array $config
  45. * @return PDO
  46. */
  47. protected static function connect($config)
  48. {
  49. return static::connector($config['driver'])->connect($config);
  50. }
  51. /**
  52. * Create a new database connector instance.
  53. *
  54. * @param string $driver
  55. * @return Database\Connectors\Connector
  56. */
  57. protected static function connector($driver)
  58. {
  59. switch ($driver)
  60. {
  61. case 'sqlite':
  62. return new Database\Connectors\SQLite;
  63. case 'mysql':
  64. return new Database\Connectors\MySQL;
  65. case 'pgsql':
  66. return new Database\Connectors\Postgres;
  67. case 'sqlsrv':
  68. return new Database\Connectors\SQLServer;
  69. default:
  70. throw new \Exception("Database driver [$driver] is not supported.");
  71. }
  72. }
  73. /**
  74. * Begin a fluent query against a table.
  75. *
  76. * @param string $table
  77. * @param string $connection
  78. * @return Database\Query
  79. */
  80. public static function table($table, $connection = null)
  81. {
  82. return static::connection($connection)->table($table);
  83. }
  84. /**
  85. * Create a new database expression instance.
  86. *
  87. * Database expressions are used to inject raw SQL into a fluent query.
  88. *
  89. * @param string $value
  90. * @return Expression
  91. */
  92. public static function raw($value)
  93. {
  94. return new Expression($value);
  95. }
  96. /**
  97. * Get the profiling data for all queries.
  98. *
  99. * @return array
  100. */
  101. public static function profile()
  102. {
  103. return Database\Connection::$queries;
  104. }
  105. /**
  106. * Magic Method for calling methods on the default database connection.
  107. *
  108. * <code>
  109. * // Get the driver name for the default database connection
  110. * $driver = DB::driver();
  111. *
  112. * // Execute a fluent query on the default database connection
  113. * $users = DB::table('users')->get();
  114. * </code>
  115. */
  116. public static function __callStatic($method, $parameters)
  117. {
  118. return call_user_func_array(array(static::connection(), $method), $parameters);
  119. }
  120. }