
#include <QtGui>

#include "groupinfo.h"

PGroupInfo::PGroupInfo()
{
	Name="";
	
    	ID=0;
	
	this->dbSet();
}

PGroupInfo::~PGroupInfo(){
	
	this->dbUnset();

}

void PGroupInfo::dbSet(void){

db = new QSqlDatabase(QSqlDatabase::addDatabase("QSQLITE"));
db->setDatabaseName("/sdcard/test.db");

}

void PGroupInfo::dbUnset(void){

QString connection;
	connection=db->connectionName();
	db->close();
	delete db;
	QSqlDatabase::removeDatabase(connection);

}

bool PGroupInfo::AddGroup(QString name){
		
	return (this->dbQuery(QString("INSERT INTO Skupiny ('ID','Nazev') VALUES (NULL,'%1')").arg(name)));

}

bool PGroupInfo::DeleteGroup(QString name){

	return (this->dbQuery(QString("DELETE FROM Skupiny WHERE 'Nazev'= '%1'").arg(name)));	

}
bool PGroupInfo::DeleteGroup(int id){

	return (this->dbQuery(QString("DELETE FROM Skupiny WHERE ID = %1").arg(id)));

}
bool PGroupInfo::RenameGroup(int id, QString newname){

	return (this->dbQuery(QString("UPDATE Skupiny SET 'Nazev'= '%1' WHERE ID = %2").arg(newname).arg(id)));

}
bool PGroupInfo::RenameGroup(QString oldname, QString newname){

	return (this->dbQuery(QString("UPDATE Skupiny SET 'Nazev'= '%1' WHERE 'Nazev' = %2").arg(newname).arg(oldname)));

}

QStringList PGroupInfo::GetGroupList(void){

QStringList groups;

	db->open();
	if(db->isOpen()){
		QSqlQuery query(QString("SELECT Nazev FROM Skupiny"));
			while(query.next()){
				groups.append(query.value(0).toString());	
			}
			db->close();
			return groups;
	}
	

}

bool PGroupInfo::dbQuery(QString query){

	bool ret=0;

	db->open();
		if (db->isOpen()){

			QSqlQuery currentQuery;
			ret = currentQuery.exec(query);
			return ret;

		}
		else return(false);
}

