Esempio n. 1
0
 /**
  * Saves an entity to database
  *
  * @return \Scalr\Server\History
  * @throws Exception
  */
 public function save()
 {
     $stmt = array();
     $bind = array();
     $idKey = 'id';
     $idValue = array();
     $cols = array();
     foreach ($this->_getFields() as $field) {
         $cols[$field] = $this->{$field};
     }
     if (array_key_exists($idKey, $cols)) {
         if ($cols[$idKey]) {
             $idValue[] = $cols[$idKey];
         }
         unset($cols[$idKey]);
     }
     foreach ($cols as $field => $value) {
         $stmt[] = "`" . Scalr::decamelize($field) . "` = ?";
         $bind[] = $value;
     }
     try {
         $stmt = (empty($idValue) ? "INSERT" : "UPDATE") . " `servers_history` SET " . join(", ", $stmt) . (!empty($idValue) ? " WHERE `" . Scalr::decamelize($idKey) . "` = ?" : "");
         $this->db->Execute($stmt, array_merge($bind, $idValue));
         if (empty($idValue)) {
             $this->{$idKey} = $this->db->Insert_ID();
         }
     } catch (Exception $e) {
         throw new Exception(sprintf("Cannot save server history record. Error: %s", $e->getMessage()), $e->getCode());
     }
     return $this;
 }
Esempio n. 2
0
 public function save($forceInsert = false)
 {
     $set = array();
     $bind = array();
     foreach ($this->dbPropertyMap as $field => $value) {
         $isArrayValue = is_array($value);
         if ($field == $this->dbPrimaryKey && !$forceInsert) {
             continue;
         }
         if ($isArrayValue && isset($value['createSql']) && (!$this->id || $forceInsert)) {
             $set[] = "`{$field}` = {$value['createSql']}";
             continue;
         }
         if ($isArrayValue && isset($value['updateSql']) && $this->id && !$forceInsert) {
             $set[] = "`{$field}` = {$value['updateSql']}";
             continue;
         }
         if ($isArrayValue && isset($value['update']) && $value['update'] == false) {
             continue;
         }
         $property = $isArrayValue ? $value['property'] : $value;
         $val = $this->{$property};
         if ($isArrayValue && isset($value['type'])) {
             switch ($value['type']) {
                 case 'bool':
                     $val = $val ? 1 : 0;
                     break;
                 case 'datetime':
                     $val = is_null($val) ? $val : date("Y-m-d H:m:s", $val);
                     break;
                 case 'serialize':
                     $val = serialize($val);
                     break;
             }
         }
         $set[] = "`{$field}` = ?";
         $bind[] = $val;
     }
     $set = implode(', ', $set);
     try {
         if ($this->id && !$forceInsert) {
             // Perform Update
             $bind[] = $this->id;
             $this->db->Execute("UPDATE {$this->dbTableName} SET {$set} WHERE id = ?", $bind);
         } else {
             // Perform Insert
             $this->db->Execute("INSERT INTO {$this->dbTableName} SET {$set}", $bind);
             if (!$this->id) {
                 $this->id = $this->db->Insert_ID();
             }
         }
     } catch (Exception $e) {
         throw new Exception(sprintf(_("Cannot save record. Error: %s"), $e->getMessage()), $e->getCode());
     }
     return $this;
 }
Esempio n. 3
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. 4
0
File: Acl.php Progetto: recipe/scalr
 /**
  * Set roles for specified user for specified team.
  *
  * @param   int        $teamId       The identifier of the team
  * @param   int        $userId       The identifier of the user
  * @param   array      $accountRoles The list of the identifiers of the roles of account level
  * @param   int        $accountId    optional The identifier of the account
  */
 public function setUserRoles($teamId, $userId, $accountRoles, $accountId = null)
 {
     $accountId = intval($accountId);
     //Verify that team and user are from the same acount
     if (!empty($accountId)) {
         $check = $this->db->GetOne("\n                SELECT 1 FROM account_users WHERE id = ? AND account_id = ? LIMIT 1\n            ", array($userId, $accountId)) && $this->db->GetOne("\n                SELECT 1 FROM account_teams WHERE id = ? AND account_id = ? LIMIT 1\n            ", array($teamId, $accountId));
         if (!$check) {
             throw new Exception\AclException(sprintf('Cannot find the team "%d" or user "%d" in the account "%d"', $teamId, $userId, $accountId));
         }
     } else {
         //Retrieves identifier of the account
         $accountId = $this->db->GetOne("\n                SELECT u.account_id\n                FROM account_users u\n                JOIN account_teams t ON t.account_id = u.account_id\n                WHERE u.user_id = ? AND t.team_id = ?\n                LIMIT 1\n            ", array($userId, $accountId));
         if (!$accountId) {
             throw new Exception\AclException(sprintf('Cannot find the team "%d" or user "%d" in the account "%d"', $teamId, $userId, $accountId));
         }
     }
     $teamUserId = $this->db->GetOne("\n           SELECT tu.id\n           FROM `account_team_users` tu\n           WHERE tu.`team_id` = ? AND tu.`user_id` = ?\n           LIMIT 1\n        ", array($teamId, $userId));
     if (empty($teamUserId)) {
         $this->db->Execute("\n                INSERT IGNORE `account_team_users`\n                SET team_id = ?,\n                    user_id = ?\n            ", array($teamId, $userId));
         $teamUserId = $this->db->Insert_ID();
     } else {
         //Removes previous relations
         $this->db->Execute("\n                DELETE FROM `account_team_user_acls` WHERE account_team_user_id = ?\n            ", array($teamUserId));
     }
     if ($c = count($accountRoles)) {
         //Creates new relations
         $this->db->Execute("\n                INSERT IGNORE `account_team_user_acls` (account_team_user_id, account_role_id)\n                SELECT ?, r.account_role_id\n                FROM `acl_account_roles` r\n                WHERE r.account_id = ?\n                AND r.account_role_id IN (" . rtrim(str_repeat("?,", $c), ',') . ")\n            ", array_merge(array($teamUserId, $accountId), array_values($accountRoles)));
     }
 }