function test_caching() { $connection = AnewtDatabase::get_connection('cached'); /* This should not not hit the cache */ $row = $connection->prepare('SELECT 1 AS test;')->execute()->fetch_one(); $this->assertEquals(1, $row['test']); $this->assertEquals(0, $connection->n_cache_hits); $this->assertEquals(0, $connection->n_cache_misses); /* This should hit the cache the second time */ $rows = $connection->prepare_execute_fetch_all('SELECT 1 AS test;'); $this->assertEquals(1, count($rows)); $this->assertEquals(1, $rows[0]['test']); $this->assertEquals(0, $connection->n_cache_hits); $this->assertEquals(1, $connection->n_cache_misses); $rows = $connection->prepare_execute_fetch_all('SELECT 1 AS test;'); $this->assertEquals(1, count($rows)); $this->assertEquals(1, $rows[0]['test']); $this->assertEquals(1, $connection->n_cache_hits); $this->assertEquals(1, $connection->n_cache_misses); /* This should hit the cache the second time */ $row = $connection->prepare_execute_fetch_one('SELECT 1 AS test;'); $this->assertEquals(1, $row['test']); $this->assertEquals(1, $connection->n_cache_hits); $this->assertEquals(2, $connection->n_cache_misses); $row = $connection->prepare_execute_fetch_one('SELECT 1 AS test;'); $this->assertEquals(1, $row['test']); $this->assertEquals(2, $connection->n_cache_hits); $this->assertEquals(2, $connection->n_cache_misses); /* Flush the cache */ $connection->flush_cache(); $row = $connection->prepare_execute_fetch_one('SELECT 1 AS test;'); $this->assertEquals(2, $connection->n_cache_hits); $this->assertEquals(3, $connection->n_cache_misses); }
function test_two_connections() { $settings = array('type' => 'sqlite'); AnewtDatabase::setup_connection($settings); AnewtDatabase::setup_connection($settings, 'connection2'); $c2 = AnewtDatabase::get_connection('connection2'); $c1 = AnewtDatabase::get_connection(); $this->assertTrue($c1->is_connected()); $this->assertTrue($c2->is_connected()); $c1->disconnect(); $this->assertFalse($c1->is_connected()); $this->assertTrue($c2->is_connected()); }
public function setup() { AnewtDatabase::setup_connection(array('type' => 'sqlite')); $connection = AnewtDatabase::get_connection(); $connection->prepare_execute('CREATE TABLE Person ( id INTEGER PRIMARY KEY, name VARCHAR(255), age INTEGER, is_happy BOOLEAN )'); $pq = $connection->prepare('INSERT INTO Person (id, name, age, is_happy) VALUES (?int?, ?str?, ?int?, ?bool?)'); $pq->execute(1, 'A', 10, true); $pq->execute(2, 'B', 11, false); $pq->execute(3, 'C', 12, false); $pq->execute(4, 'D', 13, null); $pq->execute(5, 'E', 14, false); }
function test_transaction() { $cnt_sql = 'SELECT COUNT(*) AS cnt FROM test_table'; $connection = AnewtDatabase::get_connection(); $row = $connection->prepare_execute_fetch_one($cnt_sql); $n_rows_before = $row['cnt']; /* Start a transaction, insert, and count rows */ $connection->transaction_begin(); $connection->prepare_execute('INSERT INTO test_table (boolean_col) VALUES (TRUE)'); $row = $connection->prepare_execute_fetch_one($cnt_sql); $n_rows_after = $row['cnt']; $this->assertEquals($n_rows_before + 1, $n_rows_after); /* Rollback, and count rows again */ $connection->transaction_rollback(); $row = $connection->prepare_execute_fetch_one($cnt_sql); $n_rows_after = $row['cnt']; $this->assertEquals($n_rows_before, $n_rows_after); /* Again, but now commit */ $connection->transaction_begin(); $connection->prepare_execute('INSERT INTO test_table (boolean_col) VALUES (TRUE)'); $connection->transaction_commit(); /* And count rows again */ $row = $connection->prepare_execute_fetch_one($cnt_sql); $n_rows_after = $row['cnt']; $this->assertEquals($n_rows_before + 1, $n_rows_after); }
/** * Obtain a database connection. * * By default this returns the default database connection. Override this * method if you want to use a custom database connection. * * \return * An AnewtDatabaseConnection instance * * \see AnewtDatabase::get_connection() * \see AnewtDatabaseConnection */ public static function db_connection() { return AnewtDatabase::get_connection(); }