Skip to content

Commit

Permalink
Working multiple AV Configs optimized storage in database using EAV m…
Browse files Browse the repository at this point in the history
…odel
  • Loading branch information
Valentin Hamon committed Dec 12, 2016
1 parent f9ce7e6 commit f7f1f78
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 35 deletions.
20 changes: 13 additions & 7 deletions inc/avconfig.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,29 +53,35 @@ function toJson()

function run()
{
$i = 0;
foreach ($this->entries as $entry)
{
$i++;

PluginArmaditoLog::Verbose("[".$i."] ".$entry->{'attr'}."=".$entry->{'value'});

$is_agentrow_indb = $this->isValueForAgentInDB($entry->{'attr'}, $this->agentid);
$is_baserow_indb = $this->isValueForAgentInDB($entry->{'attr'}, 0);
$is_baserow_equal = $this->isValueEqualForAgentInDB($entry->{'attr'}, $entry->{'value'}, 0);

if($is_agentrow_indb) {
$this->updateValue($entry->{'attr'}, $entry->{'value'}, $this->agentid);
if($is_baserow_equal) {
$this->rmValueFromDB($entry->{'attr'}, $entry->{'value'}, $this->agentid);
continue;
}

$is_baserow_equal = $this->isValueEqualForAgentInDB($entry->{'attr'}, 0, $entry->{'value'});
if($is_baserow_equal) {
if($is_agentrow_indb) {
$this->updateValueInDB($entry->{'attr'}, $entry->{'value'}, $this->agentid);
continue;
}

if ($is_baserow_indb) {
$this->addValue($entry->{'attr'}, $entry->{'value'}, $this->agentid);
$this->addOrUpdateValueForAgent($entry->{'attr'}, $entry->{'value'}, $this->agentid);
} else {
$this->addValue($entry->{'attr'}, $entry->{'value'}, 0);
$this->insertValueInDB($entry->{'attr'}, $entry->{'value'}, 0);
}
}

$this->addOrUpdateValue("hasAVConfig", 1);
$this->addOrUpdateValueForAgent("hasAVConfig", 1, $this->agentid);
}
}
?>
95 changes: 67 additions & 28 deletions inc/eavcommondbtm.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,101 +107,140 @@ function getValueFromDB($type)
return current($this->find("`type`='" . $DB->escape($type) . "'"));
}

function isValueEqualForAgentInDB($type, $agentid = -1, $value)
function addOrUpdateValueForAgent($type, $value, $agentid = -1)
{
global $DB;
if($this->isValueForAgentInDB($type, $agentid))
{
$this->updateValueInDB($type, $value, $agentid);
}
else
{
$this->insertValueInDB($type, $value, $agentid);
}
}

$query = $this->getQueryForAgent($type, $agentid);
$query .= " AND `value`='" . $DB->escape($value) . "'";
function isValueEqualForAgentInDB($type, $value, $agentid = -1)
{
$data = $this->getValueForAgentInDB($type, $agentid);

return !empty($this->find($query));
if(isset($data['value'])) {
return ($data['value'] == $value);
}

return false;
}

function isValueForAgentInDB($type, $agentid = -1)
{
$data = $this->getValueForAgentInDB($type, $agentid);
return isset($data['value']);
}

function getValueForAgentInDB($type, $agentid = -1)
{
global $DB;

$query = $this->getQueryForAgent($type, $agentid);
$query = "SELECT value FROM `".$this->getTable()."`";
$query .= $this->getWhereQueryForAgent($type, $agentid);

return !empty($this->find($query));
$ret = $DB->query($query);
if (!$ret) {
throw new InvalidArgumentException(sprintf('Error isValueForAgentInDB : %s', $DB->error()));
}

if ($DB->numrows($ret) > 0) {
$data = $DB->fetch_assoc($ret);
return $data;
}

return "";
}

