List:MySQL++« Previous MessageNext Message »
From:Mickael Wolff Date:September 26 2012 8:48am
Subject:Re: Problem using the sql_create_# macro in a header file
View as plain text  
On 09/26/2012 07:18 AM, Mensfort wrote:
 > You are putting code in the include file, which is compiled for each
 > time it is included. This code should be moved to the cpp file or used
 > inside the class. For example these 3 steps:

   I would suggest Kevin the use of pimpl idiom in this case, to lower 
compilation problems.


 > 1) In the include file you put a class around your functions:
 >
 > class CsqlCreator
 > {
 > CsqlCreator() {}
 > ~CsqlCreator() {}
 >
 > static void createMyDatabase()
 > {
 >
 > sql_create_5(DBPTrans, 1, 5,
 >
 > mysqlpp::sql_int, trans_id,
 >
 > mysqlpp::sql_datetime, trans_time,
 >
 > mysqlpp::sql_varchar, trans_type,
 >
 > mysqlpp::sql_char, ticket_id,
 >
 > mysqlpp::sql_mediumtext, data);
 > }
 > };

   It should be provided standalone example of code that could be 
compiled (including a main and all includes)

 >
 > 2) At the moment you create the database, use :
 > CsqlCreator x;
 > x.createMyDatabase();

   Two problems:
   - creation of an fake initialized object
   - call of a static function member on an object (this should raise at 
least a warning, isn't it?)

 > 3) Also, you should put following line on top of each include file:
 > #pragma once

   G++ don't support that (even if C++0x is in use).

   I would suggest something like that (take care, std;;unique_ptr and 
std::make_unique are C++0x features):

// File.hpp

typedef DBPTrans ;

class CsqlCreator
{
    public:
         CsqlCreator();
         ~CsqlCreator();

         static DBPTrans * create() ;

	// Declare factory methods here

   private:
	struct Impl ;
	std::unique_ptr<Impl> p_impl ;
};


// File.cpp

// If the struct is defined here, it means it will be an incomplete type 
elsewhere
sql_create_5(DBPTrans, 1, 5,

                mysqlpp::sql_int, trans_id,

                mysqlpp::sql_datetime, trans_time,

                mysqlpp::sql_varchar, trans_type,

                mysqlpp::sql_char, ticket_id,

                mysqlpp::sql_mediumtext, data);

struct CsqlCreator::Impl
{
} ;

CsqlCreator::CsqlCreator()
	: p_impl(std::make_unique<Impl>())
{
}
CsqlCreator::~CsqlCreator() { }

static DBPTrans * CsqlCreator::create()
{
     /* ... */
}

   It can be hard for you (as a beginner) to keep MySQL++ stuff 
invisible from the rest of the application. Declare structures, use 
helper function to make usage closer from your need, but keep in mind 
you don't need (now) to encapsulate MySQL++ in your application. To do 
so, it will lead you to misconception because you don't know yet how to 
use MySQL++ (I had the same problem, overengeneering and overdesigning).

   Regards, Mickaël.
Thread
Problem using the sql_create_# macro in a header fileKevin Smith26 Sep
  • Re: Problem using the sql_create_# macro in a header fileChris Morgan26 Sep
    • RE: Problem using the sql_create_# macro in a header fileLayna Francisco26 Sep
      • Re: Problem using the sql_create_# macro in a header fileMensfort26 Sep
        • Re: Problem using the sql_create_# macro in a header fileMickael Wolff26 Sep
    • RE: Problem using the sql_create_# macro in a header fileKevin Smith26 Sep