SubSonic-3.0 icon indicating copy to clipboard operation
SubSonic-3.0 copied to clipboard

Batch update causes error in SQLite

Open yjagota opened this issue 16 years ago • 3 comments

I am using SS 3.0.0.3 ActiveRecord (Current master build) with SQLite (I think this error might be reproducable in other databases also, although I didn't checked).

If I do something like this: var customerRepo = Customer.GetRepo(); var customerList = customerRepo.GetAll().ToList(); And then after making changes to the list, try to do this: customerRepo.Update(customerList); It causes a SQLite error because if the customer is not changed (i.e. IsDirty flag is false) the sql is not empty but this: UPDATE Customers WHERE Id = @0; UPDATE Customers WHERE Id = @1; If a check for IsDirty flag is added to method public int Update(IEnumerable<T> items, IDataProvider provider) at line 279 of SubSonic.Core\Repository\SubSonicRepository.cs, the error is resolved.

The resultant public int Update(IEnumerable<T> items, IDataProvider provider) method should be: public int Update(IEnumerable<T> items, IDataProvider provider) { BatchQuery bQuery = new BatchQuery(provider); int result = 0;

        foreach(T item in items)
        {
            if (((IActiveRecord)item).IsDirty()) // adding this check fixes the problem...
            {
                var query = item.ToUpdateQuery(provider);
                bQuery.Queue(query);
            }
        }
        result = bQuery.Execute();
        return result;
    }

yjagota avatar Oct 16 '09 19:10 yjagota

You can workaround this pretty simply:

customerRepo.Update(customerList.Where(customer => customer.IsDirty());

adam7 avatar Mar 10 '10 20:03 adam7

But it is still an error, isn't it? Or is was meant to be this way only?

yjagota avatar Apr 12 '10 05:04 yjagota

Yep still an error, just a low priority one. Your fix would break batch updates for SimpleRepo and the Linq templates though.

adam7 avatar Apr 12 '10 18:04 adam7