0
1
Fork 0
hedera-web-mindshore/package/usr/share/hedera-web/rest/query.php

138 lines
2.8 KiB
PHP
Executable File

<?php
use Vn\Rest;
use Vn\Db;
class RestMod extends Rest\Module
{
function run ()
{
if (!isset ($_REQUEST['sql']) || $_REQUEST['sql'] == '')
throw new Rest\Exception ('Conn', 'emptyQuery', s('EmptyQuery'));
$results = [];
$conn = $this->conn;
try {
$conn->multiQuery ($_REQUEST['sql']);
do {
$result = $conn->storeResult ();
if ($result !== FALSE)
{
$columns = $result->fetch_fields ();
$resultMap =
[
'data' => [],
'field' => []
];
for ($i = 0; $i < $result->field_count; $i++)
{
$column = $columns[$i];
switch ($column->type)
{
case MYSQLI_TYPE_BIT:
$type = TYPE_BOOLEAN;
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:
$type = TYPE_INTEGER;
break;
case MYSQLI_TYPE_FLOAT:
case MYSQLI_TYPE_DOUBLE:
case MYSQLI_TYPE_DECIMAL:
case MYSQLI_TYPE_NEWDECIMAL:
$type = TYPE_DOUBLE;
break;
case MYSQLI_TYPE_DATE:
$type = TYPE_DATE;
break;
case MYSQLI_TYPE_DATETIME:
case MYSQLI_TYPE_TIMESTAMP:
$type = TYPE_DATE_TIME;
break;
default;
$type = TYPE_STRING;
}
$default = $this->castValue ($column->def, $type);
$resultMap['field'][] =
[
'name' => $column->name,
'orgname' => $column->orgname,
'table' => $column->table,
'orgtable' => $column->orgtable,
'def' => $default,
'db' => $column->db,
'flags' => $column->flags,
'type' => $type
];
}
$columns = $resultMap['field'];
while ($row = $result->fetch_row ())
{
for ($j = 0; $j < $result->field_count; $j++)
$this->castValue ($row[$j], $columns[$j]['type']);
$resultMap['data'][] = $row;
}
$results[] = $resultMap;
$result->free ();
}
else
$results[] = TRUE;
}
while ($conn->moreResults () && $conn->nextResult ());
}
catch (Db\Exception $e)
{
throw new Rest\Exception ('Conn', $e->getCode (), $e->getMessage ());
}
return $results;
}
function castValue (&$value, $type)
{
if ($value !== NULL)
switch ($type)
{
case TYPE_BOOLEAN:
$value = (bool) $value;
break;
case TYPE_INTEGER:
$value = (int) $value;
break;
case TYPE_DOUBLE:
$value = (float) $value;
break;
case TYPE_DATE:
case TYPE_DATE_TIME:
$value = mktime
(
substr ($value, 11 , 2)
,substr ($value, 14 , 2)
,substr ($value, 17 , 2)
,substr ($value, 5 , 2)
,substr ($value, 8 , 2)
,substr ($value, 0 , 4)
);
break;
}
}
}
?>