sqlite.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. if ($config['database'] == ':memory:')
  29. {
  30. return new PDO('sqlite::memory:', null, null, $options);
  31. }
  32. // First, we will check for the database in the default storage directory for the
  33. // application. If we don't find the database there, we will assume the database
  34. // name is actually a full qualified path to the database on disk and attempt
  35. // to load it. If we still can't find it, we'll bail out.
  36. elseif (file_exists($path = $this->path.$config['database'].'.sqlite'))
  37. {
  38. return new PDO('sqlite:'.$path, null, null, $options);
  39. }
  40. elseif (file_exists($config['database']))
  41. {
  42. return new PDO('sqlite:'.$config['database'], null, null, $options);
  43. }
  44. throw new \Exception("SQLite database [{$config['database']}] could not be found.");
  45. }
  46. }