Phactory\Sql\DbUtil\SqliteUtil::getPrimaryKey PHP Method

getPrimaryKey() public method

public getPrimaryKey ( $table )
    public function getPrimaryKey($table)
    {
        $stmt = $this->_pdo->prepare("SELECT * FROM sqlite_master WHERE type='table' AND name=:name");
        $stmt->execute(array(':name' => $table));
        $result = $stmt->fetch();
        $sql = $result['sql'];
        $matches = array();
        preg_match('/(\\w+?)\\s+\\w+?\\s+PRIMARY KEY/', $sql, $matches);
        if (!isset($matches[1])) {
            return null;
        }
        return $matches[1];
    }

Usage Example

 public function testGetPrimaryKey()
 {
     $this->pdo->exec('CREATE TABLE test_table ( id INTEGER PRIMARY KEY, name TEXT )');
     $db_util = new SqliteUtil($this->phactory);
     $pk = $db_util->getPrimaryKey('test_table');
     $this->assertEquals('id', $pk);
 }