public function getPrimaryKeys(string $table, bool $asArray = false)
    {
        $pk = [];

        if ($this->databaseType === 'mysql') {
            $sql = "SHOW COLUMNS FROM {$table}";
            $primaryKeyIndex = 'Key';
            $primaryKeyValue = 'PRI';
            $tableNameIndex = 'Field';
        } elseif ($this->databaseType === 'sqlite') {
            $sql = "PRAGMA table_info({$table})";
            $primaryKeyIndex = 'pk';
            $primaryKeyValue = 1;
            $tableNameIndex = 'name';
        } else {
            throw new \RuntimeException("Unsupported database type: '{$this->databaseType}'");
        }

        $stm = $this->query($sql);
        $r = $stm->fetchAll();
        $this->logQuery($sql);

        # Search all columns for the Primary Key flag
        foreach ($r as $col) {
            if ($col[$primaryKeyIndex] == $primaryKeyValue) {
                # Add this column to the primary keys list
                $pk[] = $col[$tableNameIndex];
            }
        }

        # if the return value is preferred as string
        if (!$asArray) {
            $pk = join(',', $pk);
        }

        return $pk;
    }
public function getPrimaryKeys(string $table, bool $asArray = false)
    {
        $pk = [];

        if ($this->databaseType === 'mysql') {
            $sql = "SHOW COLUMNS FROM {$table}";
            $primaryKeyIndex = 'Key';
            $primaryKeyValue = 'PRI';
            $tableNameIndex = 'Field';
        } elseif ($this->databaseType === 'sqlite') {
            $sql = "PRAGMA table_info({$table})";
            $primaryKeyIndex = 'pk';
            $primaryKeyValue = 1;
            $tableNameIndex = 'name';
        } else {
            throw new \RuntimeException("Unsupported database type: '{$this->databaseType}'");
        }

        $stm = $this->query($sql);
        $r = $stm->fetchAll();
        $this->logQuery($sql);

        # Search all columns for the Primary Key flag
        foreach ($r as $col) {
            if ($col[$primaryKeyIndex] == $primaryKeyValue) {
                # Add this column to the primary keys list
                $pk[] = $col[$tableNameIndex];
            }
        }

        # if the return value is preferred as string
        if (!$asArray) {
            $pk = join(',', $pk);
        }

        return $pk;
    }
public function getPrimaryKeys ( string $ table , bool $ asArray = false ) { $ pk = [ ] ; if ( $ this -> databaseType === 'mysql' ) { $ sql = "SHOW COLUMNS FROM {$table}" ; $ primaryKeyIndex = 'Key' ; $ primaryKeyValue = 'PRI' ; $ tableNameIndex = 'Field' ; } elseif ( $ this -> databaseType === 'sqlite' ) { $ sql = "PRAGMA table_info({$table})" ; $ primaryKeyIndex = 'pk' ; $ primaryKeyValue = 1 ; $ tableNameIndex = 'name' ; } else { throw new \ RuntimeException ( "Unsupported database type: '{$this->databaseType}'" ) ; } $ stm = $ this -> query ( $ sql ) ; $ r = $ stm -> fetchAll ( ) ; $ this -> logQuery ( $ sql ) ; # Search all columns for the Primary Key flag foreach ( $ r as $ col ) { if ( $ col [ $ primaryKeyIndex ] == $ primaryKeyValue ) { # Add this column to the primary keys list $ pk [ ] = $ col [ $ tableNameIndex ] ; } } # if the return value is preferred as string if ( ! $ asArray ) { $ pk = join ( ',' , $ pk ) ; } return $ pk ; }
Finds the Primary Key fields of a table.

The most common return is a string with the name of the primary key
column (for example, "id"). If the primary key is composite, this
method will return all primary keys in a comma-separated string, except
if the second parameter is specified as true, in which case the return
will be an array.

@param string $table Name of the table in the database
@param bool $asArray Return multiple keys as an array (default is false)
@return mixed A comma-separated string with the primary key fields or an
array if $asArray is true
Finds the Primary Key fields of a table .