Name of table at runtime

Forum for posting problems using QxOrm library

Name of table at runtime

Postby andigor » Fri Jun 15, 2012 5:14 pm

Hello


Is there any way to specify table name for the qx::dao functions during runtime ?
andigor
 
Posts: 14
Joined: Wed May 02, 2012 11:53 am

Re: Name of table at runtime

Postby QxOrm admin » Sat Jun 16, 2012 3:15 pm

Hi,

Is there any way to specify table name for the qx::dao functions during runtime ?

I recommend to do it statically into qx::register_class<T> function using t.setName("myTable") method to associate class T to table myTable of database.

But you can also do it at runtime in your code if you want using introspection engine of QxOrm library.
You have to get the qx::IxClass pointer and then put the name of your table, something like this :
Code: Select all
qx::IxClass * pClass = qx::QxClassX::getClass("myClass");
pClass->setName("myTable2"); // to associate the C++ class "myClass" to the table "myTable2" of database
QxOrm admin
 

Re: Name of table at runtime

Postby andigor » Sat Jun 16, 2012 4:55 pm

And I can use it for the interaction with several different named tables of same type without any side effects ?
andigor
 
Posts: 14
Joined: Wed May 02, 2012 11:53 am

Re: Name of table at runtime

Postby QxOrm admin » Sat Jun 16, 2012 8:40 pm

Yes, I don't see any side effects using this method.

But as I said, I don't recommend it : I think it"s better to map only one C++ persistent class per table into database.
So this is why I recommend to associate your table into the method qx::register_class<T>.

If you have multiple tables with the same columns, maybe it's a better solution to use C++ class inheritance : you could define a base class to map all columns, and then, for each table, just create a derived class. This way, you will have one C++ class per table without writing all columns for each C++ class (all columns are mapped only into your base class).
For more details about inheritance and QxOrm library : http://www.qxorm.com/qxorm_en/faq.html#faq_120
QxOrm admin
 

Re: Name of table at runtime

Postby andigor » Sun Jun 17, 2012 7:35 am

I have some instances of type called TAG. Instantiating controlled by the configuration file. For each instance of TAG I need to have MYSQL table of type TAG_LOG_TABLE named as concrete TAG instance... So it seems that static initialization can't help me.
I asked about side effects because I saw QX_SQL_BUILDER_INIT_FCT macro with static QString sql.
andigor
 
Posts: 14
Joined: Wed May 02, 2012 11:53 am

Re: Name of table at runtime

Postby QxOrm admin » Sun Jun 17, 2012 11:13 am

I asked about side effects because I saw QX_SQL_BUILDER_INIT_FCT macro with static QString sql.

Yes you are right, there is a cache mechanism to build SQL query only one time per persistent class.
So it will not work to change dynamically the table name associated to the C++ class, sorry for that !

So you have to create one C++ persistent class per table, using inheritance or not.

EDIT: maybe the interface qx::IxPersistable can help you. It provides all persistents functions without knowing the real type of the persistent class.
For more details about qx::IxPersistable interface :
* FAQ here : http://www.qxorm.com/qxorm_en/faq.html#faq_260
* Documentation here : http://www.qxorm.com/doxygen/html/class ... table.html
QxOrm admin
 

Re: Name of table at runtime

Postby andigor » Sun Jun 17, 2012 4:35 pm

Ok. Thank you.
andigor
 
Posts: 14
Joined: Wed May 02, 2012 11:53 am


Return to QxOrm - Help

Who is online

Users browsing this forum: No registered users and 6 guests

cron