2015-01-23 13:09:30 +00:00
|
|
|
<?php
|
|
|
|
|
2015-01-31 01:05:12 +00:00
|
|
|
use Vn\Rest;
|
2015-02-08 15:38:38 +00:00
|
|
|
use Vn\Db;
|
2015-01-31 01:05:12 +00:00
|
|
|
|
|
|
|
class RestMod extends Rest\Module
|
2015-01-23 13:09:30 +00:00
|
|
|
{
|
|
|
|
function run ()
|
|
|
|
{
|
|
|
|
if (!isset ($_REQUEST['sql']) || $_REQUEST['sql'] == '')
|
|
|
|
throw new Rest\Exception ('Conn', 'emptyQuery', s('EmptyQuery'));
|
|
|
|
|
|
|
|
$results = [];
|
|
|
|
$conn = $this->conn;
|
|
|
|
|
|
|
|
try {
|
2015-02-08 15:38:38 +00:00
|
|
|
$conn->multiQuery ($_REQUEST['sql']);
|
|
|
|
|
2015-01-23 13:09:30 +00:00
|
|
|
do {
|
|
|
|
$result = $conn->storeResult ();
|
|
|
|
|
|
|
|
if ($result !== FALSE)
|
|
|
|
{
|
2015-03-19 19:36:11 +00:00
|
|
|
$tableMap = [];
|
2015-01-23 13:09:30 +00:00
|
|
|
$columns = $result->fetch_fields ();
|
|
|
|
|
2015-02-17 11:48:53 +00:00
|
|
|
$resultMap =
|
|
|
|
[
|
2015-03-19 19:36:11 +00:00
|
|
|
'data' => [],
|
|
|
|
'columns' => [],
|
|
|
|
'tables' => []
|
2015-02-17 11:48:53 +00:00
|
|
|
];
|
|
|
|
|
2015-02-08 15:38:38 +00:00
|
|
|
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;
|
|
|
|
}
|
2015-03-19 19:36:11 +00:00
|
|
|
|
|
|
|
if (!isset ($tableMap[$column->table]))
|
|
|
|
{
|
|
|
|
$resultMap['tables'][] =
|
|
|
|
[
|
|
|
|
'name' => $column->table,
|
|
|
|
'orgname' => $column->orgtable,
|
|
|
|
'schema' => $column->db,
|
|
|
|
'pks' => []
|
|
|
|
];
|
|
|
|
$tableIndex = count ($resultMap['tables']) - 1;
|
|
|
|
$tableMap[$column->table] = $tableIndex;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
$tableIndex = $tableMap[$column->table];
|
|
|
|
|
|
|
|
if ($column->flags & MYSQLI_PRI_KEY_FLAG)
|
|
|
|
$resultMap['tables'][$tableIndex]['pks'][] = $i;
|
2015-02-17 11:48:53 +00:00
|
|
|
|
|
|
|
$default = $this->castValue ($column->def, $type);
|
2015-02-08 15:38:38 +00:00
|
|
|
|
2015-03-19 19:36:11 +00:00
|
|
|
$resultMap['columns'][] =
|
2015-02-08 15:38:38 +00:00
|
|
|
[
|
2015-03-19 19:36:11 +00:00
|
|
|
'type' => $type,
|
|
|
|
'flags' => $column->flags,
|
|
|
|
'def' => $default,
|
2015-02-08 15:38:38 +00:00
|
|
|
'name' => $column->name,
|
|
|
|
'orgname' => $column->orgname,
|
2015-03-19 19:36:11 +00:00
|
|
|
'table' => $tableIndex
|
2015-02-08 15:38:38 +00:00
|
|
|
];
|
|
|
|
}
|
2015-02-17 11:48:53 +00:00
|
|
|
|
2015-03-19 19:36:11 +00:00
|
|
|
$columns = $resultMap['columns'];
|
2015-01-23 13:09:30 +00:00
|
|
|
|
|
|
|
while ($row = $result->fetch_row ())
|
|
|
|
{
|
|
|
|
for ($j = 0; $j < $result->field_count; $j++)
|
2015-02-17 11:48:53 +00:00
|
|
|
$this->castValue ($row[$j], $columns[$j]['type']);
|
2015-01-23 13:09:30 +00:00
|
|
|
|
|
|
|
$resultMap['data'][] = $row;
|
|
|
|
}
|
|
|
|
|
|
|
|
$results[] = $resultMap;
|
|
|
|
$result->free ();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
$results[] = TRUE;
|
|
|
|
}
|
|
|
|
while ($conn->moreResults () && $conn->nextResult ());
|
2015-03-19 19:36:11 +00:00
|
|
|
|
|
|
|
$conn->checkError ();
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
catch (Db\Exception $e)
|
|
|
|
{
|
|
|
|
throw new Rest\Exception ('Conn', $e->getCode (), $e->getMessage ());
|
|
|
|
}
|
|
|
|
|
|
|
|
return $results;
|
|
|
|
}
|
2015-02-17 11:48:53 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
?>
|