function getQueryForAgent($type, $agentid)
function getWhereQueryForAgent($type, $agentid)
{
global $DB;
$antivirus_id = $this->antivirus->getId();

return "`type`='". $DB->escape($type)."'".
return " WHERE `type`='". $DB->escape($type)."'".
" AND `plugin_armadito_antiviruses_id`='". $DB->escape($antivirus_id)."'".
" AND `plugin_armadito_agents_id`='". $DB->escape($agentid)."'";
}

function insertValueInDB($type, $value)
function insertValueInDB($type, $value, $agentid = -1)
{
$dbmanager = new PluginArmaditoDbManager();
$params = $this->setCommonQueryParams();
$params = $this->setCommonQueryParams($agentid);
$query = "NewConfigValue";

if(isset($this->agentid))
{
if($agentid >= 0) {
$query = "NewValue";
}

$dbmanager->addQuery($query, "INSERT", $this->getTable(), $params);
$dbmanager->prepareQuery($query);
$dbmanager->bindQuery($query);

$dbmanager = $this->setCommonQueryValues($dbmanager, $query, $type, $value);
$dbmanager = $this->setCommonQueryValues($dbmanager, $query, $type, $value, $agentid);
$dbmanager->executeQuery($query);
}

function updateValueInDB($type, $value)
function updateValueInDB($type, $value, $agentid = -1)
{
$dbmanager = new PluginArmaditoDbManager();
$params = $this->setCommonQueryParams();
$params = $this->setCommonQueryParams($agentid);
$where_params = "type";
$query = "UpdateConfigValue";

if(isset($this->agentid))
if($agentid >= 0)
{
$query = "UpdateValue";
$where_params = array( "plugin_armadito_antiviruses_id",
$where_params = array( "type",
"plugin_armadito_antiviruses_id",
"plugin_armadito_agents_id");
}

$dbmanager->addQuery($query, "UPDATE", $this->getTable(), $params, $where_params);
$dbmanager->prepareQuery($query);
$dbmanager->bindQuery($query);

$dbmanager = $this->setCommonQueryValues($dbmanager, $query, $type, $value);
$dbmanager = $this->setCommonQueryValues($dbmanager, $query, $type, $value, $agentid);
$dbmanager->executeQuery($query);
}

function setCommonQueryParams()
function rmValueFromDB($type, $value, $agentid = -1)
{
global $DB;
$query = "DELETE from `".$this->getTable()."`";;
$query .= $this->getWhereQueryForAgent($type, $agentid);

$ret = $DB->query($query);
if (!$ret) {
throw new InvalidArgumentException(sprintf('Error isValueForAgentInDB : %s', $DB->error()));
}
}

function setCommonQueryParams($agentid = -1)
{
$params["value"]["type"] = "s";
$params["type"]["type"] = "s";

if(isset($this->agentid) && isset($this->antivirus)) {
if($agentid >= 0) {
$params['plugin_armadito_antiviruses_id']["type"] = "i";
$params['plugin_armadito_agents_id']["type"] = "i";
}

return $params;
}

function setCommonQueryValues($dbmanager, $query, $type, $value)
function setCommonQueryValues($dbmanager, $query, $type, $value, $agentid = -1)
{
$dbmanager->setQueryValue($query, "value", $value);
$dbmanager->setQueryValue($query, "type", $type);

if(($query == "UpdateValue") || ($type == "hasAVConfig")) {
$dbmanager->setQueryValue($query, "plugin_armadito_antiviruses_id", $this->antivirus->getId());
$dbmanager->setQueryValue($query, "plugin_armadito_agents_id", $this->agentid);
}
else if($query == "NewValue") {
if($agentid >= 0) {
$dbmanager->setQueryValue($query, "plugin_armadito_antiviruses_id", $this->antivirus->getId());
$dbmanager->setQueryValue($query, "plugin_armadito_agents_id", 0);
$dbmanager->setQueryValue($query, "plugin_armadito_agents_id", $agentid);
PluginArmaditoLog::Verbose(" $query - $type = $value for agent ($agentid)");
}

return $dbmanager;
Expand Down

0 comments on commit f7f1f78

Please sign in to comment.