From c08706cbf8362e07fbb28ba7159674de236d78f4 Mon Sep 17 00:00:00 2001 From: skilion Date: Mon, 7 Sep 2015 18:33:16 +0200 Subject: [PATCH] added method to iterate trough all items --- src/cache.d | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/src/cache.d b/src/cache.d index 6ea70ef6..f2d9715d 100644 --- a/src/cache.d +++ b/src/cache.d @@ -26,6 +26,7 @@ struct ItemCache { Database db; Statement insertItemStmt; + Statement selectItemsStmt; Statement selectItemByIdStmt; Statement selectItemByPathStmt; @@ -45,6 +46,7 @@ struct ItemCache )"); db.exec("CREATE UNIQUE INDEX IF NOT EXISTS path_idx ON item (path)"); insertItemStmt = db.prepare("INSERT OR REPLACE INTO item (id, path, name, type, eTag, cTag, mtime, parentId, crc32) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)"); + selectItemsStmt = db.prepare("SELECT id, path, name, type, eTag, cTag, mtime, parentId, crc32 FROM item ORDER BY path DESC"); selectItemByIdStmt = db.prepare("SELECT id, path, name, type, eTag, cTag, mtime, parentId, crc32 FROM item WHERE id = ?"); selectItemByPathStmt = db.prepare("SELECT id, path, name, type, eTag, cTag, mtime, parentId, crc32 FROM item WHERE path = ?"); } @@ -70,6 +72,37 @@ struct ItemCache } } + // returns a range that go trough all items + auto selectAll() + { + struct ItemRange + { + Statement.Result result; + + this(Statement.Result result) + { + this.result = result; + } + + @property bool empty() + { + return result.empty(); + } + + @property Item front() + { + return buildItem(result); + } + + void popFront() + { + result.popFront(); + } + } + + return ItemRange(selectItemsStmt.exec()); + } + bool selectById(const(char)[] id, out Item item) { selectItemByIdStmt.bind(1, id); @@ -126,7 +159,7 @@ struct ItemCache return false; } - private Item buildItem(Statement.Result result) + private static Item buildItem(Statement.Result result) { assert(!result.empty && result.front.length == 9); Item item = {