Files
PVM/public_html/classes/Utility.Db.TempTable.class.php
2021-10-20 16:32:11 +02:00

162 lines
5.2 KiB
PHP

<?php
namespace Utility\Db;
class TempTable {
private $prefix = null;
private $source = null;
const prefixPVM = "t1_PVM"; // 60 * 60 (1 ora in secondi)
const lifetime = 3600; // 60 * 60 (1 ora in secondi)
public function prefix($value = null) {
if (!is_null($value)) {
$this->prefix = \Utility\Str::remove_spaces(\Utility\Str::alphanumericFilter(\Utility\Str::normalize($value), false));
}
return $this;
}
public function source($value = null) {
$this->source = $value;
return $this;
}
private function get_fieldsInfo() {
if (!is_null($this->source) && count($this->source) > 0) {
$arr_fields = array();
foreach ($this->source as $row) {
foreach ($row as $key => $value) {
$type = !is_null($value) ? gettype($value) : null;
if (!isset($arr_fields[$key])) {
$arr_fields[$key] = array("name" => $key, "type" => $type);
} else {
if (is_null($arr_fields[$key]["type"]) && !is_null($type)) {
$arr_fields[$key]["type"] = $type; // correzione se prec. nullo
} else if ($arr_fields[$key]["type"] == "integer" && $type == "double") {
$arr_fields[$key]["type"] = "double"; // correzione se prec. int e dopo riceve double
}
}
}
}
return array_values($arr_fields);
}
return null;
}
private static function create($arr_fields, $tableName, &$Query) {
$sql = "CREATE TABLE {$tableName} (";
foreach ($arr_fields as $field) {
$sql .= $field["name"] . " ";
if ($field["type"] == "string") {
$sql .= "VARCHAR(MAX)";
} else if ($field["type"] == "integer") {
$sql .= "INTEGER";
} else if ($field["type"] == "double") {
$sql .= "DECIMAL(20, 5)";
} else if (is_null($field["type"])) {
$sql .= "VARCHAR(1)";
}
$sql .= " NULL,";
}
$sql = rtrim($sql, ",") . ")";
return $Query->setSql($sql)->toRet()->execute();
}
private static function insert($arr_rows, $arr_fields, $tableName, &$Query) {
$Ret = new \Ret;
foreach ($arr_rows as $row) {
$QueryB = new \Query;
$QueryB->insert($tableName);
foreach ($arr_fields as $field) {
$fieldName = $field["name"];
$value = isset($row[$fieldName]) ? $row[$fieldName] : null;
$QueryB->value($fieldName, $value);
}
$Ret = $Query->setSql($QueryB->getSql())->toRet()->execute();
if (!$Ret->is_OK()) {
break;
}
}
return $Ret;
}
public function run() {
self::clean();
$Ret = new \Ret;
$arr_rows = $this->source;
if (!is_null($arr_rows) && count($arr_rows) > 0) {
$Query = new \Query;
$Query->begin_transaction();
$arr_fields = $this->get_fieldsInfo();
$tableName = $this->gen_tableName();
$Ret = self::create($arr_fields, $tableName, $Query);
if ($Ret->is_OK()) {
$Ret = self::insert($arr_rows, $arr_fields, $tableName, $Query);
}
if ($Ret->is_OK()) {
$Query->commit();
$Ret->set_string($tableName);
} else {
$Query->rollback();
}
} else {
$Ret->set_error("Nessuna riga ricevuta");
}
return $Ret;
}
private function gen_tableName($c = null) {
$prefix = $this->prefix;
$effPrefix = self::prefixPVM . (!is_null($prefix) ? "_" . $prefix : ".") . "__" . \Utility\Date::getNow();
if (!is_null($c)) {
$effPrefix .= "-{$c}";
}
if (\Utility\Db::table_exists($effPrefix)) {
if (is_null($c)) {
$c = 0;
}
return $this->gen_tableName($c + 1);
}
return $effPrefix;
}
private static function clean($lifetime = null) { // $lifetime in sec.
if (is_null($lifetime) || is_int($lifetime)) {
$lifetime = self::lifetime;
}
$sql = "SELECT TABLE_NAME
FROM ( SELECT substring(TABLE_NAME, CHARINDEX('__',TABLE_NAME)+2, LEN(TABLE_NAME)) AS t_table,
dbo.UNIX_TIMESTAMP(GETDATE()) AS t_now,
TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'dbo' AND
TABLE_NAME LIKE '" . self::prefixPVM . "%'
) t
WHERE (t_now-t_table) > {$lifetime}";
$Query = new \Query($sql);
$Ret = $Query->firstColumn()->toRet()->execute();
if ($Ret->is_OK()) {
$arr_tables = $Ret->get_data();
foreach ($arr_tables as $table) {
\Utility\Db::table_drop($table);
}
}
}
}