In progress

This commit is contained in:
Grégory Soutadé 2010-05-15 11:21:42 +02:00
parent ee8d4c0c8f
commit 74e6d36c22
10 changed files with 167 additions and 37 deletions

View File

@ -1,10 +1,12 @@
CREATE TABLE user (id INTEGER PRIMARY KEY, name VARCHAR(255), password VARCHAR(255)); CREATE TABLE user (id INTEGER PRIMARY KEY, name VARCHAR(255), password VARCHAR(255));
CREATE TABLE account(id INTEGER PRIMARY KEY, user REFERENCES user(id), name VARCHAR(255), number VARCHAR(255), shared CHAR(1), default CHAR(1)); CREATE TABLE account(id INTEGER PRIMARY KEY, user REFERENCES user(id), name VARCHAR(255), number VARCHAR(255), amount INTEGER, shared CHAR(1), default_account CHAR(1));
CREATE TABLE operation(id INTEGER PRIMARY KEY, account REFERENCES account(id), year INTEGER, month INTEGER, day INTEGER, description VARCHAR(255), category VARCHAR(255), fix_cost CHAR(1)); CREATE TABLE operation(id INTEGER PRIMARY KEY, user REFERENCES user(id), account REFERENCES account(id), year INTEGER, month INTEGER, day INTEGER, amount INTEGER, description VARCHAR(255), category VARCHAR(255), fix_cost CHAR(1));
CREATE TABLE preference(id INTEGER PRIMARY KEY, user REFERENCES user(id), type VARCHAR(255), name VARCHAR(255), value VARCHAR(255)); CREATE TABLE preference(id INTEGER PRIMARY KEY, user REFERENCES user(id), type VARCHAR(255), name VARCHAR(255), value VARCHAR(255));
CREATE TABLE default_preference(id INTEGER PRIMARY KEY, type VARCHAR(255), name VARCHAR(255), value VARCHAR(255)); CREATE TABLE default_preference(id INTEGER PRIMARY KEY, type VARCHAR(255), name VARCHAR(255), value VARCHAR(255));
INSERT INTO default_preference ("type", "value") VALUES ("category", "Fixe"); INSERT INTO default_preference ("type", "value") VALUES ("category", "Fixe");
INSERT INTO default_preference ("type", "value") VALUES ("category", "Courses"); INSERT INTO default_preference ("type", "value") VALUES ("category", "Courses");
INSERT INTO default_preference ("type", "value") VALUES ("category", "Loisirs"); INSERT INTO default_preference ("type", "value") VALUES ("category", "Loisirs");
INSERT INTO default_preference ("type", "value") VALUES ("category", "Frais de fonctionnement"); INSERT INTO default_preference ("type", "value") VALUES ("category", "Frais de fonctionnement");
INSERT INTO default_preference ("type", "value") VALUES ("category", "Exceptionnel"); INSERT INTO default_preference ("type", "value") VALUES ("category", "Exceptionnel");
INSERT INTO user ("id", "name", "password") VALUES ("0", "Greg", "da39a3ee5e6b4b0d3255bfef95601890afd80709");
INSERT INTO account ("id", "user", "name", "number", "amount", "shared", "default_account") VALUES ("0", "0", "Compte courant", "000", "1234", "0", "1");

7
model/Account.cpp Normal file
View File

@ -0,0 +1,7 @@
#include "Account.h"
Account::~Account()
{
}

View File

@ -3,24 +3,28 @@
#include <list> #include <list>
#include <map> #include <map>
#include <wx/wx.h>
struct operation { struct operation {
wxString id;
unsigned int day; unsigned int day;
unsigned int month; unsigned int month;
unsigned int year; unsigned int year;
unsigned int amount; int amount;
std::string description; wxString description;
std::string category; wxString category;
bool fix_cost; bool fix_cost;
} ; } ;
class Account class Account
{ {
private: public:
std::string _name; ~Account();
std::string _number;
std::map<unsigned int, std::map<unsigned int, std::list<operation> > > _operations; wxString _id;
wxString _name;
wxString _number;
int _amount;
bool _shared; bool _shared;
bool _default; bool _default;
}; };

View File

