Fimbulwinter Project  Pre-Alpha
An Ragnarok Online Emulator
Common/database_helper.h
00001 /*==================================================================*
00002 *     ___ _           _           _          _       _                          *
00003 *    / __(_)_ __ ___ | |__  _   _| |_      _(_)_ __ | |_ ___ _ __       *
00004 *   / _\ | | '_ ` _ \| '_ \| | | | \ \ /\ / / | '_ \| __/ _ \ '__|      *
00005 *  / /   | | | | | | | |_) | |_| | |\ V  V /| | | | | ||  __/ |         *
00006 *  \/    |_|_| |_| |_|_.__/ \__,_|_| \_/\_/ |_|_| |_|\__\___|_|         *
00007 *                                                                                                                                       *
00008 * ------------------------------------------------------------------*
00009 *                                                        Emulator                                       *
00010 * ------------------------------------------------------------------*
00011 *                     Licenced under GNU GPL v3                     *
00012 * ----------------------------------------------------------------- *
00013 *                        Database Functions                         *
00014 * ==================================================================*/
00015 
00016 #pragma once
00017 
00018 #include <config_file.hpp>
00019 #include <show_message.hpp>
00020 #include <soci/soci.h>
00021 
00022 class database_helper
00023 {
00024 private:
00025         static std::string driver_;
00026 
00027 public:
00028         static soci::session *get_session(config_file *conf)
00029         {
00030                 char connection_string[1024]; // 1024 should be enought
00031                 
00032                 {
00033                         string driver = conf->read<string>("database.driver", "mysql");
00034 
00035                         if (driver == "mysql")
00036                         {
00037                                 string host = conf->read<string>("database.mysql.host", "127.0.0.1");
00038                                 string port = conf->read<string>("database.mysql.port", "3306");
00039                                 string user = conf->read<string>("database.mysql.user", "ragnarok");
00040                                 string pass = conf->read<string>("database.mysql.pass", "ragnarok");
00041                                 string name = conf->read<string>("database.mysql.name", "ragnarok");
00042 
00043                                 sprintf(connection_string, "mysql://host='%s' port='%s' user='%s' password='%s' db='%s'", host.c_str(), port.c_str(), user.c_str(), pass.c_str(), name.c_str());
00044                         }
00045                         else if (driver == "sqlite3")
00046                         {
00047                                 string file = conf->read<string>("database.sqlite3.file", "ragnarok.db");
00048 
00049                                 sprintf(connection_string, "sqlite://%s", file.c_str());
00050                         }
00051                         else
00052                         {
00053                                 ShowError("Invalid driver database.");
00054 
00055                                 abort();
00056                                 
00057                                 return NULL;
00058                         }
00059 
00060                         driver_ = driver;
00061                 }
00062 
00063                 return new soci::session((std::string)connection_string);
00064         }
00065 
00066         static int get_last_insert_id(soci::session *s)
00067         {
00068                 int result = 0;
00069 
00070                 if (driver_ == "mysql")
00071                         *s << "SELECT LAST_INSERT_ID()", soci::into(result);
00072                 else if (driver_ == "sqlite3")
00073                 {
00074                         // TODO: Implement
00075                 }
00076 
00077                 return result;
00078         }
00079 };
 All Classes Functions