From aa376d6be1895bc2a99356be8ec0a330449f395c Mon Sep 17 00:00:00 2001 From: Clayton Carter Date: Mon, 1 Aug 2016 09:44:56 -0400 Subject: [PATCH 1/3] Add support for SQLite :memory: databases. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit $cached_connection was needed so that new, empty db wasn’t created on every call. --- lib/Connection.php | 4 ++++ lib/adapters/SqliteAdapter.php | 8 ++++++-- test/SqliteAdapterMemoryTest.php | 30 ++++++++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 test/SqliteAdapterMemoryTest.php diff --git a/lib/Connection.php b/lib/Connection.php index 69e8a1b20..b21faffaa 100644 --- a/lib/Connection.php +++ b/lib/Connection.php @@ -168,6 +168,7 @@ private static function load_adapter_class($adapter) * sqlite://../relative/path/to/file.db * sqlite://unix(/absolute/path/to/file.db) * sqlite://windows(c%2A/absolute/path/to/file.db) + * sqlite://:memory: * * * @param string $connection_url A connection URL @@ -177,6 +178,9 @@ public static function parse_connection_url($connection_url) { $url = @parse_url($connection_url); + if ( $url['scheme'] == "sqlite" && $url['host'] == ":memory" ) + $url['host'] = ":memory:"; + if (!isset($url['host'])) throw new DatabaseException('Database host must be specified in the connection string. If you want to specify an absolute filename, use e.g. sqlite://unix(/path/to/file)'); diff --git a/lib/adapters/SqliteAdapter.php b/lib/adapters/SqliteAdapter.php index 0917043be..e215823be 100644 --- a/lib/adapters/SqliteAdapter.php +++ b/lib/adapters/SqliteAdapter.php @@ -15,13 +15,17 @@ class SqliteAdapter extends Connection { static $datetime_format = 'Y-m-d H:i:s'; + private static $cached_connection; protected function __construct($info) { - if (!file_exists($info->host)) + if ($info->host != ":memory:" && !file_exists($info->host)) throw new DatabaseException("Could not find sqlite db: $info->host"); - $this->connection = new PDO("sqlite:$info->host",null,null,static::$PDO_OPTIONS); + if ( self::$cached_connection == null ) { + self::$cached_connection = new PDO("sqlite:$info->host",null,null,static::$PDO_OPTIONS); + } + $this->connection = self::$cached_connection; } public function limit($sql, $offset, $limit) diff --git a/test/SqliteAdapterMemoryTest.php b/test/SqliteAdapterMemoryTest.php new file mode 100644 index 000000000..5624a81e4 --- /dev/null +++ b/test/SqliteAdapterMemoryTest.php @@ -0,0 +1,30 @@ +get_connections(); + $connections['sqlite'] = 'sqlite://:memory:'; + ActiveRecord\Config::instance()->set_connections( $connections ); + + parent::set_up(); + } + + public function testConnectToMemoryDatabaseShouldNotCreateDbFile() + { + try + { + ActiveRecord\Connection::instance("sqlite://:memory:"); + $this->assertFalse(file_exists(__DIR__ . "/" . ":memory:" )); + } + catch (ActiveRecord\DatabaseException $e) + { + $this->assertFalse(true); + } + } + +} +?> From 3af27faf52ab60e4ac60567ab9d9a50d86170e48 Mon Sep 17 00:00:00 2001 From: Clayton Carter Date: Wed, 17 Aug 2016 05:57:19 -0400 Subject: [PATCH 2/3] clarify failure assertion --- test/SqliteAdapterMemoryTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/SqliteAdapterMemoryTest.php b/test/SqliteAdapterMemoryTest.php index 5624a81e4..7f7fb3fe0 100644 --- a/test/SqliteAdapterMemoryTest.php +++ b/test/SqliteAdapterMemoryTest.php @@ -22,7 +22,7 @@ public function testConnectToMemoryDatabaseShouldNotCreateDbFile() } catch (ActiveRecord\DatabaseException $e) { - $this->assertFalse(true); + $this->fail("could not open connection to :memory: database"); } } From 68c87afea7353e884ecbf884fcdce7252aac3c7f Mon Sep 17 00:00:00 2001 From: Clayton Carter Date: Thu, 18 Aug 2016 05:49:16 -0400 Subject: [PATCH 3/3] Fix whitespace. --- lib/Connection.php | 2 +- lib/adapters/SqliteAdapter.php | 2 +- test/SqliteAdapterMemoryTest.php | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/Connection.php b/lib/Connection.php index b21faffaa..de2766a42 100644 --- a/lib/Connection.php +++ b/lib/Connection.php @@ -178,7 +178,7 @@ public static function parse_connection_url($connection_url) { $url = @parse_url($connection_url); - if ( $url['scheme'] == "sqlite" && $url['host'] == ":memory" ) + if ($url['scheme'] == "sqlite" && $url['host'] == ":memory") $url['host'] = ":memory:"; if (!isset($url['host'])) diff --git a/lib/adapters/SqliteAdapter.php b/lib/adapters/SqliteAdapter.php index e215823be..581cd28c0 100644 --- a/lib/adapters/SqliteAdapter.php +++ b/lib/adapters/SqliteAdapter.php @@ -22,7 +22,7 @@ protected function __construct($info) if ($info->host != ":memory:" && !file_exists($info->host)) throw new DatabaseException("Could not find sqlite db: $info->host"); - if ( self::$cached_connection == null ) { + if (self::$cached_connection == null) { self::$cached_connection = new PDO("sqlite:$info->host",null,null,static::$PDO_OPTIONS); } $this->connection = self::$cached_connection; diff --git a/test/SqliteAdapterMemoryTest.php b/test/SqliteAdapterMemoryTest.php index 7f7fb3fe0..0106c3823 100644 --- a/test/SqliteAdapterMemoryTest.php +++ b/test/SqliteAdapterMemoryTest.php @@ -8,7 +8,7 @@ public function set_up($connection_name=null) { $connections = ActiveRecord\Config::instance()->get_connections(); $connections['sqlite'] = 'sqlite://:memory:'; - ActiveRecord\Config::instance()->set_connections( $connections ); + ActiveRecord\Config::instance()->set_connections($connections); parent::set_up(); } @@ -18,7 +18,7 @@ public function testConnectToMemoryDatabaseShouldNotCreateDbFile() try { ActiveRecord\Connection::instance("sqlite://:memory:"); - $this->assertFalse(file_exists(__DIR__ . "/" . ":memory:" )); + $this->assertFalse(file_exists(__DIR__ . "/" . ":memory:")); } catch (ActiveRecord\DatabaseException $e) {