@ -1,5 +1,26 @@
#include "Database.h" #include "Database.h"
#define EXECUTE_SQL_QUERY_WITH_CODE(req, res, return_value, code_if_fail, code_if_syntax_fail) \
do{\
try\
{\
if (!_db.CheckSyntax(req)) \
{\
std::cout << req << " is invalid !\n" ; \
code_if_syntax_fail; \
return return_value;\
} \
res = _db.ExecuteQuery(req);\
}\
catch (wxSQLite3Exception e)\
{\
std::cerr << e.GetMessage().mb_str() << "\n" ;\
code_if_fail; \
return return_value;\
}\
} while(0);
#define EXECUTE_SQL_QUERY(req, res, return_value) EXECUTE_SQL_QUERY_WITH_CODE(req, res, return_value, {}, {})
Database::Database() Database::Database()
{ {
@ -21,6 +42,7 @@ void Database::CreateDatabase()
{ {
std::ifstream init_script; std::ifstream init_script;
std::string line; std::string line;
wxString wxline;
wxMessageBox(_("No database found, creating a new one"), _("KissCount"), wxICON_EXCLAMATION | wxOK ); wxMessageBox(_("No database found, creating a new one"), _("KissCount"), wxICON_EXCLAMATION | wxOK );
@ -37,18 +59,22 @@ void Database::CreateDatabase()
do do
{ {
getline(init_script, line); getline(init_script, line);
if (!_db.CheckSyntax(line.data())) wxline = wxString(line.c_str(), wxConvUTF8);
wxline.Trim(false);
if (!wxline.Length() || wxline.StartsWith(_("#"))) continue;
if (!_db.CheckSyntax(wxline))
{ {
std::cout << line << " is invalid !\n" ; std::cout << line << " is invalid !\n" ;
continue; continue;
} }
try try
{ {
_db.ExecuteUpdate(line.data()); _db.ExecuteUpdate(wxline);
} }
catch (...) catch (...)
{ {
wxMessageBox(_("Error creating original database"), _("Error"), wxICON_ERROR | wxOK ); wxMessageBox(_("Error creating original database"), _("Error"), wxICON_ERROR | wxOK );
remove(BDD_FILE);
throw line; throw line;
} }
} while (init_script); } while (init_script);
@ -62,15 +88,8 @@ std::list<wxString> Database::GetUsers()
// Check whether value exists in table // Check whether value exists in table
wxSQLite3ResultSet set ; wxSQLite3ResultSet set ;
try
{ EXECUTE_SQL_QUERY(_("SELECT name FROM user ORDER BY name"), set, res);
set = _db.ExecuteQuery(wxT("SELECT name FROM user"));
}
catch (wxSQLite3Exception e)
{
std::cerr << e.GetMessage().mb_str() << "\n" ;
return res;
}
while (set.NextRow()) while (set.NextRow())
{ {
@ -97,19 +116,75 @@ bool Database::IsValidUser(wxString user, wxString password)
wxSHA += wxString::Format(wxT("%02F"), (int)sha[i]); wxSHA += wxString::Format(wxT("%02F"), (int)sha[i]);
req = _("SELECT name FROM user WHERE name='") + user + _("' AND password='") + wxSHA + _("'"); req = _("SELECT name FROM user WHERE name='") + user + _("' AND password='") + wxSHA + _("'");
// Check whether value exists in table req.Printf(_("req : %s\n"));
try
{ EXECUTE_SQL_QUERY(req, set, false);
set = _db.ExecuteQuery(req);
}
catch (wxSQLite3Exception e)
{
std::cerr << e.GetMessage().mb_str() << "\n" ;
return false;
}
res = set.NextRow() ; res = set.NextRow() ;
set.Finalize(); set.Finalize();
return res; return res;
} }
User* Database::LoadUser(wxString name)
{
wxSQLite3ResultSet set;
wxString req;
User* user;
Account* account;
std::list<Account*>::iterator it;
req = _("SELECT * FROM user WHERE name='") + name + _("'");
EXECUTE_SQL_QUERY(req, set, NULL);
if (!set.NextRow())
return NULL;
user = new User();
user->_id = set.GetAsString(_("id"));
user->_name = set.GetAsString(_("name"));
user->_password = _("") ; // Security reasons set.GetAsString("password");
set.Finalize();
req = _("SELECT * FROM account WHERE user='") + user->_id + _("' ORDER BY default DESC, name ASC");
EXECUTE_SQL_QUERY_WITH_CODE(req, set, NULL, {delete user;});
while (set.NextRow())
{
account = new Account();
account->_id = set.GetAsString(_("id"));
account->_name = set.GetAsString(_("name"));
account->_number = set.GetAsString(_("number"));
account->_amount = set.GetInt(_("amount"));
account->_shared = set.GetBool(_("shared"));
account->_default = set.GetBool(_("default_account"));
user->_accounts.push_back(account);
}
set.Finalize();
if (!user->_accounts.empty())
{
it = user->_accounts.begin();
req = _("SELECT DISTINCT year FROM operation WHERE account IN(") + (*it)->_id;
it++;
for (;it != user->_accounts.end(); it++)
{
req += _(", ") + (*it)->_id ;
}
req += _(") ORDER BY year ASC");
EXECUTE_SQL_QUERY_WITH_CODE(req, set, NULL, {delete user;});
while (set.NextRow())
{
user->_operations[set.GetInt(_("year"))] = NULL;
}
set.Finalize();
}
return user;
}

View File

@ -1,12 +1,15 @@
#ifndef DATABASE_H #ifndef DATABASE_H
#define DATABASE_H #define DATABASE_H
#include <stdio.h>
#include <fstream> #include <fstream>
#include <list> #include <list>
#include <wx/wxsqlite3.h> #include <wx/wxsqlite3.h>
#include <wx/wx.h> #include <wx/wx.h>
#include <sha1.h> #include <sha1.h>
#include "model.h"
#define BDD_FILE "kc.bdd" #define BDD_FILE "kc.bdd"
#define INIT_SCRIPT "init.sql" #define INIT_SCRIPT "init.sql"
@ -17,6 +20,8 @@ class Database
std::list<wxString> GetUsers(); std::list<wxString> GetUsers();
bool IsValidUser(wxString user, wxString password); bool IsValidUser(wxString user, wxString password);
User* LoadUser(wxString name);
private: private:
wxSQLite3Database _db; wxSQLite3Database _db;

View File

@ -6,8 +6,9 @@
class Preferences class Preferences
{ {
private: public:
std::map<std::string, wxColour> _colors; std::map<wxString, wxColour> _colors;
std::list<wxString> _categories;
}; };
#endif #endif

21
model/User.cpp Normal file
View File

@ -0,0 +1,21 @@
#include "User.h"
User::~User()
{
std::map<unsigned int, std::map<unsigned int, std::list<operation> >* >::iterator it;
while (!_accounts.empty())
{
delete _accounts.front();
_accounts.pop_front();
}
for (it = _operations.begin(); it != _operations.end(); it++)
{
if (_operations[it->first])
{
delete it->second;
}
}
}

View File

@ -4,14 +4,19 @@
#include <list> #include <list>
#include "Account.h" #include "Account.h"
#include "Preferences.h"
class User class User
{ {
private: public:
unsigned int _id; ~User();
wxString _id;
wxString _name; wxString _name;
wxString _password; wxString _password;
std::list<Account> _accounts; std::list<Account*> _accounts;
std::map<unsigned int, std::map<unsigned int, std::list<operation> >* > _operations;
Preferences _preferences;
}; };
#endif #endif

8
model/model.h Normal file
View File

@ -0,0 +1,8 @@
#ifndef MODEL_H
#define MODEL_H
#include "User.h"
#include "Account.h"
#include "Preferences.h"
#endif

View File

@ -29,6 +29,8 @@ UsersDialog::UsersDialog(KissCount* kiss, wxUI *parent) : wxDialog(&(*parent), -
_users->Append(*i); _users->Append(*i);
} }
_users->Select(0);
vbox1->Add(_users); vbox1->Add(_users);
vbox1->Add(-1, 10); vbox1->Add(-1, 10);
vbox1->Add(_password); vbox1->Add(_password);