Esempio n. 1
0
 public function save($update_system_records = false)
 {
     $row = $this->unBind();
     unset($row['id']);
     unset($row['dtlastmodified']);
     $this->db->BeginTrans();
     // Prepare SQL statement
     $set = array();
     $bind = array();
     foreach ($row as $field => $value) {
         $set[] = "`{$field}` = ?";
         $bind[] = $value;
     }
     $set = join(', ', $set);
     try {
         //Save zone;
         if ($this->id) {
             if ($update_system_records) {
                 $this->updateSystemRecords();
             }
             // Perform Update
             $bind[] = $this->id;
             $this->db->Execute("UPDATE dns_zones SET {$set}, `dtlastmodified` = NOW() WHERE id = ?", $bind);
             //TODO:
             if ($update_system_records) {
                 $this->db->Execute("UPDATE dns_zones SET status=?, `dtlastmodified` = NOW() WHERE id = ?", array($this->status, $this->id));
             }
         } else {
             // Perform Insert
             $this->db->Execute("INSERT INTO dns_zones SET {$set}", $bind);
             $this->id = $this->db->Insert_ID();
             if ($update_system_records) {
                 $this->updateSystemRecords();
                 $this->db->Execute("UPDATE dns_zones SET status=?, `dtlastmodified` = NOW() WHERE id = ?", array($this->status, $this->id));
             }
         }
         if ($this->updateRecords) {
             $this->db->Execute("DELETE FROM dns_zone_records WHERE zone_id=? AND issystem='0'", array($this->id));
             foreach ($this->records as $record) {
                 //UNIQUE KEY `zoneid` (`zone_id`,`type`(1),`value`,`name`)
                 $this->db->Execute("\n                        INSERT INTO dns_zone_records\n                        SET `zone_id` = ?,\n                            `type` = ?,\n                            `value` = ?,\n                            `name` = ?,\n                            `issystem` = '0',\n                            `ttl` = ?,\n                            `priority` = ?,\n                            `weight` = ?,\n                            `port` = ?\n                        ON DUPLICATE KEY UPDATE\n                            `issystem` = '0',\n                            `ttl` = ?,\n                            `priority` = ?,\n                            `weight` = ?,\n                            `port` = ?\n                    ", array($this->id, $record['type'], $record['value'], $record['name'], (int) $record['ttl'], (int) $record['priority'], (int) $record['weight'], (int) $record['port'], (int) $record['ttl'], (int) $record['priority'], (int) $record['weight'], (int) $record['port']));
             }
         }
     } catch (Exception $e) {
         $this->db->RollbackTrans();
         throw new Exception("Cannot save DBDNS zone. Error: " . $e->getMessage(), $e->getCode());
     }
     $this->db->CommitTrans();
     try {
         //$this->saveInPowerDns();
     } catch (Exception $e) {
         Logger::getLogger("DNS")->fatal("Unable to save data in PowerDNS db: {$e->getMessage()}");
     }
 }
Esempio n. 2
0
 /**
  * Saves price
  *
  * @param  PriceHistoryEntity $price The PriceHistoryEntity with details set.
  * @throws \InvalidArgumentException
  * @throws \ADODB_Exception
  */
 public function save(PriceHistoryEntity $price)
 {
     if (!isset($price->platform) || !isset($price->cloudLocation)) {
         throw new \InvalidArgumentException(sprintf("Both platform and cloudLocation properties must be set"));
     }
     if (!isset($price->applied)) {
         $price->applied = new \DateTime('now', new \DateTimeZone('UTC'));
     } else {
         if (!$price->applied instanceof \DateTime) {
             $price->applied = new \DateTime($price->applied, new \DateTimeZone('UTC'));
         }
     }
     if (!$price->priceId) {
         //Trying to find if the price already exists on this day
         $found = PriceHistoryEntity::findOne([['platform' => $price->platform], ['url' => $price->url ?: ''], ['cloudLocation' => $price->cloudLocation], ['accountId' => $price->accountId ?: 0], ['applied' => $price->applied->format('Y-m-d')]]);
         if ($found) {
             $price->priceId = $found->priceId;
         }
     }
     if (!$price->priceId) {
         $bNew = true;
         $price->priceId = \Scalr::GenerateUID();
     } else {
         $bNew = false;
     }
     $sId = "`price_id` = " . $price->qstr('priceId');
     $this->cadb->BeginTrans();
     try {
         $this->cadb->Execute("\n                " . ($bNew ? "INSERT" : "UPDATE") . " " . $price->table() . "\n                SET " . ($bNew ? $sId . "," : "") . "\n                    platform = ?,\n                    url = ?,\n                    cloud_location = ?,\n                    account_id = ?,\n                    applied = ?,\n                    deny_override = ?\n                " . ($bNew ? "" : "WHERE " . $sId . " LIMIT 1") . "\n            ", [$price->platform, $price->url ?: '', $price->cloudLocation, $price->accountId ?: 0, $price->applied->format('Y-m-d'), $price->denyOverride ? 1 : 0]);
         //Removes previous values
         if (!$bNew) {
             $this->cadb->Execute("DELETE FROM `prices` WHERE price_id = " . $price->qstr('priceId'));
         }
         $stmt = "";
         foreach ($price->getDetails() as $priceEntity) {
             if ($priceEntity instanceof PriceEntity) {
                 $stmt .= ", (" . $price->qstr('priceId') . ", " . $priceEntity->qstr('instanceType') . ", " . $priceEntity->qstr('os') . ", " . $priceEntity->qstr('name') . ", " . $priceEntity->qstr('cost') . ")";
             } else {
                 throw new \InvalidArgumentException(sprintf("Details should contain collection of the PriceEntity objects. %s given.", gettype($priceEntity)));
             }
         }
         if ($stmt !== '') {
             $this->cadb->Execute("REPLACE `prices` (`price_id`,`instance_type`, `os`, `name`, `cost`) VALUES " . ltrim($stmt, ','));
         }
         $this->cadb->CommitTrans();
     } catch (\Exception $e) {
         $this->cadb->RollbackTrans();
         throw $e;
     }
 }