3

I've declared a database where the customer data is stored:

type Customer = 
{
  name : string
  email: string
  cp: int
}

db /customer : Customer

I would like to execute certain queries to work with the data such as:

Select name where id > <number>
Select * where cp IN(<number>, <number>)
Select * order by name

It is possible to execute a function which returns an Array, Map, or similar to iterate it?
How could iterate such a result?

Thanks!

Anne
  • 26,765
  • 9
  • 65
  • 71
semoru
  • 90
  • 5

1 Answers1

1

Ok, to begin with your db declaration declares a single customer, not a collection of customers, which is what you want to do, I believe. I'd suggest to declare it as a map from customer numbers to customer data as:

db /customer : intmap(Customer)

Now with customers = /customer you can get the complete collection of customers and do arbitrary processing on it. Alternatively Db.intmap_fold_range gives you a fold function on a range of keys from the collection; with that you can easily code your first query as:

names_with_ids_gt(x) =
  get_names(names, id) = [/customer[id]/name | names]
  Db.intmap_fold_range(@/customer, get_names, [], x, none, (_ -> true))

Of course getting full collection of customers and doing some processing on it will not be very efficient. For a more efficient solution you will need to use an external database and its querying capabilities. Opa support for those is coming very soon: http://blog.opalang.org/2011/11/opas-database-and-where-its-heading.html.

akoprowski
  • 3,297
  • 1
  • 18
  • 26