Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

add in a /contact interface to retrieve contact information (no servi…

…ce stuff yet)
  • Loading branch information...
commit 5bba90ff1302554ab215aceaa7b29c89226e2a9e 1 parent a154c50
Anil Madhavapeddy authored
6 lifedb_dispatch.ml
@@ -120,6 +120,12 @@ let dispatch (lifedb : Lifedb_schema.Init.t) (syncdb : Sync_schema.Init.t) env (
120 120 |(`GET|`HEAD), "doc" ->
121 121 mark_get_rpc cgi;
122 122 Lifedb_query.dispatch lifedb syncdb env cgi (`Doc (List.nth url_list 1))
  123 + |(`GET|`HEAD), "contact" -> begin
  124 + match url_list with
  125 + |[_] -> Lifedb_query.dispatch lifedb syncdb env cgi `Contact_list
  126 + |[_;uid] -> Lifedb_query.dispatch lifedb syncdb env cgi (`Contact_get uid)
  127 + |_ -> raise (Lifedb_rpc.Invalid_rpc "need 0/1 args")
  128 + end
123 129 |(`GET|`HEAD), "att" ->
124 130 mark_get_rpc cgi;
125 131 Lifedb_query.dispatch lifedb syncdb env cgi (`Att (List.nth url_list 1))
12 lifedb_query.ml
@@ -177,5 +177,15 @@ let dispatch lifedb syncdb env (cgi:Netcgi.cgi_activation) = function
177 177 let contacts = results_of_search (unique (fun x y -> x#uid <> y#uid) contacts) in
178 178 Lifedb_rpc.return_json cgi (Query.json_of_contacts contacts)
179 179 end
180   - |_ -> Lifedb_rpc.return_error cgi `Not_found "bad mode" "bad mode"
  180 + |_ -> Lifedb_rpc.return_error cgi `Not_found "bad mode" "bad mode"
181 181 end
  182 + |`Contact_list ->
  183 + let cs :> Query.contact list = LS.Contact.get lifedb in
  184 + let contacts = results_of_search cs in
  185 + Lifedb_rpc.return_json cgi (Query.json_of_contacts contacts)
  186 + |`Contact_get uid -> begin
  187 + match LS.Contact.get_by_uid uid lifedb with
  188 + |[c] -> Lifedb_rpc.return_json cgi (Query.json_of_contact (c :> Query.contact))
  189 + |_ -> raise (Lifedb_rpc.Resource_not_found uid)
  190 + end
  191 +
52 lifedb_schema.ml
@@ -516,6 +516,58 @@ module Contact = struct
516 516 (* execute the SQL query *)
517 517 step_fold db stmt of_stmt
518 518
  519 + let get_by_uid ~uid ?(custom_where=("",[])) db =
  520 + let q = "WHERE contact.uid=?" in
  521 + let q = match custom_where with |"",_ -> q |w,_ -> q ^ " AND (" ^ w ^ ")" in
  522 + let sql="SELECT contact.id, contact.file_name, contact.uid, contact.first_name, contact.last_name, contact.mtime FROM contact " ^ q in
  523 + let stmt=Sqlite3.prepare db.db sql in
  524 + db_must_ok db (fun () -> let v = uid in Sqlite3.bind stmt 1 (Sqlite3.Data.TEXT v));
  525 + ignore(match custom_where with |_,[] -> () |_,eb ->
  526 + let pos = ref 2 in
  527 + List.iter (fun b ->
  528 + db_must_ok db (fun () -> Sqlite3.bind stmt !pos b);
  529 + incr pos;
  530 + ) eb);
  531 + (* convert statement into an ocaml object *)
  532 + let of_stmt stmt =
  533 + t
  534 + (* native fields *)
  535 + ~id:(
  536 + (match Sqlite3.column stmt 0 with
  537 + |Sqlite3.Data.NULL -> None
  538 + |x -> Some (match x with |Sqlite3.Data.INT i -> i |x -> (try Int64.of_string (Sqlite3.Data.to_string x) with _ -> failwith "error: contact id")))
  539 + )
  540 + ~file_name:(
  541 + (match Sqlite3.column stmt 1 with
  542 + |Sqlite3.Data.NULL -> failwith "null of_stmt"
  543 + |x -> Sqlite3.Data.to_string x)
  544 + )
  545 + ~uid:(
  546 + (match Sqlite3.column stmt 2 with
  547 + |Sqlite3.Data.NULL -> failwith "null of_stmt"
  548 + |x -> Sqlite3.Data.to_string x)
  549 + )
  550 + ~first_name:(
  551 + (match Sqlite3.column stmt 3 with
  552 + |Sqlite3.Data.NULL -> None
  553 + |x -> Some (Sqlite3.Data.to_string x))
  554 + )
  555 + ~last_name:(
  556 + (match Sqlite3.column stmt 4 with
  557 + |Sqlite3.Data.NULL -> None
  558 + |x -> Some (Sqlite3.Data.to_string x))
  559 + )
  560 + ~mtime:(
  561 + (match Sqlite3.column stmt 5 with
  562 + |Sqlite3.Data.NULL -> failwith "null of_stmt"
  563 + |x -> match x with |Sqlite3.Data.FLOAT i -> i|x -> (try float_of_string (Sqlite3.Data.to_string x) with _ -> failwith "error: contact mtime"))
  564 + )
  565 + (* foreign fields *)
  566 + db
  567 + in
  568 + (* execute the SQL query *)
  569 + step_fold db stmt of_stmt
  570 +
519 571 end
520 572
521 573 module Mtype = struct
5 lifedb_schema.mli
@@ -107,6 +107,11 @@ module Contact : sig
107 107 @raise Sql_error if a database error is encountered
108 108 *)
109 109
  110 + val get_by_uid :
  111 + uid:string ->
  112 + ?custom_where:string * Sqlite3.Data.t list -> Init.t ->
  113 + t list
  114 +
110 115 end
111 116 module Mtype : sig
112 117 type t = <
2  lifedb_schema_generator.ml
@@ -30,7 +30,7 @@ let lifedb = make [
30 30 text ~flags:[`Optional] "first_name";
31 31 text ~flags:[`Optional] "last_name";
32 32 date "mtime";
33   - ], [], default_opts;
  33 + ], [ [], ["uid"] ], default_opts;
34 34
35 35 "mtype" , [
36 36 text ~flags:[`Unique; `Index] "name";

0 comments on commit 5bba90f

Please sign in to comment.
Something went wrong with that request. Please try again.