Commit 5b3126d2 authored by Camilo Higuita's avatar Camilo Higuita

work continues on db interfacing #2

parent 7a1d9d33
...@@ -21,7 +21,9 @@ DEFINES += QT_DEPRECATED_WARNINGS ...@@ -21,7 +21,9 @@ DEFINES += QT_DEPRECATED_WARNINGS
SOURCES += main.cpp \ SOURCES += main.cpp \
src/db/collectionDB.cpp \ src/db/collectionDB.cpp \
src/utils/pix.cpp src/utils/pix.cpp \
src/db/db.cpp \
src/db/dbactions.cpp
# Additional import path used to resolve QML modules in Qt Creator's code model # Additional import path used to resolve QML modules in Qt Creator's code model
QML_IMPORT_PATH = QML_IMPORT_PATH =
...@@ -45,4 +47,7 @@ RESOURCES += \ ...@@ -45,4 +47,7 @@ RESOURCES += \
HEADERS += \ HEADERS += \
src/utils/pix.h \ src/utils/pix.h \
src/db/collectionDB.h \ src/db/collectionDB.h \
src/db/fileloader.h src/db/fileloader.h \
src/db/db.h \
src/db/dbactions.h \
src/utils/pic.h
...@@ -13,7 +13,7 @@ ...@@ -13,7 +13,7 @@
#include <QDir> #include <QDir>
#include <QVariantMap> #include <QVariantMap>
#include "../utils/pix.h" #include "../utils/pic.h"
enum sourceTypes enum sourceTypes
{ {
......
#include "db.h" #include "db.h"
#include <QUuid>
#include <QString>
#include <QStringList>
DB::DB(QObject *parent) : QObject(parent) DB::DB(QObject *parent) : QObject(parent)
{
this->name = QUuid::createUuid().toString();
if(!PIX::fileExists(PIX::CollectionDBPath + PIX::DBName))
{
this->openDB(this->name);
qDebug()<<"Collection doesn't exists, trying to create it" << PIX::CollectionDBPath + PIX::DBName;
this->prepareCollectionDB();
}else this->openDB(this->name);
}
DB::~DB()
{
this->m_db.close();
}
void DB::openDB(const QString &name)
{
if(!QSqlDatabase::contains(name))
{
this->m_db = QSqlDatabase::addDatabase(QStringLiteral("QSQLITE"), name);
this->m_db.setDatabaseName(PIX::CollectionDBPath + PIX::DBName);
}
if (!this->m_db.isOpen())
{
if(!this->m_db.open())
qDebug()<<"ERROR OPENING DB"<<this->m_db.lastError().text()<<m_db.connectionName();
}
}
void DB::prepareCollectionDB() const
{
QSqlQuery query(this->m_db);
QFile file(":/db/script.sql");
if (!file.exists())
{
QString log = QStringLiteral("Fatal error on build database. The file '");
log.append(file.fileName() + QStringLiteral("' for database and tables creation query cannot be not found!"));
qDebug()<<log;
return;
}
if (!file.open(QIODevice::ReadOnly))
{
qDebug()<<QStringLiteral("Fatal error on try to create database! The file with sql queries for database creation cannot be opened!");
return;
}
bool hasText;
QString line;
QByteArray readLine;
QString cleanedLine;
QStringList strings;
while (!file.atEnd())
{
hasText = false;
line = "";
readLine = "";
cleanedLine = "";
strings.clear();
while (!hasText)
{
readLine = file.readLine();
cleanedLine = readLine.trimmed();
strings = cleanedLine.split("--");
cleanedLine = strings.at(0);
if (!cleanedLine.startsWith("--") && !cleanedLine.startsWith("DROP") && !cleanedLine.isEmpty())
line += cleanedLine;
if (cleanedLine.endsWith(";"))
break;
if (cleanedLine.startsWith("COMMIT"))
hasText = true;
}
if (!line.isEmpty())
{
if (!query.exec(line))
{
qDebug()<<"exec failed"<<query.lastQuery()<<query.lastError();
}
} else qDebug()<<"exec wrong"<<query.lastError();
}
file.close();
}
bool DB::insert(const QString &tableName, const QVariantMap &insertData)
{
}
bool DB::update(const QString &tableName, const PIX::DB &updateData, const QVariantMap &where)
{
}
bool DB::update(const QString &table, const QString &column, const QVariant &newValue, const QVariant &op, const QString &id)
{
}
bool DB::remove()
{ {
} }
...@@ -2,16 +2,46 @@ ...@@ -2,16 +2,46 @@
#define DB_H #define DB_H
#include <QObject> #include <QObject>
#include <QString>
#include <QStringList>
#include <QList>
#include <QSqlDatabase>
#include <QDebug>
#include <QSqlQuery>
#include <QSqlError>
#include <QSqlRecord>
#include <QSqlDriver>
#include <QFileInfo>
#include <QDir>
#include <QVariantMap>
#include "../utils/pic.h"
class DB : public QObject class DB : public QObject
{ {
Q_OBJECT Q_OBJECT
private:
QString name;
QSqlDatabase m_db;
public: public:
explicit DB(QObject *parent = nullptr); explicit DB(QObject *parent = nullptr);
~ DB();
void openDB(const QString &name);
/*basic public actions*/
void prepareCollectionDB() const;
protected:
bool insert(const QString &tableName, const QVariantMap &insertData);
bool update(const QString &tableName, const PIX::DB &updateData, const QVariantMap &where);
bool update(const QString &table, const QString &column, const QVariant &newValue, const QVariant &op, const QString &id);
bool remove();
signals: signals:
public slots: public slots:
}; };
#endif // DB_H #endif // DB_H
\ No newline at end of file
#include "dbactions.h" #include "dbactions.h"
DBActions::DBActions() DBActions::DBActions(QObject *parent) : DB(parent)
{
}
DBActions::~DBActions()
{ {
} }
...@@ -2,11 +2,13 @@ ...@@ -2,11 +2,13 @@
#define DBACTIONS_H #define DBACTIONS_H
#include <QObject> #include <QObject>
#include "db.h"
class DBActions class DBActions : public DB
{ {
public: public:
DBActions(); DBActions(QObject *parent = nullptr);
~DBActions();
}; };
#endif // DBACTIONS_H #endif // DBACTIONS_H
\ No newline at end of file
#ifndef PIC_H #ifndef PIC_H
#define PIC_H #define PIC_H
#include <QString>
#include <QDebug>
#include <QStandardPaths>
#include <QFileInfo>
#include <QImage>
#include <QTime>
#include <QSettings>
#include <QDirIterator>
#include <QVariantList>
namespace PIX
{
Q_NAMESPACE
enum SearchT
{
LIKE,
SIMILAR
};
typedef QMap<PIX::SearchT,QString> SEARCH;
static const SEARCH SearchTMap
{
{ PIX::SearchT::LIKE, "like" },
{ PIX::SearchT::SIMILAR, "similar" }
};
enum class W : uint_fast8_t
{
ALL,
NONE,
LIKE,
TAG,
SIMILAR,
UNKNOWN,
DONE,
DESC,
ASC
};
static const QMap<W,QString> SLANG =
{
{W::ALL, "ALL"},
{W::NONE, "NONE"},
{W::LIKE, "LIKE"},
{W::SIMILAR, "SIMILAR"},
{W::UNKNOWN, "UNKNOWN"},
{W::DONE, "DONE"},
{W::DESC, "DESC"},
{W::ASC,"ASC"},
{W::TAG,"TAG"}
};
enum class TABLE : uint8_t
{
SOURCES,
IMAGES,
TAGS,
ALBUMS,
IMAGES_TAGS,
IMAGES_ALBUMS,
ALL,
NONE
};
static const QMap<TABLE,QString> TABLEMAP =
{
{TABLE::ALBUMS,"albums"},
{TABLE::SOURCES,"sources"},
{TABLE::IMAGES,"images"},
{TABLE::IMAGES_TAGS,"images_tags"},
{TABLE::IMAGES_ALBUMS,"images_albums"},
{TABLE::TAGS,"tags"}
};
enum class KEY :uint8_t
{
URL,
SOURCES_URL,
TITLE,
ALBUM,
RATE,
FAV,
COLOR,
NOTE,
ADD_DATE,
PIC_DATE,
PLACE,
FORMAT,
TAG,
NONE
};
typedef QMap<PIX::KEY, QString> DB;
typedef QList<DB> DB_LIST;
static const DB KEYMAP =
{
{KEY::URL, "url"},
{KEY::SOURCES_URL, "sources_url"},
{KEY::RATE, "rate"},
{KEY::TITLE, "title"},
{KEY::ALBUM, "album"},
{KEY::FAV, "fav"},
{KEY::COLOR, "color"},
{KEY::NOTE, "note"},
{KEY::ADD_DATE, "addDate"},
{KEY::PIC_DATE, "picDate"},
{KEY::PLACE, "place"},
{KEY::FORMAT, "format"},
{KEY::ADD_DATE, "addDate"},
{KEY::TAG, "tag"}
};
enum class AlbumSizeHint : uint
{
BIG_ALBUM = 200,
MEDIUM_ALBUM = 120,
SMALL_ALBUM = 80
};
inline QString getNameFromLocation(const QString &str)
{
QString ret;
int index = 0;
for(int i = str.size() - 1; i >= 0; i--)
if(str[i] == '/')
{
index = i + 1;
i = -1;
}
for(; index < str.size(); index++)
ret.push_back(str[index]);
return ret;
}
const QString PicturesPath = QStandardPaths::writableLocation(QStandardPaths::PicturesLocation);
const QString HomePath = QStandardPaths::writableLocation(QStandardPaths::HomeLocation);
const QString SettingPath = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation)+"/pix/";
const QString CollectionDBPath = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation)+"/pix/";
const QString CachePath = QStandardPaths::writableLocation(QStandardPaths::GenericCacheLocation)+"/pix/";
const QString NotifyDir = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation);
const QString App = "Pix";
const QString version = "1.0";
const QString DBName = "collection.db";
const QStringList MoodColors = {"#F0FF01","#01FF5B","#3DAEFD","#B401FF","#E91E63"};
const QStringList formats {"*.jpg","*.jpeg","*.png","*.bmp","*.gif"};
inline QString ucfirst(const QString &str)/*uppercase first letter*/
{
if (str.isEmpty()) return "";
QStringList tokens;
QStringList result;
QString output;
if(str.contains(" "))
{
tokens = str.split(" ");
for(auto str : tokens)
{
str = str.toLower();
str[0] = str[0].toUpper();
result<<str;
}
output = result.join(" ");
}else output = str;
return output.simplified();
}
inline bool fileExists(const QString &url)
{
QFileInfo path(url);
if (path.exists()) return true;
else return false;
}
inline void savePic(DB &track, const QByteArray &array, const QString &path)
{
if(!array.isNull()&&!array.isEmpty())
{
// qDebug()<<"tryna save array: "<< array;
QImage img;
img.loadFromData(array);
QString name = track[PIX::KEY::TITLE];
name.replace("/", "-");
name.replace("&", "-");
QString format = "JPEG";
if (img.save(path + name + ".jpg", format.toLatin1(), 100))
qDebug()<<"image saved!";
else qDebug() << "couldn't save artwork";
}else qDebug()<<"array is empty";
}
inline void saveSettings(const QString &key, const QVariant &value, const QString &group)
{
QSettings setting("Babe","babe");
setting.beginGroup(group);
setting.setValue(key,value);
setting.endGroup();
}
inline QVariant loadSettings(const QString &key, const QString &group, const QVariant &defaultValue)
{
QVariant variant;
QSettings setting("Babe","babe");
setting.beginGroup(group);
variant = setting.value(key,defaultValue);
setting.endGroup();
return variant;
}
inline bool isMobile()
{
#if defined(Q_OS_ANDROID)
return true;
#elif defined(Q_OS_LINUX)
return false;
#elif defined(Q_OS_WIN32)
return false;
#elif defined(Q_OS_WIN64)
return false;
#elif defined(Q_OS_MACOS)
return false;
#elif defined(Q_OS_IOS)
return true;
#elif defined(Q_OS_HAIKU)
return false;
#endif
}
}
#endif // PIC_H #endif // PIC_H
#ifndef PIX_H #ifndef PIX_H
#define PIX_H #define PIX_H
#include "string"
#include <QString> #include <QString>
#include <QDebug> #include <QDebug>
#include <QStandardPaths> #include <QStandardPaths>
...@@ -12,244 +12,9 @@ ...@@ -12,244 +12,9 @@
#include <QDirIterator> #include <QDirIterator>
#include <QVariantList> #include <QVariantList>
#include <cmath>
using namespace std; using namespace std;
namespace PIX
{
Q_NAMESPACE
enum SearchT
{
LIKE,
SIMILAR
};
typedef QMap<PIX::SearchT,QString> SEARCH;
static const SEARCH SearchTMap
{
{ PIX::SearchT::LIKE, "like" },
{ PIX::SearchT::SIMILAR, "similar" }
};
enum class W : uint_fast8_t
{
ALL,
NONE,
LIKE,
TAG,
SIMILAR,
UNKNOWN,
DONE,
DESC,
ASC
};
static const QMap<W,QString> SLANG =
{
{W::ALL, "ALL"},
{W::NONE, "NONE"},
{W::LIKE, "LIKE"},
{W::SIMILAR, "SIMILAR"},
{W::UNKNOWN, "UNKNOWN"},
{W::DONE, "DONE"},
{W::DESC, "DESC"},
{W::ASC,"ASC"},
{W::TAG,"TAG"}
};
enum class TABLE : uint8_t
{
SOURCES,
IMAGES,
TAGS,
ALBUMS,
IMAGES_TAGS,
IMAGES_ALBUMS,
ALL,
NONE
};
static const QMap<TABLE,QString> TABLEMAP =
{
{TABLE::ALBUMS,"albums"},
{TABLE::SOURCES,"sources"},
{TABLE::IMAGES,"images"},
{TABLE::IMAGES_TAGS,"images_tags"},
{TABLE::IMAGES_ALBUMS,"images_albums"},
{TABLE::TAGS,"tags"}
};
enum class KEY :uint8_t
{
URL,
SOURCES_URL,
TITLE,
ALBUM,
RATE,
FAV,
COLOR,
NOTE,
ADD_DATE,
PIC_DATE,
PLACE,
FORMAT,
TAG,
NONE
};
typedef QMap<PIX::KEY, QString> DB;
typedef QList<DB> DB_LIST;
static const DB KEYMAP =
{
{KEY::URL, "url"},
{KEY::SOURCES_URL, "sources_url"},
{KEY::RATE, "rate"},
{KEY::TITLE, "title"},
{KEY::ALBUM, "album"},
{KEY::FAV, "fav"},
{KEY::COLOR, "color"},
{KEY::NOTE, "note"},
{KEY::ADD_DATE, "addDate"},
{KEY::PIC_DATE, "picDate"},
{KEY::PLACE, "place"},
{KEY::FORMAT, "format"},
{KEY::ADD_DATE, "addDate"},
{KEY::TAG, "tag"}
};
enum class AlbumSizeHint : uint
{
BIG_ALBUM = 200,
MEDIUM_ALBUM = 120,
SMALL_ALBUM = 80
};
inline QString getNameFromLocation(const QString &str)
{
QString ret;
int index = 0;
for(int i = str.size() - 1; i >= 0; i--)
if(str[i] == '/')
{
index = i + 1;
i = -1;
}
for(; index < str.size(); index++)
ret.push_back(str[index]);