sqlite.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php namespace Laravel\Database\Connectors; use PDO;
  2. class SQLite extends Connector {
  3. /**
  4. * The path to the SQLite databases for the application.
  5. *
  6. * @var string
  7. */
  8. protected $path;
  9. /**
  10. * Create a new SQLite database connector instance.
  11. *
  12. * @param string $path
  13. * @return void
  14. */
  15. public function __construct($path)
  16. {
  17. $this->path = $path;
  18. }
  19. /**
  20. * Establish a PDO database connection for a given database configuration.
  21. *
  22. * @param array $config
  23. * @return PDO
  24. */
  25. public function connect($config)
  26. {
  27. $options = $this->options($config);
  28. // SQLite provides supported for "in-memory" databases, which exist only for the
  29. // lifetime of the request. Any given in-memory database may only have one PDO
  30. // connection open to it at a time. Generally, these databases are use for
  31. // testing and development purposes, not in production scenarios.
  32. if ($config['database'] == ':memory:')
  33. {
  34. return new PDO('sqlite::memory:', null, null, $options);
  35. }
  36. // First, we will check for the database in the default storage directory for the
  37. // application. If we don't find the database there, we will assume the database
  38. // name is actually a full qualified path to the database on disk and attempt
  39. // to load it. If we still can't find it, we'll bail out.
  40. elseif (file_exists($path = $this->path.$config['database'].'.sqlite'))
  41. {
  42. return new PDO('sqlite:'.$path, null, null, $options);
  43. }
  44. elseif (file_exists($config['database']))
  45. {
  46. return new PDO('sqlite:'.$config['database'], null, null, $options);
  47. }
  48. throw new \OutOfBoundsException("SQLite database [{$config['database']}] could not be found.");
  49. }
  50. }