/** * @test */ public function synopsis() { $pdo = new PDO('sqlite::memory:', null, null, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]); $runner = new PDORunner($pdo); /* Executes statements without params, returning affected rows */ $this->assertEquals(0, $runner->exec("CREATE TABLE test (foo int, bar varchar)"), "Expected no rows to be affected"); $this->assertEquals("test", $pdo->query("SELECT tbl_name FROM sqlite_master WHERE type='table' AND tbl_name='test'")->fetch(PDO::FETCH_COLUMN), "Expected table was NOT created"); /* Executes statements with params, returning affected rows */ $this->assertEquals(1, $runner->exec("INSERT INTO test VALUES(?, ?)", [1, "two"]), "Expected one row to be affected"); $this->assertEquals([['foo' => 1, 'bar' => 'two']], $pdo->query("SELECT * FROM test")->fetchAll(PDO::FETCH_ASSOC), "Expected row was NOT inserted"); /* Executes queries without params in the given fetch mode, returning data. */ $this->assertEquals(['two'], $runner->query("SELECT bar FROM test", [], PDO::FETCH_COLUMN), "Expected the contents of the 'bar' column ('two')"); /* Executes queries with params in the given fetch mode, returning data. */ $this->assertEquals([(object) ['foo' => 1]], $runner->query("SELECT foo FROM test WHERE bar=:bar", ['bar' => 'two'], PDO::FETCH_OBJ), "Expected the contents of the 'foo' column (1) within an object"); }
/** * Fetch the structure of a table for a given database, schema, and table. * * @param string $database The name of the database. * @param string $schema The name of the schema. * @param string $tbl THe name of the table. * @return Table A Table object that represents the table structure in a database. */ public function getTable($database, $schema, $tbl) { if ($database = $this->interpretDatabase($database, $schema)) { $columns = $this->queryRunner->query(sprintf("DESCRIBE %s.%s", $this->quoteIdentifier($database), $this->quoteIdentifier($tbl)), [], PDO::FETCH_ASSOC); } else { $columns = $this->queryRunner->query(sprintf("DESCRIBE %s", $this->quoteIdentifier($tbl)), [], PDO::FETCH_ASSOC); } if (!$columns) { return false; } $table = new Table(); $table->name = $tbl; foreach ($columns as $columnData) { $table->columns[] = $this->getColumnFromDescription($columnData); } return $table; }