god damn it php

This commit is contained in:
Shish 2023-01-11 13:27:57 +00:00
parent 8e1b371e83
commit 2a18322dd5
6 changed files with 44 additions and 35 deletions

View file

@ -46,7 +46,7 @@ class Database
{ {
$this->db = new PDO($this->dsn); $this->db = new PDO($this->dsn);
$this->connect_engine(); $this->connect_engine();
$this->engine->init($this->db); $this->get_engine()->init($this->db);
$this->begin_transaction(); $this->begin_transaction();
} }
@ -102,31 +102,32 @@ class Database
} }
} }
public function scoreql_to_sql(string $input): string private function get_engine(): DBEngine {
{
if (is_null($this->engine)) { if (is_null($this->engine)) {
$this->connect_engine(); $this->connect_engine();
} }
return $this->engine->scoreql_to_sql($input); return $this->engine;
}
public function scoreql_to_sql(string $input): string
{
return $this->get_engine()->scoreql_to_sql($input);
} }
public function get_driver_id(): DatabaseDriverID public function get_driver_id(): DatabaseDriverID
{ {
if (is_null($this->engine)) { return $this->get_engine()->id;
$this->connect_engine();
}
return $this->engine->id;
} }
public function get_version(): string public function get_version(): string
{ {
return $this->engine->get_version($this->db); return $this->get_engine()->get_version($this->db);
} }
private function count_time(string $method, float $start, string $query, ?array $args): void private function count_time(string $method, float $start, string $query, ?array $args): void
{ {
global $_tracer, $tracer_enabled; global $_tracer, $tracer_enabled;
$dur = microtime(true) - $start; $dur = ftime() - $start;
if ($tracer_enabled) { if ($tracer_enabled) {
$query = trim(preg_replace('/^[\t ]+/m', '', $query)); // trim leading whitespace $query = trim(preg_replace('/^[\t ]+/m', '', $query)); // trim leading whitespace
$_tracer->complete($start * 1000000, $dur * 1000000, "DB Query", ["query"=>$query, "args"=>$args, "method"=>$method]); $_tracer->complete($start * 1000000, $dur * 1000000, "DB Query", ["query"=>$query, "args"=>$args, "method"=>$method]);
@ -137,12 +138,12 @@ class Database
public function set_timeout(?int $time): void public function set_timeout(?int $time): void
{ {
$this->engine->set_timeout($this->db, $time); $this->get_engine()->set_timeout($this->db, $time);
} }
public function notify(string $channel, ?string $data=null): void public function notify(string $channel, ?string $data=null): void
{ {
$this->engine->notify($this->db, $channel, $data); $this->get_engine()->notify($this->db, $channel, $data);
} }
public function execute(string $query, array $args = []): PDOStatement public function execute(string $query, array $args = []): PDOStatement
@ -171,7 +172,7 @@ class Database
*/ */
public function get_all(string $query, array $args = []): array public function get_all(string $query, array $args = []): array
{ {
$_start = microtime(true); $_start = ftime();
$data = $this->execute($query, $args)->fetchAll(); $data = $this->execute($query, $args)->fetchAll();
$this->count_time("get_all", $_start, $query, $args); $this->count_time("get_all", $_start, $query, $args);
return $data; return $data;
@ -182,7 +183,7 @@ class Database
*/ */
public function get_all_iterable(string $query, array $args = []): PDOStatement public function get_all_iterable(string $query, array $args = []): PDOStatement
{ {
$_start = microtime(true); $_start = ftime();
$data = $this->execute($query, $args); $data = $this->execute($query, $args);
$this->count_time("get_all_iterable", $_start, $query, $args); $this->count_time("get_all_iterable", $_start, $query, $args);
return $data; return $data;
@ -193,7 +194,7 @@ class Database
*/ */
public function get_row(string $query, array $args = []): ?array public function get_row(string $query, array $args = []): ?array
{ {
$_start = microtime(true); $_start = ftime();
$row = $this->execute($query, $args)->fetch(); $row = $this->execute($query, $args)->fetch();
$this->count_time("get_row", $_start, $query, $args); $this->count_time("get_row", $_start, $query, $args);
return $row ? $row : null; return $row ? $row : null;
@ -204,7 +205,7 @@ class Database
*/ */
public function get_col(string $query, array $args = []): array public function get_col(string $query, array $args = []): array
{ {
$_start = microtime(true); $_start = ftime();
$res = $this->execute($query, $args)->fetchAll(PDO::FETCH_COLUMN); $res = $this->execute($query, $args)->fetchAll(PDO::FETCH_COLUMN);
$this->count_time("get_col", $_start, $query, $args); $this->count_time("get_col", $_start, $query, $args);
return $res; return $res;
@ -215,7 +216,7 @@ class Database
*/ */
public function get_col_iterable(string $query, array $args = []): \Generator public function get_col_iterable(string $query, array $args = []): \Generator
{ {
$_start = microtime(true); $_start = ftime();
$stmt = $this->execute($query, $args); $stmt = $this->execute($query, $args);
$this->count_time("get_col_iterable", $_start, $query, $args); $this->count_time("get_col_iterable", $_start, $query, $args);
foreach ($stmt as $row) { foreach ($stmt as $row) {
@ -228,7 +229,7 @@ class Database
*/ */
public function get_pairs(string $query, array $args = []): array public function get_pairs(string $query, array $args = []): array
{ {
$_start = microtime(true); $_start = ftime();
$res = $this->execute($query, $args)->fetchAll(PDO::FETCH_KEY_PAIR); $res = $this->execute($query, $args)->fetchAll(PDO::FETCH_KEY_PAIR);
$this->count_time("get_pairs", $_start, $query, $args); $this->count_time("get_pairs", $_start, $query, $args);
return $res; return $res;
@ -240,7 +241,7 @@ class Database
*/ */
public function get_pairs_iterable(string $query, array $args = []): \Generator public function get_pairs_iterable(string $query, array $args = []): \Generator
{ {
$_start = microtime(true); $_start = ftime();
$stmt = $this->execute($query, $args); $stmt = $this->execute($query, $args);
$this->count_time("get_pairs_iterable", $_start, $query, $args); $this->count_time("get_pairs_iterable", $_start, $query, $args);
foreach ($stmt as $row) { foreach ($stmt as $row) {
@ -253,7 +254,7 @@ class Database
*/ */
public function get_one(string $query, array $args = []) public function get_one(string $query, array $args = [])
{ {
$_start = microtime(true); $_start = ftime();
$row = $this->execute($query, $args)->fetch(); $row = $this->execute($query, $args)->fetch();
$this->count_time("get_one", $_start, $query, $args); $this->count_time("get_one", $_start, $query, $args);
return $row ? $row[0] : null; return $row ? $row[0] : null;
@ -264,7 +265,7 @@ class Database
*/ */
public function exists(string $query, array $args = []): bool public function exists(string $query, array $args = []): bool
{ {
$_start = microtime(true); $_start = ftime();
$row = $this->execute($query, $args)->fetch(); $row = $this->execute($query, $args)->fetch();
$this->count_time("exists", $_start, $query, $args); $this->count_time("exists", $_start, $query, $args);
if ($row==null) { if ($row==null) {
@ -278,7 +279,7 @@ class Database
*/ */
public function get_last_insert_id(string $seq): int public function get_last_insert_id(string $seq): int
{ {
if ($this->engine->id == DatabaseDriverID::PGSQL) { if ($this->get_engine()->id == DatabaseDriverID::PGSQL) {
$id = $this->db->lastInsertId($seq); $id = $this->db->lastInsertId($seq);
} else { } else {
$id = $this->db->lastInsertId(); $id = $this->db->lastInsertId();
@ -296,7 +297,7 @@ class Database
$this->connect_engine(); $this->connect_engine();
} }
$data = trim($data, ", \t\n\r\0\x0B"); // mysql doesn't like trailing commas $data = trim($data, ", \t\n\r\0\x0B"); // mysql doesn't like trailing commas
$this->execute($this->engine->create_table_sql($name, $data)); $this->execute($this->get_engine()->create_table_sql($name, $data));
} }
/** /**
@ -310,20 +311,20 @@ class Database
$this->connect_db(); $this->connect_db();
} }
if ($this->engine->id === DatabaseDriverID::MYSQL) { if ($this->get_engine()->id === DatabaseDriverID::MYSQL) {
return count( return count(
$this->get_all("SHOW TABLES") $this->get_all("SHOW TABLES")
); );
} elseif ($this->engine->id === DatabaseDriverID::PGSQL) { } elseif ($this->get_engine()->id === DatabaseDriverID::PGSQL) {
return count( return count(
$this->get_all("SELECT table_name FROM information_schema.tables WHERE table_schema = 'public'") $this->get_all("SELECT table_name FROM information_schema.tables WHERE table_schema = 'public'")
); );
} elseif ($this->engine->id === DatabaseDriverID::SQLITE) { } elseif ($this->get_engine()->id === DatabaseDriverID::SQLITE) {
return count( return count(
$this->get_all("SELECT name FROM sqlite_master WHERE type = 'table'") $this->get_all("SELECT name FROM sqlite_master WHERE type = 'table'")
); );
} else { } else {
throw new SCoreException("Can't count tables for database type {$this->engine->id}"); throw new SCoreException("Can't count tables for database type {$this->get_engine()->id}");
} }
} }

View file

@ -4,6 +4,8 @@ declare(strict_types=1);
namespace Shimmie2; namespace Shimmie2;
use PhpParser\Node\Expr\Cast\Double;
class Link class Link
{ {
public ?string $page; public ?string $page;

View file

@ -516,14 +516,20 @@ function scan_dir(string $path): array
} }
/**
* because microtime() returns string|float, and we only ever want float
*/
function ftime(): float
{
return (float)microtime(true);
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\ /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
* Debugging functions * * Debugging functions *
\* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ \* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
// SHIT by default this returns the time as a string. And it's not even a $_shm_load_start = ftime();
// string representation of a number, it's two numbers separated by a space.
// What the fuck were the PHP developers smoking.
$_shm_load_start = microtime(true);
/** /**
* Collects some debug information (execution time, memory usage, queries, etc) * Collects some debug information (execution time, memory usage, queries, etc)
@ -540,7 +546,7 @@ function get_debug_info(): string
} else { } else {
$commit = " (".$config->get_string("commit_hash").")"; $commit = " (".$config->get_string("commit_hash").")";
} }
$time = sprintf("%.2f", microtime(true) - $_shm_load_start); $time = sprintf("%.2f", ftime() - $_shm_load_start);
$dbtime = sprintf("%.2f", $database->dbtime); $dbtime = sprintf("%.2f", $database->dbtime);
$i_files = count(get_included_files()); $i_files = count(get_included_files());
$hits = $cache->get_hits(); $hits = $cache->get_hits();

View file

@ -364,7 +364,7 @@ class CronUploader extends Extension
// Upload the file(s) // Upload the file(s)
foreach ($image_queue as $img) { foreach ($image_queue as $img) {
$execution_time = microtime(true) - $_shm_load_start; $execution_time = ftime() - $_shm_load_start;
if ($execution_time>$max_time) { if ($execution_time>$max_time) {
break; break;
} else { } else {

View file

@ -13,7 +13,7 @@ class StatsDInterface extends Extension
private function _stats(string $type) private function _stats(string $type)
{ {
global $_shm_event_count, $cache, $database, $_shm_load_start; global $_shm_event_count, $cache, $database, $_shm_load_start;
$time = microtime(true) - $_shm_load_start; $time = ftime() - $_shm_load_start;
StatsDInterface::$stats["shimmie.$type.hits"] = "1|c"; StatsDInterface::$stats["shimmie.$type.hits"] = "1|c";
StatsDInterface::$stats["shimmie.$type.time"] = "$time|ms"; StatsDInterface::$stats["shimmie.$type.time"] = "$time|ms";
StatsDInterface::$stats["shimmie.$type.time-db"] = "{$database->dbtime}|ms"; StatsDInterface::$stats["shimmie.$type.time-db"] = "{$database->dbtime}|ms";

View file

@ -107,7 +107,7 @@ try {
empty($_SERVER["REQUEST_URI"]) empty($_SERVER["REQUEST_URI"])
|| (@$_GET["trace"] == "on") || (@$_GET["trace"] == "on")
|| ( || (
(microtime(true) - $_shm_load_start) > TRACE_THRESHOLD (ftime() - $_shm_load_start) > TRACE_THRESHOLD
&& ($_SERVER["REQUEST_URI"] ?? "") != "/upload" && ($_SERVER["REQUEST_URI"] ?? "") != "/upload"
) )
) { ) {