Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Replace strings by list in query generation

  • Loading branch information...
commit 6a494e9023def7d0db39d0e64aa856c28e6f433d 1 parent 3c5abb4
@ckroxigor ckroxigor authored
Showing with 144 additions and 27 deletions.
  1. +98 −25 eval.ml
  2. +46 −1 examples.lang
  3. +0 −1  main.ml
View
123 eval.ml
@@ -19,7 +19,7 @@ let store_schema (conn:Postgresql.connection) res =
| Tuples_ok ->
for tuple = 0 to res#ntuples - 1 do
let field = 1 in
- let table_name = (res#getvalue tuple 0) in
+ let table_name = String.lowercase (res#getvalue tuple 0) in
let value = (res#getvalue tuple field) in
begin
try
@@ -36,18 +36,34 @@ let store_schema (conn:Postgresql.connection) res =
| _ -> failwith "error: unexpected status"
;;
-(* Iterate over the result *)
+(* Iterate over the schema result *)
let rec load_res_schema (conn:Postgresql.connection) =
match conn#get_result with
| Some res -> store_schema conn res; flush stdout; load_res_schema conn
| None -> ()
;;
+(*Given a table and column number form 0 to n-1, returns the name of the column*)
+let get_column_name table pos =
+ let t = String.lowercase table in
+ begin
+ try
+ let cols = Hashtbl.find hash_schema t in
+ List.nth cols pos;
+ with Not_found ->
+ ""
+ end;;
-let test_sql (conn:Postgresql.connection) =
- conn#send_query "aaaa";;
-
-(*val test_sql : Postgresql.connection -> bool = <fun>*)
+(*Returns the ordered list of columns for the given table*)
+let get_column table =
+ let t = String.lowercase table in
+ printf "Call get_column with table: %s\n" t;
+ begin
+ try
+ Hashtbl.find hash_schema t;
+ with Not_found ->
+ [""]; (*Add error management: this table doesn't exists*)
+ end;;
(* get the query expression *)
let get_query e = match e with
@@ -109,48 +125,103 @@ let is_prog e = is_single e && ( let qname =
List.mem qname (get_idb's e) )
;;
+(*Returns the first non-empty element of the list*)
+let rec search_non_empty_list l pos =
+ if pos = List.length l then []
+ else if (List.nth l pos) = [] then
+ search_non_empty_list l (pos+1)
+ else List.nth l pos
+;;
+
+(*Create a list given the set of implication of the rule*)
+let rec prepare_table_attributes l pos =
+ if (pos+1) = List.length l then [get_predname(List.nth l pos); (String.concat ", " (get_varlist (List.nth l pos)))]
+ else List.append (prepare_table_attributes l (pos+1)) ([get_predname(List.nth l pos); (String.concat ", " (get_varlist (List.nth l pos)))])
+;;
+
(* get relation definition *)
-let get_rel e name = match e with
+let get_rel e name =
+ let l = match e with
| Prog sttl -> ( List.fold_right (fun s acc -> s::acc) (List.map (fun sl ->
match sl with
- | Rule (r, t) -> if ((get_predname (Rel r)) = name) then (
- String.concat ":" [get_predname (List.hd t);
- (String.concat ", " (get_varlist (List.hd t)));]) else ""
+ | Rule (r, t) -> if ((get_predname (Rel r)) = name) then (prepare_table_attributes t 0)
+ else []
| _ -> invalid_arg "get_idb"
) (List.filter (fun r -> match r with
| Rule (_, _) -> true
| _ -> false) sttl)) []
- )
+ ) in
+ search_non_empty_list l 0
+;;
+
+(*Get head attribute list*)
+let get_head_attibutes e name =
+ let l = match e with
+ | Prog sttl -> ( List.fold_right (fun s acc -> s::acc) (List.map (fun sl ->
+ match sl with
+ | Rule (r, t) -> if ((get_predname (Rel r)) = name) then get_varlist (Rel r)
+ else []
+ | _ -> invalid_arg "get_idb"
+ ) (List.filter (fun r -> match r with
+ | Rule (_, _) -> true
+ | _ -> false) sttl)) []
+ ) in
+ search_non_empty_list l 0
;;
+(*Merges the content of two columns*)
+let rec merge_names l ll pos =
+ let llname = (if pos >= List.length ll then
+ (String.concat "" ["_unused_"; string_of_int pos])
+ else
+ List.nth ll pos) in
+ if pos == List.length l - 1 then
+ (String.concat " " [
+ List.nth l pos;
+ "AS";
+ llname;
+ ])
+ else
+ (String.concat " " [
+ List.nth l pos;
+ "AS";
+ llname;
+ ",";
+ merge_names l ll (pos+1);
+ ]);;
+
(* Subquery generator *)
-let rec get_subquery e name =
+let rec get_subquery e name col_names =
Printf.printf "Call %s\n" name; flush stdout;
+ let head_cols = get_head_attibutes e name in
+ Printf.printf "Head columns(%i): %s\n" (List.length head_cols) (String.concat ", " head_cols);
let l = get_rel e name in
- let newname = (String.concat "" l) in
+ let newname = (String.concat ":" l) in
Printf.printf "List (%i): %s\n" (List.length l) newname;
flush stdout;
if (newname = "") then
- name
+ (*It's an edb*)
+ (String.concat " " [
+ "(SELECT";
+ (merge_names (get_column name) (Str.split (Str.regexp ",") col_names) 0);
+ "FROM ";
+ name;
+ ")";
+ "AS foo";
+ ])
else
+ let new_cols = (List.nth (Str.split (Str.regexp ":") newname) 1) in
(String.concat " " [
"(SELECT";
- (List.hd (List.rev (Str.split (Str.regexp ":") newname)));
+ (merge_names head_cols (Str.split (Str.regexp ",") col_names) 0);
"FROM ";
- ( get_subquery e (List.hd (Str.split (Str.regexp ":") newname)));
+ ( get_subquery e (List.hd (Str.split (Str.regexp ":") newname)) new_cols);
")";
"AS foo";
]);
;;
-let get_column_name table pos =
- begin
- try
- let cols = Hashtbl.find hash_schema table in
- List.nth cols pos;
- with Not_found ->
- ""
- end;;
+
(* generate SQL statement from the ast *)
@@ -165,7 +236,9 @@ let sql_stt e = String.concat ""
(String.concat ", " (get_idb's e));
"----";*)
( get_subquery e (get_predname(match (get_query e) with
- Query p -> Rel p | _ -> invalid_arg "is_prog")));
+ Query p -> Rel p | _ -> invalid_arg "is_prog"))
+ (String.concat ", " (get_varlist(match (get_query e) with
+ Query p -> Rel p | _ -> invalid_arg "is_prog"))));
]
;;
View
47 examples.lang
@@ -22,6 +22,10 @@ Q(id,descr) :- Test(id,descr).
?-Q(descr, id).
/
+Q(descr,id) :- Test(id,descr).
+?-Q(id).
+/
+
Q(id,descr) :- Test(id,descr).
?-Q(id).
/
@@ -31,5 +35,46 @@ Q(id,descr) :- Test(id,descr).
/
Q(id,descr) :- Test(id,descr).
-?-Q(descr, id, descr).
+?-Q(descr, id).
+/
+
+Q(y,x) :- Test(x,y).
+?-Q(a,b).
+/
+
+Q(x,y) :- Test(y,x).
+R(x,x) :- Q(x,y).
+?-R(a,b).
+/
+
+Q(x,y) :- Test(y,x).
+R(y,x) :- Q(y,x).
+?-R(a,b).
+/
+
+Q(x,y) :- Test(x,y).
+?-Q(a,b).
+/
+
+Q(x,y) :- Test(x,y).
+?-Q(a,b).
+/
+Q(x,y) :- Test(x,y).
+?-Q(b).
+/
+
+Q(x,y) :- Test(x,y) and x=1.
+?-Q(b,c).
+/
+
+Q(x) :- Test(x,1).
+?-Q(b).
+/
+
+Q(x) :- Test(x,y) and Test(x,e).
+?-Q(b).
+/
+
+Q(x) :- Test(x,x).
+?-Q(b).
/
View
1  main.ml
@@ -73,7 +73,6 @@ let main () =
c#set_notice_processor (fun s -> eprintf "postgresql error [%s]\n" s);
c#send_query sql_schema_query;
Eval.load_res_schema c;
- printf "Hi!!%s\n" (Eval.get_column_name "test" 0);
try
let lexbuf = Lexing.from_channel stdin in
while true do
Please sign in to comment.
Something went wrong with that request. Please try again.