qx::dao::update_optimized ??

Forum for posting problems using QxOrm library

qx::dao::update_optimized ??

Postby doulos » Thu Aug 28, 2014 3:24 pm

Hello,

I am trying to implement persistent classes that will perform a database update inside each property setter. Ideally, I would like to see only the columns affected by the setter being updated, which is why I chose the qx::dao::update_optimized() function. Heres the issues that I have:

1. the qx::dao::update_optimized function takes a qx::dao::ptr argument, which dont seem to be able to obtain other than by creating it locally. This seems quite strange, as I have to also prevent the ptr to delete my object when it goes out of scope (see code below). Is there a better way?
2. the SQL update statement generated references all columns, which is what I wanted to avoid.

Code: Select all
   
static void dontdelete(ProjectDefaults *obj)
{
}
void ProjectDefaults::setHoursLaborPerCowYearP(const int & val)
{
    setHoursLaborPerCowYear(val);
    qx::dao::ptr<ProjectDefaults> ptr(this, dontdelete);
    qx::dao::update_optimized(ptr);
}
doulos
 
Posts: 15
Joined: Mon Aug 11, 2014 1:24 pm

Re: qx::dao::update_optimized ??

Postby qxorm » Thu Aug 28, 2014 4:37 pm

Hello,

To update a column (or a list of columns), you have to use the classic qx::dao::update() function.
The third parameter of this function is the column you want to update.

About qx::dao::ptr<T> and qx::dao::update_optimized(), there is an answer in the FAQ here :
http://www.qxorm.com/qxorm_en/faq.html#faq_81
qxorm
Site Admin
 
Posts: 481
Joined: Mon Apr 12, 2010 7:45 am

Re: qx::dao::update_optimized ??

Postby doulos » Fri Aug 29, 2014 7:48 am

To update a column (or a list of columns), you have to use the classic qx::dao::update() function.

ok, I am doing this now using the generated column_XYZ methods to retreive the columns names. However, I also have one N->1 relationship, for which there does not exist such a method. Is there any more elegant way of querying the foreign key column other than hard-coding it?

also, when using the update() method, I am seeing an update to both the column passed as parameter AND the id column, as shown below. The id, being the primary key, is the last column I would expect an update for
Code: Select all
qx::dao::update(this, 0, QStringList(column_MilkYieldPerCowYear()));

[QxOrm] sql query (15 ms) : UPDATE t_ProjectDefaults SET id = :id, MilkYieldPerCowYear = :MilkYieldPerCowYear WHERE id = :id_bis


thanks,
Christian
Last edited by doulos on Fri Aug 29, 2014 8:08 am, edited 1 time in total.
doulos
 
Posts: 15
Joined: Mon Aug 11, 2014 1:24 pm

Re: qx::dao::update_optimized ??

Postby qxorm » Fri Aug 29, 2014 7:54 am

I also have one N->1 relationship, for which there does not exist such a method. Is there any more elegant way of querying the foreign key column other than hard-coding it?

What do you mean by hard-coding it ?
You have the qx::dao::update_with_relation() function where you can choose wich relation to update.
qxorm
Site Admin
 
Posts: 481
Joined: Mon Apr 12, 2010 7:45 am

Re: qx::dao::update_optimized ??

Postby doulos » Fri Aug 29, 2014 8:15 am

What do you mean by hard-coding it ?


this is not hard-coded, i.e. when mapping changes, it is transparent or immediately visible by compile error
Code: Select all
qx::dao::update(this, 0, QStringList(column_PercentWageIncreasePerYear()));

here the relationship name is hard coded, and thus susceptible to intransparent mapping changes (if the relationship name is changed)
Code: Select all
qx::dao::update_with_relation(QStringList("Country"), this);

never mind, not that much of an issue as long as "Country" is the relationship name and not the column name. Please have a look at my previous question regarding the update to the id columns

thanks,
C.
doulos
 
Posts: 15
Joined: Mon Aug 11, 2014 1:24 pm

Re: qx::dao::update_optimized ??

Postby qxorm » Fri Aug 29, 2014 9:07 am

here the relationship name is hard coded, and thus susceptible to intransparent mapping changes (if the relationship name is changed)

Like column_XXX() static methods to get the column name of a property in a generated class, you have also relation_XXX() static methods for relationships.

also, when using the update() method, I am seeing an update to both the column passed as parameter AND the id column, as shown below. The id, being the primary key, is the last column I would expect an update for

Try to disable this option (by default it is 'true') :
Code: Select all
qx::QxSqlDatabase::getSingleton()->setAddAutoIncrementIdToUpdateQuery(false);

And here the comment : For Microsoft SqlServer database compatibility : add or not auto-increment id to SQL update query
qxorm
Site Admin
 
Posts: 481
Joined: Mon Apr 12, 2010 7:45 am


Return to QxOrm - Help

Who is online

Users browsing this forum: No registered users and 4 guests

cron