method getKeys documentation in bbn\Db\Languages\Sqlite
Returns the keys of the given table.
function(string $table)
{
if (!$this->check()) {
return null;
}
$r = [];
if ($full = $this->tableFullName($table)) {
$r = [];
$keys = [];
$cols = [];
$database = $this->getCurrent() === 'main' ? '' : '"'.$this->getCurrent().'".';
if ($indexes = $this->getRows('PRAGMA index_list('.$table.')')) {
foreach ($indexes as $d){
if ($fields = $this->getRows('PRAGMA index_info('.$database.'"'.$d['name'].'")')) {
foreach ($fields as $d2){
$key_name = strtolower($d['origin']) === 'pk' ? 'PRIMARY' : $d['name'];
if (!isset($keys[$key_name])) {
$keys[$key_name] = [
'columns' => [$d2['name']],
'ref_db' => null,
'ref_table' => null,
'ref_column' => null,
'constraint' => null,
'update' => null,
'delete' => null,
'unique' => $d['unique'] == 1 ? 1 : 0
];
}
else{
$keys[$key_name]['columns'][] = $d2['name'];
}
if (!isset($cols[$d2['name']])) {
$cols[$d2['name']] = [$key_name];
}
else{
$cols[$d2['name']][] = $key_name;
}
}
}
}
}
// when a column is INTEGER PRIMARY KEY it doesn't show up in the query: PRAGMA index_list
// INTEGER PRIMARY KEY considered as auto_increment: https://www.sqlite.org/autoinc.html
if ($columns = $this->getColumns($table)) {
$columns = array_filter($columns, function ($item) {
return $item['extra'] === 'auto_increment' && $item['key'] === 'PRI';
});
foreach ($columns as $column_name => $column) {
if (!isset($keys['PRIMARY'])) {
$keys['PRIMARY'] = [
'columns' => [$column_name],
'ref_db' => null,
'ref_table' => null,
'ref_column' => null,
'constraint' => null,
'update' => null,
'delete' => null,
'unique' => 1
];
}
else {
$keys['PRIMARY']['columns'][] = $column_name;
}
if (!isset($cols[$column_name])) {
$cols[$column_name] = ['PRIMARY'];
}
else {
$cols[$column_name][] = 'PRIMARY';
}
}
}
if ($constraints = $this->getRows("PRAGMA foreign_key_list($database\"$table\")")) {
foreach ($constraints as $constraint) {
$constraint_name = "{$constraint['table']}_{$constraint['from']}";
if (empty($cols[$constraint['from']])) {
$keys[$constraint_name] = [
'columns' => [$constraint['from']],
'ref_db' => $this->getCurrent(),
'ref_table' => $constraint['table'] ?? null,
'ref_column' => $constraint['to'] ?? null,
'constraint' => $constraint_name,
'update' => $constraint['on_update'] ?? null,
'delete' => $constraint['on_delete'] ?? null,
'unique' => 0
];
$cols[$constraint['from']] = [$constraint_name];
} else {
foreach ($cols[$constraint['from']] as $col) {
if (isset($keys[$col])) {
$keys[$col]['ref_db'] = $this->getCurrent();
$keys[$col]['ref_table'] = $constraint['table'] ?? null;
$keys[$col]['ref_column'] = $constraint['to'] ?? null;
$keys[$col]['constraint'] = $constraint_name;
$keys[$col]['update'] = $constraint['on_update'] ?? null;
$keys[$col]['delete'] = $constraint['on_delete'] ?? null;
}
}
}
}
}
$r['keys'] = $keys;
$r['cols'] = $cols;
}
return $r;
}
Returns the keys of the given table. BBN is a suite of PHP and JS libraries and VueJS components - all open-source! bbn.io, build applications, the quick way
This website uses cookies to ensure you get the best experience on our website.
© 2011-2024
BBN Solutions