~adrian-arroyocalle/divel/master

« back to all changes in this revision

Viewing changes to drivers/Local.hpp

  • Committer: adrian
  • Date: 2015-07-09 14:43:56 UTC
  • Revision ID: git-v1:3116a5f4580e8f21378696be05aecbf69431d359
SQLite 3 backend y diálogo de Add

Show diffs side-by-side

added added

removed removed

Lines of Context:
7
7
#include "../core/Task.hpp"
8
8
#include "../core/Category.hpp"
9
9
#include <iostream>
 
10
#include <cstdlib>
 
11
#include <cstring>
10
12
 
11
13
class LocalDriver : public MagnasTareasDriver {
12
14
public:
13
15
        LocalDriver(){
 
16
                int result;
14
17
                std::cout << "Local driver. Using SQlite: " << SQLITE_VERSION << std::endl;
15
 
        };
16
 
        ~LocalDriver(){};
 
18
                std::string tableSentence="CREATE TABLE IF NOT EXISTS MagnasTareas(TITLE TEXT, UUID TEXT UNIQUE, DESCRIPTION TEXT, CATEGORY TEXT, CATEGORY_UUID TEXT, FINISHED INTEGER);";
 
19
                #ifdef WIN32
 
20
                std::string path=std::getenv("APPDATA");
 
21
                #else
 
22
                std::string path=std::getenv("HOME");
 
23
                #endif
 
24
                path+="/.MagnasTareas";
 
25
                
 
26
                result=sqlite3_open(path.c_str(),&db);
 
27
                if(result!=SQLITE_OK){
 
28
                        std::cerr << "Error while opening the database" << std::endl;
 
29
                        std::exit(2);
 
30
                }
 
31
                result=sqlite3_exec(db,tableSentence.c_str(),0,0,0);
 
32
                if(result!=SQLITE_OK){
 
33
                        std::cerr << "Error while creating the database table" << std::endl;
 
34
                }
 
35
                
 
36
                
 
37
                
 
38
                
 
39
        };
 
40
        ~LocalDriver(){
 
41
                sqlite3_close(db);
 
42
        };
17
43
        bool IsEnabled(){
18
44
                return true; // Always true because local storage always works
19
45
        }
20
46
        std::vector<Category> GetCategories(){
21
 
                //Hacerlo bien en SQlite 3
22
 
                Category cat1;
23
 
                cat1.name="Categoria 1";
24
 
                //cat1.driverName=LOCAL;
25
 
                cat1.uuid="local-1";
26
 
 
27
 
                Category cat2;
28
 
                cat2.name="Categoria 2";
29
 
                //cat2.driverName=LOCAL;
30
 
                cat2.uuid="local-2";
31
 
 
32
 
                Category cat3;
33
 
                cat3.name="Categoria TONTA";
34
 
                cat3.uuid="local-3";
35
 
 
36
 
                Category cat4;
37
 
                cat4.name="SQLite";
38
 
                cat4.uuid="local-4";
39
 
 
40
 
                Task task1;
41
 
                task1.name="MagnasTareas 001";
42
 
                task1.description="Cargar tareas desde las categorias";
43
 
                task1.completed=false;
44
 
                task1.uuid="local-1";
45
 
                cat4.tasks.push_back(task1);
46
 
 
47
 
                std::vector<Category> vector;
48
 
                vector.push_back(cat1);
49
 
                vector.push_back(cat2);
50
 
                vector.push_back(cat3);
51
 
                vector.push_back(cat4);
52
 
 
53
 
                return vector;
 
47
                std::vector<Category> categories;
 
48
                int result;
 
49
                std::string sentence="SELECT DISTINCT CATEGORY_UUID,CATEGORY FROM MagnasTareas;";
 
50
                
 
51
                sqlite3_stmt* next;
 
52
                if(sqlite3_prepare(db,sentence.c_str(),std::strlen(sentence.c_str()),&next,0)==SQLITE_OK){
 
53
                        while(sqlite3_step(next)==SQLITE_ROW){
 
54
                                Category cat;
 
55
                                const char* category=reinterpret_cast<const char*>(sqlite3_column_text(next,1));
 
56
                                cat.name=category;
 
57
                                const char* uuid=reinterpret_cast<const char*>(sqlite3_column_text(next,0));
 
58
                                cat.uuid=uuid;
 
59
                                
 
60
                                std::string sent="SELECT TITLE, UUID, DESCRIPTION, FINISHED FROM MagnasTareas WHERE CATEGORY_UUID='"+cat.uuid+"';";
 
61
                                sqlite3_stmt* next2;
 
62
                                if(sqlite3_prepare(db,sent.c_str(),std::strlen(sent.c_str()),&next2,0)==SQLITE_OK){
 
63
                                        while(sqlite3_step(next2)==SQLITE_ROW){
 
64
                                                Task task;
 
65
                                                task.name=reinterpret_cast<const char*>(sqlite3_column_text(next2,0));
 
66
                                                task.uuid=reinterpret_cast<const char*>(sqlite3_column_text(next2,1));
 
67
                                                task.description=reinterpret_cast<const char*>(sqlite3_column_text(next2,2));
 
68
                                                task.completed=(bool)sqlite3_column_int(next2,3);
 
69
                                                cat.tasks.push_back(task);
 
70
                                        }
 
71
                                }
 
72
                                categories.push_back(cat);
 
73
                        }
 
74
                }
 
75
 
 
76
                return categories;
54
77
        }
 
78
private:
 
79
        sqlite3* db;
55
80
 
56
81
};
57
82
 
58
 
#endif
 
 
b'\\ No newline at end of file'
 
83
#endif