Conditional insert

4 views
Skip to first unread message

Michel Martens

unread,
Jul 22, 2011, 4:37:44 PM7/22/11
to band...@googlegroups.com
Hello again,

I have this:

rel Items {
id: int,
pid: int,
ts: long,
}

And this data:

id:int,pid:int,ts:long
1,38961,1310084883

I want to append a new item but only if the id is not in the database
yet. For example, if I send this information:

id:int,pid:int,ts:long
1,0,1310085367

I want the database to remain unaltered. If the database doesn't
contain a tuple with id == 1, then I want the new information to be
added to the database.

Is that possible?

Thanks.

Ostap Cherkashin

unread,
Jul 23, 2011, 11:08:27 AM7/23/11
to band...@googlegroups.com
Hi Michael

You can achieve this with the help of the minus operator. First, you can
remove the items which are already stored from the incoming dataset, and
then append the remainings to your global variable (store in the example
below):

---
store: Items;

fn AppendNewItems(i: Items)
{
newItems := i - store project(id);
store += newItems;
}
---

Hope this helps.

Regards
Ostap

Michel Martens

unread,
Jul 23, 2011, 11:23:21 AM7/23/11
to band...@googlegroups.com
On Sat, Jul 23, 2011 at 12:08 PM, Ostap Cherkashin <os...@bandilab.org> wrote:
> Hi Michael
>
> You can achieve this with the help of the minus operator. First, you can
> remove the items which are already stored from the incoming dataset, and
> then append the remainings to your global variable (store in the example
> below):
>
> ---
> store: Items;
>
> fn AppendNewItems(i: Items)
> {
>  newItems := i - store project(id);
>  store += newItems;
> }
> ---
>
> Hope this helps.

That was super elegant, thanks a lot!

I had found a terrible solution for a very specific case of this
general problem, and I'm delighted such a simple solution exists.
Again, thanks :-)

Reply all
Reply to author
Forward
0 new messages