2015-01-23 13:09:30 +00:00
|
|
|
<?php
|
|
|
|
|
2016-07-22 20:00:27 +00:00
|
|
|
use Vn\Lib;
|
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
|
|
|
|
|
|
|
class Query extends Vn\Web\JsonRequest
|
2015-01-23 13:09:30 +00:00
|
|
|
{
|
2016-09-06 14:25:02 +00:00
|
|
|
const PARAMS = ['sql'];
|
2016-09-23 22:47:34 +00:00
|
|
|
const SECURITY = Security::INVOKER;
|
2015-01-23 13:09:30 +00:00
|
|
|
|
2016-09-06 14:25:02 +00:00
|
|
|
function run ($db)
|
|
|
|
{
|
2015-01-23 13:09:30 +00:00
|
|
|
$results = [];
|
|
|
|
|
|
|
|
try {
|
2016-09-24 14:32:31 +00:00
|
|
|
$db->multiQuery ($_REQUEST['sql']);
|
2015-02-08 15:38:38 +00:00
|
|
|
|
2015-01-23 13:09:30 +00:00
|
|
|
do {
|
2016-09-24 14:32:31 +00:00
|
|
|
$result = $db->storeResult ();
|
2015-01-23 13:09:30 +00:00
|
|
|
|
|
|
|
if ($result !== FALSE)
|
|
|
|
{
|
2016-09-06 14:25:02 +00:00
|
|
|
$results[] = $this->transformResult ($result);
|
2015-01-23 13:09:30 +00:00
|
|
|
$result->free ();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
$results[] = TRUE;
|
|
|
|
}
|
2016-09-24 14:32:31 +00:00
|
|
|
while ($db->moreResults () && $db->nextResult ());
|
2016-09-06 14:25:02 +00:00
|
|
|
|
2015-08-17 18:02:14 +00:00
|
|
|
// Checks for warnings
|
2016-09-06 14:25:02 +00:00
|
|
|
|
2016-09-24 14:32:31 +00:00
|
|
|
if ($db->checkWarnings ()
|
|
|
|
&& ($result = $db->query ('SHOW WARNINGS')))
|
2015-08-17 18:02:14 +00:00
|
|
|
{
|
|
|
|
$sql = 'SELECT description, @warn code '.
|
|
|
|
'FROM sql_message WHERE code = @warn';
|
|
|
|
|
|
|
|
while ($row = $result->fetch_assoc ())
|
|
|
|
{
|
|
|
|
if ($row['Code'] == 1265
|
2016-09-24 14:32:31 +00:00
|
|
|
&& ($warning = $db->getRow ($sql)))
|
2016-09-19 06:40:18 +00:00
|
|
|
trigger_error ("{$warning['code']}: {$warning['description']}", E_USER_WARNING);
|
2015-08-17 18:02:14 +00:00
|
|
|
else
|
2016-09-19 06:40:18 +00:00
|
|
|
trigger_error ("{$row['Code']}: {$row['Message']}", E_USER_WARNING);
|
2015-08-17 18:02:14 +00:00
|
|
|
}
|
|
|
|
}
|
2016-09-06 14:25:02 +00:00
|
|
|
|
2015-08-17 18:02:14 +00:00
|
|
|
// Checks for errors
|
2015-03-19 19:36:11 +00:00
|
|
|
|
2016-09-24 14:32:31 +00:00
|
|
|
$db->checkError ();
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
2016-07-22 20:00:27 +00:00
|
|
|
catch (Vn\Db\Exception $e)
|
2015-01-23 13:09:30 +00:00
|
|
|
{
|
2017-05-02 12:33:48 +00:00
|
|
|
if ($e->getCode () == 1644)
|
2015-08-17 18:02:14 +00:00
|
|
|
{
|
2017-05-02 12:33:48 +00:00
|
|
|
$dbMessage = $e->getMessage ();
|
|
|
|
$sql = 'SELECT description FROM sql_message WHERE code = #';
|
|
|
|
$message = $db->getValue ($sql, [$dbMessage]);
|
2015-08-17 18:02:14 +00:00
|
|
|
|
2017-05-02 12:33:48 +00:00
|
|
|
if ($message)
|
|
|
|
throw new Lib\UserException ($message, $dbMessage);
|
2015-08-17 18:02:14 +00:00
|
|
|
}
|
|
|
|
|
2017-05-02 12:33:48 +00:00
|
|
|
throw $e;
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
2016-09-06 14:25:02 +00:00
|
|
|
**/
|
|
|
|
function transformResult ($result)
|
|
|
|
{
|
|
|
|
$tableMap = [];
|
|
|
|
$columns = $result->fetch_fields ();
|
|
|
|
|
|
|
|
$resultMap =
|
|
|
|
[
|
|
|
|
'data' => [],
|
|
|
|
'columns' => [],
|
|
|
|
'tables' => []
|
|
|
|
];
|
2015-02-17 11:48:53 +00:00
|
|
|
|
2016-09-06 14:25:02 +00:00
|
|
|
for ($i = 0; $i < $result->field_count; $i++)
|
|
|
|
{
|
|
|
|
$column = $columns[$i];
|
|
|
|
|
|
|
|
switch ($column->type)
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
$default = $this->castValue ($column->def, $type);
|
|
|
|
|
|
|
|
$resultMap['columns'][] =
|
|
|
|
[
|
|
|
|
'type' => $type,
|
|
|
|
'flags' => $column->flags,
|
|
|
|
'def' => $default,
|
|
|
|
'name' => $column->name,
|
|
|
|
'orgname' => $column->orgname,
|
|
|
|
'table' => $tableIndex
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
$columns = $resultMap['columns'];
|
|
|
|
|
|
|
|
while ($row = $result->fetch_row ())
|
|
|
|
{
|
|
|
|
for ($j = 0; $j < $result->field_count; $j++)
|
2016-09-26 09:28:47 +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.
|
|
|
|
**/
|
2016-09-26 09:28:47 +00:00
|
|
|
function castValue ($value, $type)
|
2015-02-17 11:48:53 +00:00
|
|
|
{
|
|
|
|
if ($value !== NULL)
|
|
|
|
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;
|
|
|
|
case Type::DATE:
|
|
|
|
case Type::DATE_TIME:
|
|
|
|
return mktime
|
2015-02-17 11:48:53 +00:00
|
|
|
(
|
|
|
|
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;
|
|
|
|
}
|
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
|
|
|
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
|