shared: sqlitedbservice: Introduce exec(),execBool(). Refactor
This commit is contained in:
		@@ -5,22 +5,6 @@
 | 
				
			|||||||
#include "sqlitedbservice.h"
 | 
					#include "sqlitedbservice.h"
 | 
				
			||||||
#include "filedata.h"
 | 
					#include "filedata.h"
 | 
				
			||||||
#include "logger.h"
 | 
					#include "logger.h"
 | 
				
			||||||
bool SqliteDbService::fileExistsInDatabase(QString path, qint64 mtime)
 | 
					 | 
				
			||||||
{
 | 
					 | 
				
			||||||
	auto query = QSqlQuery(dbFactory->forCurrentThread());
 | 
					 | 
				
			||||||
	query.prepare("SELECT 1 FROM file WHERE path = ? and mtime = ?");
 | 
					 | 
				
			||||||
	query.addBindValue(path);
 | 
					 | 
				
			||||||
	query.addBindValue(mtime);
 | 
					 | 
				
			||||||
	if(!query.exec())
 | 
					 | 
				
			||||||
	{
 | 
					 | 
				
			||||||
		throw LooqsGeneralException("Error while trying to query for file existance: " + query.lastError().text());
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	if(!query.next())
 | 
					 | 
				
			||||||
	{
 | 
					 | 
				
			||||||
		return false;
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	return query.value(0).toBool();
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
QVector<SearchResult> SqliteDbService::search(const LooqsQuery &query)
 | 
					QVector<SearchResult> SqliteDbService::search(const LooqsQuery &query)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
@@ -31,13 +15,7 @@ QVector<SearchResult> SqliteDbService::search(const LooqsQuery &query)
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
std::optional<QChar> SqliteDbService::queryFileType(QString absPath)
 | 
					std::optional<QChar> SqliteDbService::queryFileType(QString absPath)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	auto query = QSqlQuery(dbFactory->forCurrentThread());
 | 
						auto query = exec("SELCET filetype FROM file WHERE path = ?", {absPath});
 | 
				
			||||||
	query.prepare("SELECT filetype FROM file WHERE path = ?");
 | 
					 | 
				
			||||||
	query.addBindValue(absPath);
 | 
					 | 
				
			||||||
	if(!query.exec())
 | 
					 | 
				
			||||||
	{
 | 
					 | 
				
			||||||
		throw LooqsGeneralException("Error while trying to query for file type: " + query.lastError().text());
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	if(!query.next())
 | 
						if(!query.next())
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
		return {};
 | 
							return {};
 | 
				
			||||||
@@ -47,18 +25,17 @@ std::optional<QChar> SqliteDbService::queryFileType(QString absPath)
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
bool SqliteDbService::fileExistsInDatabase(QString path)
 | 
					bool SqliteDbService::fileExistsInDatabase(QString path)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	auto query = QSqlQuery(dbFactory->forCurrentThread());
 | 
						return execBool("SELECT 1 FROM file WHERE path = ?", {path});
 | 
				
			||||||
	query.prepare("SELECT 1 FROM file WHERE path = ?");
 | 
					 | 
				
			||||||
	query.addBindValue(path);
 | 
					 | 
				
			||||||
	if(!query.exec())
 | 
					 | 
				
			||||||
	{
 | 
					 | 
				
			||||||
		throw LooqsGeneralException("Error while trying to query for file existance: " + query.lastError().text());
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
	if(!query.next())
 | 
					
 | 
				
			||||||
 | 
					bool SqliteDbService::fileExistsInDatabase(QString path, qint64 mtime)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
		return false;
 | 
						return execBool("SELECT 1 FROM file WHERE path = ? AND mtime = ?", {path, mtime});
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
	return query.value(0).toBool();
 | 
					
 | 
				
			||||||
 | 
					bool SqliteDbService::fileExistsInDatabase(QString path, qint64 mtime, QChar fileType)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						return execBool("SELECT 1 FROM file WHERE path = ? AND mtime = ? AND filetype = ?", {path, mtime, fileType});
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
SqliteDbService::SqliteDbService(DatabaseFactory &dbFactory)
 | 
					SqliteDbService::SqliteDbService(DatabaseFactory &dbFactory)
 | 
				
			||||||
@@ -164,6 +141,31 @@ bool SqliteDbService::insertToFTS(bool useTrigrams, QSqlDatabase &db, int fileid
 | 
				
			|||||||
	return true;
 | 
						return true;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					QSqlQuery SqliteDbService::exec(QString querystr, std::initializer_list<QVariant> args)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						auto query = QSqlQuery(dbFactory->forCurrentThread());
 | 
				
			||||||
 | 
						query.prepare(querystr);
 | 
				
			||||||
 | 
						for(const QVariant &v : args)
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							query.addBindValue(v);
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						if(!query.exec())
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							throw LooqsGeneralException("Error while exec(): " + query.lastError().text());
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return query;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					bool SqliteDbService::execBool(QString querystr, std::initializer_list<QVariant> args)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						auto query = exec(querystr, args);
 | 
				
			||||||
 | 
						if(!query.next())
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							return false;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return query.value(0).toBool();
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
SaveFileResult SqliteDbService::saveFile(QFileInfo fileInfo, QVector<PageData> &pageData, bool pathsOnly)
 | 
					SaveFileResult SqliteDbService::saveFile(QFileInfo fileInfo, QVector<PageData> &pageData, bool pathsOnly)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	QString absPath = fileInfo.absoluteFilePath();
 | 
						QString absPath = fileInfo.absoluteFilePath();
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -17,6 +17,9 @@ class SqliteDbService
 | 
				
			|||||||
	DatabaseFactory *dbFactory = nullptr;
 | 
						DatabaseFactory *dbFactory = nullptr;
 | 
				
			||||||
	bool insertToFTS(bool useTrigrams, QSqlDatabase &db, int fileid, QVector<PageData> &pageData);
 | 
						bool insertToFTS(bool useTrigrams, QSqlDatabase &db, int fileid, QVector<PageData> &pageData);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						QSqlQuery exec(QString query, std::initializer_list<QVariant> args);
 | 
				
			||||||
 | 
						bool execBool(QString querystr, std::initializer_list<QVariant> args);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  public:
 | 
					  public:
 | 
				
			||||||
	SqliteDbService(DatabaseFactory &dbFactory);
 | 
						SqliteDbService(DatabaseFactory &dbFactory);
 | 
				
			||||||
	SaveFileResult saveFile(QFileInfo fileInfo, QVector<PageData> &pageData, bool pathsOnly);
 | 
						SaveFileResult saveFile(QFileInfo fileInfo, QVector<PageData> &pageData, bool pathsOnly);
 | 
				
			||||||
@@ -24,6 +27,7 @@ class SqliteDbService
 | 
				
			|||||||
	bool deleteFile(QString path);
 | 
						bool deleteFile(QString path);
 | 
				
			||||||
	bool fileExistsInDatabase(QString path);
 | 
						bool fileExistsInDatabase(QString path);
 | 
				
			||||||
	bool fileExistsInDatabase(QString path, qint64 mtime);
 | 
						bool fileExistsInDatabase(QString path, qint64 mtime);
 | 
				
			||||||
 | 
						bool fileExistsInDatabase(QString path, qint64 mtime, QChar filetype);
 | 
				
			||||||
	QVector<SearchResult> search(const LooqsQuery &query);
 | 
						QVector<SearchResult> search(const LooqsQuery &query);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	std::optional<QChar> queryFileType(QString absPath);
 | 
						std::optional<QChar> queryFileType(QString absPath);
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user