115 lines
2.9 KiB
PHP
115 lines
2.9 KiB
PHP
<?php
|
|
namespace Mocha\Oms;
|
|
|
|
use Mocha\Core\InstanceKey;
|
|
use Mocha\Core\InstanceReference;
|
|
|
|
class MySQLDatabaseOms extends DatabaseOms
|
|
{
|
|
public \PDO $PDO;
|
|
|
|
public function __construct($hostname, $port, $databasename, $username, $password)
|
|
{
|
|
$this->PDO = new \PDO("mysql:host=" . $hostname . ";port=" . $port . ";dbname=" . $databasename, $username, $password);
|
|
}
|
|
|
|
public function getDbId($instRef)
|
|
{
|
|
if ($instRef == null)
|
|
return 0;
|
|
|
|
return $instRef->DatabaseId;
|
|
}
|
|
|
|
public function getInstanceByGlobalIdentifier(string $globalIdentifier) : ?InstanceReference
|
|
{
|
|
$query = "CALL mocha_get_instance_by_global_identifier(:global_identifier)";
|
|
$stmt = $this->PDO->prepare($query);
|
|
$result = $stmt->execute(array
|
|
(
|
|
"global_identifier" => $globalIdentifier
|
|
));
|
|
|
|
if ($result === false)
|
|
return null;
|
|
|
|
$values = $stmt->fetchAll();
|
|
if (count($values) == 0)
|
|
return null;
|
|
|
|
$dbid = $values[0]["id"];
|
|
$class_id = $values[0]["class_id"];
|
|
$inst_id = $values[0]["inst_id"];
|
|
$global_id = $values[0]["global_identifier"];
|
|
|
|
$ir = new InstanceReference();
|
|
$ir->DatabaseId = $dbid;
|
|
$ir->GlobalIdentifier = $global_id;
|
|
$ir->InstanceKey = new InstanceKey($class_id, $inst_id);
|
|
return $ir;
|
|
}
|
|
|
|
public function getRelatedInstances(InstanceReference $sourceInstance, InstanceReference $relationshipInstance, \DateTime $effectiveDate = null) : ?array
|
|
{
|
|
$dt = null;
|
|
if ($effectiveDate !== null)
|
|
{
|
|
$dt = $effectiveDate->format("Y-m-d");
|
|
}
|
|
$query = "CALL mocha_get_related_instances(:src_inst_id, :rel_inst_id, :eff_date)";
|
|
$stmt = $this->PDO->prepare($query);
|
|
$result = $stmt->execute(array
|
|
(
|
|
"src_inst_id" => $this->getDbId($sourceInstance),
|
|
"rel_inst_id" => $this->getDbId($relationshipInstance),
|
|
"eff_date" => $dt
|
|
));
|
|
|
|
if ($result === false)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
$values = $stmt->fetchAll();
|
|
if (count($values) >= 0)
|
|
{
|
|
$ret = array();
|
|
foreach ($values as $value)
|
|
{
|
|
$retval = new InstanceReference();
|
|
$retval->DatabaseId = $value["id"];
|
|
$retval->InstanceKey = new InstanceKey($value["class_id"], $value["inst_id"]);
|
|
$retval->GlobalIdentifier = $value["global_identifier"];
|
|
$ret[] = $retval;
|
|
}
|
|
return $ret;
|
|
}
|
|
return [];
|
|
}
|
|
|
|
public function getAttributeValue($sourceInstance, $attributeInstance, $defaultValue = null, $effectiveDate = null) : ?string
|
|
{
|
|
$query = "CALL mocha_get_attribute_value(:src_inst_id, :attr_inst_id, :eff_date)";
|
|
$stmt = $this->PDO->prepare($query);
|
|
$result = $stmt->execute(array
|
|
(
|
|
"src_inst_id" => $this->getDbId($sourceInstance),
|
|
"attr_inst_id" => $this->getDbId($attributeInstance),
|
|
"eff_date" => $effectiveDate
|
|
));
|
|
|
|
if ($result === false)
|
|
{
|
|
return $defaultValue;
|
|
}
|
|
|
|
$values = $stmt->fetchAll();
|
|
if (count($values) >= 0)
|
|
{
|
|
$value = $values[0]["att_value"];
|
|
return $value;
|
|
}
|
|
return $defaultValue;
|
|
}
|
|
}
|
|
?>
|