OSDN Git Service

fix cmake/Macros.cmake
[moflib/moflib.git] / saisei-1.0 / rpg / rpg / sqlite_wrapper.cpp
1 #include <sqlite_wrapper.hpp>
2 #include <mof/ConsoleIO.hpp>
3 #include <mof/utilities.hpp>
4 #include <mof/script/ObjectData.hpp>
5 #include <sqlite3.h>
6 #include <expatJP.h>
7
8 namespace 
9 {
10         int callback_select(void* data, int argc, char** argv, char** colnames)
11         {
12                 mof::script::GameData* gamedata = static_cast<mof::script::GameData*>(data);
13                 gamedata->data_.resize(gamedata->data_.size() + 1);
14                 static char converted_text[512];
15                 for (int i = 0 ; i < argc; i++) {
16                         char* colname = colnames[i];    
17                         char* value = argv[i];  
18                         if (!value) throw std::runtime_error(std::string(colname) + ": null value");
19                         int converted_len = UTF8toSJIS(value, strlen(value), converted_text, mof::lengthOf(converted_text));
20                         gamedata->data_.back()[colname] = mof::tstring(converted_text, converted_len);
21                 }
22                 return 0;
23         }
24 }
25
26 //namespace mof
27 //{
28 //namespace script
29 //{
30 //{{{ impl
31         struct sqlite_wrapper::impl
32         {
33                 sqlite3* db;
34
35                 impl()
36                         : db(NULL)
37                 {
38                 }
39         };
40 //}}}
41 //{{{ constructor
42         sqlite_wrapper::sqlite_wrapper()
43                 : pimpl_(new impl())
44         {
45                 if (sqlite3_open("gamedata.db", &pimpl_->db) != SQLITE_OK) {
46                         throw std::runtime_error("Faild: sqlite3_open");
47                 }
48         }
49 //}}}
50 //{{{ destructor
51         sqlite_wrapper::~sqlite_wrapper()
52         {
53                 sqlite3_close(pimpl_->db);
54         }
55 //}}}
56 //{{{ query_item_data
57         mof::script::GameData::ptr sqlite_wrapper::query_item_profile()
58         {
59                 using namespace mof::script;
60                 GameData::ptr p = std::make_shared<GameData>();
61                 static const char* sql = 
62                         "SELECT item_profile.id, item_id, item_profile.durability, item.durability as max_durability, name, "
63                         "type, attached, stored, equipped, icon_type "
64                         "FROM item_profile, item WHERE item.id = item_profile.item_id";
65                 char* err;
66                 if (sqlite3_exec(pimpl_->db, sql, callback_select, (void*)p.get(), &err) != SQLITE_OK) {
67                         std::logic_error e(std::string("[") + sql + "]" + err);
68                         sqlite3_free(err);
69                         throw e;
70                 }
71                 return p;
72         }
73 //}}}
74 //{{{ query_relic_data
75         mof::script::GameData::ptr sqlite_wrapper::query_relic_profile()
76         {
77                 using namespace mof::script;
78                 GameData::ptr p = std::make_shared<GameData>();
79                 static const char* sql = 
80                         "SELECT relic_profile.id, relic_id, num, name, icon_type "
81                         "FROM relic_profile, relic WHERE relic.id = relic_profile.relic_id";
82                 char* err;
83                 if (sqlite3_exec(pimpl_->db, sql, callback_select, (void*)p.get(), &err) != SQLITE_OK) {
84                         std::logic_error e(std::string("[") + sql + "]" + err);
85                         sqlite3_free(err);
86                         throw e;
87                 }
88                 return p;
89         }
90 //}}}
91 //{{{ query_ideal_data
92         mof::script::GameData::ptr sqlite_wrapper::query_ideal_profile()
93         {
94                 using namespace mof::script;
95                 GameData::ptr p = std::make_shared<GameData>();
96                 static const char* sql = 
97                         "SELECT ideal_profile.id, ideal_id, name, icon_type, type, attend "
98                         "FROM ideal_profile, ideal WHERE ideal.id = ideal_profile.ideal_id";
99                 char* err;
100                 if (sqlite3_exec(pimpl_->db, sql, callback_select, (void*)p.get(), &err) != SQLITE_OK) {
101                         std::logic_error e(std::string("[") + sql + "]" + err);
102                         sqlite3_free(err);
103                         throw e;
104                 }
105                 return p;
106         }
107 //}}}
108 //}// namespace script
109 //}// namespace mof