ExpressionToApply

Posts   
 
    
Rogelio
User
Posts: 221
Joined: 29-Mar-2005
# Posted on: 06-Apr-2005 06:54:36   

Frans,

I am applying a Expression (field + 1) to a entity's field, I would like to know how the value is refetched when I specify True for refetch during the adapter.SaveEntity

Before the update the field = 5, after update it is 6. What value is refetched 6 or 7?

Rogelio
User
Posts: 221
Joined: 29-Mar-2005
# Posted on: 06-Apr-2005 07:10:02   

Frans,

About the above message. Does LLBLGen Pro updated a record that only has a Expression assigned to one field, and none field ha changed its value?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39621
Joined: 17-Aug-2003
# Posted on: 06-Apr-2005 10:11:11   

Rogelio wrote:

Frans,

About the above message. Does LLBLGen Pro updated a record that only has a Expression assigned to one field, and none field ha changed its value?

LLBLGen Pro updates fields which are changed and/or have an expression applied to them. It then executes an update query, and after that query has succeeded it eventually executes a refetch query if that's necessary, which means that it executes a single row select.

If you call UpdateEntitiesDirectly() it doesn't perform refetches. What's exactly the code you executed?

Frans Bouma | Lead developer LLBLGen Pro
Rogelio
User
Posts: 221
Joined: 29-Mar-2005
# Posted on: 06-Apr-2005 13:29:36   

Otis wrote:

Rogelio wrote:

Frans,

About the above message. Does LLBLGen Pro updated a record that only has a Expression assigned to one field, and none field ha changed its value?

LLBLGen Pro updates fields which are changed and/or have an expression applied to them. It then executes an update query, and after that query has succeeded it eventually executes a refetch query if that's necessary, which means that it executes a single row select.

If you call UpdateEntitiesDirectly() it doesn't perform refetches. What's exactly the code you executed?

Ok, to make things clear, I have a field called Quantity and I applied a expression = Quantity + 1, before the update the field Quantity = 5, after the update it would be Quantity = 6. I would like to know the Select that is executed: Select Quantity or Select (Quantity + 1) as Quantity ?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39621
Joined: 17-Aug-2003
# Posted on: 06-Apr-2005 13:53:05   

If you added an expression to a field of an entity you're saving, the fetch will use the expression again indeed... Is this the case? How exactly did you execute the expression?

Frans Bouma | Lead developer LLBLGen Pro
Rogelio
User
Posts: 221
Joined: 29-Mar-2005
# Posted on: 06-Apr-2005 14:42:54   

Otis wrote:

If you added an expression to a field of an entity you're saving, the fetch will use the expression again indeed... Is this the case? How exactly did you execute the expression?

I have a Inventory system where I want to update the Entity's StockQuantity with a expression (StockQuantity + value); after the update, I need to fetch the current value of StockQuantity. By your above answer, it looks that can not use the refetch parameter of the DataAccessAdapter, I will need to execute a FetchEntity with the StockQuantity's ExpressionToApply = nothing.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39621
Joined: 17-Aug-2003
# Posted on: 06-Apr-2005 16:44:55   

Rogelio wrote:

Otis wrote:

If you added an expression to a field of an entity you're saving, the fetch will use the expression again indeed... Is this the case? How exactly did you execute the expression?

I have a Inventory system where I want to update the Entity's StockQuantity with a expression (StockQuantity + value); after the update, I need to fetch the current value of StockQuantity. By your above answer, it looks that can not use the refetch parameter of the DataAccessAdapter, I will need to execute a FetchEntity with the StockQuantity's ExpressionToApply = nothing.

Indeed. What I found a bit strange is that you follow this path. As I understand you do something like the following:


Dim inventoryElement As New InventoryItemEntity(id)
adapter.FetchEntity(inventoryElement)

Dim exp As New Expression(EntityFieldFactory.Create(InventoryItemFieldIndex.StockQuantity), ExOp.Add, value)
inventoryElement.Fields("StockQuantity").ExpressionToApply = exp

adapter.SaveEntity(inventoryElement)

but, why not just do:


Dim inventoryElement As New InventoryItemEntity(id)
adapter.FetchEntity(inventoryElement)
inventoryElement.StockQuantity += value
adapter.SaveEntity(inventoryElement)

?

Frans Bouma | Lead developer LLBLGen Pro
Rogelio
User
Posts: 221
Joined: 29-Mar-2005
# Posted on: 07-Apr-2005 00:25:56   

Hi,

I do not use:

Dim inventoryElement As New InventoryItemEntity(id)
adapter.FetchEntity(inventoryElement)
inventoryElement.StockQuantity += value
adapter.SaveEntity(inventoryElement)

Because the system has high volume of transactions for inventoryElement, and in the time between fetching and saving, other user could have changed inventoryElement.StockQuantity. The safe way to go, without a concurrency predicate, is:

Update ... Set inventoryElement.StockQuantity = inventoryElement.StockQuantity +value

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39621
Joined: 17-Aug-2003
# Posted on: 07-Apr-2005 10:29:22   

Rogelio wrote:

Hi,

I do not use:

Dim inventoryElement As New InventoryItemEntity(id)
adapter.FetchEntity(inventoryElement)
inventoryElement.StockQuantity += value
adapter.SaveEntity(inventoryElement)

Because the system has high volume of transactions for inventoryElement, and in the time between fetching and saving, other user could have changed inventoryElement.StockQuantity. The safe way to go, without a concurrency predicate, is:

Update ... Set inventoryElement.StockQuantity = inventoryElement.StockQuantity +value

Good point, I didn't think of that. To update the stock directly, you could also opt for doing that directly using UpdateEntitiesDirectly(), and then simply do a normal fetch using a new entity instance. This would make the code you have to write a bit more clean I think.

Frans Bouma | Lead developer LLBLGen Pro
Marcus avatar
Marcus
User
Posts: 747
Joined: 23-Apr-2004
# Posted on: 08-Nov-2005 10:49:47   
Update ... Set inventoryElement.StockQuantity = inventoryElement.StockQuantity +value

Frans do you have a C# example of this kind of Expression for use with UpdateEntitiesDirectly?

[edit]Its okay I found it:

// C#
IExpression updateExpression = new Expression(
    EmployeeFields.Salary, ExOp.Add,
    new Expression(EmployeeFields.Salary, ExOp.Mul, 0.1f)));
EmployeeEntity employee = new EmployeeEntity();
employee.Fields[(int)EmployeeFieldIndex.Salary].ExpressionToApply = updateExpression;
DataAccessAdapter adapter = new DataAccessAdapter();
// no filter is specified, everybody gets 10% extra, but you could of course
// specify a filter to limit the scope of the update.
adapter.UpdateEntitiesDirectly(employee, null);

// the expression declaration can also be done like this:
EmployeeEntity employee = new EmployeeEntity();
employee.Fields[(int)EmployeeFieldIndex.Salary].ExpressionToApply = 
    (EmployeeFields.Salary + (EmployeeFields.Salary * 0.01f));
Marcus avatar
Marcus
User
Posts: 747
Joined: 23-Apr-2004
# Posted on: 08-Nov-2005 11:01:57   

The little green box is gone!!! I can't remove this post from you active threads list... simple_smile

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39621
Joined: 17-Aug-2003
# Posted on: 08-Nov-2005 11:07:49   

Marcus wrote:

The little green box is gone!!! I can't remove this post from you active threads list... simple_smile

Let me do that for you smile

Frans Bouma | Lead developer LLBLGen Pro