0

I'd like to know if a record is present in a database, using a field different from the key field.

I've try the following code :

function start()
{
  jlog("start db query")
  myType d1 = {A:"rabbit", B:"poney"};
  /myDataBase/data[A == d1.A] = d1
  jlog("db write done")
   option opt = ?/myDataBase/data[B == "rabit"]
  jlog("db query done")

  match(opt)
  {
   case {none} : <>Nothing in db</>
   case {some:data} : <>{data} in database</>
  }
}

Server.start(
   {port:8092, netmask:0.0.0.0, encryption: {no_encryption}, name:"test"},
   [
     {page: start, title: "test" }
   ]
)

But the server hang up, and never get to the line jlog("db query done"). I mispell "rabit" willingly. What should I've done ?

Thanks

user984846
  • 263
  • 2
  • 8

1 Answers1

0

Indeed it fails with your example, I have a "Match failure 8859742" exception, don't you see it?

But do not use ?/myDataBase/data[B == "rabit"] (which should have been rejected at compile time - bug report sent) but /myDataBase/data[B == "rabit"] which is a DbSet. The reason is when you don't use a primary key, then you can have more than one value in return, ie a set of values.

You can convert a dbset to an iter with DbSet.iterator. Then manipulate it with Iter: http://doc.opalang.org/module/stdlib.core.iter/Iter

Cédrics
  • 1,974
  • 11
  • 13