added method to iterate trough all items

This commit is contained in:
skilion 2015-09-07 18:33:16 +02:00
parent 20dbc9795c
commit c08706cbf8

View file

@ -26,6 +26,7 @@ struct ItemCache
{ {
Database db; Database db;
Statement insertItemStmt; Statement insertItemStmt;
Statement selectItemsStmt;
Statement selectItemByIdStmt; Statement selectItemByIdStmt;
Statement selectItemByPathStmt; Statement selectItemByPathStmt;
@ -45,6 +46,7 @@ struct ItemCache
)"); )");
db.exec("CREATE UNIQUE INDEX IF NOT EXISTS path_idx ON item (path)"); 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 (?, ?, ?, ?, ?, ?, ?, ?, ?)"); 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 = ?"); 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 = ?"); 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) bool selectById(const(char)[] id, out Item item)
{ {
selectItemByIdStmt.bind(1, id); selectItemByIdStmt.bind(1, id);
@ -126,7 +159,7 @@ struct ItemCache
return false; return false;
} }
private Item buildItem(Statement.Result result) private static Item buildItem(Statement.Result result)
{ {
assert(!result.empty && result.front.length == 9); assert(!result.empty && result.front.length == 9);
Item item = { Item item = {