From 83769410c36e1f1bf7a2fc1c0a735d3ce5b51b05 Mon Sep 17 00:00:00 2001 From: Alexander Zhirov Date: Fri, 21 Nov 2025 14:57:53 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=BE=20=D0=BA=D0=BE=D0=BB=D0=B8=D1=87=D0=B5=D1=81=D1=82?= =?UTF-8?q?=D0=B2=D0=BE=20=D1=81=D0=BD=D0=B8=D0=BC=D0=BA=D0=BE=D0=B2=20?= =?UTF-8?q?=D0=B2=20=D0=BE=D0=B1=D1=8A=D0=B5=D0=BA=D1=82=20StorageFile?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/cdcdb/dblite.d | 80 +++++++++++++++++++++++++++++++------- source/cdcdb/storagefile.d | 1 + source/cdcdb/version_.d | 2 +- 3 files changed, 68 insertions(+), 15 deletions(-) diff --git a/source/cdcdb/dblite.d b/source/cdcdb/dblite.d index 63f7725..3059a90 100644 --- a/source/cdcdb/dblite.d +++ b/source/cdcdb/dblite.d @@ -14,6 +14,7 @@ import cdcdb.lib; struct DBFile { Identifier id; string path; + long countSnapshots; @trusted pure nothrow @nogc @property bool empty() const { return id.empty(); @@ -224,7 +225,13 @@ public: DBFile[] getFiles() { auto queryResult = sql( q{ - SELECT id, name FROM files + SELECT + f.id, + f.name, + count(s.file) count_snapshots + FROM files f + JOIN snapshots s ON s.file = f.id + GROUP BY f.id } ); @@ -236,6 +243,7 @@ public: file.id = row["id"].as!Blob(Blob.init); file.path = row["name"].as!string; + file.countSnapshots = row["count_snapshots"].as!long; files ~= file; } @@ -246,7 +254,13 @@ public: DBFile getFile(string path) { auto queryResult = sql( q{ - SELECT id FROM files WHERE name = ?1 + SELECT + f.id, + count(s.file) count_snapshots + FROM files f + JOIN snapshots s ON s.file = f.id + WHERE f.name = ?1 + GROUP BY f.id }, path ); @@ -257,6 +271,7 @@ public: file.id = data["id"].as!Blob(Blob.init); file.path = path; + file.countSnapshots = data["count_snapshots"].as!long; } return file; @@ -265,9 +280,14 @@ public: DBFile getFile(Identifier id) { auto queryResult = sql( q{ - SELECT id, name - FROM files - WHERE id = ?1 + SELECT + f.id, + f.name, + count(s.file) count_snapshots + FROM files f + JOIN snapshots s ON s.file = f.id + WHERE f.id = ?1 + GROUP BY f.id }, id[] ); @@ -278,6 +298,7 @@ public: file.id = id; file.path = data["name"].as!string; + file.countSnapshots = data["count_snapshots"].as!long; } return file; @@ -313,7 +334,14 @@ public: DBFile[] findFile(string pattern) { auto queryResult = sql( q{ - SELECT id, name FROM files WHERE name LIKE ?1 + SELECT + f.id, + f.name, + count(s.file) count_snapshots + FROM files f + JOIN snapshots s ON s.file = f.id + WHERE f.name LIKE ?1 + GROUP BY f.id }, '%' ~ pattern ~ '%' ); @@ -325,6 +353,7 @@ public: file.id = row["id"].as!Blob(Blob.init); file.path = row["name"].as!string; + file.countSnapshots = row["count_snapshots"].as!long; files ~= file; } @@ -338,14 +367,19 @@ public: DBFile[] findFile(Identifier id) { auto queryResult = sql( q{ - SELECT id, name - FROM files + SELECT + f.id, + f.name, + count(s.file) count_snapshots + FROM files f + JOIN snapshots s ON s.file = f.id WHERE length(?1) BETWEEN 1 AND 16 AND CASE WHEN substr(hex(?1), 2 * length(?1), 1) = '0' - THEN instr(hex(id), substr(hex(?1), 1, 2 * length(?1) - 1)) > 0 - ELSE substr(id, 1, length(?1)) = ?1 + THEN instr(hex(f.id), substr(hex(?1), 1, 2 * length(?1) - 1)) > 0 + ELSE substr(f.id, 1, length(?1)) = ?1 END + GROUP BY f.id }, id[] ); @@ -357,6 +391,7 @@ public: file.id = row["id"].as!Blob(Blob.init); file.path = row["name"].as!string; + file.countSnapshots = row["count_snapshots"].as!long; files ~= file; } @@ -650,6 +685,7 @@ public: s.id, f.id file_id, f.name file_name, + count(sc.file) count_snapshots, s.sha256, s.description, s.created_utc, @@ -671,6 +707,8 @@ public: JOIN users u ON u.uid = s.uid JOIN users r ON r.uid = s.ruid JOIN files f ON f.id = s.file AND (length(?) = 0 OR f.name = ?1) + JOIN snapshots sc ON f.id = sc.file + GROUP BY s.id ORDER BY s.created_utc }, file ); @@ -684,7 +722,8 @@ public: snapshot.id = row["id"].as!Blob(Blob.init); snapshot.file = DBFile( Identifier(row["file_id"].as!Blob(Blob.init)), - row["file_name"].as!string + row["file_name"].as!string, + row["count_snapshots"].as!long ); snapshot.sha256 = row["sha256"].as!Blob(Blob.init); snapshot.description = row["description"].as!string(""); // может быть NULL @@ -719,6 +758,7 @@ public: s.id, f.id file_id, f.name file_name, + count(sc.file) count_snapshots, s.sha256, s.description, s.created_utc, @@ -740,7 +780,9 @@ public: JOIN users u ON u.uid = s.uid JOIN users r ON r.uid = s.ruid JOIN files f ON f.id = s.file + JOIN snapshots sc ON f.id = sc.file WHERE f.id = ?1 + GROUP BY s.id ORDER BY s.created_utc }, id[] ); @@ -754,7 +796,8 @@ public: snapshot.id = row["id"].as!Blob(Blob.init); snapshot.file = DBFile( Identifier(row["file_id"].as!Blob(Blob.init)), - row["file_name"].as!string + row["file_name"].as!string, + row["count_snapshots"].as!long ); snapshot.sha256 = row["sha256"].as!Blob(Blob.init); snapshot.description = row["description"].as!string(""); @@ -788,6 +831,7 @@ public: s.id, f.id file_id, f.name file_name, + count(sc.file) count_snapshots, s.sha256, s.description, s.created_utc, @@ -809,7 +853,9 @@ public: JOIN users u ON u.uid = s.uid JOIN users r ON r.uid = s.ruid JOIN files f ON f.id = s.file + JOIN snapshots sc ON f.id = sc.file WHERE s.id = ?1 + GROUP BY s.id ORDER BY s.created_utc }, id[] ); @@ -822,7 +868,8 @@ public: snapshot.id = data["id"].as!Blob(Blob.init); snapshot.file = DBFile( Identifier(data["file_id"].as!Blob(Blob.init)), - data["file_name"].as!string + data["file_name"].as!string, + data["count_snapshots"].as!long ); snapshot.sha256 = data["sha256"].as!Blob(Blob.init); snapshot.description = data["description"].as!string(""); @@ -857,6 +904,7 @@ public: s.id, f.id file_id, f.name file_name, + count(sc.file) count_snapshots, s.sha256, s.description, s.created_utc, @@ -878,12 +926,15 @@ public: JOIN users u ON u.uid = s.uid JOIN users r ON r.uid = s.ruid JOIN files f ON f.id = s.file + JOIN snapshots sc ON f.id = sc.file WHERE length(?1) BETWEEN 1 AND 16 AND CASE WHEN substr(hex(?1), 2 * length(?1), 1) = '0' THEN instr(hex(s.id), substr(hex(?1), 1, 2 * length(?1) - 1)) > 0 ELSE substr(s.id, 1, length(?1)) = ?1 END + GROUP BY s.id + ORDER BY s.created_utc }, id[] ); @@ -896,7 +947,8 @@ public: snapshot.id = row["id"].as!Blob(Blob.init); snapshot.file = DBFile( Identifier(row["file_id"].as!Blob(Blob.init)), - row["file_name"].as!string + row["file_name"].as!string, + row["count_snapshots"].as!long ); snapshot.sha256 = row["sha256"].as!Blob(Blob.init); snapshot.description = row["description"].as!string(""); diff --git a/source/cdcdb/storagefile.d b/source/cdcdb/storagefile.d index a2c8101..350a5cd 100644 --- a/source/cdcdb/storagefile.d +++ b/source/cdcdb/storagefile.d @@ -14,6 +14,7 @@ public: @property ref Identifier id() return { return _dbfile.id; } @property string name() const nothrow @safe { return _dbfile.path; } + @property long countSnapshots() const nothrow @safe { return _dbfile.countSnapshots; } Snapshot[] snapshots() { Snapshot[] snapshots; diff --git a/source/cdcdb/version_.d b/source/cdcdb/version_.d index b7cd2d0..3609f61 100644 --- a/source/cdcdb/version_.d +++ b/source/cdcdb/version_.d @@ -1,3 +1,3 @@ module cdcdb.version_; -enum cdcdbVersion = "0.2.0"; +enum cdcdbVersion = "0.2.1";