Auto-Increment IDs

10 views
Skip to first unread message

cyx

unread,
Aug 23, 2011, 1:23:44 PM8/23/11
to bandicoot
Hi,

I was just wondering if there's already an established pattern for
doing
auto-increment IDs with relations.

Thanks in advance!

Regards,
cyx

Julius Chrobak

unread,
Aug 24, 2011, 1:22:39 PM8/24/11
to bandicoot
there is a simple pattern you can use to generate numeric auto-
increment values. However, in bandicoot you can only do it on a set
level.

Let's assume we have the following program:
-------------------------------------------------------------
rel Data {
id: int,
name: string
}

data: Data;

fn save(d: rel {name: string}): Data
{
tmp := data summary(m = max(id, 0)) extend(id = m + 1);
new := data + (d * tmp) project(id, name);
data = new;

return new;
}
-------------------------------------------------------------
the "save" function takes a set of new names as an input parameter. It
generates a new value for the "id" attribute as an increment to the
current maximum. However, if the input variable contains several
tuples they all are going to have the same value for the "id":

1) empty request:

$ echo -e 'name:string' | curl --data-binary @- http://localhost:12345/save
id:int,name:string


2) add two records in two separate calls:

$ echo -e 'name:string\nAlan' | curl --data-binary @- http://localhost:12345/save
id:int,name:string
1,Alan
$ echo -e 'name:string\nJohn' | curl --data-binary @- http://localhost:12345/save
id:int,name:string
1,Alan
2,John


3) adding two records in one call:

$ echo -e 'name:string\nAnna\nJane' | curl --data-binary @-
http://localhost:12345/save
id:int,name:string
1,Alan
2,John
3,Anna
3,Jane


To achieve uniqueness you could have a compound key of two attributes.
One would be provided as a unique key with in a call by client and the
other generated by bandicoot automatically e.g.:
-------------------------------------------------------------
rel Data {
rid: int,
gid: int,
name: string
}

data: Data;

fn save(d: rel {rid: int, name: string}): Data
{
tmp := data summary(m = max(gid, 0)) extend(gid = m + 1);
new := data + (d * tmp) project(rid, gid, name);
data = new;

return new;
}
-------------------------------------------------------------
and the same actions:

$ echo -e 'rid:int,name:string' | curl --data-binary @- http://localhost:12345/save
gid:int,name:string,rid:int

$ echo -e 'rid:int,name:string\n1,Alan' | curl --data-binary @-
http://localhost:12345/save
gid:int,name:string,rid:int
1,Alan,1

$ echo -e 'rid:int,name:string\n1,John' | curl --data-binary @-
http://localhost:12345/save
gid:int,name:string,rid:int
1,Alan,1
2,John,1

$ echo -e 'rid:int,name:string\n1,Anna\n2,Jane' | curl --data-binary
@- http://localhost:12345/save
gid:int,name:string,rid:int
1,Alan,1
2,John,1
3,Anna,1
3,Jane,2




I hope this helps.

regards,
Julius
Reply all
Reply to author
Forward
0 new messages