Skip to content

Commit

Permalink
Merge pull request #1547 from tripal/tv4g1-issue1544-organism-infrasp…
Browse files Browse the repository at this point in the history
…ecies-validation

Tripal 4 change chado storage handling of empty fields
  • Loading branch information
laceysanderson committed Jun 21, 2023
2 parents 0e78c14 + 16b7ffa commit 4f42251
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ fields:
content_type: bio_data_1
label: Abbreviation
type: chado_string_type
description: A shortened name (or abbreviation) for the organism (e.g. O.sativa).
description: A shortened name (or abbreviation) for the organism (e.g. O. sativa).
cardinality: 1
required: false
storage_settings:
Expand Down Expand Up @@ -61,7 +61,7 @@ fields:
content_type: bio_data_1
label: Species
type: chado_string_type
description: "The species name of the organism"
description: "The species name of the organism."
cardinality: 1
required: true
storage_settings:
Expand All @@ -88,7 +88,7 @@ fields:
content_type: bio_data_1
label: Common Name
type: chado_string_type
description: "The common name for the organism"
description: "The common name for the organism."
cardinality: 1
required: false
storage_settings:
Expand Down
24 changes: 18 additions & 6 deletions tripal_chado/src/Plugin/TripalStorage/ChadoStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -1122,14 +1122,26 @@ private function validateUnique($values, $chado_table, $record_id, $record, &$vi
if (array_key_exists($col, $record['fields'])) {
$col_val = $record['fields'][$col];
}
// There is an issue with postgreSQL that if the value is allowed
// to be null but it is in a unique constraint then it will allow
// it to be inserted because a Null != NULL. So, skip checking
// these columns in the unique constraint if they are empty.
// If there is not a NOT NULL constraint on this column,
// and it is of a string type, then we need to handle
// empty values specially, since they might be stored
// as either NULL or as an empty string in the database
// table. Create a condition that checks for both. For
// other types, e.g. integer, just check for null.
if ($table_def['fields'][$col]['not null'] == FALSE and !$col_val) {
continue;
if (in_array($table_def['fields'][$col]['type'],
['character', 'character varying', 'text'])) {
$query->condition($query->orConditionGroup()
->condition($col, '', '=')
->isNull($col));
}
else {
$query->isNull($col);
}
}
else {
$query->condition($col, $col_val);
}
$query->condition($col, $col_val);
}

// If we have matching record, check for a unique constraint
Expand Down

0 comments on commit 4f42251

Please sign in to comment.