queries
| 1608 | |
| 1609 | // queries |
| 1610 | QList<LibraryItem *> DBHelper::getFoldersFromParent(qulonglong parentId, QSqlDatabase &db, bool sort) |
| 1611 | { |
| 1612 | QList<LibraryItem *> list; |
| 1613 | |
| 1614 | QSqlQuery selectQuery(db); // TODO check |
| 1615 | selectQuery.prepare("SELECT * FROM folder WHERE parentId = :parentId and id <> 1"); |
| 1616 | selectQuery.bindValue(":parentId", parentId); |
| 1617 | selectQuery.exec(); |
| 1618 | |
| 1619 | QSqlRecord record = selectQuery.record(); |
| 1620 | |
| 1621 | int name = record.indexOf("name"); |
| 1622 | int path = record.indexOf("path"); |
| 1623 | int finished = record.indexOf("finished"); |
| 1624 | int completed = record.indexOf("completed"); |
| 1625 | int id = record.indexOf("id"); |
| 1626 | int numChildren = record.indexOf("numChildren"); |
| 1627 | int firstChildHash = record.indexOf("firstChildHash"); |
| 1628 | int customImage = record.indexOf("customImage"); |
| 1629 | int type = record.indexOf("type"); |
| 1630 | int added = record.indexOf("added"); |
| 1631 | int updated = record.indexOf("updated"); |
| 1632 | |
| 1633 | Folder *currentItem; |
| 1634 | while (selectQuery.next()) { |
| 1635 | // TODO sort by sort indicator and name |
| 1636 | currentItem = new Folder(selectQuery.value(id).toULongLong(), parentId, selectQuery.value(name).toString(), selectQuery.value(path).toString()); |
| 1637 | |
| 1638 | currentItem->finished = selectQuery.value(finished).toBool(); |
| 1639 | currentItem->completed = selectQuery.value(completed).toBool(); |
| 1640 | if (!selectQuery.value(numChildren).isNull() && selectQuery.value(numChildren).isValid()) { |
| 1641 | currentItem->numChildren = selectQuery.value(numChildren).toInt(); |
| 1642 | } |
| 1643 | currentItem->firstChildHash = selectQuery.value(firstChildHash).toString(); |
| 1644 | currentItem->customImage = selectQuery.value(customImage).toString(); |
| 1645 | currentItem->type = selectQuery.value(type).value<YACReader::FileType>(); |
| 1646 | currentItem->added = selectQuery.value(added).toLongLong(); |
| 1647 | currentItem->updated = selectQuery.value(updated).toLongLong(); |
| 1648 | |
| 1649 | int lessThan = 0; |
| 1650 | |
| 1651 | if (list.isEmpty() || !sort) |
| 1652 | list.append(currentItem); |
| 1653 | else { |
| 1654 | auto last = static_cast<Folder *>(list.back()); |
| 1655 | QString nameLast = last->name; |
| 1656 | QString nameCurrent = currentItem->name; |
| 1657 | QList<LibraryItem *>::iterator i; |
| 1658 | i = list.end(); |
| 1659 | i--; |
| 1660 | while ((0 > (lessThan = naturalCompare(nameCurrent, nameLast, Qt::CaseInsensitive))) && i != list.begin()) { |
| 1661 | i--; |
| 1662 | nameLast = (*i)->name; |
| 1663 | } |
| 1664 | if (lessThan >= 0) // si se ha encontrado un elemento menor que current, se inserta justo después |
| 1665 | list.insert(++i, currentItem); |
| 1666 | else |
| 1667 | list.insert(i, currentItem); |