#include #include #include #include #include "src/db/databasemanager.h" #include "src/db/dao/mailitemdao.h" #include "src/db/dao/folderdao.h" #include "src/db/dao/accountdao.h" #include "src/core/mailitem.h" #include "src/core/models/account.h" #include "src/core/models/folder.h" #include #include int main(int argc, char *argv[]) { QCoreApplication app(argc, argv); // Initialize database (will open/create wino-mail.sqlite in current dir) if (!DatabaseManager::instance().initialize()) { qCritical() << "Failed to initialize database"; return 1; } qInfo() << "Database initialized"; // Debug: print table schema QSqlDatabase& db = DatabaseManager::instance().database(); QSqlQuery query(db); if (query.exec("PRAGMA table_info(MailCopy)")) { qInfo() << "MailCopy table schema:"; while (query.next()) { qInfo() << " cid:" << query.value(0).toInt() << " name:" << query.value(1).toString() << " type:" << query.value(2).toString() << " notnull:" << query.value(3).toInt() << " dflt_value:" << query.value(4).toString() << " pk:" << query.value(5).toInt(); } } else { qWarning() << "Failed to get table info:" << query.lastError().text(); } // Create and insert an Account Account account; account.setEmail(QStringLiteral("test@example.com")); account.setDisplayName(QStringLiteral("Test User")); account.setSignature(QStringLiteral("Test signature")); account.setType(AccountType::Outlook); qint64 accountId = AccountDao::insert(account); if (accountId == -1) { qCritical() << "Failed to insert account"; return 1; } account.setId(accountId); qInfo() << "Inserted account with ID:" << account.id(); // Create and insert a Folder (Inbox) under the account Folder folder; folder.setAccountId(account.id()); folder.setName(QStringLiteral("Inbox")); folder.setInbox(true); qint64 folderId = FolderDao::insert(folder); if (folderId == -1) { qCritical() << "Failed to insert folder"; return 1; } folder.setId(folderId); qInfo() << "Inserted folder with ID:" << folder.id(); // Create a test MailItem MailItem item; item.setFolderId(folder.id()); // Inbox folder item.setMessageId(QUuid::createUuid().toString(QUuid::WithoutBraces)); item.setSubject(QStringLiteral("Test Subject")); item.setSender(QStringLiteral("sender@example.com")); item.setRecipient(QStringLiteral("recipient@example.com")); // maps to recipient column item.setTo(QStringLiteral("to@example.com")); item.setCc(QStringLiteral("cc@example.com")); item.setBcc(QStringLiteral("bcc@example.com")); item.setDate(QDateTime::currentDateTimeUtc()); item.setRead(false); item.setFlagged(false); item.setSize(1024); item.setFileId(QStringLiteral("file123")); item.setUid(12345); item.setBodyHtml(QStringLiteral("Hello")); // Insert bool ok = MailItemDao::insert(item); if (!ok) { qCritical() << "Failed to insert mail item"; return 1; } qInfo() << "Inserted mail item with ID:" << item.id(); // Debug: let's see what's in the table for the inserted row QSqlQuery query2(db); query2.prepare("SELECT * FROM MailCopy WHERE id = :id"); query2.bindValue(":id", item.id()); if (query2.exec() && query2.next()) { qInfo() << "Row found in MailCopy:"; qInfo() << " id:" << query2.value(0).toInt(); qInfo() << " folderId:" << query2.value(1).toInt(); qInfo() << " messageId:" << query2.value(2).toString(); qInfo() << " subject:" << query2.value(3).toString(); qInfo() << " sender:" << query2.value(4).toString(); qInfo() << " recipient:" << query2.value(5).toString(); qInfo() << " date:" << query2.value(6).toDateTime(); qInfo() << " read:" << query2.value(7).toBool(); qInfo() << " flagged:" << query2.value(8).toBool(); qInfo() << " hasAttachment:" << query2.value(9).toBool(); qInfo() << " size:" << query2.value(10).toLongLong(); qInfo() << " uid:" << query2.value(11).toLongLong(); qInfo() << " fileId:" << query2.value(12).toString(); qInfo() << " bodyHtml:" << query2.value(13).toString(); qInfo() << " toAddr:" << query2.value(14).toString(); qInfo() << " ccAddr:" << query2.value(15).toString(); qInfo() << " bccAddr:" << query2.value(16).toString(); } else { qWarning() << "Failed to fetch row:" << query2.lastError().text(); } // Retrieve by ID auto opt = MailItemDao::findById(item.id()); if (!opt) { qCritical() << "Failed to fetch inserted mail item"; return 1; } MailItem fetched = *opt; qInfo() << "Fetched ID:" << fetched.id(); qInfo() << "Subject:" << fetched.subject(); qInfo() << "From:" << fetched.sender(); qInfo() << "To:" << fetched.to(); qInfo() << "CC:" << fetched.cc(); qInfo() << "BCC:" << fetched.bcc(); qInfo() << "Recipient column (recipient):" << fetched.recipient(); // Verify fields match bool ok2 = (fetched.messageId() == item.messageId() && fetched.subject() == item.subject() && fetched.sender() == item.sender() && fetched.recipient() == item.recipient() && fetched.to() == item.to() && fetched.cc() == item.cc() && fetched.bcc() == item.bcc() && fetched.bodyHtml() == item.bodyHtml()); if (ok2) { qInfo() << "All fields match!"; } else { qCritical() << "Mismatch in fields"; return 1; } // Clean up (optional) MailItemDao::remove(item.id()); FolderDao::remove(folder.id()); AccountDao::remove(account.id()); return 0; }