Skip to content

Commit

Permalink
Introducing $ilDB->primaryExistsByFields('table', ['id1', 'id2']); to…
Browse files Browse the repository at this point in the history
… check the existance of an specific primary for a database table
  • Loading branch information
Fabian Wolf authored and chfsx committed Aug 17, 2023
1 parent 98a2971 commit 3d4ccc6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
15 changes: 15 additions & 0 deletions Services/Database/classes/PDO/class.ilDBPdo.php
Original file line number Diff line number Diff line change
Expand Up @@ -1500,4 +1500,19 @@ public function cast(string $a_field_name, string $a_dest_type): string
{
return $this->manager->getQueryUtils()->cast($a_field_name, $a_dest_type);
}

public function primaryExistsByFields(string $table_name, array $fields): bool
{
$constraints = $this->manager->listTableConstraints($table_name);

if (in_array('primary', $constraints)) {
$definitions = $this->reverse->getTableConstraintDefinition($table_name, 'primary');
$primary_fields = array_keys($definitions['fields']);
sort($primary_fields);
sort($fields);

return $primary_fields === $fields;
}
return false;
}
}
2 changes: 2 additions & 0 deletions Services/Database/interfaces/interface.ilDBInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -293,4 +293,6 @@ public function buildAtomQuery(): ilAtomQuery;
public function groupConcat(string $a_field_name, string $a_seperator = ",", ?string $a_order = null): string;

public function cast(string $a_field_name, string $a_dest_type): string;

public function primaryExistsByFields(string $table_name, array $fields) : bool;
}

0 comments on commit 3d4ccc6

Please sign in to comment.