hedera-web/rest/core/query.php

175 lines
3.9 KiB
PHP
Raw Permalink Normal View History

<?php
2016-07-22 20:00:27 +00:00
use Vn\Lib;
use Vn\Lib\Locale;
2016-09-23 22:47:34 +00:00
use Vn\Web\Security;
2016-09-26 09:28:47 +00:00
use Vn\Lib\Type;
2016-07-22 20:00:27 +00:00
2018-05-23 10:14:20 +00:00
class Query extends Vn\Web\JsonRequest {
2016-09-06 14:25:02 +00:00
const PARAMS = ['sql'];
2016-09-23 22:47:34 +00:00
const SECURITY = Security::INVOKER;
2020-05-03 20:35:24 +00:00
function __construct($app) {
parent::__construct($app);
$this->utcTz = new DateTimeZone('UTC');
$dbConf = $app->getConf()['db'];
if (isset($dbConf['tz']))
$this->tz = new DateTimeZone($dbConf['tz']);
else
$this->tz = $this->utcTz;
}
2018-05-23 10:14:20 +00:00
function run($db) {
$results = [];
2023-01-16 12:59:11 +00:00
$db->multiQuery($_REQUEST['sql']);
2023-01-16 12:59:11 +00:00
do {
$result = $db->storeResult();
2015-02-08 15:38:38 +00:00
2023-01-16 12:59:11 +00:00
if ($result !== FALSE) {
$results[] = $this->transformResult($result);
$result->free();
} else
$results[] = TRUE;
}
while ($db->moreResults() && $db->nextResult());
if ($db->checkWarnings()
&& ($result = $db->query('SHOW WARNINGS'))) {
$sql =
'SELECT IFNULL(i.`description`, m.`description`) `description`, @warn `code`
FROM `message` m
LEFT JOIN `messageI18n` i
ON i.`code` = m.`code` AND i.lang = #
WHERE m.`code` = @warn';
2023-01-16 12:59:11 +00:00
while ($row = $result->fetch_object()) {
if ($row->Code == 1265
&& ($warning = $db->getObject($sql, [Locale::get()])))
2023-01-16 12:59:11 +00:00
trigger_error("{$warning->code}: {$warning->description}", E_USER_WARNING);
else
trigger_error("{$row->Code}: {$row->Message}", E_USER_WARNING);
2015-08-17 18:02:14 +00:00
}
}
2023-01-16 12:59:11 +00:00
$db->checkError();
return $results;
}
2016-09-06 14:25:02 +00:00
/**
2016-09-20 18:36:22 +00:00
* Transforms the database result into a JSON parseable object.
2022-05-26 06:08:31 +00:00
*/
2018-05-23 10:14:20 +00:00
function transformResult($result) {
2016-09-06 14:25:02 +00:00
$tableMap = [];
2018-05-23 10:14:20 +00:00
$columns = $result->fetch_fields();
2016-09-06 14:25:02 +00:00
$resultMap =
[
'data' => [],
'columns' => [],
'tables' => []
];
2015-02-17 11:48:53 +00:00
2018-05-23 10:14:20 +00:00
for ($i = 0; $i < $result->field_count; $i++) {
2016-09-06 14:25:02 +00:00
$column = $columns[$i];
2018-05-23 10:14:20 +00:00
switch ($column->type) {
2016-09-06 14:25:02 +00:00
case MYSQLI_TYPE_BIT:
2016-09-26 09:28:47 +00:00
$type = Type::BOOLEAN;
2016-09-06 14:25:02 +00:00
break;
case MYSQLI_TYPE_TINY:
case MYSQLI_TYPE_SHORT:
case MYSQLI_TYPE_LONG:
case MYSQLI_TYPE_LONGLONG:
case MYSQLI_TYPE_INT24:
case MYSQLI_TYPE_YEAR:
2016-09-26 09:28:47 +00:00
$type = Type::INTEGER;
2016-09-06 14:25:02 +00:00
break;
case MYSQLI_TYPE_FLOAT:
case MYSQLI_TYPE_DOUBLE:
case MYSQLI_TYPE_DECIMAL:
case MYSQLI_TYPE_NEWDECIMAL:
2016-09-26 09:28:47 +00:00
$type = Type::DOUBLE;
2016-09-06 14:25:02 +00:00
break;
case MYSQLI_TYPE_DATE:
2016-09-26 09:28:47 +00:00
$type = Type::DATE;
2016-09-06 14:25:02 +00:00
break;
case MYSQLI_TYPE_DATETIME:
case MYSQLI_TYPE_TIMESTAMP:
2016-09-26 09:28:47 +00:00
$type = Type::DATE_TIME;
2016-09-06 14:25:02 +00:00
break;
default;
2016-09-26 09:28:47 +00:00
$type = Type::STRING;
2016-09-06 14:25:02 +00:00
}
2018-05-23 10:14:20 +00:00
if (!isset($tableMap[$column->table])) {
2016-09-06 14:25:02 +00:00
$resultMap['tables'][] =
[
'name' => $column->table,
'orgname' => $column->orgtable,
'schema' => $column->db,
'pks' => []
];
2018-05-23 10:14:20 +00:00
$tableIndex = count($resultMap['tables']) - 1;
2016-09-06 14:25:02 +00:00
$tableMap[$column->table] = $tableIndex;
2018-05-23 11:09:55 +00:00
} else
2016-09-06 14:25:02 +00:00
$tableIndex = $tableMap[$column->table];
if ($column->flags & MYSQLI_PRI_KEY_FLAG)
2022-05-28 15:49:46 +00:00
$resultMap['tables'][$tableIndex]['pks'][] = $column->name;
2016-09-06 14:25:02 +00:00
2018-05-23 10:14:20 +00:00
$default = $this->castValue($column->def, $type);
2016-09-06 14:25:02 +00:00
2022-05-28 15:49:46 +00:00
$resultMap['columns'][] = [
2016-09-06 14:25:02 +00:00
'type' => $type,
'flags' => $column->flags,
'def' => $default,
'name' => $column->name,
'orgname' => $column->orgname,
'table' => $tableIndex
];
}
$columns = $resultMap['columns'];
2018-05-23 10:14:20 +00:00
while ($row = $result->fetch_row()) {
2016-09-06 14:25:02 +00:00
for ($j = 0; $j < $result->field_count; $j++)
2018-05-23 10:14:20 +00:00
$row[$j] = $this->castValue($row[$j], $columns[$j]['type']);
2016-09-06 14:25:02 +00:00
$resultMap['data'][] = $row;
}
return $resultMap;
}
/**
* Transforms the database value into a JSON parseable value.
2022-05-26 06:08:31 +00:00
*/
2018-05-23 10:14:20 +00:00
function castValue($value, $type) {
2020-05-04 19:55:18 +00:00
if ($value === '' && $type != Type::STRING)
2020-05-03 20:35:24 +00:00
$value = NULL;
2020-05-04 19:55:18 +00:00
if ($value !== NULL)
2018-05-23 10:14:20 +00:00
switch ($type) {
2016-09-26 09:28:47 +00:00
case Type::BOOLEAN:
return (bool) $value;
case Type::INTEGER:
return (int) $value;
case Type::DOUBLE:
return (float) $value;
2020-05-03 20:35:24 +00:00
case Type::DATE:
case Type::DATE_TIME:
$d = new DateTime($value, $this->tz);
$d->setTimeZone($this->utcTz);
return $d->format('Y-m-d\TH:i:s.v\Z');
2015-02-17 11:48:53 +00:00
break;
}
2016-09-26 09:28:47 +00:00
return $value;
2015-02-17 11:48:53 +00:00
}
2016-09-06 14:25:02 +00:00
}