foreach ($GLOBALS['_PUT'] as $key => $value) { $data[$key] = sprintf('"%s" = ?', $key); } $query = array(sprintf('UPDATE "%s" SET %s WHERE "%s" = ?', $table, implode(', ', $data), 'id')); $query = sprintf('%s;', implode(' ', $query)); $result = ArrestDB::Query($query, $GLOBALS['_PUT'], $id); if ($result === false) { $result = ArrestDB::$HTTP[409]; } else { $result = ArrestDB::$HTTP[200]; } } } return ArrestDB::Reply($result); }); exit(ArrestDB::Reply(ArrestDB::$HTTP[400])); class ArrestDB { public static $HTTP = [200 => ['success' => ['code' => 200, 'status' => 'OK']], 201 => ['success' => ['code' => 201, 'status' => 'Created']], 204 => ['error' => ['code' => 204, 'status' => 'No Content']], 400 => ['error' => ['code' => 400, 'status' => 'Bad Request']], 403 => ['error' => ['code' => 403, 'status' => 'Forbidden']], 404 => ['error' => ['code' => 404, 'status' => 'Not Found']], 409 => ['error' => ['code' => 409, 'status' => 'Conflict']], 503 => ['error' => ['code' => 503, 'status' => 'Service Unavailable']]]; public static function Query($query = null) { static $db = null; static $result = []; try { if (isset($db, $query) === true) { if (strncasecmp($db->getAttribute(\PDO::ATTR_DRIVER_NAME), 'mysql', 5) === 0) { $query = strtr($query, '"', '`'); } if (empty($result[$hash = crc32($query)]) === true) { $result[$hash] = $db->prepare($query); }
$result = ['error' => 'Error retrieving inserted rows.']; } else { if (count($ids) === 1) { $result = array_shift($result); } } } return ArrestDB::Reply($result); }); ArrestDB::Serve('PUT', '/(#any)/(#num)', function ($table, $id) { if (empty($GLOBALS['_PUT']) === true || is_array($GLOBALS['_PUT']) !== true) { http_response_code(400); return false; } $data = []; foreach ($GLOBALS['_PUT'] as $key => $value) { $data[$key] = sprintf('"%s" = ?', $key); } $query = array(sprintf('UPDATE "%s" SET %s WHERE "%s" = ?', $table, implode(', ', $data), 'id')); $query = sprintf('%s;', implode(' ', $query)); $result = ArrestDB::Query($query, $GLOBALS['_PUT'], $id); if ($result === false) { http_response_code(404); return; } else { //$result = ArrestDB::$HTTP[200]; } return ArrestDB::Reply($result); }); http_response_code(400); exit('Request failed to match');
public static function getQuery($query, $extends = null, $id = "") { $table = $query["TABLE"]; $query = ArrestDB::PrepareQueryGET($query, false); $result = ArrestDB::Query($query); if ($result === false || count($result) == 0) { return null; } else { if (isset($result[0])) { foreach ($result as $k => $object) { $result[$k]["__table"] = $table; } } else { $result["__table"] = $table; } } if (isset($extends) === true) { $extends = explode(",", $extends); try { $result = ArrestDB::Extend($result, $extends); } catch (Exception $e) { $result = ArrestDB::$HTTP[$e->getCode()]; $result["error"]["detail"] = $e->getMessage(); return ArrestDB::Reply($result); } } if (function_exists("ArrestDB_postProcess")) { $result = ArrestDB_postProcess('GET', $table, $id, $result); } return ArrestDB::ObfuscateId($result); }
} if ($rowCount != $result['result']->rowCount()) { syslog(LOG_ERR, sprintf('Archived unloaded %d rows but delete removed %d rows - rolling back.', $rowCount, $result['result']->rowCount())); $result = ArrestDB::Query('ROLLBACK'); if ($result === false) { http_response_code(400); exit('Unable to ROLLBACK transaction!! Oh noes...'); } } $result = ArrestDB::Query('COMMIT'); if ($result === false) { http_response_code(400); exit('Unable to COMMIT transaction'); } } catch (Exception $e) { $result = ArrestDB::Query('ROLLBACK'); if ($result === false) { http_response_code(400); exit('Unable to ROLLBACK transaction!! Oh noes...'); } } http_response_code(201); header(sprintf('Location: %s/%s', $ARCHIVE_URI_PREFIX, basename($archive))); return ArrestDB::Reply(['count' => $rowCount]); }); ArrestDB::Serve('DELETE', '/(#any)', function () { http_response_code(400); exit('Not yet implemented'); }); http_response_code(400); exit('Request failed to match');