Documentation:Doc Outils PFCXX User exemple1

From Gameforge Official Website

Jump to: navigation, search
#include <iostream>
#include <string>
 
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
 
#include "include1/TotoTyty.hh"
#include "include2/Tata.hh"
 
#include "gameforge/tools/pfcxx/tools/DbConnectorPool.hh"
#include "gameforge/tools/pfcxx/tools/DefaultLoggerHandler.hh"
#include "gameforge/tools/pfcxx/tools/PfcxxException.hh"
#include "gameforge/tools/pfcxx/tools/String.hh"
 
#include "gameforge/tools/pfcxx/query/Exist.hh"
#include "gameforge/tools/pfcxx/query/Create.hh"
#include "gameforge/tools/pfcxx/query/Select.hh"
 
using namespace std;
using namespace ::gt::hh::ppp;
using namespace ::hh::jj::kkkk;
 
using namespace gameforge::tools::pfcxx;
using namespace gameforge::tools::pfcxx::tools;
using namespace gameforge::tools::pfcxx::drivers;
using namespace gameforge::tools::pfcxx::query;
 
class Log : public DefaultLoggerHandler
{
  public:
    /**
     *
     */
    Log(void)
    {
    }
 
    /**
     *
     */
    virtual ~Log(void)
    {
    }
 
    virtual void debug(const std::string& msg, const char* file, int line)
    {
      cout << "debug (file: " << file << ", line: " << line << ") : " << msg << endl;
    }
 
    virtual void info(const std::string& msg, const char* file, int line)
    {
      cout << "info (file: " << file << ", line: " << line << ") : " << msg << endl;
    }
 
    virtual void warn(const std::string& msg, const char* file, int line)
    {
      cout << "warn (file: " << file << ", line: " << line << ") : " << msg << endl;
    }
 
    virtual void error(const std::string& msg, const char* file, int line)
    {
      cout << "error (file: " << file << ", line: " << line << ") : " << msg << endl;
    }
 
    virtual void fatal(const std::string& msg, const char* file, int line)
    {
      cout << "fatal (file: " << file << ", line: " << line << ") : " << msg << endl;
    }
};
 
int main(void)
{
  // Set conf
  DbConnectorPool::Config conf;
 
  conf.driver = Postgresql;
  conf.userDb = "luther";
  conf.db = "testdb";
  conf.pwdDb = "";
  conf.cacheType = DbConnectorPool::Config::OnePerConnection;
  conf.logger = LoggerHandlerPtr(new Log);
 
  DbConnectorPool::getInstance() -> setConfig(conf);
 
  // Get connector
  try
  {
    DbConnectorPtr con = DbConnectorPool::getInstance() -> getConnector();
 
    Exist<Tata> exist1;
    if(! exist1.execute())
    {
      Create<Tata> create;
      create.execute();
    }
 
    Exist<TotoTyty> exist2;
    if(! exist2.execute())
    {
      Create<TotoTyty> create;
      create.execute();
    }
 
    for(int i = 0 ; i < 15 ; ++i)
    {
      Tata::ptr tata = Tata::newInstance();
 
      // insert
      tata -> update();
 
      // mod text
      tata -> yuyu = "ddd " + toString(i);
      tata -> update();
 
      // do nothong !
      tata -> update();
 
      TotoTyty::ptr toto = TotoTyty::newInstance();
 
      // link
      toto -> fkid.link(tata);
      toto -> yuyu = "toto " + toString(i);
 
      // insert
      toto -> update();
    }
 
    Select<TotoTyty> select1;
    select1.where<TotoTyty::eId>(LessThan, 7);
 
    vector<TotoTyty::ptr> r = select1.all();
 
    for(unsigned int i = 0 ; i < r.size() ; ++i)
    {
      cout << r[i] -> id.value() << " ";
      cout << r[i] -> yuyu.value() << " ";
      cout << r[i] -> pouet.value() << " ";
      cout << r[i] -> fkid.value() << " " << endl;
    }
 
    Select<TotoTyty> select2;
    select2.where<TotoTyty::eId>(Equals, 10);
 
    TotoTyty::ptr toto = select2.one();
 
    Tata::ptr tata = toto -> fkid();
 
    con -> rollback();
  }
  catch(PfcxxException &e)
  {
    cerr << e.what() << endl;
  }
 
  // End
  try
  {
    DbConnectorPool::getInstance() -> destroy();
  }
  catch(PfcxxException &e)
  {
    cerr << e.what() << endl;
  }
 
  return 0;
}
Personal tools