auto increment using jscript function in orientDB (NewBie)

53 views
Skip to first unread message

Shivanandan Gupta

unread,
Feb 11, 2015, 4:44:48 AM2/11/15
to orient-...@googlegroups.com
Hi All,

I have started working on orient db 2 days ago , I am trying to create a jscript function using which I am reading a counter table to get the latest counter value and then increment it with 1 and have to update back the counter table with the latest value.

I tried a code given below( Its not working as expected ):

var db = orient.getDatabase();
var currid = db.query("select from AMcounter");
var incrid = currid + 1;
if( currid == null ){
  response.send(404, "currid name not found", "text/plain", "Error: currid name not found" );
} else {

  db.begin();
  try{
    var result = db.save({ "@class" : "AMcounter", counter : incrid });
    db.commit();
    return result;
  }catch ( err ){
    db.rollback();
    response.send(500, "Error on creating new user", "text/plain", err.toString() );
  }
}




Please help me if you know the solution for this. Really appreciate your help. Thanks in advance.

Shivanandan Gupta

unread,
Feb 16, 2015, 3:30:54 AM2/16/15
to orient-...@googlegroups.com
I got the solution for this, I have created a vertex counterAM   with property counter and initiated it with value 0. then create the below given functions in orientdb one javascript and another sql funtion.

run the sql on command prompt or on the browser.

select jf_autogen("counterAM ")

Created a Java function ( this is generic function which you have use to call different counter tables , you just have to pass table name , this java function calls the sql funtion with the table name and it delete the older record and insert the incremented record.


jf_autogen()
var db = orient.getDatabase();
var incrid = db.query("select sum(count,1) as count from "+tname+"");

db.begin();
for(var i=0; i<incrid.length; i++){
  var c = incrid[i];
  var delid = c.field("count");
  var delincr= db.query("select sf_delete"+tname+"("+delid+")");
  
  db.save({
    "@class":""+tname+"",
    count:  c.field("count")
  });
}
db.commit();
return delid;





Create a SQL function ( which delete older record from the counter table)

delete vertex counterAM where count <> $vcount.asString() 
Reply all
Reply to author
Forward
0 new messages