diff --git a/vendor/action/log/log_save.php b/vendor/action/log/log_save.php index 0cd531d..12c4942 100644 --- a/vendor/action/log/log_save.php +++ b/vendor/action/log/log_save.php @@ -10,6 +10,7 @@ use vendor\model\Backup; use vendor\model\Station; use vendor\model\StationDisk; use vendor\model\StationFolder; +use vendor\model\Database; use vendor\controller\log\LogValidate; try { @@ -19,6 +20,7 @@ try { $Client = new Client(); $Backup = new Backup(); $Station = new Station(); + $Database = new Database(); $StationDisk = new StationDisk(); $LogValidate = new LogValidate(); $StationFolder = new StationFolder(); @@ -239,6 +241,16 @@ try { ); } + + /** Exclui o registro anterior sobre o banco de dados */ + $Database->Delete($clientId); + + /** Grava os novos dados do banco de dados */ + $Database->Save($clientId, + $jsonLog->database->file_size, + $jsonLog->database->last_modified, + $jsonLog->database->db_accessible); + /** Exclui o log anterior da estação correspondente */ $Log->Delete($clientId, $stationId); diff --git a/vendor/model/Database.class.php b/vendor/model/Database.class.php new file mode 100644 index 0000000..df62334 --- /dev/null +++ b/vendor/model/Database.class.php @@ -0,0 +1,235 @@ +connection = new Mysql(); + } + + /** Carrega os campos de uma tabela */ + public function Describe() + { + + /** Consulta SQL */ + $this->sql = "describe db"; + + /** Preparo o SQL para execução */ + $this->stmt = $this->connection->connect()->prepare($this->sql); + + /** Executo o SQL */ + $this->stmt->execute(); + + /** Retorno o resultado */ + $this->field = $this->stmt->fetchAll(\PDO::FETCH_OBJ); + + /** Declara o objeto */ + $resultDescribe = new \stdClass(); + $Field = ''; + + /** Lista os campos da tabela para objetos */ + foreach($this->field as $UsersKey => $Result){ + + /** Pega o nome do Field/Campo */ + $Field = $Result->Field; + + /** Carrega os objetos como null */ + $resultDescribe->$Field = null; + + } + + /** Retorna os campos declarados como vazios */ + return $resultDescribe; + + } + + /** Lista os registros do banco de dados com limitação */ + public function Get(int $dbId) + { + + /** Parametros de entrada */ + $this->dbId = $dbId; + + /** Consulta SQL */ + $this->sql = 'select * from db + where db_id = :db_id'; + + /** Preparo o SQL para execução */ + $this->stmt = $this->connection->connect()->prepare($this->sql); + + /** Preencho os parâmetros do SQL */ + $this->stmt->bindParam(':db_id', $this->dbId); + + /** Executo o SQL */ + $this->stmt->execute(); + + /** Retorno o resultado */ + return $this->stmt->fetchObject(); + + } + + /** Insere um novo registro no banco */ + public function Save(int $clientId, + ? string $fileSize, + ? string $lastModified, + ? string $dbAccessible) + { + + + /** Parametros */ + $this->clientId = $clientId; + $this->fileSize = $fileSize; + $this->lastModified = $lastModified; + $this->dbAccessible = $dbAccessible; + + /** Consulta SQL */ + $this->sql = 'insert into db(client_id, + file_size, + last_modified, + db_accessible + ) values (:client_id, + :file_size, + :last_modified, + :db_accessible, + :capacity)'; + + /** Preparo o sql para receber os valores */ + $this->stmt = $this->connection->connect()->prepare($this->sql); + + + /** Inicia a transação */ + $this->connection->connect()->beginTransaction(); + + /** Preencho os parâmetros do SQL */ + $this->stmt->bindParam(':client_id', $this->clientId); + $this->stmt->bindParam(':file_size', $this->fileSize); + $this->stmt->bindParam(':last_modified', $this->lastModified); + $this->stmt->bindParam(':db_accessible', $this->dbAccessible); + + /** Executo o SQL */ + $this->stmt->execute(); + + } + + /** Deleta um determinado registro no banco de dados */ + function Delete(int $clientId) + { + /** Parametros de entrada */ + $this->clientId = $clientId; + + /** Consulta SQL */ + $this->sql = 'delete from db + where client_id = :client_id'; + + /** Preparo o sql para receber os valores */ + $this->stmt = $this->connection->connect()->prepare($this->sql); + + try { + + /** Inicia a transação */ + $this->connection->connect()->beginTransaction(); + + /** Preencho os parâmetros do SQL */ + $this->stmt->bindParam('client_id', $this->clientId); + + /** Executo o SQL */ + $this->stmt->execute(); + + /** Confirma a transação */ + $this->connection->connect()->commit(); + return true; + + } catch (\Exception $exception) { + + /** Desfaz a transação */ + $this->connection->connect()->rollback(); + + /** Captura o erro */ + array_push($this->errors, 'Error:: ' . $exception->getMessage()); + return false; + } + } + + /** Define o Último ID inserido */ + public function setId($lastId) : void + { + $this->lastId = $lastId; + } + + /** Recupera o Último ID inserido */ + public function getId(): ? int + { + return (int)$this->lastId; + } + + /** Retorna os erros ocorridos */ + public function getErrors(): ? string + { + + /** Verifico se deve informar os erros */ + if (count($this->errors)) { + + /** Verifica a quantidade de erros para informar a legenda */ + $this->info = count($this->errors) > 1 ? '
Os seguintes erros foram encontrados
' : '
O seguinte erro foi encontrado
'; + + /** Lista os erros */ + foreach ($this->errors as $keyError => $error) { + + /** Monto a mensagem de erro */ + $this->info .= '
' . ($keyError + 1) . ' - ' . $error; + + } + + /** Retorno os erros encontrados */ + return (string)$this->info; + + } else { + + return false; + + } + + } + + /** Fecha uma conexão aberta anteriormente com o banco de dados */ + function __destruct() + { + $this->connection = null; + } +}