Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for SQLite :memory: databases. #551

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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:
* </code>
*
* @param string $connection_url A connection URL
Expand All @@ -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)');

Expand Down
8 changes: 6 additions & 2 deletions lib/adapters/SqliteAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
30 changes: 30 additions & 0 deletions test/SqliteAdapterMemoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
require_once __DIR__ . '/../lib/adapters/SqliteAdapter.php';
require_once __DIR__ . '/SqliteAdapterTest.php';

class SqliteAdapterMemoryTest extends SqliteAdapterTest
{
public function set_up($connection_name=null)
{
$connections = ActiveRecord\Config::instance()->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->fail("could not open connection to :memory: database");
}
}

}
?>